crypto.h 179 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951
  1. /**
  2. * \file psa/crypto.h
  3. * \brief Platform Security Architecture cryptography module
  4. */
  5. /*
  6. * Copyright The Mbed TLS Contributors
  7. * SPDX-License-Identifier: Apache-2.0
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10. * not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. #ifndef PSA_CRYPTO_H
  22. #define PSA_CRYPTO_H
  23. #include "crypto_platform.h"
  24. #include <stddef.h>
  25. #ifdef __DOXYGEN_ONLY__
  26. /* This __DOXYGEN_ONLY__ block contains mock definitions for things that
  27. * must be defined in the crypto_platform.h header. These mock definitions
  28. * are present in this file as a convenience to generate pretty-printed
  29. * documentation that includes those definitions. */
  30. /** \defgroup platform Implementation-specific definitions
  31. * @{
  32. */
  33. /**@}*/
  34. #endif /* __DOXYGEN_ONLY__ */
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /* The file "crypto_types.h" declares types that encode errors,
  39. * algorithms, key types, policies, etc. */
  40. #include "crypto_types.h"
  41. /** \defgroup version API version
  42. * @{
  43. */
  44. /**
  45. * The major version of this implementation of the PSA Crypto API
  46. */
  47. #define PSA_CRYPTO_API_VERSION_MAJOR 1
  48. /**
  49. * The minor version of this implementation of the PSA Crypto API
  50. */
  51. #define PSA_CRYPTO_API_VERSION_MINOR 0
  52. /**@}*/
  53. /* The file "crypto_values.h" declares macros to build and analyze values
  54. * of integral types defined in "crypto_types.h". */
  55. #include "crypto_values.h"
  56. /** \defgroup initialization Library initialization
  57. * @{
  58. */
  59. /**
  60. * \brief Library initialization.
  61. *
  62. * Applications must call this function before calling any other
  63. * function in this module.
  64. *
  65. * Applications may call this function more than once. Once a call
  66. * succeeds, subsequent calls are guaranteed to succeed.
  67. *
  68. * If the application calls other functions before calling psa_crypto_init(),
  69. * the behavior is undefined. Implementations are encouraged to either perform
  70. * the operation as if the library had been initialized or to return
  71. * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
  72. * implementations should not return a success status if the lack of
  73. * initialization may have security implications, for example due to improper
  74. * seeding of the random number generator.
  75. *
  76. * \retval #PSA_SUCCESS
  77. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  78. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
  79. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  80. * \retval #PSA_ERROR_HARDWARE_FAILURE
  81. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  82. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
  83. * \retval #PSA_ERROR_STORAGE_FAILURE
  84. * \retval #PSA_ERROR_DATA_INVALID
  85. * \retval #PSA_ERROR_DATA_CORRUPT
  86. */
  87. psa_status_t psa_crypto_init(void);
  88. /**@}*/
  89. /** \addtogroup attributes
  90. * @{
  91. */
  92. /** \def PSA_KEY_ATTRIBUTES_INIT
  93. *
  94. * This macro returns a suitable initializer for a key attribute structure
  95. * of type #psa_key_attributes_t.
  96. */
  97. #ifdef __DOXYGEN_ONLY__
  98. /* This is an example definition for documentation purposes.
  99. * Implementations should define a suitable value in `crypto_struct.h`.
  100. */
  101. #define PSA_KEY_ATTRIBUTES_INIT {0}
  102. #endif
  103. /** Return an initial value for a key attributes structure.
  104. */
  105. static psa_key_attributes_t psa_key_attributes_init(void);
  106. /** Declare a key as persistent and set its key identifier.
  107. *
  108. * If the attribute structure currently declares the key as volatile (which
  109. * is the default content of an attribute structure), this function sets
  110. * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
  111. *
  112. * This function does not access storage, it merely stores the given
  113. * value in the structure.
  114. * The persistent key will be written to storage when the attribute
  115. * structure is passed to a key creation function such as
  116. * psa_import_key(), psa_generate_key(),
  117. * psa_key_derivation_output_key() or psa_copy_key().
  118. *
  119. * This function may be declared as `static` (i.e. without external
  120. * linkage). This function may be provided as a function-like macro,
  121. * but in this case it must evaluate each of its arguments exactly once.
  122. *
  123. * \param[out] attributes The attribute structure to write to.
  124. * \param key The persistent identifier for the key.
  125. */
  126. static void psa_set_key_id( psa_key_attributes_t *attributes,
  127. mbedtls_svc_key_id_t key );
  128. #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
  129. /** Set the owner identifier of a key.
  130. *
  131. * When key identifiers encode key owner identifiers, psa_set_key_id() does
  132. * not allow to define in key attributes the owner of volatile keys as
  133. * psa_set_key_id() enforces the key to be persistent.
  134. *
  135. * This function allows to set in key attributes the owner identifier of a
  136. * key. It is intended to be used for volatile keys. For persistent keys,
  137. * it is recommended to use the PSA Cryptography API psa_set_key_id() to define
  138. * the owner of a key.
  139. *
  140. * \param[out] attributes The attribute structure to write to.
  141. * \param owner_id The key owner identifier.
  142. */
  143. static void mbedtls_set_key_owner_id( psa_key_attributes_t *attributes,
  144. mbedtls_key_owner_id_t owner_id );
  145. #endif
  146. /** Set the location of a persistent key.
  147. *
  148. * To make a key persistent, you must give it a persistent key identifier
  149. * with psa_set_key_id(). By default, a key that has a persistent identifier
  150. * is stored in the default storage area identifier by
  151. * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
  152. * area, or to explicitly declare the key as volatile.
  153. *
  154. * This function does not access storage, it merely stores the given
  155. * value in the structure.
  156. * The persistent key will be written to storage when the attribute
  157. * structure is passed to a key creation function such as
  158. * psa_import_key(), psa_generate_key(),
  159. * psa_key_derivation_output_key() or psa_copy_key().
  160. *
  161. * This function may be declared as `static` (i.e. without external
  162. * linkage). This function may be provided as a function-like macro,
  163. * but in this case it must evaluate each of its arguments exactly once.
  164. *
  165. * \param[out] attributes The attribute structure to write to.
  166. * \param lifetime The lifetime for the key.
  167. * If this is #PSA_KEY_LIFETIME_VOLATILE, the
  168. * key will be volatile, and the key identifier
  169. * attribute is reset to 0.
  170. */
  171. static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
  172. psa_key_lifetime_t lifetime);
  173. /** Retrieve the key identifier from key attributes.
  174. *
  175. * This function may be declared as `static` (i.e. without external
  176. * linkage). This function may be provided as a function-like macro,
  177. * but in this case it must evaluate its argument exactly once.
  178. *
  179. * \param[in] attributes The key attribute structure to query.
  180. *
  181. * \return The persistent identifier stored in the attribute structure.
  182. * This value is unspecified if the attribute structure declares
  183. * the key as volatile.
  184. */
  185. static mbedtls_svc_key_id_t psa_get_key_id(
  186. const psa_key_attributes_t *attributes);
  187. /** Retrieve the lifetime from key attributes.
  188. *
  189. * This function may be declared as `static` (i.e. without external
  190. * linkage). This function may be provided as a function-like macro,
  191. * but in this case it must evaluate its argument exactly once.
  192. *
  193. * \param[in] attributes The key attribute structure to query.
  194. *
  195. * \return The lifetime value stored in the attribute structure.
  196. */
  197. static psa_key_lifetime_t psa_get_key_lifetime(
  198. const psa_key_attributes_t *attributes);
  199. /** Declare usage flags for a key.
  200. *
  201. * Usage flags are part of a key's usage policy. They encode what
  202. * kind of operations are permitted on the key. For more details,
  203. * refer to the documentation of the type #psa_key_usage_t.
  204. *
  205. * This function overwrites any usage flags
  206. * previously set in \p attributes.
  207. *
  208. * This function may be declared as `static` (i.e. without external
  209. * linkage). This function may be provided as a function-like macro,
  210. * but in this case it must evaluate each of its arguments exactly once.
  211. *
  212. * \param[out] attributes The attribute structure to write to.
  213. * \param usage_flags The usage flags to write.
  214. */
  215. static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
  216. psa_key_usage_t usage_flags);
  217. /** Retrieve the usage flags from key attributes.
  218. *
  219. * This function may be declared as `static` (i.e. without external
  220. * linkage). This function may be provided as a function-like macro,
  221. * but in this case it must evaluate its argument exactly once.
  222. *
  223. * \param[in] attributes The key attribute structure to query.
  224. *
  225. * \return The usage flags stored in the attribute structure.
  226. */
  227. static psa_key_usage_t psa_get_key_usage_flags(
  228. const psa_key_attributes_t *attributes);
  229. /** Declare the permitted algorithm policy for a key.
  230. *
  231. * The permitted algorithm policy of a key encodes which algorithm or
  232. * algorithms are permitted to be used with this key. The following
  233. * algorithm policies are supported:
  234. * - 0 does not allow any cryptographic operation with the key. The key
  235. * may be used for non-cryptographic actions such as exporting (if
  236. * permitted by the usage flags).
  237. * - An algorithm value permits this particular algorithm.
  238. * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
  239. * signature scheme with any hash algorithm.
  240. * - An algorithm built from #PSA_ALG_AT_LEAST_THIS_LENGTH_MAC allows
  241. * any MAC algorithm from the same base class (e.g. CMAC) which
  242. * generates/verifies a MAC length greater than or equal to the length
  243. * encoded in the wildcard algorithm.
  244. * - An algorithm built from #PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG
  245. * allows any AEAD algorithm from the same base class (e.g. CCM) which
  246. * generates/verifies a tag length greater than or equal to the length
  247. * encoded in the wildcard algorithm.
  248. *
  249. * This function overwrites any algorithm policy
  250. * previously set in \p attributes.
  251. *
  252. * This function may be declared as `static` (i.e. without external
  253. * linkage). This function may be provided as a function-like macro,
  254. * but in this case it must evaluate each of its arguments exactly once.
  255. *
  256. * \param[out] attributes The attribute structure to write to.
  257. * \param alg The permitted algorithm policy to write.
  258. */
  259. static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
  260. psa_algorithm_t alg);
  261. /** Retrieve the algorithm policy from key attributes.
  262. *
  263. * This function may be declared as `static` (i.e. without external
  264. * linkage). This function may be provided as a function-like macro,
  265. * but in this case it must evaluate its argument exactly once.
  266. *
  267. * \param[in] attributes The key attribute structure to query.
  268. *
  269. * \return The algorithm stored in the attribute structure.
  270. */
  271. static psa_algorithm_t psa_get_key_algorithm(
  272. const psa_key_attributes_t *attributes);
  273. /** Declare the type of a key.
  274. *
  275. * This function overwrites any key type
  276. * previously set in \p attributes.
  277. *
  278. * This function may be declared as `static` (i.e. without external
  279. * linkage). This function may be provided as a function-like macro,
  280. * but in this case it must evaluate each of its arguments exactly once.
  281. *
  282. * \param[out] attributes The attribute structure to write to.
  283. * \param type The key type to write.
  284. * If this is 0, the key type in \p attributes
  285. * becomes unspecified.
  286. */
  287. static void psa_set_key_type(psa_key_attributes_t *attributes,
  288. psa_key_type_t type);
  289. /** Declare the size of a key.
  290. *
  291. * This function overwrites any key size previously set in \p attributes.
  292. *
  293. * This function may be declared as `static` (i.e. without external
  294. * linkage). This function may be provided as a function-like macro,
  295. * but in this case it must evaluate each of its arguments exactly once.
  296. *
  297. * \param[out] attributes The attribute structure to write to.
  298. * \param bits The key size in bits.
  299. * If this is 0, the key size in \p attributes
  300. * becomes unspecified. Keys of size 0 are
  301. * not supported.
  302. */
  303. static void psa_set_key_bits(psa_key_attributes_t *attributes,
  304. size_t bits);
  305. /** Retrieve the key type from key attributes.
  306. *
  307. * This function may be declared as `static` (i.e. without external
  308. * linkage). This function may be provided as a function-like macro,
  309. * but in this case it must evaluate its argument exactly once.
  310. *
  311. * \param[in] attributes The key attribute structure to query.
  312. *
  313. * \return The key type stored in the attribute structure.
  314. */
  315. static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
  316. /** Retrieve the key size from key attributes.
  317. *
  318. * This function may be declared as `static` (i.e. without external
  319. * linkage). This function may be provided as a function-like macro,
  320. * but in this case it must evaluate its argument exactly once.
  321. *
  322. * \param[in] attributes The key attribute structure to query.
  323. *
  324. * \return The key size stored in the attribute structure, in bits.
  325. */
  326. static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
  327. /** Retrieve the attributes of a key.
  328. *
  329. * This function first resets the attribute structure as with
  330. * psa_reset_key_attributes(). It then copies the attributes of
  331. * the given key into the given attribute structure.
  332. *
  333. * \note This function may allocate memory or other resources.
  334. * Once you have called this function on an attribute structure,
  335. * you must call psa_reset_key_attributes() to free these resources.
  336. *
  337. * \param[in] key Identifier of the key to query.
  338. * \param[in,out] attributes On success, the attributes of the key.
  339. * On failure, equivalent to a
  340. * freshly-initialized structure.
  341. *
  342. * \retval #PSA_SUCCESS
  343. * \retval #PSA_ERROR_INVALID_HANDLE
  344. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  345. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  346. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  347. * \retval #PSA_ERROR_STORAGE_FAILURE
  348. * \retval #PSA_ERROR_DATA_CORRUPT
  349. * \retval #PSA_ERROR_DATA_INVALID
  350. * \retval #PSA_ERROR_BAD_STATE
  351. * The library has not been previously initialized by psa_crypto_init().
  352. * It is implementation-dependent whether a failure to initialize
  353. * results in this error code.
  354. */
  355. psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
  356. psa_key_attributes_t *attributes);
  357. /** Reset a key attribute structure to a freshly initialized state.
  358. *
  359. * You must initialize the attribute structure as described in the
  360. * documentation of the type #psa_key_attributes_t before calling this
  361. * function. Once the structure has been initialized, you may call this
  362. * function at any time.
  363. *
  364. * This function frees any auxiliary resources that the structure
  365. * may contain.
  366. *
  367. * \param[in,out] attributes The attribute structure to reset.
  368. */
  369. void psa_reset_key_attributes(psa_key_attributes_t *attributes);
  370. /**@}*/
  371. /** \defgroup key_management Key management
  372. * @{
  373. */
  374. /** Remove non-essential copies of key material from memory.
  375. *
  376. * If the key identifier designates a volatile key, this functions does not do
  377. * anything and returns successfully.
  378. *
  379. * If the key identifier designates a persistent key, then this function will
  380. * free all resources associated with the key in volatile memory. The key
  381. * data in persistent storage is not affected and the key can still be used.
  382. *
  383. * \param key Identifier of the key to purge.
  384. *
  385. * \retval #PSA_SUCCESS
  386. * The key material will have been removed from memory if it is not
  387. * currently required.
  388. * \retval #PSA_ERROR_INVALID_ARGUMENT
  389. * \p key is not a valid key identifier.
  390. * \retval #PSA_ERROR_BAD_STATE
  391. * The library has not been previously initialized by psa_crypto_init().
  392. * It is implementation-dependent whether a failure to initialize
  393. * results in this error code.
  394. */
  395. psa_status_t psa_purge_key(mbedtls_svc_key_id_t key);
  396. /** Make a copy of a key.
  397. *
  398. * Copy key material from one location to another.
  399. *
  400. * This function is primarily useful to copy a key from one location
  401. * to another, since it populates a key using the material from
  402. * another key which may have a different lifetime.
  403. *
  404. * This function may be used to share a key with a different party,
  405. * subject to implementation-defined restrictions on key sharing.
  406. *
  407. * The policy on the source key must have the usage flag
  408. * #PSA_KEY_USAGE_COPY set.
  409. * This flag is sufficient to permit the copy if the key has the lifetime
  410. * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
  411. * Some secure elements do not provide a way to copy a key without
  412. * making it extractable from the secure element. If a key is located
  413. * in such a secure element, then the key must have both usage flags
  414. * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
  415. * a copy of the key outside the secure element.
  416. *
  417. * The resulting key may only be used in a way that conforms to
  418. * both the policy of the original key and the policy specified in
  419. * the \p attributes parameter:
  420. * - The usage flags on the resulting key are the bitwise-and of the
  421. * usage flags on the source policy and the usage flags in \p attributes.
  422. * - If both allow the same algorithm or wildcard-based
  423. * algorithm policy, the resulting key has the same algorithm policy.
  424. * - If either of the policies allows an algorithm and the other policy
  425. * allows a wildcard-based algorithm policy that includes this algorithm,
  426. * the resulting key allows the same algorithm.
  427. * - If the policies do not allow any algorithm in common, this function
  428. * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
  429. *
  430. * The effect of this function on implementation-defined attributes is
  431. * implementation-defined.
  432. *
  433. * \param source_key The key to copy. It must allow the usage
  434. * #PSA_KEY_USAGE_COPY. If a private or secret key is
  435. * being copied outside of a secure element it must
  436. * also allow #PSA_KEY_USAGE_EXPORT.
  437. * \param[in] attributes The attributes for the new key.
  438. * They are used as follows:
  439. * - The key type and size may be 0. If either is
  440. * nonzero, it must match the corresponding
  441. * attribute of the source key.
  442. * - The key location (the lifetime and, for
  443. * persistent keys, the key identifier) is
  444. * used directly.
  445. * - The policy constraints (usage flags and
  446. * algorithm policy) are combined from
  447. * the source key and \p attributes so that
  448. * both sets of restrictions apply, as
  449. * described in the documentation of this function.
  450. * \param[out] target_key On success, an identifier for the newly created
  451. * key. For persistent keys, this is the key
  452. * identifier defined in \p attributes.
  453. * \c 0 on failure.
  454. *
  455. * \retval #PSA_SUCCESS
  456. * \retval #PSA_ERROR_INVALID_HANDLE
  457. * \p source_key is invalid.
  458. * \retval #PSA_ERROR_ALREADY_EXISTS
  459. * This is an attempt to create a persistent key, and there is
  460. * already a persistent key with the given identifier.
  461. * \retval #PSA_ERROR_INVALID_ARGUMENT
  462. * The lifetime or identifier in \p attributes are invalid.
  463. * \retval #PSA_ERROR_INVALID_ARGUMENT
  464. * The policy constraints on the source and specified in
  465. * \p attributes are incompatible.
  466. * \retval #PSA_ERROR_INVALID_ARGUMENT
  467. * \p attributes specifies a key type or key size
  468. * which does not match the attributes of the source key.
  469. * \retval #PSA_ERROR_NOT_PERMITTED
  470. * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
  471. * \retval #PSA_ERROR_NOT_PERMITTED
  472. * The source key is not exportable and its lifetime does not
  473. * allow copying it to the target's lifetime.
  474. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  475. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
  476. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  477. * \retval #PSA_ERROR_HARDWARE_FAILURE
  478. * \retval #PSA_ERROR_DATA_INVALID
  479. * \retval #PSA_ERROR_DATA_CORRUPT
  480. * \retval #PSA_ERROR_STORAGE_FAILURE
  481. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  482. * \retval #PSA_ERROR_BAD_STATE
  483. * The library has not been previously initialized by psa_crypto_init().
  484. * It is implementation-dependent whether a failure to initialize
  485. * results in this error code.
  486. */
  487. psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
  488. const psa_key_attributes_t *attributes,
  489. mbedtls_svc_key_id_t *target_key);
  490. /**
  491. * \brief Destroy a key.
  492. *
  493. * This function destroys a key from both volatile
  494. * memory and, if applicable, non-volatile storage. Implementations shall
  495. * make a best effort to ensure that that the key material cannot be recovered.
  496. *
  497. * This function also erases any metadata such as policies and frees
  498. * resources associated with the key.
  499. *
  500. * If a key is currently in use in a multipart operation, then destroying the
  501. * key will cause the multipart operation to fail.
  502. *
  503. * \param key Identifier of the key to erase. If this is \c 0, do nothing and
  504. * return #PSA_SUCCESS.
  505. *
  506. * \retval #PSA_SUCCESS
  507. * \p key was a valid identifier and the key material that it
  508. * referred to has been erased. Alternatively, \p key is \c 0.
  509. * \retval #PSA_ERROR_NOT_PERMITTED
  510. * The key cannot be erased because it is
  511. * read-only, either due to a policy or due to physical restrictions.
  512. * \retval #PSA_ERROR_INVALID_HANDLE
  513. * \p key is not a valid identifier nor \c 0.
  514. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  515. * There was an failure in communication with the cryptoprocessor.
  516. * The key material may still be present in the cryptoprocessor.
  517. * \retval #PSA_ERROR_DATA_INVALID
  518. * This error is typically a result of either storage corruption on a
  519. * cleartext storage backend, or an attempt to read data that was
  520. * written by an incompatible version of the library.
  521. * \retval #PSA_ERROR_STORAGE_FAILURE
  522. * The storage is corrupted. Implementations shall make a best effort
  523. * to erase key material even in this stage, however applications
  524. * should be aware that it may be impossible to guarantee that the
  525. * key material is not recoverable in such cases.
  526. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  527. * An unexpected condition which is not a storage corruption or
  528. * a communication failure occurred. The cryptoprocessor may have
  529. * been compromised.
  530. * \retval #PSA_ERROR_BAD_STATE
  531. * The library has not been previously initialized by psa_crypto_init().
  532. * It is implementation-dependent whether a failure to initialize
  533. * results in this error code.
  534. */
  535. psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
  536. /**@}*/
  537. /** \defgroup import_export Key import and export
  538. * @{
  539. */
  540. /**
  541. * \brief Import a key in binary format.
  542. *
  543. * This function supports any output from psa_export_key(). Refer to the
  544. * documentation of psa_export_public_key() for the format of public keys
  545. * and to the documentation of psa_export_key() for the format for
  546. * other key types.
  547. *
  548. * The key data determines the key size. The attributes may optionally
  549. * specify a key size; in this case it must match the size determined
  550. * from the key data. A key size of 0 in \p attributes indicates that
  551. * the key size is solely determined by the key data.
  552. *
  553. * Implementations must reject an attempt to import a key of size 0.
  554. *
  555. * This specification supports a single format for each key type.
  556. * Implementations may support other formats as long as the standard
  557. * format is supported. Implementations that support other formats
  558. * should ensure that the formats are clearly unambiguous so as to
  559. * minimize the risk that an invalid input is accidentally interpreted
  560. * according to a different format.
  561. *
  562. * \param[in] attributes The attributes for the new key.
  563. * The key size is always determined from the
  564. * \p data buffer.
  565. * If the key size in \p attributes is nonzero,
  566. * it must be equal to the size from \p data.
  567. * \param[out] key On success, an identifier to the newly created key.
  568. * For persistent keys, this is the key identifier
  569. * defined in \p attributes.
  570. * \c 0 on failure.
  571. * \param[in] data Buffer containing the key data. The content of this
  572. * buffer is interpreted according to the type declared
  573. * in \p attributes.
  574. * All implementations must support at least the format
  575. * described in the documentation
  576. * of psa_export_key() or psa_export_public_key() for
  577. * the chosen type. Implementations may allow other
  578. * formats, but should be conservative: implementations
  579. * should err on the side of rejecting content if it
  580. * may be erroneous (e.g. wrong type or truncated data).
  581. * \param data_length Size of the \p data buffer in bytes.
  582. *
  583. * \retval #PSA_SUCCESS
  584. * Success.
  585. * If the key is persistent, the key material and the key's metadata
  586. * have been saved to persistent storage.
  587. * \retval #PSA_ERROR_ALREADY_EXISTS
  588. * This is an attempt to create a persistent key, and there is
  589. * already a persistent key with the given identifier.
  590. * \retval #PSA_ERROR_NOT_SUPPORTED
  591. * The key type or key size is not supported, either by the
  592. * implementation in general or in this particular persistent location.
  593. * \retval #PSA_ERROR_INVALID_ARGUMENT
  594. * The key attributes, as a whole, are invalid.
  595. * \retval #PSA_ERROR_INVALID_ARGUMENT
  596. * The key data is not correctly formatted.
  597. * \retval #PSA_ERROR_INVALID_ARGUMENT
  598. * The size in \p attributes is nonzero and does not match the size
  599. * of the key data.
  600. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  601. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
  602. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  603. * \retval #PSA_ERROR_DATA_CORRUPT
  604. * \retval #PSA_ERROR_DATA_INVALID
  605. * \retval #PSA_ERROR_STORAGE_FAILURE
  606. * \retval #PSA_ERROR_HARDWARE_FAILURE
  607. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  608. * \retval #PSA_ERROR_BAD_STATE
  609. * The library has not been previously initialized by psa_crypto_init().
  610. * It is implementation-dependent whether a failure to initialize
  611. * results in this error code.
  612. */
  613. psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
  614. const uint8_t *data,
  615. size_t data_length,
  616. mbedtls_svc_key_id_t *key);
  617. /**
  618. * \brief Export a key in binary format.
  619. *
  620. * The output of this function can be passed to psa_import_key() to
  621. * create an equivalent object.
  622. *
  623. * If the implementation of psa_import_key() supports other formats
  624. * beyond the format specified here, the output from psa_export_key()
  625. * must use the representation specified here, not the original
  626. * representation.
  627. *
  628. * For standard key types, the output format is as follows:
  629. *
  630. * - For symmetric keys (including MAC keys), the format is the
  631. * raw bytes of the key.
  632. * - For DES, the key data consists of 8 bytes. The parity bits must be
  633. * correct.
  634. * - For Triple-DES, the format is the concatenation of the
  635. * two or three DES keys.
  636. * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
  637. * is the non-encrypted DER encoding of the representation defined by
  638. * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
  639. * ```
  640. * RSAPrivateKey ::= SEQUENCE {
  641. * version INTEGER, -- must be 0
  642. * modulus INTEGER, -- n
  643. * publicExponent INTEGER, -- e
  644. * privateExponent INTEGER, -- d
  645. * prime1 INTEGER, -- p
  646. * prime2 INTEGER, -- q
  647. * exponent1 INTEGER, -- d mod (p-1)
  648. * exponent2 INTEGER, -- d mod (q-1)
  649. * coefficient INTEGER, -- (inverse of q) mod p
  650. * }
  651. * ```
  652. * - For elliptic curve key pairs (key types for which
  653. * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
  654. * a representation of the private value as a `ceiling(m/8)`-byte string
  655. * where `m` is the bit size associated with the curve, i.e. the bit size
  656. * of the order of the curve's coordinate field. This byte string is
  657. * in little-endian order for Montgomery curves (curve types
  658. * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
  659. * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
  660. * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
  661. * For Weierstrass curves, this is the content of the `privateKey` field of
  662. * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
  663. * the format is defined by RFC 7748, and output is masked according to §5.
  664. * For twisted Edwards curves, the private key is as defined by RFC 8032
  665. * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
  666. * - For Diffie-Hellman key exchange key pairs (key types for which
  667. * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
  668. * format is the representation of the private key `x` as a big-endian byte
  669. * string. The length of the byte string is the private key size in bytes
  670. * (leading zeroes are not stripped).
  671. * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
  672. * true), the format is the same as for psa_export_public_key().
  673. *
  674. * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
  675. *
  676. * \param key Identifier of the key to export. It must allow the
  677. * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
  678. * key.
  679. * \param[out] data Buffer where the key data is to be written.
  680. * \param data_size Size of the \p data buffer in bytes.
  681. * \param[out] data_length On success, the number of bytes
  682. * that make up the key data.
  683. *
  684. * \retval #PSA_SUCCESS
  685. * \retval #PSA_ERROR_INVALID_HANDLE
  686. * \retval #PSA_ERROR_NOT_PERMITTED
  687. * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
  688. * \retval #PSA_ERROR_NOT_SUPPORTED
  689. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  690. * The size of the \p data buffer is too small. You can determine a
  691. * sufficient buffer size by calling
  692. * #PSA_EXPORT_KEY_OUTPUT_SIZE(\c type, \c bits)
  693. * where \c type is the key type
  694. * and \c bits is the key size in bits.
  695. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  696. * \retval #PSA_ERROR_HARDWARE_FAILURE
  697. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  698. * \retval #PSA_ERROR_STORAGE_FAILURE
  699. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  700. * \retval #PSA_ERROR_BAD_STATE
  701. * The library has not been previously initialized by psa_crypto_init().
  702. * It is implementation-dependent whether a failure to initialize
  703. * results in this error code.
  704. */
  705. psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
  706. uint8_t *data,
  707. size_t data_size,
  708. size_t *data_length);
  709. /**
  710. * \brief Export a public key or the public part of a key pair in binary format.
  711. *
  712. * The output of this function can be passed to psa_import_key() to
  713. * create an object that is equivalent to the public key.
  714. *
  715. * This specification supports a single format for each key type.
  716. * Implementations may support other formats as long as the standard
  717. * format is supported. Implementations that support other formats
  718. * should ensure that the formats are clearly unambiguous so as to
  719. * minimize the risk that an invalid input is accidentally interpreted
  720. * according to a different format.
  721. *
  722. * For standard key types, the output format is as follows:
  723. * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
  724. * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
  725. * ```
  726. * RSAPublicKey ::= SEQUENCE {
  727. * modulus INTEGER, -- n
  728. * publicExponent INTEGER } -- e
  729. * ```
  730. * - For elliptic curve keys on a twisted Edwards curve (key types for which
  731. * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true and #PSA_KEY_TYPE_ECC_GET_FAMILY
  732. * returns #PSA_ECC_FAMILY_TWISTED_EDWARDS), the public key is as defined
  733. * by RFC 8032
  734. * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
  735. * - For other elliptic curve public keys (key types for which
  736. * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
  737. * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
  738. * Let `m` be the bit size associated with the curve, i.e. the bit size of
  739. * `q` for a curve over `F_q`. The representation consists of:
  740. * - The byte 0x04;
  741. * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
  742. * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
  743. * - For Diffie-Hellman key exchange public keys (key types for which
  744. * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
  745. * the format is the representation of the public key `y = g^x mod p` as a
  746. * big-endian byte string. The length of the byte string is the length of the
  747. * base prime `p` in bytes.
  748. *
  749. * Exporting a public key object or the public part of a key pair is
  750. * always permitted, regardless of the key's usage flags.
  751. *
  752. * \param key Identifier of the key to export.
  753. * \param[out] data Buffer where the key data is to be written.
  754. * \param data_size Size of the \p data buffer in bytes.
  755. * \param[out] data_length On success, the number of bytes
  756. * that make up the key data.
  757. *
  758. * \retval #PSA_SUCCESS
  759. * \retval #PSA_ERROR_INVALID_HANDLE
  760. * \retval #PSA_ERROR_INVALID_ARGUMENT
  761. * The key is neither a public key nor a key pair.
  762. * \retval #PSA_ERROR_NOT_SUPPORTED
  763. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  764. * The size of the \p data buffer is too small. You can determine a
  765. * sufficient buffer size by calling
  766. * #PSA_EXPORT_KEY_OUTPUT_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
  767. * where \c type is the key type
  768. * and \c bits is the key size in bits.
  769. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  770. * \retval #PSA_ERROR_HARDWARE_FAILURE
  771. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  772. * \retval #PSA_ERROR_STORAGE_FAILURE
  773. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  774. * \retval #PSA_ERROR_BAD_STATE
  775. * The library has not been previously initialized by psa_crypto_init().
  776. * It is implementation-dependent whether a failure to initialize
  777. * results in this error code.
  778. */
  779. psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
  780. uint8_t *data,
  781. size_t data_size,
  782. size_t *data_length);
  783. /**@}*/
  784. /** \defgroup hash Message digests
  785. * @{
  786. */
  787. /** Calculate the hash (digest) of a message.
  788. *
  789. * \note To verify the hash of a message against an
  790. * expected value, use psa_hash_compare() instead.
  791. *
  792. * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
  793. * such that #PSA_ALG_IS_HASH(\p alg) is true).
  794. * \param[in] input Buffer containing the message to hash.
  795. * \param input_length Size of the \p input buffer in bytes.
  796. * \param[out] hash Buffer where the hash is to be written.
  797. * \param hash_size Size of the \p hash buffer in bytes.
  798. * \param[out] hash_length On success, the number of bytes
  799. * that make up the hash value. This is always
  800. * #PSA_HASH_LENGTH(\p alg).
  801. *
  802. * \retval #PSA_SUCCESS
  803. * Success.
  804. * \retval #PSA_ERROR_NOT_SUPPORTED
  805. * \p alg is not supported or is not a hash algorithm.
  806. * \retval #PSA_ERROR_INVALID_ARGUMENT
  807. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  808. * \p hash_size is too small
  809. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  810. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  811. * \retval #PSA_ERROR_HARDWARE_FAILURE
  812. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  813. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  814. * \retval #PSA_ERROR_BAD_STATE
  815. * The library has not been previously initialized by psa_crypto_init().
  816. * It is implementation-dependent whether a failure to initialize
  817. * results in this error code.
  818. */
  819. psa_status_t psa_hash_compute(psa_algorithm_t alg,
  820. const uint8_t *input,
  821. size_t input_length,
  822. uint8_t *hash,
  823. size_t hash_size,
  824. size_t *hash_length);
  825. /** Calculate the hash (digest) of a message and compare it with a
  826. * reference value.
  827. *
  828. * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
  829. * such that #PSA_ALG_IS_HASH(\p alg) is true).
  830. * \param[in] input Buffer containing the message to hash.
  831. * \param input_length Size of the \p input buffer in bytes.
  832. * \param[out] hash Buffer containing the expected hash value.
  833. * \param hash_length Size of the \p hash buffer in bytes.
  834. *
  835. * \retval #PSA_SUCCESS
  836. * The expected hash is identical to the actual hash of the input.
  837. * \retval #PSA_ERROR_INVALID_SIGNATURE
  838. * The hash of the message was calculated successfully, but it
  839. * differs from the expected hash.
  840. * \retval #PSA_ERROR_NOT_SUPPORTED
  841. * \p alg is not supported or is not a hash algorithm.
  842. * \retval #PSA_ERROR_INVALID_ARGUMENT
  843. * \p input_length or \p hash_length do not match the hash size for \p alg
  844. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  845. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  846. * \retval #PSA_ERROR_HARDWARE_FAILURE
  847. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  848. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  849. * \retval #PSA_ERROR_BAD_STATE
  850. * The library has not been previously initialized by psa_crypto_init().
  851. * It is implementation-dependent whether a failure to initialize
  852. * results in this error code.
  853. */
  854. psa_status_t psa_hash_compare(psa_algorithm_t alg,
  855. const uint8_t *input,
  856. size_t input_length,
  857. const uint8_t *hash,
  858. size_t hash_length);
  859. /** The type of the state data structure for multipart hash operations.
  860. *
  861. * Before calling any function on a hash operation object, the application must
  862. * initialize it by any of the following means:
  863. * - Set the structure to all-bits-zero, for example:
  864. * \code
  865. * psa_hash_operation_t operation;
  866. * memset(&operation, 0, sizeof(operation));
  867. * \endcode
  868. * - Initialize the structure to logical zero values, for example:
  869. * \code
  870. * psa_hash_operation_t operation = {0};
  871. * \endcode
  872. * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
  873. * for example:
  874. * \code
  875. * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
  876. * \endcode
  877. * - Assign the result of the function psa_hash_operation_init()
  878. * to the structure, for example:
  879. * \code
  880. * psa_hash_operation_t operation;
  881. * operation = psa_hash_operation_init();
  882. * \endcode
  883. *
  884. * This is an implementation-defined \c struct. Applications should not
  885. * make any assumptions about the content of this structure except
  886. * as directed by the documentation of a specific implementation. */
  887. typedef struct psa_hash_operation_s psa_hash_operation_t;
  888. /** \def PSA_HASH_OPERATION_INIT
  889. *
  890. * This macro returns a suitable initializer for a hash operation object
  891. * of type #psa_hash_operation_t.
  892. */
  893. #ifdef __DOXYGEN_ONLY__
  894. /* This is an example definition for documentation purposes.
  895. * Implementations should define a suitable value in `crypto_struct.h`.
  896. */
  897. #define PSA_HASH_OPERATION_INIT {0}
  898. #endif
  899. /** Return an initial value for a hash operation object.
  900. */
  901. static psa_hash_operation_t psa_hash_operation_init(void);
  902. /** Set up a multipart hash operation.
  903. *
  904. * The sequence of operations to calculate a hash (message digest)
  905. * is as follows:
  906. * -# Allocate an operation object which will be passed to all the functions
  907. * listed here.
  908. * -# Initialize the operation object with one of the methods described in the
  909. * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
  910. * -# Call psa_hash_setup() to specify the algorithm.
  911. * -# Call psa_hash_update() zero, one or more times, passing a fragment
  912. * of the message each time. The hash that is calculated is the hash
  913. * of the concatenation of these messages in order.
  914. * -# To calculate the hash, call psa_hash_finish().
  915. * To compare the hash with an expected value, call psa_hash_verify().
  916. *
  917. * If an error occurs at any step after a call to psa_hash_setup(), the
  918. * operation will need to be reset by a call to psa_hash_abort(). The
  919. * application may call psa_hash_abort() at any time after the operation
  920. * has been initialized.
  921. *
  922. * After a successful call to psa_hash_setup(), the application must
  923. * eventually terminate the operation. The following events terminate an
  924. * operation:
  925. * - A successful call to psa_hash_finish() or psa_hash_verify().
  926. * - A call to psa_hash_abort().
  927. *
  928. * \param[in,out] operation The operation object to set up. It must have
  929. * been initialized as per the documentation for
  930. * #psa_hash_operation_t and not yet in use.
  931. * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
  932. * such that #PSA_ALG_IS_HASH(\p alg) is true).
  933. *
  934. * \retval #PSA_SUCCESS
  935. * Success.
  936. * \retval #PSA_ERROR_NOT_SUPPORTED
  937. * \p alg is not a supported hash algorithm.
  938. * \retval #PSA_ERROR_INVALID_ARGUMENT
  939. * \p alg is not a hash algorithm.
  940. * \retval #PSA_ERROR_BAD_STATE
  941. * The operation state is not valid (it must be inactive).
  942. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  943. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  944. * \retval #PSA_ERROR_HARDWARE_FAILURE
  945. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  946. * \retval #PSA_ERROR_BAD_STATE
  947. * The library has not been previously initialized by psa_crypto_init().
  948. * It is implementation-dependent whether a failure to initialize
  949. * results in this error code.
  950. */
  951. psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
  952. psa_algorithm_t alg);
  953. /** Add a message fragment to a multipart hash operation.
  954. *
  955. * The application must call psa_hash_setup() before calling this function.
  956. *
  957. * If this function returns an error status, the operation enters an error
  958. * state and must be aborted by calling psa_hash_abort().
  959. *
  960. * \param[in,out] operation Active hash operation.
  961. * \param[in] input Buffer containing the message fragment to hash.
  962. * \param input_length Size of the \p input buffer in bytes.
  963. *
  964. * \retval #PSA_SUCCESS
  965. * Success.
  966. * \retval #PSA_ERROR_BAD_STATE
  967. * The operation state is not valid (it muct be active).
  968. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  969. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  970. * \retval #PSA_ERROR_HARDWARE_FAILURE
  971. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  972. * \retval #PSA_ERROR_BAD_STATE
  973. * The library has not been previously initialized by psa_crypto_init().
  974. * It is implementation-dependent whether a failure to initialize
  975. * results in this error code.
  976. */
  977. psa_status_t psa_hash_update(psa_hash_operation_t *operation,
  978. const uint8_t *input,
  979. size_t input_length);
  980. /** Finish the calculation of the hash of a message.
  981. *
  982. * The application must call psa_hash_setup() before calling this function.
  983. * This function calculates the hash of the message formed by concatenating
  984. * the inputs passed to preceding calls to psa_hash_update().
  985. *
  986. * When this function returns successfuly, the operation becomes inactive.
  987. * If this function returns an error status, the operation enters an error
  988. * state and must be aborted by calling psa_hash_abort().
  989. *
  990. * \warning Applications should not call this function if they expect
  991. * a specific value for the hash. Call psa_hash_verify() instead.
  992. * Beware that comparing integrity or authenticity data such as
  993. * hash values with a function such as \c memcmp is risky
  994. * because the time taken by the comparison may leak information
  995. * about the hashed data which could allow an attacker to guess
  996. * a valid hash and thereby bypass security controls.
  997. *
  998. * \param[in,out] operation Active hash operation.
  999. * \param[out] hash Buffer where the hash is to be written.
  1000. * \param hash_size Size of the \p hash buffer in bytes.
  1001. * \param[out] hash_length On success, the number of bytes
  1002. * that make up the hash value. This is always
  1003. * #PSA_HASH_LENGTH(\c alg) where \c alg is the
  1004. * hash algorithm that is calculated.
  1005. *
  1006. * \retval #PSA_SUCCESS
  1007. * Success.
  1008. * \retval #PSA_ERROR_BAD_STATE
  1009. * The operation state is not valid (it must be active).
  1010. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  1011. * The size of the \p hash buffer is too small. You can determine a
  1012. * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
  1013. * where \c alg is the hash algorithm that is calculated.
  1014. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  1015. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1016. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1017. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1018. * \retval #PSA_ERROR_BAD_STATE
  1019. * The library has not been previously initialized by psa_crypto_init().
  1020. * It is implementation-dependent whether a failure to initialize
  1021. * results in this error code.
  1022. */
  1023. psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
  1024. uint8_t *hash,
  1025. size_t hash_size,
  1026. size_t *hash_length);
  1027. /** Finish the calculation of the hash of a message and compare it with
  1028. * an expected value.
  1029. *
  1030. * The application must call psa_hash_setup() before calling this function.
  1031. * This function calculates the hash of the message formed by concatenating
  1032. * the inputs passed to preceding calls to psa_hash_update(). It then
  1033. * compares the calculated hash with the expected hash passed as a
  1034. * parameter to this function.
  1035. *
  1036. * When this function returns successfuly, the operation becomes inactive.
  1037. * If this function returns an error status, the operation enters an error
  1038. * state and must be aborted by calling psa_hash_abort().
  1039. *
  1040. * \note Implementations shall make the best effort to ensure that the
  1041. * comparison between the actual hash and the expected hash is performed
  1042. * in constant time.
  1043. *
  1044. * \param[in,out] operation Active hash operation.
  1045. * \param[in] hash Buffer containing the expected hash value.
  1046. * \param hash_length Size of the \p hash buffer in bytes.
  1047. *
  1048. * \retval #PSA_SUCCESS
  1049. * The expected hash is identical to the actual hash of the message.
  1050. * \retval #PSA_ERROR_INVALID_SIGNATURE
  1051. * The hash of the message was calculated successfully, but it
  1052. * differs from the expected hash.
  1053. * \retval #PSA_ERROR_BAD_STATE
  1054. * The operation state is not valid (it must be active).
  1055. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  1056. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1057. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1058. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1059. * \retval #PSA_ERROR_BAD_STATE
  1060. * The library has not been previously initialized by psa_crypto_init().
  1061. * It is implementation-dependent whether a failure to initialize
  1062. * results in this error code.
  1063. */
  1064. psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
  1065. const uint8_t *hash,
  1066. size_t hash_length);
  1067. /** Abort a hash operation.
  1068. *
  1069. * Aborting an operation frees all associated resources except for the
  1070. * \p operation structure itself. Once aborted, the operation object
  1071. * can be reused for another operation by calling
  1072. * psa_hash_setup() again.
  1073. *
  1074. * You may call this function any time after the operation object has
  1075. * been initialized by one of the methods described in #psa_hash_operation_t.
  1076. *
  1077. * In particular, calling psa_hash_abort() after the operation has been
  1078. * terminated by a call to psa_hash_abort(), psa_hash_finish() or
  1079. * psa_hash_verify() is safe and has no effect.
  1080. *
  1081. * \param[in,out] operation Initialized hash operation.
  1082. *
  1083. * \retval #PSA_SUCCESS
  1084. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1085. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1086. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1087. * \retval #PSA_ERROR_BAD_STATE
  1088. * The library has not been previously initialized by psa_crypto_init().
  1089. * It is implementation-dependent whether a failure to initialize
  1090. * results in this error code.
  1091. */
  1092. psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
  1093. /** Clone a hash operation.
  1094. *
  1095. * This function copies the state of an ongoing hash operation to
  1096. * a new operation object. In other words, this function is equivalent
  1097. * to calling psa_hash_setup() on \p target_operation with the same
  1098. * algorithm that \p source_operation was set up for, then
  1099. * psa_hash_update() on \p target_operation with the same input that
  1100. * that was passed to \p source_operation. After this function returns, the
  1101. * two objects are independent, i.e. subsequent calls involving one of
  1102. * the objects do not affect the other object.
  1103. *
  1104. * \param[in] source_operation The active hash operation to clone.
  1105. * \param[in,out] target_operation The operation object to set up.
  1106. * It must be initialized but not active.
  1107. *
  1108. * \retval #PSA_SUCCESS
  1109. * \retval #PSA_ERROR_BAD_STATE
  1110. * The \p source_operation state is not valid (it must be active).
  1111. * \retval #PSA_ERROR_BAD_STATE
  1112. * The \p target_operation state is not valid (it must be inactive).
  1113. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1114. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1115. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1116. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  1117. * \retval #PSA_ERROR_BAD_STATE
  1118. * The library has not been previously initialized by psa_crypto_init().
  1119. * It is implementation-dependent whether a failure to initialize
  1120. * results in this error code.
  1121. */
  1122. psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
  1123. psa_hash_operation_t *target_operation);
  1124. /**@}*/
  1125. /** \defgroup MAC Message authentication codes
  1126. * @{
  1127. */
  1128. /** Calculate the MAC (message authentication code) of a message.
  1129. *
  1130. * \note To verify the MAC of a message against an
  1131. * expected value, use psa_mac_verify() instead.
  1132. * Beware that comparing integrity or authenticity data such as
  1133. * MAC values with a function such as \c memcmp is risky
  1134. * because the time taken by the comparison may leak information
  1135. * about the MAC value which could allow an attacker to guess
  1136. * a valid MAC and thereby bypass security controls.
  1137. *
  1138. * \param key Identifier of the key to use for the operation. It
  1139. * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
  1140. * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
  1141. * such that #PSA_ALG_IS_MAC(\p alg) is true).
  1142. * \param[in] input Buffer containing the input message.
  1143. * \param input_length Size of the \p input buffer in bytes.
  1144. * \param[out] mac Buffer where the MAC value is to be written.
  1145. * \param mac_size Size of the \p mac buffer in bytes.
  1146. * \param[out] mac_length On success, the number of bytes
  1147. * that make up the MAC value.
  1148. *
  1149. * \retval #PSA_SUCCESS
  1150. * Success.
  1151. * \retval #PSA_ERROR_INVALID_HANDLE
  1152. * \retval #PSA_ERROR_NOT_PERMITTED
  1153. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1154. * \p key is not compatible with \p alg.
  1155. * \retval #PSA_ERROR_NOT_SUPPORTED
  1156. * \p alg is not supported or is not a MAC algorithm.
  1157. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  1158. * \p mac_size is too small
  1159. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  1160. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1161. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1162. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1163. * \retval #PSA_ERROR_STORAGE_FAILURE
  1164. * The key could not be retrieved from storage.
  1165. * \retval #PSA_ERROR_BAD_STATE
  1166. * The library has not been previously initialized by psa_crypto_init().
  1167. * It is implementation-dependent whether a failure to initialize
  1168. * results in this error code.
  1169. */
  1170. psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
  1171. psa_algorithm_t alg,
  1172. const uint8_t *input,
  1173. size_t input_length,
  1174. uint8_t *mac,
  1175. size_t mac_size,
  1176. size_t *mac_length);
  1177. /** Calculate the MAC of a message and compare it with a reference value.
  1178. *
  1179. * \param key Identifier of the key to use for the operation. It
  1180. * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
  1181. * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
  1182. * such that #PSA_ALG_IS_MAC(\p alg) is true).
  1183. * \param[in] input Buffer containing the input message.
  1184. * \param input_length Size of the \p input buffer in bytes.
  1185. * \param[out] mac Buffer containing the expected MAC value.
  1186. * \param mac_length Size of the \p mac buffer in bytes.
  1187. *
  1188. * \retval #PSA_SUCCESS
  1189. * The expected MAC is identical to the actual MAC of the input.
  1190. * \retval #PSA_ERROR_INVALID_SIGNATURE
  1191. * The MAC of the message was calculated successfully, but it
  1192. * differs from the expected value.
  1193. * \retval #PSA_ERROR_INVALID_HANDLE
  1194. * \retval #PSA_ERROR_NOT_PERMITTED
  1195. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1196. * \p key is not compatible with \p alg.
  1197. * \retval #PSA_ERROR_NOT_SUPPORTED
  1198. * \p alg is not supported or is not a MAC algorithm.
  1199. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  1200. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1201. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1202. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1203. * \retval #PSA_ERROR_STORAGE_FAILURE
  1204. * The key could not be retrieved from storage.
  1205. * \retval #PSA_ERROR_BAD_STATE
  1206. * The library has not been previously initialized by psa_crypto_init().
  1207. * It is implementation-dependent whether a failure to initialize
  1208. * results in this error code.
  1209. */
  1210. psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
  1211. psa_algorithm_t alg,
  1212. const uint8_t *input,
  1213. size_t input_length,
  1214. const uint8_t *mac,
  1215. size_t mac_length);
  1216. /** The type of the state data structure for multipart MAC operations.
  1217. *
  1218. * Before calling any function on a MAC operation object, the application must
  1219. * initialize it by any of the following means:
  1220. * - Set the structure to all-bits-zero, for example:
  1221. * \code
  1222. * psa_mac_operation_t operation;
  1223. * memset(&operation, 0, sizeof(operation));
  1224. * \endcode
  1225. * - Initialize the structure to logical zero values, for example:
  1226. * \code
  1227. * psa_mac_operation_t operation = {0};
  1228. * \endcode
  1229. * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
  1230. * for example:
  1231. * \code
  1232. * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
  1233. * \endcode
  1234. * - Assign the result of the function psa_mac_operation_init()
  1235. * to the structure, for example:
  1236. * \code
  1237. * psa_mac_operation_t operation;
  1238. * operation = psa_mac_operation_init();
  1239. * \endcode
  1240. *
  1241. * This is an implementation-defined \c struct. Applications should not
  1242. * make any assumptions about the content of this structure except
  1243. * as directed by the documentation of a specific implementation. */
  1244. typedef struct psa_mac_operation_s psa_mac_operation_t;
  1245. /** \def PSA_MAC_OPERATION_INIT
  1246. *
  1247. * This macro returns a suitable initializer for a MAC operation object of type
  1248. * #psa_mac_operation_t.
  1249. */
  1250. #ifdef __DOXYGEN_ONLY__
  1251. /* This is an example definition for documentation purposes.
  1252. * Implementations should define a suitable value in `crypto_struct.h`.
  1253. */
  1254. #define PSA_MAC_OPERATION_INIT {0}
  1255. #endif
  1256. /** Return an initial value for a MAC operation object.
  1257. */
  1258. static psa_mac_operation_t psa_mac_operation_init(void);
  1259. /** Set up a multipart MAC calculation operation.
  1260. *
  1261. * This function sets up the calculation of the MAC
  1262. * (message authentication code) of a byte string.
  1263. * To verify the MAC of a message against an
  1264. * expected value, use psa_mac_verify_setup() instead.
  1265. *
  1266. * The sequence of operations to calculate a MAC is as follows:
  1267. * -# Allocate an operation object which will be passed to all the functions
  1268. * listed here.
  1269. * -# Initialize the operation object with one of the methods described in the
  1270. * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
  1271. * -# Call psa_mac_sign_setup() to specify the algorithm and key.
  1272. * -# Call psa_mac_update() zero, one or more times, passing a fragment
  1273. * of the message each time. The MAC that is calculated is the MAC
  1274. * of the concatenation of these messages in order.
  1275. * -# At the end of the message, call psa_mac_sign_finish() to finish
  1276. * calculating the MAC value and retrieve it.
  1277. *
  1278. * If an error occurs at any step after a call to psa_mac_sign_setup(), the
  1279. * operation will need to be reset by a call to psa_mac_abort(). The
  1280. * application may call psa_mac_abort() at any time after the operation
  1281. * has been initialized.
  1282. *
  1283. * After a successful call to psa_mac_sign_setup(), the application must
  1284. * eventually terminate the operation through one of the following methods:
  1285. * - A successful call to psa_mac_sign_finish().
  1286. * - A call to psa_mac_abort().
  1287. *
  1288. * \param[in,out] operation The operation object to set up. It must have
  1289. * been initialized as per the documentation for
  1290. * #psa_mac_operation_t and not yet in use.
  1291. * \param key Identifier of the key to use for the operation. It
  1292. * must remain valid until the operation terminates.
  1293. * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
  1294. * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
  1295. * such that #PSA_ALG_IS_MAC(\p alg) is true).
  1296. *
  1297. * \retval #PSA_SUCCESS
  1298. * Success.
  1299. * \retval #PSA_ERROR_INVALID_HANDLE
  1300. * \retval #PSA_ERROR_NOT_PERMITTED
  1301. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1302. * \p key is not compatible with \p alg.
  1303. * \retval #PSA_ERROR_NOT_SUPPORTED
  1304. * \p alg is not supported or is not a MAC algorithm.
  1305. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  1306. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1307. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1308. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1309. * \retval #PSA_ERROR_STORAGE_FAILURE
  1310. * The key could not be retrieved from storage.
  1311. * \retval #PSA_ERROR_BAD_STATE
  1312. * The operation state is not valid (it must be inactive).
  1313. * \retval #PSA_ERROR_BAD_STATE
  1314. * The library has not been previously initialized by psa_crypto_init().
  1315. * It is implementation-dependent whether a failure to initialize
  1316. * results in this error code.
  1317. */
  1318. psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
  1319. mbedtls_svc_key_id_t key,
  1320. psa_algorithm_t alg);
  1321. /** Set up a multipart MAC verification operation.
  1322. *
  1323. * This function sets up the verification of the MAC
  1324. * (message authentication code) of a byte string against an expected value.
  1325. *
  1326. * The sequence of operations to verify a MAC is as follows:
  1327. * -# Allocate an operation object which will be passed to all the functions
  1328. * listed here.
  1329. * -# Initialize the operation object with one of the methods described in the
  1330. * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
  1331. * -# Call psa_mac_verify_setup() to specify the algorithm and key.
  1332. * -# Call psa_mac_update() zero, one or more times, passing a fragment
  1333. * of the message each time. The MAC that is calculated is the MAC
  1334. * of the concatenation of these messages in order.
  1335. * -# At the end of the message, call psa_mac_verify_finish() to finish
  1336. * calculating the actual MAC of the message and verify it against
  1337. * the expected value.
  1338. *
  1339. * If an error occurs at any step after a call to psa_mac_verify_setup(), the
  1340. * operation will need to be reset by a call to psa_mac_abort(). The
  1341. * application may call psa_mac_abort() at any time after the operation
  1342. * has been initialized.
  1343. *
  1344. * After a successful call to psa_mac_verify_setup(), the application must
  1345. * eventually terminate the operation through one of the following methods:
  1346. * - A successful call to psa_mac_verify_finish().
  1347. * - A call to psa_mac_abort().
  1348. *
  1349. * \param[in,out] operation The operation object to set up. It must have
  1350. * been initialized as per the documentation for
  1351. * #psa_mac_operation_t and not yet in use.
  1352. * \param key Identifier of the key to use for the operation. It
  1353. * must remain valid until the operation terminates.
  1354. * It must allow the usage
  1355. * PSA_KEY_USAGE_VERIFY_MESSAGE.
  1356. * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
  1357. * such that #PSA_ALG_IS_MAC(\p alg) is true).
  1358. *
  1359. * \retval #PSA_SUCCESS
  1360. * Success.
  1361. * \retval #PSA_ERROR_INVALID_HANDLE
  1362. * \retval #PSA_ERROR_NOT_PERMITTED
  1363. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1364. * \c key is not compatible with \c alg.
  1365. * \retval #PSA_ERROR_NOT_SUPPORTED
  1366. * \c alg is not supported or is not a MAC algorithm.
  1367. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  1368. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1369. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1370. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1371. * \retval #PSA_ERROR_STORAGE_FAILURE
  1372. * The key could not be retrieved from storage
  1373. * \retval #PSA_ERROR_BAD_STATE
  1374. * The operation state is not valid (it must be inactive).
  1375. * \retval #PSA_ERROR_BAD_STATE
  1376. * The library has not been previously initialized by psa_crypto_init().
  1377. * It is implementation-dependent whether a failure to initialize
  1378. * results in this error code.
  1379. */
  1380. psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
  1381. mbedtls_svc_key_id_t key,
  1382. psa_algorithm_t alg);
  1383. /** Add a message fragment to a multipart MAC operation.
  1384. *
  1385. * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
  1386. * before calling this function.
  1387. *
  1388. * If this function returns an error status, the operation enters an error
  1389. * state and must be aborted by calling psa_mac_abort().
  1390. *
  1391. * \param[in,out] operation Active MAC operation.
  1392. * \param[in] input Buffer containing the message fragment to add to
  1393. * the MAC calculation.
  1394. * \param input_length Size of the \p input buffer in bytes.
  1395. *
  1396. * \retval #PSA_SUCCESS
  1397. * Success.
  1398. * \retval #PSA_ERROR_BAD_STATE
  1399. * The operation state is not valid (it must be active).
  1400. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  1401. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1402. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1403. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1404. * \retval #PSA_ERROR_STORAGE_FAILURE
  1405. * \retval #PSA_ERROR_BAD_STATE
  1406. * The library has not been previously initialized by psa_crypto_init().
  1407. * It is implementation-dependent whether a failure to initialize
  1408. * results in this error code.
  1409. */
  1410. psa_status_t psa_mac_update(psa_mac_operation_t *operation,
  1411. const uint8_t *input,
  1412. size_t input_length);
  1413. /** Finish the calculation of the MAC of a message.
  1414. *
  1415. * The application must call psa_mac_sign_setup() before calling this function.
  1416. * This function calculates the MAC of the message formed by concatenating
  1417. * the inputs passed to preceding calls to psa_mac_update().
  1418. *
  1419. * When this function returns successfuly, the operation becomes inactive.
  1420. * If this function returns an error status, the operation enters an error
  1421. * state and must be aborted by calling psa_mac_abort().
  1422. *
  1423. * \warning Applications should not call this function if they expect
  1424. * a specific value for the MAC. Call psa_mac_verify_finish() instead.
  1425. * Beware that comparing integrity or authenticity data such as
  1426. * MAC values with a function such as \c memcmp is risky
  1427. * because the time taken by the comparison may leak information
  1428. * about the MAC value which could allow an attacker to guess
  1429. * a valid MAC and thereby bypass security controls.
  1430. *
  1431. * \param[in,out] operation Active MAC operation.
  1432. * \param[out] mac Buffer where the MAC value is to be written.
  1433. * \param mac_size Size of the \p mac buffer in bytes.
  1434. * \param[out] mac_length On success, the number of bytes
  1435. * that make up the MAC value. This is always
  1436. * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
  1437. * where \c key_type and \c key_bits are the type and
  1438. * bit-size respectively of the key and \c alg is the
  1439. * MAC algorithm that is calculated.
  1440. *
  1441. * \retval #PSA_SUCCESS
  1442. * Success.
  1443. * \retval #PSA_ERROR_BAD_STATE
  1444. * The operation state is not valid (it must be an active mac sign
  1445. * operation).
  1446. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  1447. * The size of the \p mac buffer is too small. You can determine a
  1448. * sufficient buffer size by calling PSA_MAC_LENGTH().
  1449. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  1450. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1451. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1452. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1453. * \retval #PSA_ERROR_STORAGE_FAILURE
  1454. * \retval #PSA_ERROR_BAD_STATE
  1455. * The library has not been previously initialized by psa_crypto_init().
  1456. * It is implementation-dependent whether a failure to initialize
  1457. * results in this error code.
  1458. */
  1459. psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
  1460. uint8_t *mac,
  1461. size_t mac_size,
  1462. size_t *mac_length);
  1463. /** Finish the calculation of the MAC of a message and compare it with
  1464. * an expected value.
  1465. *
  1466. * The application must call psa_mac_verify_setup() before calling this function.
  1467. * This function calculates the MAC of the message formed by concatenating
  1468. * the inputs passed to preceding calls to psa_mac_update(). It then
  1469. * compares the calculated MAC with the expected MAC passed as a
  1470. * parameter to this function.
  1471. *
  1472. * When this function returns successfuly, the operation becomes inactive.
  1473. * If this function returns an error status, the operation enters an error
  1474. * state and must be aborted by calling psa_mac_abort().
  1475. *
  1476. * \note Implementations shall make the best effort to ensure that the
  1477. * comparison between the actual MAC and the expected MAC is performed
  1478. * in constant time.
  1479. *
  1480. * \param[in,out] operation Active MAC operation.
  1481. * \param[in] mac Buffer containing the expected MAC value.
  1482. * \param mac_length Size of the \p mac buffer in bytes.
  1483. *
  1484. * \retval #PSA_SUCCESS
  1485. * The expected MAC is identical to the actual MAC of the message.
  1486. * \retval #PSA_ERROR_INVALID_SIGNATURE
  1487. * The MAC of the message was calculated successfully, but it
  1488. * differs from the expected MAC.
  1489. * \retval #PSA_ERROR_BAD_STATE
  1490. * The operation state is not valid (it must be an active mac verify
  1491. * operation).
  1492. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  1493. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1494. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1495. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1496. * \retval #PSA_ERROR_STORAGE_FAILURE
  1497. * \retval #PSA_ERROR_BAD_STATE
  1498. * The library has not been previously initialized by psa_crypto_init().
  1499. * It is implementation-dependent whether a failure to initialize
  1500. * results in this error code.
  1501. */
  1502. psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
  1503. const uint8_t *mac,
  1504. size_t mac_length);
  1505. /** Abort a MAC operation.
  1506. *
  1507. * Aborting an operation frees all associated resources except for the
  1508. * \p operation structure itself. Once aborted, the operation object
  1509. * can be reused for another operation by calling
  1510. * psa_mac_sign_setup() or psa_mac_verify_setup() again.
  1511. *
  1512. * You may call this function any time after the operation object has
  1513. * been initialized by one of the methods described in #psa_mac_operation_t.
  1514. *
  1515. * In particular, calling psa_mac_abort() after the operation has been
  1516. * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
  1517. * psa_mac_verify_finish() is safe and has no effect.
  1518. *
  1519. * \param[in,out] operation Initialized MAC operation.
  1520. *
  1521. * \retval #PSA_SUCCESS
  1522. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1523. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1524. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1525. * \retval #PSA_ERROR_BAD_STATE
  1526. * The library has not been previously initialized by psa_crypto_init().
  1527. * It is implementation-dependent whether a failure to initialize
  1528. * results in this error code.
  1529. */
  1530. psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
  1531. /**@}*/
  1532. /** \defgroup cipher Symmetric ciphers
  1533. * @{
  1534. */
  1535. /** Encrypt a message using a symmetric cipher.
  1536. *
  1537. * This function encrypts a message with a random IV (initialization
  1538. * vector). Use the multipart operation interface with a
  1539. * #psa_cipher_operation_t object to provide other forms of IV.
  1540. *
  1541. * \param key Identifier of the key to use for the operation.
  1542. * It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
  1543. * \param alg The cipher algorithm to compute
  1544. * (\c PSA_ALG_XXX value such that
  1545. * #PSA_ALG_IS_CIPHER(\p alg) is true).
  1546. * \param[in] input Buffer containing the message to encrypt.
  1547. * \param input_length Size of the \p input buffer in bytes.
  1548. * \param[out] output Buffer where the output is to be written.
  1549. * The output contains the IV followed by
  1550. * the ciphertext proper.
  1551. * \param output_size Size of the \p output buffer in bytes.
  1552. * \param[out] output_length On success, the number of bytes
  1553. * that make up the output.
  1554. *
  1555. * \retval #PSA_SUCCESS
  1556. * Success.
  1557. * \retval #PSA_ERROR_INVALID_HANDLE
  1558. * \retval #PSA_ERROR_NOT_PERMITTED
  1559. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1560. * \p key is not compatible with \p alg.
  1561. * \retval #PSA_ERROR_NOT_SUPPORTED
  1562. * \p alg is not supported or is not a cipher algorithm.
  1563. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  1564. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  1565. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1566. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1567. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1568. * \retval #PSA_ERROR_STORAGE_FAILURE
  1569. * \retval #PSA_ERROR_BAD_STATE
  1570. * The library has not been previously initialized by psa_crypto_init().
  1571. * It is implementation-dependent whether a failure to initialize
  1572. * results in this error code.
  1573. */
  1574. psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
  1575. psa_algorithm_t alg,
  1576. const uint8_t *input,
  1577. size_t input_length,
  1578. uint8_t *output,
  1579. size_t output_size,
  1580. size_t *output_length);
  1581. /** Decrypt a message using a symmetric cipher.
  1582. *
  1583. * This function decrypts a message encrypted with a symmetric cipher.
  1584. *
  1585. * \param key Identifier of the key to use for the operation.
  1586. * It must remain valid until the operation
  1587. * terminates. It must allow the usage
  1588. * #PSA_KEY_USAGE_DECRYPT.
  1589. * \param alg The cipher algorithm to compute
  1590. * (\c PSA_ALG_XXX value such that
  1591. * #PSA_ALG_IS_CIPHER(\p alg) is true).
  1592. * \param[in] input Buffer containing the message to decrypt.
  1593. * This consists of the IV followed by the
  1594. * ciphertext proper.
  1595. * \param input_length Size of the \p input buffer in bytes.
  1596. * \param[out] output Buffer where the plaintext is to be written.
  1597. * \param output_size Size of the \p output buffer in bytes.
  1598. * \param[out] output_length On success, the number of bytes
  1599. * that make up the output.
  1600. *
  1601. * \retval #PSA_SUCCESS
  1602. * Success.
  1603. * \retval #PSA_ERROR_INVALID_HANDLE
  1604. * \retval #PSA_ERROR_NOT_PERMITTED
  1605. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1606. * \p key is not compatible with \p alg.
  1607. * \retval #PSA_ERROR_NOT_SUPPORTED
  1608. * \p alg is not supported or is not a cipher algorithm.
  1609. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  1610. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  1611. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1612. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1613. * \retval #PSA_ERROR_STORAGE_FAILURE
  1614. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1615. * \retval #PSA_ERROR_BAD_STATE
  1616. * The library has not been previously initialized by psa_crypto_init().
  1617. * It is implementation-dependent whether a failure to initialize
  1618. * results in this error code.
  1619. */
  1620. psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
  1621. psa_algorithm_t alg,
  1622. const uint8_t *input,
  1623. size_t input_length,
  1624. uint8_t *output,
  1625. size_t output_size,
  1626. size_t *output_length);
  1627. /** The type of the state data structure for multipart cipher operations.
  1628. *
  1629. * Before calling any function on a cipher operation object, the application
  1630. * must initialize it by any of the following means:
  1631. * - Set the structure to all-bits-zero, for example:
  1632. * \code
  1633. * psa_cipher_operation_t operation;
  1634. * memset(&operation, 0, sizeof(operation));
  1635. * \endcode
  1636. * - Initialize the structure to logical zero values, for example:
  1637. * \code
  1638. * psa_cipher_operation_t operation = {0};
  1639. * \endcode
  1640. * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
  1641. * for example:
  1642. * \code
  1643. * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  1644. * \endcode
  1645. * - Assign the result of the function psa_cipher_operation_init()
  1646. * to the structure, for example:
  1647. * \code
  1648. * psa_cipher_operation_t operation;
  1649. * operation = psa_cipher_operation_init();
  1650. * \endcode
  1651. *
  1652. * This is an implementation-defined \c struct. Applications should not
  1653. * make any assumptions about the content of this structure except
  1654. * as directed by the documentation of a specific implementation. */
  1655. typedef struct psa_cipher_operation_s psa_cipher_operation_t;
  1656. /** \def PSA_CIPHER_OPERATION_INIT
  1657. *
  1658. * This macro returns a suitable initializer for a cipher operation object of
  1659. * type #psa_cipher_operation_t.
  1660. */
  1661. #ifdef __DOXYGEN_ONLY__
  1662. /* This is an example definition for documentation purposes.
  1663. * Implementations should define a suitable value in `crypto_struct.h`.
  1664. */
  1665. #define PSA_CIPHER_OPERATION_INIT {0}
  1666. #endif
  1667. /** Return an initial value for a cipher operation object.
  1668. */
  1669. static psa_cipher_operation_t psa_cipher_operation_init(void);
  1670. /** Set the key for a multipart symmetric encryption operation.
  1671. *
  1672. * The sequence of operations to encrypt a message with a symmetric cipher
  1673. * is as follows:
  1674. * -# Allocate an operation object which will be passed to all the functions
  1675. * listed here.
  1676. * -# Initialize the operation object with one of the methods described in the
  1677. * documentation for #psa_cipher_operation_t, e.g.
  1678. * #PSA_CIPHER_OPERATION_INIT.
  1679. * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
  1680. * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
  1681. * generate or set the IV (initialization vector). You should use
  1682. * psa_cipher_generate_iv() unless the protocol you are implementing
  1683. * requires a specific IV value.
  1684. * -# Call psa_cipher_update() zero, one or more times, passing a fragment
  1685. * of the message each time.
  1686. * -# Call psa_cipher_finish().
  1687. *
  1688. * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
  1689. * the operation will need to be reset by a call to psa_cipher_abort(). The
  1690. * application may call psa_cipher_abort() at any time after the operation
  1691. * has been initialized.
  1692. *
  1693. * After a successful call to psa_cipher_encrypt_setup(), the application must
  1694. * eventually terminate the operation. The following events terminate an
  1695. * operation:
  1696. * - A successful call to psa_cipher_finish().
  1697. * - A call to psa_cipher_abort().
  1698. *
  1699. * \param[in,out] operation The operation object to set up. It must have
  1700. * been initialized as per the documentation for
  1701. * #psa_cipher_operation_t and not yet in use.
  1702. * \param key Identifier of the key to use for the operation.
  1703. * It must remain valid until the operation
  1704. * terminates. It must allow the usage
  1705. * #PSA_KEY_USAGE_ENCRYPT.
  1706. * \param alg The cipher algorithm to compute
  1707. * (\c PSA_ALG_XXX value such that
  1708. * #PSA_ALG_IS_CIPHER(\p alg) is true).
  1709. *
  1710. * \retval #PSA_SUCCESS
  1711. * Success.
  1712. * \retval #PSA_ERROR_INVALID_HANDLE
  1713. * \retval #PSA_ERROR_NOT_PERMITTED
  1714. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1715. * \p key is not compatible with \p alg.
  1716. * \retval #PSA_ERROR_NOT_SUPPORTED
  1717. * \p alg is not supported or is not a cipher algorithm.
  1718. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  1719. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1720. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1721. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1722. * \retval #PSA_ERROR_STORAGE_FAILURE
  1723. * \retval #PSA_ERROR_BAD_STATE
  1724. * The operation state is not valid (it must be inactive).
  1725. * \retval #PSA_ERROR_BAD_STATE
  1726. * The library has not been previously initialized by psa_crypto_init().
  1727. * It is implementation-dependent whether a failure to initialize
  1728. * results in this error code.
  1729. */
  1730. psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
  1731. mbedtls_svc_key_id_t key,
  1732. psa_algorithm_t alg);
  1733. /** Set the key for a multipart symmetric decryption operation.
  1734. *
  1735. * The sequence of operations to decrypt a message with a symmetric cipher
  1736. * is as follows:
  1737. * -# Allocate an operation object which will be passed to all the functions
  1738. * listed here.
  1739. * -# Initialize the operation object with one of the methods described in the
  1740. * documentation for #psa_cipher_operation_t, e.g.
  1741. * #PSA_CIPHER_OPERATION_INIT.
  1742. * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
  1743. * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
  1744. * decryption. If the IV is prepended to the ciphertext, you can call
  1745. * psa_cipher_update() on a buffer containing the IV followed by the
  1746. * beginning of the message.
  1747. * -# Call psa_cipher_update() zero, one or more times, passing a fragment
  1748. * of the message each time.
  1749. * -# Call psa_cipher_finish().
  1750. *
  1751. * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
  1752. * the operation will need to be reset by a call to psa_cipher_abort(). The
  1753. * application may call psa_cipher_abort() at any time after the operation
  1754. * has been initialized.
  1755. *
  1756. * After a successful call to psa_cipher_decrypt_setup(), the application must
  1757. * eventually terminate the operation. The following events terminate an
  1758. * operation:
  1759. * - A successful call to psa_cipher_finish().
  1760. * - A call to psa_cipher_abort().
  1761. *
  1762. * \param[in,out] operation The operation object to set up. It must have
  1763. * been initialized as per the documentation for
  1764. * #psa_cipher_operation_t and not yet in use.
  1765. * \param key Identifier of the key to use for the operation.
  1766. * It must remain valid until the operation
  1767. * terminates. It must allow the usage
  1768. * #PSA_KEY_USAGE_DECRYPT.
  1769. * \param alg The cipher algorithm to compute
  1770. * (\c PSA_ALG_XXX value such that
  1771. * #PSA_ALG_IS_CIPHER(\p alg) is true).
  1772. *
  1773. * \retval #PSA_SUCCESS
  1774. * Success.
  1775. * \retval #PSA_ERROR_INVALID_HANDLE
  1776. * \retval #PSA_ERROR_NOT_PERMITTED
  1777. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1778. * \p key is not compatible with \p alg.
  1779. * \retval #PSA_ERROR_NOT_SUPPORTED
  1780. * \p alg is not supported or is not a cipher algorithm.
  1781. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  1782. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1783. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1784. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1785. * \retval #PSA_ERROR_STORAGE_FAILURE
  1786. * \retval #PSA_ERROR_BAD_STATE
  1787. * The operation state is not valid (it must be inactive).
  1788. * \retval #PSA_ERROR_BAD_STATE
  1789. * The library has not been previously initialized by psa_crypto_init().
  1790. * It is implementation-dependent whether a failure to initialize
  1791. * results in this error code.
  1792. */
  1793. psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
  1794. mbedtls_svc_key_id_t key,
  1795. psa_algorithm_t alg);
  1796. /** Generate an IV for a symmetric encryption operation.
  1797. *
  1798. * This function generates a random IV (initialization vector), nonce
  1799. * or initial counter value for the encryption operation as appropriate
  1800. * for the chosen algorithm, key type and key size.
  1801. *
  1802. * The application must call psa_cipher_encrypt_setup() before
  1803. * calling this function.
  1804. *
  1805. * If this function returns an error status, the operation enters an error
  1806. * state and must be aborted by calling psa_cipher_abort().
  1807. *
  1808. * \param[in,out] operation Active cipher operation.
  1809. * \param[out] iv Buffer where the generated IV is to be written.
  1810. * \param iv_size Size of the \p iv buffer in bytes.
  1811. * \param[out] iv_length On success, the number of bytes of the
  1812. * generated IV.
  1813. *
  1814. * \retval #PSA_SUCCESS
  1815. * Success.
  1816. * \retval #PSA_ERROR_BAD_STATE
  1817. * The operation state is not valid (it must be active, with no IV set).
  1818. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  1819. * The size of the \p iv buffer is too small.
  1820. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  1821. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1822. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1823. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1824. * \retval #PSA_ERROR_STORAGE_FAILURE
  1825. * \retval #PSA_ERROR_BAD_STATE
  1826. * The library has not been previously initialized by psa_crypto_init().
  1827. * It is implementation-dependent whether a failure to initialize
  1828. * results in this error code.
  1829. */
  1830. psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
  1831. uint8_t *iv,
  1832. size_t iv_size,
  1833. size_t *iv_length);
  1834. /** Set the IV for a symmetric encryption or decryption operation.
  1835. *
  1836. * This function sets the IV (initialization vector), nonce
  1837. * or initial counter value for the encryption or decryption operation.
  1838. *
  1839. * The application must call psa_cipher_encrypt_setup() before
  1840. * calling this function.
  1841. *
  1842. * If this function returns an error status, the operation enters an error
  1843. * state and must be aborted by calling psa_cipher_abort().
  1844. *
  1845. * \note When encrypting, applications should use psa_cipher_generate_iv()
  1846. * instead of this function, unless implementing a protocol that requires
  1847. * a non-random IV.
  1848. *
  1849. * \param[in,out] operation Active cipher operation.
  1850. * \param[in] iv Buffer containing the IV to use.
  1851. * \param iv_length Size of the IV in bytes.
  1852. *
  1853. * \retval #PSA_SUCCESS
  1854. * Success.
  1855. * \retval #PSA_ERROR_BAD_STATE
  1856. * The operation state is not valid (it must be an active cipher
  1857. * encrypt operation, with no IV set).
  1858. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1859. * The size of \p iv is not acceptable for the chosen algorithm,
  1860. * or the chosen algorithm does not use an IV.
  1861. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  1862. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1863. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1864. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1865. * \retval #PSA_ERROR_STORAGE_FAILURE
  1866. * \retval #PSA_ERROR_BAD_STATE
  1867. * The library has not been previously initialized by psa_crypto_init().
  1868. * It is implementation-dependent whether a failure to initialize
  1869. * results in this error code.
  1870. */
  1871. psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
  1872. const uint8_t *iv,
  1873. size_t iv_length);
  1874. /** Encrypt or decrypt a message fragment in an active cipher operation.
  1875. *
  1876. * Before calling this function, you must:
  1877. * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
  1878. * The choice of setup function determines whether this function
  1879. * encrypts or decrypts its input.
  1880. * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
  1881. * (recommended when encrypting) or psa_cipher_set_iv().
  1882. *
  1883. * If this function returns an error status, the operation enters an error
  1884. * state and must be aborted by calling psa_cipher_abort().
  1885. *
  1886. * \param[in,out] operation Active cipher operation.
  1887. * \param[in] input Buffer containing the message fragment to
  1888. * encrypt or decrypt.
  1889. * \param input_length Size of the \p input buffer in bytes.
  1890. * \param[out] output Buffer where the output is to be written.
  1891. * \param output_size Size of the \p output buffer in bytes.
  1892. * \param[out] output_length On success, the number of bytes
  1893. * that make up the returned output.
  1894. *
  1895. * \retval #PSA_SUCCESS
  1896. * Success.
  1897. * \retval #PSA_ERROR_BAD_STATE
  1898. * The operation state is not valid (it must be active, with an IV set
  1899. * if required for the algorithm).
  1900. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  1901. * The size of the \p output buffer is too small.
  1902. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  1903. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1904. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1905. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1906. * \retval #PSA_ERROR_STORAGE_FAILURE
  1907. * \retval #PSA_ERROR_BAD_STATE
  1908. * The library has not been previously initialized by psa_crypto_init().
  1909. * It is implementation-dependent whether a failure to initialize
  1910. * results in this error code.
  1911. */
  1912. psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
  1913. const uint8_t *input,
  1914. size_t input_length,
  1915. uint8_t *output,
  1916. size_t output_size,
  1917. size_t *output_length);
  1918. /** Finish encrypting or decrypting a message in a cipher operation.
  1919. *
  1920. * The application must call psa_cipher_encrypt_setup() or
  1921. * psa_cipher_decrypt_setup() before calling this function. The choice
  1922. * of setup function determines whether this function encrypts or
  1923. * decrypts its input.
  1924. *
  1925. * This function finishes the encryption or decryption of the message
  1926. * formed by concatenating the inputs passed to preceding calls to
  1927. * psa_cipher_update().
  1928. *
  1929. * When this function returns successfuly, the operation becomes inactive.
  1930. * If this function returns an error status, the operation enters an error
  1931. * state and must be aborted by calling psa_cipher_abort().
  1932. *
  1933. * \param[in,out] operation Active cipher operation.
  1934. * \param[out] output Buffer where the output is to be written.
  1935. * \param output_size Size of the \p output buffer in bytes.
  1936. * \param[out] output_length On success, the number of bytes
  1937. * that make up the returned output.
  1938. *
  1939. * \retval #PSA_SUCCESS
  1940. * Success.
  1941. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1942. * The total input size passed to this operation is not valid for
  1943. * this particular algorithm. For example, the algorithm is a based
  1944. * on block cipher and requires a whole number of blocks, but the
  1945. * total input size is not a multiple of the block size.
  1946. * \retval #PSA_ERROR_INVALID_PADDING
  1947. * This is a decryption operation for an algorithm that includes
  1948. * padding, and the ciphertext does not contain valid padding.
  1949. * \retval #PSA_ERROR_BAD_STATE
  1950. * The operation state is not valid (it must be active, with an IV set
  1951. * if required for the algorithm).
  1952. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  1953. * The size of the \p output buffer is too small.
  1954. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  1955. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1956. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1957. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1958. * \retval #PSA_ERROR_STORAGE_FAILURE
  1959. * \retval #PSA_ERROR_BAD_STATE
  1960. * The library has not been previously initialized by psa_crypto_init().
  1961. * It is implementation-dependent whether a failure to initialize
  1962. * results in this error code.
  1963. */
  1964. psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
  1965. uint8_t *output,
  1966. size_t output_size,
  1967. size_t *output_length);
  1968. /** Abort a cipher operation.
  1969. *
  1970. * Aborting an operation frees all associated resources except for the
  1971. * \p operation structure itself. Once aborted, the operation object
  1972. * can be reused for another operation by calling
  1973. * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
  1974. *
  1975. * You may call this function any time after the operation object has
  1976. * been initialized as described in #psa_cipher_operation_t.
  1977. *
  1978. * In particular, calling psa_cipher_abort() after the operation has been
  1979. * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
  1980. * is safe and has no effect.
  1981. *
  1982. * \param[in,out] operation Initialized cipher operation.
  1983. *
  1984. * \retval #PSA_SUCCESS
  1985. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1986. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1987. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  1988. * \retval #PSA_ERROR_BAD_STATE
  1989. * The library has not been previously initialized by psa_crypto_init().
  1990. * It is implementation-dependent whether a failure to initialize
  1991. * results in this error code.
  1992. */
  1993. psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
  1994. /**@}*/
  1995. /** \defgroup aead Authenticated encryption with associated data (AEAD)
  1996. * @{
  1997. */
  1998. /** Process an authenticated encryption operation.
  1999. *
  2000. * \param key Identifier of the key to use for the
  2001. * operation. It must allow the usage
  2002. * #PSA_KEY_USAGE_ENCRYPT.
  2003. * \param alg The AEAD algorithm to compute
  2004. * (\c PSA_ALG_XXX value such that
  2005. * #PSA_ALG_IS_AEAD(\p alg) is true).
  2006. * \param[in] nonce Nonce or IV to use.
  2007. * \param nonce_length Size of the \p nonce buffer in bytes.
  2008. * \param[in] additional_data Additional data that will be authenticated
  2009. * but not encrypted.
  2010. * \param additional_data_length Size of \p additional_data in bytes.
  2011. * \param[in] plaintext Data that will be authenticated and
  2012. * encrypted.
  2013. * \param plaintext_length Size of \p plaintext in bytes.
  2014. * \param[out] ciphertext Output buffer for the authenticated and
  2015. * encrypted data. The additional data is not
  2016. * part of this output. For algorithms where the
  2017. * encrypted data and the authentication tag
  2018. * are defined as separate outputs, the
  2019. * authentication tag is appended to the
  2020. * encrypted data.
  2021. * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
  2022. * This must be appropriate for the selected
  2023. * algorithm and key:
  2024. * - A sufficient output size is
  2025. * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type,
  2026. * \p alg, \p plaintext_length) where
  2027. * \c key_type is the type of \p key.
  2028. * - #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p
  2029. * plaintext_length) evaluates to the maximum
  2030. * ciphertext size of any supported AEAD
  2031. * encryption.
  2032. * \param[out] ciphertext_length On success, the size of the output
  2033. * in the \p ciphertext buffer.
  2034. *
  2035. * \retval #PSA_SUCCESS
  2036. * Success.
  2037. * \retval #PSA_ERROR_INVALID_HANDLE
  2038. * \retval #PSA_ERROR_NOT_PERMITTED
  2039. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2040. * \p key is not compatible with \p alg.
  2041. * \retval #PSA_ERROR_NOT_SUPPORTED
  2042. * \p alg is not supported or is not an AEAD algorithm.
  2043. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  2044. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  2045. * \p ciphertext_size is too small.
  2046. * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type, \p alg,
  2047. * \p plaintext_length) or
  2048. * #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length) can be used to
  2049. * determine the required buffer size.
  2050. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  2051. * \retval #PSA_ERROR_HARDWARE_FAILURE
  2052. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  2053. * \retval #PSA_ERROR_STORAGE_FAILURE
  2054. * \retval #PSA_ERROR_BAD_STATE
  2055. * The library has not been previously initialized by psa_crypto_init().
  2056. * It is implementation-dependent whether a failure to initialize
  2057. * results in this error code.
  2058. */
  2059. psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
  2060. psa_algorithm_t alg,
  2061. const uint8_t *nonce,
  2062. size_t nonce_length,
  2063. const uint8_t *additional_data,
  2064. size_t additional_data_length,
  2065. const uint8_t *plaintext,
  2066. size_t plaintext_length,
  2067. uint8_t *ciphertext,
  2068. size_t ciphertext_size,
  2069. size_t *ciphertext_length);
  2070. /** Process an authenticated decryption operation.
  2071. *
  2072. * \param key Identifier of the key to use for the
  2073. * operation. It must allow the usage
  2074. * #PSA_KEY_USAGE_DECRYPT.
  2075. * \param alg The AEAD algorithm to compute
  2076. * (\c PSA_ALG_XXX value such that
  2077. * #PSA_ALG_IS_AEAD(\p alg) is true).
  2078. * \param[in] nonce Nonce or IV to use.
  2079. * \param nonce_length Size of the \p nonce buffer in bytes.
  2080. * \param[in] additional_data Additional data that has been authenticated
  2081. * but not encrypted.
  2082. * \param additional_data_length Size of \p additional_data in bytes.
  2083. * \param[in] ciphertext Data that has been authenticated and
  2084. * encrypted. For algorithms where the
  2085. * encrypted data and the authentication tag
  2086. * are defined as separate inputs, the buffer
  2087. * must contain the encrypted data followed
  2088. * by the authentication tag.
  2089. * \param ciphertext_length Size of \p ciphertext in bytes.
  2090. * \param[out] plaintext Output buffer for the decrypted data.
  2091. * \param plaintext_size Size of the \p plaintext buffer in bytes.
  2092. * This must be appropriate for the selected
  2093. * algorithm and key:
  2094. * - A sufficient output size is
  2095. * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type,
  2096. * \p alg, \p ciphertext_length) where
  2097. * \c key_type is the type of \p key.
  2098. * - #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p
  2099. * ciphertext_length) evaluates to the maximum
  2100. * plaintext size of any supported AEAD
  2101. * decryption.
  2102. * \param[out] plaintext_length On success, the size of the output
  2103. * in the \p plaintext buffer.
  2104. *
  2105. * \retval #PSA_SUCCESS
  2106. * Success.
  2107. * \retval #PSA_ERROR_INVALID_HANDLE
  2108. * \retval #PSA_ERROR_INVALID_SIGNATURE
  2109. * The ciphertext is not authentic.
  2110. * \retval #PSA_ERROR_NOT_PERMITTED
  2111. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2112. * \p key is not compatible with \p alg.
  2113. * \retval #PSA_ERROR_NOT_SUPPORTED
  2114. * \p alg is not supported or is not an AEAD algorithm.
  2115. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  2116. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  2117. * \p plaintext_size is too small.
  2118. * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type, \p alg,
  2119. * \p ciphertext_length) or
  2120. * #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length) can be used
  2121. * to determine the required buffer size.
  2122. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  2123. * \retval #PSA_ERROR_HARDWARE_FAILURE
  2124. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  2125. * \retval #PSA_ERROR_STORAGE_FAILURE
  2126. * \retval #PSA_ERROR_BAD_STATE
  2127. * The library has not been previously initialized by psa_crypto_init().
  2128. * It is implementation-dependent whether a failure to initialize
  2129. * results in this error code.
  2130. */
  2131. psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
  2132. psa_algorithm_t alg,
  2133. const uint8_t *nonce,
  2134. size_t nonce_length,
  2135. const uint8_t *additional_data,
  2136. size_t additional_data_length,
  2137. const uint8_t *ciphertext,
  2138. size_t ciphertext_length,
  2139. uint8_t *plaintext,
  2140. size_t plaintext_size,
  2141. size_t *plaintext_length);
  2142. /** The type of the state data structure for multipart AEAD operations.
  2143. *
  2144. * Before calling any function on an AEAD operation object, the application
  2145. * must initialize it by any of the following means:
  2146. * - Set the structure to all-bits-zero, for example:
  2147. * \code
  2148. * psa_aead_operation_t operation;
  2149. * memset(&operation, 0, sizeof(operation));
  2150. * \endcode
  2151. * - Initialize the structure to logical zero values, for example:
  2152. * \code
  2153. * psa_aead_operation_t operation = {0};
  2154. * \endcode
  2155. * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
  2156. * for example:
  2157. * \code
  2158. * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
  2159. * \endcode
  2160. * - Assign the result of the function psa_aead_operation_init()
  2161. * to the structure, for example:
  2162. * \code
  2163. * psa_aead_operation_t operation;
  2164. * operation = psa_aead_operation_init();
  2165. * \endcode
  2166. *
  2167. * This is an implementation-defined \c struct. Applications should not
  2168. * make any assumptions about the content of this structure except
  2169. * as directed by the documentation of a specific implementation. */
  2170. typedef struct psa_aead_operation_s psa_aead_operation_t;
  2171. /** \def PSA_AEAD_OPERATION_INIT
  2172. *
  2173. * This macro returns a suitable initializer for an AEAD operation object of
  2174. * type #psa_aead_operation_t.
  2175. */
  2176. #ifdef __DOXYGEN_ONLY__
  2177. /* This is an example definition for documentation purposes.
  2178. * Implementations should define a suitable value in `crypto_struct.h`.
  2179. */
  2180. #define PSA_AEAD_OPERATION_INIT {0}
  2181. #endif
  2182. /** Return an initial value for an AEAD operation object.
  2183. */
  2184. static psa_aead_operation_t psa_aead_operation_init(void);
  2185. /** Set the key for a multipart authenticated encryption operation.
  2186. *
  2187. * The sequence of operations to encrypt a message with authentication
  2188. * is as follows:
  2189. * -# Allocate an operation object which will be passed to all the functions
  2190. * listed here.
  2191. * -# Initialize the operation object with one of the methods described in the
  2192. * documentation for #psa_aead_operation_t, e.g.
  2193. * #PSA_AEAD_OPERATION_INIT.
  2194. * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
  2195. * -# If needed, call psa_aead_set_lengths() to specify the length of the
  2196. * inputs to the subsequent calls to psa_aead_update_ad() and
  2197. * psa_aead_update(). See the documentation of psa_aead_set_lengths()
  2198. * for details.
  2199. * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
  2200. * generate or set the nonce. You should use
  2201. * psa_aead_generate_nonce() unless the protocol you are implementing
  2202. * requires a specific nonce value.
  2203. * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
  2204. * of the non-encrypted additional authenticated data each time.
  2205. * -# Call psa_aead_update() zero, one or more times, passing a fragment
  2206. * of the message to encrypt each time.
  2207. * -# Call psa_aead_finish().
  2208. *
  2209. * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
  2210. * the operation will need to be reset by a call to psa_aead_abort(). The
  2211. * application may call psa_aead_abort() at any time after the operation
  2212. * has been initialized.
  2213. *
  2214. * After a successful call to psa_aead_encrypt_setup(), the application must
  2215. * eventually terminate the operation. The following events terminate an
  2216. * operation:
  2217. * - A successful call to psa_aead_finish().
  2218. * - A call to psa_aead_abort().
  2219. *
  2220. * \param[in,out] operation The operation object to set up. It must have
  2221. * been initialized as per the documentation for
  2222. * #psa_aead_operation_t and not yet in use.
  2223. * \param key Identifier of the key to use for the operation.
  2224. * It must remain valid until the operation
  2225. * terminates. It must allow the usage
  2226. * #PSA_KEY_USAGE_ENCRYPT.
  2227. * \param alg The AEAD algorithm to compute
  2228. * (\c PSA_ALG_XXX value such that
  2229. * #PSA_ALG_IS_AEAD(\p alg) is true).
  2230. *
  2231. * \retval #PSA_SUCCESS
  2232. * Success.
  2233. * \retval #PSA_ERROR_BAD_STATE
  2234. * The operation state is not valid (it must be inactive).
  2235. * \retval #PSA_ERROR_INVALID_HANDLE
  2236. * \retval #PSA_ERROR_NOT_PERMITTED
  2237. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2238. * \p key is not compatible with \p alg.
  2239. * \retval #PSA_ERROR_NOT_SUPPORTED
  2240. * \p alg is not supported or is not an AEAD algorithm.
  2241. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  2242. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  2243. * \retval #PSA_ERROR_HARDWARE_FAILURE
  2244. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  2245. * \retval #PSA_ERROR_STORAGE_FAILURE
  2246. * \retval #PSA_ERROR_BAD_STATE
  2247. * The library has not been previously initialized by psa_crypto_init().
  2248. * It is implementation-dependent whether a failure to initialize
  2249. * results in this error code.
  2250. */
  2251. psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
  2252. mbedtls_svc_key_id_t key,
  2253. psa_algorithm_t alg);
  2254. /** Set the key for a multipart authenticated decryption operation.
  2255. *
  2256. * The sequence of operations to decrypt a message with authentication
  2257. * is as follows:
  2258. * -# Allocate an operation object which will be passed to all the functions
  2259. * listed here.
  2260. * -# Initialize the operation object with one of the methods described in the
  2261. * documentation for #psa_aead_operation_t, e.g.
  2262. * #PSA_AEAD_OPERATION_INIT.
  2263. * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
  2264. * -# If needed, call psa_aead_set_lengths() to specify the length of the
  2265. * inputs to the subsequent calls to psa_aead_update_ad() and
  2266. * psa_aead_update(). See the documentation of psa_aead_set_lengths()
  2267. * for details.
  2268. * -# Call psa_aead_set_nonce() with the nonce for the decryption.
  2269. * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
  2270. * of the non-encrypted additional authenticated data each time.
  2271. * -# Call psa_aead_update() zero, one or more times, passing a fragment
  2272. * of the ciphertext to decrypt each time.
  2273. * -# Call psa_aead_verify().
  2274. *
  2275. * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
  2276. * the operation will need to be reset by a call to psa_aead_abort(). The
  2277. * application may call psa_aead_abort() at any time after the operation
  2278. * has been initialized.
  2279. *
  2280. * After a successful call to psa_aead_decrypt_setup(), the application must
  2281. * eventually terminate the operation. The following events terminate an
  2282. * operation:
  2283. * - A successful call to psa_aead_verify().
  2284. * - A call to psa_aead_abort().
  2285. *
  2286. * \param[in,out] operation The operation object to set up. It must have
  2287. * been initialized as per the documentation for
  2288. * #psa_aead_operation_t and not yet in use.
  2289. * \param key Identifier of the key to use for the operation.
  2290. * It must remain valid until the operation
  2291. * terminates. It must allow the usage
  2292. * #PSA_KEY_USAGE_DECRYPT.
  2293. * \param alg The AEAD algorithm to compute
  2294. * (\c PSA_ALG_XXX value such that
  2295. * #PSA_ALG_IS_AEAD(\p alg) is true).
  2296. *
  2297. * \retval #PSA_SUCCESS
  2298. * Success.
  2299. * \retval #PSA_ERROR_BAD_STATE
  2300. * The operation state is not valid (it must be inactive).
  2301. * \retval #PSA_ERROR_INVALID_HANDLE
  2302. * \retval #PSA_ERROR_NOT_PERMITTED
  2303. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2304. * \p key is not compatible with \p alg.
  2305. * \retval #PSA_ERROR_NOT_SUPPORTED
  2306. * \p alg is not supported or is not an AEAD algorithm.
  2307. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  2308. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  2309. * \retval #PSA_ERROR_HARDWARE_FAILURE
  2310. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  2311. * \retval #PSA_ERROR_STORAGE_FAILURE
  2312. * \retval #PSA_ERROR_BAD_STATE
  2313. * The library has not been previously initialized by psa_crypto_init().
  2314. * It is implementation-dependent whether a failure to initialize
  2315. * results in this error code.
  2316. */
  2317. psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
  2318. mbedtls_svc_key_id_t key,
  2319. psa_algorithm_t alg);
  2320. /** Generate a random nonce for an authenticated encryption operation.
  2321. *
  2322. * This function generates a random nonce for the authenticated encryption
  2323. * operation with an appropriate size for the chosen algorithm, key type
  2324. * and key size.
  2325. *
  2326. * The application must call psa_aead_encrypt_setup() before
  2327. * calling this function.
  2328. *
  2329. * If this function returns an error status, the operation enters an error
  2330. * state and must be aborted by calling psa_aead_abort().
  2331. *
  2332. * \param[in,out] operation Active AEAD operation.
  2333. * \param[out] nonce Buffer where the generated nonce is to be
  2334. * written.
  2335. * \param nonce_size Size of the \p nonce buffer in bytes.
  2336. * \param[out] nonce_length On success, the number of bytes of the
  2337. * generated nonce.
  2338. *
  2339. * \retval #PSA_SUCCESS
  2340. * Success.
  2341. * \retval #PSA_ERROR_BAD_STATE
  2342. * The operation state is not valid (it must be an active aead encrypt
  2343. * operation, with no nonce set).
  2344. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  2345. * The size of the \p nonce buffer is too small.
  2346. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  2347. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  2348. * \retval #PSA_ERROR_HARDWARE_FAILURE
  2349. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  2350. * \retval #PSA_ERROR_STORAGE_FAILURE
  2351. * \retval #PSA_ERROR_BAD_STATE
  2352. * The library has not been previously initialized by psa_crypto_init().
  2353. * It is implementation-dependent whether a failure to initialize
  2354. * results in this error code.
  2355. */
  2356. psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
  2357. uint8_t *nonce,
  2358. size_t nonce_size,
  2359. size_t *nonce_length);
  2360. /** Set the nonce for an authenticated encryption or decryption operation.
  2361. *
  2362. * This function sets the nonce for the authenticated
  2363. * encryption or decryption operation.
  2364. *
  2365. * The application must call psa_aead_encrypt_setup() or
  2366. * psa_aead_decrypt_setup() before calling this function.
  2367. *
  2368. * If this function returns an error status, the operation enters an error
  2369. * state and must be aborted by calling psa_aead_abort().
  2370. *
  2371. * \note When encrypting, applications should use psa_aead_generate_nonce()
  2372. * instead of this function, unless implementing a protocol that requires
  2373. * a non-random IV.
  2374. *
  2375. * \param[in,out] operation Active AEAD operation.
  2376. * \param[in] nonce Buffer containing the nonce to use.
  2377. * \param nonce_length Size of the nonce in bytes.
  2378. *
  2379. * \retval #PSA_SUCCESS
  2380. * Success.
  2381. * \retval #PSA_ERROR_BAD_STATE
  2382. * The operation state is not valid (it must be active, with no nonce
  2383. * set).
  2384. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2385. * The size of \p nonce is not acceptable for the chosen algorithm.
  2386. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  2387. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  2388. * \retval #PSA_ERROR_HARDWARE_FAILURE
  2389. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  2390. * \retval #PSA_ERROR_STORAGE_FAILURE
  2391. * \retval #PSA_ERROR_BAD_STATE
  2392. * The library has not been previously initialized by psa_crypto_init().
  2393. * It is implementation-dependent whether a failure to initialize
  2394. * results in this error code.
  2395. */
  2396. psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
  2397. const uint8_t *nonce,
  2398. size_t nonce_length);
  2399. /** Declare the lengths of the message and additional data for AEAD.
  2400. *
  2401. * The application must call this function before calling
  2402. * psa_aead_update_ad() or psa_aead_update() if the algorithm for
  2403. * the operation requires it. If the algorithm does not require it,
  2404. * calling this function is optional, but if this function is called
  2405. * then the implementation must enforce the lengths.
  2406. *
  2407. * You may call this function before or after setting the nonce with
  2408. * psa_aead_set_nonce() or psa_aead_generate_nonce().
  2409. *
  2410. * - For #PSA_ALG_CCM, calling this function is required.
  2411. * - For the other AEAD algorithms defined in this specification, calling
  2412. * this function is not required.
  2413. * - For vendor-defined algorithm, refer to the vendor documentation.
  2414. *
  2415. * If this function returns an error status, the operation enters an error
  2416. * state and must be aborted by calling psa_aead_abort().
  2417. *
  2418. * \param[in,out] operation Active AEAD operation.
  2419. * \param ad_length Size of the non-encrypted additional
  2420. * authenticated data in bytes.
  2421. * \param plaintext_length Size of the plaintext to encrypt in bytes.
  2422. *
  2423. * \retval #PSA_SUCCESS
  2424. * Success.
  2425. * \retval #PSA_ERROR_BAD_STATE
  2426. * The operation state is not valid (it must be active, and
  2427. * psa_aead_update_ad() and psa_aead_update() must not have been
  2428. * called yet).
  2429. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2430. * At least one of the lengths is not acceptable for the chosen
  2431. * algorithm.
  2432. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  2433. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  2434. * \retval #PSA_ERROR_HARDWARE_FAILURE
  2435. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  2436. * \retval #PSA_ERROR_BAD_STATE
  2437. * The library has not been previously initialized by psa_crypto_init().
  2438. * It is implementation-dependent whether a failure to initialize
  2439. * results in this error code.
  2440. */
  2441. psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
  2442. size_t ad_length,
  2443. size_t plaintext_length);
  2444. /** Pass additional data to an active AEAD operation.
  2445. *
  2446. * Additional data is authenticated, but not encrypted.
  2447. *
  2448. * You may call this function multiple times to pass successive fragments
  2449. * of the additional data. You may not call this function after passing
  2450. * data to encrypt or decrypt with psa_aead_update().
  2451. *
  2452. * Before calling this function, you must:
  2453. * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
  2454. * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
  2455. *
  2456. * If this function returns an error status, the operation enters an error
  2457. * state and must be aborted by calling psa_aead_abort().
  2458. *
  2459. * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
  2460. * there is no guarantee that the input is valid. Therefore, until
  2461. * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
  2462. * treat the input as untrusted and prepare to undo any action that
  2463. * depends on the input if psa_aead_verify() returns an error status.
  2464. *
  2465. * \param[in,out] operation Active AEAD operation.
  2466. * \param[in] input Buffer containing the fragment of
  2467. * additional data.
  2468. * \param input_length Size of the \p input buffer in bytes.
  2469. *
  2470. * \retval #PSA_SUCCESS
  2471. * Success.
  2472. * \retval #PSA_ERROR_BAD_STATE
  2473. * The operation state is not valid (it must be active, have a nonce
  2474. * set, have lengths set if required by the algorithm, and
  2475. * psa_aead_update() must not have been called yet).
  2476. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2477. * The total input length overflows the additional data length that
  2478. * was previously specified with psa_aead_set_lengths().
  2479. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  2480. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  2481. * \retval #PSA_ERROR_HARDWARE_FAILURE
  2482. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  2483. * \retval #PSA_ERROR_STORAGE_FAILURE
  2484. * \retval #PSA_ERROR_BAD_STATE
  2485. * The library has not been previously initialized by psa_crypto_init().
  2486. * It is implementation-dependent whether a failure to initialize
  2487. * results in this error code.
  2488. */
  2489. psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
  2490. const uint8_t *input,
  2491. size_t input_length);
  2492. /** Encrypt or decrypt a message fragment in an active AEAD operation.
  2493. *
  2494. * Before calling this function, you must:
  2495. * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
  2496. * The choice of setup function determines whether this function
  2497. * encrypts or decrypts its input.
  2498. * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
  2499. * 3. Call psa_aead_update_ad() to pass all the additional data.
  2500. *
  2501. * If this function returns an error status, the operation enters an error
  2502. * state and must be aborted by calling psa_aead_abort().
  2503. *
  2504. * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
  2505. * there is no guarantee that the input is valid. Therefore, until
  2506. * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
  2507. * - Do not use the output in any way other than storing it in a
  2508. * confidential location. If you take any action that depends
  2509. * on the tentative decrypted data, this action will need to be
  2510. * undone if the input turns out not to be valid. Furthermore,
  2511. * if an adversary can observe that this action took place
  2512. * (for example through timing), they may be able to use this
  2513. * fact as an oracle to decrypt any message encrypted with the
  2514. * same key.
  2515. * - In particular, do not copy the output anywhere but to a
  2516. * memory or storage space that you have exclusive access to.
  2517. *
  2518. * This function does not require the input to be aligned to any
  2519. * particular block boundary. If the implementation can only process
  2520. * a whole block at a time, it must consume all the input provided, but
  2521. * it may delay the end of the corresponding output until a subsequent
  2522. * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
  2523. * provides sufficient input. The amount of data that can be delayed
  2524. * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
  2525. *
  2526. * \param[in,out] operation Active AEAD operation.
  2527. * \param[in] input Buffer containing the message fragment to
  2528. * encrypt or decrypt.
  2529. * \param input_length Size of the \p input buffer in bytes.
  2530. * \param[out] output Buffer where the output is to be written.
  2531. * \param output_size Size of the \p output buffer in bytes.
  2532. * This must be appropriate for the selected
  2533. * algorithm and key:
  2534. * - A sufficient output size is
  2535. * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type,
  2536. * \c alg, \p input_length) where
  2537. * \c key_type is the type of key and \c alg is
  2538. * the algorithm that were used to set up the
  2539. * operation.
  2540. * - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p
  2541. * input_length) evaluates to the maximum
  2542. * output size of any supported AEAD
  2543. * algorithm.
  2544. * \param[out] output_length On success, the number of bytes
  2545. * that make up the returned output.
  2546. *
  2547. * \retval #PSA_SUCCESS
  2548. * Success.
  2549. * \retval #PSA_ERROR_BAD_STATE
  2550. * The operation state is not valid (it must be active, have a nonce
  2551. * set, and have lengths set if required by the algorithm).
  2552. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  2553. * The size of the \p output buffer is too small.
  2554. * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or
  2555. * #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to
  2556. * determine the required buffer size.
  2557. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2558. * The total length of input to psa_aead_update_ad() so far is
  2559. * less than the additional data length that was previously
  2560. * specified with psa_aead_set_lengths().
  2561. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2562. * The total input length overflows the plaintext length that
  2563. * was previously specified with psa_aead_set_lengths().
  2564. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  2565. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  2566. * \retval #PSA_ERROR_HARDWARE_FAILURE
  2567. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  2568. * \retval #PSA_ERROR_STORAGE_FAILURE
  2569. * \retval #PSA_ERROR_BAD_STATE
  2570. * The library has not been previously initialized by psa_crypto_init().
  2571. * It is implementation-dependent whether a failure to initialize
  2572. * results in this error code.
  2573. */
  2574. psa_status_t psa_aead_update(psa_aead_operation_t *operation,
  2575. const uint8_t *input,
  2576. size_t input_length,
  2577. uint8_t *output,
  2578. size_t output_size,
  2579. size_t *output_length);
  2580. /** Finish encrypting a message in an AEAD operation.
  2581. *
  2582. * The operation must have been set up with psa_aead_encrypt_setup().
  2583. *
  2584. * This function finishes the authentication of the additional data
  2585. * formed by concatenating the inputs passed to preceding calls to
  2586. * psa_aead_update_ad() with the plaintext formed by concatenating the
  2587. * inputs passed to preceding calls to psa_aead_update().
  2588. *
  2589. * This function has two output buffers:
  2590. * - \p ciphertext contains trailing ciphertext that was buffered from
  2591. * preceding calls to psa_aead_update().
  2592. * - \p tag contains the authentication tag.
  2593. *
  2594. * When this function returns successfuly, the operation becomes inactive.
  2595. * If this function returns an error status, the operation enters an error
  2596. * state and must be aborted by calling psa_aead_abort().
  2597. *
  2598. * \param[in,out] operation Active AEAD operation.
  2599. * \param[out] ciphertext Buffer where the last part of the ciphertext
  2600. * is to be written.
  2601. * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
  2602. * This must be appropriate for the selected
  2603. * algorithm and key:
  2604. * - A sufficient output size is
  2605. * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type,
  2606. * \c alg) where \c key_type is the type of key
  2607. * and \c alg is the algorithm that were used to
  2608. * set up the operation.
  2609. * - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to
  2610. * the maximum output size of any supported AEAD
  2611. * algorithm.
  2612. * \param[out] ciphertext_length On success, the number of bytes of
  2613. * returned ciphertext.
  2614. * \param[out] tag Buffer where the authentication tag is
  2615. * to be written.
  2616. * \param tag_size Size of the \p tag buffer in bytes.
  2617. * This must be appropriate for the selected
  2618. * algorithm and key:
  2619. * - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c
  2620. * key_type, \c key_bits, \c alg) where
  2621. * \c key_type and \c key_bits are the type and
  2622. * bit-size of the key, and \c alg is the
  2623. * algorithm that were used in the call to
  2624. * psa_aead_encrypt_setup().
  2625. * - #PSA_AEAD_TAG_MAX_SIZE evaluates to the
  2626. * maximum tag size of any supported AEAD
  2627. * algorithm.
  2628. * \param[out] tag_length On success, the number of bytes
  2629. * that make up the returned tag.
  2630. *
  2631. * \retval #PSA_SUCCESS
  2632. * Success.
  2633. * \retval #PSA_ERROR_BAD_STATE
  2634. * The operation state is not valid (it must be an active encryption
  2635. * operation with a nonce set).
  2636. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  2637. * The size of the \p ciphertext or \p tag buffer is too small.
  2638. * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, \c alg) or
  2639. * #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE can be used to determine the
  2640. * required \p ciphertext buffer size. #PSA_AEAD_TAG_LENGTH(\c key_type,
  2641. * \c key_bits, \c alg) or #PSA_AEAD_TAG_MAX_SIZE can be used to
  2642. * determine the required \p tag buffer size.
  2643. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2644. * The total length of input to psa_aead_update_ad() so far is
  2645. * less than the additional data length that was previously
  2646. * specified with psa_aead_set_lengths().
  2647. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2648. * The total length of input to psa_aead_update() so far is
  2649. * less than the plaintext length that was previously
  2650. * specified with psa_aead_set_lengths().
  2651. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  2652. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  2653. * \retval #PSA_ERROR_HARDWARE_FAILURE
  2654. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  2655. * \retval #PSA_ERROR_STORAGE_FAILURE
  2656. * \retval #PSA_ERROR_BAD_STATE
  2657. * The library has not been previously initialized by psa_crypto_init().
  2658. * It is implementation-dependent whether a failure to initialize
  2659. * results in this error code.
  2660. */
  2661. psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
  2662. uint8_t *ciphertext,
  2663. size_t ciphertext_size,
  2664. size_t *ciphertext_length,
  2665. uint8_t *tag,
  2666. size_t tag_size,
  2667. size_t *tag_length);
  2668. /** Finish authenticating and decrypting a message in an AEAD operation.
  2669. *
  2670. * The operation must have been set up with psa_aead_decrypt_setup().
  2671. *
  2672. * This function finishes the authenticated decryption of the message
  2673. * components:
  2674. *
  2675. * - The additional data consisting of the concatenation of the inputs
  2676. * passed to preceding calls to psa_aead_update_ad().
  2677. * - The ciphertext consisting of the concatenation of the inputs passed to
  2678. * preceding calls to psa_aead_update().
  2679. * - The tag passed to this function call.
  2680. *
  2681. * If the authentication tag is correct, this function outputs any remaining
  2682. * plaintext and reports success. If the authentication tag is not correct,
  2683. * this function returns #PSA_ERROR_INVALID_SIGNATURE.
  2684. *
  2685. * When this function returns successfuly, the operation becomes inactive.
  2686. * If this function returns an error status, the operation enters an error
  2687. * state and must be aborted by calling psa_aead_abort().
  2688. *
  2689. * \note Implementations shall make the best effort to ensure that the
  2690. * comparison between the actual tag and the expected tag is performed
  2691. * in constant time.
  2692. *
  2693. * \param[in,out] operation Active AEAD operation.
  2694. * \param[out] plaintext Buffer where the last part of the plaintext
  2695. * is to be written. This is the remaining data
  2696. * from previous calls to psa_aead_update()
  2697. * that could not be processed until the end
  2698. * of the input.
  2699. * \param plaintext_size Size of the \p plaintext buffer in bytes.
  2700. * This must be appropriate for the selected algorithm and key:
  2701. * - A sufficient output size is
  2702. * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type,
  2703. * \c alg) where \c key_type is the type of key
  2704. * and \c alg is the algorithm that were used to
  2705. * set up the operation.
  2706. * - #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE evaluates to
  2707. * the maximum output size of any supported AEAD
  2708. * algorithm.
  2709. * \param[out] plaintext_length On success, the number of bytes of
  2710. * returned plaintext.
  2711. * \param[in] tag Buffer containing the authentication tag.
  2712. * \param tag_length Size of the \p tag buffer in bytes.
  2713. *
  2714. * \retval #PSA_SUCCESS
  2715. * Success.
  2716. * \retval #PSA_ERROR_INVALID_SIGNATURE
  2717. * The calculations were successful, but the authentication tag is
  2718. * not correct.
  2719. * \retval #PSA_ERROR_BAD_STATE
  2720. * The operation state is not valid (it must be an active decryption
  2721. * operation with a nonce set).
  2722. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  2723. * The size of the \p plaintext buffer is too small.
  2724. * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, \c alg) or
  2725. * #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE can be used to determine the
  2726. * required buffer size.
  2727. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2728. * The total length of input to psa_aead_update_ad() so far is
  2729. * less than the additional data length that was previously
  2730. * specified with psa_aead_set_lengths().
  2731. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2732. * The total length of input to psa_aead_update() so far is
  2733. * less than the plaintext length that was previously
  2734. * specified with psa_aead_set_lengths().
  2735. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  2736. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  2737. * \retval #PSA_ERROR_HARDWARE_FAILURE
  2738. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  2739. * \retval #PSA_ERROR_STORAGE_FAILURE
  2740. * \retval #PSA_ERROR_BAD_STATE
  2741. * The library has not been previously initialized by psa_crypto_init().
  2742. * It is implementation-dependent whether a failure to initialize
  2743. * results in this error code.
  2744. */
  2745. psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
  2746. uint8_t *plaintext,
  2747. size_t plaintext_size,
  2748. size_t *plaintext_length,
  2749. const uint8_t *tag,
  2750. size_t tag_length);
  2751. /** Abort an AEAD operation.
  2752. *
  2753. * Aborting an operation frees all associated resources except for the
  2754. * \p operation structure itself. Once aborted, the operation object
  2755. * can be reused for another operation by calling
  2756. * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
  2757. *
  2758. * You may call this function any time after the operation object has
  2759. * been initialized as described in #psa_aead_operation_t.
  2760. *
  2761. * In particular, calling psa_aead_abort() after the operation has been
  2762. * terminated by a call to psa_aead_abort(), psa_aead_finish() or
  2763. * psa_aead_verify() is safe and has no effect.
  2764. *
  2765. * \param[in,out] operation Initialized AEAD operation.
  2766. *
  2767. * \retval #PSA_SUCCESS
  2768. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  2769. * \retval #PSA_ERROR_HARDWARE_FAILURE
  2770. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  2771. * \retval #PSA_ERROR_BAD_STATE
  2772. * The library has not been previously initialized by psa_crypto_init().
  2773. * It is implementation-dependent whether a failure to initialize
  2774. * results in this error code.
  2775. */
  2776. psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
  2777. /**@}*/
  2778. /** \defgroup asymmetric Asymmetric cryptography
  2779. * @{
  2780. */
  2781. /**
  2782. * \brief Sign a message with a private key. For hash-and-sign algorithms,
  2783. * this includes the hashing step.
  2784. *
  2785. * \note To perform a multi-part hash-and-sign signature algorithm, first use
  2786. * a multi-part hash operation and then pass the resulting hash to
  2787. * psa_sign_hash(). PSA_ALG_GET_HASH(\p alg) can be used to determine the
  2788. * hash algorithm to use.
  2789. *
  2790. * \param[in] key Identifier of the key to use for the operation.
  2791. * It must be an asymmetric key pair. The key must
  2792. * allow the usage #PSA_KEY_USAGE_SIGN_MESSAGE.
  2793. * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
  2794. * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
  2795. * is true), that is compatible with the type of
  2796. * \p key.
  2797. * \param[in] input The input message to sign.
  2798. * \param[in] input_length Size of the \p input buffer in bytes.
  2799. * \param[out] signature Buffer where the signature is to be written.
  2800. * \param[in] signature_size Size of the \p signature buffer in bytes. This
  2801. * must be appropriate for the selected
  2802. * algorithm and key:
  2803. * - The required signature size is
  2804. * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  2805. * where \c key_type and \c key_bits are the type and
  2806. * bit-size respectively of key.
  2807. * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
  2808. * maximum signature size of any supported
  2809. * signature algorithm.
  2810. * \param[out] signature_length On success, the number of bytes that make up
  2811. * the returned signature value.
  2812. *
  2813. * \retval #PSA_SUCCESS
  2814. * \retval #PSA_ERROR_INVALID_HANDLE
  2815. * \retval #PSA_ERROR_NOT_PERMITTED
  2816. * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
  2817. * or it does not permit the requested algorithm.
  2818. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  2819. * The size of the \p signature buffer is too small. You can
  2820. * determine a sufficient buffer size by calling
  2821. * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  2822. * where \c key_type and \c key_bits are the type and bit-size
  2823. * respectively of \p key.
  2824. * \retval #PSA_ERROR_NOT_SUPPORTED
  2825. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2826. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  2827. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  2828. * \retval #PSA_ERROR_HARDWARE_FAILURE
  2829. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  2830. * \retval #PSA_ERROR_STORAGE_FAILURE
  2831. * \retval #PSA_ERROR_DATA_CORRUPT
  2832. * \retval #PSA_ERROR_DATA_INVALID
  2833. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
  2834. * \retval #PSA_ERROR_BAD_STATE
  2835. * The library has not been previously initialized by psa_crypto_init().
  2836. * It is implementation-dependent whether a failure to initialize
  2837. * results in this error code.
  2838. */
  2839. psa_status_t psa_sign_message( mbedtls_svc_key_id_t key,
  2840. psa_algorithm_t alg,
  2841. const uint8_t * input,
  2842. size_t input_length,
  2843. uint8_t * signature,
  2844. size_t signature_size,
  2845. size_t * signature_length );
  2846. /** \brief Verify the signature of a message with a public key, using
  2847. * a hash-and-sign verification algorithm.
  2848. *
  2849. * \note To perform a multi-part hash-and-sign signature verification
  2850. * algorithm, first use a multi-part hash operation to hash the message
  2851. * and then pass the resulting hash to psa_verify_hash().
  2852. * PSA_ALG_GET_HASH(\p alg) can be used to determine the hash algorithm
  2853. * to use.
  2854. *
  2855. * \param[in] key Identifier of the key to use for the operation.
  2856. * It must be a public key or an asymmetric key
  2857. * pair. The key must allow the usage
  2858. * #PSA_KEY_USAGE_VERIFY_MESSAGE.
  2859. * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
  2860. * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
  2861. * is true), that is compatible with the type of
  2862. * \p key.
  2863. * \param[in] input The message whose signature is to be verified.
  2864. * \param[in] input_length Size of the \p input buffer in bytes.
  2865. * \param[out] signature Buffer containing the signature to verify.
  2866. * \param[in] signature_length Size of the \p signature buffer in bytes.
  2867. *
  2868. * \retval #PSA_SUCCESS
  2869. * \retval #PSA_ERROR_INVALID_HANDLE
  2870. * \retval #PSA_ERROR_NOT_PERMITTED
  2871. * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
  2872. * or it does not permit the requested algorithm.
  2873. * \retval #PSA_ERROR_INVALID_SIGNATURE
  2874. * The calculation was performed successfully, but the passed signature
  2875. * is not a valid signature.
  2876. * \retval #PSA_ERROR_NOT_SUPPORTED
  2877. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2878. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  2879. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  2880. * \retval #PSA_ERROR_HARDWARE_FAILURE
  2881. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  2882. * \retval #PSA_ERROR_STORAGE_FAILURE
  2883. * \retval #PSA_ERROR_DATA_CORRUPT
  2884. * \retval #PSA_ERROR_DATA_INVALID
  2885. * \retval #PSA_ERROR_BAD_STATE
  2886. * The library has not been previously initialized by psa_crypto_init().
  2887. * It is implementation-dependent whether a failure to initialize
  2888. * results in this error code.
  2889. */
  2890. psa_status_t psa_verify_message( mbedtls_svc_key_id_t key,
  2891. psa_algorithm_t alg,
  2892. const uint8_t * input,
  2893. size_t input_length,
  2894. const uint8_t * signature,
  2895. size_t signature_length );
  2896. /**
  2897. * \brief Sign a hash or short message with a private key.
  2898. *
  2899. * Note that to perform a hash-and-sign signature algorithm, you must
  2900. * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
  2901. * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
  2902. * Then pass the resulting hash as the \p hash
  2903. * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
  2904. * to determine the hash algorithm to use.
  2905. *
  2906. * \param key Identifier of the key to use for the operation.
  2907. * It must be an asymmetric key pair. The key must
  2908. * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
  2909. * \param alg A signature algorithm that is compatible with
  2910. * the type of \p key.
  2911. * \param[in] hash The hash or message to sign.
  2912. * \param hash_length Size of the \p hash buffer in bytes.
  2913. * \param[out] signature Buffer where the signature is to be written.
  2914. * \param signature_size Size of the \p signature buffer in bytes.
  2915. * \param[out] signature_length On success, the number of bytes
  2916. * that make up the returned signature value.
  2917. *
  2918. * \retval #PSA_SUCCESS
  2919. * \retval #PSA_ERROR_INVALID_HANDLE
  2920. * \retval #PSA_ERROR_NOT_PERMITTED
  2921. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  2922. * The size of the \p signature buffer is too small. You can
  2923. * determine a sufficient buffer size by calling
  2924. * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  2925. * where \c key_type and \c key_bits are the type and bit-size
  2926. * respectively of \p key.
  2927. * \retval #PSA_ERROR_NOT_SUPPORTED
  2928. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2929. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  2930. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  2931. * \retval #PSA_ERROR_HARDWARE_FAILURE
  2932. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  2933. * \retval #PSA_ERROR_STORAGE_FAILURE
  2934. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
  2935. * \retval #PSA_ERROR_BAD_STATE
  2936. * The library has not been previously initialized by psa_crypto_init().
  2937. * It is implementation-dependent whether a failure to initialize
  2938. * results in this error code.
  2939. */
  2940. psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
  2941. psa_algorithm_t alg,
  2942. const uint8_t *hash,
  2943. size_t hash_length,
  2944. uint8_t *signature,
  2945. size_t signature_size,
  2946. size_t *signature_length);
  2947. /**
  2948. * \brief Verify the signature of a hash or short message using a public key.
  2949. *
  2950. * Note that to perform a hash-and-sign signature algorithm, you must
  2951. * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
  2952. * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
  2953. * Then pass the resulting hash as the \p hash
  2954. * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
  2955. * to determine the hash algorithm to use.
  2956. *
  2957. * \param key Identifier of the key to use for the operation. It
  2958. * must be a public key or an asymmetric key pair. The
  2959. * key must allow the usage
  2960. * #PSA_KEY_USAGE_VERIFY_HASH.
  2961. * \param alg A signature algorithm that is compatible with
  2962. * the type of \p key.
  2963. * \param[in] hash The hash or message whose signature is to be
  2964. * verified.
  2965. * \param hash_length Size of the \p hash buffer in bytes.
  2966. * \param[in] signature Buffer containing the signature to verify.
  2967. * \param signature_length Size of the \p signature buffer in bytes.
  2968. *
  2969. * \retval #PSA_SUCCESS
  2970. * The signature is valid.
  2971. * \retval #PSA_ERROR_INVALID_HANDLE
  2972. * \retval #PSA_ERROR_NOT_PERMITTED
  2973. * \retval #PSA_ERROR_INVALID_SIGNATURE
  2974. * The calculation was perfomed successfully, but the passed
  2975. * signature is not a valid signature.
  2976. * \retval #PSA_ERROR_NOT_SUPPORTED
  2977. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2978. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  2979. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  2980. * \retval #PSA_ERROR_HARDWARE_FAILURE
  2981. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  2982. * \retval #PSA_ERROR_STORAGE_FAILURE
  2983. * \retval #PSA_ERROR_BAD_STATE
  2984. * The library has not been previously initialized by psa_crypto_init().
  2985. * It is implementation-dependent whether a failure to initialize
  2986. * results in this error code.
  2987. */
  2988. psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
  2989. psa_algorithm_t alg,
  2990. const uint8_t *hash,
  2991. size_t hash_length,
  2992. const uint8_t *signature,
  2993. size_t signature_length);
  2994. /**
  2995. * \brief Encrypt a short message with a public key.
  2996. *
  2997. * \param key Identifer of the key to use for the operation.
  2998. * It must be a public key or an asymmetric key
  2999. * pair. It must allow the usage
  3000. * #PSA_KEY_USAGE_ENCRYPT.
  3001. * \param alg An asymmetric encryption algorithm that is
  3002. * compatible with the type of \p key.
  3003. * \param[in] input The message to encrypt.
  3004. * \param input_length Size of the \p input buffer in bytes.
  3005. * \param[in] salt A salt or label, if supported by the
  3006. * encryption algorithm.
  3007. * If the algorithm does not support a
  3008. * salt, pass \c NULL.
  3009. * If the algorithm supports an optional
  3010. * salt and you do not want to pass a salt,
  3011. * pass \c NULL.
  3012. *
  3013. * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
  3014. * supported.
  3015. * \param salt_length Size of the \p salt buffer in bytes.
  3016. * If \p salt is \c NULL, pass 0.
  3017. * \param[out] output Buffer where the encrypted message is to
  3018. * be written.
  3019. * \param output_size Size of the \p output buffer in bytes.
  3020. * \param[out] output_length On success, the number of bytes
  3021. * that make up the returned output.
  3022. *
  3023. * \retval #PSA_SUCCESS
  3024. * \retval #PSA_ERROR_INVALID_HANDLE
  3025. * \retval #PSA_ERROR_NOT_PERMITTED
  3026. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  3027. * The size of the \p output buffer is too small. You can
  3028. * determine a sufficient buffer size by calling
  3029. * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  3030. * where \c key_type and \c key_bits are the type and bit-size
  3031. * respectively of \p key.
  3032. * \retval #PSA_ERROR_NOT_SUPPORTED
  3033. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3034. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  3035. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  3036. * \retval #PSA_ERROR_HARDWARE_FAILURE
  3037. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  3038. * \retval #PSA_ERROR_STORAGE_FAILURE
  3039. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
  3040. * \retval #PSA_ERROR_BAD_STATE
  3041. * The library has not been previously initialized by psa_crypto_init().
  3042. * It is implementation-dependent whether a failure to initialize
  3043. * results in this error code.
  3044. */
  3045. psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
  3046. psa_algorithm_t alg,
  3047. const uint8_t *input,
  3048. size_t input_length,
  3049. const uint8_t *salt,
  3050. size_t salt_length,
  3051. uint8_t *output,
  3052. size_t output_size,
  3053. size_t *output_length);
  3054. /**
  3055. * \brief Decrypt a short message with a private key.
  3056. *
  3057. * \param key Identifier of the key to use for the operation.
  3058. * It must be an asymmetric key pair. It must
  3059. * allow the usage #PSA_KEY_USAGE_DECRYPT.
  3060. * \param alg An asymmetric encryption algorithm that is
  3061. * compatible with the type of \p key.
  3062. * \param[in] input The message to decrypt.
  3063. * \param input_length Size of the \p input buffer in bytes.
  3064. * \param[in] salt A salt or label, if supported by the
  3065. * encryption algorithm.
  3066. * If the algorithm does not support a
  3067. * salt, pass \c NULL.
  3068. * If the algorithm supports an optional
  3069. * salt and you do not want to pass a salt,
  3070. * pass \c NULL.
  3071. *
  3072. * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
  3073. * supported.
  3074. * \param salt_length Size of the \p salt buffer in bytes.
  3075. * If \p salt is \c NULL, pass 0.
  3076. * \param[out] output Buffer where the decrypted message is to
  3077. * be written.
  3078. * \param output_size Size of the \c output buffer in bytes.
  3079. * \param[out] output_length On success, the number of bytes
  3080. * that make up the returned output.
  3081. *
  3082. * \retval #PSA_SUCCESS
  3083. * \retval #PSA_ERROR_INVALID_HANDLE
  3084. * \retval #PSA_ERROR_NOT_PERMITTED
  3085. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  3086. * The size of the \p output buffer is too small. You can
  3087. * determine a sufficient buffer size by calling
  3088. * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  3089. * where \c key_type and \c key_bits are the type and bit-size
  3090. * respectively of \p key.
  3091. * \retval #PSA_ERROR_NOT_SUPPORTED
  3092. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3093. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  3094. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  3095. * \retval #PSA_ERROR_HARDWARE_FAILURE
  3096. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  3097. * \retval #PSA_ERROR_STORAGE_FAILURE
  3098. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
  3099. * \retval #PSA_ERROR_INVALID_PADDING
  3100. * \retval #PSA_ERROR_BAD_STATE
  3101. * The library has not been previously initialized by psa_crypto_init().
  3102. * It is implementation-dependent whether a failure to initialize
  3103. * results in this error code.
  3104. */
  3105. psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
  3106. psa_algorithm_t alg,
  3107. const uint8_t *input,
  3108. size_t input_length,
  3109. const uint8_t *salt,
  3110. size_t salt_length,
  3111. uint8_t *output,
  3112. size_t output_size,
  3113. size_t *output_length);
  3114. /**@}*/
  3115. /** \defgroup key_derivation Key derivation and pseudorandom generation
  3116. * @{
  3117. */
  3118. /** The type of the state data structure for key derivation operations.
  3119. *
  3120. * Before calling any function on a key derivation operation object, the
  3121. * application must initialize it by any of the following means:
  3122. * - Set the structure to all-bits-zero, for example:
  3123. * \code
  3124. * psa_key_derivation_operation_t operation;
  3125. * memset(&operation, 0, sizeof(operation));
  3126. * \endcode
  3127. * - Initialize the structure to logical zero values, for example:
  3128. * \code
  3129. * psa_key_derivation_operation_t operation = {0};
  3130. * \endcode
  3131. * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
  3132. * for example:
  3133. * \code
  3134. * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  3135. * \endcode
  3136. * - Assign the result of the function psa_key_derivation_operation_init()
  3137. * to the structure, for example:
  3138. * \code
  3139. * psa_key_derivation_operation_t operation;
  3140. * operation = psa_key_derivation_operation_init();
  3141. * \endcode
  3142. *
  3143. * This is an implementation-defined \c struct. Applications should not
  3144. * make any assumptions about the content of this structure except
  3145. * as directed by the documentation of a specific implementation.
  3146. */
  3147. typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
  3148. /** \def PSA_KEY_DERIVATION_OPERATION_INIT
  3149. *
  3150. * This macro returns a suitable initializer for a key derivation operation
  3151. * object of type #psa_key_derivation_operation_t.
  3152. */
  3153. #ifdef __DOXYGEN_ONLY__
  3154. /* This is an example definition for documentation purposes.
  3155. * Implementations should define a suitable value in `crypto_struct.h`.
  3156. */
  3157. #define PSA_KEY_DERIVATION_OPERATION_INIT {0}
  3158. #endif
  3159. /** Return an initial value for a key derivation operation object.
  3160. */
  3161. static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
  3162. /** Set up a key derivation operation.
  3163. *
  3164. * A key derivation algorithm takes some inputs and uses them to generate
  3165. * a byte stream in a deterministic way.
  3166. * This byte stream can be used to produce keys and other
  3167. * cryptographic material.
  3168. *
  3169. * To derive a key:
  3170. * -# Start with an initialized object of type #psa_key_derivation_operation_t.
  3171. * -# Call psa_key_derivation_setup() to select the algorithm.
  3172. * -# Provide the inputs for the key derivation by calling
  3173. * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
  3174. * as appropriate. Which inputs are needed, in what order, and whether
  3175. * they may be keys and if so of what type depends on the algorithm.
  3176. * -# Optionally set the operation's maximum capacity with
  3177. * psa_key_derivation_set_capacity(). You may do this before, in the middle
  3178. * of or after providing inputs. For some algorithms, this step is mandatory
  3179. * because the output depends on the maximum capacity.
  3180. * -# To derive a key, call psa_key_derivation_output_key().
  3181. * To derive a byte string for a different purpose, call
  3182. * psa_key_derivation_output_bytes().
  3183. * Successive calls to these functions use successive output bytes
  3184. * calculated by the key derivation algorithm.
  3185. * -# Clean up the key derivation operation object with
  3186. * psa_key_derivation_abort().
  3187. *
  3188. * If this function returns an error, the key derivation operation object is
  3189. * not changed.
  3190. *
  3191. * If an error occurs at any step after a call to psa_key_derivation_setup(),
  3192. * the operation will need to be reset by a call to psa_key_derivation_abort().
  3193. *
  3194. * Implementations must reject an attempt to derive a key of size 0.
  3195. *
  3196. * \param[in,out] operation The key derivation operation object
  3197. * to set up. It must
  3198. * have been initialized but not set up yet.
  3199. * \param alg The key derivation algorithm to compute
  3200. * (\c PSA_ALG_XXX value such that
  3201. * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
  3202. *
  3203. * \retval #PSA_SUCCESS
  3204. * Success.
  3205. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3206. * \c alg is not a key derivation algorithm.
  3207. * \retval #PSA_ERROR_NOT_SUPPORTED
  3208. * \c alg is not supported or is not a key derivation algorithm.
  3209. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  3210. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  3211. * \retval #PSA_ERROR_HARDWARE_FAILURE
  3212. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  3213. * \retval #PSA_ERROR_STORAGE_FAILURE
  3214. * \retval #PSA_ERROR_BAD_STATE
  3215. * The operation state is not valid (it must be inactive).
  3216. * \retval #PSA_ERROR_BAD_STATE
  3217. * The library has not been previously initialized by psa_crypto_init().
  3218. * It is implementation-dependent whether a failure to initialize
  3219. * results in this error code.
  3220. */
  3221. psa_status_t psa_key_derivation_setup(
  3222. psa_key_derivation_operation_t *operation,
  3223. psa_algorithm_t alg);
  3224. /** Retrieve the current capacity of a key derivation operation.
  3225. *
  3226. * The capacity of a key derivation is the maximum number of bytes that it can
  3227. * return. When you get *N* bytes of output from a key derivation operation,
  3228. * this reduces its capacity by *N*.
  3229. *
  3230. * \param[in] operation The operation to query.
  3231. * \param[out] capacity On success, the capacity of the operation.
  3232. *
  3233. * \retval #PSA_SUCCESS
  3234. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  3235. * \retval #PSA_ERROR_BAD_STATE
  3236. * The operation state is not valid (it must be active).
  3237. * \retval #PSA_ERROR_HARDWARE_FAILURE
  3238. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  3239. * \retval #PSA_ERROR_BAD_STATE
  3240. * The library has not been previously initialized by psa_crypto_init().
  3241. * It is implementation-dependent whether a failure to initialize
  3242. * results in this error code.
  3243. */
  3244. psa_status_t psa_key_derivation_get_capacity(
  3245. const psa_key_derivation_operation_t *operation,
  3246. size_t *capacity);
  3247. /** Set the maximum capacity of a key derivation operation.
  3248. *
  3249. * The capacity of a key derivation operation is the maximum number of bytes
  3250. * that the key derivation operation can return from this point onwards.
  3251. *
  3252. * \param[in,out] operation The key derivation operation object to modify.
  3253. * \param capacity The new capacity of the operation.
  3254. * It must be less or equal to the operation's
  3255. * current capacity.
  3256. *
  3257. * \retval #PSA_SUCCESS
  3258. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3259. * \p capacity is larger than the operation's current capacity.
  3260. * In this case, the operation object remains valid and its capacity
  3261. * remains unchanged.
  3262. * \retval #PSA_ERROR_BAD_STATE
  3263. * The operation state is not valid (it must be active).
  3264. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  3265. * \retval #PSA_ERROR_HARDWARE_FAILURE
  3266. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  3267. * \retval #PSA_ERROR_BAD_STATE
  3268. * The library has not been previously initialized by psa_crypto_init().
  3269. * It is implementation-dependent whether a failure to initialize
  3270. * results in this error code.
  3271. */
  3272. psa_status_t psa_key_derivation_set_capacity(
  3273. psa_key_derivation_operation_t *operation,
  3274. size_t capacity);
  3275. /** Use the maximum possible capacity for a key derivation operation.
  3276. *
  3277. * Use this value as the capacity argument when setting up a key derivation
  3278. * to indicate that the operation should have the maximum possible capacity.
  3279. * The value of the maximum possible capacity depends on the key derivation
  3280. * algorithm.
  3281. */
  3282. #define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
  3283. /** Provide an input for key derivation or key agreement.
  3284. *
  3285. * Which inputs are required and in what order depends on the algorithm.
  3286. * Refer to the documentation of each key derivation or key agreement
  3287. * algorithm for information.
  3288. *
  3289. * This function passes direct inputs, which is usually correct for
  3290. * non-secret inputs. To pass a secret input, which should be in a key
  3291. * object, call psa_key_derivation_input_key() instead of this function.
  3292. * Refer to the documentation of individual step types
  3293. * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
  3294. * for more information.
  3295. *
  3296. * If this function returns an error status, the operation enters an error
  3297. * state and must be aborted by calling psa_key_derivation_abort().
  3298. *
  3299. * \param[in,out] operation The key derivation operation object to use.
  3300. * It must have been set up with
  3301. * psa_key_derivation_setup() and must not
  3302. * have produced any output yet.
  3303. * \param step Which step the input data is for.
  3304. * \param[in] data Input data to use.
  3305. * \param data_length Size of the \p data buffer in bytes.
  3306. *
  3307. * \retval #PSA_SUCCESS
  3308. * Success.
  3309. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3310. * \c step is not compatible with the operation's algorithm.
  3311. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3312. * \c step does not allow direct inputs.
  3313. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  3314. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  3315. * \retval #PSA_ERROR_HARDWARE_FAILURE
  3316. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  3317. * \retval #PSA_ERROR_STORAGE_FAILURE
  3318. * \retval #PSA_ERROR_BAD_STATE
  3319. * The operation state is not valid for this input \p step.
  3320. * \retval #PSA_ERROR_BAD_STATE
  3321. * The library has not been previously initialized by psa_crypto_init().
  3322. * It is implementation-dependent whether a failure to initialize
  3323. * results in this error code.
  3324. */
  3325. psa_status_t psa_key_derivation_input_bytes(
  3326. psa_key_derivation_operation_t *operation,
  3327. psa_key_derivation_step_t step,
  3328. const uint8_t *data,
  3329. size_t data_length);
  3330. /** Provide an input for key derivation in the form of a key.
  3331. *
  3332. * Which inputs are required and in what order depends on the algorithm.
  3333. * Refer to the documentation of each key derivation or key agreement
  3334. * algorithm for information.
  3335. *
  3336. * This function obtains input from a key object, which is usually correct for
  3337. * secret inputs or for non-secret personalization strings kept in the key
  3338. * store. To pass a non-secret parameter which is not in the key store,
  3339. * call psa_key_derivation_input_bytes() instead of this function.
  3340. * Refer to the documentation of individual step types
  3341. * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
  3342. * for more information.
  3343. *
  3344. * If this function returns an error status, the operation enters an error
  3345. * state and must be aborted by calling psa_key_derivation_abort().
  3346. *
  3347. * \param[in,out] operation The key derivation operation object to use.
  3348. * It must have been set up with
  3349. * psa_key_derivation_setup() and must not
  3350. * have produced any output yet.
  3351. * \param step Which step the input data is for.
  3352. * \param key Identifier of the key. It must have an
  3353. * appropriate type for step and must allow the
  3354. * usage #PSA_KEY_USAGE_DERIVE.
  3355. *
  3356. * \retval #PSA_SUCCESS
  3357. * Success.
  3358. * \retval #PSA_ERROR_INVALID_HANDLE
  3359. * \retval #PSA_ERROR_NOT_PERMITTED
  3360. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3361. * \c step is not compatible with the operation's algorithm.
  3362. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3363. * \c step does not allow key inputs of the given type
  3364. * or does not allow key inputs at all.
  3365. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  3366. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  3367. * \retval #PSA_ERROR_HARDWARE_FAILURE
  3368. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  3369. * \retval #PSA_ERROR_STORAGE_FAILURE
  3370. * \retval #PSA_ERROR_BAD_STATE
  3371. * The operation state is not valid for this input \p step.
  3372. * \retval #PSA_ERROR_BAD_STATE
  3373. * The library has not been previously initialized by psa_crypto_init().
  3374. * It is implementation-dependent whether a failure to initialize
  3375. * results in this error code.
  3376. */
  3377. psa_status_t psa_key_derivation_input_key(
  3378. psa_key_derivation_operation_t *operation,
  3379. psa_key_derivation_step_t step,
  3380. mbedtls_svc_key_id_t key);
  3381. /** Perform a key agreement and use the shared secret as input to a key
  3382. * derivation.
  3383. *
  3384. * A key agreement algorithm takes two inputs: a private key \p private_key
  3385. * a public key \p peer_key.
  3386. * The result of this function is passed as input to a key derivation.
  3387. * The output of this key derivation can be extracted by reading from the
  3388. * resulting operation to produce keys and other cryptographic material.
  3389. *
  3390. * If this function returns an error status, the operation enters an error
  3391. * state and must be aborted by calling psa_key_derivation_abort().
  3392. *
  3393. * \param[in,out] operation The key derivation operation object to use.
  3394. * It must have been set up with
  3395. * psa_key_derivation_setup() with a
  3396. * key agreement and derivation algorithm
  3397. * \c alg (\c PSA_ALG_XXX value such that
  3398. * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
  3399. * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
  3400. * is false).
  3401. * The operation must be ready for an
  3402. * input of the type given by \p step.
  3403. * \param step Which step the input data is for.
  3404. * \param private_key Identifier of the private key to use. It must
  3405. * allow the usage #PSA_KEY_USAGE_DERIVE.
  3406. * \param[in] peer_key Public key of the peer. The peer key must be in the
  3407. * same format that psa_import_key() accepts for the
  3408. * public key type corresponding to the type of
  3409. * private_key. That is, this function performs the
  3410. * equivalent of
  3411. * #psa_import_key(...,
  3412. * `peer_key`, `peer_key_length`) where
  3413. * with key attributes indicating the public key
  3414. * type corresponding to the type of `private_key`.
  3415. * For example, for EC keys, this means that peer_key
  3416. * is interpreted as a point on the curve that the
  3417. * private key is on. The standard formats for public
  3418. * keys are documented in the documentation of
  3419. * psa_export_public_key().
  3420. * \param peer_key_length Size of \p peer_key in bytes.
  3421. *
  3422. * \retval #PSA_SUCCESS
  3423. * Success.
  3424. * \retval #PSA_ERROR_BAD_STATE
  3425. * The operation state is not valid for this key agreement \p step.
  3426. * \retval #PSA_ERROR_INVALID_HANDLE
  3427. * \retval #PSA_ERROR_NOT_PERMITTED
  3428. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3429. * \c private_key is not compatible with \c alg,
  3430. * or \p peer_key is not valid for \c alg or not compatible with
  3431. * \c private_key.
  3432. * \retval #PSA_ERROR_NOT_SUPPORTED
  3433. * \c alg is not supported or is not a key derivation algorithm.
  3434. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3435. * \c step does not allow an input resulting from a key agreement.
  3436. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  3437. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  3438. * \retval #PSA_ERROR_HARDWARE_FAILURE
  3439. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  3440. * \retval #PSA_ERROR_STORAGE_FAILURE
  3441. * \retval #PSA_ERROR_BAD_STATE
  3442. * The library has not been previously initialized by psa_crypto_init().
  3443. * It is implementation-dependent whether a failure to initialize
  3444. * results in this error code.
  3445. */
  3446. psa_status_t psa_key_derivation_key_agreement(
  3447. psa_key_derivation_operation_t *operation,
  3448. psa_key_derivation_step_t step,
  3449. mbedtls_svc_key_id_t private_key,
  3450. const uint8_t *peer_key,
  3451. size_t peer_key_length);
  3452. /** Read some data from a key derivation operation.
  3453. *
  3454. * This function calculates output bytes from a key derivation algorithm and
  3455. * return those bytes.
  3456. * If you view the key derivation's output as a stream of bytes, this
  3457. * function destructively reads the requested number of bytes from the
  3458. * stream.
  3459. * The operation's capacity decreases by the number of bytes read.
  3460. *
  3461. * If this function returns an error status other than
  3462. * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
  3463. * state and must be aborted by calling psa_key_derivation_abort().
  3464. *
  3465. * \param[in,out] operation The key derivation operation object to read from.
  3466. * \param[out] output Buffer where the output will be written.
  3467. * \param output_length Number of bytes to output.
  3468. *
  3469. * \retval #PSA_SUCCESS
  3470. * \retval #PSA_ERROR_INSUFFICIENT_DATA
  3471. * The operation's capacity was less than
  3472. * \p output_length bytes. Note that in this case,
  3473. * no output is written to the output buffer.
  3474. * The operation's capacity is set to 0, thus
  3475. * subsequent calls to this function will not
  3476. * succeed, even with a smaller output buffer.
  3477. * \retval #PSA_ERROR_BAD_STATE
  3478. * The operation state is not valid (it must be active and completed
  3479. * all required input steps).
  3480. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  3481. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  3482. * \retval #PSA_ERROR_HARDWARE_FAILURE
  3483. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  3484. * \retval #PSA_ERROR_STORAGE_FAILURE
  3485. * \retval #PSA_ERROR_BAD_STATE
  3486. * The library has not been previously initialized by psa_crypto_init().
  3487. * It is implementation-dependent whether a failure to initialize
  3488. * results in this error code.
  3489. */
  3490. psa_status_t psa_key_derivation_output_bytes(
  3491. psa_key_derivation_operation_t *operation,
  3492. uint8_t *output,
  3493. size_t output_length);
  3494. /** Derive a key from an ongoing key derivation operation.
  3495. *
  3496. * This function calculates output bytes from a key derivation algorithm
  3497. * and uses those bytes to generate a key deterministically.
  3498. * The key's location, usage policy, type and size are taken from
  3499. * \p attributes.
  3500. *
  3501. * If you view the key derivation's output as a stream of bytes, this
  3502. * function destructively reads as many bytes as required from the
  3503. * stream.
  3504. * The operation's capacity decreases by the number of bytes read.
  3505. *
  3506. * If this function returns an error status other than
  3507. * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
  3508. * state and must be aborted by calling psa_key_derivation_abort().
  3509. *
  3510. * How much output is produced and consumed from the operation, and how
  3511. * the key is derived, depends on the key type and on the key size
  3512. * (denoted \c bits below):
  3513. *
  3514. * - For key types for which the key is an arbitrary sequence of bytes
  3515. * of a given size, this function is functionally equivalent to
  3516. * calling #psa_key_derivation_output_bytes
  3517. * and passing the resulting output to #psa_import_key.
  3518. * However, this function has a security benefit:
  3519. * if the implementation provides an isolation boundary then
  3520. * the key material is not exposed outside the isolation boundary.
  3521. * As a consequence, for these key types, this function always consumes
  3522. * exactly (\c bits / 8) bytes from the operation.
  3523. * The following key types defined in this specification follow this scheme:
  3524. *
  3525. * - #PSA_KEY_TYPE_AES;
  3526. * - #PSA_KEY_TYPE_ARC4;
  3527. * - #PSA_KEY_TYPE_CAMELLIA;
  3528. * - #PSA_KEY_TYPE_DERIVE;
  3529. * - #PSA_KEY_TYPE_HMAC.
  3530. *
  3531. * - For ECC keys on a Montgomery elliptic curve
  3532. * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
  3533. * Montgomery curve), this function always draws a byte string whose
  3534. * length is determined by the curve, and sets the mandatory bits
  3535. * accordingly. That is:
  3536. *
  3537. * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
  3538. * string and process it as specified in RFC 7748 &sect;5.
  3539. * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
  3540. * string and process it as specified in RFC 7748 &sect;5.
  3541. *
  3542. * - For key types for which the key is represented by a single sequence of
  3543. * \c bits bits with constraints as to which bit sequences are acceptable,
  3544. * this function draws a byte string of length (\c bits / 8) bytes rounded
  3545. * up to the nearest whole number of bytes. If the resulting byte string
  3546. * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
  3547. * This process is repeated until an acceptable byte string is drawn.
  3548. * The byte string drawn from the operation is interpreted as specified
  3549. * for the output produced by psa_export_key().
  3550. * The following key types defined in this specification follow this scheme:
  3551. *
  3552. * - #PSA_KEY_TYPE_DES.
  3553. * Force-set the parity bits, but discard forbidden weak keys.
  3554. * For 2-key and 3-key triple-DES, the three keys are generated
  3555. * successively (for example, for 3-key triple-DES,
  3556. * if the first 8 bytes specify a weak key and the next 8 bytes do not,
  3557. * discard the first 8 bytes, use the next 8 bytes as the first key,
  3558. * and continue reading output from the operation to derive the other
  3559. * two keys).
  3560. * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
  3561. * where \c group designates any Diffie-Hellman group) and
  3562. * ECC keys on a Weierstrass elliptic curve
  3563. * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
  3564. * Weierstrass curve).
  3565. * For these key types, interpret the byte string as integer
  3566. * in big-endian order. Discard it if it is not in the range
  3567. * [0, *N* - 2] where *N* is the boundary of the private key domain
  3568. * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
  3569. * or the order of the curve's base point for ECC).
  3570. * Add 1 to the resulting integer and use this as the private key *x*.
  3571. * This method allows compliance to NIST standards, specifically
  3572. * the methods titled "key-pair generation by testing candidates"
  3573. * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
  3574. * in FIPS 186-4 &sect;B.1.2 for DSA, and
  3575. * in NIST SP 800-56A &sect;5.6.1.2.2 or
  3576. * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
  3577. *
  3578. * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
  3579. * the way in which the operation output is consumed is
  3580. * implementation-defined.
  3581. *
  3582. * In all cases, the data that is read is discarded from the operation.
  3583. * The operation's capacity is decreased by the number of bytes read.
  3584. *
  3585. * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
  3586. * the input to that step must be provided with psa_key_derivation_input_key().
  3587. * Future versions of this specification may include additional restrictions
  3588. * on the derived key based on the attributes and strength of the secret key.
  3589. *
  3590. * \param[in] attributes The attributes for the new key.
  3591. * \param[in,out] operation The key derivation operation object to read from.
  3592. * \param[out] key On success, an identifier for the newly created
  3593. * key. For persistent keys, this is the key
  3594. * identifier defined in \p attributes.
  3595. * \c 0 on failure.
  3596. *
  3597. * \retval #PSA_SUCCESS
  3598. * Success.
  3599. * If the key is persistent, the key material and the key's metadata
  3600. * have been saved to persistent storage.
  3601. * \retval #PSA_ERROR_ALREADY_EXISTS
  3602. * This is an attempt to create a persistent key, and there is
  3603. * already a persistent key with the given identifier.
  3604. * \retval #PSA_ERROR_INSUFFICIENT_DATA
  3605. * There was not enough data to create the desired key.
  3606. * Note that in this case, no output is written to the output buffer.
  3607. * The operation's capacity is set to 0, thus subsequent calls to
  3608. * this function will not succeed, even with a smaller output buffer.
  3609. * \retval #PSA_ERROR_NOT_SUPPORTED
  3610. * The key type or key size is not supported, either by the
  3611. * implementation in general or in this particular location.
  3612. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3613. * The provided key attributes are not valid for the operation.
  3614. * \retval #PSA_ERROR_NOT_PERMITTED
  3615. * The #PSA_KEY_DERIVATION_INPUT_SECRET input was not provided through
  3616. * a key.
  3617. * \retval #PSA_ERROR_BAD_STATE
  3618. * The operation state is not valid (it must be active and completed
  3619. * all required input steps).
  3620. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  3621. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
  3622. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  3623. * \retval #PSA_ERROR_HARDWARE_FAILURE
  3624. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  3625. * \retval #PSA_ERROR_DATA_INVALID
  3626. * \retval #PSA_ERROR_DATA_CORRUPT
  3627. * \retval #PSA_ERROR_STORAGE_FAILURE
  3628. * \retval #PSA_ERROR_BAD_STATE
  3629. * The library has not been previously initialized by psa_crypto_init().
  3630. * It is implementation-dependent whether a failure to initialize
  3631. * results in this error code.
  3632. */
  3633. psa_status_t psa_key_derivation_output_key(
  3634. const psa_key_attributes_t *attributes,
  3635. psa_key_derivation_operation_t *operation,
  3636. mbedtls_svc_key_id_t *key);
  3637. /** Abort a key derivation operation.
  3638. *
  3639. * Aborting an operation frees all associated resources except for the \c
  3640. * operation structure itself. Once aborted, the operation object can be reused
  3641. * for another operation by calling psa_key_derivation_setup() again.
  3642. *
  3643. * This function may be called at any time after the operation
  3644. * object has been initialized as described in #psa_key_derivation_operation_t.
  3645. *
  3646. * In particular, it is valid to call psa_key_derivation_abort() twice, or to
  3647. * call psa_key_derivation_abort() on an operation that has not been set up.
  3648. *
  3649. * \param[in,out] operation The operation to abort.
  3650. *
  3651. * \retval #PSA_SUCCESS
  3652. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  3653. * \retval #PSA_ERROR_HARDWARE_FAILURE
  3654. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  3655. * \retval #PSA_ERROR_BAD_STATE
  3656. * The library has not been previously initialized by psa_crypto_init().
  3657. * It is implementation-dependent whether a failure to initialize
  3658. * results in this error code.
  3659. */
  3660. psa_status_t psa_key_derivation_abort(
  3661. psa_key_derivation_operation_t *operation);
  3662. /** Perform a key agreement and return the raw shared secret.
  3663. *
  3664. * \warning The raw result of a key agreement algorithm such as finite-field
  3665. * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
  3666. * not be used directly as key material. It should instead be passed as
  3667. * input to a key derivation algorithm. To chain a key agreement with
  3668. * a key derivation, use psa_key_derivation_key_agreement() and other
  3669. * functions from the key derivation interface.
  3670. *
  3671. * \param alg The key agreement algorithm to compute
  3672. * (\c PSA_ALG_XXX value such that
  3673. * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
  3674. * is true).
  3675. * \param private_key Identifier of the private key to use. It must
  3676. * allow the usage #PSA_KEY_USAGE_DERIVE.
  3677. * \param[in] peer_key Public key of the peer. It must be
  3678. * in the same format that psa_import_key()
  3679. * accepts. The standard formats for public
  3680. * keys are documented in the documentation
  3681. * of psa_export_public_key().
  3682. * \param peer_key_length Size of \p peer_key in bytes.
  3683. * \param[out] output Buffer where the decrypted message is to
  3684. * be written.
  3685. * \param output_size Size of the \c output buffer in bytes.
  3686. * \param[out] output_length On success, the number of bytes
  3687. * that make up the returned output.
  3688. *
  3689. * \retval #PSA_SUCCESS
  3690. * Success.
  3691. * \retval #PSA_ERROR_INVALID_HANDLE
  3692. * \retval #PSA_ERROR_NOT_PERMITTED
  3693. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3694. * \p alg is not a key agreement algorithm
  3695. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3696. * \p private_key is not compatible with \p alg,
  3697. * or \p peer_key is not valid for \p alg or not compatible with
  3698. * \p private_key.
  3699. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  3700. * \p output_size is too small
  3701. * \retval #PSA_ERROR_NOT_SUPPORTED
  3702. * \p alg is not a supported key agreement algorithm.
  3703. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  3704. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  3705. * \retval #PSA_ERROR_HARDWARE_FAILURE
  3706. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  3707. * \retval #PSA_ERROR_STORAGE_FAILURE
  3708. * \retval #PSA_ERROR_BAD_STATE
  3709. * The library has not been previously initialized by psa_crypto_init().
  3710. * It is implementation-dependent whether a failure to initialize
  3711. * results in this error code.
  3712. */
  3713. psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
  3714. mbedtls_svc_key_id_t private_key,
  3715. const uint8_t *peer_key,
  3716. size_t peer_key_length,
  3717. uint8_t *output,
  3718. size_t output_size,
  3719. size_t *output_length);
  3720. /**@}*/
  3721. /** \defgroup random Random generation
  3722. * @{
  3723. */
  3724. /**
  3725. * \brief Generate random bytes.
  3726. *
  3727. * \warning This function **can** fail! Callers MUST check the return status
  3728. * and MUST NOT use the content of the output buffer if the return
  3729. * status is not #PSA_SUCCESS.
  3730. *
  3731. * \note To generate a key, use psa_generate_key() instead.
  3732. *
  3733. * \param[out] output Output buffer for the generated data.
  3734. * \param output_size Number of bytes to generate and output.
  3735. *
  3736. * \retval #PSA_SUCCESS
  3737. * \retval #PSA_ERROR_NOT_SUPPORTED
  3738. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
  3739. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  3740. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  3741. * \retval #PSA_ERROR_HARDWARE_FAILURE
  3742. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  3743. * \retval #PSA_ERROR_BAD_STATE
  3744. * The library has not been previously initialized by psa_crypto_init().
  3745. * It is implementation-dependent whether a failure to initialize
  3746. * results in this error code.
  3747. */
  3748. psa_status_t psa_generate_random(uint8_t *output,
  3749. size_t output_size);
  3750. /**
  3751. * \brief Generate a key or key pair.
  3752. *
  3753. * The key is generated randomly.
  3754. * Its location, usage policy, type and size are taken from \p attributes.
  3755. *
  3756. * Implementations must reject an attempt to generate a key of size 0.
  3757. *
  3758. * The following type-specific considerations apply:
  3759. * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
  3760. * the public exponent is 65537.
  3761. * The modulus is a product of two probabilistic primes
  3762. * between 2^{n-1} and 2^n where n is the bit size specified in the
  3763. * attributes.
  3764. *
  3765. * \param[in] attributes The attributes for the new key.
  3766. * \param[out] key On success, an identifier for the newly created
  3767. * key. For persistent keys, this is the key
  3768. * identifier defined in \p attributes.
  3769. * \c 0 on failure.
  3770. *
  3771. * \retval #PSA_SUCCESS
  3772. * Success.
  3773. * If the key is persistent, the key material and the key's metadata
  3774. * have been saved to persistent storage.
  3775. * \retval #PSA_ERROR_ALREADY_EXISTS
  3776. * This is an attempt to create a persistent key, and there is
  3777. * already a persistent key with the given identifier.
  3778. * \retval #PSA_ERROR_NOT_SUPPORTED
  3779. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3780. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  3781. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
  3782. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  3783. * \retval #PSA_ERROR_HARDWARE_FAILURE
  3784. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  3785. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
  3786. * \retval #PSA_ERROR_DATA_INVALID
  3787. * \retval #PSA_ERROR_DATA_CORRUPT
  3788. * \retval #PSA_ERROR_STORAGE_FAILURE
  3789. * \retval #PSA_ERROR_BAD_STATE
  3790. * The library has not been previously initialized by psa_crypto_init().
  3791. * It is implementation-dependent whether a failure to initialize
  3792. * results in this error code.
  3793. */
  3794. psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
  3795. mbedtls_svc_key_id_t *key);
  3796. /**@}*/
  3797. #ifdef __cplusplus
  3798. }
  3799. #endif
  3800. /* The file "crypto_sizes.h" contains definitions for size calculation
  3801. * macros whose definitions are implementation-specific. */
  3802. #include "crypto_sizes.h"
  3803. /* The file "crypto_struct.h" contains definitions for
  3804. * implementation-specific structs that are declared above. */
  3805. #include "crypto_struct.h"
  3806. /* The file "crypto_extra.h" contains vendor-specific definitions. This
  3807. * can include vendor-defined algorithms, extra functions, etc. */
  3808. #include "crypto_extra.h"
  3809. #endif /* PSA_CRYPTO_H */