crypto_values.h 107 KB

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