crypto.h 240 KB

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