crypto_values.h 120 KB

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