crypto.h 179 KB

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