2
0

crypto.h 187 KB

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