crypto.h 178 KB

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