crypto.h 239 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977
  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 OR GPL-2.0-or-later
  8. */
  9. #ifndef PSA_CRYPTO_H
  10. #define PSA_CRYPTO_H
  11. #if defined(MBEDTLS_PSA_CRYPTO_PLATFORM_FILE)
  12. #include MBEDTLS_PSA_CRYPTO_PLATFORM_FILE
  13. #else
  14. #include "crypto_platform.h"
  15. #endif
  16. #include <stddef.h>
  17. #ifdef __DOXYGEN_ONLY__
  18. /* This __DOXYGEN_ONLY__ block contains mock definitions for things that
  19. * must be defined in the crypto_platform.h header. These mock definitions
  20. * are present in this file as a convenience to generate pretty-printed
  21. * documentation that includes those definitions. */
  22. /** \defgroup platform Implementation-specific definitions
  23. * @{
  24. */
  25. /**@}*/
  26. #endif /* __DOXYGEN_ONLY__ */
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. /* The file "crypto_types.h" declares types that encode errors,
  31. * algorithms, key types, policies, etc. */
  32. #include "crypto_types.h"
  33. /** \defgroup version API version
  34. * @{
  35. */
  36. /**
  37. * The major version of this implementation of the PSA Crypto API
  38. */
  39. #define PSA_CRYPTO_API_VERSION_MAJOR 1
  40. /**
  41. * The minor version of this implementation of the PSA Crypto API
  42. */
  43. #define PSA_CRYPTO_API_VERSION_MINOR 0
  44. /**@}*/
  45. /* The file "crypto_values.h" declares macros to build and analyze values
  46. * of integral types defined in "crypto_types.h". */
  47. #include "crypto_values.h"
  48. /* The file "crypto_sizes.h" contains definitions for size calculation
  49. * macros whose definitions are implementation-specific. */
  50. #include "crypto_sizes.h"
  51. /* The file "crypto_struct.h" contains definitions for
  52. * implementation-specific structs that are declared above. */
  53. #if defined(MBEDTLS_PSA_CRYPTO_STRUCT_FILE)
  54. #include MBEDTLS_PSA_CRYPTO_STRUCT_FILE
  55. #else
  56. #include "crypto_struct.h"
  57. #endif
  58. /** \defgroup initialization Library initialization
  59. * @{
  60. */
  61. /**
  62. * \brief Library initialization.
  63. *
  64. * Applications must call this function before calling any other
  65. * function in this module.
  66. *
  67. * Applications may call this function more than once. Once a call
  68. * succeeds, subsequent calls are guaranteed to succeed.
  69. *
  70. * If the application calls other functions before calling psa_crypto_init(),
  71. * the behavior is undefined. Implementations are encouraged to either perform
  72. * the operation as if the library had been initialized or to return
  73. * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
  74. * implementations should not return a success status if the lack of
  75. * initialization may have security implications, for example due to improper
  76. * seeding of the random number generator.
  77. *
  78. * \retval #PSA_SUCCESS \emptydescription
  79. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  80. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
  81. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  82. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  83. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  84. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  85. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  86. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  87. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  88. */
  89. psa_status_t psa_crypto_init(void);
  90. /**@}*/
  91. /** \addtogroup attributes
  92. * @{
  93. */
  94. /** \def PSA_KEY_ATTRIBUTES_INIT
  95. *
  96. * This macro returns a suitable initializer for a key attribute structure
  97. * of type #psa_key_attributes_t.
  98. */
  99. /** Return an initial value for a key attributes structure.
  100. */
  101. static psa_key_attributes_t psa_key_attributes_init(void);
  102. /** Declare a key as persistent and set its key identifier.
  103. *
  104. * If the attribute structure currently declares the key as volatile (which
  105. * is the default content of an attribute structure), this function sets
  106. * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
  107. *
  108. * This function does not access storage, it merely stores the given
  109. * value in the structure.
  110. * The persistent key will be written to storage when the attribute
  111. * structure is passed to a key creation function such as
  112. * psa_import_key(), psa_generate_key(), psa_generate_key_custom(),
  113. * psa_key_derivation_output_key(), psa_key_derivation_output_key_custom()
  114. * or psa_copy_key().
  115. *
  116. * This function may be declared as `static` (i.e. without external
  117. * linkage). This function may be provided as a function-like macro,
  118. * but in this case it must evaluate each of its arguments exactly once.
  119. *
  120. * \param[out] attributes The attribute structure to write to.
  121. * \param key The persistent identifier for the key.
  122. * This can be any value in the range from
  123. * #PSA_KEY_ID_USER_MIN to #PSA_KEY_ID_USER_MAX
  124. * inclusive.
  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 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);
  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(), psa_generate_key_custom(),
  159. * psa_key_derivation_output_key(), psa_key_derivation_output_key_custom()
  160. * or psa_copy_key().
  161. *
  162. * This function may be declared as `static` (i.e. without external
  163. * linkage). This function may be provided as a function-like macro,
  164. * but in this case it must evaluate each of its arguments exactly once.
  165. *
  166. * \param[out] attributes The attribute structure to write to.
  167. * \param lifetime The lifetime for the key.
  168. * If this is #PSA_KEY_LIFETIME_VOLATILE, the
  169. * key will be volatile, and the key identifier
  170. * attribute is reset to 0.
  171. */
  172. static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
  173. psa_key_lifetime_t lifetime);
  174. /** Retrieve the key identifier from key attributes.
  175. *
  176. * This function may be declared as `static` (i.e. without external
  177. * linkage). This function may be provided as a function-like macro,
  178. * but in this case it must evaluate its argument exactly once.
  179. *
  180. * \param[in] attributes The key attribute structure to query.
  181. *
  182. * \return The persistent identifier stored in the attribute structure.
  183. * This value is unspecified if the attribute structure declares
  184. * the key as volatile.
  185. */
  186. static mbedtls_svc_key_id_t psa_get_key_id(
  187. const psa_key_attributes_t *attributes);
  188. /** Retrieve the lifetime from key attributes.
  189. *
  190. * This function may be declared as `static` (i.e. without external
  191. * linkage). This function may be provided as a function-like macro,
  192. * but in this case it must evaluate its argument exactly once.
  193. *
  194. * \param[in] attributes The key attribute structure to query.
  195. *
  196. * \return The lifetime value stored in the attribute structure.
  197. */
  198. static psa_key_lifetime_t psa_get_key_lifetime(
  199. const psa_key_attributes_t *attributes);
  200. /** Declare usage flags for a key.
  201. *
  202. * Usage flags are part of a key's usage policy. They encode what
  203. * kind of operations are permitted on the key. For more details,
  204. * refer to the documentation of the type #psa_key_usage_t.
  205. *
  206. * This function overwrites any usage flags
  207. * previously set in \p attributes.
  208. *
  209. * This function may be declared as `static` (i.e. without external
  210. * linkage). This function may be provided as a function-like macro,
  211. * but in this case it must evaluate each of its arguments exactly once.
  212. *
  213. * \param[out] attributes The attribute structure to write to.
  214. * \param usage_flags The usage flags to write.
  215. */
  216. static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
  217. psa_key_usage_t usage_flags);
  218. /** Retrieve the usage flags from key attributes.
  219. *
  220. * This function may be declared as `static` (i.e. without external
  221. * linkage). This function may be provided as a function-like macro,
  222. * but in this case it must evaluate its argument exactly once.
  223. *
  224. * \param[in] attributes The key attribute structure to query.
  225. *
  226. * \return The usage flags stored in the attribute structure.
  227. */
  228. static psa_key_usage_t psa_get_key_usage_flags(
  229. const psa_key_attributes_t *attributes);
  230. /** Declare the permitted algorithm policy for a key.
  231. *
  232. * The permitted algorithm policy of a key encodes which algorithm or
  233. * algorithms are permitted to be used with this key. The following
  234. * algorithm policies are supported:
  235. * - 0 does not allow any cryptographic operation with the key. The key
  236. * may be used for non-cryptographic actions such as exporting (if
  237. * permitted by the usage flags).
  238. * - An algorithm value permits this particular algorithm.
  239. * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
  240. * signature scheme with any hash algorithm.
  241. * - An algorithm built from #PSA_ALG_AT_LEAST_THIS_LENGTH_MAC allows
  242. * any MAC algorithm from the same base class (e.g. CMAC) which
  243. * generates/verifies a MAC length greater than or equal to the length
  244. * encoded in the wildcard algorithm.
  245. * - An algorithm built from #PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG
  246. * allows any AEAD algorithm from the same base class (e.g. CCM) which
  247. * generates/verifies a tag length greater than or equal to the length
  248. * encoded in the wildcard algorithm.
  249. *
  250. * This function overwrites any algorithm policy
  251. * previously set in \p attributes.
  252. *
  253. * This function may be declared as `static` (i.e. without external
  254. * linkage). This function may be provided as a function-like macro,
  255. * but in this case it must evaluate each of its arguments exactly once.
  256. *
  257. * \param[out] attributes The attribute structure to write to.
  258. * \param alg The permitted algorithm policy to write.
  259. */
  260. static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
  261. psa_algorithm_t alg);
  262. /** Retrieve the algorithm policy from key attributes.
  263. *
  264. * This function may be declared as `static` (i.e. without external
  265. * linkage). This function may be provided as a function-like macro,
  266. * but in this case it must evaluate its argument exactly once.
  267. *
  268. * \param[in] attributes The key attribute structure to query.
  269. *
  270. * \return The algorithm stored in the attribute structure.
  271. */
  272. static psa_algorithm_t psa_get_key_algorithm(
  273. const psa_key_attributes_t *attributes);
  274. /** Declare the type of a key.
  275. *
  276. * This function overwrites any key type
  277. * previously set in \p attributes.
  278. *
  279. * This function may be declared as `static` (i.e. without external
  280. * linkage). This function may be provided as a function-like macro,
  281. * but in this case it must evaluate each of its arguments exactly once.
  282. *
  283. * \param[out] attributes The attribute structure to write to.
  284. * \param type The key type to write.
  285. * If this is 0, the key type in \p attributes
  286. * becomes unspecified.
  287. */
  288. static void psa_set_key_type(psa_key_attributes_t *attributes,
  289. psa_key_type_t type);
  290. /** Declare the size of a key.
  291. *
  292. * This function overwrites any key size previously set in \p attributes.
  293. *
  294. * This function may be declared as `static` (i.e. without external
  295. * linkage). This function may be provided as a function-like macro,
  296. * but in this case it must evaluate each of its arguments exactly once.
  297. *
  298. * \param[out] attributes The attribute structure to write to.
  299. * \param bits The key size in bits.
  300. * If this is 0, the key size in \p attributes
  301. * becomes unspecified. Keys of size 0 are
  302. * not supported.
  303. */
  304. static void psa_set_key_bits(psa_key_attributes_t *attributes,
  305. size_t bits);
  306. /** Retrieve the key type from key attributes.
  307. *
  308. * This function may be declared as `static` (i.e. without external
  309. * linkage). This function may be provided as a function-like macro,
  310. * but in this case it must evaluate its argument exactly once.
  311. *
  312. * \param[in] attributes The key attribute structure to query.
  313. *
  314. * \return The key type stored in the attribute structure.
  315. */
  316. static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
  317. /** Retrieve the key size from key attributes.
  318. *
  319. * This function may be declared as `static` (i.e. without external
  320. * linkage). This function may be provided as a function-like macro,
  321. * but in this case it must evaluate its argument exactly once.
  322. *
  323. * \param[in] attributes The key attribute structure to query.
  324. *
  325. * \return The key size stored in the attribute structure, in bits.
  326. */
  327. static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
  328. /** Retrieve the attributes of a key.
  329. *
  330. * This function first resets the attribute structure as with
  331. * psa_reset_key_attributes(). It then copies the attributes of
  332. * the given key into the given attribute structure.
  333. *
  334. * \note This function may allocate memory or other resources.
  335. * Once you have called this function on an attribute structure,
  336. * you must call psa_reset_key_attributes() to free these resources.
  337. *
  338. * \param[in] key Identifier of the key to query.
  339. * \param[in,out] attributes On success, the attributes of the key.
  340. * On failure, equivalent to a
  341. * freshly-initialized structure.
  342. *
  343. * \retval #PSA_SUCCESS \emptydescription
  344. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  345. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  346. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  347. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  348. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  349. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  350. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  351. * \retval #PSA_ERROR_BAD_STATE
  352. * The library has not been previously initialized by psa_crypto_init().
  353. * It is implementation-dependent whether a failure to initialize
  354. * results in this error code.
  355. */
  356. psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
  357. psa_key_attributes_t *attributes);
  358. /** Reset a key attribute structure to a freshly initialized state.
  359. *
  360. * You must initialize the attribute structure as described in the
  361. * documentation of the type #psa_key_attributes_t before calling this
  362. * function. Once the structure has been initialized, you may call this
  363. * function at any time.
  364. *
  365. * This function frees any auxiliary resources that the structure
  366. * may contain.
  367. *
  368. * \param[in,out] attributes The attribute structure to reset.
  369. */
  370. void psa_reset_key_attributes(psa_key_attributes_t *attributes);
  371. /**@}*/
  372. /** \defgroup key_management Key management
  373. * @{
  374. */
  375. /** Remove non-essential copies of key material from memory.
  376. *
  377. * If the key identifier designates a volatile key, this functions does not do
  378. * anything and returns successfully.
  379. *
  380. * If the key identifier designates a persistent key, then this function will
  381. * free all resources associated with the key in volatile memory. The key
  382. * data in persistent storage is not affected and the key can still be used.
  383. *
  384. * \param key Identifier of the key to purge.
  385. *
  386. * \retval #PSA_SUCCESS
  387. * The key material will have been removed from memory if it is not
  388. * currently required.
  389. * \retval #PSA_ERROR_INVALID_ARGUMENT
  390. * \p key is not a valid key identifier.
  391. * \retval #PSA_ERROR_BAD_STATE
  392. * The library has not been previously initialized by psa_crypto_init().
  393. * It is implementation-dependent whether a failure to initialize
  394. * results in this error code.
  395. */
  396. psa_status_t psa_purge_key(mbedtls_svc_key_id_t key);
  397. /** Make a copy of a key.
  398. *
  399. * Copy key material from one location to another.
  400. *
  401. * This function is primarily useful to copy a key from one location
  402. * to another, since it populates a key using the material from
  403. * another key which may have a different lifetime.
  404. *
  405. * This function may be used to share a key with a different party,
  406. * subject to implementation-defined restrictions on key sharing.
  407. *
  408. * The policy on the source key must have the usage flag
  409. * #PSA_KEY_USAGE_COPY set.
  410. * This flag is sufficient to permit the copy if the key has the lifetime
  411. * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
  412. * Some secure elements do not provide a way to copy a key without
  413. * making it extractable from the secure element. If a key is located
  414. * in such a secure element, then the key must have both usage flags
  415. * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
  416. * a copy of the key outside the secure element.
  417. *
  418. * The resulting key may only be used in a way that conforms to
  419. * both the policy of the original key and the policy specified in
  420. * the \p attributes parameter:
  421. * - The usage flags on the resulting key are the bitwise-and of the
  422. * usage flags on the source policy and the usage flags in \p attributes.
  423. * - If both allow the same algorithm or wildcard-based
  424. * algorithm policy, the resulting key has the same algorithm policy.
  425. * - If either of the policies allows an algorithm and the other policy
  426. * allows a wildcard-based algorithm policy that includes this algorithm,
  427. * the resulting key allows the same algorithm.
  428. * - If the policies do not allow any algorithm in common, this function
  429. * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
  430. *
  431. * The effect of this function on implementation-defined attributes is
  432. * implementation-defined.
  433. *
  434. * \param source_key The key to copy. It must allow the usage
  435. * #PSA_KEY_USAGE_COPY. If a private or secret key is
  436. * being copied outside of a secure element it must
  437. * also allow #PSA_KEY_USAGE_EXPORT.
  438. * \param[in] attributes The attributes for the new key.
  439. * They are used as follows:
  440. * - The key type and size may be 0. If either is
  441. * nonzero, it must match the corresponding
  442. * attribute of the source key.
  443. * - The key location (the lifetime and, for
  444. * persistent keys, the key identifier) is
  445. * used directly.
  446. * - The policy constraints (usage flags and
  447. * algorithm policy) are combined from
  448. * the source key and \p attributes so that
  449. * both sets of restrictions apply, as
  450. * described in the documentation of this function.
  451. * \param[out] target_key On success, an identifier for the newly created
  452. * key. For persistent keys, this is the key
  453. * identifier defined in \p attributes.
  454. * \c 0 on failure.
  455. *
  456. * \retval #PSA_SUCCESS \emptydescription
  457. * \retval #PSA_ERROR_INVALID_HANDLE
  458. * \p source_key is invalid.
  459. * \retval #PSA_ERROR_ALREADY_EXISTS
  460. * This is an attempt to create a persistent key, and there is
  461. * already a persistent key with the given identifier.
  462. * \retval #PSA_ERROR_INVALID_ARGUMENT
  463. * The lifetime or identifier in \p attributes are invalid, or
  464. * the policy constraints on the source and specified in
  465. * \p attributes are incompatible, or
  466. * \p attributes specifies a key type or key size
  467. * which does not match the attributes of the source key.
  468. * \retval #PSA_ERROR_NOT_PERMITTED
  469. * The source key does not have the #PSA_KEY_USAGE_COPY usage flag, or
  470. * the source key is not exportable and its lifetime does not
  471. * allow copying it to the target's lifetime.
  472. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  473. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
  474. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  475. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  476. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  477. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  478. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  479. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  480. * \retval #PSA_ERROR_BAD_STATE
  481. * The library has not been previously initialized by psa_crypto_init().
  482. * It is implementation-dependent whether a failure to initialize
  483. * results in this error code.
  484. */
  485. psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
  486. const psa_key_attributes_t *attributes,
  487. mbedtls_svc_key_id_t *target_key);
  488. /**
  489. * \brief Destroy a key.
  490. *
  491. * This function destroys a key from both volatile
  492. * memory and, if applicable, non-volatile storage. Implementations shall
  493. * make a best effort to ensure that the key material cannot be recovered.
  494. *
  495. * This function also erases any metadata such as policies and frees
  496. * resources associated with the key.
  497. *
  498. * If a key is currently in use in a multipart operation, then destroying the
  499. * key will cause the multipart operation to fail.
  500. *
  501. * \warning We can only guarantee that the the key material will
  502. * eventually be wiped from memory. With threading enabled
  503. * and during concurrent execution, copies of the key material may
  504. * still exist until all threads have finished using the key.
  505. *
  506. * \param key Identifier of the key to erase. If this is \c 0, do nothing and
  507. * return #PSA_SUCCESS.
  508. *
  509. * \retval #PSA_SUCCESS
  510. * \p key was a valid identifier and the key material that it
  511. * referred to has been erased. Alternatively, \p key is \c 0.
  512. * \retval #PSA_ERROR_NOT_PERMITTED
  513. * The key cannot be erased because it is
  514. * read-only, either due to a policy or due to physical restrictions.
  515. * \retval #PSA_ERROR_INVALID_HANDLE
  516. * \p key is not a valid identifier nor \c 0.
  517. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  518. * There was a failure in communication with the cryptoprocessor.
  519. * The key material may still be present in the cryptoprocessor.
  520. * \retval #PSA_ERROR_DATA_INVALID
  521. * This error is typically a result of either storage corruption on a
  522. * cleartext storage backend, or an attempt to read data that was
  523. * written by an incompatible version of the library.
  524. * \retval #PSA_ERROR_STORAGE_FAILURE
  525. * The storage is corrupted. Implementations shall make a best effort
  526. * to erase key material even in this stage, however applications
  527. * should be aware that it may be impossible to guarantee that the
  528. * key material is not recoverable in such cases.
  529. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  530. * An unexpected condition which is not a storage corruption or
  531. * a communication failure occurred. The cryptoprocessor may have
  532. * been compromised.
  533. * \retval #PSA_ERROR_BAD_STATE
  534. * The library has not been previously initialized by psa_crypto_init().
  535. * It is implementation-dependent whether a failure to initialize
  536. * results in this error code.
  537. */
  538. psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
  539. /**@}*/
  540. /** \defgroup import_export Key import and export
  541. * @{
  542. */
  543. /**
  544. * \brief Import a key in binary format.
  545. *
  546. * This function supports any output from psa_export_key(). Refer to the
  547. * documentation of psa_export_public_key() for the format of public keys
  548. * and to the documentation of psa_export_key() for the format for
  549. * other key types.
  550. *
  551. * The key data determines the key size. The attributes may optionally
  552. * specify a key size; in this case it must match the size determined
  553. * from the key data. A key size of 0 in \p attributes indicates that
  554. * the key size is solely determined by the key data.
  555. *
  556. * Implementations must reject an attempt to import a key of size 0.
  557. *
  558. * This specification supports a single format for each key type.
  559. * Implementations may support other formats as long as the standard
  560. * format is supported. Implementations that support other formats
  561. * should ensure that the formats are clearly unambiguous so as to
  562. * minimize the risk that an invalid input is accidentally interpreted
  563. * according to a different format.
  564. *
  565. * \param[in] attributes The attributes for the new key.
  566. * The key size is always determined from the
  567. * \p data buffer.
  568. * If the key size in \p attributes is nonzero,
  569. * it must be equal to the size from \p data.
  570. * \param[out] key On success, an identifier to the newly created key.
  571. * For persistent keys, this is the key identifier
  572. * defined in \p attributes.
  573. * \c 0 on failure.
  574. * \param[in] data Buffer containing the key data. The content of this
  575. * buffer is interpreted according to the type declared
  576. * in \p attributes.
  577. * All implementations must support at least the format
  578. * described in the documentation
  579. * of psa_export_key() or psa_export_public_key() for
  580. * the chosen type. Implementations may allow other
  581. * formats, but should be conservative: implementations
  582. * should err on the side of rejecting content if it
  583. * may be erroneous (e.g. wrong type or truncated data).
  584. * \param data_length Size of the \p data buffer in bytes.
  585. *
  586. * \retval #PSA_SUCCESS
  587. * Success.
  588. * If the key is persistent, the key material and the key's metadata
  589. * have been saved to persistent storage.
  590. * \retval #PSA_ERROR_ALREADY_EXISTS
  591. * This is an attempt to create a persistent key, and there is
  592. * already a persistent key with the given identifier.
  593. * \retval #PSA_ERROR_NOT_SUPPORTED
  594. * The key type or key size is not supported, either by the
  595. * implementation in general or in this particular persistent location.
  596. * \retval #PSA_ERROR_INVALID_ARGUMENT
  597. * The key attributes, as a whole, are invalid, or
  598. * the key data is not correctly formatted, or
  599. * the size in \p attributes is nonzero and does not match the size
  600. * of the key data.
  601. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  602. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
  603. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  604. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  605. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  606. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  607. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  608. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  609. * \retval #PSA_ERROR_BAD_STATE
  610. * The library has not been previously initialized by psa_crypto_init().
  611. * It is implementation-dependent whether a failure to initialize
  612. * results in this error code.
  613. */
  614. psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
  615. const uint8_t *data,
  616. size_t data_length,
  617. mbedtls_svc_key_id_t *key);
  618. /**
  619. * \brief Export a key in binary format.
  620. *
  621. * The output of this function can be passed to psa_import_key() to
  622. * create an equivalent object.
  623. *
  624. * If the implementation of psa_import_key() supports other formats
  625. * beyond the format specified here, the output from psa_export_key()
  626. * must use the representation specified here, not the original
  627. * representation.
  628. *
  629. * For standard key types, the output format is as follows:
  630. *
  631. * - For symmetric keys (including MAC keys), the format is the
  632. * raw bytes of the key.
  633. * - For DES, the key data consists of 8 bytes. The parity bits must be
  634. * correct.
  635. * - For Triple-DES, the format is the concatenation of the
  636. * two or three DES keys.
  637. * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
  638. * is the non-encrypted DER encoding of the representation defined by
  639. * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
  640. * ```
  641. * RSAPrivateKey ::= SEQUENCE {
  642. * version INTEGER, -- must be 0
  643. * modulus INTEGER, -- n
  644. * publicExponent INTEGER, -- e
  645. * privateExponent INTEGER, -- d
  646. * prime1 INTEGER, -- p
  647. * prime2 INTEGER, -- q
  648. * exponent1 INTEGER, -- d mod (p-1)
  649. * exponent2 INTEGER, -- d mod (q-1)
  650. * coefficient INTEGER, -- (inverse of q) mod p
  651. * }
  652. * ```
  653. * - For elliptic curve key pairs (key types for which
  654. * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
  655. * a representation of the private value as a `ceiling(m/8)`-byte string
  656. * where `m` is the bit size associated with the curve, i.e. the bit size
  657. * of the order of the curve's coordinate field. This byte string is
  658. * in little-endian order for Montgomery curves (curve types
  659. * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
  660. * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
  661. * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
  662. * For Weierstrass curves, this is the content of the `privateKey` field of
  663. * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
  664. * the format is defined by RFC 7748, and output is masked according to §5.
  665. * For twisted Edwards curves, the private key is as defined by RFC 8032
  666. * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
  667. * - For Diffie-Hellman key exchange key pairs (key types for which
  668. * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
  669. * format is the representation of the private key `x` as a big-endian byte
  670. * string. The length of the byte string is the private key size in bytes
  671. * (leading zeroes are not stripped).
  672. * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
  673. * true), the format is the same as for psa_export_public_key().
  674. *
  675. * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
  676. *
  677. * \param key Identifier of the key to export. It must allow the
  678. * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
  679. * key.
  680. * \param[out] data Buffer where the key data is to be written.
  681. * \param data_size Size of the \p data buffer in bytes.
  682. * \param[out] data_length On success, the number of bytes
  683. * that make up the key data.
  684. *
  685. * \retval #PSA_SUCCESS \emptydescription
  686. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  687. * \retval #PSA_ERROR_NOT_PERMITTED
  688. * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
  689. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  690. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  691. * The size of the \p data buffer is too small. You can determine a
  692. * sufficient buffer size by calling
  693. * #PSA_EXPORT_KEY_OUTPUT_SIZE(\c type, \c bits)
  694. * where \c type is the key type
  695. * and \c bits is the key size in bits.
  696. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  697. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  698. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  699. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  700. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  701. * \retval #PSA_ERROR_BAD_STATE
  702. * The library has not been previously initialized by psa_crypto_init().
  703. * It is implementation-dependent whether a failure to initialize
  704. * results in this error code.
  705. */
  706. psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
  707. uint8_t *data,
  708. size_t data_size,
  709. size_t *data_length);
  710. /**
  711. * \brief Export a public key or the public part of a key pair in binary format.
  712. *
  713. * The output of this function can be passed to psa_import_key() to
  714. * create an object that is equivalent to the public key.
  715. *
  716. * This specification supports a single format for each key type.
  717. * Implementations may support other formats as long as the standard
  718. * format is supported. Implementations that support other formats
  719. * should ensure that the formats are clearly unambiguous so as to
  720. * minimize the risk that an invalid input is accidentally interpreted
  721. * according to a different format.
  722. *
  723. * For standard key types, the output format is as follows:
  724. * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
  725. * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
  726. * ```
  727. * RSAPublicKey ::= SEQUENCE {
  728. * modulus INTEGER, -- n
  729. * publicExponent INTEGER } -- e
  730. * ```
  731. * - For elliptic curve keys on a twisted Edwards curve (key types for which
  732. * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true and #PSA_KEY_TYPE_ECC_GET_FAMILY
  733. * returns #PSA_ECC_FAMILY_TWISTED_EDWARDS), the public key is as defined
  734. * by RFC 8032
  735. * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
  736. * - For other elliptic curve public keys (key types for which
  737. * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
  738. * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
  739. * Let `m` be the bit size associated with the curve, i.e. the bit size of
  740. * `q` for a curve over `F_q`. The representation consists of:
  741. * - The byte 0x04;
  742. * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
  743. * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
  744. * - For Diffie-Hellman key exchange public keys (key types for which
  745. * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
  746. * the format is the representation of the public key `y = g^x mod p` as a
  747. * big-endian byte string. The length of the byte string is the length of the
  748. * base prime `p` in bytes.
  749. *
  750. * Exporting a public key object or the public part of a key pair is
  751. * always permitted, regardless of the key's usage flags.
  752. *
  753. * \param key Identifier of the key to export.
  754. * \param[out] data Buffer where the key data is to be written.
  755. * \param data_size Size of the \p data buffer in bytes.
  756. * \param[out] data_length On success, the number of bytes
  757. * that make up the key data.
  758. *
  759. * \retval #PSA_SUCCESS \emptydescription
  760. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  761. * \retval #PSA_ERROR_INVALID_ARGUMENT
  762. * The key is neither a public key nor a key pair.
  763. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  764. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  765. * The size of the \p data buffer is too small. You can determine a
  766. * sufficient buffer size by calling
  767. * #PSA_EXPORT_KEY_OUTPUT_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
  768. * where \c type is the key type
  769. * and \c bits is the key size in bits.
  770. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  771. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  772. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  773. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  774. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  775. * \retval #PSA_ERROR_BAD_STATE
  776. * The library has not been previously initialized by psa_crypto_init().
  777. * It is implementation-dependent whether a failure to initialize
  778. * results in this error code.
  779. */
  780. psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
  781. uint8_t *data,
  782. size_t data_size,
  783. size_t *data_length);
  784. /**@}*/
  785. /** \defgroup hash Message digests
  786. * @{
  787. */
  788. /** Calculate the hash (digest) of a message.
  789. *
  790. * \note To verify the hash of a message against an
  791. * expected value, use psa_hash_compare() instead.
  792. *
  793. * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
  794. * such that #PSA_ALG_IS_HASH(\p alg) is true).
  795. * \param[in] input Buffer containing the message to hash.
  796. * \param input_length Size of the \p input buffer in bytes.
  797. * \param[out] hash Buffer where the hash is to be written.
  798. * \param hash_size Size of the \p hash buffer in bytes.
  799. * \param[out] hash_length On success, the number of bytes
  800. * that make up the hash value. This is always
  801. * #PSA_HASH_LENGTH(\p alg).
  802. *
  803. * \retval #PSA_SUCCESS
  804. * Success.
  805. * \retval #PSA_ERROR_NOT_SUPPORTED
  806. * \p alg is not supported or is not a hash algorithm.
  807. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  808. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  809. * \p hash_size is too small
  810. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  811. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  812. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  813. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  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[in] 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 \emptydescription
  845. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  846. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  847. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  848. * \retval #PSA_ERROR_BAD_STATE
  849. * The library has not been previously initialized by psa_crypto_init().
  850. * It is implementation-dependent whether a failure to initialize
  851. * results in this error code.
  852. */
  853. psa_status_t psa_hash_compare(psa_algorithm_t alg,
  854. const uint8_t *input,
  855. size_t input_length,
  856. const uint8_t *hash,
  857. size_t hash_length);
  858. /** The type of the state data structure for multipart hash operations.
  859. *
  860. * Before calling any function on a hash operation object, the application must
  861. * initialize it by any of the following means:
  862. * - Set the structure to all-bits-zero, for example:
  863. * \code
  864. * psa_hash_operation_t operation;
  865. * memset(&operation, 0, sizeof(operation));
  866. * \endcode
  867. * - Initialize the structure to logical zero values, for example:
  868. * \code
  869. * psa_hash_operation_t operation = {0};
  870. * \endcode
  871. * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
  872. * for example:
  873. * \code
  874. * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
  875. * \endcode
  876. * - Assign the result of the function psa_hash_operation_init()
  877. * to the structure, for example:
  878. * \code
  879. * psa_hash_operation_t operation;
  880. * operation = psa_hash_operation_init();
  881. * \endcode
  882. *
  883. * This is an implementation-defined \c struct. Applications should not
  884. * make any assumptions about the content of this structure.
  885. * Implementation details can change in future versions without notice. */
  886. typedef struct psa_hash_operation_s psa_hash_operation_t;
  887. /** \def PSA_HASH_OPERATION_INIT
  888. *
  889. * This macro returns a suitable initializer for a hash operation object
  890. * of type #psa_hash_operation_t.
  891. */
  892. /** Return an initial value for a hash operation object.
  893. */
  894. static psa_hash_operation_t psa_hash_operation_init(void);
  895. /** Set up a multipart hash operation.
  896. *
  897. * The sequence of operations to calculate a hash (message digest)
  898. * is as follows:
  899. * -# Allocate an operation object which will be passed to all the functions
  900. * listed here.
  901. * -# Initialize the operation object with one of the methods described in the
  902. * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
  903. * -# Call psa_hash_setup() to specify the algorithm.
  904. * -# Call psa_hash_update() zero, one or more times, passing a fragment
  905. * of the message each time. The hash that is calculated is the hash
  906. * of the concatenation of these messages in order.
  907. * -# To calculate the hash, call psa_hash_finish().
  908. * To compare the hash with an expected value, call psa_hash_verify().
  909. *
  910. * If an error occurs at any step after a call to psa_hash_setup(), the
  911. * operation will need to be reset by a call to psa_hash_abort(). The
  912. * application may call psa_hash_abort() at any time after the operation
  913. * has been initialized.
  914. *
  915. * After a successful call to psa_hash_setup(), the application must
  916. * eventually terminate the operation. The following events terminate an
  917. * operation:
  918. * - A successful call to psa_hash_finish() or psa_hash_verify().
  919. * - A call to psa_hash_abort().
  920. *
  921. * \param[in,out] operation The operation object to set up. It must have
  922. * been initialized as per the documentation for
  923. * #psa_hash_operation_t and not yet in use.
  924. * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
  925. * such that #PSA_ALG_IS_HASH(\p alg) is true).
  926. *
  927. * \retval #PSA_SUCCESS
  928. * Success.
  929. * \retval #PSA_ERROR_NOT_SUPPORTED
  930. * \p alg is not a supported hash algorithm.
  931. * \retval #PSA_ERROR_INVALID_ARGUMENT
  932. * \p alg is not a hash algorithm.
  933. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  934. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  935. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  936. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  937. * \retval #PSA_ERROR_BAD_STATE
  938. * The operation state is not valid (it must be inactive), or
  939. * the library has not been previously initialized by psa_crypto_init().
  940. * It is implementation-dependent whether a failure to initialize
  941. * results in this error code.
  942. */
  943. psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
  944. psa_algorithm_t alg);
  945. /** Add a message fragment to a multipart hash operation.
  946. *
  947. * The application must call psa_hash_setup() before calling this function.
  948. *
  949. * If this function returns an error status, the operation enters an error
  950. * state and must be aborted by calling psa_hash_abort().
  951. *
  952. * \param[in,out] operation Active hash operation.
  953. * \param[in] input Buffer containing the message fragment to hash.
  954. * \param input_length Size of the \p input buffer in bytes.
  955. *
  956. * \retval #PSA_SUCCESS
  957. * Success.
  958. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  959. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  960. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  961. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  962. * \retval #PSA_ERROR_BAD_STATE
  963. * The operation state is not valid (it must be active), or
  964. * the library has not been previously initialized by psa_crypto_init().
  965. * It is implementation-dependent whether a failure to initialize
  966. * results in this error code.
  967. */
  968. psa_status_t psa_hash_update(psa_hash_operation_t *operation,
  969. const uint8_t *input,
  970. size_t input_length);
  971. /** Finish the calculation of the hash of a message.
  972. *
  973. * The application must call psa_hash_setup() before calling this function.
  974. * This function calculates the hash of the message formed by concatenating
  975. * the inputs passed to preceding calls to psa_hash_update().
  976. *
  977. * When this function returns successfully, the operation becomes inactive.
  978. * If this function returns an error status, the operation enters an error
  979. * state and must be aborted by calling psa_hash_abort().
  980. *
  981. * \warning Applications should not call this function if they expect
  982. * a specific value for the hash. Call psa_hash_verify() instead.
  983. * Beware that comparing integrity or authenticity data such as
  984. * hash values with a function such as \c memcmp is risky
  985. * because the time taken by the comparison may leak information
  986. * about the hashed data which could allow an attacker to guess
  987. * a valid hash and thereby bypass security controls.
  988. *
  989. * \param[in,out] operation Active hash operation.
  990. * \param[out] hash Buffer where the hash is to be written.
  991. * \param hash_size Size of the \p hash buffer in bytes.
  992. * \param[out] hash_length On success, the number of bytes
  993. * that make up the hash value. This is always
  994. * #PSA_HASH_LENGTH(\c alg) where \c alg is the
  995. * hash algorithm that is calculated.
  996. *
  997. * \retval #PSA_SUCCESS
  998. * Success.
  999. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  1000. * The size of the \p hash buffer is too small. You can determine a
  1001. * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
  1002. * where \c alg is the hash algorithm that is calculated.
  1003. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1004. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1005. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1006. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1007. * \retval #PSA_ERROR_BAD_STATE
  1008. * The operation state is not valid (it must be active), or
  1009. * the library has not been previously initialized by psa_crypto_init().
  1010. * It is implementation-dependent whether a failure to initialize
  1011. * results in this error code.
  1012. */
  1013. psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
  1014. uint8_t *hash,
  1015. size_t hash_size,
  1016. size_t *hash_length);
  1017. /** Finish the calculation of the hash of a message and compare it with
  1018. * an expected value.
  1019. *
  1020. * The application must call psa_hash_setup() before calling this function.
  1021. * This function calculates the hash of the message formed by concatenating
  1022. * the inputs passed to preceding calls to psa_hash_update(). It then
  1023. * compares the calculated hash with the expected hash passed as a
  1024. * parameter to this function.
  1025. *
  1026. * When this function returns successfully, the operation becomes inactive.
  1027. * If this function returns an error status, the operation enters an error
  1028. * state and must be aborted by calling psa_hash_abort().
  1029. *
  1030. * \note Implementations shall make the best effort to ensure that the
  1031. * comparison between the actual hash and the expected hash is performed
  1032. * in constant time.
  1033. *
  1034. * \param[in,out] operation Active hash operation.
  1035. * \param[in] hash Buffer containing the expected hash value.
  1036. * \param hash_length Size of the \p hash buffer in bytes.
  1037. *
  1038. * \retval #PSA_SUCCESS
  1039. * The expected hash is identical to the actual hash of the message.
  1040. * \retval #PSA_ERROR_INVALID_SIGNATURE
  1041. * The hash of the message was calculated successfully, but it
  1042. * differs from the expected hash.
  1043. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1044. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1045. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1046. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1047. * \retval #PSA_ERROR_BAD_STATE
  1048. * The operation state is not valid (it must be active), or
  1049. * the library has not been previously initialized by psa_crypto_init().
  1050. * It is implementation-dependent whether a failure to initialize
  1051. * results in this error code.
  1052. */
  1053. psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
  1054. const uint8_t *hash,
  1055. size_t hash_length);
  1056. /** Abort a hash operation.
  1057. *
  1058. * Aborting an operation frees all associated resources except for the
  1059. * \p operation structure itself. Once aborted, the operation object
  1060. * can be reused for another operation by calling
  1061. * psa_hash_setup() again.
  1062. *
  1063. * You may call this function any time after the operation object has
  1064. * been initialized by one of the methods described in #psa_hash_operation_t.
  1065. *
  1066. * In particular, calling psa_hash_abort() after the operation has been
  1067. * terminated by a call to psa_hash_abort(), psa_hash_finish() or
  1068. * psa_hash_verify() is safe and has no effect.
  1069. *
  1070. * \param[in,out] operation Initialized hash operation.
  1071. *
  1072. * \retval #PSA_SUCCESS \emptydescription
  1073. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1074. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1075. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1076. * \retval #PSA_ERROR_BAD_STATE
  1077. * The library has not been previously initialized by psa_crypto_init().
  1078. * It is implementation-dependent whether a failure to initialize
  1079. * results in this error code.
  1080. */
  1081. psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
  1082. /** Clone a hash operation.
  1083. *
  1084. * This function copies the state of an ongoing hash operation to
  1085. * a new operation object. In other words, this function is equivalent
  1086. * to calling psa_hash_setup() on \p target_operation with the same
  1087. * algorithm that \p source_operation was set up for, then
  1088. * psa_hash_update() on \p target_operation with the same input that
  1089. * that was passed to \p source_operation. After this function returns, the
  1090. * two objects are independent, i.e. subsequent calls involving one of
  1091. * the objects do not affect the other object.
  1092. *
  1093. * \param[in] source_operation The active hash operation to clone.
  1094. * \param[in,out] target_operation The operation object to set up.
  1095. * It must be initialized but not active.
  1096. *
  1097. * \retval #PSA_SUCCESS \emptydescription
  1098. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1099. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1100. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1101. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1102. * \retval #PSA_ERROR_BAD_STATE
  1103. * The \p source_operation state is not valid (it must be active), or
  1104. * the \p target_operation state is not valid (it must be inactive), or
  1105. * the library has not been previously initialized by psa_crypto_init().
  1106. * It is implementation-dependent whether a failure to initialize
  1107. * results in this error code.
  1108. */
  1109. psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
  1110. psa_hash_operation_t *target_operation);
  1111. /**@}*/
  1112. /** \defgroup MAC Message authentication codes
  1113. * @{
  1114. */
  1115. /** Calculate the MAC (message authentication code) of a message.
  1116. *
  1117. * \note To verify the MAC of a message against an
  1118. * expected value, use psa_mac_verify() instead.
  1119. * Beware that comparing integrity or authenticity data such as
  1120. * MAC values with a function such as \c memcmp is risky
  1121. * because the time taken by the comparison may leak information
  1122. * about the MAC value which could allow an attacker to guess
  1123. * a valid MAC and thereby bypass security controls.
  1124. *
  1125. * \param key Identifier of the key to use for the operation. It
  1126. * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
  1127. * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
  1128. * such that #PSA_ALG_IS_MAC(\p alg) is true).
  1129. * \param[in] input Buffer containing the input message.
  1130. * \param input_length Size of the \p input buffer in bytes.
  1131. * \param[out] mac Buffer where the MAC value is to be written.
  1132. * \param mac_size Size of the \p mac buffer in bytes.
  1133. * \param[out] mac_length On success, the number of bytes
  1134. * that make up the MAC value.
  1135. *
  1136. * \retval #PSA_SUCCESS
  1137. * Success.
  1138. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  1139. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  1140. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1141. * \p key is not compatible with \p alg.
  1142. * \retval #PSA_ERROR_NOT_SUPPORTED
  1143. * \p alg is not supported or is not a MAC algorithm.
  1144. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  1145. * \p mac_size is too small
  1146. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1147. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1148. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1149. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1150. * \retval #PSA_ERROR_STORAGE_FAILURE
  1151. * The key could not be retrieved from storage.
  1152. * \retval #PSA_ERROR_BAD_STATE
  1153. * The library has not been previously initialized by psa_crypto_init().
  1154. * It is implementation-dependent whether a failure to initialize
  1155. * results in this error code.
  1156. */
  1157. psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
  1158. psa_algorithm_t alg,
  1159. const uint8_t *input,
  1160. size_t input_length,
  1161. uint8_t *mac,
  1162. size_t mac_size,
  1163. size_t *mac_length);
  1164. /** Calculate the MAC of a message and compare it with a reference value.
  1165. *
  1166. * \param key Identifier of the key to use for the operation. It
  1167. * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
  1168. * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
  1169. * such that #PSA_ALG_IS_MAC(\p alg) is true).
  1170. * \param[in] input Buffer containing the input message.
  1171. * \param input_length Size of the \p input buffer in bytes.
  1172. * \param[in] mac Buffer containing the expected MAC value.
  1173. * \param mac_length Size of the \p mac buffer in bytes.
  1174. *
  1175. * \retval #PSA_SUCCESS
  1176. * The expected MAC is identical to the actual MAC of the input.
  1177. * \retval #PSA_ERROR_INVALID_SIGNATURE
  1178. * The MAC of the message was calculated successfully, but it
  1179. * differs from the expected value.
  1180. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  1181. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  1182. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1183. * \p key is not compatible with \p alg.
  1184. * \retval #PSA_ERROR_NOT_SUPPORTED
  1185. * \p alg is not supported or is not a MAC algorithm.
  1186. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1187. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1188. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1189. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1190. * \retval #PSA_ERROR_STORAGE_FAILURE
  1191. * The key could not be retrieved from storage.
  1192. * \retval #PSA_ERROR_BAD_STATE
  1193. * The library has not been previously initialized by psa_crypto_init().
  1194. * It is implementation-dependent whether a failure to initialize
  1195. * results in this error code.
  1196. */
  1197. psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
  1198. psa_algorithm_t alg,
  1199. const uint8_t *input,
  1200. size_t input_length,
  1201. const uint8_t *mac,
  1202. size_t mac_length);
  1203. /** The type of the state data structure for multipart MAC operations.
  1204. *
  1205. * Before calling any function on a MAC operation object, the application must
  1206. * initialize it by any of the following means:
  1207. * - Set the structure to all-bits-zero, for example:
  1208. * \code
  1209. * psa_mac_operation_t operation;
  1210. * memset(&operation, 0, sizeof(operation));
  1211. * \endcode
  1212. * - Initialize the structure to logical zero values, for example:
  1213. * \code
  1214. * psa_mac_operation_t operation = {0};
  1215. * \endcode
  1216. * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
  1217. * for example:
  1218. * \code
  1219. * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
  1220. * \endcode
  1221. * - Assign the result of the function psa_mac_operation_init()
  1222. * to the structure, for example:
  1223. * \code
  1224. * psa_mac_operation_t operation;
  1225. * operation = psa_mac_operation_init();
  1226. * \endcode
  1227. *
  1228. *
  1229. * This is an implementation-defined \c struct. Applications should not
  1230. * make any assumptions about the content of this structure.
  1231. * Implementation details can change in future versions without notice. */
  1232. typedef struct psa_mac_operation_s psa_mac_operation_t;
  1233. /** \def PSA_MAC_OPERATION_INIT
  1234. *
  1235. * This macro returns a suitable initializer for a MAC operation object of type
  1236. * #psa_mac_operation_t.
  1237. */
  1238. /** Return an initial value for a MAC operation object.
  1239. */
  1240. static psa_mac_operation_t psa_mac_operation_init(void);
  1241. /** Set up a multipart MAC calculation operation.
  1242. *
  1243. * This function sets up the calculation of the MAC
  1244. * (message authentication code) of a byte string.
  1245. * To verify the MAC of a message against an
  1246. * expected value, use psa_mac_verify_setup() instead.
  1247. *
  1248. * The sequence of operations to calculate a MAC is as follows:
  1249. * -# Allocate an operation object which will be passed to all the functions
  1250. * listed here.
  1251. * -# Initialize the operation object with one of the methods described in the
  1252. * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
  1253. * -# Call psa_mac_sign_setup() to specify the algorithm and key.
  1254. * -# Call psa_mac_update() zero, one or more times, passing a fragment
  1255. * of the message each time. The MAC that is calculated is the MAC
  1256. * of the concatenation of these messages in order.
  1257. * -# At the end of the message, call psa_mac_sign_finish() to finish
  1258. * calculating the MAC value and retrieve it.
  1259. *
  1260. * If an error occurs at any step after a call to psa_mac_sign_setup(), the
  1261. * operation will need to be reset by a call to psa_mac_abort(). The
  1262. * application may call psa_mac_abort() at any time after the operation
  1263. * has been initialized.
  1264. *
  1265. * After a successful call to psa_mac_sign_setup(), the application must
  1266. * eventually terminate the operation through one of the following methods:
  1267. * - A successful call to psa_mac_sign_finish().
  1268. * - A call to psa_mac_abort().
  1269. *
  1270. * \param[in,out] operation The operation object to set up. It must have
  1271. * been initialized as per the documentation for
  1272. * #psa_mac_operation_t and not yet in use.
  1273. * \param key Identifier of the key to use for the operation. It
  1274. * must remain valid until the operation terminates.
  1275. * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
  1276. * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
  1277. * such that #PSA_ALG_IS_MAC(\p alg) is true).
  1278. *
  1279. * \retval #PSA_SUCCESS
  1280. * Success.
  1281. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  1282. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  1283. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1284. * \p key is not compatible with \p alg.
  1285. * \retval #PSA_ERROR_NOT_SUPPORTED
  1286. * \p alg is not supported or is not a MAC algorithm.
  1287. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1288. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1289. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1290. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1291. * \retval #PSA_ERROR_STORAGE_FAILURE
  1292. * The key could not be retrieved from storage.
  1293. * \retval #PSA_ERROR_BAD_STATE
  1294. * The operation state is not valid (it must be inactive), or
  1295. * the library has not been previously initialized by psa_crypto_init().
  1296. * It is implementation-dependent whether a failure to initialize
  1297. * results in this error code.
  1298. */
  1299. psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
  1300. mbedtls_svc_key_id_t key,
  1301. psa_algorithm_t alg);
  1302. /** Set up a multipart MAC verification operation.
  1303. *
  1304. * This function sets up the verification of the MAC
  1305. * (message authentication code) of a byte string against an expected value.
  1306. *
  1307. * The sequence of operations to verify a MAC is as follows:
  1308. * -# Allocate an operation object which will be passed to all the functions
  1309. * listed here.
  1310. * -# Initialize the operation object with one of the methods described in the
  1311. * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
  1312. * -# Call psa_mac_verify_setup() to specify the algorithm and key.
  1313. * -# Call psa_mac_update() zero, one or more times, passing a fragment
  1314. * of the message each time. The MAC that is calculated is the MAC
  1315. * of the concatenation of these messages in order.
  1316. * -# At the end of the message, call psa_mac_verify_finish() to finish
  1317. * calculating the actual MAC of the message and verify it against
  1318. * the expected value.
  1319. *
  1320. * If an error occurs at any step after a call to psa_mac_verify_setup(), the
  1321. * operation will need to be reset by a call to psa_mac_abort(). The
  1322. * application may call psa_mac_abort() at any time after the operation
  1323. * has been initialized.
  1324. *
  1325. * After a successful call to psa_mac_verify_setup(), the application must
  1326. * eventually terminate the operation through one of the following methods:
  1327. * - A successful call to psa_mac_verify_finish().
  1328. * - A call to psa_mac_abort().
  1329. *
  1330. * \param[in,out] operation The operation object to set up. It must have
  1331. * been initialized as per the documentation for
  1332. * #psa_mac_operation_t and not yet in use.
  1333. * \param key Identifier of the key to use for the operation. It
  1334. * must remain valid until the operation terminates.
  1335. * It must allow the usage
  1336. * PSA_KEY_USAGE_VERIFY_MESSAGE.
  1337. * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
  1338. * such that #PSA_ALG_IS_MAC(\p alg) is true).
  1339. *
  1340. * \retval #PSA_SUCCESS
  1341. * Success.
  1342. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  1343. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  1344. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1345. * \c key is not compatible with \c alg.
  1346. * \retval #PSA_ERROR_NOT_SUPPORTED
  1347. * \c alg is not supported or is not a MAC algorithm.
  1348. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1349. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1350. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1351. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1352. * \retval #PSA_ERROR_STORAGE_FAILURE
  1353. * The key could not be retrieved from storage.
  1354. * \retval #PSA_ERROR_BAD_STATE
  1355. * The operation state is not valid (it must be inactive), or
  1356. * the library has not been previously initialized by psa_crypto_init().
  1357. * It is implementation-dependent whether a failure to initialize
  1358. * results in this error code.
  1359. */
  1360. psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
  1361. mbedtls_svc_key_id_t key,
  1362. psa_algorithm_t alg);
  1363. /** Add a message fragment to a multipart MAC operation.
  1364. *
  1365. * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
  1366. * before calling this function.
  1367. *
  1368. * If this function returns an error status, the operation enters an error
  1369. * state and must be aborted by calling psa_mac_abort().
  1370. *
  1371. * \param[in,out] operation Active MAC operation.
  1372. * \param[in] input Buffer containing the message fragment to add to
  1373. * the MAC calculation.
  1374. * \param input_length Size of the \p input buffer in bytes.
  1375. *
  1376. * \retval #PSA_SUCCESS
  1377. * Success.
  1378. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1379. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1380. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1381. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1382. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  1383. * \retval #PSA_ERROR_BAD_STATE
  1384. * The operation state is not valid (it must be active), or
  1385. * the library has not been previously initialized by psa_crypto_init().
  1386. * It is implementation-dependent whether a failure to initialize
  1387. * results in this error code.
  1388. */
  1389. psa_status_t psa_mac_update(psa_mac_operation_t *operation,
  1390. const uint8_t *input,
  1391. size_t input_length);
  1392. /** Finish the calculation of the MAC of a message.
  1393. *
  1394. * The application must call psa_mac_sign_setup() before calling this function.
  1395. * This function calculates the MAC of the message formed by concatenating
  1396. * the inputs passed to preceding calls to psa_mac_update().
  1397. *
  1398. * When this function returns successfully, the operation becomes inactive.
  1399. * If this function returns an error status, the operation enters an error
  1400. * state and must be aborted by calling psa_mac_abort().
  1401. *
  1402. * \warning Applications should not call this function if they expect
  1403. * a specific value for the MAC. Call psa_mac_verify_finish() instead.
  1404. * Beware that comparing integrity or authenticity data such as
  1405. * MAC values with a function such as \c memcmp is risky
  1406. * because the time taken by the comparison may leak information
  1407. * about the MAC value which could allow an attacker to guess
  1408. * a valid MAC and thereby bypass security controls.
  1409. *
  1410. * \param[in,out] operation Active MAC operation.
  1411. * \param[out] mac Buffer where the MAC value is to be written.
  1412. * \param mac_size Size of the \p mac buffer in bytes.
  1413. * \param[out] mac_length On success, the number of bytes
  1414. * that make up the MAC value. This is always
  1415. * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
  1416. * where \c key_type and \c key_bits are the type and
  1417. * bit-size respectively of the key and \c alg is the
  1418. * MAC algorithm that is calculated.
  1419. *
  1420. * \retval #PSA_SUCCESS
  1421. * Success.
  1422. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  1423. * The size of the \p mac buffer is too small. You can determine a
  1424. * sufficient buffer size by calling PSA_MAC_LENGTH().
  1425. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1426. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1427. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1428. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1429. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  1430. * \retval #PSA_ERROR_BAD_STATE
  1431. * The operation state is not valid (it must be an active mac sign
  1432. * operation), or the library has not been previously initialized
  1433. * by psa_crypto_init().
  1434. * It is implementation-dependent whether a failure to initialize
  1435. * results in this error code.
  1436. */
  1437. psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
  1438. uint8_t *mac,
  1439. size_t mac_size,
  1440. size_t *mac_length);
  1441. /** Finish the calculation of the MAC of a message and compare it with
  1442. * an expected value.
  1443. *
  1444. * The application must call psa_mac_verify_setup() before calling this function.
  1445. * This function calculates the MAC of the message formed by concatenating
  1446. * the inputs passed to preceding calls to psa_mac_update(). It then
  1447. * compares the calculated MAC with the expected MAC passed as a
  1448. * parameter to this function.
  1449. *
  1450. * When this function returns successfully, the operation becomes inactive.
  1451. * If this function returns an error status, the operation enters an error
  1452. * state and must be aborted by calling psa_mac_abort().
  1453. *
  1454. * \note Implementations shall make the best effort to ensure that the
  1455. * comparison between the actual MAC and the expected MAC is performed
  1456. * in constant time.
  1457. *
  1458. * \param[in,out] operation Active MAC operation.
  1459. * \param[in] mac Buffer containing the expected MAC value.
  1460. * \param mac_length Size of the \p mac buffer in bytes.
  1461. *
  1462. * \retval #PSA_SUCCESS
  1463. * The expected MAC is identical to the actual MAC of the message.
  1464. * \retval #PSA_ERROR_INVALID_SIGNATURE
  1465. * The MAC of the message was calculated successfully, but it
  1466. * differs from the expected MAC.
  1467. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1468. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1469. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1470. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1471. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  1472. * \retval #PSA_ERROR_BAD_STATE
  1473. * The operation state is not valid (it must be an active mac verify
  1474. * operation), or the library has not been previously initialized
  1475. * by psa_crypto_init().
  1476. * It is implementation-dependent whether a failure to initialize
  1477. * results in this error code.
  1478. */
  1479. psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
  1480. const uint8_t *mac,
  1481. size_t mac_length);
  1482. /** Abort a MAC operation.
  1483. *
  1484. * Aborting an operation frees all associated resources except for the
  1485. * \p operation structure itself. Once aborted, the operation object
  1486. * can be reused for another operation by calling
  1487. * psa_mac_sign_setup() or psa_mac_verify_setup() again.
  1488. *
  1489. * You may call this function any time after the operation object has
  1490. * been initialized by one of the methods described in #psa_mac_operation_t.
  1491. *
  1492. * In particular, calling psa_mac_abort() after the operation has been
  1493. * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
  1494. * psa_mac_verify_finish() is safe and has no effect.
  1495. *
  1496. * \param[in,out] operation Initialized MAC operation.
  1497. *
  1498. * \retval #PSA_SUCCESS \emptydescription
  1499. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1500. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1501. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1502. * \retval #PSA_ERROR_BAD_STATE
  1503. * The library has not been previously initialized by psa_crypto_init().
  1504. * It is implementation-dependent whether a failure to initialize
  1505. * results in this error code.
  1506. */
  1507. psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
  1508. /**@}*/
  1509. /** \defgroup cipher Symmetric ciphers
  1510. * @{
  1511. */
  1512. /** Encrypt a message using a symmetric cipher.
  1513. *
  1514. * This function encrypts a message with a random IV (initialization
  1515. * vector). Use the multipart operation interface with a
  1516. * #psa_cipher_operation_t object to provide other forms of IV.
  1517. *
  1518. * \param key Identifier of the key to use for the operation.
  1519. * It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
  1520. * \param alg The cipher algorithm to compute
  1521. * (\c PSA_ALG_XXX value such that
  1522. * #PSA_ALG_IS_CIPHER(\p alg) is true).
  1523. * \param[in] input Buffer containing the message to encrypt.
  1524. * \param input_length Size of the \p input buffer in bytes.
  1525. * \param[out] output Buffer where the output is to be written.
  1526. * The output contains the IV followed by
  1527. * the ciphertext proper.
  1528. * \param output_size Size of the \p output buffer in bytes.
  1529. * \param[out] output_length On success, the number of bytes
  1530. * that make up the output.
  1531. *
  1532. * \retval #PSA_SUCCESS
  1533. * Success.
  1534. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  1535. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  1536. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1537. * \p key is not compatible with \p alg.
  1538. * \retval #PSA_ERROR_NOT_SUPPORTED
  1539. * \p alg is not supported or is not a cipher algorithm.
  1540. * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
  1541. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1542. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1543. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1544. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1545. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  1546. * \retval #PSA_ERROR_BAD_STATE
  1547. * The library has not been previously initialized by psa_crypto_init().
  1548. * It is implementation-dependent whether a failure to initialize
  1549. * results in this error code.
  1550. */
  1551. psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
  1552. psa_algorithm_t alg,
  1553. const uint8_t *input,
  1554. size_t input_length,
  1555. uint8_t *output,
  1556. size_t output_size,
  1557. size_t *output_length);
  1558. /** Decrypt a message using a symmetric cipher.
  1559. *
  1560. * This function decrypts a message encrypted with a symmetric cipher.
  1561. *
  1562. * \param key Identifier of the key to use for the operation.
  1563. * It must remain valid until the operation
  1564. * terminates. It must allow the usage
  1565. * #PSA_KEY_USAGE_DECRYPT.
  1566. * \param alg The cipher algorithm to compute
  1567. * (\c PSA_ALG_XXX value such that
  1568. * #PSA_ALG_IS_CIPHER(\p alg) is true).
  1569. * \param[in] input Buffer containing the message to decrypt.
  1570. * This consists of the IV followed by the
  1571. * ciphertext proper.
  1572. * \param input_length Size of the \p input buffer in bytes.
  1573. * \param[out] output Buffer where the plaintext is to be written.
  1574. * \param output_size Size of the \p output buffer in bytes.
  1575. * \param[out] output_length On success, the number of bytes
  1576. * that make up the output.
  1577. *
  1578. * \retval #PSA_SUCCESS
  1579. * Success.
  1580. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  1581. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  1582. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1583. * \p key is not compatible with \p alg.
  1584. * \retval #PSA_ERROR_NOT_SUPPORTED
  1585. * \p alg is not supported or is not a cipher algorithm.
  1586. * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
  1587. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1588. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1589. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1590. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  1591. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1592. * \retval #PSA_ERROR_BAD_STATE
  1593. * The library has not been previously initialized by psa_crypto_init().
  1594. * It is implementation-dependent whether a failure to initialize
  1595. * results in this error code.
  1596. */
  1597. psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
  1598. psa_algorithm_t alg,
  1599. const uint8_t *input,
  1600. size_t input_length,
  1601. uint8_t *output,
  1602. size_t output_size,
  1603. size_t *output_length);
  1604. /** The type of the state data structure for multipart cipher operations.
  1605. *
  1606. * Before calling any function on a cipher operation object, the application
  1607. * must initialize it by any of the following means:
  1608. * - Set the structure to all-bits-zero, for example:
  1609. * \code
  1610. * psa_cipher_operation_t operation;
  1611. * memset(&operation, 0, sizeof(operation));
  1612. * \endcode
  1613. * - Initialize the structure to logical zero values, for example:
  1614. * \code
  1615. * psa_cipher_operation_t operation = {0};
  1616. * \endcode
  1617. * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
  1618. * for example:
  1619. * \code
  1620. * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  1621. * \endcode
  1622. * - Assign the result of the function psa_cipher_operation_init()
  1623. * to the structure, for example:
  1624. * \code
  1625. * psa_cipher_operation_t operation;
  1626. * operation = psa_cipher_operation_init();
  1627. * \endcode
  1628. *
  1629. * This is an implementation-defined \c struct. Applications should not
  1630. * make any assumptions about the content of this structure.
  1631. * Implementation details can change in future versions without notice. */
  1632. typedef struct psa_cipher_operation_s psa_cipher_operation_t;
  1633. /** \def PSA_CIPHER_OPERATION_INIT
  1634. *
  1635. * This macro returns a suitable initializer for a cipher operation object of
  1636. * type #psa_cipher_operation_t.
  1637. */
  1638. /** Return an initial value for a cipher operation object.
  1639. */
  1640. static psa_cipher_operation_t psa_cipher_operation_init(void);
  1641. /** Set the key for a multipart symmetric encryption operation.
  1642. *
  1643. * The sequence of operations to encrypt a message with a symmetric cipher
  1644. * is as follows:
  1645. * -# Allocate an operation object which will be passed to all the functions
  1646. * listed here.
  1647. * -# Initialize the operation object with one of the methods described in the
  1648. * documentation for #psa_cipher_operation_t, e.g.
  1649. * #PSA_CIPHER_OPERATION_INIT.
  1650. * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
  1651. * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
  1652. * generate or set the IV (initialization vector). You should use
  1653. * psa_cipher_generate_iv() unless the protocol you are implementing
  1654. * requires a specific IV value.
  1655. * -# Call psa_cipher_update() zero, one or more times, passing a fragment
  1656. * of the message each time.
  1657. * -# Call psa_cipher_finish().
  1658. *
  1659. * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
  1660. * the operation will need to be reset by a call to psa_cipher_abort(). The
  1661. * application may call psa_cipher_abort() at any time after the operation
  1662. * has been initialized.
  1663. *
  1664. * After a successful call to psa_cipher_encrypt_setup(), the application must
  1665. * eventually terminate the operation. The following events terminate an
  1666. * operation:
  1667. * - A successful call to psa_cipher_finish().
  1668. * - A call to psa_cipher_abort().
  1669. *
  1670. * \param[in,out] operation The operation object to set up. It must have
  1671. * been initialized as per the documentation for
  1672. * #psa_cipher_operation_t and not yet in use.
  1673. * \param key Identifier of the key to use for the operation.
  1674. * It must remain valid until the operation
  1675. * terminates. It must allow the usage
  1676. * #PSA_KEY_USAGE_ENCRYPT.
  1677. * \param alg The cipher algorithm to compute
  1678. * (\c PSA_ALG_XXX value such that
  1679. * #PSA_ALG_IS_CIPHER(\p alg) is true).
  1680. *
  1681. * \retval #PSA_SUCCESS
  1682. * Success.
  1683. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  1684. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  1685. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1686. * \p key is not compatible with \p alg.
  1687. * \retval #PSA_ERROR_NOT_SUPPORTED
  1688. * \p alg is not supported or is not a cipher algorithm.
  1689. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1690. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1691. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1692. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1693. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  1694. * \retval #PSA_ERROR_BAD_STATE
  1695. * The operation state is not valid (it must be inactive), or
  1696. * the library has not been previously initialized by psa_crypto_init().
  1697. * It is implementation-dependent whether a failure to initialize
  1698. * results in this error code.
  1699. */
  1700. psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
  1701. mbedtls_svc_key_id_t key,
  1702. psa_algorithm_t alg);
  1703. /** Set the key for a multipart symmetric decryption operation.
  1704. *
  1705. * The sequence of operations to decrypt a message with a symmetric cipher
  1706. * is as follows:
  1707. * -# Allocate an operation object which will be passed to all the functions
  1708. * listed here.
  1709. * -# Initialize the operation object with one of the methods described in the
  1710. * documentation for #psa_cipher_operation_t, e.g.
  1711. * #PSA_CIPHER_OPERATION_INIT.
  1712. * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
  1713. * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
  1714. * decryption. If the IV is prepended to the ciphertext, you can call
  1715. * psa_cipher_update() on a buffer containing the IV followed by the
  1716. * beginning of the message.
  1717. * -# Call psa_cipher_update() zero, one or more times, passing a fragment
  1718. * of the message each time.
  1719. * -# Call psa_cipher_finish().
  1720. *
  1721. * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
  1722. * the operation will need to be reset by a call to psa_cipher_abort(). The
  1723. * application may call psa_cipher_abort() at any time after the operation
  1724. * has been initialized.
  1725. *
  1726. * After a successful call to psa_cipher_decrypt_setup(), the application must
  1727. * eventually terminate the operation. The following events terminate an
  1728. * operation:
  1729. * - A successful call to psa_cipher_finish().
  1730. * - A call to psa_cipher_abort().
  1731. *
  1732. * \param[in,out] operation The operation object to set up. It must have
  1733. * been initialized as per the documentation for
  1734. * #psa_cipher_operation_t and not yet in use.
  1735. * \param key Identifier of the key to use for the operation.
  1736. * It must remain valid until the operation
  1737. * terminates. It must allow the usage
  1738. * #PSA_KEY_USAGE_DECRYPT.
  1739. * \param alg The cipher algorithm to compute
  1740. * (\c PSA_ALG_XXX value such that
  1741. * #PSA_ALG_IS_CIPHER(\p alg) is true).
  1742. *
  1743. * \retval #PSA_SUCCESS
  1744. * Success.
  1745. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  1746. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  1747. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1748. * \p key is not compatible with \p alg.
  1749. * \retval #PSA_ERROR_NOT_SUPPORTED
  1750. * \p alg is not supported or is not a cipher algorithm.
  1751. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1752. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1753. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1754. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1755. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  1756. * \retval #PSA_ERROR_BAD_STATE
  1757. * The operation state is not valid (it must be inactive), or
  1758. * the library has not been previously initialized by psa_crypto_init().
  1759. * It is implementation-dependent whether a failure to initialize
  1760. * results in this error code.
  1761. */
  1762. psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
  1763. mbedtls_svc_key_id_t key,
  1764. psa_algorithm_t alg);
  1765. /** Generate an IV for a symmetric encryption operation.
  1766. *
  1767. * This function generates a random IV (initialization vector), nonce
  1768. * or initial counter value for the encryption operation as appropriate
  1769. * for the chosen algorithm, key type and key size.
  1770. *
  1771. * The application must call psa_cipher_encrypt_setup() before
  1772. * calling this function.
  1773. *
  1774. * If this function returns an error status, the operation enters an error
  1775. * state and must be aborted by calling psa_cipher_abort().
  1776. *
  1777. * \param[in,out] operation Active cipher operation.
  1778. * \param[out] iv Buffer where the generated IV is to be written.
  1779. * \param iv_size Size of the \p iv buffer in bytes.
  1780. * \param[out] iv_length On success, the number of bytes of the
  1781. * generated IV.
  1782. *
  1783. * \retval #PSA_SUCCESS
  1784. * Success.
  1785. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  1786. * The size of the \p iv buffer is too small.
  1787. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1788. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1789. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1790. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1791. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  1792. * \retval #PSA_ERROR_BAD_STATE
  1793. * The operation state is not valid (it must be active, with no IV set),
  1794. * or the library has not been previously initialized
  1795. * by psa_crypto_init().
  1796. * It is implementation-dependent whether a failure to initialize
  1797. * results in this error code.
  1798. */
  1799. psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
  1800. uint8_t *iv,
  1801. size_t iv_size,
  1802. size_t *iv_length);
  1803. /** Set the IV for a symmetric encryption or decryption operation.
  1804. *
  1805. * This function sets the IV (initialization vector), nonce
  1806. * or initial counter value for the encryption or decryption operation.
  1807. *
  1808. * The application must call psa_cipher_encrypt_setup() before
  1809. * calling this function.
  1810. *
  1811. * If this function returns an error status, the operation enters an error
  1812. * state and must be aborted by calling psa_cipher_abort().
  1813. *
  1814. * \note When encrypting, applications should use psa_cipher_generate_iv()
  1815. * instead of this function, unless implementing a protocol that requires
  1816. * a non-random IV.
  1817. *
  1818. * \param[in,out] operation Active cipher operation.
  1819. * \param[in] iv Buffer containing the IV to use.
  1820. * \param iv_length Size of the IV in bytes.
  1821. *
  1822. * \retval #PSA_SUCCESS
  1823. * Success.
  1824. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1825. * The size of \p iv is not acceptable for the chosen algorithm,
  1826. * or the chosen algorithm does not use an IV.
  1827. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1828. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1829. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1830. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1831. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  1832. * \retval #PSA_ERROR_BAD_STATE
  1833. * The operation state is not valid (it must be an active cipher
  1834. * encrypt operation, with no IV set), or the library has not been
  1835. * previously initialized by psa_crypto_init().
  1836. * It is implementation-dependent whether a failure to initialize
  1837. * results in this error code.
  1838. */
  1839. psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
  1840. const uint8_t *iv,
  1841. size_t iv_length);
  1842. /** Encrypt or decrypt a message fragment in an active cipher operation.
  1843. *
  1844. * Before calling this function, you must:
  1845. * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
  1846. * The choice of setup function determines whether this function
  1847. * encrypts or decrypts its input.
  1848. * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
  1849. * (recommended when encrypting) or psa_cipher_set_iv().
  1850. *
  1851. * If this function returns an error status, the operation enters an error
  1852. * state and must be aborted by calling psa_cipher_abort().
  1853. *
  1854. * \param[in,out] operation Active cipher operation.
  1855. * \param[in] input Buffer containing the message fragment to
  1856. * encrypt or decrypt.
  1857. * \param input_length Size of the \p input buffer in bytes.
  1858. * \param[out] output Buffer where the output is to be written.
  1859. * \param output_size Size of the \p output buffer in bytes.
  1860. * \param[out] output_length On success, the number of bytes
  1861. * that make up the returned output.
  1862. *
  1863. * \retval #PSA_SUCCESS
  1864. * Success.
  1865. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  1866. * The size of the \p output buffer is too small.
  1867. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1868. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1869. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1870. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1871. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  1872. * \retval #PSA_ERROR_BAD_STATE
  1873. * The operation state is not valid (it must be active, with an IV set
  1874. * if required for the algorithm), or the library has not been
  1875. * previously initialized by psa_crypto_init().
  1876. * It is implementation-dependent whether a failure to initialize
  1877. * results in this error code.
  1878. */
  1879. psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
  1880. const uint8_t *input,
  1881. size_t input_length,
  1882. uint8_t *output,
  1883. size_t output_size,
  1884. size_t *output_length);
  1885. /** Finish encrypting or decrypting a message in a cipher operation.
  1886. *
  1887. * The application must call psa_cipher_encrypt_setup() or
  1888. * psa_cipher_decrypt_setup() before calling this function. The choice
  1889. * of setup function determines whether this function encrypts or
  1890. * decrypts its input.
  1891. *
  1892. * This function finishes the encryption or decryption of the message
  1893. * formed by concatenating the inputs passed to preceding calls to
  1894. * psa_cipher_update().
  1895. *
  1896. * When this function returns successfully, the operation becomes inactive.
  1897. * If this function returns an error status, the operation enters an error
  1898. * state and must be aborted by calling psa_cipher_abort().
  1899. *
  1900. * \param[in,out] operation Active cipher operation.
  1901. * \param[out] output Buffer where the output is to be written.
  1902. * \param output_size Size of the \p output buffer in bytes.
  1903. * \param[out] output_length On success, the number of bytes
  1904. * that make up the returned output.
  1905. *
  1906. * \retval #PSA_SUCCESS
  1907. * Success.
  1908. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1909. * The total input size passed to this operation is not valid for
  1910. * this particular algorithm. For example, the algorithm is a based
  1911. * on block cipher and requires a whole number of blocks, but the
  1912. * total input size is not a multiple of the block size.
  1913. * \retval #PSA_ERROR_INVALID_PADDING
  1914. * This is a decryption operation for an algorithm that includes
  1915. * padding, and the ciphertext does not contain valid padding.
  1916. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  1917. * The size of the \p output buffer is too small.
  1918. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1919. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1920. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1921. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1922. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  1923. * \retval #PSA_ERROR_BAD_STATE
  1924. * The operation state is not valid (it must be active, with an IV set
  1925. * if required for the algorithm), or the library has not been
  1926. * previously initialized by psa_crypto_init().
  1927. * It is implementation-dependent whether a failure to initialize
  1928. * results in this error code.
  1929. */
  1930. psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
  1931. uint8_t *output,
  1932. size_t output_size,
  1933. size_t *output_length);
  1934. /** Abort a cipher operation.
  1935. *
  1936. * Aborting an operation frees all associated resources except for the
  1937. * \p operation structure itself. Once aborted, the operation object
  1938. * can be reused for another operation by calling
  1939. * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
  1940. *
  1941. * You may call this function any time after the operation object has
  1942. * been initialized as described in #psa_cipher_operation_t.
  1943. *
  1944. * In particular, calling psa_cipher_abort() after the operation has been
  1945. * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
  1946. * is safe and has no effect.
  1947. *
  1948. * \param[in,out] operation Initialized cipher operation.
  1949. *
  1950. * \retval #PSA_SUCCESS \emptydescription
  1951. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1952. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  1953. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1954. * \retval #PSA_ERROR_BAD_STATE
  1955. * The library has not been previously initialized by psa_crypto_init().
  1956. * It is implementation-dependent whether a failure to initialize
  1957. * results in this error code.
  1958. */
  1959. psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
  1960. /**@}*/
  1961. /** \defgroup aead Authenticated encryption with associated data (AEAD)
  1962. * @{
  1963. */
  1964. /** Process an authenticated encryption operation.
  1965. *
  1966. * \param key Identifier of the key to use for the
  1967. * operation. It must allow the usage
  1968. * #PSA_KEY_USAGE_ENCRYPT.
  1969. * \param alg The AEAD algorithm to compute
  1970. * (\c PSA_ALG_XXX value such that
  1971. * #PSA_ALG_IS_AEAD(\p alg) is true).
  1972. * \param[in] nonce Nonce or IV to use.
  1973. * \param nonce_length Size of the \p nonce buffer in bytes.
  1974. * \param[in] additional_data Additional data that will be authenticated
  1975. * but not encrypted.
  1976. * \param additional_data_length Size of \p additional_data in bytes.
  1977. * \param[in] plaintext Data that will be authenticated and
  1978. * encrypted.
  1979. * \param plaintext_length Size of \p plaintext in bytes.
  1980. * \param[out] ciphertext Output buffer for the authenticated and
  1981. * encrypted data. The additional data is not
  1982. * part of this output. For algorithms where the
  1983. * encrypted data and the authentication tag
  1984. * are defined as separate outputs, the
  1985. * authentication tag is appended to the
  1986. * encrypted data.
  1987. * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
  1988. * This must be appropriate for the selected
  1989. * algorithm and key:
  1990. * - A sufficient output size is
  1991. * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type,
  1992. * \p alg, \p plaintext_length) where
  1993. * \c key_type is the type of \p key.
  1994. * - #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p
  1995. * plaintext_length) evaluates to the maximum
  1996. * ciphertext size of any supported AEAD
  1997. * encryption.
  1998. * \param[out] ciphertext_length On success, the size of the output
  1999. * in the \p ciphertext buffer.
  2000. *
  2001. * \retval #PSA_SUCCESS
  2002. * Success.
  2003. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  2004. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  2005. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2006. * \p key is not compatible with \p alg.
  2007. * \retval #PSA_ERROR_NOT_SUPPORTED
  2008. * \p alg is not supported or is not an AEAD algorithm.
  2009. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  2010. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  2011. * \p ciphertext_size is too small.
  2012. * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type, \p alg,
  2013. * \p plaintext_length) or
  2014. * #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length) can be used to
  2015. * determine the required buffer size.
  2016. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  2017. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  2018. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  2019. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  2020. * \retval #PSA_ERROR_BAD_STATE
  2021. * The library has not been previously initialized by psa_crypto_init().
  2022. * It is implementation-dependent whether a failure to initialize
  2023. * results in this error code.
  2024. */
  2025. psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
  2026. psa_algorithm_t alg,
  2027. const uint8_t *nonce,
  2028. size_t nonce_length,
  2029. const uint8_t *additional_data,
  2030. size_t additional_data_length,
  2031. const uint8_t *plaintext,
  2032. size_t plaintext_length,
  2033. uint8_t *ciphertext,
  2034. size_t ciphertext_size,
  2035. size_t *ciphertext_length);
  2036. /** Process an authenticated decryption operation.
  2037. *
  2038. * \param key Identifier of the key to use for the
  2039. * operation. It must allow the usage
  2040. * #PSA_KEY_USAGE_DECRYPT.
  2041. * \param alg The AEAD algorithm to compute
  2042. * (\c PSA_ALG_XXX value such that
  2043. * #PSA_ALG_IS_AEAD(\p alg) is true).
  2044. * \param[in] nonce Nonce or IV to use.
  2045. * \param nonce_length Size of the \p nonce buffer in bytes.
  2046. * \param[in] additional_data Additional data that has been authenticated
  2047. * but not encrypted.
  2048. * \param additional_data_length Size of \p additional_data in bytes.
  2049. * \param[in] ciphertext Data that has been authenticated and
  2050. * encrypted. For algorithms where the
  2051. * encrypted data and the authentication tag
  2052. * are defined as separate inputs, the buffer
  2053. * must contain the encrypted data followed
  2054. * by the authentication tag.
  2055. * \param ciphertext_length Size of \p ciphertext in bytes.
  2056. * \param[out] plaintext Output buffer for the decrypted data.
  2057. * \param plaintext_size Size of the \p plaintext buffer in bytes.
  2058. * This must be appropriate for the selected
  2059. * algorithm and key:
  2060. * - A sufficient output size is
  2061. * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type,
  2062. * \p alg, \p ciphertext_length) where
  2063. * \c key_type is the type of \p key.
  2064. * - #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p
  2065. * ciphertext_length) evaluates to the maximum
  2066. * plaintext size of any supported AEAD
  2067. * decryption.
  2068. * \param[out] plaintext_length On success, the size of the output
  2069. * in the \p plaintext buffer.
  2070. *
  2071. * \retval #PSA_SUCCESS
  2072. * Success.
  2073. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  2074. * \retval #PSA_ERROR_INVALID_SIGNATURE
  2075. * The ciphertext is not authentic.
  2076. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  2077. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2078. * \p key is not compatible with \p alg.
  2079. * \retval #PSA_ERROR_NOT_SUPPORTED
  2080. * \p alg is not supported or is not an AEAD algorithm.
  2081. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  2082. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  2083. * \p plaintext_size is too small.
  2084. * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type, \p alg,
  2085. * \p ciphertext_length) or
  2086. * #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length) can be used
  2087. * to determine the required buffer size.
  2088. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  2089. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  2090. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  2091. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  2092. * \retval #PSA_ERROR_BAD_STATE
  2093. * The library has not been previously initialized by psa_crypto_init().
  2094. * It is implementation-dependent whether a failure to initialize
  2095. * results in this error code.
  2096. */
  2097. psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
  2098. psa_algorithm_t alg,
  2099. const uint8_t *nonce,
  2100. size_t nonce_length,
  2101. const uint8_t *additional_data,
  2102. size_t additional_data_length,
  2103. const uint8_t *ciphertext,
  2104. size_t ciphertext_length,
  2105. uint8_t *plaintext,
  2106. size_t plaintext_size,
  2107. size_t *plaintext_length);
  2108. /** The type of the state data structure for multipart AEAD operations.
  2109. *
  2110. * Before calling any function on an AEAD operation object, the application
  2111. * must initialize it by any of the following means:
  2112. * - Set the structure to all-bits-zero, for example:
  2113. * \code
  2114. * psa_aead_operation_t operation;
  2115. * memset(&operation, 0, sizeof(operation));
  2116. * \endcode
  2117. * - Initialize the structure to logical zero values, for example:
  2118. * \code
  2119. * psa_aead_operation_t operation = {0};
  2120. * \endcode
  2121. * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
  2122. * for example:
  2123. * \code
  2124. * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
  2125. * \endcode
  2126. * - Assign the result of the function psa_aead_operation_init()
  2127. * to the structure, for example:
  2128. * \code
  2129. * psa_aead_operation_t operation;
  2130. * operation = psa_aead_operation_init();
  2131. * \endcode
  2132. *
  2133. * This is an implementation-defined \c struct. Applications should not
  2134. * make any assumptions about the content of this structure.
  2135. * Implementation details can change in future versions without notice. */
  2136. typedef struct psa_aead_operation_s psa_aead_operation_t;
  2137. /** \def PSA_AEAD_OPERATION_INIT
  2138. *
  2139. * This macro returns a suitable initializer for an AEAD operation object of
  2140. * type #psa_aead_operation_t.
  2141. */
  2142. /** Return an initial value for an AEAD operation object.
  2143. */
  2144. static psa_aead_operation_t psa_aead_operation_init(void);
  2145. /** Set the key for a multipart authenticated encryption operation.
  2146. *
  2147. * The sequence of operations to encrypt a message with authentication
  2148. * is as follows:
  2149. * -# Allocate an operation object which will be passed to all the functions
  2150. * listed here.
  2151. * -# Initialize the operation object with one of the methods described in the
  2152. * documentation for #psa_aead_operation_t, e.g.
  2153. * #PSA_AEAD_OPERATION_INIT.
  2154. * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
  2155. * -# If needed, call psa_aead_set_lengths() to specify the length of the
  2156. * inputs to the subsequent calls to psa_aead_update_ad() and
  2157. * psa_aead_update(). See the documentation of psa_aead_set_lengths()
  2158. * for details.
  2159. * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
  2160. * generate or set the nonce. You should use
  2161. * psa_aead_generate_nonce() unless the protocol you are implementing
  2162. * requires a specific nonce value.
  2163. * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
  2164. * of the non-encrypted additional authenticated data each time.
  2165. * -# Call psa_aead_update() zero, one or more times, passing a fragment
  2166. * of the message to encrypt each time.
  2167. * -# Call psa_aead_finish().
  2168. *
  2169. * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
  2170. * the operation will need to be reset by a call to psa_aead_abort(). The
  2171. * application may call psa_aead_abort() at any time after the operation
  2172. * has been initialized.
  2173. *
  2174. * After a successful call to psa_aead_encrypt_setup(), the application must
  2175. * eventually terminate the operation. The following events terminate an
  2176. * operation:
  2177. * - A successful call to psa_aead_finish().
  2178. * - A call to psa_aead_abort().
  2179. *
  2180. * \param[in,out] operation The operation object to set up. It must have
  2181. * been initialized as per the documentation for
  2182. * #psa_aead_operation_t and not yet in use.
  2183. * \param key Identifier of the key to use for the operation.
  2184. * It must remain valid until the operation
  2185. * terminates. It must allow the usage
  2186. * #PSA_KEY_USAGE_ENCRYPT.
  2187. * \param alg The AEAD algorithm to compute
  2188. * (\c PSA_ALG_XXX value such that
  2189. * #PSA_ALG_IS_AEAD(\p alg) is true).
  2190. *
  2191. * \retval #PSA_SUCCESS
  2192. * Success.
  2193. * \retval #PSA_ERROR_BAD_STATE
  2194. * The operation state is not valid (it must be inactive), or
  2195. * the library has not been previously initialized by psa_crypto_init().
  2196. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  2197. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  2198. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2199. * \p key is not compatible with \p alg.
  2200. * \retval #PSA_ERROR_NOT_SUPPORTED
  2201. * \p alg is not supported or is not an AEAD algorithm.
  2202. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  2203. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  2204. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  2205. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  2206. * \retval #PSA_ERROR_STORAGE_FAILURE
  2207. * The library has not been previously initialized by psa_crypto_init().
  2208. * It is implementation-dependent whether a failure to initialize
  2209. * results in this error code.
  2210. */
  2211. psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
  2212. mbedtls_svc_key_id_t key,
  2213. psa_algorithm_t alg);
  2214. /** Set the key for a multipart authenticated decryption operation.
  2215. *
  2216. * The sequence of operations to decrypt a message with authentication
  2217. * is as follows:
  2218. * -# Allocate an operation object which will be passed to all the functions
  2219. * listed here.
  2220. * -# Initialize the operation object with one of the methods described in the
  2221. * documentation for #psa_aead_operation_t, e.g.
  2222. * #PSA_AEAD_OPERATION_INIT.
  2223. * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
  2224. * -# If needed, call psa_aead_set_lengths() to specify the length of the
  2225. * inputs to the subsequent calls to psa_aead_update_ad() and
  2226. * psa_aead_update(). See the documentation of psa_aead_set_lengths()
  2227. * for details.
  2228. * -# Call psa_aead_set_nonce() with the nonce for the decryption.
  2229. * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
  2230. * of the non-encrypted additional authenticated data each time.
  2231. * -# Call psa_aead_update() zero, one or more times, passing a fragment
  2232. * of the ciphertext to decrypt each time.
  2233. * -# Call psa_aead_verify().
  2234. *
  2235. * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
  2236. * the operation will need to be reset by a call to psa_aead_abort(). The
  2237. * application may call psa_aead_abort() at any time after the operation
  2238. * has been initialized.
  2239. *
  2240. * After a successful call to psa_aead_decrypt_setup(), the application must
  2241. * eventually terminate the operation. The following events terminate an
  2242. * operation:
  2243. * - A successful call to psa_aead_verify().
  2244. * - A call to psa_aead_abort().
  2245. *
  2246. * \param[in,out] operation The operation object to set up. It must have
  2247. * been initialized as per the documentation for
  2248. * #psa_aead_operation_t and not yet in use.
  2249. * \param key Identifier of the key to use for the operation.
  2250. * It must remain valid until the operation
  2251. * terminates. It must allow the usage
  2252. * #PSA_KEY_USAGE_DECRYPT.
  2253. * \param alg The AEAD algorithm to compute
  2254. * (\c PSA_ALG_XXX value such that
  2255. * #PSA_ALG_IS_AEAD(\p alg) is true).
  2256. *
  2257. * \retval #PSA_SUCCESS
  2258. * Success.
  2259. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  2260. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  2261. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2262. * \p key is not compatible with \p alg.
  2263. * \retval #PSA_ERROR_NOT_SUPPORTED
  2264. * \p alg is not supported or is not an AEAD algorithm.
  2265. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  2266. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  2267. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  2268. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  2269. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  2270. * \retval #PSA_ERROR_BAD_STATE
  2271. * The operation state is not valid (it must be inactive), or the
  2272. * library has not been previously initialized by psa_crypto_init().
  2273. * It is implementation-dependent whether a failure to initialize
  2274. * results in this error code.
  2275. */
  2276. psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
  2277. mbedtls_svc_key_id_t key,
  2278. psa_algorithm_t alg);
  2279. /** Generate a random nonce for an authenticated encryption operation.
  2280. *
  2281. * This function generates a random nonce for the authenticated encryption
  2282. * operation with an appropriate size for the chosen algorithm, key type
  2283. * and key size.
  2284. *
  2285. * The application must call psa_aead_encrypt_setup() before
  2286. * calling this function.
  2287. *
  2288. * If this function returns an error status, the operation enters an error
  2289. * state and must be aborted by calling psa_aead_abort().
  2290. *
  2291. * \param[in,out] operation Active AEAD operation.
  2292. * \param[out] nonce Buffer where the generated nonce is to be
  2293. * written.
  2294. * \param nonce_size Size of the \p nonce buffer in bytes.
  2295. * \param[out] nonce_length On success, the number of bytes of the
  2296. * generated nonce.
  2297. *
  2298. * \retval #PSA_SUCCESS
  2299. * Success.
  2300. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  2301. * The size of the \p nonce buffer is too small.
  2302. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  2303. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  2304. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  2305. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  2306. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  2307. * \retval #PSA_ERROR_BAD_STATE
  2308. * The operation state is not valid (it must be an active aead encrypt
  2309. * operation, with no nonce set), or the library has not been
  2310. * previously initialized by psa_crypto_init().
  2311. * It is implementation-dependent whether a failure to initialize
  2312. * results in this error code.
  2313. */
  2314. psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
  2315. uint8_t *nonce,
  2316. size_t nonce_size,
  2317. size_t *nonce_length);
  2318. /** Set the nonce for an authenticated encryption or decryption operation.
  2319. *
  2320. * This function sets the nonce for the authenticated
  2321. * encryption or decryption operation.
  2322. *
  2323. * The application must call psa_aead_encrypt_setup() or
  2324. * psa_aead_decrypt_setup() before calling this function.
  2325. *
  2326. * If this function returns an error status, the operation enters an error
  2327. * state and must be aborted by calling psa_aead_abort().
  2328. *
  2329. * \note When encrypting, applications should use psa_aead_generate_nonce()
  2330. * instead of this function, unless implementing a protocol that requires
  2331. * a non-random IV.
  2332. *
  2333. * \param[in,out] operation Active AEAD operation.
  2334. * \param[in] nonce Buffer containing the nonce to use.
  2335. * \param nonce_length Size of the nonce in bytes.
  2336. *
  2337. * \retval #PSA_SUCCESS
  2338. * Success.
  2339. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2340. * The size of \p nonce is not acceptable for the chosen algorithm.
  2341. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  2342. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  2343. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  2344. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  2345. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  2346. * \retval #PSA_ERROR_BAD_STATE
  2347. * The operation state is not valid (it must be active, with no nonce
  2348. * set), or the library has not been previously initialized
  2349. * by psa_crypto_init().
  2350. * It is implementation-dependent whether a failure to initialize
  2351. * results in this error code.
  2352. */
  2353. psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
  2354. const uint8_t *nonce,
  2355. size_t nonce_length);
  2356. /** Declare the lengths of the message and additional data for AEAD.
  2357. *
  2358. * The application must call this function before calling
  2359. * psa_aead_update_ad() or psa_aead_update() if the algorithm for
  2360. * the operation requires it. If the algorithm does not require it,
  2361. * calling this function is optional, but if this function is called
  2362. * then the implementation must enforce the lengths.
  2363. *
  2364. * You may call this function before or after setting the nonce with
  2365. * psa_aead_set_nonce() or psa_aead_generate_nonce().
  2366. *
  2367. * - For #PSA_ALG_CCM, calling this function is required.
  2368. * - For the other AEAD algorithms defined in this specification, calling
  2369. * this function is not required.
  2370. * - For vendor-defined algorithm, refer to the vendor documentation.
  2371. *
  2372. * If this function returns an error status, the operation enters an error
  2373. * state and must be aborted by calling psa_aead_abort().
  2374. *
  2375. * \param[in,out] operation Active AEAD operation.
  2376. * \param ad_length Size of the non-encrypted additional
  2377. * authenticated data in bytes.
  2378. * \param plaintext_length Size of the plaintext to encrypt in bytes.
  2379. *
  2380. * \retval #PSA_SUCCESS
  2381. * Success.
  2382. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2383. * At least one of the lengths is not acceptable for the chosen
  2384. * algorithm.
  2385. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  2386. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  2387. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  2388. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  2389. * \retval #PSA_ERROR_BAD_STATE
  2390. * The operation state is not valid (it must be active, and
  2391. * psa_aead_update_ad() and psa_aead_update() must not have been
  2392. * called yet), or the library has not been previously initialized
  2393. * by psa_crypto_init().
  2394. * It is implementation-dependent whether a failure to initialize
  2395. * results in this error code.
  2396. */
  2397. psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
  2398. size_t ad_length,
  2399. size_t plaintext_length);
  2400. /** Pass additional data to an active AEAD operation.
  2401. *
  2402. * Additional data is authenticated, but not encrypted.
  2403. *
  2404. * You may call this function multiple times to pass successive fragments
  2405. * of the additional data. You may not call this function after passing
  2406. * data to encrypt or decrypt with psa_aead_update().
  2407. *
  2408. * Before calling this function, you must:
  2409. * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
  2410. * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
  2411. *
  2412. * If this function returns an error status, the operation enters an error
  2413. * state and must be aborted by calling psa_aead_abort().
  2414. *
  2415. * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
  2416. * there is no guarantee that the input is valid. Therefore, until
  2417. * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
  2418. * treat the input as untrusted and prepare to undo any action that
  2419. * depends on the input if psa_aead_verify() returns an error status.
  2420. *
  2421. * \param[in,out] operation Active AEAD operation.
  2422. * \param[in] input Buffer containing the fragment of
  2423. * additional data.
  2424. * \param input_length Size of the \p input buffer in bytes.
  2425. *
  2426. * \retval #PSA_SUCCESS
  2427. * Success.
  2428. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2429. * The total input length overflows the additional data length that
  2430. * was previously specified with psa_aead_set_lengths().
  2431. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  2432. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  2433. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  2434. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  2435. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  2436. * \retval #PSA_ERROR_BAD_STATE
  2437. * The operation state is not valid (it must be active, have a nonce
  2438. * set, have lengths set if required by the algorithm, and
  2439. * psa_aead_update() must not have been called yet), or the library
  2440. * has not been previously initialized by psa_crypto_init().
  2441. * It is implementation-dependent whether a failure to initialize
  2442. * results in this error code.
  2443. */
  2444. psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
  2445. const uint8_t *input,
  2446. size_t input_length);
  2447. /** Encrypt or decrypt a message fragment in an active AEAD operation.
  2448. *
  2449. * Before calling this function, you must:
  2450. * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
  2451. * The choice of setup function determines whether this function
  2452. * encrypts or decrypts its input.
  2453. * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
  2454. * 3. Call psa_aead_update_ad() to pass all the additional data.
  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. * - Do not use the output in any way other than storing it in a
  2463. * confidential location. If you take any action that depends
  2464. * on the tentative decrypted data, this action will need to be
  2465. * undone if the input turns out not to be valid. Furthermore,
  2466. * if an adversary can observe that this action took place
  2467. * (for example through timing), they may be able to use this
  2468. * fact as an oracle to decrypt any message encrypted with the
  2469. * same key.
  2470. * - In particular, do not copy the output anywhere but to a
  2471. * memory or storage space that you have exclusive access to.
  2472. *
  2473. * This function does not require the input to be aligned to any
  2474. * particular block boundary. If the implementation can only process
  2475. * a whole block at a time, it must consume all the input provided, but
  2476. * it may delay the end of the corresponding output until a subsequent
  2477. * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
  2478. * provides sufficient input. The amount of data that can be delayed
  2479. * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
  2480. *
  2481. * \param[in,out] operation Active AEAD operation.
  2482. * \param[in] input Buffer containing the message fragment to
  2483. * encrypt or decrypt.
  2484. * \param input_length Size of the \p input buffer in bytes.
  2485. * \param[out] output Buffer where the output is to be written.
  2486. * \param output_size Size of the \p output buffer in bytes.
  2487. * This must be appropriate for the selected
  2488. * algorithm and key:
  2489. * - A sufficient output size is
  2490. * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type,
  2491. * \c alg, \p input_length) where
  2492. * \c key_type is the type of key and \c alg is
  2493. * the algorithm that were used to set up the
  2494. * operation.
  2495. * - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p
  2496. * input_length) evaluates to the maximum
  2497. * output size of any supported AEAD
  2498. * algorithm.
  2499. * \param[out] output_length On success, the number of bytes
  2500. * that make up the returned output.
  2501. *
  2502. * \retval #PSA_SUCCESS
  2503. * Success.
  2504. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  2505. * The size of the \p output buffer is too small.
  2506. * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or
  2507. * #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to
  2508. * determine the required buffer size.
  2509. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2510. * The total length of input to psa_aead_update_ad() so far is
  2511. * less than the additional data length that was previously
  2512. * specified with psa_aead_set_lengths(), or
  2513. * the total input length overflows the plaintext length that
  2514. * was previously specified with psa_aead_set_lengths().
  2515. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  2516. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  2517. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  2518. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  2519. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  2520. * \retval #PSA_ERROR_BAD_STATE
  2521. * The operation state is not valid (it must be active, have a nonce
  2522. * set, and have lengths set if required by the algorithm), or the
  2523. * library has not been previously initialized by psa_crypto_init().
  2524. * It is implementation-dependent whether a failure to initialize
  2525. * results in this error code.
  2526. */
  2527. psa_status_t psa_aead_update(psa_aead_operation_t *operation,
  2528. const uint8_t *input,
  2529. size_t input_length,
  2530. uint8_t *output,
  2531. size_t output_size,
  2532. size_t *output_length);
  2533. /** Finish encrypting a message in an AEAD operation.
  2534. *
  2535. * The operation must have been set up with psa_aead_encrypt_setup().
  2536. *
  2537. * This function finishes the authentication of the additional data
  2538. * formed by concatenating the inputs passed to preceding calls to
  2539. * psa_aead_update_ad() with the plaintext formed by concatenating the
  2540. * inputs passed to preceding calls to psa_aead_update().
  2541. *
  2542. * This function has two output buffers:
  2543. * - \p ciphertext contains trailing ciphertext that was buffered from
  2544. * preceding calls to psa_aead_update().
  2545. * - \p tag contains the authentication tag.
  2546. *
  2547. * When this function returns successfully, the operation becomes inactive.
  2548. * If this function returns an error status, the operation enters an error
  2549. * state and must be aborted by calling psa_aead_abort().
  2550. *
  2551. * \param[in,out] operation Active AEAD operation.
  2552. * \param[out] ciphertext Buffer where the last part of the ciphertext
  2553. * is to be written.
  2554. * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
  2555. * This must be appropriate for the selected
  2556. * algorithm and key:
  2557. * - A sufficient output size is
  2558. * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type,
  2559. * \c alg) where \c key_type is the type of key
  2560. * and \c alg is the algorithm that were used to
  2561. * set up the operation.
  2562. * - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to
  2563. * the maximum output size of any supported AEAD
  2564. * algorithm.
  2565. * \param[out] ciphertext_length On success, the number of bytes of
  2566. * returned ciphertext.
  2567. * \param[out] tag Buffer where the authentication tag is
  2568. * to be written.
  2569. * \param tag_size Size of the \p tag buffer in bytes.
  2570. * This must be appropriate for the selected
  2571. * algorithm and key:
  2572. * - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c
  2573. * key_type, \c key_bits, \c alg) where
  2574. * \c key_type and \c key_bits are the type and
  2575. * bit-size of the key, and \c alg is the
  2576. * algorithm that were used in the call to
  2577. * psa_aead_encrypt_setup().
  2578. * - #PSA_AEAD_TAG_MAX_SIZE evaluates to the
  2579. * maximum tag size of any supported AEAD
  2580. * algorithm.
  2581. * \param[out] tag_length On success, the number of bytes
  2582. * that make up the returned tag.
  2583. *
  2584. * \retval #PSA_SUCCESS
  2585. * Success.
  2586. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  2587. * The size of the \p ciphertext or \p tag buffer is too small.
  2588. * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, \c alg) or
  2589. * #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE can be used to determine the
  2590. * required \p ciphertext buffer size. #PSA_AEAD_TAG_LENGTH(\c key_type,
  2591. * \c key_bits, \c alg) or #PSA_AEAD_TAG_MAX_SIZE can be used to
  2592. * determine the required \p tag buffer size.
  2593. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2594. * The total length of input to psa_aead_update_ad() so far is
  2595. * less than the additional data length that was previously
  2596. * specified with psa_aead_set_lengths(), or
  2597. * the total length of input to psa_aead_update() so far is
  2598. * less than the plaintext length that was previously
  2599. * specified with psa_aead_set_lengths().
  2600. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  2601. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  2602. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  2603. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  2604. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  2605. * \retval #PSA_ERROR_BAD_STATE
  2606. * The operation state is not valid (it must be an active encryption
  2607. * operation with a nonce set), or the library has not been previously
  2608. * initialized by psa_crypto_init().
  2609. * It is implementation-dependent whether a failure to initialize
  2610. * results in this error code.
  2611. */
  2612. psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
  2613. uint8_t *ciphertext,
  2614. size_t ciphertext_size,
  2615. size_t *ciphertext_length,
  2616. uint8_t *tag,
  2617. size_t tag_size,
  2618. size_t *tag_length);
  2619. /** Finish authenticating and decrypting a message in an AEAD operation.
  2620. *
  2621. * The operation must have been set up with psa_aead_decrypt_setup().
  2622. *
  2623. * This function finishes the authenticated decryption of the message
  2624. * components:
  2625. *
  2626. * - The additional data consisting of the concatenation of the inputs
  2627. * passed to preceding calls to psa_aead_update_ad().
  2628. * - The ciphertext consisting of the concatenation of the inputs passed to
  2629. * preceding calls to psa_aead_update().
  2630. * - The tag passed to this function call.
  2631. *
  2632. * If the authentication tag is correct, this function outputs any remaining
  2633. * plaintext and reports success. If the authentication tag is not correct,
  2634. * this function returns #PSA_ERROR_INVALID_SIGNATURE.
  2635. *
  2636. * When this function returns successfully, the operation becomes inactive.
  2637. * If this function returns an error status, the operation enters an error
  2638. * state and must be aborted by calling psa_aead_abort().
  2639. *
  2640. * \note Implementations shall make the best effort to ensure that the
  2641. * comparison between the actual tag and the expected tag is performed
  2642. * in constant time.
  2643. *
  2644. * \param[in,out] operation Active AEAD operation.
  2645. * \param[out] plaintext Buffer where the last part of the plaintext
  2646. * is to be written. This is the remaining data
  2647. * from previous calls to psa_aead_update()
  2648. * that could not be processed until the end
  2649. * of the input.
  2650. * \param plaintext_size Size of the \p plaintext buffer in bytes.
  2651. * This must be appropriate for the selected algorithm and key:
  2652. * - A sufficient output size is
  2653. * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type,
  2654. * \c alg) where \c key_type is the type of key
  2655. * and \c alg is the algorithm that were used to
  2656. * set up the operation.
  2657. * - #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE evaluates to
  2658. * the maximum output size of any supported AEAD
  2659. * algorithm.
  2660. * \param[out] plaintext_length On success, the number of bytes of
  2661. * returned plaintext.
  2662. * \param[in] tag Buffer containing the authentication tag.
  2663. * \param tag_length Size of the \p tag buffer in bytes.
  2664. *
  2665. * \retval #PSA_SUCCESS
  2666. * Success.
  2667. * \retval #PSA_ERROR_INVALID_SIGNATURE
  2668. * The calculations were successful, but the authentication tag is
  2669. * not correct.
  2670. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  2671. * The size of the \p plaintext buffer is too small.
  2672. * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, \c alg) or
  2673. * #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE can be used to determine the
  2674. * required buffer size.
  2675. * \retval #PSA_ERROR_INVALID_ARGUMENT
  2676. * The total length of input to psa_aead_update_ad() so far is
  2677. * less than the additional data length that was previously
  2678. * specified with psa_aead_set_lengths(), or
  2679. * the total length of input to psa_aead_update() so far is
  2680. * less than the plaintext length that was previously
  2681. * specified with psa_aead_set_lengths().
  2682. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  2683. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  2684. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  2685. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  2686. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  2687. * \retval #PSA_ERROR_BAD_STATE
  2688. * The operation state is not valid (it must be an active decryption
  2689. * operation with a nonce set), or the library has not been previously
  2690. * initialized by psa_crypto_init().
  2691. * It is implementation-dependent whether a failure to initialize
  2692. * results in this error code.
  2693. */
  2694. psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
  2695. uint8_t *plaintext,
  2696. size_t plaintext_size,
  2697. size_t *plaintext_length,
  2698. const uint8_t *tag,
  2699. size_t tag_length);
  2700. /** Abort an AEAD operation.
  2701. *
  2702. * Aborting an operation frees all associated resources except for the
  2703. * \p operation structure itself. Once aborted, the operation object
  2704. * can be reused for another operation by calling
  2705. * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
  2706. *
  2707. * You may call this function any time after the operation object has
  2708. * been initialized as described in #psa_aead_operation_t.
  2709. *
  2710. * In particular, calling psa_aead_abort() after the operation has been
  2711. * terminated by a call to psa_aead_abort(), psa_aead_finish() or
  2712. * psa_aead_verify() is safe and has no effect.
  2713. *
  2714. * \param[in,out] operation Initialized AEAD operation.
  2715. *
  2716. * \retval #PSA_SUCCESS \emptydescription
  2717. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  2718. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  2719. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  2720. * \retval #PSA_ERROR_BAD_STATE
  2721. * The library has not been previously initialized by psa_crypto_init().
  2722. * It is implementation-dependent whether a failure to initialize
  2723. * results in this error code.
  2724. */
  2725. psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
  2726. /**@}*/
  2727. /** \defgroup asymmetric Asymmetric cryptography
  2728. * @{
  2729. */
  2730. /**
  2731. * \brief Sign a message with a private key. For hash-and-sign algorithms,
  2732. * this includes the hashing step.
  2733. *
  2734. * \note To perform a multi-part hash-and-sign signature algorithm, first use
  2735. * a multi-part hash operation and then pass the resulting hash to
  2736. * psa_sign_hash(). PSA_ALG_GET_HASH(\p alg) can be used to determine the
  2737. * hash algorithm to use.
  2738. *
  2739. * \param[in] key Identifier of the key to use for the operation.
  2740. * It must be an asymmetric key pair. The key must
  2741. * allow the usage #PSA_KEY_USAGE_SIGN_MESSAGE.
  2742. * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
  2743. * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
  2744. * is true), that is compatible with the type of
  2745. * \p key.
  2746. * \param[in] input The input message to sign.
  2747. * \param[in] input_length Size of the \p input buffer in bytes.
  2748. * \param[out] signature Buffer where the signature is to be written.
  2749. * \param[in] signature_size Size of the \p signature buffer in bytes. This
  2750. * must be appropriate for the selected
  2751. * algorithm and key:
  2752. * - The required signature size is
  2753. * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  2754. * where \c key_type and \c key_bits are the type and
  2755. * bit-size respectively of key.
  2756. * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
  2757. * maximum signature size of any supported
  2758. * signature algorithm.
  2759. * \param[out] signature_length On success, the number of bytes that make up
  2760. * the returned signature value.
  2761. *
  2762. * \retval #PSA_SUCCESS \emptydescription
  2763. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  2764. * \retval #PSA_ERROR_NOT_PERMITTED
  2765. * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
  2766. * or it does not permit the requested algorithm.
  2767. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  2768. * The size of the \p signature buffer is too small. You can
  2769. * determine a sufficient buffer size by calling
  2770. * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  2771. * where \c key_type and \c key_bits are the type and bit-size
  2772. * respectively of \p key.
  2773. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  2774. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  2775. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  2776. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  2777. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  2778. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  2779. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  2780. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  2781. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  2782. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  2783. * \retval #PSA_ERROR_BAD_STATE
  2784. * The library has not been previously initialized by psa_crypto_init().
  2785. * It is implementation-dependent whether a failure to initialize
  2786. * results in this error code.
  2787. */
  2788. psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
  2789. psa_algorithm_t alg,
  2790. const uint8_t *input,
  2791. size_t input_length,
  2792. uint8_t *signature,
  2793. size_t signature_size,
  2794. size_t *signature_length);
  2795. /** \brief Verify the signature of a message with a public key, using
  2796. * a hash-and-sign verification algorithm.
  2797. *
  2798. * \note To perform a multi-part hash-and-sign signature verification
  2799. * algorithm, first use a multi-part hash operation to hash the message
  2800. * and then pass the resulting hash to psa_verify_hash().
  2801. * PSA_ALG_GET_HASH(\p alg) can be used to determine the hash algorithm
  2802. * to use.
  2803. *
  2804. * \param[in] key Identifier of the key to use for the operation.
  2805. * It must be a public key or an asymmetric key
  2806. * pair. The key must allow the usage
  2807. * #PSA_KEY_USAGE_VERIFY_MESSAGE.
  2808. * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
  2809. * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
  2810. * is true), that is compatible with the type of
  2811. * \p key.
  2812. * \param[in] input The message whose signature is to be verified.
  2813. * \param[in] input_length Size of the \p input buffer in bytes.
  2814. * \param[in] signature Buffer containing the signature to verify.
  2815. * \param[in] signature_length Size of the \p signature buffer in bytes.
  2816. *
  2817. * \retval #PSA_SUCCESS \emptydescription
  2818. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  2819. * \retval #PSA_ERROR_NOT_PERMITTED
  2820. * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
  2821. * or it does not permit the requested algorithm.
  2822. * \retval #PSA_ERROR_INVALID_SIGNATURE
  2823. * The calculation was performed successfully, but the passed signature
  2824. * is not a valid signature.
  2825. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  2826. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  2827. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  2828. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  2829. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  2830. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  2831. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  2832. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  2833. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  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_verify_message(mbedtls_svc_key_id_t key,
  2840. psa_algorithm_t alg,
  2841. const uint8_t *input,
  2842. size_t input_length,
  2843. const uint8_t *signature,
  2844. size_t signature_length);
  2845. /**
  2846. * \brief Sign a hash or short message with a private key.
  2847. *
  2848. * Note that to perform a hash-and-sign signature algorithm, you must
  2849. * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
  2850. * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
  2851. * Then pass the resulting hash as the \p hash
  2852. * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
  2853. * to determine the hash algorithm to use.
  2854. *
  2855. * \param key Identifier of the key to use for the operation.
  2856. * It must be an asymmetric key pair. The key must
  2857. * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
  2858. * \param alg A signature algorithm (PSA_ALG_XXX
  2859. * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
  2860. * is true), that is compatible with
  2861. * the type of \p key.
  2862. * \param[in] hash The hash or message to sign.
  2863. * \param hash_length Size of the \p hash buffer in bytes.
  2864. * \param[out] signature Buffer where the signature is to be written.
  2865. * \param signature_size Size of the \p signature buffer in bytes.
  2866. * \param[out] signature_length On success, the number of bytes
  2867. * that make up the returned signature value.
  2868. *
  2869. * \retval #PSA_SUCCESS \emptydescription
  2870. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  2871. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  2872. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  2873. * The size of the \p signature buffer is too small. You can
  2874. * determine a sufficient buffer size by calling
  2875. * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  2876. * where \c key_type and \c key_bits are the type and bit-size
  2877. * respectively of \p key.
  2878. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  2879. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  2880. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  2881. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  2882. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  2883. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  2884. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  2885. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  2886. * \retval #PSA_ERROR_BAD_STATE
  2887. * The library has not been previously initialized by psa_crypto_init().
  2888. * It is implementation-dependent whether a failure to initialize
  2889. * results in this error code.
  2890. */
  2891. psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
  2892. psa_algorithm_t alg,
  2893. const uint8_t *hash,
  2894. size_t hash_length,
  2895. uint8_t *signature,
  2896. size_t signature_size,
  2897. size_t *signature_length);
  2898. /**
  2899. * \brief Verify the signature of a hash or short message using a public key.
  2900. *
  2901. * Note that to perform a hash-and-sign signature algorithm, you must
  2902. * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
  2903. * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
  2904. * Then pass the resulting hash as the \p hash
  2905. * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
  2906. * to determine the hash algorithm to use.
  2907. *
  2908. * \param key Identifier of the key to use for the operation. It
  2909. * must be a public key or an asymmetric key pair. The
  2910. * key must allow the usage
  2911. * #PSA_KEY_USAGE_VERIFY_HASH.
  2912. * \param alg A signature algorithm (PSA_ALG_XXX
  2913. * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
  2914. * is true), that is compatible with
  2915. * the type of \p key.
  2916. * \param[in] hash The hash or message whose signature is to be
  2917. * verified.
  2918. * \param hash_length Size of the \p hash buffer in bytes.
  2919. * \param[in] signature Buffer containing the signature to verify.
  2920. * \param signature_length Size of the \p signature buffer in bytes.
  2921. *
  2922. * \retval #PSA_SUCCESS
  2923. * The signature is valid.
  2924. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  2925. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  2926. * \retval #PSA_ERROR_INVALID_SIGNATURE
  2927. * The calculation was performed successfully, but the passed
  2928. * signature is not a valid signature.
  2929. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  2930. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  2931. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  2932. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  2933. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  2934. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  2935. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  2936. * \retval #PSA_ERROR_BAD_STATE
  2937. * The library has not been previously initialized by psa_crypto_init().
  2938. * It is implementation-dependent whether a failure to initialize
  2939. * results in this error code.
  2940. */
  2941. psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
  2942. psa_algorithm_t alg,
  2943. const uint8_t *hash,
  2944. size_t hash_length,
  2945. const uint8_t *signature,
  2946. size_t signature_length);
  2947. /**
  2948. * \brief Encrypt a short message with a public key.
  2949. *
  2950. * \param key Identifier of the key to use for the operation.
  2951. * It must be a public key or an asymmetric key
  2952. * pair. It must allow the usage
  2953. * #PSA_KEY_USAGE_ENCRYPT.
  2954. * \param alg An asymmetric encryption algorithm that is
  2955. * compatible with the type of \p key.
  2956. * \param[in] input The message to encrypt.
  2957. * \param input_length Size of the \p input buffer in bytes.
  2958. * \param[in] salt A salt or label, if supported by the
  2959. * encryption algorithm.
  2960. * If the algorithm does not support a
  2961. * salt, pass \c NULL.
  2962. * If the algorithm supports an optional
  2963. * salt and you do not want to pass a salt,
  2964. * pass \c NULL.
  2965. *
  2966. * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
  2967. * supported.
  2968. * \param salt_length Size of the \p salt buffer in bytes.
  2969. * If \p salt is \c NULL, pass 0.
  2970. * \param[out] output Buffer where the encrypted message is to
  2971. * be written.
  2972. * \param output_size Size of the \p output buffer in bytes.
  2973. * \param[out] output_length On success, the number of bytes
  2974. * that make up the returned output.
  2975. *
  2976. * \retval #PSA_SUCCESS \emptydescription
  2977. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  2978. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  2979. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  2980. * The size of the \p output buffer is too small. You can
  2981. * determine a sufficient buffer size by calling
  2982. * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  2983. * where \c key_type and \c key_bits are the type and bit-size
  2984. * respectively of \p key.
  2985. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  2986. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  2987. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  2988. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  2989. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  2990. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  2991. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  2992. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  2993. * \retval #PSA_ERROR_BAD_STATE
  2994. * The library has not been previously initialized by psa_crypto_init().
  2995. * It is implementation-dependent whether a failure to initialize
  2996. * results in this error code.
  2997. */
  2998. psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
  2999. psa_algorithm_t alg,
  3000. const uint8_t *input,
  3001. size_t input_length,
  3002. const uint8_t *salt,
  3003. size_t salt_length,
  3004. uint8_t *output,
  3005. size_t output_size,
  3006. size_t *output_length);
  3007. /**
  3008. * \brief Decrypt a short message with a private key.
  3009. *
  3010. * \param key Identifier of the key to use for the operation.
  3011. * It must be an asymmetric key pair. It must
  3012. * allow the usage #PSA_KEY_USAGE_DECRYPT.
  3013. * \param alg An asymmetric encryption algorithm that is
  3014. * compatible with the type of \p key.
  3015. * \param[in] input The message to decrypt.
  3016. * \param input_length Size of the \p input buffer in bytes.
  3017. * \param[in] salt A salt or label, if supported by the
  3018. * encryption algorithm.
  3019. * If the algorithm does not support a
  3020. * salt, pass \c NULL.
  3021. * If the algorithm supports an optional
  3022. * salt and you do not want to pass a salt,
  3023. * pass \c NULL.
  3024. *
  3025. * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
  3026. * supported.
  3027. * \param salt_length Size of the \p salt buffer in bytes.
  3028. * If \p salt is \c NULL, pass 0.
  3029. * \param[out] output Buffer where the decrypted message is to
  3030. * be written.
  3031. * \param output_size Size of the \c output buffer in bytes.
  3032. * \param[out] output_length On success, the number of bytes
  3033. * that make up the returned output.
  3034. *
  3035. * \retval #PSA_SUCCESS \emptydescription
  3036. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  3037. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  3038. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  3039. * The size of the \p output buffer is too small. You can
  3040. * determine a sufficient buffer size by calling
  3041. * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  3042. * where \c key_type and \c key_bits are the type and bit-size
  3043. * respectively of \p key.
  3044. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  3045. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  3046. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  3047. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  3048. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  3049. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  3050. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  3051. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  3052. * \retval #PSA_ERROR_INVALID_PADDING \emptydescription
  3053. * \retval #PSA_ERROR_BAD_STATE
  3054. * The library has not been previously initialized by psa_crypto_init().
  3055. * It is implementation-dependent whether a failure to initialize
  3056. * results in this error code.
  3057. */
  3058. psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
  3059. psa_algorithm_t alg,
  3060. const uint8_t *input,
  3061. size_t input_length,
  3062. const uint8_t *salt,
  3063. size_t salt_length,
  3064. uint8_t *output,
  3065. size_t output_size,
  3066. size_t *output_length);
  3067. /**@}*/
  3068. /** \defgroup key_derivation Key derivation and pseudorandom generation
  3069. * @{
  3070. */
  3071. /** The type of the state data structure for key derivation operations.
  3072. *
  3073. * Before calling any function on a key derivation operation object, the
  3074. * application must initialize it by any of the following means:
  3075. * - Set the structure to all-bits-zero, for example:
  3076. * \code
  3077. * psa_key_derivation_operation_t operation;
  3078. * memset(&operation, 0, sizeof(operation));
  3079. * \endcode
  3080. * - Initialize the structure to logical zero values, for example:
  3081. * \code
  3082. * psa_key_derivation_operation_t operation = {0};
  3083. * \endcode
  3084. * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
  3085. * for example:
  3086. * \code
  3087. * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
  3088. * \endcode
  3089. * - Assign the result of the function psa_key_derivation_operation_init()
  3090. * to the structure, for example:
  3091. * \code
  3092. * psa_key_derivation_operation_t operation;
  3093. * operation = psa_key_derivation_operation_init();
  3094. * \endcode
  3095. *
  3096. * This is an implementation-defined \c struct. Applications should not
  3097. * make any assumptions about the content of this structure.
  3098. * Implementation details can change in future versions without notice.
  3099. */
  3100. typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
  3101. /** \def PSA_KEY_DERIVATION_OPERATION_INIT
  3102. *
  3103. * This macro returns a suitable initializer for a key derivation operation
  3104. * object of type #psa_key_derivation_operation_t.
  3105. */
  3106. /** Return an initial value for a key derivation operation object.
  3107. */
  3108. static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
  3109. /** Set up a key derivation operation.
  3110. *
  3111. * A key derivation algorithm takes some inputs and uses them to generate
  3112. * a byte stream in a deterministic way.
  3113. * This byte stream can be used to produce keys and other
  3114. * cryptographic material.
  3115. *
  3116. * To derive a key:
  3117. * -# Start with an initialized object of type #psa_key_derivation_operation_t.
  3118. * -# Call psa_key_derivation_setup() to select the algorithm.
  3119. * -# Provide the inputs for the key derivation by calling
  3120. * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
  3121. * as appropriate. Which inputs are needed, in what order, and whether
  3122. * they may be keys and if so of what type depends on the algorithm.
  3123. * -# Optionally set the operation's maximum capacity with
  3124. * psa_key_derivation_set_capacity(). You may do this before, in the middle
  3125. * of or after providing inputs. For some algorithms, this step is mandatory
  3126. * because the output depends on the maximum capacity.
  3127. * -# To derive a key, call psa_key_derivation_output_key() or
  3128. * psa_key_derivation_output_key_custom().
  3129. * To derive a byte string for a different purpose, call
  3130. * psa_key_derivation_output_bytes().
  3131. * Successive calls to these functions use successive output bytes
  3132. * calculated by the key derivation algorithm.
  3133. * -# Clean up the key derivation operation object with
  3134. * psa_key_derivation_abort().
  3135. *
  3136. * If this function returns an error, the key derivation operation object is
  3137. * not changed.
  3138. *
  3139. * If an error occurs at any step after a call to psa_key_derivation_setup(),
  3140. * the operation will need to be reset by a call to psa_key_derivation_abort().
  3141. *
  3142. * Implementations must reject an attempt to derive a key of size 0.
  3143. *
  3144. * \param[in,out] operation The key derivation operation object
  3145. * to set up. It must
  3146. * have been initialized but not set up yet.
  3147. * \param alg The key derivation algorithm to compute
  3148. * (\c PSA_ALG_XXX value such that
  3149. * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
  3150. *
  3151. * \retval #PSA_SUCCESS
  3152. * Success.
  3153. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3154. * \c alg is not a key derivation algorithm.
  3155. * \retval #PSA_ERROR_NOT_SUPPORTED
  3156. * \c alg is not supported or is not a key derivation algorithm.
  3157. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  3158. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  3159. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  3160. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  3161. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  3162. * \retval #PSA_ERROR_BAD_STATE
  3163. * The operation state is not valid (it must be inactive), or
  3164. * the library has not been previously initialized by psa_crypto_init().
  3165. * It is implementation-dependent whether a failure to initialize
  3166. * results in this error code.
  3167. */
  3168. psa_status_t psa_key_derivation_setup(
  3169. psa_key_derivation_operation_t *operation,
  3170. psa_algorithm_t alg);
  3171. /** Retrieve the current capacity of a key derivation operation.
  3172. *
  3173. * The capacity of a key derivation is the maximum number of bytes that it can
  3174. * return. When you get *N* bytes of output from a key derivation operation,
  3175. * this reduces its capacity by *N*.
  3176. *
  3177. * \param[in] operation The operation to query.
  3178. * \param[out] capacity On success, the capacity of the operation.
  3179. *
  3180. * \retval #PSA_SUCCESS \emptydescription
  3181. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  3182. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  3183. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  3184. * \retval #PSA_ERROR_BAD_STATE
  3185. * The operation state is not valid (it must be active), or
  3186. * the library has not been previously initialized by psa_crypto_init().
  3187. * It is implementation-dependent whether a failure to initialize
  3188. * results in this error code.
  3189. */
  3190. psa_status_t psa_key_derivation_get_capacity(
  3191. const psa_key_derivation_operation_t *operation,
  3192. size_t *capacity);
  3193. /** Set the maximum capacity of a key derivation operation.
  3194. *
  3195. * The capacity of a key derivation operation is the maximum number of bytes
  3196. * that the key derivation operation can return from this point onwards.
  3197. *
  3198. * \param[in,out] operation The key derivation operation object to modify.
  3199. * \param capacity The new capacity of the operation.
  3200. * It must be less or equal to the operation's
  3201. * current capacity.
  3202. *
  3203. * \retval #PSA_SUCCESS \emptydescription
  3204. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3205. * \p capacity is larger than the operation's current capacity.
  3206. * In this case, the operation object remains valid and its capacity
  3207. * remains unchanged.
  3208. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  3209. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  3210. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  3211. * \retval #PSA_ERROR_BAD_STATE
  3212. * The operation state is not valid (it must be active), or the
  3213. * library has not been previously initialized by psa_crypto_init().
  3214. * It is implementation-dependent whether a failure to initialize
  3215. * results in this error code.
  3216. */
  3217. psa_status_t psa_key_derivation_set_capacity(
  3218. psa_key_derivation_operation_t *operation,
  3219. size_t capacity);
  3220. /** Use the maximum possible capacity for a key derivation operation.
  3221. *
  3222. * Use this value as the capacity argument when setting up a key derivation
  3223. * to indicate that the operation should have the maximum possible capacity.
  3224. * The value of the maximum possible capacity depends on the key derivation
  3225. * algorithm.
  3226. */
  3227. #define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t) (-1))
  3228. /** Provide an input for key derivation or key agreement.
  3229. *
  3230. * Which inputs are required and in what order depends on the algorithm.
  3231. * Refer to the documentation of each key derivation or key agreement
  3232. * algorithm for information.
  3233. *
  3234. * This function passes direct inputs, which is usually correct for
  3235. * non-secret inputs. To pass a secret input, which should be in a key
  3236. * object, call psa_key_derivation_input_key() instead of this function.
  3237. * Refer to the documentation of individual step types
  3238. * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
  3239. * for more information.
  3240. *
  3241. * If this function returns an error status, the operation enters an error
  3242. * state and must be aborted by calling psa_key_derivation_abort().
  3243. *
  3244. * \param[in,out] operation The key derivation operation object to use.
  3245. * It must have been set up with
  3246. * psa_key_derivation_setup() and must not
  3247. * have produced any output yet.
  3248. * \param step Which step the input data is for.
  3249. * \param[in] data Input data to use.
  3250. * \param data_length Size of the \p data buffer in bytes.
  3251. *
  3252. * \retval #PSA_SUCCESS
  3253. * Success.
  3254. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3255. * \c step is not compatible with the operation's algorithm, or
  3256. * \c step does not allow direct inputs.
  3257. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  3258. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  3259. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  3260. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  3261. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  3262. * \retval #PSA_ERROR_BAD_STATE
  3263. * The operation state is not valid for this input \p step, or
  3264. * the library has not been previously initialized by psa_crypto_init().
  3265. * It is implementation-dependent whether a failure to initialize
  3266. * results in this error code.
  3267. */
  3268. psa_status_t psa_key_derivation_input_bytes(
  3269. psa_key_derivation_operation_t *operation,
  3270. psa_key_derivation_step_t step,
  3271. const uint8_t *data,
  3272. size_t data_length);
  3273. /** Provide a numeric input for key derivation or key agreement.
  3274. *
  3275. * Which inputs are required and in what order depends on the algorithm.
  3276. * However, when an algorithm requires a particular order, numeric inputs
  3277. * usually come first as they tend to be configuration parameters.
  3278. * Refer to the documentation of each key derivation or key agreement
  3279. * algorithm for information.
  3280. *
  3281. * This function is used for inputs which are fixed-size non-negative
  3282. * integers.
  3283. *
  3284. * If this function returns an error status, the operation enters an error
  3285. * state and must be aborted by calling psa_key_derivation_abort().
  3286. *
  3287. * \param[in,out] operation The key derivation operation object to use.
  3288. * It must have been set up with
  3289. * psa_key_derivation_setup() and must not
  3290. * have produced any output yet.
  3291. * \param step Which step the input data is for.
  3292. * \param[in] value The value of the numeric input.
  3293. *
  3294. * \retval #PSA_SUCCESS
  3295. * Success.
  3296. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3297. * \c step is not compatible with the operation's algorithm, or
  3298. * \c step does not allow numeric inputs.
  3299. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  3300. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  3301. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  3302. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  3303. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  3304. * \retval #PSA_ERROR_BAD_STATE
  3305. * The operation state is not valid for this input \p step, or
  3306. * the library has not been previously initialized by psa_crypto_init().
  3307. * It is implementation-dependent whether a failure to initialize
  3308. * results in this error code.
  3309. */
  3310. psa_status_t psa_key_derivation_input_integer(
  3311. psa_key_derivation_operation_t *operation,
  3312. psa_key_derivation_step_t step,
  3313. uint64_t value);
  3314. /** Provide an input for key derivation in the form of a key.
  3315. *
  3316. * Which inputs are required and in what order depends on the algorithm.
  3317. * Refer to the documentation of each key derivation or key agreement
  3318. * algorithm for information.
  3319. *
  3320. * This function obtains input from a key object, which is usually correct for
  3321. * secret inputs or for non-secret personalization strings kept in the key
  3322. * store. To pass a non-secret parameter which is not in the key store,
  3323. * call psa_key_derivation_input_bytes() instead of this function.
  3324. * Refer to the documentation of individual step types
  3325. * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
  3326. * for more information.
  3327. *
  3328. * If this function returns an error status, the operation enters an error
  3329. * state and must be aborted by calling psa_key_derivation_abort().
  3330. *
  3331. * \param[in,out] operation The key derivation operation object to use.
  3332. * It must have been set up with
  3333. * psa_key_derivation_setup() and must not
  3334. * have produced any output yet.
  3335. * \param step Which step the input data is for.
  3336. * \param key Identifier of the key. It must have an
  3337. * appropriate type for step and must allow the
  3338. * usage #PSA_KEY_USAGE_DERIVE or
  3339. * #PSA_KEY_USAGE_VERIFY_DERIVATION (see note)
  3340. * and the algorithm used by the operation.
  3341. *
  3342. * \note Once all inputs steps are completed, the operations will allow:
  3343. * - psa_key_derivation_output_bytes() if each input was either a direct input
  3344. * or a key with #PSA_KEY_USAGE_DERIVE set;
  3345. * - psa_key_derivation_output_key() or psa_key_derivation_output_key_custom()
  3346. * if the input for step
  3347. * #PSA_KEY_DERIVATION_INPUT_SECRET or #PSA_KEY_DERIVATION_INPUT_PASSWORD
  3348. * was from a key slot with #PSA_KEY_USAGE_DERIVE and each other input was
  3349. * either a direct input or a key with #PSA_KEY_USAGE_DERIVE set;
  3350. * - psa_key_derivation_verify_bytes() if each input was either a direct input
  3351. * or a key with #PSA_KEY_USAGE_VERIFY_DERIVATION set;
  3352. * - psa_key_derivation_verify_key() under the same conditions as
  3353. * psa_key_derivation_verify_bytes().
  3354. *
  3355. * \retval #PSA_SUCCESS
  3356. * Success.
  3357. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  3358. * \retval #PSA_ERROR_NOT_PERMITTED
  3359. * The key allows neither #PSA_KEY_USAGE_DERIVE nor
  3360. * #PSA_KEY_USAGE_VERIFY_DERIVATION, or it doesn't allow this
  3361. * algorithm.
  3362. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3363. * \c step is not compatible with the operation's algorithm, or
  3364. * \c step does not allow key inputs of the given type
  3365. * or does not allow key inputs at all.
  3366. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  3367. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  3368. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  3369. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  3370. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  3371. * \retval #PSA_ERROR_BAD_STATE
  3372. * The operation state is not valid for this input \p step, or
  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_INVALID_HANDLE \emptydescription
  3425. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  3426. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3427. * \c private_key is not compatible with \c alg,
  3428. * or \p peer_key is not valid for \c alg or not compatible with
  3429. * \c private_key, or \c step does not allow an input resulting
  3430. * from a key agreement.
  3431. * \retval #PSA_ERROR_NOT_SUPPORTED
  3432. * \c alg is not supported or is not a key derivation algorithm.
  3433. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  3434. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  3435. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  3436. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  3437. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  3438. * \retval #PSA_ERROR_BAD_STATE
  3439. * The operation state is not valid for this key agreement \p step,
  3440. * or the library has not been previously initialized by psa_crypto_init().
  3441. * It is implementation-dependent whether a failure to initialize
  3442. * results in this error code.
  3443. */
  3444. psa_status_t psa_key_derivation_key_agreement(
  3445. psa_key_derivation_operation_t *operation,
  3446. psa_key_derivation_step_t step,
  3447. mbedtls_svc_key_id_t private_key,
  3448. const uint8_t *peer_key,
  3449. size_t peer_key_length);
  3450. /** Read some data from a key derivation operation.
  3451. *
  3452. * This function calculates output bytes from a key derivation algorithm and
  3453. * return those bytes.
  3454. * If you view the key derivation's output as a stream of bytes, this
  3455. * function destructively reads the requested number of bytes from the
  3456. * stream.
  3457. * The operation's capacity decreases by the number of bytes read.
  3458. *
  3459. * If this function returns an error status other than
  3460. * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
  3461. * state and must be aborted by calling psa_key_derivation_abort().
  3462. *
  3463. * \param[in,out] operation The key derivation operation object to read from.
  3464. * \param[out] output Buffer where the output will be written.
  3465. * \param output_length Number of bytes to output.
  3466. *
  3467. * \retval #PSA_SUCCESS \emptydescription
  3468. * \retval #PSA_ERROR_NOT_PERMITTED
  3469. * One of the inputs was a key whose policy didn't allow
  3470. * #PSA_KEY_USAGE_DERIVE.
  3471. * \retval #PSA_ERROR_INSUFFICIENT_DATA
  3472. * The operation's capacity was less than
  3473. * \p output_length bytes. Note that in this case,
  3474. * no output is written to the output buffer.
  3475. * The operation's capacity is set to 0, thus
  3476. * subsequent calls to this function will not
  3477. * succeed, even with a smaller output buffer.
  3478. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  3479. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  3480. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  3481. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  3482. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  3483. * \retval #PSA_ERROR_BAD_STATE
  3484. * The operation state is not valid (it must be active and completed
  3485. * all required input steps), or the library has not been previously
  3486. * 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_ARIA;
  3527. * - #PSA_KEY_TYPE_CAMELLIA;
  3528. * - #PSA_KEY_TYPE_DERIVE;
  3529. * - #PSA_KEY_TYPE_HMAC;
  3530. * - #PSA_KEY_TYPE_PASSWORD_HASH.
  3531. *
  3532. * - For ECC keys on a Montgomery elliptic curve
  3533. * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
  3534. * Montgomery curve), this function always draws a byte string whose
  3535. * length is determined by the curve, and sets the mandatory bits
  3536. * accordingly. That is:
  3537. *
  3538. * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
  3539. * string and process it as specified in RFC 7748 &sect;5.
  3540. * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
  3541. * string and process it as specified in RFC 7748 &sect;5.
  3542. *
  3543. * - For key types for which the key is represented by a single sequence of
  3544. * \c bits bits with constraints as to which bit sequences are acceptable,
  3545. * this function draws a byte string of length (\c bits / 8) bytes rounded
  3546. * up to the nearest whole number of bytes. If the resulting byte string
  3547. * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
  3548. * This process is repeated until an acceptable byte string is drawn.
  3549. * The byte string drawn from the operation is interpreted as specified
  3550. * for the output produced by psa_export_key().
  3551. * The following key types defined in this specification follow this scheme:
  3552. *
  3553. * - #PSA_KEY_TYPE_DES.
  3554. * Force-set the parity bits, but discard forbidden weak keys.
  3555. * For 2-key and 3-key triple-DES, the three keys are generated
  3556. * successively (for example, for 3-key triple-DES,
  3557. * if the first 8 bytes specify a weak key and the next 8 bytes do not,
  3558. * discard the first 8 bytes, use the next 8 bytes as the first key,
  3559. * and continue reading output from the operation to derive the other
  3560. * two keys).
  3561. * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
  3562. * where \c group designates any Diffie-Hellman group) and
  3563. * ECC keys on a Weierstrass elliptic curve
  3564. * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
  3565. * Weierstrass curve).
  3566. * For these key types, interpret the byte string as integer
  3567. * in big-endian order. Discard it if it is not in the range
  3568. * [0, *N* - 2] where *N* is the boundary of the private key domain
  3569. * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
  3570. * or the order of the curve's base point for ECC).
  3571. * Add 1 to the resulting integer and use this as the private key *x*.
  3572. * This method allows compliance to NIST standards, specifically
  3573. * the methods titled "key-pair generation by testing candidates"
  3574. * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
  3575. * in FIPS 186-4 &sect;B.1.2 for DSA, and
  3576. * in NIST SP 800-56A &sect;5.6.1.2.2 or
  3577. * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
  3578. *
  3579. * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
  3580. * the way in which the operation output is consumed is
  3581. * implementation-defined.
  3582. *
  3583. * In all cases, the data that is read is discarded from the operation.
  3584. * The operation's capacity is decreased by the number of bytes read.
  3585. *
  3586. * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
  3587. * the input to that step must be provided with psa_key_derivation_input_key().
  3588. * Future versions of this specification may include additional restrictions
  3589. * on the derived key based on the attributes and strength of the secret key.
  3590. *
  3591. * \note This function is equivalent to calling
  3592. * psa_key_derivation_output_key_custom()
  3593. * with the custom production parameters #PSA_CUSTOM_KEY_PARAMETERS_INIT
  3594. * and `custom_data_length == 0` (i.e. `custom_data` is empty).
  3595. *
  3596. * \param[in] attributes The attributes for the new key.
  3597. * If the key type to be created is
  3598. * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
  3599. * the policy must be the same as in the current
  3600. * operation.
  3601. * \param[in,out] operation The key derivation operation object to read from.
  3602. * \param[out] key On success, an identifier for the newly created
  3603. * key. For persistent keys, this is the key
  3604. * identifier defined in \p attributes.
  3605. * \c 0 on failure.
  3606. *
  3607. * \retval #PSA_SUCCESS
  3608. * Success.
  3609. * If the key is persistent, the key material and the key's metadata
  3610. * have been saved to persistent storage.
  3611. * \retval #PSA_ERROR_ALREADY_EXISTS
  3612. * This is an attempt to create a persistent key, and there is
  3613. * already a persistent key with the given identifier.
  3614. * \retval #PSA_ERROR_INSUFFICIENT_DATA
  3615. * There was not enough data to create the desired key.
  3616. * Note that in this case, no output is written to the output buffer.
  3617. * The operation's capacity is set to 0, thus subsequent calls to
  3618. * this function will not succeed, even with a smaller output buffer.
  3619. * \retval #PSA_ERROR_NOT_SUPPORTED
  3620. * The key type or key size is not supported, either by the
  3621. * implementation in general or in this particular location.
  3622. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3623. * The provided key attributes are not valid for the operation.
  3624. * \retval #PSA_ERROR_NOT_PERMITTED
  3625. * The #PSA_KEY_DERIVATION_INPUT_SECRET or
  3626. * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
  3627. * key; or one of the inputs was a key whose policy didn't allow
  3628. * #PSA_KEY_USAGE_DERIVE.
  3629. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  3630. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
  3631. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  3632. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  3633. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  3634. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  3635. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  3636. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  3637. * \retval #PSA_ERROR_BAD_STATE
  3638. * The operation state is not valid (it must be active and completed
  3639. * all required input steps), or the library has not been previously
  3640. * initialized by psa_crypto_init().
  3641. * It is implementation-dependent whether a failure to initialize
  3642. * results in this error code.
  3643. */
  3644. psa_status_t psa_key_derivation_output_key(
  3645. const psa_key_attributes_t *attributes,
  3646. psa_key_derivation_operation_t *operation,
  3647. mbedtls_svc_key_id_t *key);
  3648. /** Derive a key from an ongoing key derivation operation with custom
  3649. * production parameters.
  3650. *
  3651. * See the description of psa_key_derivation_out_key() for the operation of
  3652. * this function with the default production parameters.
  3653. * Mbed TLS currently does not currently support any non-default production
  3654. * parameters.
  3655. *
  3656. * \note This function is experimental and may change in future minor
  3657. * versions of Mbed TLS.
  3658. *
  3659. * \param[in] attributes The attributes for the new key.
  3660. * If the key type to be created is
  3661. * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
  3662. * the policy must be the same as in the current
  3663. * operation.
  3664. * \param[in,out] operation The key derivation operation object to read from.
  3665. * \param[in] custom Customization parameters for the key generation.
  3666. * When this is #PSA_CUSTOM_KEY_PARAMETERS_INIT
  3667. * with \p custom_data_length = 0,
  3668. * this function is equivalent to
  3669. * psa_key_derivation_output_key().
  3670. * \param[in] custom_data Variable-length data associated with \c custom.
  3671. * \param custom_data_length
  3672. * Length of `custom_data` in bytes.
  3673. * \param[out] key On success, an identifier for the newly created
  3674. * key. For persistent keys, this is the key
  3675. * identifier defined in \p attributes.
  3676. * \c 0 on failure.
  3677. *
  3678. * \retval #PSA_SUCCESS
  3679. * Success.
  3680. * If the key is persistent, the key material and the key's metadata
  3681. * have been saved to persistent storage.
  3682. * \retval #PSA_ERROR_ALREADY_EXISTS
  3683. * This is an attempt to create a persistent key, and there is
  3684. * already a persistent key with the given identifier.
  3685. * \retval #PSA_ERROR_INSUFFICIENT_DATA
  3686. * There was not enough data to create the desired key.
  3687. * Note that in this case, no output is written to the output buffer.
  3688. * The operation's capacity is set to 0, thus subsequent calls to
  3689. * this function will not succeed, even with a smaller output buffer.
  3690. * \retval #PSA_ERROR_NOT_SUPPORTED
  3691. * The key type or key size is not supported, either by the
  3692. * implementation in general or in this particular location.
  3693. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3694. * The provided key attributes are not valid for the operation.
  3695. * \retval #PSA_ERROR_NOT_PERMITTED
  3696. * The #PSA_KEY_DERIVATION_INPUT_SECRET or
  3697. * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
  3698. * key; or one of the inputs was a key whose policy didn't allow
  3699. * #PSA_KEY_USAGE_DERIVE.
  3700. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  3701. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
  3702. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  3703. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  3704. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  3705. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  3706. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  3707. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  3708. * \retval #PSA_ERROR_BAD_STATE
  3709. * The operation state is not valid (it must be active and completed
  3710. * all required input steps), or the library has not been previously
  3711. * initialized by psa_crypto_init().
  3712. * It is implementation-dependent whether a failure to initialize
  3713. * results in this error code.
  3714. */
  3715. psa_status_t psa_key_derivation_output_key_custom(
  3716. const psa_key_attributes_t *attributes,
  3717. psa_key_derivation_operation_t *operation,
  3718. const psa_custom_key_parameters_t *custom,
  3719. const uint8_t *custom_data,
  3720. size_t custom_data_length,
  3721. mbedtls_svc_key_id_t *key);
  3722. #ifndef __cplusplus
  3723. /* Omitted when compiling in C++, because one of the parameters is a
  3724. * pointer to a struct with a flexible array member, and that is not
  3725. * standard C++.
  3726. * https://github.com/Mbed-TLS/mbedtls/issues/9020
  3727. */
  3728. /** Derive a key from an ongoing key derivation operation with custom
  3729. * production parameters.
  3730. *
  3731. * \note
  3732. * This is a deprecated variant of psa_key_derivation_output_key_custom().
  3733. * It is equivalent except that the associated variable-length data
  3734. * is passed in `params->data` instead of a separate parameter.
  3735. * This function will be removed in a future version of Mbed TLS.
  3736. *
  3737. * \param[in] attributes The attributes for the new key.
  3738. * If the key type to be created is
  3739. * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
  3740. * the policy must be the same as in the current
  3741. * operation.
  3742. * \param[in,out] operation The key derivation operation object to read from.
  3743. * \param[in] params Customization parameters for the key derivation.
  3744. * When this is #PSA_KEY_PRODUCTION_PARAMETERS_INIT
  3745. * with \p params_data_length = 0,
  3746. * this function is equivalent to
  3747. * psa_key_derivation_output_key().
  3748. * Mbed TLS currently only supports the default
  3749. * production parameters, i.e.
  3750. * #PSA_KEY_PRODUCTION_PARAMETERS_INIT,
  3751. * for all key types.
  3752. * \param params_data_length
  3753. * Length of `params->data` in bytes.
  3754. * \param[out] key On success, an identifier for the newly created
  3755. * key. For persistent keys, this is the key
  3756. * identifier defined in \p attributes.
  3757. * \c 0 on failure.
  3758. *
  3759. * \retval #PSA_SUCCESS
  3760. * Success.
  3761. * If the key is persistent, the key material and the key's metadata
  3762. * have been saved to persistent storage.
  3763. * \retval #PSA_ERROR_ALREADY_EXISTS
  3764. * This is an attempt to create a persistent key, and there is
  3765. * already a persistent key with the given identifier.
  3766. * \retval #PSA_ERROR_INSUFFICIENT_DATA
  3767. * There was not enough data to create the desired key.
  3768. * Note that in this case, no output is written to the output buffer.
  3769. * The operation's capacity is set to 0, thus subsequent calls to
  3770. * this function will not succeed, even with a smaller output buffer.
  3771. * \retval #PSA_ERROR_NOT_SUPPORTED
  3772. * The key type or key size is not supported, either by the
  3773. * implementation in general or in this particular location.
  3774. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3775. * The provided key attributes are not valid for the operation.
  3776. * \retval #PSA_ERROR_NOT_PERMITTED
  3777. * The #PSA_KEY_DERIVATION_INPUT_SECRET or
  3778. * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
  3779. * key; or one of the inputs was a key whose policy didn't allow
  3780. * #PSA_KEY_USAGE_DERIVE.
  3781. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  3782. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
  3783. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  3784. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  3785. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  3786. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  3787. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  3788. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  3789. * \retval #PSA_ERROR_BAD_STATE
  3790. * The operation state is not valid (it must be active and completed
  3791. * all required input steps), or the library has not been previously
  3792. * initialized by psa_crypto_init().
  3793. * It is implementation-dependent whether a failure to initialize
  3794. * results in this error code.
  3795. */
  3796. psa_status_t psa_key_derivation_output_key_ext(
  3797. const psa_key_attributes_t *attributes,
  3798. psa_key_derivation_operation_t *operation,
  3799. const psa_key_production_parameters_t *params,
  3800. size_t params_data_length,
  3801. mbedtls_svc_key_id_t *key);
  3802. #endif /* !__cplusplus */
  3803. /** Compare output data from a key derivation operation to an expected value.
  3804. *
  3805. * This function calculates output bytes from a key derivation algorithm and
  3806. * compares those bytes to an expected value in constant time.
  3807. * If you view the key derivation's output as a stream of bytes, this
  3808. * function destructively reads the expected number of bytes from the
  3809. * stream before comparing them.
  3810. * The operation's capacity decreases by the number of bytes read.
  3811. *
  3812. * This is functionally equivalent to the following code:
  3813. * \code
  3814. * psa_key_derivation_output_bytes(operation, tmp, output_length);
  3815. * if (memcmp(output, tmp, output_length) != 0)
  3816. * return PSA_ERROR_INVALID_SIGNATURE;
  3817. * \endcode
  3818. * except (1) it works even if the key's policy does not allow outputting the
  3819. * bytes, and (2) the comparison will be done in constant time.
  3820. *
  3821. * If this function returns an error status other than
  3822. * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
  3823. * the operation enters an error state and must be aborted by calling
  3824. * psa_key_derivation_abort().
  3825. *
  3826. * \param[in,out] operation The key derivation operation object to read from.
  3827. * \param[in] expected Buffer containing the expected derivation output.
  3828. * \param expected_length Length of the expected output; this is also the
  3829. * number of bytes that will be read.
  3830. *
  3831. * \retval #PSA_SUCCESS \emptydescription
  3832. * \retval #PSA_ERROR_INVALID_SIGNATURE
  3833. * The output was read successfully, but it differs from the expected
  3834. * output.
  3835. * \retval #PSA_ERROR_NOT_PERMITTED
  3836. * One of the inputs was a key whose policy didn't allow
  3837. * #PSA_KEY_USAGE_VERIFY_DERIVATION.
  3838. * \retval #PSA_ERROR_INSUFFICIENT_DATA
  3839. * The operation's capacity was less than
  3840. * \p output_length bytes. Note that in this case,
  3841. * the operation's capacity is set to 0, thus
  3842. * subsequent calls to this function will not
  3843. * succeed, even with a smaller expected output.
  3844. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  3845. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  3846. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  3847. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  3848. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  3849. * \retval #PSA_ERROR_BAD_STATE
  3850. * The operation state is not valid (it must be active and completed
  3851. * all required input steps), or the library has not been previously
  3852. * initialized by psa_crypto_init().
  3853. * It is implementation-dependent whether a failure to initialize
  3854. * results in this error code.
  3855. */
  3856. psa_status_t psa_key_derivation_verify_bytes(
  3857. psa_key_derivation_operation_t *operation,
  3858. const uint8_t *expected,
  3859. size_t expected_length);
  3860. /** Compare output data from a key derivation operation to an expected value
  3861. * stored in a key object.
  3862. *
  3863. * This function calculates output bytes from a key derivation algorithm and
  3864. * compares those bytes to an expected value, provided as key of type
  3865. * #PSA_KEY_TYPE_PASSWORD_HASH.
  3866. * If you view the key derivation's output as a stream of bytes, this
  3867. * function destructively reads the number of bytes corresponding to the
  3868. * length of the expected value from the stream before comparing them.
  3869. * The operation's capacity decreases by the number of bytes read.
  3870. *
  3871. * This is functionally equivalent to exporting the key and calling
  3872. * psa_key_derivation_verify_bytes() on the result, except that it
  3873. * works even if the key cannot be exported.
  3874. *
  3875. * If this function returns an error status other than
  3876. * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
  3877. * the operation enters an error state and must be aborted by calling
  3878. * psa_key_derivation_abort().
  3879. *
  3880. * \param[in,out] operation The key derivation operation object to read from.
  3881. * \param[in] expected A key of type #PSA_KEY_TYPE_PASSWORD_HASH
  3882. * containing the expected output. Its policy must
  3883. * include the #PSA_KEY_USAGE_VERIFY_DERIVATION flag
  3884. * and the permitted algorithm must match the
  3885. * operation. The value of this key was likely
  3886. * computed by a previous call to
  3887. * psa_key_derivation_output_key() or
  3888. * psa_key_derivation_output_key_custom().
  3889. *
  3890. * \retval #PSA_SUCCESS \emptydescription
  3891. * \retval #PSA_ERROR_INVALID_SIGNATURE
  3892. * The output was read successfully, but if differs from the expected
  3893. * output.
  3894. * \retval #PSA_ERROR_INVALID_HANDLE
  3895. * The key passed as the expected value does not exist.
  3896. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3897. * The key passed as the expected value has an invalid type.
  3898. * \retval #PSA_ERROR_NOT_PERMITTED
  3899. * The key passed as the expected value does not allow this usage or
  3900. * this algorithm; or one of the inputs was a key whose policy didn't
  3901. * allow #PSA_KEY_USAGE_VERIFY_DERIVATION.
  3902. * \retval #PSA_ERROR_INSUFFICIENT_DATA
  3903. * The operation's capacity was less than
  3904. * the length of the expected value. In this case,
  3905. * the operation's capacity is set to 0, thus
  3906. * subsequent calls to this function will not
  3907. * succeed, even with a smaller expected output.
  3908. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  3909. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  3910. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  3911. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  3912. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  3913. * \retval #PSA_ERROR_BAD_STATE
  3914. * The operation state is not valid (it must be active and completed
  3915. * all required input steps), or the library has not been previously
  3916. * initialized by psa_crypto_init().
  3917. * It is implementation-dependent whether a failure to initialize
  3918. * results in this error code.
  3919. */
  3920. psa_status_t psa_key_derivation_verify_key(
  3921. psa_key_derivation_operation_t *operation,
  3922. psa_key_id_t expected);
  3923. /** Abort a key derivation operation.
  3924. *
  3925. * Aborting an operation frees all associated resources except for the \c
  3926. * operation structure itself. Once aborted, the operation object can be reused
  3927. * for another operation by calling psa_key_derivation_setup() again.
  3928. *
  3929. * This function may be called at any time after the operation
  3930. * object has been initialized as described in #psa_key_derivation_operation_t.
  3931. *
  3932. * In particular, it is valid to call psa_key_derivation_abort() twice, or to
  3933. * call psa_key_derivation_abort() on an operation that has not been set up.
  3934. *
  3935. * \param[in,out] operation The operation to abort.
  3936. *
  3937. * \retval #PSA_SUCCESS \emptydescription
  3938. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  3939. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  3940. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  3941. * \retval #PSA_ERROR_BAD_STATE
  3942. * The library has not been previously initialized by psa_crypto_init().
  3943. * It is implementation-dependent whether a failure to initialize
  3944. * results in this error code.
  3945. */
  3946. psa_status_t psa_key_derivation_abort(
  3947. psa_key_derivation_operation_t *operation);
  3948. /** Perform a key agreement and return the raw shared secret.
  3949. *
  3950. * \warning The raw result of a key agreement algorithm such as finite-field
  3951. * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
  3952. * not be used directly as key material. It should instead be passed as
  3953. * input to a key derivation algorithm. To chain a key agreement with
  3954. * a key derivation, use psa_key_derivation_key_agreement() and other
  3955. * functions from the key derivation interface.
  3956. *
  3957. * \param alg The key agreement algorithm to compute
  3958. * (\c PSA_ALG_XXX value such that
  3959. * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
  3960. * is true).
  3961. * \param private_key Identifier of the private key to use. It must
  3962. * allow the usage #PSA_KEY_USAGE_DERIVE.
  3963. * \param[in] peer_key Public key of the peer. It must be
  3964. * in the same format that psa_import_key()
  3965. * accepts. The standard formats for public
  3966. * keys are documented in the documentation
  3967. * of psa_export_public_key().
  3968. * \param peer_key_length Size of \p peer_key in bytes.
  3969. * \param[out] output Buffer where the decrypted message is to
  3970. * be written.
  3971. * \param output_size Size of the \c output buffer in bytes.
  3972. * \param[out] output_length On success, the number of bytes
  3973. * that make up the returned output.
  3974. *
  3975. * \retval #PSA_SUCCESS
  3976. * Success.
  3977. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  3978. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  3979. * \retval #PSA_ERROR_INVALID_ARGUMENT
  3980. * \p alg is not a key agreement algorithm, or
  3981. * \p private_key is not compatible with \p alg,
  3982. * or \p peer_key is not valid for \p alg or not compatible with
  3983. * \p private_key.
  3984. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  3985. * \p output_size is too small
  3986. * \retval #PSA_ERROR_NOT_SUPPORTED
  3987. * \p alg is not a supported key agreement algorithm.
  3988. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  3989. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  3990. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  3991. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  3992. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  3993. * \retval #PSA_ERROR_BAD_STATE
  3994. * The library has not been previously initialized by psa_crypto_init().
  3995. * It is implementation-dependent whether a failure to initialize
  3996. * results in this error code.
  3997. */
  3998. psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
  3999. mbedtls_svc_key_id_t private_key,
  4000. const uint8_t *peer_key,
  4001. size_t peer_key_length,
  4002. uint8_t *output,
  4003. size_t output_size,
  4004. size_t *output_length);
  4005. /**@}*/
  4006. /** \defgroup random Random generation
  4007. * @{
  4008. */
  4009. /**
  4010. * \brief Generate random bytes.
  4011. *
  4012. * \warning This function **can** fail! Callers MUST check the return status
  4013. * and MUST NOT use the content of the output buffer if the return
  4014. * status is not #PSA_SUCCESS.
  4015. *
  4016. * \note To generate a key, use psa_generate_key() instead.
  4017. *
  4018. * \param[out] output Output buffer for the generated data.
  4019. * \param output_size Number of bytes to generate and output.
  4020. *
  4021. * \retval #PSA_SUCCESS \emptydescription
  4022. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  4023. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  4024. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  4025. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  4026. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  4027. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  4028. * \retval #PSA_ERROR_BAD_STATE
  4029. * The library has not been previously initialized by psa_crypto_init().
  4030. * It is implementation-dependent whether a failure to initialize
  4031. * results in this error code.
  4032. */
  4033. psa_status_t psa_generate_random(uint8_t *output,
  4034. size_t output_size);
  4035. /**
  4036. * \brief Generate a key or key pair.
  4037. *
  4038. * The key is generated randomly.
  4039. * Its location, usage policy, type and size are taken from \p attributes.
  4040. *
  4041. * Implementations must reject an attempt to generate a key of size 0.
  4042. *
  4043. * The following type-specific considerations apply:
  4044. * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
  4045. * the public exponent is 65537.
  4046. * The modulus is a product of two probabilistic primes
  4047. * between 2^{n-1} and 2^n where n is the bit size specified in the
  4048. * attributes.
  4049. *
  4050. * \note This function is equivalent to calling psa_generate_key_custom()
  4051. * with the custom production parameters #PSA_CUSTOM_KEY_PARAMETERS_INIT
  4052. * and `custom_data_length == 0` (i.e. `custom_data` is empty).
  4053. *
  4054. * \param[in] attributes The attributes for the new key.
  4055. * \param[out] key On success, an identifier for the newly created
  4056. * key. For persistent keys, this is the key
  4057. * identifier defined in \p attributes.
  4058. * \c 0 on failure.
  4059. *
  4060. * \retval #PSA_SUCCESS
  4061. * Success.
  4062. * If the key is persistent, the key material and the key's metadata
  4063. * have been saved to persistent storage.
  4064. * \retval #PSA_ERROR_ALREADY_EXISTS
  4065. * This is an attempt to create a persistent key, and there is
  4066. * already a persistent key with the given identifier.
  4067. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  4068. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  4069. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  4070. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  4071. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  4072. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  4073. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  4074. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
  4075. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  4076. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  4077. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  4078. * \retval #PSA_ERROR_BAD_STATE
  4079. * The library has not been previously initialized by psa_crypto_init().
  4080. * It is implementation-dependent whether a failure to initialize
  4081. * results in this error code.
  4082. */
  4083. psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
  4084. mbedtls_svc_key_id_t *key);
  4085. /**
  4086. * \brief Generate a key or key pair using custom production parameters.
  4087. *
  4088. * See the description of psa_generate_key() for the operation of this
  4089. * function with the default production parameters. In addition, this function
  4090. * supports the following production customizations, described in more detail
  4091. * in the documentation of ::psa_custom_key_parameters_t:
  4092. *
  4093. * - RSA keys: generation with a custom public exponent.
  4094. *
  4095. * \note This function is experimental and may change in future minor
  4096. * versions of Mbed TLS.
  4097. *
  4098. * \param[in] attributes The attributes for the new key.
  4099. * \param[in] custom Customization parameters for the key generation.
  4100. * When this is #PSA_CUSTOM_KEY_PARAMETERS_INIT
  4101. * with \p custom_data_length = 0,
  4102. * this function is equivalent to
  4103. * psa_generate_key().
  4104. * \param[in] custom_data Variable-length data associated with \c custom.
  4105. * \param custom_data_length
  4106. * Length of `custom_data` in bytes.
  4107. * \param[out] key On success, an identifier for the newly created
  4108. * key. For persistent keys, this is the key
  4109. * identifier defined in \p attributes.
  4110. * \c 0 on failure.
  4111. *
  4112. * \retval #PSA_SUCCESS
  4113. * Success.
  4114. * If the key is persistent, the key material and the key's metadata
  4115. * have been saved to persistent storage.
  4116. * \retval #PSA_ERROR_ALREADY_EXISTS
  4117. * This is an attempt to create a persistent key, and there is
  4118. * already a persistent key with the given identifier.
  4119. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  4120. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  4121. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  4122. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  4123. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  4124. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  4125. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  4126. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
  4127. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  4128. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  4129. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  4130. * \retval #PSA_ERROR_BAD_STATE
  4131. * The library has not been previously initialized by psa_crypto_init().
  4132. * It is implementation-dependent whether a failure to initialize
  4133. * results in this error code.
  4134. */
  4135. psa_status_t psa_generate_key_custom(const psa_key_attributes_t *attributes,
  4136. const psa_custom_key_parameters_t *custom,
  4137. const uint8_t *custom_data,
  4138. size_t custom_data_length,
  4139. mbedtls_svc_key_id_t *key);
  4140. #ifndef __cplusplus
  4141. /* Omitted when compiling in C++, because one of the parameters is a
  4142. * pointer to a struct with a flexible array member, and that is not
  4143. * standard C++.
  4144. * https://github.com/Mbed-TLS/mbedtls/issues/9020
  4145. */
  4146. /**
  4147. * \brief Generate a key or key pair using custom production parameters.
  4148. *
  4149. * \note
  4150. * This is a deprecated variant of psa_key_derivation_output_key_custom().
  4151. * It is equivalent except that the associated variable-length data
  4152. * is passed in `params->data` instead of a separate parameter.
  4153. * This function will be removed in a future version of Mbed TLS.
  4154. *
  4155. * \param[in] attributes The attributes for the new key.
  4156. * \param[in] params Customization parameters for the key generation.
  4157. * When this is #PSA_KEY_PRODUCTION_PARAMETERS_INIT
  4158. * with \p params_data_length = 0,
  4159. * this function is equivalent to
  4160. * psa_generate_key().
  4161. * \param params_data_length
  4162. * Length of `params->data` in bytes.
  4163. * \param[out] key On success, an identifier for the newly created
  4164. * key. For persistent keys, this is the key
  4165. * identifier defined in \p attributes.
  4166. * \c 0 on failure.
  4167. *
  4168. * \retval #PSA_SUCCESS
  4169. * Success.
  4170. * If the key is persistent, the key material and the key's metadata
  4171. * have been saved to persistent storage.
  4172. * \retval #PSA_ERROR_ALREADY_EXISTS
  4173. * This is an attempt to create a persistent key, and there is
  4174. * already a persistent key with the given identifier.
  4175. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  4176. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  4177. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  4178. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  4179. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  4180. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  4181. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  4182. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
  4183. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  4184. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  4185. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  4186. * \retval #PSA_ERROR_BAD_STATE
  4187. * The library has not been previously initialized by psa_crypto_init().
  4188. * It is implementation-dependent whether a failure to initialize
  4189. * results in this error code.
  4190. */
  4191. psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
  4192. const psa_key_production_parameters_t *params,
  4193. size_t params_data_length,
  4194. mbedtls_svc_key_id_t *key);
  4195. #endif /* !__cplusplus */
  4196. /**@}*/
  4197. /** \defgroup interruptible_hash Interruptible sign/verify hash
  4198. * @{
  4199. */
  4200. /** The type of the state data structure for interruptible hash
  4201. * signing operations.
  4202. *
  4203. * Before calling any function on a sign hash operation object, the
  4204. * application must initialize it by any of the following means:
  4205. * - Set the structure to all-bits-zero, for example:
  4206. * \code
  4207. * psa_sign_hash_interruptible_operation_t operation;
  4208. * memset(&operation, 0, sizeof(operation));
  4209. * \endcode
  4210. * - Initialize the structure to logical zero values, for example:
  4211. * \code
  4212. * psa_sign_hash_interruptible_operation_t operation = {0};
  4213. * \endcode
  4214. * - Initialize the structure to the initializer
  4215. * #PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT, for example:
  4216. * \code
  4217. * psa_sign_hash_interruptible_operation_t operation =
  4218. * PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT;
  4219. * \endcode
  4220. * - Assign the result of the function
  4221. * psa_sign_hash_interruptible_operation_init() to the structure, for
  4222. * example:
  4223. * \code
  4224. * psa_sign_hash_interruptible_operation_t operation;
  4225. * operation = psa_sign_hash_interruptible_operation_init();
  4226. * \endcode
  4227. *
  4228. * This is an implementation-defined \c struct. Applications should not
  4229. * make any assumptions about the content of this structure.
  4230. * Implementation details can change in future versions without notice. */
  4231. typedef struct psa_sign_hash_interruptible_operation_s psa_sign_hash_interruptible_operation_t;
  4232. /** The type of the state data structure for interruptible hash
  4233. * verification operations.
  4234. *
  4235. * Before calling any function on a sign hash operation object, the
  4236. * application must initialize it by any of the following means:
  4237. * - Set the structure to all-bits-zero, for example:
  4238. * \code
  4239. * psa_verify_hash_interruptible_operation_t operation;
  4240. * memset(&operation, 0, sizeof(operation));
  4241. * \endcode
  4242. * - Initialize the structure to logical zero values, for example:
  4243. * \code
  4244. * psa_verify_hash_interruptible_operation_t operation = {0};
  4245. * \endcode
  4246. * - Initialize the structure to the initializer
  4247. * #PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT, for example:
  4248. * \code
  4249. * psa_verify_hash_interruptible_operation_t operation =
  4250. * PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT;
  4251. * \endcode
  4252. * - Assign the result of the function
  4253. * psa_verify_hash_interruptible_operation_init() to the structure, for
  4254. * example:
  4255. * \code
  4256. * psa_verify_hash_interruptible_operation_t operation;
  4257. * operation = psa_verify_hash_interruptible_operation_init();
  4258. * \endcode
  4259. *
  4260. * This is an implementation-defined \c struct. Applications should not
  4261. * make any assumptions about the content of this structure.
  4262. * Implementation details can change in future versions without notice. */
  4263. typedef struct psa_verify_hash_interruptible_operation_s psa_verify_hash_interruptible_operation_t;
  4264. /**
  4265. * \brief Set the maximum number of ops allowed to be
  4266. * executed by an interruptible function in a
  4267. * single call.
  4268. *
  4269. * \warning This is a beta API, and thus subject to change
  4270. * at any point. It is not bound by the usual
  4271. * interface stability promises.
  4272. *
  4273. * \note The time taken to execute a single op is
  4274. * implementation specific and depends on
  4275. * software, hardware, the algorithm, key type and
  4276. * curve chosen. Even within a single operation,
  4277. * successive ops can take differing amounts of
  4278. * time. The only guarantee is that lower values
  4279. * for \p max_ops means functions will block for a
  4280. * lesser maximum amount of time. The functions
  4281. * \c psa_sign_interruptible_get_num_ops() and
  4282. * \c psa_verify_interruptible_get_num_ops() are
  4283. * provided to help with tuning this value.
  4284. *
  4285. * \note This value defaults to
  4286. * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, which
  4287. * means the whole operation will be done in one
  4288. * go, regardless of the number of ops required.
  4289. *
  4290. * \note If more ops are needed to complete a
  4291. * computation, #PSA_OPERATION_INCOMPLETE will be
  4292. * returned by the function performing the
  4293. * computation. It is then the caller's
  4294. * responsibility to either call again with the
  4295. * same operation context until it returns 0 or an
  4296. * error code; or to call the relevant abort
  4297. * function if the answer is no longer required.
  4298. *
  4299. * \note The interpretation of \p max_ops is also
  4300. * implementation defined. On a hard real time
  4301. * system, this can indicate a hard deadline, as a
  4302. * real-time system needs a guarantee of not
  4303. * spending more than X time, however care must be
  4304. * taken in such an implementation to avoid the
  4305. * situation whereby calls just return, not being
  4306. * able to do any actual work within the allotted
  4307. * time. On a non-real-time system, the
  4308. * implementation can be more relaxed, but again
  4309. * whether this number should be interpreted as as
  4310. * hard or soft limit or even whether a less than
  4311. * or equals as regards to ops executed in a
  4312. * single call is implementation defined.
  4313. *
  4314. * \note For keys in local storage when no accelerator
  4315. * driver applies, please see also the
  4316. * documentation for \c mbedtls_ecp_set_max_ops(),
  4317. * which is the internal implementation in these
  4318. * cases.
  4319. *
  4320. * \warning With implementations that interpret this number
  4321. * as a hard limit, setting this number too small
  4322. * may result in an infinite loop, whereby each
  4323. * call results in immediate return with no ops
  4324. * done (as there is not enough time to execute
  4325. * any), and thus no result will ever be achieved.
  4326. *
  4327. * \note This only applies to functions whose
  4328. * documentation mentions they may return
  4329. * #PSA_OPERATION_INCOMPLETE.
  4330. *
  4331. * \param max_ops The maximum number of ops to be executed in a
  4332. * single call. This can be a number from 0 to
  4333. * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, where 0
  4334. * is the least amount of work done per call.
  4335. */
  4336. void psa_interruptible_set_max_ops(uint32_t max_ops);
  4337. /**
  4338. * \brief Get the maximum number of ops allowed to be
  4339. * executed by an interruptible function in a
  4340. * single call. This will return the last
  4341. * value set by
  4342. * \c psa_interruptible_set_max_ops() or
  4343. * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED if
  4344. * that function has never been called.
  4345. *
  4346. * \warning This is a beta API, and thus subject to change
  4347. * at any point. It is not bound by the usual
  4348. * interface stability promises.
  4349. *
  4350. * \return Maximum number of ops allowed to be
  4351. * executed by an interruptible function in a
  4352. * single call.
  4353. */
  4354. uint32_t psa_interruptible_get_max_ops(void);
  4355. /**
  4356. * \brief Get the number of ops that a hash signing
  4357. * operation has taken so far. If the operation
  4358. * has completed, then this will represent the
  4359. * number of ops required for the entire
  4360. * operation. After initialization or calling
  4361. * \c psa_sign_hash_interruptible_abort() on
  4362. * the operation, a value of 0 will be returned.
  4363. *
  4364. * \note This interface is guaranteed re-entrant and
  4365. * thus may be called from driver code.
  4366. *
  4367. * \warning This is a beta API, and thus subject to change
  4368. * at any point. It is not bound by the usual
  4369. * interface stability promises.
  4370. *
  4371. * This is a helper provided to help you tune the
  4372. * value passed to \c
  4373. * psa_interruptible_set_max_ops().
  4374. *
  4375. * \param operation The \c psa_sign_hash_interruptible_operation_t
  4376. * to use. This must be initialized first.
  4377. *
  4378. * \return Number of ops that the operation has taken so
  4379. * far.
  4380. */
  4381. uint32_t psa_sign_hash_get_num_ops(
  4382. const psa_sign_hash_interruptible_operation_t *operation);
  4383. /**
  4384. * \brief Get the number of ops that a hash verification
  4385. * operation has taken so far. If the operation
  4386. * has completed, then this will represent the
  4387. * number of ops required for the entire
  4388. * operation. After initialization or calling \c
  4389. * psa_verify_hash_interruptible_abort() on the
  4390. * operation, a value of 0 will be returned.
  4391. *
  4392. * \warning This is a beta API, and thus subject to change
  4393. * at any point. It is not bound by the usual
  4394. * interface stability promises.
  4395. *
  4396. * This is a helper provided to help you tune the
  4397. * value passed to \c
  4398. * psa_interruptible_set_max_ops().
  4399. *
  4400. * \param operation The \c
  4401. * psa_verify_hash_interruptible_operation_t to
  4402. * use. This must be initialized first.
  4403. *
  4404. * \return Number of ops that the operation has taken so
  4405. * far.
  4406. */
  4407. uint32_t psa_verify_hash_get_num_ops(
  4408. const psa_verify_hash_interruptible_operation_t *operation);
  4409. /**
  4410. * \brief Start signing a hash or short message with a
  4411. * private key, in an interruptible manner.
  4412. *
  4413. * \see \c psa_sign_hash_complete()
  4414. *
  4415. * \warning This is a beta API, and thus subject to change
  4416. * at any point. It is not bound by the usual
  4417. * interface stability promises.
  4418. *
  4419. * \note This function combined with \c
  4420. * psa_sign_hash_complete() is equivalent to
  4421. * \c psa_sign_hash() but
  4422. * \c psa_sign_hash_complete() can return early and
  4423. * resume according to the limit set with \c
  4424. * psa_interruptible_set_max_ops() to reduce the
  4425. * maximum time spent in a function call.
  4426. *
  4427. * \note Users should call \c psa_sign_hash_complete()
  4428. * repeatedly on the same context after a
  4429. * successful call to this function until \c
  4430. * psa_sign_hash_complete() either returns 0 or an
  4431. * error. \c psa_sign_hash_complete() will return
  4432. * #PSA_OPERATION_INCOMPLETE if there is more work
  4433. * to do. Alternatively users can call
  4434. * \c psa_sign_hash_abort() at any point if they no
  4435. * longer want the result.
  4436. *
  4437. * \note If this function returns an error status, the
  4438. * operation enters an error state and must be
  4439. * aborted by calling \c psa_sign_hash_abort().
  4440. *
  4441. * \param[in, out] operation The \c psa_sign_hash_interruptible_operation_t
  4442. * to use. This must be initialized first.
  4443. *
  4444. * \param key Identifier of the key to use for the operation.
  4445. * It must be an asymmetric key pair. The key must
  4446. * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
  4447. * \param alg A signature algorithm (\c PSA_ALG_XXX
  4448. * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
  4449. * is true), that is compatible with
  4450. * the type of \p key.
  4451. * \param[in] hash The hash or message to sign.
  4452. * \param hash_length Size of the \p hash buffer in bytes.
  4453. *
  4454. * \retval #PSA_SUCCESS
  4455. * The operation started successfully - call \c psa_sign_hash_complete()
  4456. * with the same context to complete the operation
  4457. *
  4458. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  4459. * \retval #PSA_ERROR_NOT_PERMITTED
  4460. * The key does not have the #PSA_KEY_USAGE_SIGN_HASH flag, or it does
  4461. * not permit the requested algorithm.
  4462. * \retval #PSA_ERROR_BAD_STATE
  4463. * An operation has previously been started on this context, and is
  4464. * still in progress.
  4465. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  4466. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  4467. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  4468. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  4469. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  4470. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  4471. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  4472. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  4473. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  4474. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  4475. * \retval #PSA_ERROR_BAD_STATE
  4476. * The library has not been previously initialized by psa_crypto_init().
  4477. * It is implementation-dependent whether a failure to initialize
  4478. * results in this error code.
  4479. */
  4480. psa_status_t psa_sign_hash_start(
  4481. psa_sign_hash_interruptible_operation_t *operation,
  4482. mbedtls_svc_key_id_t key, psa_algorithm_t alg,
  4483. const uint8_t *hash, size_t hash_length);
  4484. /**
  4485. * \brief Continue and eventually complete the action of
  4486. * signing a hash or short message with a private
  4487. * key, in an interruptible manner.
  4488. *
  4489. * \see \c psa_sign_hash_start()
  4490. *
  4491. * \warning This is a beta API, and thus subject to change
  4492. * at any point. It is not bound by the usual
  4493. * interface stability promises.
  4494. *
  4495. * \note This function combined with \c
  4496. * psa_sign_hash_start() is equivalent to
  4497. * \c psa_sign_hash() but this function can return
  4498. * early and resume according to the limit set with
  4499. * \c psa_interruptible_set_max_ops() to reduce the
  4500. * maximum time spent in a function call.
  4501. *
  4502. * \note Users should call this function on the same
  4503. * operation object repeatedly until it either
  4504. * returns 0 or an error. This function will return
  4505. * #PSA_OPERATION_INCOMPLETE if there is more work
  4506. * to do. Alternatively users can call
  4507. * \c psa_sign_hash_abort() at any point if they no
  4508. * longer want the result.
  4509. *
  4510. * \note When this function returns successfully, the
  4511. * operation becomes inactive. If this function
  4512. * returns an error status, the operation enters an
  4513. * error state and must be aborted by calling
  4514. * \c psa_sign_hash_abort().
  4515. *
  4516. * \param[in, out] operation The \c psa_sign_hash_interruptible_operation_t
  4517. * to use. This must be initialized first, and have
  4518. * had \c psa_sign_hash_start() called with it
  4519. * first.
  4520. *
  4521. * \param[out] signature Buffer where the signature is to be written.
  4522. * \param signature_size Size of the \p signature buffer in bytes. This
  4523. * must be appropriate for the selected
  4524. * algorithm and key:
  4525. * - The required signature size is
  4526. * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c
  4527. * key_bits, \c alg) where \c key_type and \c
  4528. * key_bits are the type and bit-size
  4529. * respectively of key.
  4530. * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
  4531. * maximum signature size of any supported
  4532. * signature algorithm.
  4533. * \param[out] signature_length On success, the number of bytes that make up
  4534. * the returned signature value.
  4535. *
  4536. * \retval #PSA_SUCCESS
  4537. * Operation completed successfully
  4538. *
  4539. * \retval #PSA_OPERATION_INCOMPLETE
  4540. * Operation was interrupted due to the setting of \c
  4541. * psa_interruptible_set_max_ops(). There is still work to be done.
  4542. * Call this function again with the same operation object.
  4543. *
  4544. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  4545. * The size of the \p signature buffer is too small. You can
  4546. * determine a sufficient buffer size by calling
  4547. * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \c alg)
  4548. * where \c key_type and \c key_bits are the type and bit-size
  4549. * respectively of \c key.
  4550. *
  4551. * \retval #PSA_ERROR_BAD_STATE
  4552. * An operation was not previously started on this context via
  4553. * \c psa_sign_hash_start().
  4554. *
  4555. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  4556. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  4557. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  4558. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  4559. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  4560. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  4561. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  4562. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  4563. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  4564. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  4565. * \retval #PSA_ERROR_BAD_STATE
  4566. * The library has either not been previously initialized by
  4567. * psa_crypto_init() or you did not previously call
  4568. * psa_sign_hash_start() with this operation object. It is
  4569. * implementation-dependent whether a failure to initialize results in
  4570. * this error code.
  4571. */
  4572. psa_status_t psa_sign_hash_complete(
  4573. psa_sign_hash_interruptible_operation_t *operation,
  4574. uint8_t *signature, size_t signature_size,
  4575. size_t *signature_length);
  4576. /**
  4577. * \brief Abort a sign hash operation.
  4578. *
  4579. * \warning This is a beta API, and thus subject to change
  4580. * at any point. It is not bound by the usual
  4581. * interface stability promises.
  4582. *
  4583. * \note This function is the only function that clears
  4584. * the number of ops completed as part of the
  4585. * operation. Please ensure you copy this value via
  4586. * \c psa_sign_hash_get_num_ops() if required
  4587. * before calling.
  4588. *
  4589. * \note Aborting an operation frees all associated
  4590. * resources except for the \p operation structure
  4591. * itself. Once aborted, the operation object can
  4592. * be reused for another operation by calling \c
  4593. * psa_sign_hash_start() again.
  4594. *
  4595. * \note You may call this function any time after the
  4596. * operation object has been initialized. In
  4597. * particular, calling \c psa_sign_hash_abort()
  4598. * after the operation has already been terminated
  4599. * by a call to \c psa_sign_hash_abort() or
  4600. * psa_sign_hash_complete() is safe.
  4601. *
  4602. * \param[in,out] operation Initialized sign hash operation.
  4603. *
  4604. * \retval #PSA_SUCCESS
  4605. * The operation was aborted successfully.
  4606. *
  4607. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  4608. * \retval #PSA_ERROR_BAD_STATE
  4609. * The library has not been previously initialized by psa_crypto_init().
  4610. * It is implementation-dependent whether a failure to initialize
  4611. * results in this error code.
  4612. */
  4613. psa_status_t psa_sign_hash_abort(
  4614. psa_sign_hash_interruptible_operation_t *operation);
  4615. /**
  4616. * \brief Start reading and verifying a hash or short
  4617. * message, in an interruptible manner.
  4618. *
  4619. * \see \c psa_verify_hash_complete()
  4620. *
  4621. * \warning This is a beta API, and thus subject to change
  4622. * at any point. It is not bound by the usual
  4623. * interface stability promises.
  4624. *
  4625. * \note This function combined with \c
  4626. * psa_verify_hash_complete() is equivalent to
  4627. * \c psa_verify_hash() but \c
  4628. * psa_verify_hash_complete() can return early and
  4629. * resume according to the limit set with \c
  4630. * psa_interruptible_set_max_ops() to reduce the
  4631. * maximum time spent in a function.
  4632. *
  4633. * \note Users should call \c psa_verify_hash_complete()
  4634. * repeatedly on the same operation object after a
  4635. * successful call to this function until \c
  4636. * psa_verify_hash_complete() either returns 0 or
  4637. * an error. \c psa_verify_hash_complete() will
  4638. * return #PSA_OPERATION_INCOMPLETE if there is
  4639. * more work to do. Alternatively users can call
  4640. * \c psa_verify_hash_abort() at any point if they
  4641. * no longer want the result.
  4642. *
  4643. * \note If this function returns an error status, the
  4644. * operation enters an error state and must be
  4645. * aborted by calling \c psa_verify_hash_abort().
  4646. *
  4647. * \param[in, out] operation The \c psa_verify_hash_interruptible_operation_t
  4648. * to use. This must be initialized first.
  4649. *
  4650. * \param key Identifier of the key to use for the operation.
  4651. * The key must allow the usage
  4652. * #PSA_KEY_USAGE_VERIFY_HASH.
  4653. * \param alg A signature algorithm (\c PSA_ALG_XXX
  4654. * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
  4655. * is true), that is compatible with
  4656. * the type of \p key.
  4657. * \param[in] hash The hash whose signature is to be verified.
  4658. * \param hash_length Size of the \p hash buffer in bytes.
  4659. * \param[in] signature Buffer containing the signature to verify.
  4660. * \param signature_length Size of the \p signature buffer in bytes.
  4661. *
  4662. * \retval #PSA_SUCCESS
  4663. * The operation started successfully - please call \c
  4664. * psa_verify_hash_complete() with the same context to complete the
  4665. * operation.
  4666. *
  4667. * \retval #PSA_ERROR_BAD_STATE
  4668. * Another operation has already been started on this context, and is
  4669. * still in progress.
  4670. *
  4671. * \retval #PSA_ERROR_NOT_PERMITTED
  4672. * The key does not have the #PSA_KEY_USAGE_VERIFY_HASH flag, or it does
  4673. * not permit the requested algorithm.
  4674. *
  4675. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  4676. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  4677. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  4678. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  4679. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  4680. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  4681. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  4682. * \retval PSA_ERROR_DATA_CORRUPT \emptydescription
  4683. * \retval PSA_ERROR_DATA_INVALID \emptydescription
  4684. * \retval #PSA_ERROR_BAD_STATE
  4685. * The library has not been previously initialized by psa_crypto_init().
  4686. * It is implementation-dependent whether a failure to initialize
  4687. * results in this error code.
  4688. */
  4689. psa_status_t psa_verify_hash_start(
  4690. psa_verify_hash_interruptible_operation_t *operation,
  4691. mbedtls_svc_key_id_t key, psa_algorithm_t alg,
  4692. const uint8_t *hash, size_t hash_length,
  4693. const uint8_t *signature, size_t signature_length);
  4694. /**
  4695. * \brief Continue and eventually complete the action of
  4696. * reading and verifying a hash or short message
  4697. * signed with a private key, in an interruptible
  4698. * manner.
  4699. *
  4700. * \see \c psa_verify_hash_start()
  4701. *
  4702. * \warning This is a beta API, and thus subject to change
  4703. * at any point. It is not bound by the usual
  4704. * interface stability promises.
  4705. *
  4706. * \note This function combined with \c
  4707. * psa_verify_hash_start() is equivalent to
  4708. * \c psa_verify_hash() but this function can
  4709. * return early and resume according to the limit
  4710. * set with \c psa_interruptible_set_max_ops() to
  4711. * reduce the maximum time spent in a function
  4712. * call.
  4713. *
  4714. * \note Users should call this function on the same
  4715. * operation object repeatedly until it either
  4716. * returns 0 or an error. This function will return
  4717. * #PSA_OPERATION_INCOMPLETE if there is more work
  4718. * to do. Alternatively users can call
  4719. * \c psa_verify_hash_abort() at any point if they
  4720. * no longer want the result.
  4721. *
  4722. * \note When this function returns successfully, the
  4723. * operation becomes inactive. If this function
  4724. * returns an error status, the operation enters an
  4725. * error state and must be aborted by calling
  4726. * \c psa_verify_hash_abort().
  4727. *
  4728. * \param[in, out] operation The \c psa_verify_hash_interruptible_operation_t
  4729. * to use. This must be initialized first, and have
  4730. * had \c psa_verify_hash_start() called with it
  4731. * first.
  4732. *
  4733. * \retval #PSA_SUCCESS
  4734. * Operation completed successfully, and the passed signature is valid.
  4735. *
  4736. * \retval #PSA_OPERATION_INCOMPLETE
  4737. * Operation was interrupted due to the setting of \c
  4738. * psa_interruptible_set_max_ops(). There is still work to be done.
  4739. * Call this function again with the same operation object.
  4740. *
  4741. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  4742. * \retval #PSA_ERROR_INVALID_SIGNATURE
  4743. * The calculation was performed successfully, but the passed
  4744. * signature is not a valid signature.
  4745. * \retval #PSA_ERROR_BAD_STATE
  4746. * An operation was not previously started on this context via
  4747. * \c psa_verify_hash_start().
  4748. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  4749. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  4750. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  4751. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  4752. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  4753. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  4754. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  4755. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  4756. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  4757. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  4758. * \retval #PSA_ERROR_BAD_STATE
  4759. * The library has either not been previously initialized by
  4760. * psa_crypto_init() or you did not previously call
  4761. * psa_verify_hash_start() on this object. It is
  4762. * implementation-dependent whether a failure to initialize results in
  4763. * this error code.
  4764. */
  4765. psa_status_t psa_verify_hash_complete(
  4766. psa_verify_hash_interruptible_operation_t *operation);
  4767. /**
  4768. * \brief Abort a verify hash operation.
  4769. *
  4770. * \warning This is a beta API, and thus subject to change at
  4771. * any point. It is not bound by the usual interface
  4772. * stability promises.
  4773. *
  4774. * \note This function is the only function that clears the
  4775. * number of ops completed as part of the operation.
  4776. * Please ensure you copy this value via
  4777. * \c psa_verify_hash_get_num_ops() if required
  4778. * before calling.
  4779. *
  4780. * \note Aborting an operation frees all associated
  4781. * resources except for the operation structure
  4782. * itself. Once aborted, the operation object can be
  4783. * reused for another operation by calling \c
  4784. * psa_verify_hash_start() again.
  4785. *
  4786. * \note You may call this function any time after the
  4787. * operation object has been initialized.
  4788. * In particular, calling \c psa_verify_hash_abort()
  4789. * after the operation has already been terminated by
  4790. * a call to \c psa_verify_hash_abort() or
  4791. * psa_verify_hash_complete() is safe.
  4792. *
  4793. * \param[in,out] operation Initialized verify hash operation.
  4794. *
  4795. * \retval #PSA_SUCCESS
  4796. * The operation was aborted successfully.
  4797. *
  4798. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  4799. * \retval #PSA_ERROR_BAD_STATE
  4800. * The library has not been previously initialized by psa_crypto_init().
  4801. * It is implementation-dependent whether a failure to initialize
  4802. * results in this error code.
  4803. */
  4804. psa_status_t psa_verify_hash_abort(
  4805. psa_verify_hash_interruptible_operation_t *operation);
  4806. /**@}*/
  4807. #ifdef __cplusplus
  4808. }
  4809. #endif
  4810. /* The file "crypto_extra.h" contains vendor-specific definitions. This
  4811. * can include vendor-defined algorithms, extra functions, etc. */
  4812. #include "crypto_extra.h"
  4813. #endif /* PSA_CRYPTO_H */