crypto.h 225 KB

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