crypto_values.h 99 KB

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