crypto_values.h 119 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763
  1. /**
  2. * \file psa/crypto_values.h
  3. *
  4. * \brief PSA cryptography module: macros to build and analyze integer values.
  5. *
  6. * \note This file may not be included directly. Applications must
  7. * include psa/crypto.h. Drivers must include the appropriate driver
  8. * header file.
  9. *
  10. * This file contains portable definitions of macros to build and analyze
  11. * values of integral types that encode properties of cryptographic keys,
  12. * designations of cryptographic algorithms, and error codes returned by
  13. * the library.
  14. *
  15. * Note that many of the constants defined in this file are embedded in
  16. * the persistent key store, as part of key metadata (including usage
  17. * policies). As a consequence, they must not be changed (unless the storage
  18. * format version changes).
  19. *
  20. * This header file only defines preprocessor macros.
  21. */
  22. /*
  23. * Copyright The Mbed TLS Contributors
  24. * SPDX-License-Identifier: Apache-2.0
  25. *
  26. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  27. * not use this file except in compliance with the License.
  28. * You may obtain a copy of the License at
  29. *
  30. * http://www.apache.org/licenses/LICENSE-2.0
  31. *
  32. * Unless required by applicable law or agreed to in writing, software
  33. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  34. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  35. * See the License for the specific language governing permissions and
  36. * limitations under the License.
  37. */
  38. #ifndef PSA_CRYPTO_VALUES_H
  39. #define PSA_CRYPTO_VALUES_H
  40. #include "mbedtls/private_access.h"
  41. /** \defgroup error Error codes
  42. * @{
  43. */
  44. /* PSA error codes */
  45. /* Error codes are standardized across PSA domains (framework, crypto, storage,
  46. * etc.). Do not change the values in this section or even the expansions
  47. * of each macro: it must be possible to `#include` both this header
  48. * and some other PSA component's headers in the same C source,
  49. * which will lead to duplicate definitions of the `PSA_SUCCESS` and
  50. * `PSA_ERROR_xxx` macros, which is ok if and only if the macros expand
  51. * to the same sequence of tokens.
  52. *
  53. * If you must add a new
  54. * value, check with the Arm PSA framework group to pick one that other
  55. * domains aren't already using. */
  56. /* Tell uncrustify not to touch the constant definitions, otherwise
  57. * it might change the spacing to something that is not PSA-compliant
  58. * (e.g. adding a space after casts).
  59. *
  60. * *INDENT-OFF*
  61. */
  62. /** The action was completed successfully. */
  63. #define PSA_SUCCESS ((psa_status_t)0)
  64. /** An error occurred that does not correspond to any defined
  65. * failure cause.
  66. *
  67. * Implementations may use this error code if none of the other standard
  68. * error codes are applicable. */
  69. #define PSA_ERROR_GENERIC_ERROR ((psa_status_t)-132)
  70. /** The requested operation or a parameter is not supported
  71. * by this implementation.
  72. *
  73. * Implementations should return this error code when an enumeration
  74. * parameter such as a key type, algorithm, etc. is not recognized.
  75. * If a combination of parameters is recognized and identified as
  76. * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
  77. #define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)-134)
  78. /** The requested action is denied by a policy.
  79. *
  80. * Implementations should return this error code when the parameters
  81. * are recognized as valid and supported, and a policy explicitly
  82. * denies the requested operation.
  83. *
  84. * If a subset of the parameters of a function call identify a
  85. * forbidden operation, and another subset of the parameters are
  86. * not valid or not supported, it is unspecified whether the function
  87. * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
  88. * #PSA_ERROR_INVALID_ARGUMENT. */
  89. #define PSA_ERROR_NOT_PERMITTED ((psa_status_t)-133)
  90. /** An output buffer is too small.
  91. *
  92. * Applications can call the \c PSA_xxx_SIZE macro listed in the function
  93. * description to determine a sufficient buffer size.
  94. *
  95. * Implementations should preferably return this error code only
  96. * in cases when performing the operation with a larger output
  97. * buffer would succeed. However implementations may return this
  98. * error if a function has invalid or unsupported parameters in addition
  99. * to the parameters that determine the necessary output buffer size. */
  100. #define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)-138)
  101. /** Asking for an item that already exists
  102. *
  103. * Implementations should return this error, when attempting
  104. * to write an item (like a key) that already exists. */
  105. #define PSA_ERROR_ALREADY_EXISTS ((psa_status_t)-139)
  106. /** Asking for an item that doesn't exist
  107. *
  108. * Implementations should return this error, if a requested item (like
  109. * a key) does not exist. */
  110. #define PSA_ERROR_DOES_NOT_EXIST ((psa_status_t)-140)
  111. /** The requested action cannot be performed in the current state.
  112. *
  113. * Multipart operations return this error when one of the
  114. * functions is called out of sequence. Refer to the function
  115. * descriptions for permitted sequencing of functions.
  116. *
  117. * Implementations shall not return this error code to indicate
  118. * that a key either exists or not,
  119. * but shall instead return #PSA_ERROR_ALREADY_EXISTS or #PSA_ERROR_DOES_NOT_EXIST
  120. * as applicable.
  121. *
  122. * Implementations shall not return this error code to indicate that a
  123. * key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
  124. * instead. */
  125. #define PSA_ERROR_BAD_STATE ((psa_status_t)-137)
  126. /** The parameters passed to the function are invalid.
  127. *
  128. * Implementations may return this error any time a parameter or
  129. * combination of parameters are recognized as invalid.
  130. *
  131. * Implementations shall not return this error code to indicate that a
  132. * key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
  133. * instead.
  134. */
  135. #define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)-135)
  136. /** There is not enough runtime memory.
  137. *
  138. * If the action is carried out across multiple security realms, this
  139. * error can refer to available memory in any of the security realms. */
  140. #define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)-141)
  141. /** There is not enough persistent storage.
  142. *
  143. * Functions that modify the key storage return this error code if
  144. * there is insufficient storage space on the host media. In addition,
  145. * many functions that do not otherwise access storage may return this
  146. * error code if the implementation requires a mandatory log entry for
  147. * the requested action and the log storage space is full. */
  148. #define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)-142)
  149. /** There was a communication failure inside the implementation.
  150. *
  151. * This can indicate a communication failure between the application
  152. * and an external cryptoprocessor or between the cryptoprocessor and
  153. * an external volatile or persistent memory. A communication failure
  154. * may be transient or permanent depending on the cause.
  155. *
  156. * \warning If a function returns this error, it is undetermined
  157. * whether the requested action has completed or not. Implementations
  158. * should return #PSA_SUCCESS on successful completion whenever
  159. * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
  160. * if the requested action was completed successfully in an external
  161. * cryptoprocessor but there was a breakdown of communication before
  162. * the cryptoprocessor could report the status to the application.
  163. */
  164. #define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)-145)
  165. /** There was a storage failure that may have led to data loss.
  166. *
  167. * This error indicates that some persistent storage is corrupted.
  168. * It should not be used for a corruption of volatile memory
  169. * (use #PSA_ERROR_CORRUPTION_DETECTED), for a communication error
  170. * between the cryptoprocessor and its external storage (use
  171. * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
  172. * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
  173. *
  174. * Note that a storage failure does not indicate that any data that was
  175. * previously read is invalid. However this previously read data may no
  176. * longer be readable from storage.
  177. *
  178. * When a storage failure occurs, it is no longer possible to ensure
  179. * the global integrity of the keystore. Depending on the global
  180. * integrity guarantees offered by the implementation, access to other
  181. * data may or may not fail even if the data is still readable but
  182. * its integrity cannot be guaranteed.
  183. *
  184. * Implementations should only use this error code to report a
  185. * permanent storage corruption. However application writers should
  186. * keep in mind that transient errors while reading the storage may be
  187. * reported using this error code. */
  188. #define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)-146)
  189. /** A hardware failure was detected.
  190. *
  191. * A hardware failure may be transient or permanent depending on the
  192. * cause. */
  193. #define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)-147)
  194. /** A tampering attempt was detected.
  195. *
  196. * If an application receives this error code, there is no guarantee
  197. * that previously accessed or computed data was correct and remains
  198. * confidential. Applications should not perform any security function
  199. * and should enter a safe failure state.
  200. *
  201. * Implementations may return this error code if they detect an invalid
  202. * state that cannot happen during normal operation and that indicates
  203. * that the implementation's security guarantees no longer hold. Depending
  204. * on the implementation architecture and on its security and safety goals,
  205. * the implementation may forcibly terminate the application.
  206. *
  207. * This error code is intended as a last resort when a security breach
  208. * is detected and it is unsure whether the keystore data is still
  209. * protected. Implementations shall only return this error code
  210. * to report an alarm from a tampering detector, to indicate that
  211. * the confidentiality of stored data can no longer be guaranteed,
  212. * or to indicate that the integrity of previously returned data is now
  213. * considered compromised. Implementations shall not use this error code
  214. * to indicate a hardware failure that merely makes it impossible to
  215. * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
  216. * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
  217. * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
  218. * instead).
  219. *
  220. * This error indicates an attack against the application. Implementations
  221. * shall not return this error code as a consequence of the behavior of
  222. * the application itself. */
  223. #define PSA_ERROR_CORRUPTION_DETECTED ((psa_status_t)-151)
  224. /** There is not enough entropy to generate random data needed
  225. * for the requested action.
  226. *
  227. * This error indicates a failure of a hardware random generator.
  228. * Application writers should note that this error can be returned not
  229. * only by functions whose purpose is to generate random data, such
  230. * as key, IV or nonce generation, but also by functions that execute
  231. * an algorithm with a randomized result, as well as functions that
  232. * use randomization of intermediate computations as a countermeasure
  233. * to certain attacks.
  234. *
  235. * Implementations should avoid returning this error after psa_crypto_init()
  236. * has succeeded. Implementations should generate sufficient
  237. * entropy during initialization and subsequently use a cryptographically
  238. * secure pseudorandom generator (PRNG). However implementations may return
  239. * this error at any time if a policy requires the PRNG to be reseeded
  240. * during normal operation. */
  241. #define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)-148)
  242. /** The signature, MAC or hash is incorrect.
  243. *
  244. * Verification functions return this error if the verification
  245. * calculations completed successfully, and the value to be verified
  246. * was determined to be incorrect.
  247. *
  248. * If the value to verify has an invalid size, implementations may return
  249. * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
  250. #define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)-149)
  251. /** The decrypted padding is incorrect.
  252. *
  253. * \warning In some protocols, when decrypting data, it is essential that
  254. * the behavior of the application does not depend on whether the padding
  255. * is correct, down to precise timing. Applications should prefer
  256. * protocols that use authenticated encryption rather than plain
  257. * encryption. If the application must perform a decryption of
  258. * unauthenticated data, the application writer should take care not
  259. * to reveal whether the padding is invalid.
  260. *
  261. * Implementations should strive to make valid and invalid padding
  262. * as close as possible to indistinguishable to an external observer.
  263. * In particular, the timing of a decryption operation should not
  264. * depend on the validity of the padding. */
  265. #define PSA_ERROR_INVALID_PADDING ((psa_status_t)-150)
  266. /** Return this error when there's insufficient data when attempting
  267. * to read from a resource. */
  268. #define PSA_ERROR_INSUFFICIENT_DATA ((psa_status_t)-143)
  269. /** The key identifier is not valid. See also :ref:\`key-handles\`.
  270. */
  271. #define PSA_ERROR_INVALID_HANDLE ((psa_status_t)-136)
  272. /** Stored data has been corrupted.
  273. *
  274. * This error indicates that some persistent storage has suffered corruption.
  275. * It does not indicate the following situations, which have specific error
  276. * codes:
  277. *
  278. * - A corruption of volatile memory - use #PSA_ERROR_CORRUPTION_DETECTED.
  279. * - A communication error between the cryptoprocessor and its external
  280. * storage - use #PSA_ERROR_COMMUNICATION_FAILURE.
  281. * - When the storage is in a valid state but is full - use
  282. * #PSA_ERROR_INSUFFICIENT_STORAGE.
  283. * - When the storage fails for other reasons - use
  284. * #PSA_ERROR_STORAGE_FAILURE.
  285. * - When the stored data is not valid - use #PSA_ERROR_DATA_INVALID.
  286. *
  287. * \note A storage corruption does not indicate that any data that was
  288. * previously read is invalid. However this previously read data might no
  289. * longer be readable from storage.
  290. *
  291. * When a storage failure occurs, it is no longer possible to ensure the
  292. * global integrity of the keystore.
  293. */
  294. #define PSA_ERROR_DATA_CORRUPT ((psa_status_t)-152)
  295. /** Data read from storage is not valid for the implementation.
  296. *
  297. * This error indicates that some data read from storage does not have a valid
  298. * format. It does not indicate the following situations, which have specific
  299. * error codes:
  300. *
  301. * - When the storage or stored data is corrupted - use #PSA_ERROR_DATA_CORRUPT
  302. * - When the storage fails for other reasons - use #PSA_ERROR_STORAGE_FAILURE
  303. * - An invalid argument to the API - use #PSA_ERROR_INVALID_ARGUMENT
  304. *
  305. * This error is typically a result of either storage corruption on a
  306. * cleartext storage backend, or an attempt to read data that was
  307. * written by an incompatible version of the library.
  308. */
  309. #define PSA_ERROR_DATA_INVALID ((psa_status_t)-153)
  310. /** The function that returns this status is defined as interruptible and
  311. * still has work to do, thus the user should call the function again with the
  312. * same operation context until it either returns #PSA_SUCCESS or any other
  313. * error. This is not an error per se, more a notification of status.
  314. */
  315. #define PSA_OPERATION_INCOMPLETE ((psa_status_t)-248)
  316. /* *INDENT-ON* */
  317. /**@}*/
  318. /** \defgroup crypto_types Key and algorithm types
  319. * @{
  320. */
  321. /* Note that key type values, including ECC family and DH group values, are
  322. * embedded in the persistent key store, as part of key metadata. As a
  323. * consequence, they must not be changed (unless the storage format version
  324. * changes).
  325. */
  326. /** An invalid key type value.
  327. *
  328. * Zero is not the encoding of any key type.
  329. */
  330. #define PSA_KEY_TYPE_NONE ((psa_key_type_t) 0x0000)
  331. /** Vendor-defined key type flag.
  332. *
  333. * Key types defined by this standard will never have the
  334. * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
  335. * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
  336. * respect the bitwise structure used by standard encodings whenever practical.
  337. */
  338. #define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t) 0x8000)
  339. #define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t) 0x7000)
  340. #define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t) 0x1000)
  341. #define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t) 0x2000)
  342. #define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t) 0x4000)
  343. #define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t) 0x7000)
  344. #define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t) 0x3000)
  345. /** Whether a key type is vendor-defined.
  346. *
  347. * See also #PSA_KEY_TYPE_VENDOR_FLAG.
  348. */
  349. #define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
  350. (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
  351. /** Whether a key type is an unstructured array of bytes.
  352. *
  353. * This encompasses both symmetric keys and non-key data.
  354. */
  355. #define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \
  356. (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_RAW || \
  357. ((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
  358. /** Whether a key type is asymmetric: either a key pair or a public key. */
  359. #define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
  360. (((type) & PSA_KEY_TYPE_CATEGORY_MASK \
  361. & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \
  362. PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
  363. /** Whether a key type is the public part of a key pair. */
  364. #define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
  365. (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
  366. /** Whether a key type is a key pair containing a private part and a public
  367. * part. */
  368. #define PSA_KEY_TYPE_IS_KEY_PAIR(type) \
  369. (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR)
  370. /** The key pair type corresponding to a public key type.
  371. *
  372. * You may also pass a key pair type as \p type, it will be left unchanged.
  373. *
  374. * \param type A public key type or key pair type.
  375. *
  376. * \return The corresponding key pair type.
  377. * If \p type is not a public key or a key pair,
  378. * the return value is undefined.
  379. */
  380. #define PSA_KEY_TYPE_KEY_PAIR_OF_PUBLIC_KEY(type) \
  381. ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
  382. /** The public key type corresponding to a key pair type.
  383. *
  384. * You may also pass a key pair type as \p type, it will be left unchanged.
  385. *
  386. * \param type A public key type or key pair type.
  387. *
  388. * \return The corresponding public key type.
  389. * If \p type is not a public key or a key pair,
  390. * the return value is undefined.
  391. */
  392. #define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) \
  393. ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
  394. /** Raw data.
  395. *
  396. * A "key" of this type cannot be used for any cryptographic operation.
  397. * Applications may use this type to store arbitrary data in the keystore. */
  398. #define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t) 0x1001)
  399. /** HMAC key.
  400. *
  401. * The key policy determines which underlying hash algorithm the key can be
  402. * used for.
  403. *
  404. * HMAC keys should generally have the same size as the underlying hash.
  405. * This size can be calculated with #PSA_HASH_LENGTH(\c alg) where
  406. * \c alg is the HMAC algorithm or the underlying hash algorithm. */
  407. #define PSA_KEY_TYPE_HMAC ((psa_key_type_t) 0x1100)
  408. /** A secret for key derivation.
  409. *
  410. * This key type is for high-entropy secrets only. For low-entropy secrets,
  411. * #PSA_KEY_TYPE_PASSWORD should be used instead.
  412. *
  413. * These keys can be used as the #PSA_KEY_DERIVATION_INPUT_SECRET or
  414. * #PSA_KEY_DERIVATION_INPUT_PASSWORD input of key derivation algorithms.
  415. *
  416. * The key policy determines which key derivation algorithm the key
  417. * can be used for.
  418. */
  419. #define PSA_KEY_TYPE_DERIVE ((psa_key_type_t) 0x1200)
  420. /** A low-entropy secret for password hashing or key derivation.
  421. *
  422. * This key type is suitable for passwords and passphrases which are typically
  423. * intended to be memorizable by humans, and have a low entropy relative to
  424. * their size. It can be used for randomly generated or derived keys with
  425. * maximum or near-maximum entropy, but #PSA_KEY_TYPE_DERIVE is more suitable
  426. * for such keys. It is not suitable for passwords with extremely low entropy,
  427. * such as numerical PINs.
  428. *
  429. * These keys can be used as the #PSA_KEY_DERIVATION_INPUT_PASSWORD input of
  430. * key derivation algorithms. Algorithms that accept such an input were
  431. * designed to accept low-entropy secret and are known as password hashing or
  432. * key stretching algorithms.
  433. *
  434. * These keys cannot be used as the #PSA_KEY_DERIVATION_INPUT_SECRET input of
  435. * key derivation algorithms, as the algorithms that take such an input expect
  436. * it to be high-entropy.
  437. *
  438. * The key policy determines which key derivation algorithm the key can be
  439. * used for, among the permissible subset defined above.
  440. */
  441. #define PSA_KEY_TYPE_PASSWORD ((psa_key_type_t) 0x1203)
  442. /** A secret value that can be used to verify a password hash.
  443. *
  444. * The key policy determines which key derivation algorithm the key
  445. * can be used for, among the same permissible subset as for
  446. * #PSA_KEY_TYPE_PASSWORD.
  447. */
  448. #define PSA_KEY_TYPE_PASSWORD_HASH ((psa_key_type_t) 0x1205)
  449. /** A secret value that can be used in when computing a password hash.
  450. *
  451. * The key policy determines which key derivation algorithm the key
  452. * can be used for, among the subset of algorithms that can use pepper.
  453. */
  454. #define PSA_KEY_TYPE_PEPPER ((psa_key_type_t) 0x1206)
  455. /** Key for a cipher, AEAD or MAC algorithm based on the AES block cipher.
  456. *
  457. * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
  458. * 32 bytes (AES-256).
  459. */
  460. #define PSA_KEY_TYPE_AES ((psa_key_type_t) 0x2400)
  461. /** Key for a cipher, AEAD or MAC algorithm based on the
  462. * ARIA block cipher. */
  463. #define PSA_KEY_TYPE_ARIA ((psa_key_type_t) 0x2406)
  464. /** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
  465. *
  466. * The size of the key can be 64 bits (single DES), 128 bits (2-key 3DES) or
  467. * 192 bits (3-key 3DES).
  468. *
  469. * Note that single DES and 2-key 3DES are weak and strongly
  470. * deprecated and should only be used to decrypt legacy data. 3-key 3DES
  471. * is weak and deprecated and should only be used in legacy protocols.
  472. */
  473. #define PSA_KEY_TYPE_DES ((psa_key_type_t) 0x2301)
  474. /** Key for a cipher, AEAD or MAC algorithm based on the
  475. * Camellia block cipher. */
  476. #define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t) 0x2403)
  477. /** Key for the ChaCha20 stream cipher or the Chacha20-Poly1305 AEAD algorithm.
  478. *
  479. * ChaCha20 and the ChaCha20_Poly1305 construction are defined in RFC 7539.
  480. *
  481. * \note For ChaCha20 and ChaCha20_Poly1305, Mbed TLS only supports
  482. * 12-byte nonces.
  483. *
  484. * \note For ChaCha20, the initial counter value is 0. To encrypt or decrypt
  485. * with the initial counter value 1, you can process and discard a
  486. * 64-byte block before the real data.
  487. */
  488. #define PSA_KEY_TYPE_CHACHA20 ((psa_key_type_t) 0x2004)
  489. /** RSA public key.
  490. *
  491. * The size of an RSA key is the bit size of the modulus.
  492. */
  493. #define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t) 0x4001)
  494. /** RSA key pair (private and public key).
  495. *
  496. * The size of an RSA key is the bit size of the modulus.
  497. */
  498. #define PSA_KEY_TYPE_RSA_KEY_PAIR ((psa_key_type_t) 0x7001)
  499. /** Whether a key type is an RSA key (pair or public-only). */
  500. #define PSA_KEY_TYPE_IS_RSA(type) \
  501. (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
  502. #define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t) 0x4100)
  503. #define PSA_KEY_TYPE_ECC_KEY_PAIR_BASE ((psa_key_type_t) 0x7100)
  504. #define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t) 0x00ff)
  505. /** Elliptic curve key pair.
  506. *
  507. * The size of an elliptic curve key is the bit size associated with the curve,
  508. * i.e. the bit size of *q* for a curve over a field *F<sub>q</sub>*.
  509. * See the documentation of `PSA_ECC_FAMILY_xxx` curve families for details.
  510. *
  511. * \param curve A value of type ::psa_ecc_family_t that
  512. * identifies the ECC curve to be used.
  513. */
  514. #define PSA_KEY_TYPE_ECC_KEY_PAIR(curve) \
  515. (PSA_KEY_TYPE_ECC_KEY_PAIR_BASE | (curve))
  516. /** Elliptic curve public key.
  517. *
  518. * The size of an elliptic curve public key is the same as the corresponding
  519. * private key (see #PSA_KEY_TYPE_ECC_KEY_PAIR and the documentation of
  520. * `PSA_ECC_FAMILY_xxx` curve families).
  521. *
  522. * \param curve A value of type ::psa_ecc_family_t that
  523. * identifies the ECC curve to be used.
  524. */
  525. #define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
  526. (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
  527. /** Whether a key type is an elliptic curve key (pair or public-only). */
  528. #define PSA_KEY_TYPE_IS_ECC(type) \
  529. ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
  530. ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
  531. /** Whether a key type is an elliptic curve key pair. */
  532. #define PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type) \
  533. (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
  534. PSA_KEY_TYPE_ECC_KEY_PAIR_BASE)
  535. /** Whether a key type is an elliptic curve public key. */
  536. #define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
  537. (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
  538. PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
  539. /** Extract the curve from an elliptic curve key type. */
  540. #define PSA_KEY_TYPE_ECC_GET_FAMILY(type) \
  541. ((psa_ecc_family_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
  542. ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
  543. 0))
  544. /** Check if the curve of given family is Weierstrass elliptic curve. */
  545. #define PSA_ECC_FAMILY_IS_WEIERSTRASS(family) ((family & 0xc0) == 0)
  546. /** SEC Koblitz curves over prime fields.
  547. *
  548. * This family comprises the following curves:
  549. * secp192k1, secp224k1, secp256k1.
  550. * They are defined in _Standards for Efficient Cryptography_,
  551. * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
  552. * https://www.secg.org/sec2-v2.pdf
  553. */
  554. #define PSA_ECC_FAMILY_SECP_K1 ((psa_ecc_family_t) 0x17)
  555. /** SEC random curves over prime fields.
  556. *
  557. * This family comprises the following curves:
  558. * secp192k1, secp224r1, secp256r1, secp384r1, secp521r1.
  559. * They are defined in _Standards for Efficient Cryptography_,
  560. * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
  561. * https://www.secg.org/sec2-v2.pdf
  562. */
  563. #define PSA_ECC_FAMILY_SECP_R1 ((psa_ecc_family_t) 0x12)
  564. /* SECP160R2 (SEC2 v1, obsolete) */
  565. #define PSA_ECC_FAMILY_SECP_R2 ((psa_ecc_family_t) 0x1b)
  566. /** SEC Koblitz curves over binary fields.
  567. *
  568. * This family comprises the following curves:
  569. * sect163k1, sect233k1, sect239k1, sect283k1, sect409k1, sect571k1.
  570. * They are defined in _Standards for Efficient Cryptography_,
  571. * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
  572. * https://www.secg.org/sec2-v2.pdf
  573. */
  574. #define PSA_ECC_FAMILY_SECT_K1 ((psa_ecc_family_t) 0x27)
  575. /** SEC random curves over binary fields.
  576. *
  577. * This family comprises the following curves:
  578. * sect163r1, sect233r1, sect283r1, sect409r1, sect571r1.
  579. * They are defined in _Standards for Efficient Cryptography_,
  580. * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
  581. * https://www.secg.org/sec2-v2.pdf
  582. */
  583. #define PSA_ECC_FAMILY_SECT_R1 ((psa_ecc_family_t) 0x22)
  584. /** SEC additional random curves over binary fields.
  585. *
  586. * This family comprises the following curve:
  587. * sect163r2.
  588. * It is defined in _Standards for Efficient Cryptography_,
  589. * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
  590. * https://www.secg.org/sec2-v2.pdf
  591. */
  592. #define PSA_ECC_FAMILY_SECT_R2 ((psa_ecc_family_t) 0x2b)
  593. /** Brainpool P random curves.
  594. *
  595. * This family comprises the following curves:
  596. * brainpoolP160r1, brainpoolP192r1, brainpoolP224r1, brainpoolP256r1,
  597. * brainpoolP320r1, brainpoolP384r1, brainpoolP512r1.
  598. * It is defined in RFC 5639.
  599. */
  600. #define PSA_ECC_FAMILY_BRAINPOOL_P_R1 ((psa_ecc_family_t) 0x30)
  601. /** Curve25519 and Curve448.
  602. *
  603. * This family comprises the following Montgomery curves:
  604. * - 255-bit: Bernstein et al.,
  605. * _Curve25519: new Diffie-Hellman speed records_, LNCS 3958, 2006.
  606. * The algorithm #PSA_ALG_ECDH performs X25519 when used with this curve.
  607. * - 448-bit: Hamburg,
  608. * _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
  609. * The algorithm #PSA_ALG_ECDH performs X448 when used with this curve.
  610. */
  611. #define PSA_ECC_FAMILY_MONTGOMERY ((psa_ecc_family_t) 0x41)
  612. /** The twisted Edwards curves Ed25519 and Ed448.
  613. *
  614. * These curves are suitable for EdDSA (#PSA_ALG_PURE_EDDSA for both curves,
  615. * #PSA_ALG_ED25519PH for the 255-bit curve,
  616. * #PSA_ALG_ED448PH for the 448-bit curve).
  617. *
  618. * This family comprises the following twisted Edwards curves:
  619. * - 255-bit: Edwards25519, the twisted Edwards curve birationally equivalent
  620. * to Curve25519.
  621. * Bernstein et al., _Twisted Edwards curves_, Africacrypt 2008.
  622. * - 448-bit: Edwards448, the twisted Edwards curve birationally equivalent
  623. * to Curve448.
  624. * Hamburg, _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
  625. */
  626. #define PSA_ECC_FAMILY_TWISTED_EDWARDS ((psa_ecc_family_t) 0x42)
  627. #define PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE ((psa_key_type_t) 0x4200)
  628. #define PSA_KEY_TYPE_DH_KEY_PAIR_BASE ((psa_key_type_t) 0x7200)
  629. #define PSA_KEY_TYPE_DH_GROUP_MASK ((psa_key_type_t) 0x00ff)
  630. /** Diffie-Hellman key pair.
  631. *
  632. * \param group A value of type ::psa_dh_family_t that identifies the
  633. * Diffie-Hellman group to be used.
  634. */
  635. #define PSA_KEY_TYPE_DH_KEY_PAIR(group) \
  636. (PSA_KEY_TYPE_DH_KEY_PAIR_BASE | (group))
  637. /** Diffie-Hellman public key.
  638. *
  639. * \param group A value of type ::psa_dh_family_t that identifies the
  640. * Diffie-Hellman group to be used.
  641. */
  642. #define PSA_KEY_TYPE_DH_PUBLIC_KEY(group) \
  643. (PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE | (group))
  644. /** Whether a key type is a Diffie-Hellman key (pair or public-only). */
  645. #define PSA_KEY_TYPE_IS_DH(type) \
  646. ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
  647. ~PSA_KEY_TYPE_DH_GROUP_MASK) == PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
  648. /** Whether a key type is a Diffie-Hellman key pair. */
  649. #define PSA_KEY_TYPE_IS_DH_KEY_PAIR(type) \
  650. (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
  651. PSA_KEY_TYPE_DH_KEY_PAIR_BASE)
  652. /** Whether a key type is a Diffie-Hellman public key. */
  653. #define PSA_KEY_TYPE_IS_DH_PUBLIC_KEY(type) \
  654. (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
  655. PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
  656. /** Extract the group from a Diffie-Hellman key type. */
  657. #define PSA_KEY_TYPE_DH_GET_FAMILY(type) \
  658. ((psa_dh_family_t) (PSA_KEY_TYPE_IS_DH(type) ? \
  659. ((type) & PSA_KEY_TYPE_DH_GROUP_MASK) : \
  660. 0))
  661. /** Diffie-Hellman groups defined in RFC 7919 Appendix A.
  662. *
  663. * This family includes groups with the following key sizes (in bits):
  664. * 2048, 3072, 4096, 6144, 8192. A given implementation may support
  665. * all of these sizes or only a subset.
  666. */
  667. #define PSA_DH_FAMILY_RFC7919 ((psa_dh_family_t) 0x03)
  668. #define PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) \
  669. (((type) >> 8) & 7)
  670. /** The block size of a block cipher.
  671. *
  672. * \param type A cipher key type (value of type #psa_key_type_t).
  673. *
  674. * \return The block size for a block cipher, or 1 for a stream cipher.
  675. * The return value is undefined if \p type is not a supported
  676. * cipher key type.
  677. *
  678. * \note It is possible to build stream cipher algorithms on top of a block
  679. * cipher, for example CTR mode (#PSA_ALG_CTR).
  680. * This macro only takes the key type into account, so it cannot be
  681. * used to determine the size of the data that #psa_cipher_update()
  682. * might buffer for future processing in general.
  683. *
  684. * \note This macro returns a compile-time constant if its argument is one.
  685. *
  686. * \warning This macro may evaluate its argument multiple times.
  687. */
  688. #define PSA_BLOCK_CIPHER_BLOCK_LENGTH(type) \
  689. (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
  690. 1u << PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) : \
  691. 0u)
  692. /* Note that algorithm values are embedded in the persistent key store,
  693. * as part of key metadata. As a consequence, they must not be changed
  694. * (unless the storage format version changes).
  695. */
  696. /** Vendor-defined algorithm flag.
  697. *
  698. * Algorithms defined by this standard will never have the #PSA_ALG_VENDOR_FLAG
  699. * bit set. Vendors who define additional algorithms must use an encoding with
  700. * the #PSA_ALG_VENDOR_FLAG bit set and should respect the bitwise structure
  701. * used by standard encodings whenever practical.
  702. */
  703. #define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t) 0x80000000)
  704. #define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t) 0x7f000000)
  705. #define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t) 0x02000000)
  706. #define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t) 0x03000000)
  707. #define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t) 0x04000000)
  708. #define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t) 0x05000000)
  709. #define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t) 0x06000000)
  710. #define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t) 0x07000000)
  711. #define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t) 0x08000000)
  712. #define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t) 0x09000000)
  713. /** Whether an algorithm is vendor-defined.
  714. *
  715. * See also #PSA_ALG_VENDOR_FLAG.
  716. */
  717. #define PSA_ALG_IS_VENDOR_DEFINED(alg) \
  718. (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
  719. /** Whether the specified algorithm is a hash algorithm.
  720. *
  721. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  722. *
  723. * \return 1 if \p alg is a hash algorithm, 0 otherwise.
  724. * This macro may return either 0 or 1 if \p alg is not a supported
  725. * algorithm identifier.
  726. */
  727. #define PSA_ALG_IS_HASH(alg) \
  728. (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
  729. /** Whether the specified algorithm is a MAC algorithm.
  730. *
  731. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  732. *
  733. * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
  734. * This macro may return either 0 or 1 if \p alg is not a supported
  735. * algorithm identifier.
  736. */
  737. #define PSA_ALG_IS_MAC(alg) \
  738. (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
  739. /** Whether the specified algorithm is a symmetric cipher algorithm.
  740. *
  741. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  742. *
  743. * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
  744. * This macro may return either 0 or 1 if \p alg is not a supported
  745. * algorithm identifier.
  746. */
  747. #define PSA_ALG_IS_CIPHER(alg) \
  748. (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
  749. /** Whether the specified algorithm is an authenticated encryption
  750. * with associated data (AEAD) algorithm.
  751. *
  752. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  753. *
  754. * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
  755. * This macro may return either 0 or 1 if \p alg is not a supported
  756. * algorithm identifier.
  757. */
  758. #define PSA_ALG_IS_AEAD(alg) \
  759. (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
  760. /** Whether the specified algorithm is an asymmetric signature algorithm,
  761. * also known as public-key signature algorithm.
  762. *
  763. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  764. *
  765. * \return 1 if \p alg is an asymmetric signature algorithm, 0 otherwise.
  766. * This macro may return either 0 or 1 if \p alg is not a supported
  767. * algorithm identifier.
  768. */
  769. #define PSA_ALG_IS_SIGN(alg) \
  770. (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
  771. /** Whether the specified algorithm is an asymmetric encryption algorithm,
  772. * also known as public-key encryption algorithm.
  773. *
  774. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  775. *
  776. * \return 1 if \p alg is an asymmetric encryption algorithm, 0 otherwise.
  777. * This macro may return either 0 or 1 if \p alg is not a supported
  778. * algorithm identifier.
  779. */
  780. #define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
  781. (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
  782. /** Whether the specified algorithm is a key agreement algorithm.
  783. *
  784. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  785. *
  786. * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
  787. * This macro may return either 0 or 1 if \p alg is not a supported
  788. * algorithm identifier.
  789. */
  790. #define PSA_ALG_IS_KEY_AGREEMENT(alg) \
  791. (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
  792. /** Whether the specified algorithm is a key derivation algorithm.
  793. *
  794. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  795. *
  796. * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
  797. * This macro may return either 0 or 1 if \p alg is not a supported
  798. * algorithm identifier.
  799. */
  800. #define PSA_ALG_IS_KEY_DERIVATION(alg) \
  801. (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
  802. /** Whether the specified algorithm is a key stretching / password hashing
  803. * algorithm.
  804. *
  805. * A key stretching / password hashing algorithm is a key derivation algorithm
  806. * that is suitable for use with a low-entropy secret such as a password.
  807. * Equivalently, it's a key derivation algorithm that uses a
  808. * #PSA_KEY_DERIVATION_INPUT_PASSWORD input step.
  809. *
  810. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  811. *
  812. * \return 1 if \p alg is a key stretching / password hashing algorithm, 0
  813. * otherwise. This macro may return either 0 or 1 if \p alg is not a
  814. * supported algorithm identifier.
  815. */
  816. #define PSA_ALG_IS_KEY_DERIVATION_STRETCHING(alg) \
  817. (PSA_ALG_IS_KEY_DERIVATION(alg) && \
  818. (alg) & PSA_ALG_KEY_DERIVATION_STRETCHING_FLAG)
  819. /** An invalid algorithm identifier value. */
  820. /* *INDENT-OFF* (https://github.com/ARM-software/psa-arch-tests/issues/337) */
  821. #define PSA_ALG_NONE ((psa_algorithm_t)0)
  822. /* *INDENT-ON* */
  823. #define PSA_ALG_HASH_MASK ((psa_algorithm_t) 0x000000ff)
  824. /** MD5 */
  825. #define PSA_ALG_MD5 ((psa_algorithm_t) 0x02000003)
  826. /** PSA_ALG_RIPEMD160 */
  827. #define PSA_ALG_RIPEMD160 ((psa_algorithm_t) 0x02000004)
  828. /** SHA1 */
  829. #define PSA_ALG_SHA_1 ((psa_algorithm_t) 0x02000005)
  830. /** SHA2-224 */
  831. #define PSA_ALG_SHA_224 ((psa_algorithm_t) 0x02000008)
  832. /** SHA2-256 */
  833. #define PSA_ALG_SHA_256 ((psa_algorithm_t) 0x02000009)
  834. /** SHA2-384 */
  835. #define PSA_ALG_SHA_384 ((psa_algorithm_t) 0x0200000a)
  836. /** SHA2-512 */
  837. #define PSA_ALG_SHA_512 ((psa_algorithm_t) 0x0200000b)
  838. /** SHA2-512/224 */
  839. #define PSA_ALG_SHA_512_224 ((psa_algorithm_t) 0x0200000c)
  840. /** SHA2-512/256 */
  841. #define PSA_ALG_SHA_512_256 ((psa_algorithm_t) 0x0200000d)
  842. /** SHA3-224 */
  843. #define PSA_ALG_SHA3_224 ((psa_algorithm_t) 0x02000010)
  844. /** SHA3-256 */
  845. #define PSA_ALG_SHA3_256 ((psa_algorithm_t) 0x02000011)
  846. /** SHA3-384 */
  847. #define PSA_ALG_SHA3_384 ((psa_algorithm_t) 0x02000012)
  848. /** SHA3-512 */
  849. #define PSA_ALG_SHA3_512 ((psa_algorithm_t) 0x02000013)
  850. /** The first 512 bits (64 bytes) of the SHAKE256 output.
  851. *
  852. * This is the prehashing for Ed448ph (see #PSA_ALG_ED448PH). For other
  853. * scenarios where a hash function based on SHA3/SHAKE is desired, SHA3-512
  854. * has the same output size and a (theoretically) higher security strength.
  855. */
  856. #define PSA_ALG_SHAKE256_512 ((psa_algorithm_t) 0x02000015)
  857. /** In a hash-and-sign algorithm policy, allow any hash algorithm.
  858. *
  859. * This value may be used to form the algorithm usage field of a policy
  860. * for a signature algorithm that is parametrized by a hash. The key
  861. * may then be used to perform operations using the same signature
  862. * algorithm parametrized with any supported hash.
  863. *
  864. * That is, suppose that `PSA_xxx_SIGNATURE` is one of the following macros:
  865. * - #PSA_ALG_RSA_PKCS1V15_SIGN, #PSA_ALG_RSA_PSS, #PSA_ALG_RSA_PSS_ANY_SALT,
  866. * - #PSA_ALG_ECDSA, #PSA_ALG_DETERMINISTIC_ECDSA.
  867. * Then you may create and use a key as follows:
  868. * - Set the key usage field using #PSA_ALG_ANY_HASH, for example:
  869. * ```
  870. * psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); // or VERIFY
  871. * psa_set_key_algorithm(&attributes, PSA_xxx_SIGNATURE(PSA_ALG_ANY_HASH));
  872. * ```
  873. * - Import or generate key material.
  874. * - Call psa_sign_hash() or psa_verify_hash(), passing
  875. * an algorithm built from `PSA_xxx_SIGNATURE` and a specific hash. Each
  876. * call to sign or verify a message may use a different hash.
  877. * ```
  878. * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_256), ...);
  879. * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_512), ...);
  880. * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA3_256), ...);
  881. * ```
  882. *
  883. * This value may not be used to build other algorithms that are
  884. * parametrized over a hash. For any valid use of this macro to build
  885. * an algorithm \c alg, #PSA_ALG_IS_HASH_AND_SIGN(\c alg) is true.
  886. *
  887. * This value may not be used to build an algorithm specification to
  888. * perform an operation. It is only valid to build policies.
  889. */
  890. #define PSA_ALG_ANY_HASH ((psa_algorithm_t) 0x020000ff)
  891. #define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t) 0x00c00000)
  892. #define PSA_ALG_HMAC_BASE ((psa_algorithm_t) 0x03800000)
  893. /** Macro to build an HMAC algorithm.
  894. *
  895. * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
  896. *
  897. * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
  898. * #PSA_ALG_IS_HASH(\p hash_alg) is true).
  899. *
  900. * \return The corresponding HMAC algorithm.
  901. * \return Unspecified if \p hash_alg is not a supported
  902. * hash algorithm.
  903. */
  904. #define PSA_ALG_HMAC(hash_alg) \
  905. (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
  906. #define PSA_ALG_HMAC_GET_HASH(hmac_alg) \
  907. (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
  908. /** Whether the specified algorithm is an HMAC algorithm.
  909. *
  910. * HMAC is a family of MAC algorithms that are based on a hash function.
  911. *
  912. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  913. *
  914. * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
  915. * This macro may return either 0 or 1 if \p alg is not a supported
  916. * algorithm identifier.
  917. */
  918. #define PSA_ALG_IS_HMAC(alg) \
  919. (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
  920. PSA_ALG_HMAC_BASE)
  921. /* In the encoding of a MAC algorithm, the bits corresponding to
  922. * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is
  923. * truncated. As an exception, the value 0 means the untruncated algorithm,
  924. * whatever its length is. The length is encoded in 6 bits, so it can
  925. * reach up to 63; the largest MAC is 64 bytes so its trivial truncation
  926. * to full length is correctly encoded as 0 and any non-trivial truncation
  927. * is correctly encoded as a value between 1 and 63. */
  928. #define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t) 0x003f0000)
  929. #define PSA_MAC_TRUNCATION_OFFSET 16
  930. /* In the encoding of a MAC algorithm, the bit corresponding to
  931. * #PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm
  932. * is a wildcard algorithm. A key with such wildcard algorithm as permitted
  933. * algorithm policy can be used with any algorithm corresponding to the
  934. * same base class and having a (potentially truncated) MAC length greater or
  935. * equal than the one encoded in #PSA_ALG_MAC_TRUNCATION_MASK. */
  936. #define PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ((psa_algorithm_t) 0x00008000)
  937. /** Macro to build a truncated MAC algorithm.
  938. *
  939. * A truncated MAC algorithm is identical to the corresponding MAC
  940. * algorithm except that the MAC value for the truncated algorithm
  941. * consists of only the first \p mac_length bytes of the MAC value
  942. * for the untruncated algorithm.
  943. *
  944. * \note This macro may allow constructing algorithm identifiers that
  945. * are not valid, either because the specified length is larger
  946. * than the untruncated MAC or because the specified length is
  947. * smaller than permitted by the implementation.
  948. *
  949. * \note It is implementation-defined whether a truncated MAC that
  950. * is truncated to the same length as the MAC of the untruncated
  951. * algorithm is considered identical to the untruncated algorithm
  952. * for policy comparison purposes.
  953. *
  954. * \param mac_alg A MAC algorithm identifier (value of type
  955. * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
  956. * is true). This may be a truncated or untruncated
  957. * MAC algorithm.
  958. * \param mac_length Desired length of the truncated MAC in bytes.
  959. * This must be at most the full length of the MAC
  960. * and must be at least an implementation-specified
  961. * minimum. The implementation-specified minimum
  962. * shall not be zero.
  963. *
  964. * \return The corresponding MAC algorithm with the specified
  965. * length.
  966. * \return Unspecified if \p mac_alg is not a supported
  967. * MAC algorithm or if \p mac_length is too small or
  968. * too large for the specified MAC algorithm.
  969. */
  970. #define PSA_ALG_TRUNCATED_MAC(mac_alg, mac_length) \
  971. (((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK | \
  972. PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG)) | \
  973. ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
  974. /** Macro to build the base MAC algorithm corresponding to a truncated
  975. * MAC algorithm.
  976. *
  977. * \param mac_alg A MAC algorithm identifier (value of type
  978. * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
  979. * is true). This may be a truncated or untruncated
  980. * MAC algorithm.
  981. *
  982. * \return The corresponding base MAC algorithm.
  983. * \return Unspecified if \p mac_alg is not a supported
  984. * MAC algorithm.
  985. */
  986. #define PSA_ALG_FULL_LENGTH_MAC(mac_alg) \
  987. ((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK | \
  988. PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG))
  989. /** Length to which a MAC algorithm is truncated.
  990. *
  991. * \param mac_alg A MAC algorithm identifier (value of type
  992. * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
  993. * is true).
  994. *
  995. * \return Length of the truncated MAC in bytes.
  996. * \return 0 if \p mac_alg is a non-truncated MAC algorithm.
  997. * \return Unspecified if \p mac_alg is not a supported
  998. * MAC algorithm.
  999. */
  1000. #define PSA_MAC_TRUNCATED_LENGTH(mac_alg) \
  1001. (((mac_alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
  1002. /** Macro to build a MAC minimum-MAC-length wildcard algorithm.
  1003. *
  1004. * A minimum-MAC-length MAC wildcard algorithm permits all MAC algorithms
  1005. * sharing the same base algorithm, and where the (potentially truncated) MAC
  1006. * length of the specific algorithm is equal to or larger then the wildcard
  1007. * algorithm's minimum MAC length.
  1008. *
  1009. * \note When setting the minimum required MAC length to less than the
  1010. * smallest MAC length allowed by the base algorithm, this effectively
  1011. * becomes an 'any-MAC-length-allowed' policy for that base algorithm.
  1012. *
  1013. * \param mac_alg A MAC algorithm identifier (value of type
  1014. * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
  1015. * is true).
  1016. * \param min_mac_length Desired minimum length of the message authentication
  1017. * code in bytes. This must be at most the untruncated
  1018. * length of the MAC and must be at least 1.
  1019. *
  1020. * \return The corresponding MAC wildcard algorithm with the
  1021. * specified minimum length.
  1022. * \return Unspecified if \p mac_alg is not a supported MAC
  1023. * algorithm or if \p min_mac_length is less than 1 or
  1024. * too large for the specified MAC algorithm.
  1025. */
  1026. #define PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(mac_alg, min_mac_length) \
  1027. (PSA_ALG_TRUNCATED_MAC(mac_alg, min_mac_length) | \
  1028. PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG)
  1029. #define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t) 0x03c00000)
  1030. /** The CBC-MAC construction over a block cipher
  1031. *
  1032. * \warning CBC-MAC is insecure in many cases.
  1033. * A more secure mode, such as #PSA_ALG_CMAC, is recommended.
  1034. */
  1035. #define PSA_ALG_CBC_MAC ((psa_algorithm_t) 0x03c00100)
  1036. /** The CMAC construction over a block cipher */
  1037. #define PSA_ALG_CMAC ((psa_algorithm_t) 0x03c00200)
  1038. /** Whether the specified algorithm is a MAC algorithm based on a block cipher.
  1039. *
  1040. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  1041. *
  1042. * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
  1043. * This macro may return either 0 or 1 if \p alg is not a supported
  1044. * algorithm identifier.
  1045. */
  1046. #define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \
  1047. (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
  1048. PSA_ALG_CIPHER_MAC_BASE)
  1049. #define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t) 0x00800000)
  1050. #define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t) 0x00400000)
  1051. /** Whether the specified algorithm is a stream cipher.
  1052. *
  1053. * A stream cipher is a symmetric cipher that encrypts or decrypts messages
  1054. * by applying a bitwise-xor with a stream of bytes that is generated
  1055. * from a key.
  1056. *
  1057. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  1058. *
  1059. * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
  1060. * This macro may return either 0 or 1 if \p alg is not a supported
  1061. * algorithm identifier or if it is not a symmetric cipher algorithm.
  1062. */
  1063. #define PSA_ALG_IS_STREAM_CIPHER(alg) \
  1064. (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
  1065. (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
  1066. /** The stream cipher mode of a stream cipher algorithm.
  1067. *
  1068. * The underlying stream cipher is determined by the key type.
  1069. * - To use ChaCha20, use a key type of #PSA_KEY_TYPE_CHACHA20.
  1070. */
  1071. #define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t) 0x04800100)
  1072. /** The CTR stream cipher mode.
  1073. *
  1074. * CTR is a stream cipher which is built from a block cipher.
  1075. * The underlying block cipher is determined by the key type.
  1076. * For example, to use AES-128-CTR, use this algorithm with
  1077. * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
  1078. */
  1079. #define PSA_ALG_CTR ((psa_algorithm_t) 0x04c01000)
  1080. /** The CFB stream cipher mode.
  1081. *
  1082. * The underlying block cipher is determined by the key type.
  1083. */
  1084. #define PSA_ALG_CFB ((psa_algorithm_t) 0x04c01100)
  1085. /** The OFB stream cipher mode.
  1086. *
  1087. * The underlying block cipher is determined by the key type.
  1088. */
  1089. #define PSA_ALG_OFB ((psa_algorithm_t) 0x04c01200)
  1090. /** The XTS cipher mode.
  1091. *
  1092. * XTS is a cipher mode which is built from a block cipher. It requires at
  1093. * least one full block of input, but beyond this minimum the input
  1094. * does not need to be a whole number of blocks.
  1095. */
  1096. #define PSA_ALG_XTS ((psa_algorithm_t) 0x0440ff00)
  1097. /** The Electronic Code Book (ECB) mode of a block cipher, with no padding.
  1098. *
  1099. * \warning ECB mode does not protect the confidentiality of the encrypted data
  1100. * except in extremely narrow circumstances. It is recommended that applications
  1101. * only use ECB if they need to construct an operating mode that the
  1102. * implementation does not provide. Implementations are encouraged to provide
  1103. * the modes that applications need in preference to supporting direct access
  1104. * to ECB.
  1105. *
  1106. * The underlying block cipher is determined by the key type.
  1107. *
  1108. * This symmetric cipher mode can only be used with messages whose lengths are a
  1109. * multiple of the block size of the chosen block cipher.
  1110. *
  1111. * ECB mode does not accept an initialization vector (IV). When using a
  1112. * multi-part cipher operation with this algorithm, psa_cipher_generate_iv()
  1113. * and psa_cipher_set_iv() must not be called.
  1114. */
  1115. #define PSA_ALG_ECB_NO_PADDING ((psa_algorithm_t) 0x04404400)
  1116. /** The CBC block cipher chaining mode, with no padding.
  1117. *
  1118. * The underlying block cipher is determined by the key type.
  1119. *
  1120. * This symmetric cipher mode can only be used with messages whose lengths
  1121. * are whole number of blocks for the chosen block cipher.
  1122. */
  1123. #define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t) 0x04404000)
  1124. /** The CBC block cipher chaining mode with PKCS#7 padding.
  1125. *
  1126. * The underlying block cipher is determined by the key type.
  1127. *
  1128. * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
  1129. */
  1130. #define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t) 0x04404100)
  1131. #define PSA_ALG_AEAD_FROM_BLOCK_FLAG ((psa_algorithm_t) 0x00400000)
  1132. /** Whether the specified algorithm is an AEAD mode on a block cipher.
  1133. *
  1134. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  1135. *
  1136. * \return 1 if \p alg is an AEAD algorithm which is an AEAD mode based on
  1137. * a block cipher, 0 otherwise.
  1138. * This macro may return either 0 or 1 if \p alg is not a supported
  1139. * algorithm identifier.
  1140. */
  1141. #define PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) \
  1142. (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_AEAD_FROM_BLOCK_FLAG)) == \
  1143. (PSA_ALG_CATEGORY_AEAD | PSA_ALG_AEAD_FROM_BLOCK_FLAG))
  1144. /** The CCM authenticated encryption algorithm.
  1145. *
  1146. * The underlying block cipher is determined by the key type.
  1147. */
  1148. #define PSA_ALG_CCM ((psa_algorithm_t) 0x05500100)
  1149. /** The CCM* cipher mode without authentication.
  1150. *
  1151. * This is CCM* as specified in IEEE 802.15.4 §7, with a tag length of 0.
  1152. * For CCM* with a nonzero tag length, use the AEAD algorithm #PSA_ALG_CCM.
  1153. *
  1154. * The underlying block cipher is determined by the key type.
  1155. *
  1156. * Currently only 13-byte long IV's are supported.
  1157. */
  1158. #define PSA_ALG_CCM_STAR_NO_TAG ((psa_algorithm_t) 0x04c01300)
  1159. /** The GCM authenticated encryption algorithm.
  1160. *
  1161. * The underlying block cipher is determined by the key type.
  1162. */
  1163. #define PSA_ALG_GCM ((psa_algorithm_t) 0x05500200)
  1164. /** The Chacha20-Poly1305 AEAD algorithm.
  1165. *
  1166. * The ChaCha20_Poly1305 construction is defined in RFC 7539.
  1167. *
  1168. * Implementations must support 12-byte nonces, may support 8-byte nonces,
  1169. * and should reject other sizes.
  1170. *
  1171. * Implementations must support 16-byte tags and should reject other sizes.
  1172. */
  1173. #define PSA_ALG_CHACHA20_POLY1305 ((psa_algorithm_t) 0x05100500)
  1174. /* In the encoding of an AEAD algorithm, the bits corresponding to
  1175. * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
  1176. * The constants for default lengths follow this encoding.
  1177. */
  1178. #define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t) 0x003f0000)
  1179. #define PSA_AEAD_TAG_LENGTH_OFFSET 16
  1180. /* In the encoding of an AEAD algorithm, the bit corresponding to
  1181. * #PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm
  1182. * is a wildcard algorithm. A key with such wildcard algorithm as permitted
  1183. * algorithm policy can be used with any algorithm corresponding to the
  1184. * same base class and having a tag length greater than or equal to the one
  1185. * encoded in #PSA_ALG_AEAD_TAG_LENGTH_MASK. */
  1186. #define PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ((psa_algorithm_t) 0x00008000)
  1187. /** Macro to build a shortened AEAD algorithm.
  1188. *
  1189. * A shortened AEAD algorithm is similar to the corresponding AEAD
  1190. * algorithm, but has an authentication tag that consists of fewer bytes.
  1191. * Depending on the algorithm, the tag length may affect the calculation
  1192. * of the ciphertext.
  1193. *
  1194. * \param aead_alg An AEAD algorithm identifier (value of type
  1195. * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p aead_alg)
  1196. * is true).
  1197. * \param tag_length Desired length of the authentication tag in bytes.
  1198. *
  1199. * \return The corresponding AEAD algorithm with the specified
  1200. * length.
  1201. * \return Unspecified if \p aead_alg is not a supported
  1202. * AEAD algorithm or if \p tag_length is not valid
  1203. * for the specified AEAD algorithm.
  1204. */
  1205. #define PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, tag_length) \
  1206. (((aead_alg) & ~(PSA_ALG_AEAD_TAG_LENGTH_MASK | \
  1207. PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)) | \
  1208. ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \
  1209. PSA_ALG_AEAD_TAG_LENGTH_MASK))
  1210. /** Retrieve the tag length of a specified AEAD algorithm
  1211. *
  1212. * \param aead_alg An AEAD algorithm identifier (value of type
  1213. * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p aead_alg)
  1214. * is true).
  1215. *
  1216. * \return The tag length specified by the input algorithm.
  1217. * \return Unspecified if \p aead_alg is not a supported
  1218. * AEAD algorithm.
  1219. */
  1220. #define PSA_ALG_AEAD_GET_TAG_LENGTH(aead_alg) \
  1221. (((aead_alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> \
  1222. PSA_AEAD_TAG_LENGTH_OFFSET)
  1223. /** Calculate the corresponding AEAD algorithm with the default tag length.
  1224. *
  1225. * \param aead_alg An AEAD algorithm (\c PSA_ALG_XXX value such that
  1226. * #PSA_ALG_IS_AEAD(\p aead_alg) is true).
  1227. *
  1228. * \return The corresponding AEAD algorithm with the default
  1229. * tag length for that algorithm.
  1230. */
  1231. #define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(aead_alg) \
  1232. ( \
  1233. PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CCM) \
  1234. PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_GCM) \
  1235. PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CHACHA20_POLY1305) \
  1236. 0)
  1237. #define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, ref) \
  1238. PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, 0) == \
  1239. PSA_ALG_AEAD_WITH_SHORTENED_TAG(ref, 0) ? \
  1240. ref :
  1241. /** Macro to build an AEAD minimum-tag-length wildcard algorithm.
  1242. *
  1243. * A minimum-tag-length AEAD wildcard algorithm permits all AEAD algorithms
  1244. * sharing the same base algorithm, and where the tag length of the specific
  1245. * algorithm is equal to or larger then the minimum tag length specified by the
  1246. * wildcard algorithm.
  1247. *
  1248. * \note When setting the minimum required tag length to less than the
  1249. * smallest tag length allowed by the base algorithm, this effectively
  1250. * becomes an 'any-tag-length-allowed' policy for that base algorithm.
  1251. *
  1252. * \param aead_alg An AEAD algorithm identifier (value of type
  1253. * #psa_algorithm_t such that
  1254. * #PSA_ALG_IS_AEAD(\p aead_alg) is true).
  1255. * \param min_tag_length Desired minimum length of the authentication tag in
  1256. * bytes. This must be at least 1 and at most the largest
  1257. * allowed tag length of the algorithm.
  1258. *
  1259. * \return The corresponding AEAD wildcard algorithm with the
  1260. * specified minimum length.
  1261. * \return Unspecified if \p aead_alg is not a supported
  1262. * AEAD algorithm or if \p min_tag_length is less than 1
  1263. * or too large for the specified AEAD algorithm.
  1264. */
  1265. #define PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(aead_alg, min_tag_length) \
  1266. (PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, min_tag_length) | \
  1267. PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)
  1268. #define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t) 0x06000200)
  1269. /** RSA PKCS#1 v1.5 signature with hashing.
  1270. *
  1271. * This is the signature scheme defined by RFC 8017
  1272. * (PKCS#1: RSA Cryptography Specifications) under the name
  1273. * RSASSA-PKCS1-v1_5.
  1274. *
  1275. * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
  1276. * #PSA_ALG_IS_HASH(\p hash_alg) is true).
  1277. * This includes #PSA_ALG_ANY_HASH
  1278. * when specifying the algorithm in a usage policy.
  1279. *
  1280. * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
  1281. * \return Unspecified if \p hash_alg is not a supported
  1282. * hash algorithm.
  1283. */
  1284. #define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
  1285. (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
  1286. /** Raw PKCS#1 v1.5 signature.
  1287. *
  1288. * The input to this algorithm is the DigestInfo structure used by
  1289. * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
  1290. * steps 3&ndash;6.
  1291. */
  1292. #define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
  1293. #define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
  1294. (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
  1295. #define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t) 0x06000300)
  1296. #define PSA_ALG_RSA_PSS_ANY_SALT_BASE ((psa_algorithm_t) 0x06001300)
  1297. /** RSA PSS signature with hashing.
  1298. *
  1299. * This is the signature scheme defined by RFC 8017
  1300. * (PKCS#1: RSA Cryptography Specifications) under the name
  1301. * RSASSA-PSS, with the message generation function MGF1, and with
  1302. * a salt length equal to the length of the hash, or the largest
  1303. * possible salt length for the algorithm and key size if that is
  1304. * smaller than the hash length. The specified hash algorithm is
  1305. * used to hash the input message, to create the salted hash, and
  1306. * for the mask generation.
  1307. *
  1308. * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
  1309. * #PSA_ALG_IS_HASH(\p hash_alg) is true).
  1310. * This includes #PSA_ALG_ANY_HASH
  1311. * when specifying the algorithm in a usage policy.
  1312. *
  1313. * \return The corresponding RSA PSS signature algorithm.
  1314. * \return Unspecified if \p hash_alg is not a supported
  1315. * hash algorithm.
  1316. */
  1317. #define PSA_ALG_RSA_PSS(hash_alg) \
  1318. (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
  1319. /** RSA PSS signature with hashing with relaxed verification.
  1320. *
  1321. * This algorithm has the same behavior as #PSA_ALG_RSA_PSS when signing,
  1322. * but allows an arbitrary salt length (including \c 0) when verifying a
  1323. * signature.
  1324. *
  1325. * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
  1326. * #PSA_ALG_IS_HASH(\p hash_alg) is true).
  1327. * This includes #PSA_ALG_ANY_HASH
  1328. * when specifying the algorithm in a usage policy.
  1329. *
  1330. * \return The corresponding RSA PSS signature algorithm.
  1331. * \return Unspecified if \p hash_alg is not a supported
  1332. * hash algorithm.
  1333. */
  1334. #define PSA_ALG_RSA_PSS_ANY_SALT(hash_alg) \
  1335. (PSA_ALG_RSA_PSS_ANY_SALT_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
  1336. /** Whether the specified algorithm is RSA PSS with standard salt.
  1337. *
  1338. * \param alg An algorithm value or an algorithm policy wildcard.
  1339. *
  1340. * \return 1 if \p alg is of the form
  1341. * #PSA_ALG_RSA_PSS(\c hash_alg),
  1342. * where \c hash_alg is a hash algorithm or
  1343. * #PSA_ALG_ANY_HASH. 0 otherwise.
  1344. * This macro may return either 0 or 1 if \p alg is not
  1345. * a supported algorithm identifier or policy.
  1346. */
  1347. #define PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg) \
  1348. (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
  1349. /** Whether the specified algorithm is RSA PSS with any salt.
  1350. *
  1351. * \param alg An algorithm value or an algorithm policy wildcard.
  1352. *
  1353. * \return 1 if \p alg is of the form
  1354. * #PSA_ALG_RSA_PSS_ANY_SALT_BASE(\c hash_alg),
  1355. * where \c hash_alg is a hash algorithm or
  1356. * #PSA_ALG_ANY_HASH. 0 otherwise.
  1357. * This macro may return either 0 or 1 if \p alg is not
  1358. * a supported algorithm identifier or policy.
  1359. */
  1360. #define PSA_ALG_IS_RSA_PSS_ANY_SALT(alg) \
  1361. (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_ANY_SALT_BASE)
  1362. /** Whether the specified algorithm is RSA PSS.
  1363. *
  1364. * This includes any of the RSA PSS algorithm variants, regardless of the
  1365. * constraints on salt length.
  1366. *
  1367. * \param alg An algorithm value or an algorithm policy wildcard.
  1368. *
  1369. * \return 1 if \p alg is of the form
  1370. * #PSA_ALG_RSA_PSS(\c hash_alg) or
  1371. * #PSA_ALG_RSA_PSS_ANY_SALT_BASE(\c hash_alg),
  1372. * where \c hash_alg is a hash algorithm or
  1373. * #PSA_ALG_ANY_HASH. 0 otherwise.
  1374. * This macro may return either 0 or 1 if \p alg is not
  1375. * a supported algorithm identifier or policy.
  1376. */
  1377. #define PSA_ALG_IS_RSA_PSS(alg) \
  1378. (PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg) || \
  1379. PSA_ALG_IS_RSA_PSS_ANY_SALT(alg))
  1380. #define PSA_ALG_ECDSA_BASE ((psa_algorithm_t) 0x06000600)
  1381. /** ECDSA signature with hashing.
  1382. *
  1383. * This is the ECDSA signature scheme defined by ANSI X9.62,
  1384. * with a random per-message secret number (*k*).
  1385. *
  1386. * The representation of the signature as a byte string consists of
  1387. * the concatenation of the signature values *r* and *s*. Each of
  1388. * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
  1389. * of the base point of the curve in octets. Each value is represented
  1390. * in big-endian order (most significant octet first).
  1391. *
  1392. * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
  1393. * #PSA_ALG_IS_HASH(\p hash_alg) is true).
  1394. * This includes #PSA_ALG_ANY_HASH
  1395. * when specifying the algorithm in a usage policy.
  1396. *
  1397. * \return The corresponding ECDSA signature algorithm.
  1398. * \return Unspecified if \p hash_alg is not a supported
  1399. * hash algorithm.
  1400. */
  1401. #define PSA_ALG_ECDSA(hash_alg) \
  1402. (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
  1403. /** ECDSA signature without hashing.
  1404. *
  1405. * This is the same signature scheme as #PSA_ALG_ECDSA(), but
  1406. * without specifying a hash algorithm. This algorithm may only be
  1407. * used to sign or verify a sequence of bytes that should be an
  1408. * already-calculated hash. Note that the input is padded with
  1409. * zeros on the left or truncated on the left as required to fit
  1410. * the curve size.
  1411. */
  1412. #define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
  1413. #define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t) 0x06000700)
  1414. /** Deterministic ECDSA signature with hashing.
  1415. *
  1416. * This is the deterministic ECDSA signature scheme defined by RFC 6979.
  1417. *
  1418. * The representation of a signature is the same as with #PSA_ALG_ECDSA().
  1419. *
  1420. * Note that when this algorithm is used for verification, signatures
  1421. * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
  1422. * same private key are accepted. In other words,
  1423. * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
  1424. * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
  1425. *
  1426. * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
  1427. * #PSA_ALG_IS_HASH(\p hash_alg) is true).
  1428. * This includes #PSA_ALG_ANY_HASH
  1429. * when specifying the algorithm in a usage policy.
  1430. *
  1431. * \return The corresponding deterministic ECDSA signature
  1432. * algorithm.
  1433. * \return Unspecified if \p hash_alg is not a supported
  1434. * hash algorithm.
  1435. */
  1436. #define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
  1437. (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
  1438. #define PSA_ALG_ECDSA_DETERMINISTIC_FLAG ((psa_algorithm_t) 0x00000100)
  1439. #define PSA_ALG_IS_ECDSA(alg) \
  1440. (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_ECDSA_DETERMINISTIC_FLAG) == \
  1441. PSA_ALG_ECDSA_BASE)
  1442. #define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
  1443. (((alg) & PSA_ALG_ECDSA_DETERMINISTIC_FLAG) != 0)
  1444. #define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
  1445. (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
  1446. #define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
  1447. (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
  1448. /** Edwards-curve digital signature algorithm without prehashing (PureEdDSA),
  1449. * using standard parameters.
  1450. *
  1451. * Contexts are not supported in the current version of this specification
  1452. * because there is no suitable signature interface that can take the
  1453. * context as a parameter. A future version of this specification may add
  1454. * suitable functions and extend this algorithm to support contexts.
  1455. *
  1456. * PureEdDSA requires an elliptic curve key on a twisted Edwards curve.
  1457. * In this specification, the following curves are supported:
  1458. * - #PSA_ECC_FAMILY_TWISTED_EDWARDS, 255-bit: Ed25519 as specified
  1459. * in RFC 8032.
  1460. * The curve is Edwards25519.
  1461. * The hash function used internally is SHA-512.
  1462. * - #PSA_ECC_FAMILY_TWISTED_EDWARDS, 448-bit: Ed448 as specified
  1463. * in RFC 8032.
  1464. * The curve is Edwards448.
  1465. * The hash function used internally is the first 114 bytes of the
  1466. * SHAKE256 output.
  1467. *
  1468. * This algorithm can be used with psa_sign_message() and
  1469. * psa_verify_message(). Since there is no prehashing, it cannot be used
  1470. * with psa_sign_hash() or psa_verify_hash().
  1471. *
  1472. * The signature format is the concatenation of R and S as defined by
  1473. * RFC 8032 §5.1.6 and §5.2.6 (a 64-byte string for Ed25519, a 114-byte
  1474. * string for Ed448).
  1475. */
  1476. #define PSA_ALG_PURE_EDDSA ((psa_algorithm_t) 0x06000800)
  1477. #define PSA_ALG_HASH_EDDSA_BASE ((psa_algorithm_t) 0x06000900)
  1478. #define PSA_ALG_IS_HASH_EDDSA(alg) \
  1479. (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HASH_EDDSA_BASE)
  1480. /** Edwards-curve digital signature algorithm with prehashing (HashEdDSA),
  1481. * using SHA-512 and the Edwards25519 curve.
  1482. *
  1483. * See #PSA_ALG_PURE_EDDSA regarding context support and the signature format.
  1484. *
  1485. * This algorithm is Ed25519 as specified in RFC 8032.
  1486. * The curve is Edwards25519.
  1487. * The prehash is SHA-512.
  1488. * The hash function used internally is SHA-512.
  1489. *
  1490. * This is a hash-and-sign algorithm: to calculate a signature,
  1491. * you can either:
  1492. * - call psa_sign_message() on the message;
  1493. * - or calculate the SHA-512 hash of the message
  1494. * with psa_hash_compute()
  1495. * or with a multi-part hash operation started with psa_hash_setup(),
  1496. * using the hash algorithm #PSA_ALG_SHA_512,
  1497. * then sign the calculated hash with psa_sign_hash().
  1498. * Verifying a signature is similar, using psa_verify_message() or
  1499. * psa_verify_hash() instead of the signature function.
  1500. */
  1501. #define PSA_ALG_ED25519PH \
  1502. (PSA_ALG_HASH_EDDSA_BASE | (PSA_ALG_SHA_512 & PSA_ALG_HASH_MASK))
  1503. /** Edwards-curve digital signature algorithm with prehashing (HashEdDSA),
  1504. * using SHAKE256 and the Edwards448 curve.
  1505. *
  1506. * See #PSA_ALG_PURE_EDDSA regarding context support and the signature format.
  1507. *
  1508. * This algorithm is Ed448 as specified in RFC 8032.
  1509. * The curve is Edwards448.
  1510. * The prehash is the first 64 bytes of the SHAKE256 output.
  1511. * The hash function used internally is the first 114 bytes of the
  1512. * SHAKE256 output.
  1513. *
  1514. * This is a hash-and-sign algorithm: to calculate a signature,
  1515. * you can either:
  1516. * - call psa_sign_message() on the message;
  1517. * - or calculate the first 64 bytes of the SHAKE256 output of the message
  1518. * with psa_hash_compute()
  1519. * or with a multi-part hash operation started with psa_hash_setup(),
  1520. * using the hash algorithm #PSA_ALG_SHAKE256_512,
  1521. * then sign the calculated hash with psa_sign_hash().
  1522. * Verifying a signature is similar, using psa_verify_message() or
  1523. * psa_verify_hash() instead of the signature function.
  1524. */
  1525. #define PSA_ALG_ED448PH \
  1526. (PSA_ALG_HASH_EDDSA_BASE | (PSA_ALG_SHAKE256_512 & PSA_ALG_HASH_MASK))
  1527. /* Default definition, to be overridden if the library is extended with
  1528. * more hash-and-sign algorithms that we want to keep out of this header
  1529. * file. */
  1530. #define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) 0
  1531. /** Whether the specified algorithm is a signature algorithm that can be used
  1532. * with psa_sign_hash() and psa_verify_hash().
  1533. *
  1534. * This encompasses all strict hash-and-sign algorithms categorized by
  1535. * PSA_ALG_IS_HASH_AND_SIGN(), as well as algorithms that follow the
  1536. * paradigm more loosely:
  1537. * - #PSA_ALG_RSA_PKCS1V15_SIGN_RAW (expects its input to be an encoded hash)
  1538. * - #PSA_ALG_ECDSA_ANY (doesn't specify what kind of hash the input is)
  1539. *
  1540. * \param alg An algorithm identifier (value of type psa_algorithm_t).
  1541. *
  1542. * \return 1 if alg is a signature algorithm that can be used to sign a
  1543. * hash. 0 if alg is a signature algorithm that can only be used
  1544. * to sign a message. 0 if alg is not a signature algorithm.
  1545. * This macro can return either 0 or 1 if alg is not a
  1546. * supported algorithm identifier.
  1547. */
  1548. #define PSA_ALG_IS_SIGN_HASH(alg) \
  1549. (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
  1550. PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_HASH_EDDSA(alg) || \
  1551. PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg))
  1552. /** Whether the specified algorithm is a signature algorithm that can be used
  1553. * with psa_sign_message() and psa_verify_message().
  1554. *
  1555. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  1556. *
  1557. * \return 1 if alg is a signature algorithm that can be used to sign a
  1558. * message. 0 if \p alg is a signature algorithm that can only be used
  1559. * to sign an already-calculated hash. 0 if \p alg is not a signature
  1560. * algorithm. This macro can return either 0 or 1 if \p alg is not a
  1561. * supported algorithm identifier.
  1562. */
  1563. #define PSA_ALG_IS_SIGN_MESSAGE(alg) \
  1564. (PSA_ALG_IS_SIGN_HASH(alg) || (alg) == PSA_ALG_PURE_EDDSA)
  1565. /** Whether the specified algorithm is a hash-and-sign algorithm.
  1566. *
  1567. * Hash-and-sign algorithms are asymmetric (public-key) signature algorithms
  1568. * structured in two parts: first the calculation of a hash in a way that
  1569. * does not depend on the key, then the calculation of a signature from the
  1570. * hash value and the key. Hash-and-sign algorithms encode the hash
  1571. * used for the hashing step, and you can call #PSA_ALG_SIGN_GET_HASH
  1572. * to extract this algorithm.
  1573. *
  1574. * Thus, for a hash-and-sign algorithm,
  1575. * `psa_sign_message(key, alg, input, ...)` is equivalent to
  1576. * ```
  1577. * psa_hash_compute(PSA_ALG_SIGN_GET_HASH(alg), input, ..., hash, ...);
  1578. * psa_sign_hash(key, alg, hash, ..., signature, ...);
  1579. * ```
  1580. * Most usefully, separating the hash from the signature allows the hash
  1581. * to be calculated in multiple steps with psa_hash_setup(), psa_hash_update()
  1582. * and psa_hash_finish(). Likewise psa_verify_message() is equivalent to
  1583. * calculating the hash and then calling psa_verify_hash().
  1584. *
  1585. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  1586. *
  1587. * \return 1 if \p alg is a hash-and-sign algorithm, 0 otherwise.
  1588. * This macro may return either 0 or 1 if \p alg is not a supported
  1589. * algorithm identifier.
  1590. */
  1591. #define PSA_ALG_IS_HASH_AND_SIGN(alg) \
  1592. (PSA_ALG_IS_SIGN_HASH(alg) && \
  1593. ((alg) & PSA_ALG_HASH_MASK) != 0)
  1594. /** Get the hash used by a hash-and-sign signature algorithm.
  1595. *
  1596. * A hash-and-sign algorithm is a signature algorithm which is
  1597. * composed of two phases: first a hashing phase which does not use
  1598. * the key and produces a hash of the input message, then a signing
  1599. * phase which only uses the hash and the key and not the message
  1600. * itself.
  1601. *
  1602. * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
  1603. * #PSA_ALG_IS_SIGN(\p alg) is true).
  1604. *
  1605. * \return The underlying hash algorithm if \p alg is a hash-and-sign
  1606. * algorithm.
  1607. * \return 0 if \p alg is a signature algorithm that does not
  1608. * follow the hash-and-sign structure.
  1609. * \return Unspecified if \p alg is not a signature algorithm or
  1610. * if it is not supported by the implementation.
  1611. */
  1612. #define PSA_ALG_SIGN_GET_HASH(alg) \
  1613. (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
  1614. ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
  1615. 0)
  1616. /** RSA PKCS#1 v1.5 encryption.
  1617. */
  1618. #define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t) 0x07000200)
  1619. #define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t) 0x07000300)
  1620. /** RSA OAEP encryption.
  1621. *
  1622. * This is the encryption scheme defined by RFC 8017
  1623. * (PKCS#1: RSA Cryptography Specifications) under the name
  1624. * RSAES-OAEP, with the message generation function MGF1.
  1625. *
  1626. * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
  1627. * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
  1628. * for MGF1.
  1629. *
  1630. * \return The corresponding RSA OAEP encryption algorithm.
  1631. * \return Unspecified if \p hash_alg is not a supported
  1632. * hash algorithm.
  1633. */
  1634. #define PSA_ALG_RSA_OAEP(hash_alg) \
  1635. (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
  1636. #define PSA_ALG_IS_RSA_OAEP(alg) \
  1637. (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
  1638. #define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
  1639. (PSA_ALG_IS_RSA_OAEP(alg) ? \
  1640. ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
  1641. 0)
  1642. #define PSA_ALG_HKDF_BASE ((psa_algorithm_t) 0x08000100)
  1643. /** Macro to build an HKDF algorithm.
  1644. *
  1645. * For example, `PSA_ALG_HKDF(PSA_ALG_SHA_256)` is HKDF using HMAC-SHA-256.
  1646. *
  1647. * This key derivation algorithm uses the following inputs:
  1648. * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt used in the "extract" step.
  1649. * It is optional; if omitted, the derivation uses an empty salt.
  1650. * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key used in the "extract" step.
  1651. * - #PSA_KEY_DERIVATION_INPUT_INFO is the info string used in the "expand" step.
  1652. * You must pass #PSA_KEY_DERIVATION_INPUT_SALT before #PSA_KEY_DERIVATION_INPUT_SECRET.
  1653. * You may pass #PSA_KEY_DERIVATION_INPUT_INFO at any time after steup and before
  1654. * starting to generate output.
  1655. *
  1656. * \warning HKDF processes the salt as follows: first hash it with hash_alg
  1657. * if the salt is longer than the block size of the hash algorithm; then
  1658. * pad with null bytes up to the block size. As a result, it is possible
  1659. * for distinct salt inputs to result in the same outputs. To ensure
  1660. * unique outputs, it is recommended to use a fixed length for salt values.
  1661. *
  1662. * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
  1663. * #PSA_ALG_IS_HASH(\p hash_alg) is true).
  1664. *
  1665. * \return The corresponding HKDF algorithm.
  1666. * \return Unspecified if \p hash_alg is not a supported
  1667. * hash algorithm.
  1668. */
  1669. #define PSA_ALG_HKDF(hash_alg) \
  1670. (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
  1671. /** Whether the specified algorithm is an HKDF algorithm.
  1672. *
  1673. * HKDF is a family of key derivation algorithms that are based on a hash
  1674. * function and the HMAC construction.
  1675. *
  1676. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  1677. *
  1678. * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
  1679. * This macro may return either 0 or 1 if \c alg is not a supported
  1680. * key derivation algorithm identifier.
  1681. */
  1682. #define PSA_ALG_IS_HKDF(alg) \
  1683. (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
  1684. #define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
  1685. (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
  1686. #define PSA_ALG_HKDF_EXTRACT_BASE ((psa_algorithm_t) 0x08000400)
  1687. /** Macro to build an HKDF-Extract algorithm.
  1688. *
  1689. * For example, `PSA_ALG_HKDF_EXTRACT(PSA_ALG_SHA_256)` is
  1690. * HKDF-Extract using HMAC-SHA-256.
  1691. *
  1692. * This key derivation algorithm uses the following inputs:
  1693. * - PSA_KEY_DERIVATION_INPUT_SALT is the salt.
  1694. * - PSA_KEY_DERIVATION_INPUT_SECRET is the input keying material used in the
  1695. * "extract" step.
  1696. * The inputs are mandatory and must be passed in the order above.
  1697. * Each input may only be passed once.
  1698. *
  1699. * \warning HKDF-Extract is not meant to be used on its own. PSA_ALG_HKDF
  1700. * should be used instead if possible. PSA_ALG_HKDF_EXTRACT is provided
  1701. * as a separate algorithm for the sake of protocols that use it as a
  1702. * building block. It may also be a slight performance optimization
  1703. * in applications that use HKDF with the same salt and key but many
  1704. * different info strings.
  1705. *
  1706. * \warning HKDF processes the salt as follows: first hash it with hash_alg
  1707. * if the salt is longer than the block size of the hash algorithm; then
  1708. * pad with null bytes up to the block size. As a result, it is possible
  1709. * for distinct salt inputs to result in the same outputs. To ensure
  1710. * unique outputs, it is recommended to use a fixed length for salt values.
  1711. *
  1712. * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
  1713. * #PSA_ALG_IS_HASH(\p hash_alg) is true).
  1714. *
  1715. * \return The corresponding HKDF-Extract algorithm.
  1716. * \return Unspecified if \p hash_alg is not a supported
  1717. * hash algorithm.
  1718. */
  1719. #define PSA_ALG_HKDF_EXTRACT(hash_alg) \
  1720. (PSA_ALG_HKDF_EXTRACT_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
  1721. /** Whether the specified algorithm is an HKDF-Extract algorithm.
  1722. *
  1723. * HKDF-Extract is a family of key derivation algorithms that are based
  1724. * on a hash function and the HMAC construction.
  1725. *
  1726. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  1727. *
  1728. * \return 1 if \c alg is an HKDF-Extract algorithm, 0 otherwise.
  1729. * This macro may return either 0 or 1 if \c alg is not a supported
  1730. * key derivation algorithm identifier.
  1731. */
  1732. #define PSA_ALG_IS_HKDF_EXTRACT(alg) \
  1733. (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXTRACT_BASE)
  1734. #define PSA_ALG_HKDF_EXPAND_BASE ((psa_algorithm_t) 0x08000500)
  1735. /** Macro to build an HKDF-Expand algorithm.
  1736. *
  1737. * For example, `PSA_ALG_HKDF_EXPAND(PSA_ALG_SHA_256)` is
  1738. * HKDF-Expand using HMAC-SHA-256.
  1739. *
  1740. * This key derivation algorithm uses the following inputs:
  1741. * - PSA_KEY_DERIVATION_INPUT_SECRET is the pseudorandom key (PRK).
  1742. * - PSA_KEY_DERIVATION_INPUT_INFO is the info string.
  1743. *
  1744. * The inputs are mandatory and must be passed in the order above.
  1745. * Each input may only be passed once.
  1746. *
  1747. * \warning HKDF-Expand is not meant to be used on its own. `PSA_ALG_HKDF`
  1748. * should be used instead if possible. `PSA_ALG_HKDF_EXPAND` is provided as
  1749. * a separate algorithm for the sake of protocols that use it as a building
  1750. * block. It may also be a slight performance optimization in applications
  1751. * that use HKDF with the same salt and key but many different info strings.
  1752. *
  1753. * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
  1754. * #PSA_ALG_IS_HASH(\p hash_alg) is true).
  1755. *
  1756. * \return The corresponding HKDF-Expand algorithm.
  1757. * \return Unspecified if \p hash_alg is not a supported
  1758. * hash algorithm.
  1759. */
  1760. #define PSA_ALG_HKDF_EXPAND(hash_alg) \
  1761. (PSA_ALG_HKDF_EXPAND_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
  1762. /** Whether the specified algorithm is an HKDF-Expand algorithm.
  1763. *
  1764. * HKDF-Expand is a family of key derivation algorithms that are based
  1765. * on a hash function and the HMAC construction.
  1766. *
  1767. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  1768. *
  1769. * \return 1 if \c alg is an HKDF-Expand algorithm, 0 otherwise.
  1770. * This macro may return either 0 or 1 if \c alg is not a supported
  1771. * key derivation algorithm identifier.
  1772. */
  1773. #define PSA_ALG_IS_HKDF_EXPAND(alg) \
  1774. (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXPAND_BASE)
  1775. /** Whether the specified algorithm is an HKDF or HKDF-Extract or
  1776. * HKDF-Expand algorithm.
  1777. *
  1778. *
  1779. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  1780. *
  1781. * \return 1 if \c alg is any HKDF type algorithm, 0 otherwise.
  1782. * This macro may return either 0 or 1 if \c alg is not a supported
  1783. * key derivation algorithm identifier.
  1784. */
  1785. #define PSA_ALG_IS_ANY_HKDF(alg) \
  1786. (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE || \
  1787. ((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXTRACT_BASE || \
  1788. ((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXPAND_BASE)
  1789. #define PSA_ALG_TLS12_PRF_BASE ((psa_algorithm_t) 0x08000200)
  1790. /** Macro to build a TLS-1.2 PRF algorithm.
  1791. *
  1792. * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,
  1793. * specified in Section 5 of RFC 5246. It is based on HMAC and can be
  1794. * used with either SHA-256 or SHA-384.
  1795. *
  1796. * This key derivation algorithm uses the following inputs, which must be
  1797. * passed in the order given here:
  1798. * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
  1799. * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
  1800. * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
  1801. *
  1802. * For the application to TLS-1.2 key expansion, the seed is the
  1803. * concatenation of ServerHello.Random + ClientHello.Random,
  1804. * and the label is "key expansion".
  1805. *
  1806. * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256)` represents the
  1807. * TLS 1.2 PRF using HMAC-SHA-256.
  1808. *
  1809. * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
  1810. * #PSA_ALG_IS_HASH(\p hash_alg) is true).
  1811. *
  1812. * \return The corresponding TLS-1.2 PRF algorithm.
  1813. * \return Unspecified if \p hash_alg is not a supported
  1814. * hash algorithm.
  1815. */
  1816. #define PSA_ALG_TLS12_PRF(hash_alg) \
  1817. (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
  1818. /** Whether the specified algorithm is a TLS-1.2 PRF algorithm.
  1819. *
  1820. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  1821. *
  1822. * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.
  1823. * This macro may return either 0 or 1 if \c alg is not a supported
  1824. * key derivation algorithm identifier.
  1825. */
  1826. #define PSA_ALG_IS_TLS12_PRF(alg) \
  1827. (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)
  1828. #define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg) \
  1829. (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
  1830. #define PSA_ALG_TLS12_PSK_TO_MS_BASE ((psa_algorithm_t) 0x08000300)
  1831. /** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.
  1832. *
  1833. * In a pure-PSK handshake in TLS 1.2, the master secret is derived
  1834. * from the PreSharedKey (PSK) through the application of padding
  1835. * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).
  1836. * The latter is based on HMAC and can be used with either SHA-256
  1837. * or SHA-384.
  1838. *
  1839. * This key derivation algorithm uses the following inputs, which must be
  1840. * passed in the order given here:
  1841. * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
  1842. * - #PSA_KEY_DERIVATION_INPUT_OTHER_SECRET is the other secret for the
  1843. * computation of the premaster secret. This input is optional;
  1844. * if omitted, it defaults to a string of null bytes with the same length
  1845. * as the secret (PSK) input.
  1846. * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
  1847. * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
  1848. *
  1849. * For the application to TLS-1.2, the seed (which is
  1850. * forwarded to the TLS-1.2 PRF) is the concatenation of the
  1851. * ClientHello.Random + ServerHello.Random,
  1852. * the label is "master secret" or "extended master secret" and
  1853. * the other secret depends on the key exchange specified in the cipher suite:
  1854. * - for a plain PSK cipher suite (RFC 4279, Section 2), omit
  1855. * PSA_KEY_DERIVATION_INPUT_OTHER_SECRET
  1856. * - for a DHE-PSK (RFC 4279, Section 3) or ECDHE-PSK cipher suite
  1857. * (RFC 5489, Section 2), the other secret should be the output of the
  1858. * PSA_ALG_FFDH or PSA_ALG_ECDH key agreement performed with the peer.
  1859. * The recommended way to pass this input is to use a key derivation
  1860. * algorithm constructed as
  1861. * PSA_ALG_KEY_AGREEMENT(ka_alg, PSA_ALG_TLS12_PSK_TO_MS(hash_alg))
  1862. * and to call psa_key_derivation_key_agreement(). Alternatively,
  1863. * this input may be an output of `psa_raw_key_agreement()` passed with
  1864. * psa_key_derivation_input_bytes(), or an equivalent input passed with
  1865. * psa_key_derivation_input_bytes() or psa_key_derivation_input_key().
  1866. * - for a RSA-PSK cipher suite (RFC 4279, Section 4), the other secret
  1867. * should be the 48-byte client challenge (the PreMasterSecret of
  1868. * (RFC 5246, Section 7.4.7.1)) concatenation of the TLS version and
  1869. * a 46-byte random string chosen by the client. On the server, this is
  1870. * typically an output of psa_asymmetric_decrypt() using
  1871. * PSA_ALG_RSA_PKCS1V15_CRYPT, passed to the key derivation operation
  1872. * with `psa_key_derivation_input_bytes()`.
  1873. *
  1874. * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256)` represents the
  1875. * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.
  1876. *
  1877. * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
  1878. * #PSA_ALG_IS_HASH(\p hash_alg) is true).
  1879. *
  1880. * \return The corresponding TLS-1.2 PSK to MS algorithm.
  1881. * \return Unspecified if \p hash_alg is not a supported
  1882. * hash algorithm.
  1883. */
  1884. #define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) \
  1885. (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
  1886. /** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.
  1887. *
  1888. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  1889. *
  1890. * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.
  1891. * This macro may return either 0 or 1 if \c alg is not a supported
  1892. * key derivation algorithm identifier.
  1893. */
  1894. #define PSA_ALG_IS_TLS12_PSK_TO_MS(alg) \
  1895. (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)
  1896. #define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg) \
  1897. (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
  1898. /* The TLS 1.2 ECJPAKE-to-PMS KDF. It takes the shared secret K (an EC point
  1899. * in case of EC J-PAKE) and calculates SHA256(K.X) that the rest of TLS 1.2
  1900. * will use to derive the session secret, as defined by step 2 of
  1901. * https://datatracker.ietf.org/doc/html/draft-cragie-tls-ecjpake-01#section-8.7.
  1902. * Uses PSA_ALG_SHA_256.
  1903. * This function takes a single input:
  1904. * #PSA_KEY_DERIVATION_INPUT_SECRET is the shared secret K from EC J-PAKE.
  1905. * The only supported curve is secp256r1 (the 256-bit curve in
  1906. * #PSA_ECC_FAMILY_SECP_R1), so the input must be exactly 65 bytes.
  1907. * The output has to be read as a single chunk of 32 bytes, defined as
  1908. * PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE.
  1909. */
  1910. #define PSA_ALG_TLS12_ECJPAKE_TO_PMS ((psa_algorithm_t) 0x08000609)
  1911. /* This flag indicates whether the key derivation algorithm is suitable for
  1912. * use on low-entropy secrets such as password - these algorithms are also
  1913. * known as key stretching or password hashing schemes. These are also the
  1914. * algorithms that accepts inputs of type #PSA_KEY_DERIVATION_INPUT_PASSWORD.
  1915. *
  1916. * Those algorithms cannot be combined with a key agreement algorithm.
  1917. */
  1918. #define PSA_ALG_KEY_DERIVATION_STRETCHING_FLAG ((psa_algorithm_t) 0x00800000)
  1919. #define PSA_ALG_PBKDF2_HMAC_BASE ((psa_algorithm_t) 0x08800100)
  1920. /** Macro to build a PBKDF2-HMAC password hashing / key stretching algorithm.
  1921. *
  1922. * PBKDF2 is defined by PKCS#5, republished as RFC 8018 (section 5.2).
  1923. * This macro specifies the PBKDF2 algorithm constructed using a PRF based on
  1924. * HMAC with the specified hash.
  1925. * For example, `PSA_ALG_PBKDF2_HMAC(PSA_ALG_SHA_256)` specifies PBKDF2
  1926. * using the PRF HMAC-SHA-256.
  1927. *
  1928. * This key derivation algorithm uses the following inputs, which must be
  1929. * provided in the following order:
  1930. * - #PSA_KEY_DERIVATION_INPUT_COST is the iteration count.
  1931. * This input step must be used exactly once.
  1932. * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt.
  1933. * This input step must be used one or more times; if used several times, the
  1934. * inputs will be concatenated. This can be used to build the final salt
  1935. * from multiple sources, both public and secret (also known as pepper).
  1936. * - #PSA_KEY_DERIVATION_INPUT_PASSWORD is the password to be hashed.
  1937. * This input step must be used exactly once.
  1938. *
  1939. * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
  1940. * #PSA_ALG_IS_HASH(\p hash_alg) is true).
  1941. *
  1942. * \return The corresponding PBKDF2-HMAC-XXX algorithm.
  1943. * \return Unspecified if \p hash_alg is not a supported
  1944. * hash algorithm.
  1945. */
  1946. #define PSA_ALG_PBKDF2_HMAC(hash_alg) \
  1947. (PSA_ALG_PBKDF2_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
  1948. /** Whether the specified algorithm is a PBKDF2-HMAC algorithm.
  1949. *
  1950. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  1951. *
  1952. * \return 1 if \c alg is a PBKDF2-HMAC algorithm, 0 otherwise.
  1953. * This macro may return either 0 or 1 if \c alg is not a supported
  1954. * key derivation algorithm identifier.
  1955. */
  1956. #define PSA_ALG_IS_PBKDF2_HMAC(alg) \
  1957. (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_PBKDF2_HMAC_BASE)
  1958. /** The PBKDF2-AES-CMAC-PRF-128 password hashing / key stretching algorithm.
  1959. *
  1960. * PBKDF2 is defined by PKCS#5, republished as RFC 8018 (section 5.2).
  1961. * This macro specifies the PBKDF2 algorithm constructed using the
  1962. * AES-CMAC-PRF-128 PRF specified by RFC 4615.
  1963. *
  1964. * This key derivation algorithm uses the same inputs as
  1965. * #PSA_ALG_PBKDF2_HMAC() with the same constraints.
  1966. */
  1967. #define PSA_ALG_PBKDF2_AES_CMAC_PRF_128 ((psa_algorithm_t) 0x08800200)
  1968. #define PSA_ALG_KEY_DERIVATION_MASK ((psa_algorithm_t) 0xfe00ffff)
  1969. #define PSA_ALG_KEY_AGREEMENT_MASK ((psa_algorithm_t) 0xffff0000)
  1970. /** Macro to build a combined algorithm that chains a key agreement with
  1971. * a key derivation.
  1972. *
  1973. * \param ka_alg A key agreement algorithm (\c PSA_ALG_XXX value such
  1974. * that #PSA_ALG_IS_KEY_AGREEMENT(\p ka_alg) is true).
  1975. * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
  1976. * that #PSA_ALG_IS_KEY_DERIVATION(\p kdf_alg) is true).
  1977. *
  1978. * \return The corresponding key agreement and derivation
  1979. * algorithm.
  1980. * \return Unspecified if \p ka_alg is not a supported
  1981. * key agreement algorithm or \p kdf_alg is not a
  1982. * supported key derivation algorithm.
  1983. */
  1984. #define PSA_ALG_KEY_AGREEMENT(ka_alg, kdf_alg) \
  1985. ((ka_alg) | (kdf_alg))
  1986. #define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) \
  1987. (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)
  1988. #define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) \
  1989. (((alg) & PSA_ALG_KEY_AGREEMENT_MASK) | PSA_ALG_CATEGORY_KEY_AGREEMENT)
  1990. /** Whether the specified algorithm is a raw key agreement algorithm.
  1991. *
  1992. * A raw key agreement algorithm is one that does not specify
  1993. * a key derivation function.
  1994. * Usually, raw key agreement algorithms are constructed directly with
  1995. * a \c PSA_ALG_xxx macro while non-raw key agreement algorithms are
  1996. * constructed with #PSA_ALG_KEY_AGREEMENT().
  1997. *
  1998. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  1999. *
  2000. * \return 1 if \p alg is a raw key agreement algorithm, 0 otherwise.
  2001. * This macro may return either 0 or 1 if \p alg is not a supported
  2002. * algorithm identifier.
  2003. */
  2004. #define PSA_ALG_IS_RAW_KEY_AGREEMENT(alg) \
  2005. (PSA_ALG_IS_KEY_AGREEMENT(alg) && \
  2006. PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) == PSA_ALG_CATEGORY_KEY_DERIVATION)
  2007. #define PSA_ALG_IS_KEY_DERIVATION_OR_AGREEMENT(alg) \
  2008. ((PSA_ALG_IS_KEY_DERIVATION(alg) || PSA_ALG_IS_KEY_AGREEMENT(alg)))
  2009. /** The finite-field Diffie-Hellman (DH) key agreement algorithm.
  2010. *
  2011. * The shared secret produced by key agreement is
  2012. * `g^{ab}` in big-endian format.
  2013. * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`
  2014. * in bits.
  2015. */
  2016. #define PSA_ALG_FFDH ((psa_algorithm_t) 0x09010000)
  2017. /** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.
  2018. *
  2019. * This includes the raw finite field Diffie-Hellman algorithm as well as
  2020. * finite-field Diffie-Hellman followed by any supporter key derivation
  2021. * algorithm.
  2022. *
  2023. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  2024. *
  2025. * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.
  2026. * This macro may return either 0 or 1 if \c alg is not a supported
  2027. * key agreement algorithm identifier.
  2028. */
  2029. #define PSA_ALG_IS_FFDH(alg) \
  2030. (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH)
  2031. /** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.
  2032. *
  2033. * The shared secret produced by key agreement is the x-coordinate of
  2034. * the shared secret point. It is always `ceiling(m / 8)` bytes long where
  2035. * `m` is the bit size associated with the curve, i.e. the bit size of the
  2036. * order of the curve's coordinate field. When `m` is not a multiple of 8,
  2037. * the byte containing the most significant bit of the shared secret
  2038. * is padded with zero bits. The byte order is either little-endian
  2039. * or big-endian depending on the curve type.
  2040. *
  2041. * - For Montgomery curves (curve types `PSA_ECC_FAMILY_CURVEXXX`),
  2042. * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
  2043. * in little-endian byte order.
  2044. * The bit size is 448 for Curve448 and 255 for Curve25519.
  2045. * - For Weierstrass curves over prime fields (curve types
  2046. * `PSA_ECC_FAMILY_SECPXXX` and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`),
  2047. * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
  2048. * in big-endian byte order.
  2049. * The bit size is `m = ceiling(log_2(p))` for the field `F_p`.
  2050. * - For Weierstrass curves over binary fields (curve types
  2051. * `PSA_ECC_FAMILY_SECTXXX`),
  2052. * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
  2053. * in big-endian byte order.
  2054. * The bit size is `m` for the field `F_{2^m}`.
  2055. */
  2056. #define PSA_ALG_ECDH ((psa_algorithm_t) 0x09020000)
  2057. /** Whether the specified algorithm is an elliptic curve Diffie-Hellman
  2058. * algorithm.
  2059. *
  2060. * This includes the raw elliptic curve Diffie-Hellman algorithm as well as
  2061. * elliptic curve Diffie-Hellman followed by any supporter key derivation
  2062. * algorithm.
  2063. *
  2064. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  2065. *
  2066. * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,
  2067. * 0 otherwise.
  2068. * This macro may return either 0 or 1 if \c alg is not a supported
  2069. * key agreement algorithm identifier.
  2070. */
  2071. #define PSA_ALG_IS_ECDH(alg) \
  2072. (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH)
  2073. /** Whether the specified algorithm encoding is a wildcard.
  2074. *
  2075. * Wildcard values may only be used to set the usage algorithm field in
  2076. * a policy, not to perform an operation.
  2077. *
  2078. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  2079. *
  2080. * \return 1 if \c alg is a wildcard algorithm encoding.
  2081. * \return 0 if \c alg is a non-wildcard algorithm encoding (suitable for
  2082. * an operation).
  2083. * \return This macro may return either 0 or 1 if \c alg is not a supported
  2084. * algorithm identifier.
  2085. */
  2086. #define PSA_ALG_IS_WILDCARD(alg) \
  2087. (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
  2088. PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH : \
  2089. PSA_ALG_IS_MAC(alg) ? \
  2090. (alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0 : \
  2091. PSA_ALG_IS_AEAD(alg) ? \
  2092. (alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0 : \
  2093. (alg) == PSA_ALG_ANY_HASH)
  2094. /** Get the hash used by a composite algorithm.
  2095. *
  2096. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  2097. *
  2098. * \return The underlying hash algorithm if alg is a composite algorithm that
  2099. * uses a hash algorithm.
  2100. *
  2101. * \return \c 0 if alg is not a composite algorithm that uses a hash.
  2102. */
  2103. #define PSA_ALG_GET_HASH(alg) \
  2104. (((alg) & 0x000000ff) == 0 ? ((psa_algorithm_t) 0) : 0x02000000 | ((alg) & 0x000000ff))
  2105. /**@}*/
  2106. /** \defgroup key_lifetimes Key lifetimes
  2107. * @{
  2108. */
  2109. /* Note that location and persistence level values are embedded in the
  2110. * persistent key store, as part of key metadata. As a consequence, they
  2111. * must not be changed (unless the storage format version changes).
  2112. */
  2113. /** The default lifetime for volatile keys.
  2114. *
  2115. * A volatile key only exists as long as the identifier to it is not destroyed.
  2116. * The key material is guaranteed to be erased on a power reset.
  2117. *
  2118. * A key with this lifetime is typically stored in the RAM area of the
  2119. * PSA Crypto subsystem. However this is an implementation choice.
  2120. * If an implementation stores data about the key in a non-volatile memory,
  2121. * it must release all the resources associated with the key and erase the
  2122. * key material if the calling application terminates.
  2123. */
  2124. #define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t) 0x00000000)
  2125. /** The default lifetime for persistent keys.
  2126. *
  2127. * A persistent key remains in storage until it is explicitly destroyed or
  2128. * until the corresponding storage area is wiped. This specification does
  2129. * not define any mechanism to wipe a storage area, but integrations may
  2130. * provide their own mechanism (for example to perform a factory reset,
  2131. * to prepare for device refurbishment, or to uninstall an application).
  2132. *
  2133. * This lifetime value is the default storage area for the calling
  2134. * application. Integrations of Mbed TLS may support other persistent lifetimes.
  2135. * See ::psa_key_lifetime_t for more information.
  2136. */
  2137. #define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t) 0x00000001)
  2138. /** The persistence level of volatile keys.
  2139. *
  2140. * See ::psa_key_persistence_t for more information.
  2141. */
  2142. #define PSA_KEY_PERSISTENCE_VOLATILE ((psa_key_persistence_t) 0x00)
  2143. /** The default persistence level for persistent keys.
  2144. *
  2145. * See ::psa_key_persistence_t for more information.
  2146. */
  2147. #define PSA_KEY_PERSISTENCE_DEFAULT ((psa_key_persistence_t) 0x01)
  2148. /** A persistence level indicating that a key is never destroyed.
  2149. *
  2150. * See ::psa_key_persistence_t for more information.
  2151. */
  2152. #define PSA_KEY_PERSISTENCE_READ_ONLY ((psa_key_persistence_t) 0xff)
  2153. #define PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) \
  2154. ((psa_key_persistence_t) ((lifetime) & 0x000000ff))
  2155. #define PSA_KEY_LIFETIME_GET_LOCATION(lifetime) \
  2156. ((psa_key_location_t) ((lifetime) >> 8))
  2157. /** Whether a key lifetime indicates that the key is volatile.
  2158. *
  2159. * A volatile key is automatically destroyed by the implementation when
  2160. * the application instance terminates. In particular, a volatile key
  2161. * is automatically destroyed on a power reset of the device.
  2162. *
  2163. * A key that is not volatile is persistent. Persistent keys are
  2164. * preserved until the application explicitly destroys them or until an
  2165. * implementation-specific device management event occurs (for example,
  2166. * a factory reset).
  2167. *
  2168. * \param lifetime The lifetime value to query (value of type
  2169. * ::psa_key_lifetime_t).
  2170. *
  2171. * \return \c 1 if the key is volatile, otherwise \c 0.
  2172. */
  2173. #define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime) \
  2174. (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
  2175. PSA_KEY_PERSISTENCE_VOLATILE)
  2176. /** Whether a key lifetime indicates that the key is read-only.
  2177. *
  2178. * Read-only keys cannot be created or destroyed through the PSA Crypto API.
  2179. * They must be created through platform-specific means that bypass the API.
  2180. *
  2181. * Some platforms may offer ways to destroy read-only keys. For example,
  2182. * consider a platform with multiple levels of privilege, where a
  2183. * low-privilege application can use a key but is not allowed to destroy
  2184. * it, and the platform exposes the key to the application with a read-only
  2185. * lifetime. High-privilege code can destroy the key even though the
  2186. * application sees the key as read-only.
  2187. *
  2188. * \param lifetime The lifetime value to query (value of type
  2189. * ::psa_key_lifetime_t).
  2190. *
  2191. * \return \c 1 if the key is read-only, otherwise \c 0.
  2192. */
  2193. #define PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime) \
  2194. (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
  2195. PSA_KEY_PERSISTENCE_READ_ONLY)
  2196. /** Construct a lifetime from a persistence level and a location.
  2197. *
  2198. * \param persistence The persistence level
  2199. * (value of type ::psa_key_persistence_t).
  2200. * \param location The location indicator
  2201. * (value of type ::psa_key_location_t).
  2202. *
  2203. * \return The constructed lifetime value.
  2204. */
  2205. #define PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(persistence, location) \
  2206. ((location) << 8 | (persistence))
  2207. /** The local storage area for persistent keys.
  2208. *
  2209. * This storage area is available on all systems that can store persistent
  2210. * keys without delegating the storage to a third-party cryptoprocessor.
  2211. *
  2212. * See ::psa_key_location_t for more information.
  2213. */
  2214. #define PSA_KEY_LOCATION_LOCAL_STORAGE ((psa_key_location_t) 0x000000)
  2215. #define PSA_KEY_LOCATION_VENDOR_FLAG ((psa_key_location_t) 0x800000)
  2216. /* Note that key identifier values are embedded in the
  2217. * persistent key store, as part of key metadata. As a consequence, they
  2218. * must not be changed (unless the storage format version changes).
  2219. */
  2220. /** The null key identifier.
  2221. */
  2222. /* *INDENT-OFF* (https://github.com/ARM-software/psa-arch-tests/issues/337) */
  2223. #define PSA_KEY_ID_NULL ((psa_key_id_t)0)
  2224. /* *INDENT-ON* */
  2225. /** The minimum value for a key identifier chosen by the application.
  2226. */
  2227. #define PSA_KEY_ID_USER_MIN ((psa_key_id_t) 0x00000001)
  2228. /** The maximum value for a key identifier chosen by the application.
  2229. */
  2230. #define PSA_KEY_ID_USER_MAX ((psa_key_id_t) 0x3fffffff)
  2231. /** The minimum value for a key identifier chosen by the implementation.
  2232. */
  2233. #define PSA_KEY_ID_VENDOR_MIN ((psa_key_id_t) 0x40000000)
  2234. /** The maximum value for a key identifier chosen by the implementation.
  2235. */
  2236. #define PSA_KEY_ID_VENDOR_MAX ((psa_key_id_t) 0x7fffffff)
  2237. #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
  2238. #define MBEDTLS_SVC_KEY_ID_INIT ((psa_key_id_t) 0)
  2239. #define MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id) (id)
  2240. #define MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(id) (0)
  2241. /** Utility to initialize a key identifier at runtime.
  2242. *
  2243. * \param unused Unused parameter.
  2244. * \param key_id Identifier of the key.
  2245. */
  2246. static inline mbedtls_svc_key_id_t mbedtls_svc_key_id_make(
  2247. unsigned int unused, psa_key_id_t key_id)
  2248. {
  2249. (void) unused;
  2250. return key_id;
  2251. }
  2252. /** Compare two key identifiers.
  2253. *
  2254. * \param id1 First key identifier.
  2255. * \param id2 Second key identifier.
  2256. *
  2257. * \return Non-zero if the two key identifier are equal, zero otherwise.
  2258. */
  2259. static inline int mbedtls_svc_key_id_equal(mbedtls_svc_key_id_t id1,
  2260. mbedtls_svc_key_id_t id2)
  2261. {
  2262. return id1 == id2;
  2263. }
  2264. /** Check whether a key identifier is null.
  2265. *
  2266. * \param key Key identifier.
  2267. *
  2268. * \return Non-zero if the key identifier is null, zero otherwise.
  2269. */
  2270. static inline int mbedtls_svc_key_id_is_null(mbedtls_svc_key_id_t key)
  2271. {
  2272. return key == 0;
  2273. }
  2274. #else /* MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */
  2275. #define MBEDTLS_SVC_KEY_ID_INIT ((mbedtls_svc_key_id_t){ 0, 0 })
  2276. #define MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id) ((id).MBEDTLS_PRIVATE(key_id))
  2277. #define MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(id) ((id).MBEDTLS_PRIVATE(owner))
  2278. /** Utility to initialize a key identifier at runtime.
  2279. *
  2280. * \param owner_id Identifier of the key owner.
  2281. * \param key_id Identifier of the key.
  2282. */
  2283. static inline mbedtls_svc_key_id_t mbedtls_svc_key_id_make(
  2284. mbedtls_key_owner_id_t owner_id, psa_key_id_t key_id)
  2285. {
  2286. return (mbedtls_svc_key_id_t){ .MBEDTLS_PRIVATE(key_id) = key_id,
  2287. .MBEDTLS_PRIVATE(owner) = owner_id };
  2288. }
  2289. /** Compare two key identifiers.
  2290. *
  2291. * \param id1 First key identifier.
  2292. * \param id2 Second key identifier.
  2293. *
  2294. * \return Non-zero if the two key identifier are equal, zero otherwise.
  2295. */
  2296. static inline int mbedtls_svc_key_id_equal(mbedtls_svc_key_id_t id1,
  2297. mbedtls_svc_key_id_t id2)
  2298. {
  2299. return (id1.MBEDTLS_PRIVATE(key_id) == id2.MBEDTLS_PRIVATE(key_id)) &&
  2300. mbedtls_key_owner_id_equal(id1.MBEDTLS_PRIVATE(owner), id2.MBEDTLS_PRIVATE(owner));
  2301. }
  2302. /** Check whether a key identifier is null.
  2303. *
  2304. * \param key Key identifier.
  2305. *
  2306. * \return Non-zero if the key identifier is null, zero otherwise.
  2307. */
  2308. static inline int mbedtls_svc_key_id_is_null(mbedtls_svc_key_id_t key)
  2309. {
  2310. return key.MBEDTLS_PRIVATE(key_id) == 0;
  2311. }
  2312. #endif /* !MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */
  2313. /**@}*/
  2314. /** \defgroup policy Key policies
  2315. * @{
  2316. */
  2317. /* Note that key usage flags are embedded in the
  2318. * persistent key store, as part of key metadata. As a consequence, they
  2319. * must not be changed (unless the storage format version changes).
  2320. */
  2321. /** Whether the key may be exported.
  2322. *
  2323. * A public key or the public part of a key pair may always be exported
  2324. * regardless of the value of this permission flag.
  2325. *
  2326. * If a key does not have export permission, implementations shall not
  2327. * allow the key to be exported in plain form from the cryptoprocessor,
  2328. * whether through psa_export_key() or through a proprietary interface.
  2329. * The key may however be exportable in a wrapped form, i.e. in a form
  2330. * where it is encrypted by another key.
  2331. */
  2332. #define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t) 0x00000001)
  2333. /** Whether the key may be copied.
  2334. *
  2335. * This flag allows the use of psa_copy_key() to make a copy of the key
  2336. * with the same policy or a more restrictive policy.
  2337. *
  2338. * For lifetimes for which the key is located in a secure element which
  2339. * enforce the non-exportability of keys, copying a key outside the secure
  2340. * element also requires the usage flag #PSA_KEY_USAGE_EXPORT.
  2341. * Copying the key inside the secure element is permitted with just
  2342. * #PSA_KEY_USAGE_COPY if the secure element supports it.
  2343. * For keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE or
  2344. * #PSA_KEY_LIFETIME_PERSISTENT, the usage flag #PSA_KEY_USAGE_COPY
  2345. * is sufficient to permit the copy.
  2346. */
  2347. #define PSA_KEY_USAGE_COPY ((psa_key_usage_t) 0x00000002)
  2348. /** Whether the key may be used to encrypt a message.
  2349. *
  2350. * This flag allows the key to be used for a symmetric encryption operation,
  2351. * for an AEAD encryption-and-authentication operation,
  2352. * or for an asymmetric encryption operation,
  2353. * if otherwise permitted by the key's type and policy.
  2354. *
  2355. * For a key pair, this concerns the public key.
  2356. */
  2357. #define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t) 0x00000100)
  2358. /** Whether the key may be used to decrypt a message.
  2359. *
  2360. * This flag allows the key to be used for a symmetric decryption operation,
  2361. * for an AEAD decryption-and-verification operation,
  2362. * or for an asymmetric decryption operation,
  2363. * if otherwise permitted by the key's type and policy.
  2364. *
  2365. * For a key pair, this concerns the private key.
  2366. */
  2367. #define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t) 0x00000200)
  2368. /** Whether the key may be used to sign a message.
  2369. *
  2370. * This flag allows the key to be used for a MAC calculation operation or for
  2371. * an asymmetric message signature operation, if otherwise permitted by the
  2372. * key’s type and policy.
  2373. *
  2374. * For a key pair, this concerns the private key.
  2375. */
  2376. #define PSA_KEY_USAGE_SIGN_MESSAGE ((psa_key_usage_t) 0x00000400)
  2377. /** Whether the key may be used to verify a message.
  2378. *
  2379. * This flag allows the key to be used for a MAC verification operation or for
  2380. * an asymmetric message signature verification operation, if otherwise
  2381. * permitted by the key’s type and policy.
  2382. *
  2383. * For a key pair, this concerns the public key.
  2384. */
  2385. #define PSA_KEY_USAGE_VERIFY_MESSAGE ((psa_key_usage_t) 0x00000800)
  2386. /** Whether the key may be used to sign a message.
  2387. *
  2388. * This flag allows the key to be used for a MAC calculation operation
  2389. * or for an asymmetric signature operation,
  2390. * if otherwise permitted by the key's type and policy.
  2391. *
  2392. * For a key pair, this concerns the private key.
  2393. */
  2394. #define PSA_KEY_USAGE_SIGN_HASH ((psa_key_usage_t) 0x00001000)
  2395. /** Whether the key may be used to verify a message signature.
  2396. *
  2397. * This flag allows the key to be used for a MAC verification operation
  2398. * or for an asymmetric signature verification operation,
  2399. * if otherwise permitted by the key's type and policy.
  2400. *
  2401. * For a key pair, this concerns the public key.
  2402. */
  2403. #define PSA_KEY_USAGE_VERIFY_HASH ((psa_key_usage_t) 0x00002000)
  2404. /** Whether the key may be used to derive other keys or produce a password
  2405. * hash.
  2406. *
  2407. * This flag allows the key to be used for a key derivation operation or for
  2408. * a key agreement operation, if otherwise permitted by the key's type and
  2409. * policy.
  2410. *
  2411. * If this flag is present on all keys used in calls to
  2412. * psa_key_derivation_input_key() for a key derivation operation, then it
  2413. * permits calling psa_key_derivation_output_bytes() or
  2414. * psa_key_derivation_output_key() at the end of the operation.
  2415. */
  2416. #define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t) 0x00004000)
  2417. /** Whether the key may be used to verify the result of a key derivation,
  2418. * including password hashing.
  2419. *
  2420. * This flag allows the key to be used:
  2421. *
  2422. * This flag allows the key to be used in a key derivation operation, if
  2423. * otherwise permitted by the key's type and policy.
  2424. *
  2425. * If this flag is present on all keys used in calls to
  2426. * psa_key_derivation_input_key() for a key derivation operation, then it
  2427. * permits calling psa_key_derivation_verify_bytes() or
  2428. * psa_key_derivation_verify_key() at the end of the operation.
  2429. */
  2430. #define PSA_KEY_USAGE_VERIFY_DERIVATION ((psa_key_usage_t) 0x00008000)
  2431. /**@}*/
  2432. /** \defgroup derivation Key derivation
  2433. * @{
  2434. */
  2435. /* Key input steps are not embedded in the persistent storage, so you can
  2436. * change them if needed: it's only an ABI change. */
  2437. /** A secret input for key derivation.
  2438. *
  2439. * This should be a key of type #PSA_KEY_TYPE_DERIVE
  2440. * (passed to psa_key_derivation_input_key())
  2441. * or the shared secret resulting from a key agreement
  2442. * (obtained via psa_key_derivation_key_agreement()).
  2443. *
  2444. * The secret can also be a direct input (passed to
  2445. * key_derivation_input_bytes()). In this case, the derivation operation
  2446. * may not be used to derive keys: the operation will only allow
  2447. * psa_key_derivation_output_bytes(),
  2448. * psa_key_derivation_verify_bytes(), or
  2449. * psa_key_derivation_verify_key(), but not
  2450. * psa_key_derivation_output_key().
  2451. */
  2452. #define PSA_KEY_DERIVATION_INPUT_SECRET ((psa_key_derivation_step_t) 0x0101)
  2453. /** A low-entropy secret input for password hashing / key stretching.
  2454. *
  2455. * This is usually a key of type #PSA_KEY_TYPE_PASSWORD (passed to
  2456. * psa_key_derivation_input_key()) or a direct input (passed to
  2457. * psa_key_derivation_input_bytes()) that is a password or passphrase. It can
  2458. * also be high-entropy secret such as a key of type #PSA_KEY_TYPE_DERIVE or
  2459. * the shared secret resulting from a key agreement.
  2460. *
  2461. * The secret can also be a direct input (passed to
  2462. * key_derivation_input_bytes()). In this case, the derivation operation
  2463. * may not be used to derive keys: the operation will only allow
  2464. * psa_key_derivation_output_bytes(),
  2465. * psa_key_derivation_verify_bytes(), or
  2466. * psa_key_derivation_verify_key(), but not
  2467. * psa_key_derivation_output_key().
  2468. */
  2469. #define PSA_KEY_DERIVATION_INPUT_PASSWORD ((psa_key_derivation_step_t) 0x0102)
  2470. /** A high-entropy additional secret input for key derivation.
  2471. *
  2472. * This is typically the shared secret resulting from a key agreement obtained
  2473. * via `psa_key_derivation_key_agreement()`. It may alternatively be a key of
  2474. * type `PSA_KEY_TYPE_DERIVE` passed to `psa_key_derivation_input_key()`, or
  2475. * a direct input passed to `psa_key_derivation_input_bytes()`.
  2476. */
  2477. #define PSA_KEY_DERIVATION_INPUT_OTHER_SECRET \
  2478. ((psa_key_derivation_step_t) 0x0103)
  2479. /** A label for key derivation.
  2480. *
  2481. * This should be a direct input.
  2482. * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
  2483. */
  2484. #define PSA_KEY_DERIVATION_INPUT_LABEL ((psa_key_derivation_step_t) 0x0201)
  2485. /** A salt for key derivation.
  2486. *
  2487. * This should be a direct input.
  2488. * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA or
  2489. * #PSA_KEY_TYPE_PEPPER.
  2490. */
  2491. #define PSA_KEY_DERIVATION_INPUT_SALT ((psa_key_derivation_step_t) 0x0202)
  2492. /** An information string for key derivation.
  2493. *
  2494. * This should be a direct input.
  2495. * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
  2496. */
  2497. #define PSA_KEY_DERIVATION_INPUT_INFO ((psa_key_derivation_step_t) 0x0203)
  2498. /** A seed for key derivation.
  2499. *
  2500. * This should be a direct input.
  2501. * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
  2502. */
  2503. #define PSA_KEY_DERIVATION_INPUT_SEED ((psa_key_derivation_step_t) 0x0204)
  2504. /** A cost parameter for password hashing / key stretching.
  2505. *
  2506. * This must be a direct input, passed to psa_key_derivation_input_integer().
  2507. */
  2508. #define PSA_KEY_DERIVATION_INPUT_COST ((psa_key_derivation_step_t) 0x0205)
  2509. /**@}*/
  2510. /** \defgroup helper_macros Helper macros
  2511. * @{
  2512. */
  2513. /* Helper macros */
  2514. /** Check if two AEAD algorithm identifiers refer to the same AEAD algorithm
  2515. * regardless of the tag length they encode.
  2516. *
  2517. * \param aead_alg_1 An AEAD algorithm identifier.
  2518. * \param aead_alg_2 An AEAD algorithm identifier.
  2519. *
  2520. * \return 1 if both identifiers refer to the same AEAD algorithm,
  2521. * 0 otherwise.
  2522. * Unspecified if neither \p aead_alg_1 nor \p aead_alg_2 are
  2523. * a supported AEAD algorithm.
  2524. */
  2525. #define MBEDTLS_PSA_ALG_AEAD_EQUAL(aead_alg_1, aead_alg_2) \
  2526. (!(((aead_alg_1) ^ (aead_alg_2)) & \
  2527. ~(PSA_ALG_AEAD_TAG_LENGTH_MASK | PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)))
  2528. /**@}*/
  2529. /**@}*/
  2530. /** \defgroup interruptible Interruptible operations
  2531. * @{
  2532. */
  2533. /** Maximum value for use with \c psa_interruptible_set_max_ops() to determine
  2534. * the maximum number of ops allowed to be executed by an interruptible
  2535. * function in a single call.
  2536. */
  2537. #define PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED UINT32_MAX
  2538. /**@}*/
  2539. #endif /* PSA_CRYPTO_VALUES_H */