psa_crypto.c 188 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395
  1. /*
  2. * PSA crypto layer on top of Mbed TLS crypto
  3. */
  4. /*
  5. * Copyright The Mbed TLS Contributors
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. * not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include "common.h"
  21. #if defined(MBEDTLS_PSA_CRYPTO_C)
  22. #if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
  23. #include "check_crypto_config.h"
  24. #endif
  25. #include "psa/crypto.h"
  26. #include "psa_crypto_cipher.h"
  27. #include "psa_crypto_core.h"
  28. #include "psa_crypto_invasive.h"
  29. #include "psa_crypto_driver_wrappers.h"
  30. #include "psa_crypto_ecp.h"
  31. #include "psa_crypto_hash.h"
  32. #include "psa_crypto_mac.h"
  33. #include "psa_crypto_rsa.h"
  34. #include "psa_crypto_ecp.h"
  35. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  36. #include "psa_crypto_se.h"
  37. #endif
  38. #include "psa_crypto_slot_management.h"
  39. /* Include internal declarations that are useful for implementing persistently
  40. * stored keys. */
  41. #include "psa_crypto_storage.h"
  42. #include "psa_crypto_random_impl.h"
  43. #include <assert.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include "mbedtls/platform.h"
  47. #if !defined(MBEDTLS_PLATFORM_C)
  48. #define mbedtls_calloc calloc
  49. #define mbedtls_free free
  50. #endif
  51. #include "mbedtls/aes.h"
  52. #include "mbedtls/arc4.h"
  53. #include "mbedtls/asn1.h"
  54. #include "mbedtls/asn1write.h"
  55. #include "mbedtls/bignum.h"
  56. #include "mbedtls/blowfish.h"
  57. #include "mbedtls/camellia.h"
  58. #include "mbedtls/chacha20.h"
  59. #include "mbedtls/chachapoly.h"
  60. #include "mbedtls/cipher.h"
  61. #include "mbedtls/ccm.h"
  62. #include "mbedtls/cmac.h"
  63. #include "mbedtls/des.h"
  64. #include "mbedtls/ecdh.h"
  65. #include "mbedtls/ecp.h"
  66. #include "mbedtls/entropy.h"
  67. #include "mbedtls/error.h"
  68. #include "mbedtls/gcm.h"
  69. #include "mbedtls/md2.h"
  70. #include "mbedtls/md4.h"
  71. #include "mbedtls/md5.h"
  72. #include "mbedtls/md.h"
  73. #include "mbedtls/md_internal.h"
  74. #include "mbedtls/pk.h"
  75. #include "mbedtls/pk_internal.h"
  76. #include "mbedtls/platform_util.h"
  77. #include "mbedtls/error.h"
  78. #include "mbedtls/ripemd160.h"
  79. #include "mbedtls/rsa.h"
  80. #include "mbedtls/sha1.h"
  81. #include "mbedtls/sha256.h"
  82. #include "mbedtls/sha512.h"
  83. #include "mbedtls/xtea.h"
  84. #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
  85. /****************************************************************/
  86. /* Global data, support functions and library management */
  87. /****************************************************************/
  88. static int key_type_is_raw_bytes( psa_key_type_t type )
  89. {
  90. return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
  91. }
  92. /* Values for psa_global_data_t::rng_state */
  93. #define RNG_NOT_INITIALIZED 0
  94. #define RNG_INITIALIZED 1
  95. #define RNG_SEEDED 2
  96. typedef struct
  97. {
  98. mbedtls_psa_random_context_t rng;
  99. unsigned initialized : 1;
  100. unsigned rng_state : 2;
  101. } psa_global_data_t;
  102. static psa_global_data_t global_data;
  103. #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
  104. mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
  105. &global_data.rng.drbg;
  106. #endif
  107. #define GUARD_MODULE_INITIALIZED \
  108. if( global_data.initialized == 0 ) \
  109. return( PSA_ERROR_BAD_STATE );
  110. psa_status_t mbedtls_to_psa_error( int ret )
  111. {
  112. /* Mbed TLS error codes can combine a high-level error code and a
  113. * low-level error code. The low-level error usually reflects the
  114. * root cause better, so dispatch on that preferably. */
  115. int low_level_ret = - ( -ret & 0x007f );
  116. switch( low_level_ret != 0 ? low_level_ret : ret )
  117. {
  118. case 0:
  119. return( PSA_SUCCESS );
  120. case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
  121. case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
  122. case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
  123. return( PSA_ERROR_NOT_SUPPORTED );
  124. case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
  125. return( PSA_ERROR_HARDWARE_FAILURE );
  126. case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
  127. return( PSA_ERROR_HARDWARE_FAILURE );
  128. case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
  129. case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
  130. case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
  131. case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
  132. case MBEDTLS_ERR_ASN1_INVALID_DATA:
  133. return( PSA_ERROR_INVALID_ARGUMENT );
  134. case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
  135. return( PSA_ERROR_INSUFFICIENT_MEMORY );
  136. case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
  137. return( PSA_ERROR_BUFFER_TOO_SMALL );
  138. #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
  139. case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
  140. #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
  141. case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
  142. #endif
  143. case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
  144. return( PSA_ERROR_NOT_SUPPORTED );
  145. case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
  146. return( PSA_ERROR_HARDWARE_FAILURE );
  147. #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
  148. case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
  149. #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
  150. case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
  151. #endif
  152. case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
  153. return( PSA_ERROR_NOT_SUPPORTED );
  154. case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
  155. return( PSA_ERROR_HARDWARE_FAILURE );
  156. case MBEDTLS_ERR_CCM_BAD_INPUT:
  157. return( PSA_ERROR_INVALID_ARGUMENT );
  158. case MBEDTLS_ERR_CCM_AUTH_FAILED:
  159. return( PSA_ERROR_INVALID_SIGNATURE );
  160. case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
  161. return( PSA_ERROR_HARDWARE_FAILURE );
  162. case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
  163. return( PSA_ERROR_INVALID_ARGUMENT );
  164. case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
  165. return( PSA_ERROR_BAD_STATE );
  166. case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
  167. return( PSA_ERROR_INVALID_SIGNATURE );
  168. case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
  169. return( PSA_ERROR_NOT_SUPPORTED );
  170. case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
  171. return( PSA_ERROR_INVALID_ARGUMENT );
  172. case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
  173. return( PSA_ERROR_INSUFFICIENT_MEMORY );
  174. case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
  175. return( PSA_ERROR_INVALID_PADDING );
  176. case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
  177. return( PSA_ERROR_INVALID_ARGUMENT );
  178. case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
  179. return( PSA_ERROR_INVALID_SIGNATURE );
  180. case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
  181. return( PSA_ERROR_CORRUPTION_DETECTED );
  182. case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
  183. return( PSA_ERROR_HARDWARE_FAILURE );
  184. case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
  185. return( PSA_ERROR_HARDWARE_FAILURE );
  186. #if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
  187. defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
  188. /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
  189. * functions are passed a CTR_DRBG instance. */
  190. case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
  191. return( PSA_ERROR_INSUFFICIENT_ENTROPY );
  192. case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
  193. case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
  194. return( PSA_ERROR_NOT_SUPPORTED );
  195. case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
  196. return( PSA_ERROR_INSUFFICIENT_ENTROPY );
  197. #endif
  198. case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
  199. return( PSA_ERROR_NOT_SUPPORTED );
  200. case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
  201. return( PSA_ERROR_HARDWARE_FAILURE );
  202. case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
  203. case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
  204. case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
  205. return( PSA_ERROR_INSUFFICIENT_ENTROPY );
  206. case MBEDTLS_ERR_GCM_AUTH_FAILED:
  207. return( PSA_ERROR_INVALID_SIGNATURE );
  208. case MBEDTLS_ERR_GCM_BAD_INPUT:
  209. return( PSA_ERROR_INVALID_ARGUMENT );
  210. case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
  211. return( PSA_ERROR_HARDWARE_FAILURE );
  212. #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
  213. defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
  214. /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
  215. * functions are passed a HMAC_DRBG instance. */
  216. case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
  217. return( PSA_ERROR_INSUFFICIENT_ENTROPY );
  218. case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
  219. case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
  220. return( PSA_ERROR_NOT_SUPPORTED );
  221. case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
  222. return( PSA_ERROR_INSUFFICIENT_ENTROPY );
  223. #endif
  224. case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
  225. case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
  226. case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
  227. return( PSA_ERROR_HARDWARE_FAILURE );
  228. case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
  229. return( PSA_ERROR_NOT_SUPPORTED );
  230. case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
  231. return( PSA_ERROR_INVALID_ARGUMENT );
  232. case MBEDTLS_ERR_MD_ALLOC_FAILED:
  233. return( PSA_ERROR_INSUFFICIENT_MEMORY );
  234. case MBEDTLS_ERR_MD_FILE_IO_ERROR:
  235. return( PSA_ERROR_STORAGE_FAILURE );
  236. case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
  237. return( PSA_ERROR_HARDWARE_FAILURE );
  238. case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
  239. return( PSA_ERROR_STORAGE_FAILURE );
  240. case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
  241. return( PSA_ERROR_INVALID_ARGUMENT );
  242. case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
  243. return( PSA_ERROR_INVALID_ARGUMENT );
  244. case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
  245. return( PSA_ERROR_BUFFER_TOO_SMALL );
  246. case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
  247. return( PSA_ERROR_INVALID_ARGUMENT );
  248. case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
  249. return( PSA_ERROR_INVALID_ARGUMENT );
  250. case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
  251. return( PSA_ERROR_INVALID_ARGUMENT );
  252. case MBEDTLS_ERR_MPI_ALLOC_FAILED:
  253. return( PSA_ERROR_INSUFFICIENT_MEMORY );
  254. case MBEDTLS_ERR_PK_ALLOC_FAILED:
  255. return( PSA_ERROR_INSUFFICIENT_MEMORY );
  256. case MBEDTLS_ERR_PK_TYPE_MISMATCH:
  257. case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
  258. return( PSA_ERROR_INVALID_ARGUMENT );
  259. case MBEDTLS_ERR_PK_FILE_IO_ERROR:
  260. return( PSA_ERROR_STORAGE_FAILURE );
  261. case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
  262. case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
  263. return( PSA_ERROR_INVALID_ARGUMENT );
  264. case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
  265. return( PSA_ERROR_NOT_SUPPORTED );
  266. case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
  267. case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
  268. return( PSA_ERROR_NOT_PERMITTED );
  269. case MBEDTLS_ERR_PK_INVALID_PUBKEY:
  270. return( PSA_ERROR_INVALID_ARGUMENT );
  271. case MBEDTLS_ERR_PK_INVALID_ALG:
  272. case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
  273. case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
  274. return( PSA_ERROR_NOT_SUPPORTED );
  275. case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
  276. return( PSA_ERROR_INVALID_SIGNATURE );
  277. case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
  278. return( PSA_ERROR_HARDWARE_FAILURE );
  279. case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
  280. return( PSA_ERROR_HARDWARE_FAILURE );
  281. case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
  282. return( PSA_ERROR_NOT_SUPPORTED );
  283. case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
  284. return( PSA_ERROR_HARDWARE_FAILURE );
  285. case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
  286. return( PSA_ERROR_INVALID_ARGUMENT );
  287. case MBEDTLS_ERR_RSA_INVALID_PADDING:
  288. return( PSA_ERROR_INVALID_PADDING );
  289. case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
  290. return( PSA_ERROR_HARDWARE_FAILURE );
  291. case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
  292. return( PSA_ERROR_INVALID_ARGUMENT );
  293. case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
  294. case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
  295. return( PSA_ERROR_CORRUPTION_DETECTED );
  296. case MBEDTLS_ERR_RSA_VERIFY_FAILED:
  297. return( PSA_ERROR_INVALID_SIGNATURE );
  298. case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
  299. return( PSA_ERROR_BUFFER_TOO_SMALL );
  300. case MBEDTLS_ERR_RSA_RNG_FAILED:
  301. return( PSA_ERROR_INSUFFICIENT_ENTROPY );
  302. case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
  303. return( PSA_ERROR_NOT_SUPPORTED );
  304. case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
  305. return( PSA_ERROR_HARDWARE_FAILURE );
  306. case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
  307. case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
  308. case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
  309. return( PSA_ERROR_HARDWARE_FAILURE );
  310. case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
  311. return( PSA_ERROR_INVALID_ARGUMENT );
  312. case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
  313. return( PSA_ERROR_HARDWARE_FAILURE );
  314. case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
  315. case MBEDTLS_ERR_ECP_INVALID_KEY:
  316. return( PSA_ERROR_INVALID_ARGUMENT );
  317. case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
  318. return( PSA_ERROR_BUFFER_TOO_SMALL );
  319. case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
  320. return( PSA_ERROR_NOT_SUPPORTED );
  321. case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
  322. case MBEDTLS_ERR_ECP_VERIFY_FAILED:
  323. return( PSA_ERROR_INVALID_SIGNATURE );
  324. case MBEDTLS_ERR_ECP_ALLOC_FAILED:
  325. return( PSA_ERROR_INSUFFICIENT_MEMORY );
  326. case MBEDTLS_ERR_ECP_RANDOM_FAILED:
  327. return( PSA_ERROR_INSUFFICIENT_ENTROPY );
  328. case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
  329. return( PSA_ERROR_HARDWARE_FAILURE );
  330. case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
  331. return( PSA_ERROR_CORRUPTION_DETECTED );
  332. default:
  333. return( PSA_ERROR_GENERIC_ERROR );
  334. }
  335. }
  336. /****************************************************************/
  337. /* Key management */
  338. /****************************************************************/
  339. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  340. static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
  341. {
  342. return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
  343. }
  344. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  345. /* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
  346. * current test driver in key_management.c is using this function
  347. * when accelerators are used for ECC key pair and public key.
  348. * Once that dependency is resolved these guards can be removed.
  349. */
  350. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
  351. defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
  352. defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
  353. defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
  354. mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
  355. size_t bits,
  356. int bits_is_sloppy )
  357. {
  358. switch( curve )
  359. {
  360. case PSA_ECC_FAMILY_SECP_R1:
  361. switch( bits )
  362. {
  363. #if defined(PSA_WANT_ECC_SECP_R1_192)
  364. case 192:
  365. return( MBEDTLS_ECP_DP_SECP192R1 );
  366. #endif
  367. #if defined(PSA_WANT_ECC_SECP_R1_224)
  368. case 224:
  369. return( MBEDTLS_ECP_DP_SECP224R1 );
  370. #endif
  371. #if defined(PSA_WANT_ECC_SECP_R1_256)
  372. case 256:
  373. return( MBEDTLS_ECP_DP_SECP256R1 );
  374. #endif
  375. #if defined(PSA_WANT_ECC_SECP_R1_384)
  376. case 384:
  377. return( MBEDTLS_ECP_DP_SECP384R1 );
  378. #endif
  379. #if defined(PSA_WANT_ECC_SECP_R1_521)
  380. case 521:
  381. return( MBEDTLS_ECP_DP_SECP521R1 );
  382. case 528:
  383. if( bits_is_sloppy )
  384. return( MBEDTLS_ECP_DP_SECP521R1 );
  385. break;
  386. #endif
  387. }
  388. break;
  389. case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
  390. switch( bits )
  391. {
  392. #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
  393. case 256:
  394. return( MBEDTLS_ECP_DP_BP256R1 );
  395. #endif
  396. #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
  397. case 384:
  398. return( MBEDTLS_ECP_DP_BP384R1 );
  399. #endif
  400. #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
  401. case 512:
  402. return( MBEDTLS_ECP_DP_BP512R1 );
  403. #endif
  404. }
  405. break;
  406. case PSA_ECC_FAMILY_MONTGOMERY:
  407. switch( bits )
  408. {
  409. #if defined(PSA_WANT_ECC_MONTGOMERY_255)
  410. case 255:
  411. return( MBEDTLS_ECP_DP_CURVE25519 );
  412. case 256:
  413. if( bits_is_sloppy )
  414. return( MBEDTLS_ECP_DP_CURVE25519 );
  415. break;
  416. #endif
  417. #if defined(PSA_WANT_ECC_MONTGOMERY_448)
  418. case 448:
  419. return( MBEDTLS_ECP_DP_CURVE448 );
  420. #endif
  421. }
  422. break;
  423. case PSA_ECC_FAMILY_SECP_K1:
  424. switch( bits )
  425. {
  426. #if defined(PSA_WANT_ECC_SECP_K1_192)
  427. case 192:
  428. return( MBEDTLS_ECP_DP_SECP192K1 );
  429. #endif
  430. #if defined(PSA_WANT_ECC_SECP_K1_224)
  431. case 224:
  432. return( MBEDTLS_ECP_DP_SECP224K1 );
  433. #endif
  434. #if defined(PSA_WANT_ECC_SECP_K1_256)
  435. case 256:
  436. return( MBEDTLS_ECP_DP_SECP256K1 );
  437. #endif
  438. }
  439. break;
  440. }
  441. (void) bits_is_sloppy;
  442. return( MBEDTLS_ECP_DP_NONE );
  443. }
  444. #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
  445. * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
  446. * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
  447. * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
  448. static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
  449. size_t bits )
  450. {
  451. /* Check that the bit size is acceptable for the key type */
  452. switch( type )
  453. {
  454. case PSA_KEY_TYPE_RAW_DATA:
  455. case PSA_KEY_TYPE_HMAC:
  456. case PSA_KEY_TYPE_DERIVE:
  457. break;
  458. #if defined(PSA_WANT_KEY_TYPE_AES)
  459. case PSA_KEY_TYPE_AES:
  460. if( bits != 128 && bits != 192 && bits != 256 )
  461. return( PSA_ERROR_INVALID_ARGUMENT );
  462. break;
  463. #endif
  464. #if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
  465. case PSA_KEY_TYPE_CAMELLIA:
  466. if( bits != 128 && bits != 192 && bits != 256 )
  467. return( PSA_ERROR_INVALID_ARGUMENT );
  468. break;
  469. #endif
  470. #if defined(PSA_WANT_KEY_TYPE_DES)
  471. case PSA_KEY_TYPE_DES:
  472. if( bits != 64 && bits != 128 && bits != 192 )
  473. return( PSA_ERROR_INVALID_ARGUMENT );
  474. break;
  475. #endif
  476. #if defined(PSA_WANT_KEY_TYPE_ARC4)
  477. case PSA_KEY_TYPE_ARC4:
  478. if( bits < 8 || bits > 2048 )
  479. return( PSA_ERROR_INVALID_ARGUMENT );
  480. break;
  481. #endif
  482. #if defined(PSA_WANT_KEY_TYPE_CHACHA20)
  483. case PSA_KEY_TYPE_CHACHA20:
  484. if( bits != 256 )
  485. return( PSA_ERROR_INVALID_ARGUMENT );
  486. break;
  487. #endif
  488. default:
  489. return( PSA_ERROR_NOT_SUPPORTED );
  490. }
  491. if( bits % 8 != 0 )
  492. return( PSA_ERROR_INVALID_ARGUMENT );
  493. return( PSA_SUCCESS );
  494. }
  495. /** Check whether a given key type is valid for use with a given MAC algorithm
  496. *
  497. * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
  498. * when called with the validated \p algorithm and \p key_type is well-defined.
  499. *
  500. * \param[in] algorithm The specific MAC algorithm (can be wildcard).
  501. * \param[in] key_type The key type of the key to be used with the
  502. * \p algorithm.
  503. *
  504. * \retval #PSA_SUCCESS
  505. * The \p key_type is valid for use with the \p algorithm
  506. * \retval #PSA_ERROR_INVALID_ARGUMENT
  507. * The \p key_type is not valid for use with the \p algorithm
  508. */
  509. MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
  510. psa_algorithm_t algorithm,
  511. psa_key_type_t key_type )
  512. {
  513. if( PSA_ALG_IS_HMAC( algorithm ) )
  514. {
  515. if( key_type == PSA_KEY_TYPE_HMAC )
  516. return( PSA_SUCCESS );
  517. }
  518. if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) )
  519. {
  520. /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
  521. * key. */
  522. if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) ==
  523. PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
  524. {
  525. /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
  526. * the block length (larger than 1) for block ciphers. */
  527. if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 )
  528. return( PSA_SUCCESS );
  529. }
  530. }
  531. return( PSA_ERROR_INVALID_ARGUMENT );
  532. }
  533. psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
  534. size_t buffer_length )
  535. {
  536. if( slot->key.data != NULL )
  537. return( PSA_ERROR_ALREADY_EXISTS );
  538. slot->key.data = mbedtls_calloc( 1, buffer_length );
  539. if( slot->key.data == NULL )
  540. return( PSA_ERROR_INSUFFICIENT_MEMORY );
  541. slot->key.bytes = buffer_length;
  542. return( PSA_SUCCESS );
  543. }
  544. psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
  545. const uint8_t* data,
  546. size_t data_length )
  547. {
  548. psa_status_t status = psa_allocate_buffer_to_slot( slot,
  549. data_length );
  550. if( status != PSA_SUCCESS )
  551. return( status );
  552. memcpy( slot->key.data, data, data_length );
  553. return( PSA_SUCCESS );
  554. }
  555. psa_status_t psa_import_key_into_slot(
  556. const psa_key_attributes_t *attributes,
  557. const uint8_t *data, size_t data_length,
  558. uint8_t *key_buffer, size_t key_buffer_size,
  559. size_t *key_buffer_length, size_t *bits )
  560. {
  561. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  562. psa_key_type_t type = attributes->core.type;
  563. /* zero-length keys are never supported. */
  564. if( data_length == 0 )
  565. return( PSA_ERROR_NOT_SUPPORTED );
  566. if( key_type_is_raw_bytes( type ) )
  567. {
  568. *bits = PSA_BYTES_TO_BITS( data_length );
  569. /* Ensure that the bytes-to-bits conversion hasn't overflown. */
  570. if( data_length > SIZE_MAX / 8 )
  571. return( PSA_ERROR_NOT_SUPPORTED );
  572. /* Enforce a size limit, and in particular ensure that the bit
  573. * size fits in its representation type. */
  574. if( ( *bits ) > PSA_MAX_KEY_BITS )
  575. return( PSA_ERROR_NOT_SUPPORTED );
  576. status = validate_unstructured_key_bit_size( type, *bits );
  577. if( status != PSA_SUCCESS )
  578. return( status );
  579. /* Copy the key material. */
  580. memcpy( key_buffer, data, data_length );
  581. *key_buffer_length = data_length;
  582. (void)key_buffer_size;
  583. return( PSA_SUCCESS );
  584. }
  585. else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
  586. {
  587. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
  588. defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
  589. if( PSA_KEY_TYPE_IS_ECC( type ) )
  590. {
  591. return( mbedtls_psa_ecp_import_key( attributes,
  592. data, data_length,
  593. key_buffer, key_buffer_size,
  594. key_buffer_length,
  595. bits ) );
  596. }
  597. #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
  598. * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
  599. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
  600. defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
  601. if( PSA_KEY_TYPE_IS_RSA( type ) )
  602. {
  603. return( mbedtls_psa_rsa_import_key( attributes,
  604. data, data_length,
  605. key_buffer, key_buffer_size,
  606. key_buffer_length,
  607. bits ) );
  608. }
  609. #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
  610. * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
  611. }
  612. return( PSA_ERROR_NOT_SUPPORTED );
  613. }
  614. /** Calculate the intersection of two algorithm usage policies.
  615. *
  616. * Return 0 (which allows no operation) on incompatibility.
  617. */
  618. static psa_algorithm_t psa_key_policy_algorithm_intersection(
  619. psa_key_type_t key_type,
  620. psa_algorithm_t alg1,
  621. psa_algorithm_t alg2 )
  622. {
  623. /* Common case: both sides actually specify the same policy. */
  624. if( alg1 == alg2 )
  625. return( alg1 );
  626. /* If the policies are from the same hash-and-sign family, check
  627. * if one is a wildcard. If so the other has the specific algorithm. */
  628. if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
  629. PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
  630. ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
  631. {
  632. if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
  633. return( alg2 );
  634. if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
  635. return( alg1 );
  636. }
  637. /* If the policies are from the same AEAD family, check whether
  638. * one of them is a minimum-tag-length wildcard. Calculate the most
  639. * restrictive tag length. */
  640. if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
  641. ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
  642. PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
  643. {
  644. size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
  645. size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
  646. size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
  647. /* If both are wildcards, return most restrictive wildcard */
  648. if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
  649. ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
  650. {
  651. return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
  652. alg1, restricted_len ) );
  653. }
  654. /* If only one is a wildcard, return specific algorithm if compatible. */
  655. if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
  656. ( alg1_len <= alg2_len ) )
  657. {
  658. return( alg2 );
  659. }
  660. if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
  661. ( alg2_len <= alg1_len ) )
  662. {
  663. return( alg1 );
  664. }
  665. }
  666. /* If the policies are from the same MAC family, check whether one
  667. * of them is a minimum-MAC-length policy. Calculate the most
  668. * restrictive tag length. */
  669. if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
  670. ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
  671. PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
  672. {
  673. /* Validate the combination of key type and algorithm. Since the base
  674. * algorithm of alg1 and alg2 are the same, we only need this once. */
  675. if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) )
  676. return( 0 );
  677. /* Get the (exact or at-least) output lengths for both sides of the
  678. * requested intersection. None of the currently supported algorithms
  679. * have an output length dependent on the actual key size, so setting it
  680. * to a bogus value of 0 is currently OK.
  681. *
  682. * Note that for at-least-this-length wildcard algorithms, the output
  683. * length is set to the shortest allowed length, which allows us to
  684. * calculate the most restrictive tag length for the intersection. */
  685. size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 );
  686. size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 );
  687. size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
  688. /* If both are wildcards, return most restrictive wildcard */
  689. if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
  690. ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
  691. {
  692. return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) );
  693. }
  694. /* If only one is an at-least-this-length policy, the intersection would
  695. * be the other (fixed-length) policy as long as said fixed length is
  696. * equal to or larger than the shortest allowed length. */
  697. if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
  698. {
  699. return( ( alg1_len <= alg2_len ) ? alg2 : 0 );
  700. }
  701. if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
  702. {
  703. return( ( alg2_len <= alg1_len ) ? alg1 : 0 );
  704. }
  705. /* If none of them are wildcards, check whether they define the same tag
  706. * length. This is still possible here when one is default-length and
  707. * the other specific-length. Ensure to always return the
  708. * specific-length version for the intersection. */
  709. if( alg1_len == alg2_len )
  710. return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) );
  711. }
  712. /* If the policies are incompatible, allow nothing. */
  713. return( 0 );
  714. }
  715. static int psa_key_algorithm_permits( psa_key_type_t key_type,
  716. psa_algorithm_t policy_alg,
  717. psa_algorithm_t requested_alg )
  718. {
  719. /* Common case: the policy only allows requested_alg. */
  720. if( requested_alg == policy_alg )
  721. return( 1 );
  722. /* If policy_alg is a hash-and-sign with a wildcard for the hash,
  723. * and requested_alg is the same hash-and-sign family with any hash,
  724. * then requested_alg is compliant with policy_alg. */
  725. if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
  726. PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
  727. {
  728. return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
  729. ( requested_alg & ~PSA_ALG_HASH_MASK ) );
  730. }
  731. /* If policy_alg is a wildcard AEAD algorithm of the same base as
  732. * the requested algorithm, check the requested tag length to be
  733. * equal-length or longer than the wildcard-specified length. */
  734. if( PSA_ALG_IS_AEAD( policy_alg ) &&
  735. PSA_ALG_IS_AEAD( requested_alg ) &&
  736. ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
  737. PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
  738. ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
  739. {
  740. return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
  741. PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
  742. }
  743. /* If policy_alg is a MAC algorithm of the same base as the requested
  744. * algorithm, check whether their MAC lengths are compatible. */
  745. if( PSA_ALG_IS_MAC( policy_alg ) &&
  746. PSA_ALG_IS_MAC( requested_alg ) &&
  747. ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
  748. PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) )
  749. {
  750. /* Validate the combination of key type and algorithm. Since the policy
  751. * and requested algorithms are the same, we only need this once. */
  752. if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) )
  753. return( 0 );
  754. /* Get both the requested output length for the algorithm which is to be
  755. * verified, and the default output length for the base algorithm.
  756. * Note that none of the currently supported algorithms have an output
  757. * length dependent on actual key size, so setting it to a bogus value
  758. * of 0 is currently OK. */
  759. size_t requested_output_length = PSA_MAC_LENGTH(
  760. key_type, 0, requested_alg );
  761. size_t default_output_length = PSA_MAC_LENGTH(
  762. key_type, 0,
  763. PSA_ALG_FULL_LENGTH_MAC( requested_alg ) );
  764. /* If the policy is default-length, only allow an algorithm with
  765. * a declared exact-length matching the default. */
  766. if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 )
  767. return( requested_output_length == default_output_length );
  768. /* If the requested algorithm is default-length, allow it if the policy
  769. * length exactly matches the default length. */
  770. if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 &&
  771. PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length )
  772. {
  773. return( 1 );
  774. }
  775. /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
  776. * check for the requested MAC length to be equal to or longer than the
  777. * minimum allowed length. */
  778. if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
  779. {
  780. return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
  781. requested_output_length );
  782. }
  783. }
  784. /* If policy_alg is a generic key agreement operation, then using it for
  785. * a key derivation with that key agreement should also be allowed. This
  786. * behaviour is expected to be defined in a future specification version. */
  787. if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
  788. PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
  789. {
  790. return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
  791. policy_alg );
  792. }
  793. /* If it isn't explicitly permitted, it's forbidden. */
  794. return( 0 );
  795. }
  796. /** Test whether a policy permits an algorithm.
  797. *
  798. * The caller must test usage flags separately.
  799. *
  800. * \note This function requires providing the key type for which the policy is
  801. * being validated, since some algorithm policy definitions (e.g. MAC)
  802. * have different properties depending on what kind of cipher it is
  803. * combined with.
  804. *
  805. * \retval PSA_SUCCESS When \p alg is a specific algorithm
  806. * allowed by the \p policy.
  807. * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
  808. * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
  809. * the \p policy does not allow it.
  810. */
  811. static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy,
  812. psa_key_type_t key_type,
  813. psa_algorithm_t alg )
  814. {
  815. /* '0' is not a valid algorithm */
  816. if( alg == 0 )
  817. return( PSA_ERROR_INVALID_ARGUMENT );
  818. /* A requested algorithm cannot be a wildcard. */
  819. if( PSA_ALG_IS_WILDCARD( alg ) )
  820. return( PSA_ERROR_INVALID_ARGUMENT );
  821. if( psa_key_algorithm_permits( key_type, policy->alg, alg ) ||
  822. psa_key_algorithm_permits( key_type, policy->alg2, alg ) )
  823. return( PSA_SUCCESS );
  824. else
  825. return( PSA_ERROR_NOT_PERMITTED );
  826. }
  827. /** Restrict a key policy based on a constraint.
  828. *
  829. * \note This function requires providing the key type for which the policy is
  830. * being restricted, since some algorithm policy definitions (e.g. MAC)
  831. * have different properties depending on what kind of cipher it is
  832. * combined with.
  833. *
  834. * \param[in] key_type The key type for which to restrict the policy
  835. * \param[in,out] policy The policy to restrict.
  836. * \param[in] constraint The policy constraint to apply.
  837. *
  838. * \retval #PSA_SUCCESS
  839. * \c *policy contains the intersection of the original value of
  840. * \c *policy and \c *constraint.
  841. * \retval #PSA_ERROR_INVALID_ARGUMENT
  842. * \c key_type, \c *policy and \c *constraint are incompatible.
  843. * \c *policy is unchanged.
  844. */
  845. static psa_status_t psa_restrict_key_policy(
  846. psa_key_type_t key_type,
  847. psa_key_policy_t *policy,
  848. const psa_key_policy_t *constraint )
  849. {
  850. psa_algorithm_t intersection_alg =
  851. psa_key_policy_algorithm_intersection( key_type, policy->alg,
  852. constraint->alg );
  853. psa_algorithm_t intersection_alg2 =
  854. psa_key_policy_algorithm_intersection( key_type, policy->alg2,
  855. constraint->alg2 );
  856. if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
  857. return( PSA_ERROR_INVALID_ARGUMENT );
  858. if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
  859. return( PSA_ERROR_INVALID_ARGUMENT );
  860. policy->usage &= constraint->usage;
  861. policy->alg = intersection_alg;
  862. policy->alg2 = intersection_alg2;
  863. return( PSA_SUCCESS );
  864. }
  865. /** Get the description of a key given its identifier and policy constraints
  866. * and lock it.
  867. *
  868. * The key must have allow all the usage flags set in \p usage. If \p alg is
  869. * nonzero, the key must allow operations with this algorithm. If \p alg is
  870. * zero, the algorithm is not checked.
  871. *
  872. * In case of a persistent key, the function loads the description of the key
  873. * into a key slot if not already done.
  874. *
  875. * On success, the returned key slot is locked. It is the responsibility of
  876. * the caller to unlock the key slot when it does not access it anymore.
  877. */
  878. static psa_status_t psa_get_and_lock_key_slot_with_policy(
  879. mbedtls_svc_key_id_t key,
  880. psa_key_slot_t **p_slot,
  881. psa_key_usage_t usage,
  882. psa_algorithm_t alg )
  883. {
  884. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  885. psa_key_slot_t *slot;
  886. status = psa_get_and_lock_key_slot( key, p_slot );
  887. if( status != PSA_SUCCESS )
  888. return( status );
  889. slot = *p_slot;
  890. /* Enforce that usage policy for the key slot contains all the flags
  891. * required by the usage parameter. There is one exception: public
  892. * keys can always be exported, so we treat public key objects as
  893. * if they had the export flag. */
  894. if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
  895. usage &= ~PSA_KEY_USAGE_EXPORT;
  896. if( ( slot->attr.policy.usage & usage ) != usage )
  897. {
  898. status = PSA_ERROR_NOT_PERMITTED;
  899. goto error;
  900. }
  901. /* Enforce that the usage policy permits the requested algortihm. */
  902. if( alg != 0 )
  903. {
  904. status = psa_key_policy_permits( &slot->attr.policy,
  905. slot->attr.type,
  906. alg );
  907. if( status != PSA_SUCCESS )
  908. goto error;
  909. }
  910. return( PSA_SUCCESS );
  911. error:
  912. *p_slot = NULL;
  913. psa_unlock_key_slot( slot );
  914. return( status );
  915. }
  916. /** Get a key slot containing a transparent key and lock it.
  917. *
  918. * A transparent key is a key for which the key material is directly
  919. * available, as opposed to a key in a secure element.
  920. *
  921. * This is a temporary function to use instead of
  922. * psa_get_and_lock_key_slot_with_policy() until secure element support is
  923. * fully implemented.
  924. *
  925. * On success, the returned key slot is locked. It is the responsibility of the
  926. * caller to unlock the key slot when it does not access it anymore.
  927. */
  928. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  929. static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
  930. mbedtls_svc_key_id_t key,
  931. psa_key_slot_t **p_slot,
  932. psa_key_usage_t usage,
  933. psa_algorithm_t alg )
  934. {
  935. psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
  936. usage, alg );
  937. if( status != PSA_SUCCESS )
  938. return( status );
  939. if( psa_key_slot_is_external( *p_slot ) )
  940. {
  941. psa_unlock_key_slot( *p_slot );
  942. *p_slot = NULL;
  943. return( PSA_ERROR_NOT_SUPPORTED );
  944. }
  945. return( PSA_SUCCESS );
  946. }
  947. #else /* MBEDTLS_PSA_CRYPTO_SE_C */
  948. /* With no secure element support, all keys are transparent. */
  949. #define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
  950. psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
  951. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  952. psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
  953. {
  954. /* Data pointer will always be either a valid pointer or NULL in an
  955. * initialized slot, so we can just free it. */
  956. if( slot->key.data != NULL )
  957. mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
  958. mbedtls_free( slot->key.data );
  959. slot->key.data = NULL;
  960. slot->key.bytes = 0;
  961. return( PSA_SUCCESS );
  962. }
  963. /** Completely wipe a slot in memory, including its policy.
  964. * Persistent storage is not affected. */
  965. psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
  966. {
  967. psa_status_t status = psa_remove_key_data_from_memory( slot );
  968. /*
  969. * As the return error code may not be handled in case of multiple errors,
  970. * do our best to report an unexpected lock counter: if available
  971. * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
  972. * part of the execution of a test suite this will stop the test suite
  973. * execution).
  974. */
  975. if( slot->lock_count != 1 )
  976. {
  977. #ifdef MBEDTLS_CHECK_PARAMS
  978. MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
  979. #endif
  980. status = PSA_ERROR_CORRUPTION_DETECTED;
  981. }
  982. /* Multipart operations may still be using the key. This is safe
  983. * because all multipart operation objects are independent from
  984. * the key slot: if they need to access the key after the setup
  985. * phase, they have a copy of the key. Note that this means that
  986. * key material can linger until all operations are completed. */
  987. /* At this point, key material and other type-specific content has
  988. * been wiped. Clear remaining metadata. We can call memset and not
  989. * zeroize because the metadata is not particularly sensitive. */
  990. memset( slot, 0, sizeof( *slot ) );
  991. return( status );
  992. }
  993. psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
  994. {
  995. psa_key_slot_t *slot;
  996. psa_status_t status; /* status of the last operation */
  997. psa_status_t overall_status = PSA_SUCCESS;
  998. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  999. psa_se_drv_table_entry_t *driver;
  1000. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  1001. if( mbedtls_svc_key_id_is_null( key ) )
  1002. return( PSA_SUCCESS );
  1003. /*
  1004. * Get the description of the key in a key slot. In case of a persistent
  1005. * key, this will load the key description from persistent memory if not
  1006. * done yet. We cannot avoid this loading as without it we don't know if
  1007. * the key is operated by an SE or not and this information is needed by
  1008. * the current implementation.
  1009. */
  1010. status = psa_get_and_lock_key_slot( key, &slot );
  1011. if( status != PSA_SUCCESS )
  1012. return( status );
  1013. /*
  1014. * If the key slot containing the key description is under access by the
  1015. * library (apart from the present access), the key cannot be destroyed
  1016. * yet. For the time being, just return in error. Eventually (to be
  1017. * implemented), the key should be destroyed when all accesses have
  1018. * stopped.
  1019. */
  1020. if( slot->lock_count > 1 )
  1021. {
  1022. psa_unlock_key_slot( slot );
  1023. return( PSA_ERROR_GENERIC_ERROR );
  1024. }
  1025. if( PSA_KEY_LIFETIME_IS_READ_ONLY( slot->attr.lifetime ) )
  1026. {
  1027. /* Refuse the destruction of a read-only key (which may or may not work
  1028. * if we attempt it, depending on whether the key is merely read-only
  1029. * by policy or actually physically read-only).
  1030. * Just do the best we can, which is to wipe the copy in memory
  1031. * (done in this function's cleanup code). */
  1032. overall_status = PSA_ERROR_NOT_PERMITTED;
  1033. goto exit;
  1034. }
  1035. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  1036. driver = psa_get_se_driver_entry( slot->attr.lifetime );
  1037. if( driver != NULL )
  1038. {
  1039. /* For a key in a secure element, we need to do three things:
  1040. * remove the key file in internal storage, destroy the
  1041. * key inside the secure element, and update the driver's
  1042. * persistent data. Start a transaction that will encompass these
  1043. * three actions. */
  1044. psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
  1045. psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
  1046. psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
  1047. psa_crypto_transaction.key.id = slot->attr.id;
  1048. status = psa_crypto_save_transaction( );
  1049. if( status != PSA_SUCCESS )
  1050. {
  1051. (void) psa_crypto_stop_transaction( );
  1052. /* We should still try to destroy the key in the secure
  1053. * element and the key metadata in storage. This is especially
  1054. * important if the error is that the storage is full.
  1055. * But how to do it exactly without risking an inconsistent
  1056. * state after a reset?
  1057. * https://github.com/ARMmbed/mbed-crypto/issues/215
  1058. */
  1059. overall_status = status;
  1060. goto exit;
  1061. }
  1062. status = psa_destroy_se_key( driver,
  1063. psa_key_slot_get_slot_number( slot ) );
  1064. if( overall_status == PSA_SUCCESS )
  1065. overall_status = status;
  1066. }
  1067. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  1068. #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
  1069. if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
  1070. {
  1071. status = psa_destroy_persistent_key( slot->attr.id );
  1072. if( overall_status == PSA_SUCCESS )
  1073. overall_status = status;
  1074. /* TODO: other slots may have a copy of the same key. We should
  1075. * invalidate them.
  1076. * https://github.com/ARMmbed/mbed-crypto/issues/214
  1077. */
  1078. }
  1079. #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
  1080. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  1081. if( driver != NULL )
  1082. {
  1083. status = psa_save_se_persistent_data( driver );
  1084. if( overall_status == PSA_SUCCESS )
  1085. overall_status = status;
  1086. status = psa_crypto_stop_transaction( );
  1087. if( overall_status == PSA_SUCCESS )
  1088. overall_status = status;
  1089. }
  1090. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  1091. exit:
  1092. status = psa_wipe_key_slot( slot );
  1093. /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
  1094. if( status != PSA_SUCCESS )
  1095. overall_status = status;
  1096. return( overall_status );
  1097. }
  1098. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
  1099. defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
  1100. static psa_status_t psa_get_rsa_public_exponent(
  1101. const mbedtls_rsa_context *rsa,
  1102. psa_key_attributes_t *attributes )
  1103. {
  1104. mbedtls_mpi mpi;
  1105. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1106. uint8_t *buffer = NULL;
  1107. size_t buflen;
  1108. mbedtls_mpi_init( &mpi );
  1109. ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
  1110. if( ret != 0 )
  1111. goto exit;
  1112. if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
  1113. {
  1114. /* It's the default value, which is reported as an empty string,
  1115. * so there's nothing to do. */
  1116. goto exit;
  1117. }
  1118. buflen = mbedtls_mpi_size( &mpi );
  1119. buffer = mbedtls_calloc( 1, buflen );
  1120. if( buffer == NULL )
  1121. {
  1122. ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
  1123. goto exit;
  1124. }
  1125. ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
  1126. if( ret != 0 )
  1127. goto exit;
  1128. attributes->domain_parameters = buffer;
  1129. attributes->domain_parameters_size = buflen;
  1130. exit:
  1131. mbedtls_mpi_free( &mpi );
  1132. if( ret != 0 )
  1133. mbedtls_free( buffer );
  1134. return( mbedtls_to_psa_error( ret ) );
  1135. }
  1136. #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
  1137. * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
  1138. /** Retrieve all the publicly-accessible attributes of a key.
  1139. */
  1140. psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
  1141. psa_key_attributes_t *attributes )
  1142. {
  1143. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1144. psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
  1145. psa_key_slot_t *slot;
  1146. psa_reset_key_attributes( attributes );
  1147. status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
  1148. if( status != PSA_SUCCESS )
  1149. return( status );
  1150. attributes->core = slot->attr;
  1151. attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
  1152. MBEDTLS_PSA_KA_MASK_DUAL_USE );
  1153. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  1154. if( psa_key_slot_is_external( slot ) )
  1155. psa_set_key_slot_number( attributes,
  1156. psa_key_slot_get_slot_number( slot ) );
  1157. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  1158. switch( slot->attr.type )
  1159. {
  1160. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
  1161. defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
  1162. case PSA_KEY_TYPE_RSA_KEY_PAIR:
  1163. case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
  1164. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  1165. /* TODO: reporting the public exponent for opaque keys
  1166. * is not yet implemented.
  1167. * https://github.com/ARMmbed/mbed-crypto/issues/216
  1168. */
  1169. if( psa_key_slot_is_external( slot ) )
  1170. break;
  1171. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  1172. {
  1173. mbedtls_rsa_context *rsa = NULL;
  1174. status = mbedtls_psa_rsa_load_representation(
  1175. slot->attr.type,
  1176. slot->key.data,
  1177. slot->key.bytes,
  1178. &rsa );
  1179. if( status != PSA_SUCCESS )
  1180. break;
  1181. status = psa_get_rsa_public_exponent( rsa,
  1182. attributes );
  1183. mbedtls_rsa_free( rsa );
  1184. mbedtls_free( rsa );
  1185. }
  1186. break;
  1187. #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
  1188. * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
  1189. default:
  1190. /* Nothing else to do. */
  1191. break;
  1192. }
  1193. if( status != PSA_SUCCESS )
  1194. psa_reset_key_attributes( attributes );
  1195. unlock_status = psa_unlock_key_slot( slot );
  1196. return( ( status == PSA_SUCCESS ) ? unlock_status : status );
  1197. }
  1198. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  1199. psa_status_t psa_get_key_slot_number(
  1200. const psa_key_attributes_t *attributes,
  1201. psa_key_slot_number_t *slot_number )
  1202. {
  1203. if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
  1204. {
  1205. *slot_number = attributes->slot_number;
  1206. return( PSA_SUCCESS );
  1207. }
  1208. else
  1209. return( PSA_ERROR_INVALID_ARGUMENT );
  1210. }
  1211. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  1212. static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
  1213. size_t key_buffer_size,
  1214. uint8_t *data,
  1215. size_t data_size,
  1216. size_t *data_length )
  1217. {
  1218. if( key_buffer_size > data_size )
  1219. return( PSA_ERROR_BUFFER_TOO_SMALL );
  1220. memcpy( data, key_buffer, key_buffer_size );
  1221. memset( data + key_buffer_size, 0,
  1222. data_size - key_buffer_size );
  1223. *data_length = key_buffer_size;
  1224. return( PSA_SUCCESS );
  1225. }
  1226. psa_status_t psa_export_key_internal(
  1227. const psa_key_attributes_t *attributes,
  1228. const uint8_t *key_buffer, size_t key_buffer_size,
  1229. uint8_t *data, size_t data_size, size_t *data_length )
  1230. {
  1231. psa_key_type_t type = attributes->core.type;
  1232. if( key_type_is_raw_bytes( type ) ||
  1233. PSA_KEY_TYPE_IS_RSA( type ) ||
  1234. PSA_KEY_TYPE_IS_ECC( type ) )
  1235. {
  1236. return( psa_export_key_buffer_internal(
  1237. key_buffer, key_buffer_size,
  1238. data, data_size, data_length ) );
  1239. }
  1240. else
  1241. {
  1242. /* This shouldn't happen in the reference implementation, but
  1243. it is valid for a special-purpose implementation to omit
  1244. support for exporting certain key types. */
  1245. return( PSA_ERROR_NOT_SUPPORTED );
  1246. }
  1247. }
  1248. psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
  1249. uint8_t *data,
  1250. size_t data_size,
  1251. size_t *data_length )
  1252. {
  1253. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1254. psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
  1255. psa_key_slot_t *slot;
  1256. /* Reject a zero-length output buffer now, since this can never be a
  1257. * valid key representation. This way we know that data must be a valid
  1258. * pointer and we can do things like memset(data, ..., data_size). */
  1259. if( data_size == 0 )
  1260. return( PSA_ERROR_BUFFER_TOO_SMALL );
  1261. /* Set the key to empty now, so that even when there are errors, we always
  1262. * set data_length to a value between 0 and data_size. On error, setting
  1263. * the key to empty is a good choice because an empty key representation is
  1264. * unlikely to be accepted anywhere. */
  1265. *data_length = 0;
  1266. /* Export requires the EXPORT flag. There is an exception for public keys,
  1267. * which don't require any flag, but
  1268. * psa_get_and_lock_key_slot_with_policy() takes care of this.
  1269. */
  1270. status = psa_get_and_lock_key_slot_with_policy( key, &slot,
  1271. PSA_KEY_USAGE_EXPORT, 0 );
  1272. if( status != PSA_SUCCESS )
  1273. return( status );
  1274. psa_key_attributes_t attributes = {
  1275. .core = slot->attr
  1276. };
  1277. status = psa_driver_wrapper_export_key( &attributes,
  1278. slot->key.data, slot->key.bytes,
  1279. data, data_size, data_length );
  1280. unlock_status = psa_unlock_key_slot( slot );
  1281. return( ( status == PSA_SUCCESS ) ? unlock_status : status );
  1282. }
  1283. psa_status_t psa_export_public_key_internal(
  1284. const psa_key_attributes_t *attributes,
  1285. const uint8_t *key_buffer,
  1286. size_t key_buffer_size,
  1287. uint8_t *data,
  1288. size_t data_size,
  1289. size_t *data_length )
  1290. {
  1291. psa_key_type_t type = attributes->core.type;
  1292. if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
  1293. {
  1294. if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
  1295. {
  1296. /* Exporting public -> public */
  1297. return( psa_export_key_buffer_internal(
  1298. key_buffer, key_buffer_size,
  1299. data, data_size, data_length ) );
  1300. }
  1301. if( PSA_KEY_TYPE_IS_RSA( type ) )
  1302. {
  1303. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
  1304. defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
  1305. return( mbedtls_psa_rsa_export_public_key( attributes,
  1306. key_buffer,
  1307. key_buffer_size,
  1308. data,
  1309. data_size,
  1310. data_length ) );
  1311. #else
  1312. /* We don't know how to convert a private RSA key to public. */
  1313. return( PSA_ERROR_NOT_SUPPORTED );
  1314. #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
  1315. * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
  1316. }
  1317. else
  1318. {
  1319. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
  1320. defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
  1321. return( mbedtls_psa_ecp_export_public_key( attributes,
  1322. key_buffer,
  1323. key_buffer_size,
  1324. data,
  1325. data_size,
  1326. data_length ) );
  1327. #else
  1328. /* We don't know how to convert a private ECC key to public */
  1329. return( PSA_ERROR_NOT_SUPPORTED );
  1330. #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
  1331. * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
  1332. }
  1333. }
  1334. else
  1335. {
  1336. /* This shouldn't happen in the reference implementation, but
  1337. it is valid for a special-purpose implementation to omit
  1338. support for exporting certain key types. */
  1339. return( PSA_ERROR_NOT_SUPPORTED );
  1340. }
  1341. }
  1342. psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
  1343. uint8_t *data,
  1344. size_t data_size,
  1345. size_t *data_length )
  1346. {
  1347. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1348. psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
  1349. psa_key_slot_t *slot;
  1350. /* Reject a zero-length output buffer now, since this can never be a
  1351. * valid key representation. This way we know that data must be a valid
  1352. * pointer and we can do things like memset(data, ..., data_size). */
  1353. if( data_size == 0 )
  1354. return( PSA_ERROR_BUFFER_TOO_SMALL );
  1355. /* Set the key to empty now, so that even when there are errors, we always
  1356. * set data_length to a value between 0 and data_size. On error, setting
  1357. * the key to empty is a good choice because an empty key representation is
  1358. * unlikely to be accepted anywhere. */
  1359. *data_length = 0;
  1360. /* Exporting a public key doesn't require a usage flag. */
  1361. status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
  1362. if( status != PSA_SUCCESS )
  1363. return( status );
  1364. if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
  1365. {
  1366. status = PSA_ERROR_INVALID_ARGUMENT;
  1367. goto exit;
  1368. }
  1369. psa_key_attributes_t attributes = {
  1370. .core = slot->attr
  1371. };
  1372. status = psa_driver_wrapper_export_public_key(
  1373. &attributes, slot->key.data, slot->key.bytes,
  1374. data, data_size, data_length );
  1375. exit:
  1376. unlock_status = psa_unlock_key_slot( slot );
  1377. return( ( status == PSA_SUCCESS ) ? unlock_status : status );
  1378. }
  1379. #if defined(static_assert)
  1380. static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
  1381. "One or more key attribute flag is listed as both external-only and dual-use" );
  1382. static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
  1383. "One or more key attribute flag is listed as both internal-only and dual-use" );
  1384. static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
  1385. "One or more key attribute flag is listed as both internal-only and external-only" );
  1386. #endif
  1387. /** Validate that a key policy is internally well-formed.
  1388. *
  1389. * This function only rejects invalid policies. It does not validate the
  1390. * consistency of the policy with respect to other attributes of the key
  1391. * such as the key type.
  1392. */
  1393. static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
  1394. {
  1395. if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
  1396. PSA_KEY_USAGE_COPY |
  1397. PSA_KEY_USAGE_ENCRYPT |
  1398. PSA_KEY_USAGE_DECRYPT |
  1399. PSA_KEY_USAGE_SIGN_MESSAGE |
  1400. PSA_KEY_USAGE_VERIFY_MESSAGE |
  1401. PSA_KEY_USAGE_SIGN_HASH |
  1402. PSA_KEY_USAGE_VERIFY_HASH |
  1403. PSA_KEY_USAGE_DERIVE ) ) != 0 )
  1404. return( PSA_ERROR_INVALID_ARGUMENT );
  1405. return( PSA_SUCCESS );
  1406. }
  1407. /** Validate the internal consistency of key attributes.
  1408. *
  1409. * This function only rejects invalid attribute values. If does not
  1410. * validate the consistency of the attributes with any key data that may
  1411. * be involved in the creation of the key.
  1412. *
  1413. * Call this function early in the key creation process.
  1414. *
  1415. * \param[in] attributes Key attributes for the new key.
  1416. * \param[out] p_drv On any return, the driver for the key, if any.
  1417. * NULL for a transparent key.
  1418. *
  1419. */
  1420. static psa_status_t psa_validate_key_attributes(
  1421. const psa_key_attributes_t *attributes,
  1422. psa_se_drv_table_entry_t **p_drv )
  1423. {
  1424. psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
  1425. psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
  1426. mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
  1427. status = psa_validate_key_location( lifetime, p_drv );
  1428. if( status != PSA_SUCCESS )
  1429. return( status );
  1430. status = psa_validate_key_persistence( lifetime );
  1431. if( status != PSA_SUCCESS )
  1432. return( status );
  1433. if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
  1434. {
  1435. if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
  1436. return( PSA_ERROR_INVALID_ARGUMENT );
  1437. }
  1438. else
  1439. {
  1440. if( !psa_is_valid_key_id( psa_get_key_id( attributes ), 0 ) )
  1441. return( PSA_ERROR_INVALID_ARGUMENT );
  1442. }
  1443. status = psa_validate_key_policy( &attributes->core.policy );
  1444. if( status != PSA_SUCCESS )
  1445. return( status );
  1446. /* Refuse to create overly large keys.
  1447. * Note that this doesn't trigger on import if the attributes don't
  1448. * explicitly specify a size (so psa_get_key_bits returns 0), so
  1449. * psa_import_key() needs its own checks. */
  1450. if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
  1451. return( PSA_ERROR_NOT_SUPPORTED );
  1452. /* Reject invalid flags. These should not be reachable through the API. */
  1453. if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
  1454. MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
  1455. return( PSA_ERROR_INVALID_ARGUMENT );
  1456. return( PSA_SUCCESS );
  1457. }
  1458. /** Prepare a key slot to receive key material.
  1459. *
  1460. * This function allocates a key slot and sets its metadata.
  1461. *
  1462. * If this function fails, call psa_fail_key_creation().
  1463. *
  1464. * This function is intended to be used as follows:
  1465. * -# Call psa_start_key_creation() to allocate a key slot, prepare
  1466. * it with the specified attributes, and in case of a volatile key assign it
  1467. * a volatile key identifier.
  1468. * -# Populate the slot with the key material.
  1469. * -# Call psa_finish_key_creation() to finalize the creation of the slot.
  1470. * In case of failure at any step, stop the sequence and call
  1471. * psa_fail_key_creation().
  1472. *
  1473. * On success, the key slot is locked. It is the responsibility of the caller
  1474. * to unlock the key slot when it does not access it anymore.
  1475. *
  1476. * \param method An identification of the calling function.
  1477. * \param[in] attributes Key attributes for the new key.
  1478. * \param[out] p_slot On success, a pointer to the prepared slot.
  1479. * \param[out] p_drv On any return, the driver for the key, if any.
  1480. * NULL for a transparent key.
  1481. *
  1482. * \retval #PSA_SUCCESS
  1483. * The key slot is ready to receive key material.
  1484. * \return If this function fails, the key slot is an invalid state.
  1485. * You must call psa_fail_key_creation() to wipe and free the slot.
  1486. */
  1487. static psa_status_t psa_start_key_creation(
  1488. psa_key_creation_method_t method,
  1489. const psa_key_attributes_t *attributes,
  1490. psa_key_slot_t **p_slot,
  1491. psa_se_drv_table_entry_t **p_drv )
  1492. {
  1493. psa_status_t status;
  1494. psa_key_id_t volatile_key_id;
  1495. psa_key_slot_t *slot;
  1496. (void) method;
  1497. *p_drv = NULL;
  1498. status = psa_validate_key_attributes( attributes, p_drv );
  1499. if( status != PSA_SUCCESS )
  1500. return( status );
  1501. status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
  1502. if( status != PSA_SUCCESS )
  1503. return( status );
  1504. slot = *p_slot;
  1505. /* We're storing the declared bit-size of the key. It's up to each
  1506. * creation mechanism to verify that this information is correct.
  1507. * It's automatically correct for mechanisms that use the bit-size as
  1508. * an input (generate, device) but not for those where the bit-size
  1509. * is optional (import, copy). In case of a volatile key, assign it the
  1510. * volatile key identifier associated to the slot returned to contain its
  1511. * definition. */
  1512. slot->attr = attributes->core;
  1513. if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
  1514. {
  1515. #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
  1516. slot->attr.id = volatile_key_id;
  1517. #else
  1518. slot->attr.id.key_id = volatile_key_id;
  1519. #endif
  1520. }
  1521. /* Erase external-only flags from the internal copy. To access
  1522. * external-only flags, query `attributes`. Thanks to the check
  1523. * in psa_validate_key_attributes(), this leaves the dual-use
  1524. * flags and any internal flag that psa_get_empty_key_slot()
  1525. * may have set. */
  1526. slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
  1527. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  1528. /* For a key in a secure element, we need to do three things
  1529. * when creating or registering a persistent key:
  1530. * create the key file in internal storage, create the
  1531. * key inside the secure element, and update the driver's
  1532. * persistent data. This is done by starting a transaction that will
  1533. * encompass these three actions.
  1534. * For registering a volatile key, we just need to find an appropriate
  1535. * slot number inside the SE. Since the key is designated volatile, creating
  1536. * a transaction is not required. */
  1537. /* The first thing to do is to find a slot number for the new key.
  1538. * We save the slot number in persistent storage as part of the
  1539. * transaction data. It will be needed to recover if the power
  1540. * fails during the key creation process, to clean up on the secure
  1541. * element side after restarting. Obtaining a slot number from the
  1542. * secure element driver updates its persistent state, but we do not yet
  1543. * save the driver's persistent state, so that if the power fails,
  1544. * we can roll back to a state where the key doesn't exist. */
  1545. if( *p_drv != NULL )
  1546. {
  1547. psa_key_slot_number_t slot_number;
  1548. status = psa_find_se_slot_for_key( attributes, method, *p_drv,
  1549. &slot_number );
  1550. if( status != PSA_SUCCESS )
  1551. return( status );
  1552. if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
  1553. {
  1554. psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
  1555. psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
  1556. psa_crypto_transaction.key.slot = slot_number;
  1557. psa_crypto_transaction.key.id = slot->attr.id;
  1558. status = psa_crypto_save_transaction( );
  1559. if( status != PSA_SUCCESS )
  1560. {
  1561. (void) psa_crypto_stop_transaction( );
  1562. return( status );
  1563. }
  1564. }
  1565. status = psa_copy_key_material_into_slot(
  1566. slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
  1567. }
  1568. if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
  1569. {
  1570. /* Key registration only makes sense with a secure element. */
  1571. return( PSA_ERROR_INVALID_ARGUMENT );
  1572. }
  1573. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  1574. return( PSA_SUCCESS );
  1575. }
  1576. /** Finalize the creation of a key once its key material has been set.
  1577. *
  1578. * This entails writing the key to persistent storage.
  1579. *
  1580. * If this function fails, call psa_fail_key_creation().
  1581. * See the documentation of psa_start_key_creation() for the intended use
  1582. * of this function.
  1583. *
  1584. * If the finalization succeeds, the function unlocks the key slot (it was
  1585. * locked by psa_start_key_creation()) and the key slot cannot be accessed
  1586. * anymore as part of the key creation process.
  1587. *
  1588. * \param[in,out] slot Pointer to the slot with key material.
  1589. * \param[in] driver The secure element driver for the key,
  1590. * or NULL for a transparent key.
  1591. * \param[out] key On success, identifier of the key. Note that the
  1592. * key identifier is also stored in the key slot.
  1593. *
  1594. * \retval #PSA_SUCCESS
  1595. * The key was successfully created.
  1596. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  1597. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
  1598. * \retval #PSA_ERROR_ALREADY_EXISTS
  1599. * \retval #PSA_ERROR_DATA_INVALID
  1600. * \retval #PSA_ERROR_DATA_CORRUPT
  1601. * \retval #PSA_ERROR_STORAGE_FAILURE
  1602. *
  1603. * \return If this function fails, the key slot is an invalid state.
  1604. * You must call psa_fail_key_creation() to wipe and free the slot.
  1605. */
  1606. static psa_status_t psa_finish_key_creation(
  1607. psa_key_slot_t *slot,
  1608. psa_se_drv_table_entry_t *driver,
  1609. mbedtls_svc_key_id_t *key)
  1610. {
  1611. psa_status_t status = PSA_SUCCESS;
  1612. (void) slot;
  1613. (void) driver;
  1614. #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
  1615. if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
  1616. {
  1617. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  1618. if( driver != NULL )
  1619. {
  1620. psa_se_key_data_storage_t data;
  1621. psa_key_slot_number_t slot_number =
  1622. psa_key_slot_get_slot_number( slot ) ;
  1623. #if defined(static_assert)
  1624. static_assert( sizeof( slot_number ) ==
  1625. sizeof( data.slot_number ),
  1626. "Slot number size does not match psa_se_key_data_storage_t" );
  1627. #endif
  1628. memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
  1629. status = psa_save_persistent_key( &slot->attr,
  1630. (uint8_t*) &data,
  1631. sizeof( data ) );
  1632. }
  1633. else
  1634. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  1635. {
  1636. /* Key material is saved in export representation in the slot, so
  1637. * just pass the slot buffer for storage. */
  1638. status = psa_save_persistent_key( &slot->attr,
  1639. slot->key.data,
  1640. slot->key.bytes );
  1641. }
  1642. }
  1643. #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
  1644. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  1645. /* Finish the transaction for a key creation. This does not
  1646. * happen when registering an existing key. Detect this case
  1647. * by checking whether a transaction is in progress (actual
  1648. * creation of a persistent key in a secure element requires a transaction,
  1649. * but registration or volatile key creation doesn't use one). */
  1650. if( driver != NULL &&
  1651. psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
  1652. {
  1653. status = psa_save_se_persistent_data( driver );
  1654. if( status != PSA_SUCCESS )
  1655. {
  1656. psa_destroy_persistent_key( slot->attr.id );
  1657. return( status );
  1658. }
  1659. status = psa_crypto_stop_transaction( );
  1660. }
  1661. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  1662. if( status == PSA_SUCCESS )
  1663. {
  1664. *key = slot->attr.id;
  1665. status = psa_unlock_key_slot( slot );
  1666. if( status != PSA_SUCCESS )
  1667. *key = MBEDTLS_SVC_KEY_ID_INIT;
  1668. }
  1669. return( status );
  1670. }
  1671. /** Abort the creation of a key.
  1672. *
  1673. * You may call this function after calling psa_start_key_creation(),
  1674. * or after psa_finish_key_creation() fails. In other circumstances, this
  1675. * function may not clean up persistent storage.
  1676. * See the documentation of psa_start_key_creation() for the intended use
  1677. * of this function.
  1678. *
  1679. * \param[in,out] slot Pointer to the slot with key material.
  1680. * \param[in] driver The secure element driver for the key,
  1681. * or NULL for a transparent key.
  1682. */
  1683. static void psa_fail_key_creation( psa_key_slot_t *slot,
  1684. psa_se_drv_table_entry_t *driver )
  1685. {
  1686. (void) driver;
  1687. if( slot == NULL )
  1688. return;
  1689. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  1690. /* TODO: If the key has already been created in the secure
  1691. * element, and the failure happened later (when saving metadata
  1692. * to internal storage), we need to destroy the key in the secure
  1693. * element.
  1694. * https://github.com/ARMmbed/mbed-crypto/issues/217
  1695. */
  1696. /* Abort the ongoing transaction if any (there may not be one if
  1697. * the creation process failed before starting one, or if the
  1698. * key creation is a registration of a key in a secure element).
  1699. * Earlier functions must already have done what it takes to undo any
  1700. * partial creation. All that's left is to update the transaction data
  1701. * itself. */
  1702. (void) psa_crypto_stop_transaction( );
  1703. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  1704. psa_wipe_key_slot( slot );
  1705. }
  1706. /** Validate optional attributes during key creation.
  1707. *
  1708. * Some key attributes are optional during key creation. If they are
  1709. * specified in the attributes structure, check that they are consistent
  1710. * with the data in the slot.
  1711. *
  1712. * This function should be called near the end of key creation, after
  1713. * the slot in memory is fully populated but before saving persistent data.
  1714. */
  1715. static psa_status_t psa_validate_optional_attributes(
  1716. const psa_key_slot_t *slot,
  1717. const psa_key_attributes_t *attributes )
  1718. {
  1719. if( attributes->core.type != 0 )
  1720. {
  1721. if( attributes->core.type != slot->attr.type )
  1722. return( PSA_ERROR_INVALID_ARGUMENT );
  1723. }
  1724. if( attributes->domain_parameters_size != 0 )
  1725. {
  1726. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
  1727. defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
  1728. if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
  1729. {
  1730. mbedtls_rsa_context *rsa = NULL;
  1731. mbedtls_mpi actual, required;
  1732. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1733. psa_status_t status = mbedtls_psa_rsa_load_representation(
  1734. slot->attr.type,
  1735. slot->key.data,
  1736. slot->key.bytes,
  1737. &rsa );
  1738. if( status != PSA_SUCCESS )
  1739. return( status );
  1740. mbedtls_mpi_init( &actual );
  1741. mbedtls_mpi_init( &required );
  1742. ret = mbedtls_rsa_export( rsa,
  1743. NULL, NULL, NULL, NULL, &actual );
  1744. mbedtls_rsa_free( rsa );
  1745. mbedtls_free( rsa );
  1746. if( ret != 0 )
  1747. goto rsa_exit;
  1748. ret = mbedtls_mpi_read_binary( &required,
  1749. attributes->domain_parameters,
  1750. attributes->domain_parameters_size );
  1751. if( ret != 0 )
  1752. goto rsa_exit;
  1753. if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
  1754. ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
  1755. rsa_exit:
  1756. mbedtls_mpi_free( &actual );
  1757. mbedtls_mpi_free( &required );
  1758. if( ret != 0)
  1759. return( mbedtls_to_psa_error( ret ) );
  1760. }
  1761. else
  1762. #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
  1763. * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
  1764. {
  1765. return( PSA_ERROR_INVALID_ARGUMENT );
  1766. }
  1767. }
  1768. if( attributes->core.bits != 0 )
  1769. {
  1770. if( attributes->core.bits != slot->attr.bits )
  1771. return( PSA_ERROR_INVALID_ARGUMENT );
  1772. }
  1773. return( PSA_SUCCESS );
  1774. }
  1775. psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
  1776. const uint8_t *data,
  1777. size_t data_length,
  1778. mbedtls_svc_key_id_t *key )
  1779. {
  1780. psa_status_t status;
  1781. psa_key_slot_t *slot = NULL;
  1782. psa_se_drv_table_entry_t *driver = NULL;
  1783. size_t bits;
  1784. *key = MBEDTLS_SVC_KEY_ID_INIT;
  1785. /* Reject zero-length symmetric keys (including raw data key objects).
  1786. * This also rejects any key which might be encoded as an empty string,
  1787. * which is never valid. */
  1788. if( data_length == 0 )
  1789. return( PSA_ERROR_INVALID_ARGUMENT );
  1790. status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
  1791. &slot, &driver );
  1792. if( status != PSA_SUCCESS )
  1793. goto exit;
  1794. /* In the case of a transparent key or an opaque key stored in local
  1795. * storage (thus not in the case of generating a key in a secure element
  1796. * or cryptoprocessor with storage), we have to allocate a buffer to
  1797. * hold the generated key material. */
  1798. if( slot->key.data == NULL )
  1799. {
  1800. status = psa_allocate_buffer_to_slot( slot, data_length );
  1801. if( status != PSA_SUCCESS )
  1802. goto exit;
  1803. }
  1804. bits = slot->attr.bits;
  1805. status = psa_driver_wrapper_import_key( attributes,
  1806. data, data_length,
  1807. slot->key.data,
  1808. slot->key.bytes,
  1809. &slot->key.bytes, &bits );
  1810. if( status != PSA_SUCCESS )
  1811. goto exit;
  1812. if( slot->attr.bits == 0 )
  1813. slot->attr.bits = (psa_key_bits_t) bits;
  1814. else if( bits != slot->attr.bits )
  1815. {
  1816. status = PSA_ERROR_INVALID_ARGUMENT;
  1817. goto exit;
  1818. }
  1819. status = psa_validate_optional_attributes( slot, attributes );
  1820. if( status != PSA_SUCCESS )
  1821. goto exit;
  1822. status = psa_finish_key_creation( slot, driver, key );
  1823. exit:
  1824. if( status != PSA_SUCCESS )
  1825. psa_fail_key_creation( slot, driver );
  1826. return( status );
  1827. }
  1828. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  1829. psa_status_t mbedtls_psa_register_se_key(
  1830. const psa_key_attributes_t *attributes )
  1831. {
  1832. psa_status_t status;
  1833. psa_key_slot_t *slot = NULL;
  1834. psa_se_drv_table_entry_t *driver = NULL;
  1835. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  1836. /* Leaving attributes unspecified is not currently supported.
  1837. * It could make sense to query the key type and size from the
  1838. * secure element, but not all secure elements support this
  1839. * and the driver HAL doesn't currently support it. */
  1840. if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
  1841. return( PSA_ERROR_NOT_SUPPORTED );
  1842. if( psa_get_key_bits( attributes ) == 0 )
  1843. return( PSA_ERROR_NOT_SUPPORTED );
  1844. status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
  1845. &slot, &driver );
  1846. if( status != PSA_SUCCESS )
  1847. goto exit;
  1848. status = psa_finish_key_creation( slot, driver, &key );
  1849. exit:
  1850. if( status != PSA_SUCCESS )
  1851. psa_fail_key_creation( slot, driver );
  1852. /* Registration doesn't keep the key in RAM. */
  1853. psa_close_key( key );
  1854. return( status );
  1855. }
  1856. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  1857. static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
  1858. psa_key_slot_t *target )
  1859. {
  1860. psa_status_t status = psa_copy_key_material_into_slot( target,
  1861. source->key.data,
  1862. source->key.bytes );
  1863. if( status != PSA_SUCCESS )
  1864. return( status );
  1865. target->attr.type = source->attr.type;
  1866. target->attr.bits = source->attr.bits;
  1867. return( PSA_SUCCESS );
  1868. }
  1869. psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
  1870. const psa_key_attributes_t *specified_attributes,
  1871. mbedtls_svc_key_id_t *target_key )
  1872. {
  1873. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1874. psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
  1875. psa_key_slot_t *source_slot = NULL;
  1876. psa_key_slot_t *target_slot = NULL;
  1877. psa_key_attributes_t actual_attributes = *specified_attributes;
  1878. psa_se_drv_table_entry_t *driver = NULL;
  1879. *target_key = MBEDTLS_SVC_KEY_ID_INIT;
  1880. status = psa_get_and_lock_transparent_key_slot_with_policy(
  1881. source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
  1882. if( status != PSA_SUCCESS )
  1883. goto exit;
  1884. status = psa_validate_optional_attributes( source_slot,
  1885. specified_attributes );
  1886. if( status != PSA_SUCCESS )
  1887. goto exit;
  1888. status = psa_restrict_key_policy( source_slot->attr.type,
  1889. &actual_attributes.core.policy,
  1890. &source_slot->attr.policy );
  1891. if( status != PSA_SUCCESS )
  1892. goto exit;
  1893. status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
  1894. &target_slot, &driver );
  1895. if( status != PSA_SUCCESS )
  1896. goto exit;
  1897. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  1898. if( driver != NULL )
  1899. {
  1900. /* Copying to a secure element is not implemented yet. */
  1901. status = PSA_ERROR_NOT_SUPPORTED;
  1902. goto exit;
  1903. }
  1904. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  1905. if( psa_key_lifetime_is_external( actual_attributes.core.lifetime ) )
  1906. {
  1907. /*
  1908. * Copying through an opaque driver is not implemented yet, consider
  1909. * a lifetime with an external location as an invalid parameter for
  1910. * now.
  1911. */
  1912. status = PSA_ERROR_INVALID_ARGUMENT;
  1913. goto exit;
  1914. }
  1915. status = psa_copy_key_material( source_slot, target_slot );
  1916. if( status != PSA_SUCCESS )
  1917. goto exit;
  1918. status = psa_finish_key_creation( target_slot, driver, target_key );
  1919. exit:
  1920. if( status != PSA_SUCCESS )
  1921. psa_fail_key_creation( target_slot, driver );
  1922. unlock_status = psa_unlock_key_slot( source_slot );
  1923. return( ( status == PSA_SUCCESS ) ? unlock_status : status );
  1924. }
  1925. /****************************************************************/
  1926. /* Message digests */
  1927. /****************************************************************/
  1928. psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
  1929. {
  1930. /* Aborting a non-active operation is allowed */
  1931. if( operation->id == 0 )
  1932. return( PSA_SUCCESS );
  1933. psa_status_t status = psa_driver_wrapper_hash_abort( operation );
  1934. operation->id = 0;
  1935. return( status );
  1936. }
  1937. psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
  1938. psa_algorithm_t alg )
  1939. {
  1940. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1941. /* A context must be freshly initialized before it can be set up. */
  1942. if( operation->id != 0 )
  1943. {
  1944. status = PSA_ERROR_BAD_STATE;
  1945. goto exit;
  1946. }
  1947. if( !PSA_ALG_IS_HASH( alg ) )
  1948. {
  1949. status = PSA_ERROR_INVALID_ARGUMENT;
  1950. goto exit;
  1951. }
  1952. /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
  1953. * directly zeroes the int-sized dummy member of the context union. */
  1954. memset( &operation->ctx, 0, sizeof( operation->ctx ) );
  1955. status = psa_driver_wrapper_hash_setup( operation, alg );
  1956. exit:
  1957. if( status != PSA_SUCCESS )
  1958. psa_hash_abort( operation );
  1959. return status;
  1960. }
  1961. psa_status_t psa_hash_update( psa_hash_operation_t *operation,
  1962. const uint8_t *input,
  1963. size_t input_length )
  1964. {
  1965. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1966. if( operation->id == 0 )
  1967. {
  1968. status = PSA_ERROR_BAD_STATE;
  1969. goto exit;
  1970. }
  1971. /* Don't require hash implementations to behave correctly on a
  1972. * zero-length input, which may have an invalid pointer. */
  1973. if( input_length == 0 )
  1974. return( PSA_SUCCESS );
  1975. status = psa_driver_wrapper_hash_update( operation, input, input_length );
  1976. exit:
  1977. if( status != PSA_SUCCESS )
  1978. psa_hash_abort( operation );
  1979. return( status );
  1980. }
  1981. psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
  1982. uint8_t *hash,
  1983. size_t hash_size,
  1984. size_t *hash_length )
  1985. {
  1986. *hash_length = 0;
  1987. if( operation->id == 0 )
  1988. return( PSA_ERROR_BAD_STATE );
  1989. psa_status_t status = psa_driver_wrapper_hash_finish(
  1990. operation, hash, hash_size, hash_length );
  1991. psa_hash_abort( operation );
  1992. return( status );
  1993. }
  1994. psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
  1995. const uint8_t *hash,
  1996. size_t hash_length )
  1997. {
  1998. uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
  1999. size_t actual_hash_length;
  2000. psa_status_t status = psa_hash_finish(
  2001. operation,
  2002. actual_hash, sizeof( actual_hash ),
  2003. &actual_hash_length );
  2004. if( status != PSA_SUCCESS )
  2005. goto exit;
  2006. if( actual_hash_length != hash_length )
  2007. {
  2008. status = PSA_ERROR_INVALID_SIGNATURE;
  2009. goto exit;
  2010. }
  2011. if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
  2012. status = PSA_ERROR_INVALID_SIGNATURE;
  2013. exit:
  2014. if( status != PSA_SUCCESS )
  2015. psa_hash_abort(operation);
  2016. return( status );
  2017. }
  2018. psa_status_t psa_hash_compute( psa_algorithm_t alg,
  2019. const uint8_t *input, size_t input_length,
  2020. uint8_t *hash, size_t hash_size,
  2021. size_t *hash_length )
  2022. {
  2023. *hash_length = 0;
  2024. if( !PSA_ALG_IS_HASH( alg ) )
  2025. return( PSA_ERROR_INVALID_ARGUMENT );
  2026. return( psa_driver_wrapper_hash_compute( alg, input, input_length,
  2027. hash, hash_size, hash_length ) );
  2028. }
  2029. psa_status_t psa_hash_compare( psa_algorithm_t alg,
  2030. const uint8_t *input, size_t input_length,
  2031. const uint8_t *hash, size_t hash_length )
  2032. {
  2033. uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
  2034. size_t actual_hash_length;
  2035. if( !PSA_ALG_IS_HASH( alg ) )
  2036. return( PSA_ERROR_INVALID_ARGUMENT );
  2037. psa_status_t status = psa_driver_wrapper_hash_compute(
  2038. alg, input, input_length,
  2039. actual_hash, sizeof(actual_hash),
  2040. &actual_hash_length );
  2041. if( status != PSA_SUCCESS )
  2042. return( status );
  2043. if( actual_hash_length != hash_length )
  2044. return( PSA_ERROR_INVALID_SIGNATURE );
  2045. if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
  2046. return( PSA_ERROR_INVALID_SIGNATURE );
  2047. return( PSA_SUCCESS );
  2048. }
  2049. psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
  2050. psa_hash_operation_t *target_operation )
  2051. {
  2052. if( source_operation->id == 0 ||
  2053. target_operation->id != 0 )
  2054. {
  2055. return( PSA_ERROR_BAD_STATE );
  2056. }
  2057. psa_status_t status = psa_driver_wrapper_hash_clone( source_operation,
  2058. target_operation );
  2059. if( status != PSA_SUCCESS )
  2060. psa_hash_abort( target_operation );
  2061. return( status );
  2062. }
  2063. /****************************************************************/
  2064. /* MAC */
  2065. /****************************************************************/
  2066. psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
  2067. {
  2068. /* Aborting a non-active operation is allowed */
  2069. if( operation->id == 0 )
  2070. return( PSA_SUCCESS );
  2071. psa_status_t status = psa_driver_wrapper_mac_abort( operation );
  2072. operation->mac_size = 0;
  2073. operation->is_sign = 0;
  2074. operation->id = 0;
  2075. return( status );
  2076. }
  2077. static psa_status_t psa_mac_finalize_alg_and_key_validation(
  2078. psa_algorithm_t alg,
  2079. const psa_key_attributes_t *attributes,
  2080. uint8_t *mac_size )
  2081. {
  2082. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2083. psa_key_type_t key_type = psa_get_key_type( attributes );
  2084. size_t key_bits = psa_get_key_bits( attributes );
  2085. if( ! PSA_ALG_IS_MAC( alg ) )
  2086. return( PSA_ERROR_INVALID_ARGUMENT );
  2087. /* Validate the combination of key type and algorithm */
  2088. status = psa_mac_key_can_do( alg, key_type );
  2089. if( status != PSA_SUCCESS )
  2090. return( status );
  2091. /* Get the output length for the algorithm and key combination */
  2092. *mac_size = PSA_MAC_LENGTH( key_type, key_bits, alg );
  2093. if( *mac_size < 4 )
  2094. {
  2095. /* A very short MAC is too short for security since it can be
  2096. * brute-forced. Ancient protocols with 32-bit MACs do exist,
  2097. * so we make this our minimum, even though 32 bits is still
  2098. * too small for security. */
  2099. return( PSA_ERROR_NOT_SUPPORTED );
  2100. }
  2101. if( *mac_size > PSA_MAC_LENGTH( key_type, key_bits,
  2102. PSA_ALG_FULL_LENGTH_MAC( alg ) ) )
  2103. {
  2104. /* It's impossible to "truncate" to a larger length than the full length
  2105. * of the algorithm. */
  2106. return( PSA_ERROR_INVALID_ARGUMENT );
  2107. }
  2108. return( PSA_SUCCESS );
  2109. }
  2110. static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
  2111. mbedtls_svc_key_id_t key,
  2112. psa_algorithm_t alg,
  2113. int is_sign )
  2114. {
  2115. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2116. psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
  2117. psa_key_slot_t *slot = NULL;
  2118. /* A context must be freshly initialized before it can be set up. */
  2119. if( operation->id != 0 )
  2120. {
  2121. status = PSA_ERROR_BAD_STATE;
  2122. goto exit;
  2123. }
  2124. status = psa_get_and_lock_key_slot_with_policy(
  2125. key,
  2126. &slot,
  2127. is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH,
  2128. alg );
  2129. if( status != PSA_SUCCESS )
  2130. goto exit;
  2131. psa_key_attributes_t attributes = {
  2132. .core = slot->attr
  2133. };
  2134. status = psa_mac_finalize_alg_and_key_validation( alg, &attributes,
  2135. &operation->mac_size );
  2136. if( status != PSA_SUCCESS )
  2137. goto exit;
  2138. operation->is_sign = is_sign;
  2139. /* Dispatch the MAC setup call with validated input */
  2140. if( is_sign )
  2141. {
  2142. status = psa_driver_wrapper_mac_sign_setup( operation,
  2143. &attributes,
  2144. slot->key.data,
  2145. slot->key.bytes,
  2146. alg );
  2147. }
  2148. else
  2149. {
  2150. status = psa_driver_wrapper_mac_verify_setup( operation,
  2151. &attributes,
  2152. slot->key.data,
  2153. slot->key.bytes,
  2154. alg );
  2155. }
  2156. exit:
  2157. if( status != PSA_SUCCESS )
  2158. psa_mac_abort( operation );
  2159. unlock_status = psa_unlock_key_slot( slot );
  2160. return( ( status == PSA_SUCCESS ) ? unlock_status : status );
  2161. }
  2162. psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
  2163. mbedtls_svc_key_id_t key,
  2164. psa_algorithm_t alg )
  2165. {
  2166. return( psa_mac_setup( operation, key, alg, 1 ) );
  2167. }
  2168. psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
  2169. mbedtls_svc_key_id_t key,
  2170. psa_algorithm_t alg )
  2171. {
  2172. return( psa_mac_setup( operation, key, alg, 0 ) );
  2173. }
  2174. psa_status_t psa_mac_update( psa_mac_operation_t *operation,
  2175. const uint8_t *input,
  2176. size_t input_length )
  2177. {
  2178. if( operation->id == 0 )
  2179. return( PSA_ERROR_BAD_STATE );
  2180. /* Don't require hash implementations to behave correctly on a
  2181. * zero-length input, which may have an invalid pointer. */
  2182. if( input_length == 0 )
  2183. return( PSA_SUCCESS );
  2184. psa_status_t status = psa_driver_wrapper_mac_update( operation,
  2185. input, input_length );
  2186. if( status != PSA_SUCCESS )
  2187. psa_mac_abort( operation );
  2188. return( status );
  2189. }
  2190. psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
  2191. uint8_t *mac,
  2192. size_t mac_size,
  2193. size_t *mac_length )
  2194. {
  2195. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2196. psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
  2197. if( operation->id == 0 )
  2198. {
  2199. status = PSA_ERROR_BAD_STATE;
  2200. goto exit;
  2201. }
  2202. if( ! operation->is_sign )
  2203. {
  2204. status = PSA_ERROR_BAD_STATE;
  2205. goto exit;
  2206. }
  2207. /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
  2208. * once all the error checks are done. */
  2209. if( operation->mac_size == 0 )
  2210. {
  2211. status = PSA_ERROR_BAD_STATE;
  2212. goto exit;
  2213. }
  2214. if( mac_size < operation->mac_size )
  2215. {
  2216. status = PSA_ERROR_BUFFER_TOO_SMALL;
  2217. goto exit;
  2218. }
  2219. status = psa_driver_wrapper_mac_sign_finish( operation,
  2220. mac, operation->mac_size,
  2221. mac_length );
  2222. exit:
  2223. /* In case of success, set the potential excess room in the output buffer
  2224. * to an invalid value, to avoid potentially leaking a longer MAC.
  2225. * In case of error, set the output length and content to a safe default,
  2226. * such that in case the caller misses an error check, the output would be
  2227. * an unachievable MAC.
  2228. */
  2229. if( status != PSA_SUCCESS )
  2230. {
  2231. *mac_length = mac_size;
  2232. operation->mac_size = 0;
  2233. }
  2234. if( mac_size > operation->mac_size )
  2235. memset( &mac[operation->mac_size], '!',
  2236. mac_size - operation->mac_size );
  2237. abort_status = psa_mac_abort( operation );
  2238. return( status == PSA_SUCCESS ? abort_status : status );
  2239. }
  2240. psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
  2241. const uint8_t *mac,
  2242. size_t mac_length )
  2243. {
  2244. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2245. psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
  2246. if( operation->id == 0 )
  2247. {
  2248. status = PSA_ERROR_BAD_STATE;
  2249. goto exit;
  2250. }
  2251. if( operation->is_sign )
  2252. {
  2253. status = PSA_ERROR_BAD_STATE;
  2254. goto exit;
  2255. }
  2256. if( operation->mac_size != mac_length )
  2257. {
  2258. status = PSA_ERROR_INVALID_SIGNATURE;
  2259. goto exit;
  2260. }
  2261. status = psa_driver_wrapper_mac_verify_finish( operation,
  2262. mac, mac_length );
  2263. exit:
  2264. abort_status = psa_mac_abort( operation );
  2265. return( status == PSA_SUCCESS ? abort_status : status );
  2266. }
  2267. static psa_status_t psa_mac_compute_internal( mbedtls_svc_key_id_t key,
  2268. psa_algorithm_t alg,
  2269. const uint8_t *input,
  2270. size_t input_length,
  2271. uint8_t *mac,
  2272. size_t mac_size,
  2273. size_t *mac_length,
  2274. int is_sign )
  2275. {
  2276. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2277. psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
  2278. psa_key_slot_t *slot;
  2279. uint8_t operation_mac_size = 0;
  2280. status = psa_get_and_lock_key_slot_with_policy(
  2281. key, &slot,
  2282. is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH,
  2283. alg );
  2284. if( status != PSA_SUCCESS )
  2285. goto exit;
  2286. psa_key_attributes_t attributes = {
  2287. .core = slot->attr
  2288. };
  2289. status = psa_mac_finalize_alg_and_key_validation( alg, &attributes,
  2290. &operation_mac_size );
  2291. if( status != PSA_SUCCESS )
  2292. goto exit;
  2293. if( mac_size < operation_mac_size )
  2294. {
  2295. status = PSA_ERROR_BUFFER_TOO_SMALL;
  2296. goto exit;
  2297. }
  2298. status = psa_driver_wrapper_mac_compute(
  2299. &attributes,
  2300. slot->key.data, slot->key.bytes,
  2301. alg,
  2302. input, input_length,
  2303. mac, operation_mac_size, mac_length );
  2304. exit:
  2305. /* In case of success, set the potential excess room in the output buffer
  2306. * to an invalid value, to avoid potentially leaking a longer MAC.
  2307. * In case of error, set the output length and content to a safe default,
  2308. * such that in case the caller misses an error check, the output would be
  2309. * an unachievable MAC.
  2310. */
  2311. if( status != PSA_SUCCESS )
  2312. {
  2313. *mac_length = mac_size;
  2314. operation_mac_size = 0;
  2315. }
  2316. if( mac_size > operation_mac_size )
  2317. memset( &mac[operation_mac_size], '!', mac_size - operation_mac_size );
  2318. unlock_status = psa_unlock_key_slot( slot );
  2319. return( ( status == PSA_SUCCESS ) ? unlock_status : status );
  2320. }
  2321. psa_status_t psa_mac_compute( mbedtls_svc_key_id_t key,
  2322. psa_algorithm_t alg,
  2323. const uint8_t *input,
  2324. size_t input_length,
  2325. uint8_t *mac,
  2326. size_t mac_size,
  2327. size_t *mac_length)
  2328. {
  2329. return( psa_mac_compute_internal( key, alg,
  2330. input, input_length,
  2331. mac, mac_size, mac_length, 1 ) );
  2332. }
  2333. psa_status_t psa_mac_verify( mbedtls_svc_key_id_t key,
  2334. psa_algorithm_t alg,
  2335. const uint8_t *input,
  2336. size_t input_length,
  2337. const uint8_t *mac,
  2338. size_t mac_length)
  2339. {
  2340. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2341. uint8_t actual_mac[PSA_MAC_MAX_SIZE];
  2342. size_t actual_mac_length;
  2343. status = psa_mac_compute_internal( key, alg,
  2344. input, input_length,
  2345. actual_mac, sizeof( actual_mac ),
  2346. &actual_mac_length, 0 );
  2347. if( status != PSA_SUCCESS )
  2348. goto exit;
  2349. if( mac_length != actual_mac_length )
  2350. {
  2351. status = PSA_ERROR_INVALID_SIGNATURE;
  2352. goto exit;
  2353. }
  2354. if( mbedtls_psa_safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
  2355. {
  2356. status = PSA_ERROR_INVALID_SIGNATURE;
  2357. goto exit;
  2358. }
  2359. exit:
  2360. mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
  2361. return ( status );
  2362. }
  2363. /****************************************************************/
  2364. /* Asymmetric cryptography */
  2365. /****************************************************************/
  2366. static psa_status_t psa_sign_verify_check_alg( int input_is_message,
  2367. psa_algorithm_t alg )
  2368. {
  2369. if( input_is_message )
  2370. {
  2371. if( ! PSA_ALG_IS_SIGN_MESSAGE( alg ) )
  2372. return( PSA_ERROR_INVALID_ARGUMENT );
  2373. if ( PSA_ALG_IS_HASH_AND_SIGN( alg ) )
  2374. {
  2375. if( ! PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( alg ) ) )
  2376. return( PSA_ERROR_INVALID_ARGUMENT );
  2377. }
  2378. }
  2379. else
  2380. {
  2381. if( ! PSA_ALG_IS_HASH_AND_SIGN( alg ) )
  2382. return( PSA_ERROR_INVALID_ARGUMENT );
  2383. }
  2384. return( PSA_SUCCESS );
  2385. }
  2386. static psa_status_t psa_sign_internal( mbedtls_svc_key_id_t key,
  2387. int input_is_message,
  2388. psa_algorithm_t alg,
  2389. const uint8_t * input,
  2390. size_t input_length,
  2391. uint8_t * signature,
  2392. size_t signature_size,
  2393. size_t * signature_length )
  2394. {
  2395. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2396. psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
  2397. psa_key_slot_t *slot;
  2398. *signature_length = 0;
  2399. status = psa_sign_verify_check_alg( input_is_message, alg );
  2400. if( status != PSA_SUCCESS )
  2401. return status;
  2402. /* Immediately reject a zero-length signature buffer. This guarantees
  2403. * that signature must be a valid pointer. (On the other hand, the input
  2404. * buffer can in principle be empty since it doesn't actually have
  2405. * to be a hash.) */
  2406. if( signature_size == 0 )
  2407. return( PSA_ERROR_BUFFER_TOO_SMALL );
  2408. status = psa_get_and_lock_key_slot_with_policy(
  2409. key, &slot,
  2410. input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
  2411. PSA_KEY_USAGE_SIGN_HASH,
  2412. alg );
  2413. if( status != PSA_SUCCESS )
  2414. goto exit;
  2415. if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
  2416. {
  2417. status = PSA_ERROR_INVALID_ARGUMENT;
  2418. goto exit;
  2419. }
  2420. psa_key_attributes_t attributes = {
  2421. .core = slot->attr
  2422. };
  2423. if( input_is_message )
  2424. {
  2425. status = psa_driver_wrapper_sign_message(
  2426. &attributes, slot->key.data, slot->key.bytes,
  2427. alg, input, input_length,
  2428. signature, signature_size, signature_length );
  2429. }
  2430. else
  2431. {
  2432. status = psa_driver_wrapper_sign_hash(
  2433. &attributes, slot->key.data, slot->key.bytes,
  2434. alg, input, input_length,
  2435. signature, signature_size, signature_length );
  2436. }
  2437. exit:
  2438. /* Fill the unused part of the output buffer (the whole buffer on error,
  2439. * the trailing part on success) with something that isn't a valid signature
  2440. * (barring an attack on the signature and deliberately-crafted input),
  2441. * in case the caller doesn't check the return status properly. */
  2442. if( status == PSA_SUCCESS )
  2443. memset( signature + *signature_length, '!',
  2444. signature_size - *signature_length );
  2445. else
  2446. memset( signature, '!', signature_size );
  2447. /* If signature_size is 0 then we have nothing to do. We must not call
  2448. * memset because signature may be NULL in this case. */
  2449. unlock_status = psa_unlock_key_slot( slot );
  2450. return( ( status == PSA_SUCCESS ) ? unlock_status : status );
  2451. }
  2452. static psa_status_t psa_verify_internal( mbedtls_svc_key_id_t key,
  2453. int input_is_message,
  2454. psa_algorithm_t alg,
  2455. const uint8_t * input,
  2456. size_t input_length,
  2457. const uint8_t * signature,
  2458. size_t signature_length )
  2459. {
  2460. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2461. psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
  2462. psa_key_slot_t *slot;
  2463. status = psa_sign_verify_check_alg( input_is_message, alg );
  2464. if( status != PSA_SUCCESS )
  2465. return status;
  2466. status = psa_get_and_lock_key_slot_with_policy(
  2467. key, &slot,
  2468. input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
  2469. PSA_KEY_USAGE_VERIFY_HASH,
  2470. alg );
  2471. if( status != PSA_SUCCESS )
  2472. return( status );
  2473. psa_key_attributes_t attributes = {
  2474. .core = slot->attr
  2475. };
  2476. if( input_is_message )
  2477. {
  2478. status = psa_driver_wrapper_verify_message(
  2479. &attributes, slot->key.data, slot->key.bytes,
  2480. alg, input, input_length,
  2481. signature, signature_length );
  2482. }
  2483. else
  2484. {
  2485. status = psa_driver_wrapper_verify_hash(
  2486. &attributes, slot->key.data, slot->key.bytes,
  2487. alg, input, input_length,
  2488. signature, signature_length );
  2489. }
  2490. unlock_status = psa_unlock_key_slot( slot );
  2491. return( ( status == PSA_SUCCESS ) ? unlock_status : status );
  2492. }
  2493. psa_status_t psa_sign_message_builtin(
  2494. const psa_key_attributes_t *attributes,
  2495. const uint8_t *key_buffer,
  2496. size_t key_buffer_size,
  2497. psa_algorithm_t alg,
  2498. const uint8_t *input,
  2499. size_t input_length,
  2500. uint8_t *signature,
  2501. size_t signature_size,
  2502. size_t *signature_length )
  2503. {
  2504. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2505. if ( PSA_ALG_IS_HASH_AND_SIGN( alg ) )
  2506. {
  2507. size_t hash_length;
  2508. uint8_t hash[PSA_HASH_MAX_SIZE];
  2509. status = psa_driver_wrapper_hash_compute(
  2510. PSA_ALG_SIGN_GET_HASH( alg ),
  2511. input, input_length,
  2512. hash, sizeof( hash ), &hash_length );
  2513. if( status != PSA_SUCCESS )
  2514. return status;
  2515. return psa_driver_wrapper_sign_hash(
  2516. attributes, key_buffer, key_buffer_size,
  2517. alg, hash, hash_length,
  2518. signature, signature_size, signature_length );
  2519. }
  2520. return( PSA_ERROR_NOT_SUPPORTED );
  2521. }
  2522. psa_status_t psa_sign_message( mbedtls_svc_key_id_t key,
  2523. psa_algorithm_t alg,
  2524. const uint8_t * input,
  2525. size_t input_length,
  2526. uint8_t * signature,
  2527. size_t signature_size,
  2528. size_t * signature_length )
  2529. {
  2530. return psa_sign_internal(
  2531. key, 1, alg, input, input_length,
  2532. signature, signature_size, signature_length );
  2533. }
  2534. psa_status_t psa_verify_message_builtin(
  2535. const psa_key_attributes_t *attributes,
  2536. const uint8_t *key_buffer,
  2537. size_t key_buffer_size,
  2538. psa_algorithm_t alg,
  2539. const uint8_t *input,
  2540. size_t input_length,
  2541. const uint8_t *signature,
  2542. size_t signature_length )
  2543. {
  2544. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2545. if ( PSA_ALG_IS_HASH_AND_SIGN( alg ) )
  2546. {
  2547. size_t hash_length;
  2548. uint8_t hash[PSA_HASH_MAX_SIZE];
  2549. status = psa_driver_wrapper_hash_compute(
  2550. PSA_ALG_SIGN_GET_HASH( alg ),
  2551. input, input_length,
  2552. hash, sizeof( hash ), &hash_length );
  2553. if( status != PSA_SUCCESS )
  2554. return status;
  2555. return psa_driver_wrapper_verify_hash(
  2556. attributes, key_buffer, key_buffer_size,
  2557. alg, hash, hash_length,
  2558. signature, signature_length );
  2559. }
  2560. return( PSA_ERROR_NOT_SUPPORTED );
  2561. }
  2562. psa_status_t psa_verify_message( mbedtls_svc_key_id_t key,
  2563. psa_algorithm_t alg,
  2564. const uint8_t * input,
  2565. size_t input_length,
  2566. const uint8_t * signature,
  2567. size_t signature_length )
  2568. {
  2569. return psa_verify_internal(
  2570. key, 1, alg, input, input_length,
  2571. signature, signature_length );
  2572. }
  2573. psa_status_t psa_sign_hash_builtin(
  2574. const psa_key_attributes_t *attributes,
  2575. const uint8_t *key_buffer, size_t key_buffer_size,
  2576. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  2577. uint8_t *signature, size_t signature_size, size_t *signature_length )
  2578. {
  2579. if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
  2580. {
  2581. if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
  2582. PSA_ALG_IS_RSA_PSS( alg) )
  2583. {
  2584. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
  2585. defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
  2586. return( mbedtls_psa_rsa_sign_hash(
  2587. attributes,
  2588. key_buffer, key_buffer_size,
  2589. alg, hash, hash_length,
  2590. signature, signature_size, signature_length ) );
  2591. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
  2592. * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
  2593. }
  2594. else
  2595. {
  2596. return( PSA_ERROR_INVALID_ARGUMENT );
  2597. }
  2598. }
  2599. else
  2600. if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
  2601. {
  2602. if( PSA_ALG_IS_ECDSA( alg ) )
  2603. {
  2604. #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
  2605. defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
  2606. return( mbedtls_psa_ecdsa_sign_hash(
  2607. attributes,
  2608. key_buffer, key_buffer_size,
  2609. alg, hash, hash_length,
  2610. signature, signature_size, signature_length ) );
  2611. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
  2612. * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
  2613. }
  2614. else
  2615. {
  2616. return( PSA_ERROR_INVALID_ARGUMENT );
  2617. }
  2618. }
  2619. (void)key_buffer;
  2620. (void)key_buffer_size;
  2621. (void)hash;
  2622. (void)hash_length;
  2623. (void)signature;
  2624. (void)signature_size;
  2625. (void)signature_length;
  2626. return( PSA_ERROR_NOT_SUPPORTED );
  2627. }
  2628. psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
  2629. psa_algorithm_t alg,
  2630. const uint8_t *hash,
  2631. size_t hash_length,
  2632. uint8_t *signature,
  2633. size_t signature_size,
  2634. size_t *signature_length )
  2635. {
  2636. return psa_sign_internal(
  2637. key, 0, alg, hash, hash_length,
  2638. signature, signature_size, signature_length );
  2639. }
  2640. psa_status_t psa_verify_hash_builtin(
  2641. const psa_key_attributes_t *attributes,
  2642. const uint8_t *key_buffer, size_t key_buffer_size,
  2643. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  2644. const uint8_t *signature, size_t signature_length )
  2645. {
  2646. if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) )
  2647. {
  2648. if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
  2649. PSA_ALG_IS_RSA_PSS( alg) )
  2650. {
  2651. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
  2652. defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
  2653. return( mbedtls_psa_rsa_verify_hash(
  2654. attributes,
  2655. key_buffer, key_buffer_size,
  2656. alg, hash, hash_length,
  2657. signature, signature_length ) );
  2658. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
  2659. * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
  2660. }
  2661. else
  2662. {
  2663. return( PSA_ERROR_INVALID_ARGUMENT );
  2664. }
  2665. }
  2666. else
  2667. if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
  2668. {
  2669. if( PSA_ALG_IS_ECDSA( alg ) )
  2670. {
  2671. #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
  2672. defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
  2673. return( mbedtls_psa_ecdsa_verify_hash(
  2674. attributes,
  2675. key_buffer, key_buffer_size,
  2676. alg, hash, hash_length,
  2677. signature, signature_length ) );
  2678. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
  2679. * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
  2680. }
  2681. else
  2682. {
  2683. return( PSA_ERROR_INVALID_ARGUMENT );
  2684. }
  2685. }
  2686. (void)key_buffer;
  2687. (void)key_buffer_size;
  2688. (void)hash;
  2689. (void)hash_length;
  2690. (void)signature;
  2691. (void)signature_length;
  2692. return( PSA_ERROR_NOT_SUPPORTED );
  2693. }
  2694. psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
  2695. psa_algorithm_t alg,
  2696. const uint8_t *hash,
  2697. size_t hash_length,
  2698. const uint8_t *signature,
  2699. size_t signature_length )
  2700. {
  2701. return psa_verify_internal(
  2702. key, 0, alg, hash, hash_length,
  2703. signature, signature_length );
  2704. }
  2705. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
  2706. static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
  2707. mbedtls_rsa_context *rsa )
  2708. {
  2709. psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
  2710. const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
  2711. mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
  2712. mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
  2713. }
  2714. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
  2715. psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
  2716. psa_algorithm_t alg,
  2717. const uint8_t *input,
  2718. size_t input_length,
  2719. const uint8_t *salt,
  2720. size_t salt_length,
  2721. uint8_t *output,
  2722. size_t output_size,
  2723. size_t *output_length )
  2724. {
  2725. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2726. psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
  2727. psa_key_slot_t *slot;
  2728. (void) input;
  2729. (void) input_length;
  2730. (void) salt;
  2731. (void) output;
  2732. (void) output_size;
  2733. *output_length = 0;
  2734. if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
  2735. return( PSA_ERROR_INVALID_ARGUMENT );
  2736. status = psa_get_and_lock_transparent_key_slot_with_policy(
  2737. key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
  2738. if( status != PSA_SUCCESS )
  2739. return( status );
  2740. if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
  2741. PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
  2742. {
  2743. status = PSA_ERROR_INVALID_ARGUMENT;
  2744. goto exit;
  2745. }
  2746. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
  2747. defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
  2748. if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
  2749. {
  2750. mbedtls_rsa_context *rsa = NULL;
  2751. status = mbedtls_psa_rsa_load_representation( slot->attr.type,
  2752. slot->key.data,
  2753. slot->key.bytes,
  2754. &rsa );
  2755. if( status != PSA_SUCCESS )
  2756. goto rsa_exit;
  2757. if( output_size < mbedtls_rsa_get_len( rsa ) )
  2758. {
  2759. status = PSA_ERROR_BUFFER_TOO_SMALL;
  2760. goto rsa_exit;
  2761. }
  2762. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
  2763. if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
  2764. {
  2765. status = mbedtls_to_psa_error(
  2766. mbedtls_rsa_pkcs1_encrypt( rsa,
  2767. mbedtls_psa_get_random,
  2768. MBEDTLS_PSA_RANDOM_STATE,
  2769. MBEDTLS_RSA_PUBLIC,
  2770. input_length,
  2771. input,
  2772. output ) );
  2773. }
  2774. else
  2775. #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
  2776. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
  2777. if( PSA_ALG_IS_RSA_OAEP( alg ) )
  2778. {
  2779. psa_rsa_oaep_set_padding_mode( alg, rsa );
  2780. status = mbedtls_to_psa_error(
  2781. mbedtls_rsa_rsaes_oaep_encrypt( rsa,
  2782. mbedtls_psa_get_random,
  2783. MBEDTLS_PSA_RANDOM_STATE,
  2784. MBEDTLS_RSA_PUBLIC,
  2785. salt, salt_length,
  2786. input_length,
  2787. input,
  2788. output ) );
  2789. }
  2790. else
  2791. #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
  2792. {
  2793. status = PSA_ERROR_INVALID_ARGUMENT;
  2794. goto rsa_exit;
  2795. }
  2796. rsa_exit:
  2797. if( status == PSA_SUCCESS )
  2798. *output_length = mbedtls_rsa_get_len( rsa );
  2799. mbedtls_rsa_free( rsa );
  2800. mbedtls_free( rsa );
  2801. }
  2802. else
  2803. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
  2804. * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
  2805. {
  2806. status = PSA_ERROR_NOT_SUPPORTED;
  2807. }
  2808. exit:
  2809. unlock_status = psa_unlock_key_slot( slot );
  2810. return( ( status == PSA_SUCCESS ) ? unlock_status : status );
  2811. }
  2812. psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
  2813. psa_algorithm_t alg,
  2814. const uint8_t *input,
  2815. size_t input_length,
  2816. const uint8_t *salt,
  2817. size_t salt_length,
  2818. uint8_t *output,
  2819. size_t output_size,
  2820. size_t *output_length )
  2821. {
  2822. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2823. psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
  2824. psa_key_slot_t *slot;
  2825. (void) input;
  2826. (void) input_length;
  2827. (void) salt;
  2828. (void) output;
  2829. (void) output_size;
  2830. *output_length = 0;
  2831. if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
  2832. return( PSA_ERROR_INVALID_ARGUMENT );
  2833. status = psa_get_and_lock_transparent_key_slot_with_policy(
  2834. key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
  2835. if( status != PSA_SUCCESS )
  2836. return( status );
  2837. if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
  2838. {
  2839. status = PSA_ERROR_INVALID_ARGUMENT;
  2840. goto exit;
  2841. }
  2842. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
  2843. defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
  2844. if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
  2845. {
  2846. mbedtls_rsa_context *rsa = NULL;
  2847. status = mbedtls_psa_rsa_load_representation( slot->attr.type,
  2848. slot->key.data,
  2849. slot->key.bytes,
  2850. &rsa );
  2851. if( status != PSA_SUCCESS )
  2852. goto exit;
  2853. if( input_length != mbedtls_rsa_get_len( rsa ) )
  2854. {
  2855. status = PSA_ERROR_INVALID_ARGUMENT;
  2856. goto rsa_exit;
  2857. }
  2858. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
  2859. if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
  2860. {
  2861. status = mbedtls_to_psa_error(
  2862. mbedtls_rsa_pkcs1_decrypt( rsa,
  2863. mbedtls_psa_get_random,
  2864. MBEDTLS_PSA_RANDOM_STATE,
  2865. MBEDTLS_RSA_PRIVATE,
  2866. output_length,
  2867. input,
  2868. output,
  2869. output_size ) );
  2870. }
  2871. else
  2872. #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
  2873. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
  2874. if( PSA_ALG_IS_RSA_OAEP( alg ) )
  2875. {
  2876. psa_rsa_oaep_set_padding_mode( alg, rsa );
  2877. status = mbedtls_to_psa_error(
  2878. mbedtls_rsa_rsaes_oaep_decrypt( rsa,
  2879. mbedtls_psa_get_random,
  2880. MBEDTLS_PSA_RANDOM_STATE,
  2881. MBEDTLS_RSA_PRIVATE,
  2882. salt, salt_length,
  2883. output_length,
  2884. input,
  2885. output,
  2886. output_size ) );
  2887. }
  2888. else
  2889. #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
  2890. {
  2891. status = PSA_ERROR_INVALID_ARGUMENT;
  2892. }
  2893. rsa_exit:
  2894. mbedtls_rsa_free( rsa );
  2895. mbedtls_free( rsa );
  2896. }
  2897. else
  2898. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
  2899. * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
  2900. {
  2901. status = PSA_ERROR_NOT_SUPPORTED;
  2902. }
  2903. exit:
  2904. unlock_status = psa_unlock_key_slot( slot );
  2905. return( ( status == PSA_SUCCESS ) ? unlock_status : status );
  2906. }
  2907. /****************************************************************/
  2908. /* Symmetric cryptography */
  2909. /****************************************************************/
  2910. static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
  2911. mbedtls_svc_key_id_t key,
  2912. psa_algorithm_t alg,
  2913. mbedtls_operation_t cipher_operation )
  2914. {
  2915. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2916. psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
  2917. psa_key_slot_t *slot = NULL;
  2918. psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
  2919. PSA_KEY_USAGE_ENCRYPT :
  2920. PSA_KEY_USAGE_DECRYPT );
  2921. /* A context must be freshly initialized before it can be set up. */
  2922. if( operation->id != 0 )
  2923. {
  2924. status = PSA_ERROR_BAD_STATE;
  2925. goto exit;
  2926. }
  2927. if( ! PSA_ALG_IS_CIPHER( alg ) )
  2928. {
  2929. status = PSA_ERROR_INVALID_ARGUMENT;
  2930. goto exit;
  2931. }
  2932. status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
  2933. if( status != PSA_SUCCESS )
  2934. goto exit;
  2935. /* Initialize the operation struct members, except for id. The id member
  2936. * is used to indicate to psa_cipher_abort that there are resources to free,
  2937. * so we only set it (in the driver wrapper) after resources have been
  2938. * allocated/initialized. */
  2939. operation->iv_set = 0;
  2940. if( alg == PSA_ALG_ECB_NO_PADDING )
  2941. operation->iv_required = 0;
  2942. else
  2943. operation->iv_required = 1;
  2944. operation->default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg );
  2945. psa_key_attributes_t attributes = {
  2946. .core = slot->attr
  2947. };
  2948. /* Try doing the operation through a driver before using software fallback. */
  2949. if( cipher_operation == MBEDTLS_ENCRYPT )
  2950. status = psa_driver_wrapper_cipher_encrypt_setup( operation,
  2951. &attributes,
  2952. slot->key.data,
  2953. slot->key.bytes,
  2954. alg );
  2955. else
  2956. status = psa_driver_wrapper_cipher_decrypt_setup( operation,
  2957. &attributes,
  2958. slot->key.data,
  2959. slot->key.bytes,
  2960. alg );
  2961. exit:
  2962. if( status != PSA_SUCCESS )
  2963. psa_cipher_abort( operation );
  2964. unlock_status = psa_unlock_key_slot( slot );
  2965. return( ( status == PSA_SUCCESS ) ? unlock_status : status );
  2966. }
  2967. psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
  2968. mbedtls_svc_key_id_t key,
  2969. psa_algorithm_t alg )
  2970. {
  2971. return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
  2972. }
  2973. psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
  2974. mbedtls_svc_key_id_t key,
  2975. psa_algorithm_t alg )
  2976. {
  2977. return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
  2978. }
  2979. psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
  2980. uint8_t *iv,
  2981. size_t iv_size,
  2982. size_t *iv_length )
  2983. {
  2984. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2985. *iv_length = 0;
  2986. if( operation->id == 0 )
  2987. {
  2988. status = PSA_ERROR_BAD_STATE;
  2989. goto exit;
  2990. }
  2991. if( operation->iv_set || ! operation->iv_required )
  2992. {
  2993. status = PSA_ERROR_BAD_STATE;
  2994. goto exit;
  2995. }
  2996. if( iv_size < operation->default_iv_length )
  2997. {
  2998. status = PSA_ERROR_BUFFER_TOO_SMALL;
  2999. goto exit;
  3000. }
  3001. status = psa_generate_random( iv, operation->default_iv_length );
  3002. if( status != PSA_SUCCESS )
  3003. goto exit;
  3004. status = psa_driver_wrapper_cipher_set_iv( operation,
  3005. iv,
  3006. operation->default_iv_length );
  3007. exit:
  3008. if( status == PSA_SUCCESS )
  3009. {
  3010. operation->iv_set = 1;
  3011. *iv_length = operation->default_iv_length;
  3012. }
  3013. else
  3014. psa_cipher_abort( operation );
  3015. return( status );
  3016. }
  3017. psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
  3018. const uint8_t *iv,
  3019. size_t iv_length )
  3020. {
  3021. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  3022. if( operation->id == 0 )
  3023. {
  3024. status = PSA_ERROR_BAD_STATE;
  3025. goto exit;
  3026. }
  3027. if( operation->iv_set || ! operation->iv_required )
  3028. {
  3029. status = PSA_ERROR_BAD_STATE;
  3030. goto exit;
  3031. }
  3032. if( iv_length > PSA_CIPHER_IV_MAX_SIZE )
  3033. {
  3034. status = PSA_ERROR_INVALID_ARGUMENT;
  3035. goto exit;
  3036. }
  3037. status = psa_driver_wrapper_cipher_set_iv( operation,
  3038. iv,
  3039. iv_length );
  3040. exit:
  3041. if( status == PSA_SUCCESS )
  3042. operation->iv_set = 1;
  3043. else
  3044. psa_cipher_abort( operation );
  3045. return( status );
  3046. }
  3047. psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
  3048. const uint8_t *input,
  3049. size_t input_length,
  3050. uint8_t *output,
  3051. size_t output_size,
  3052. size_t *output_length )
  3053. {
  3054. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  3055. if( operation->id == 0 )
  3056. {
  3057. status = PSA_ERROR_BAD_STATE;
  3058. goto exit;
  3059. }
  3060. if( operation->iv_required && ! operation->iv_set )
  3061. {
  3062. status = PSA_ERROR_BAD_STATE;
  3063. goto exit;
  3064. }
  3065. status = psa_driver_wrapper_cipher_update( operation,
  3066. input,
  3067. input_length,
  3068. output,
  3069. output_size,
  3070. output_length );
  3071. exit:
  3072. if( status != PSA_SUCCESS )
  3073. psa_cipher_abort( operation );
  3074. return( status );
  3075. }
  3076. psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
  3077. uint8_t *output,
  3078. size_t output_size,
  3079. size_t *output_length )
  3080. {
  3081. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  3082. if( operation->id == 0 )
  3083. {
  3084. status = PSA_ERROR_BAD_STATE;
  3085. goto exit;
  3086. }
  3087. if( operation->iv_required && ! operation->iv_set )
  3088. {
  3089. status = PSA_ERROR_BAD_STATE;
  3090. goto exit;
  3091. }
  3092. status = psa_driver_wrapper_cipher_finish( operation,
  3093. output,
  3094. output_size,
  3095. output_length );
  3096. exit:
  3097. if( status == PSA_SUCCESS )
  3098. return( psa_cipher_abort( operation ) );
  3099. else
  3100. {
  3101. *output_length = 0;
  3102. (void) psa_cipher_abort( operation );
  3103. return( status );
  3104. }
  3105. }
  3106. psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
  3107. {
  3108. if( operation->id == 0 )
  3109. {
  3110. /* The object has (apparently) been initialized but it is not (yet)
  3111. * in use. It's ok to call abort on such an object, and there's
  3112. * nothing to do. */
  3113. return( PSA_SUCCESS );
  3114. }
  3115. psa_driver_wrapper_cipher_abort( operation );
  3116. operation->id = 0;
  3117. operation->iv_set = 0;
  3118. operation->iv_required = 0;
  3119. return( PSA_SUCCESS );
  3120. }
  3121. psa_status_t psa_cipher_encrypt( mbedtls_svc_key_id_t key,
  3122. psa_algorithm_t alg,
  3123. const uint8_t *input,
  3124. size_t input_length,
  3125. uint8_t *output,
  3126. size_t output_size,
  3127. size_t *output_length )
  3128. {
  3129. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  3130. psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
  3131. psa_key_slot_t *slot;
  3132. psa_key_type_t key_type;
  3133. size_t iv_length;
  3134. *output_length = 0;
  3135. if( ! PSA_ALG_IS_CIPHER( alg ) )
  3136. return( PSA_ERROR_INVALID_ARGUMENT );
  3137. status = psa_get_and_lock_key_slot_with_policy( key, &slot,
  3138. PSA_KEY_USAGE_ENCRYPT,
  3139. alg );
  3140. if( status != PSA_SUCCESS )
  3141. return( status );
  3142. psa_key_attributes_t attributes = {
  3143. .core = slot->attr
  3144. };
  3145. key_type = slot->attr.type;
  3146. iv_length = PSA_CIPHER_IV_LENGTH( key_type, alg );
  3147. if( iv_length > 0 )
  3148. {
  3149. if( output_size < iv_length )
  3150. {
  3151. status = PSA_ERROR_BUFFER_TOO_SMALL;
  3152. goto exit;
  3153. }
  3154. status = psa_generate_random( output, iv_length );
  3155. if( status != PSA_SUCCESS )
  3156. goto exit;
  3157. }
  3158. status = psa_driver_wrapper_cipher_encrypt(
  3159. &attributes, slot->key.data, slot->key.bytes,
  3160. alg, input, input_length,
  3161. output, output_size, output_length );
  3162. exit:
  3163. unlock_status = psa_unlock_key_slot( slot );
  3164. return( ( status == PSA_SUCCESS ) ? unlock_status : status );
  3165. }
  3166. psa_status_t psa_cipher_decrypt( mbedtls_svc_key_id_t key,
  3167. psa_algorithm_t alg,
  3168. const uint8_t *input,
  3169. size_t input_length,
  3170. uint8_t *output,
  3171. size_t output_size,
  3172. size_t *output_length )
  3173. {
  3174. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  3175. psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
  3176. psa_key_slot_t *slot;
  3177. *output_length = 0;
  3178. if( ! PSA_ALG_IS_CIPHER( alg ) )
  3179. return( PSA_ERROR_INVALID_ARGUMENT );
  3180. status = psa_get_and_lock_key_slot_with_policy( key, &slot,
  3181. PSA_KEY_USAGE_DECRYPT,
  3182. alg );
  3183. if( status != PSA_SUCCESS )
  3184. return( status );
  3185. psa_key_attributes_t attributes = {
  3186. .core = slot->attr
  3187. };
  3188. if( input_length < PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ) )
  3189. {
  3190. status = PSA_ERROR_INVALID_ARGUMENT;
  3191. goto exit;
  3192. }
  3193. status = psa_driver_wrapper_cipher_decrypt(
  3194. &attributes, slot->key.data, slot->key.bytes,
  3195. alg, input, input_length,
  3196. output, output_size, output_length );
  3197. exit:
  3198. unlock_status = psa_unlock_key_slot( slot );
  3199. return( ( status == PSA_SUCCESS ) ? unlock_status : status );
  3200. }
  3201. /****************************************************************/
  3202. /* AEAD */
  3203. /****************************************************************/
  3204. psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
  3205. psa_algorithm_t alg,
  3206. const uint8_t *nonce,
  3207. size_t nonce_length,
  3208. const uint8_t *additional_data,
  3209. size_t additional_data_length,
  3210. const uint8_t *plaintext,
  3211. size_t plaintext_length,
  3212. uint8_t *ciphertext,
  3213. size_t ciphertext_size,
  3214. size_t *ciphertext_length )
  3215. {
  3216. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  3217. psa_key_slot_t *slot;
  3218. *ciphertext_length = 0;
  3219. if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) )
  3220. return( PSA_ERROR_NOT_SUPPORTED );
  3221. status = psa_get_and_lock_key_slot_with_policy(
  3222. key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
  3223. if( status != PSA_SUCCESS )
  3224. return( status );
  3225. psa_key_attributes_t attributes = {
  3226. .core = slot->attr
  3227. };
  3228. status = psa_driver_wrapper_aead_encrypt(
  3229. &attributes, slot->key.data, slot->key.bytes,
  3230. alg,
  3231. nonce, nonce_length,
  3232. additional_data, additional_data_length,
  3233. plaintext, plaintext_length,
  3234. ciphertext, ciphertext_size, ciphertext_length );
  3235. if( status != PSA_SUCCESS && ciphertext_size != 0 )
  3236. memset( ciphertext, 0, ciphertext_size );
  3237. psa_unlock_key_slot( slot );
  3238. return( status );
  3239. }
  3240. psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
  3241. psa_algorithm_t alg,
  3242. const uint8_t *nonce,
  3243. size_t nonce_length,
  3244. const uint8_t *additional_data,
  3245. size_t additional_data_length,
  3246. const uint8_t *ciphertext,
  3247. size_t ciphertext_length,
  3248. uint8_t *plaintext,
  3249. size_t plaintext_size,
  3250. size_t *plaintext_length )
  3251. {
  3252. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  3253. psa_key_slot_t *slot;
  3254. *plaintext_length = 0;
  3255. if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) )
  3256. return( PSA_ERROR_NOT_SUPPORTED );
  3257. status = psa_get_and_lock_key_slot_with_policy(
  3258. key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
  3259. if( status != PSA_SUCCESS )
  3260. return( status );
  3261. psa_key_attributes_t attributes = {
  3262. .core = slot->attr
  3263. };
  3264. status = psa_driver_wrapper_aead_decrypt(
  3265. &attributes, slot->key.data, slot->key.bytes,
  3266. alg,
  3267. nonce, nonce_length,
  3268. additional_data, additional_data_length,
  3269. ciphertext, ciphertext_length,
  3270. plaintext, plaintext_size, plaintext_length );
  3271. if( status != PSA_SUCCESS && plaintext_size != 0 )
  3272. memset( plaintext, 0, plaintext_size );
  3273. psa_unlock_key_slot( slot );
  3274. return( status );
  3275. }
  3276. /****************************************************************/
  3277. /* Generators */
  3278. /****************************************************************/
  3279. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
  3280. defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
  3281. defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
  3282. #define AT_LEAST_ONE_BUILTIN_KDF
  3283. #endif /* At least one builtin KDF */
  3284. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
  3285. defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
  3286. defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
  3287. static psa_status_t psa_key_derivation_start_hmac(
  3288. psa_mac_operation_t *operation,
  3289. psa_algorithm_t hash_alg,
  3290. const uint8_t *hmac_key,
  3291. size_t hmac_key_length )
  3292. {
  3293. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  3294. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  3295. psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
  3296. psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( hmac_key_length ) );
  3297. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
  3298. operation->is_sign = 1;
  3299. operation->mac_size = PSA_HASH_LENGTH( hash_alg );
  3300. status = psa_driver_wrapper_mac_sign_setup( operation,
  3301. &attributes,
  3302. hmac_key, hmac_key_length,
  3303. PSA_ALG_HMAC( hash_alg ) );
  3304. psa_reset_key_attributes( &attributes );
  3305. return( status );
  3306. }
  3307. #endif /* KDF algorithms reliant on HMAC */
  3308. #define HKDF_STATE_INIT 0 /* no input yet */
  3309. #define HKDF_STATE_STARTED 1 /* got salt */
  3310. #define HKDF_STATE_KEYED 2 /* got key */
  3311. #define HKDF_STATE_OUTPUT 3 /* output started */
  3312. static psa_algorithm_t psa_key_derivation_get_kdf_alg(
  3313. const psa_key_derivation_operation_t *operation )
  3314. {
  3315. if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
  3316. return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
  3317. else
  3318. return( operation->alg );
  3319. }
  3320. psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
  3321. {
  3322. psa_status_t status = PSA_SUCCESS;
  3323. psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
  3324. if( kdf_alg == 0 )
  3325. {
  3326. /* The object has (apparently) been initialized but it is not
  3327. * in use. It's ok to call abort on such an object, and there's
  3328. * nothing to do. */
  3329. }
  3330. else
  3331. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
  3332. if( PSA_ALG_IS_HKDF( kdf_alg ) )
  3333. {
  3334. mbedtls_free( operation->ctx.hkdf.info );
  3335. status = psa_mac_abort( &operation->ctx.hkdf.hmac );
  3336. }
  3337. else
  3338. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
  3339. #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
  3340. defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
  3341. if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
  3342. /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
  3343. PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
  3344. {
  3345. if( operation->ctx.tls12_prf.secret != NULL )
  3346. {
  3347. mbedtls_platform_zeroize( operation->ctx.tls12_prf.secret,
  3348. operation->ctx.tls12_prf.secret_length );
  3349. mbedtls_free( operation->ctx.tls12_prf.secret );
  3350. }
  3351. if( operation->ctx.tls12_prf.seed != NULL )
  3352. {
  3353. mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
  3354. operation->ctx.tls12_prf.seed_length );
  3355. mbedtls_free( operation->ctx.tls12_prf.seed );
  3356. }
  3357. if( operation->ctx.tls12_prf.label != NULL )
  3358. {
  3359. mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
  3360. operation->ctx.tls12_prf.label_length );
  3361. mbedtls_free( operation->ctx.tls12_prf.label );
  3362. }
  3363. status = PSA_SUCCESS;
  3364. /* We leave the fields Ai and output_block to be erased safely by the
  3365. * mbedtls_platform_zeroize() in the end of this function. */
  3366. }
  3367. else
  3368. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
  3369. * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
  3370. {
  3371. status = PSA_ERROR_BAD_STATE;
  3372. }
  3373. mbedtls_platform_zeroize( operation, sizeof( *operation ) );
  3374. return( status );
  3375. }
  3376. psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
  3377. size_t *capacity)
  3378. {
  3379. if( operation->alg == 0 )
  3380. {
  3381. /* This is a blank key derivation operation. */
  3382. return( PSA_ERROR_BAD_STATE );
  3383. }
  3384. *capacity = operation->capacity;
  3385. return( PSA_SUCCESS );
  3386. }
  3387. psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
  3388. size_t capacity )
  3389. {
  3390. if( operation->alg == 0 )
  3391. return( PSA_ERROR_BAD_STATE );
  3392. if( capacity > operation->capacity )
  3393. return( PSA_ERROR_INVALID_ARGUMENT );
  3394. operation->capacity = capacity;
  3395. return( PSA_SUCCESS );
  3396. }
  3397. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
  3398. /* Read some bytes from an HKDF-based operation. This performs a chunk
  3399. * of the expand phase of the HKDF algorithm. */
  3400. static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
  3401. psa_algorithm_t hash_alg,
  3402. uint8_t *output,
  3403. size_t output_length )
  3404. {
  3405. uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
  3406. size_t hmac_output_length;
  3407. psa_status_t status;
  3408. if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
  3409. return( PSA_ERROR_BAD_STATE );
  3410. hkdf->state = HKDF_STATE_OUTPUT;
  3411. while( output_length != 0 )
  3412. {
  3413. /* Copy what remains of the current block */
  3414. uint8_t n = hash_length - hkdf->offset_in_block;
  3415. if( n > output_length )
  3416. n = (uint8_t) output_length;
  3417. memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
  3418. output += n;
  3419. output_length -= n;
  3420. hkdf->offset_in_block += n;
  3421. if( output_length == 0 )
  3422. break;
  3423. /* We can't be wanting more output after block 0xff, otherwise
  3424. * the capacity check in psa_key_derivation_output_bytes() would have
  3425. * prevented this call. It could happen only if the operation
  3426. * object was corrupted or if this function is called directly
  3427. * inside the library. */
  3428. if( hkdf->block_number == 0xff )
  3429. return( PSA_ERROR_BAD_STATE );
  3430. /* We need a new block */
  3431. ++hkdf->block_number;
  3432. hkdf->offset_in_block = 0;
  3433. status = psa_key_derivation_start_hmac( &hkdf->hmac,
  3434. hash_alg,
  3435. hkdf->prk,
  3436. hash_length );
  3437. if( status != PSA_SUCCESS )
  3438. return( status );
  3439. if( hkdf->block_number != 1 )
  3440. {
  3441. status = psa_mac_update( &hkdf->hmac,
  3442. hkdf->output_block,
  3443. hash_length );
  3444. if( status != PSA_SUCCESS )
  3445. return( status );
  3446. }
  3447. status = psa_mac_update( &hkdf->hmac,
  3448. hkdf->info,
  3449. hkdf->info_length );
  3450. if( status != PSA_SUCCESS )
  3451. return( status );
  3452. status = psa_mac_update( &hkdf->hmac,
  3453. &hkdf->block_number, 1 );
  3454. if( status != PSA_SUCCESS )
  3455. return( status );
  3456. status = psa_mac_sign_finish( &hkdf->hmac,
  3457. hkdf->output_block,
  3458. sizeof( hkdf->output_block ),
  3459. &hmac_output_length );
  3460. if( status != PSA_SUCCESS )
  3461. return( status );
  3462. }
  3463. return( PSA_SUCCESS );
  3464. }
  3465. #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
  3466. #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
  3467. defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
  3468. static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
  3469. psa_tls12_prf_key_derivation_t *tls12_prf,
  3470. psa_algorithm_t alg )
  3471. {
  3472. psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
  3473. uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
  3474. psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
  3475. size_t hmac_output_length;
  3476. psa_status_t status, cleanup_status;
  3477. /* We can't be wanting more output after block 0xff, otherwise
  3478. * the capacity check in psa_key_derivation_output_bytes() would have
  3479. * prevented this call. It could happen only if the operation
  3480. * object was corrupted or if this function is called directly
  3481. * inside the library. */
  3482. if( tls12_prf->block_number == 0xff )
  3483. return( PSA_ERROR_CORRUPTION_DETECTED );
  3484. /* We need a new block */
  3485. ++tls12_prf->block_number;
  3486. tls12_prf->left_in_block = hash_length;
  3487. /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
  3488. *
  3489. * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
  3490. *
  3491. * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
  3492. * HMAC_hash(secret, A(2) + seed) +
  3493. * HMAC_hash(secret, A(3) + seed) + ...
  3494. *
  3495. * A(0) = seed
  3496. * A(i) = HMAC_hash(secret, A(i-1))
  3497. *
  3498. * The `psa_tls12_prf_key_derivation` structure saves the block
  3499. * `HMAC_hash(secret, A(i) + seed)` from which the output
  3500. * is currently extracted as `output_block` and where i is
  3501. * `block_number`.
  3502. */
  3503. status = psa_key_derivation_start_hmac( &hmac,
  3504. hash_alg,
  3505. tls12_prf->secret,
  3506. tls12_prf->secret_length );
  3507. if( status != PSA_SUCCESS )
  3508. goto cleanup;
  3509. /* Calculate A(i) where i = tls12_prf->block_number. */
  3510. if( tls12_prf->block_number == 1 )
  3511. {
  3512. /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
  3513. * the variable seed and in this instance means it in the context of the
  3514. * P_hash function, where seed = label + seed.) */
  3515. status = psa_mac_update( &hmac,
  3516. tls12_prf->label,
  3517. tls12_prf->label_length );
  3518. if( status != PSA_SUCCESS )
  3519. goto cleanup;
  3520. status = psa_mac_update( &hmac,
  3521. tls12_prf->seed,
  3522. tls12_prf->seed_length );
  3523. if( status != PSA_SUCCESS )
  3524. goto cleanup;
  3525. }
  3526. else
  3527. {
  3528. /* A(i) = HMAC_hash(secret, A(i-1)) */
  3529. status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length );
  3530. if( status != PSA_SUCCESS )
  3531. goto cleanup;
  3532. }
  3533. status = psa_mac_sign_finish( &hmac,
  3534. tls12_prf->Ai, hash_length,
  3535. &hmac_output_length );
  3536. if( hmac_output_length != hash_length )
  3537. status = PSA_ERROR_CORRUPTION_DETECTED;
  3538. if( status != PSA_SUCCESS )
  3539. goto cleanup;
  3540. /* Calculate HMAC_hash(secret, A(i) + label + seed). */
  3541. status = psa_key_derivation_start_hmac( &hmac,
  3542. hash_alg,
  3543. tls12_prf->secret,
  3544. tls12_prf->secret_length );
  3545. if( status != PSA_SUCCESS )
  3546. goto cleanup;
  3547. status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length );
  3548. if( status != PSA_SUCCESS )
  3549. goto cleanup;
  3550. status = psa_mac_update( &hmac, tls12_prf->label, tls12_prf->label_length );
  3551. if( status != PSA_SUCCESS )
  3552. goto cleanup;
  3553. status = psa_mac_update( &hmac, tls12_prf->seed, tls12_prf->seed_length );
  3554. if( status != PSA_SUCCESS )
  3555. goto cleanup;
  3556. status = psa_mac_sign_finish( &hmac,
  3557. tls12_prf->output_block, hash_length,
  3558. &hmac_output_length );
  3559. if( status != PSA_SUCCESS )
  3560. goto cleanup;
  3561. cleanup:
  3562. cleanup_status = psa_mac_abort( &hmac );
  3563. if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
  3564. status = cleanup_status;
  3565. return( status );
  3566. }
  3567. static psa_status_t psa_key_derivation_tls12_prf_read(
  3568. psa_tls12_prf_key_derivation_t *tls12_prf,
  3569. psa_algorithm_t alg,
  3570. uint8_t *output,
  3571. size_t output_length )
  3572. {
  3573. psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
  3574. uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
  3575. psa_status_t status;
  3576. uint8_t offset, length;
  3577. switch( tls12_prf->state )
  3578. {
  3579. case PSA_TLS12_PRF_STATE_LABEL_SET:
  3580. tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
  3581. break;
  3582. case PSA_TLS12_PRF_STATE_OUTPUT:
  3583. break;
  3584. default:
  3585. return( PSA_ERROR_BAD_STATE );
  3586. }
  3587. while( output_length != 0 )
  3588. {
  3589. /* Check if we have fully processed the current block. */
  3590. if( tls12_prf->left_in_block == 0 )
  3591. {
  3592. status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
  3593. alg );
  3594. if( status != PSA_SUCCESS )
  3595. return( status );
  3596. continue;
  3597. }
  3598. if( tls12_prf->left_in_block > output_length )
  3599. length = (uint8_t) output_length;
  3600. else
  3601. length = tls12_prf->left_in_block;
  3602. offset = hash_length - tls12_prf->left_in_block;
  3603. memcpy( output, tls12_prf->output_block + offset, length );
  3604. output += length;
  3605. output_length -= length;
  3606. tls12_prf->left_in_block -= length;
  3607. }
  3608. return( PSA_SUCCESS );
  3609. }
  3610. #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
  3611. * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
  3612. psa_status_t psa_key_derivation_output_bytes(
  3613. psa_key_derivation_operation_t *operation,
  3614. uint8_t *output,
  3615. size_t output_length )
  3616. {
  3617. psa_status_t status;
  3618. psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
  3619. if( operation->alg == 0 )
  3620. {
  3621. /* This is a blank operation. */
  3622. return( PSA_ERROR_BAD_STATE );
  3623. }
  3624. if( output_length > operation->capacity )
  3625. {
  3626. operation->capacity = 0;
  3627. /* Go through the error path to wipe all confidential data now
  3628. * that the operation object is useless. */
  3629. status = PSA_ERROR_INSUFFICIENT_DATA;
  3630. goto exit;
  3631. }
  3632. if( output_length == 0 && operation->capacity == 0 )
  3633. {
  3634. /* Edge case: this is a finished operation, and 0 bytes
  3635. * were requested. The right error in this case could
  3636. * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
  3637. * INSUFFICIENT_CAPACITY, which is right for a finished
  3638. * operation, for consistency with the case when
  3639. * output_length > 0. */
  3640. return( PSA_ERROR_INSUFFICIENT_DATA );
  3641. }
  3642. operation->capacity -= output_length;
  3643. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
  3644. if( PSA_ALG_IS_HKDF( kdf_alg ) )
  3645. {
  3646. psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
  3647. status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
  3648. output, output_length );
  3649. }
  3650. else
  3651. #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
  3652. #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
  3653. defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
  3654. if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
  3655. PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
  3656. {
  3657. status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
  3658. kdf_alg, output,
  3659. output_length );
  3660. }
  3661. else
  3662. #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
  3663. * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
  3664. {
  3665. (void) kdf_alg;
  3666. return( PSA_ERROR_BAD_STATE );
  3667. }
  3668. exit:
  3669. if( status != PSA_SUCCESS )
  3670. {
  3671. /* Preserve the algorithm upon errors, but clear all sensitive state.
  3672. * This allows us to differentiate between exhausted operations and
  3673. * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
  3674. * operations. */
  3675. psa_algorithm_t alg = operation->alg;
  3676. psa_key_derivation_abort( operation );
  3677. operation->alg = alg;
  3678. memset( output, '!', output_length );
  3679. }
  3680. return( status );
  3681. }
  3682. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
  3683. static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
  3684. {
  3685. if( data_size >= 8 )
  3686. mbedtls_des_key_set_parity( data );
  3687. if( data_size >= 16 )
  3688. mbedtls_des_key_set_parity( data + 8 );
  3689. if( data_size >= 24 )
  3690. mbedtls_des_key_set_parity( data + 16 );
  3691. }
  3692. #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
  3693. static psa_status_t psa_generate_derived_key_internal(
  3694. psa_key_slot_t *slot,
  3695. size_t bits,
  3696. psa_key_derivation_operation_t *operation )
  3697. {
  3698. uint8_t *data = NULL;
  3699. size_t bytes = PSA_BITS_TO_BYTES( bits );
  3700. psa_status_t status;
  3701. if( ! key_type_is_raw_bytes( slot->attr.type ) )
  3702. return( PSA_ERROR_INVALID_ARGUMENT );
  3703. if( bits % 8 != 0 )
  3704. return( PSA_ERROR_INVALID_ARGUMENT );
  3705. data = mbedtls_calloc( 1, bytes );
  3706. if( data == NULL )
  3707. return( PSA_ERROR_INSUFFICIENT_MEMORY );
  3708. status = psa_key_derivation_output_bytes( operation, data, bytes );
  3709. if( status != PSA_SUCCESS )
  3710. goto exit;
  3711. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
  3712. if( slot->attr.type == PSA_KEY_TYPE_DES )
  3713. psa_des_set_key_parity( data, bytes );
  3714. #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
  3715. status = psa_allocate_buffer_to_slot( slot, bytes );
  3716. if( status != PSA_SUCCESS )
  3717. goto exit;
  3718. slot->attr.bits = (psa_key_bits_t) bits;
  3719. psa_key_attributes_t attributes = {
  3720. .core = slot->attr
  3721. };
  3722. status = psa_driver_wrapper_import_key( &attributes,
  3723. data, bytes,
  3724. slot->key.data,
  3725. slot->key.bytes,
  3726. &slot->key.bytes, &bits );
  3727. if( bits != slot->attr.bits )
  3728. status = PSA_ERROR_INVALID_ARGUMENT;
  3729. exit:
  3730. mbedtls_free( data );
  3731. return( status );
  3732. }
  3733. psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
  3734. psa_key_derivation_operation_t *operation,
  3735. mbedtls_svc_key_id_t *key )
  3736. {
  3737. psa_status_t status;
  3738. psa_key_slot_t *slot = NULL;
  3739. psa_se_drv_table_entry_t *driver = NULL;
  3740. *key = MBEDTLS_SVC_KEY_ID_INIT;
  3741. /* Reject any attempt to create a zero-length key so that we don't
  3742. * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
  3743. if( psa_get_key_bits( attributes ) == 0 )
  3744. return( PSA_ERROR_INVALID_ARGUMENT );
  3745. if( ! operation->can_output_key )
  3746. return( PSA_ERROR_NOT_PERMITTED );
  3747. status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
  3748. &slot, &driver );
  3749. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  3750. if( driver != NULL )
  3751. {
  3752. /* Deriving a key in a secure element is not implemented yet. */
  3753. status = PSA_ERROR_NOT_SUPPORTED;
  3754. }
  3755. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  3756. if( status == PSA_SUCCESS )
  3757. {
  3758. status = psa_generate_derived_key_internal( slot,
  3759. attributes->core.bits,
  3760. operation );
  3761. }
  3762. if( status == PSA_SUCCESS )
  3763. status = psa_finish_key_creation( slot, driver, key );
  3764. if( status != PSA_SUCCESS )
  3765. psa_fail_key_creation( slot, driver );
  3766. return( status );
  3767. }
  3768. /****************************************************************/
  3769. /* Key derivation */
  3770. /****************************************************************/
  3771. #if defined(AT_LEAST_ONE_BUILTIN_KDF)
  3772. static psa_status_t psa_key_derivation_setup_kdf(
  3773. psa_key_derivation_operation_t *operation,
  3774. psa_algorithm_t kdf_alg )
  3775. {
  3776. int is_kdf_alg_supported;
  3777. /* Make sure that operation->ctx is properly zero-initialised. (Macro
  3778. * initialisers for this union leave some bytes unspecified.) */
  3779. memset( &operation->ctx, 0, sizeof( operation->ctx ) );
  3780. /* Make sure that kdf_alg is a supported key derivation algorithm. */
  3781. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
  3782. if( PSA_ALG_IS_HKDF( kdf_alg ) )
  3783. is_kdf_alg_supported = 1;
  3784. else
  3785. #endif
  3786. #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
  3787. if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
  3788. is_kdf_alg_supported = 1;
  3789. else
  3790. #endif
  3791. #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
  3792. if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
  3793. is_kdf_alg_supported = 1;
  3794. else
  3795. #endif
  3796. is_kdf_alg_supported = 0;
  3797. if( is_kdf_alg_supported )
  3798. {
  3799. psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
  3800. size_t hash_size = PSA_HASH_LENGTH( hash_alg );
  3801. if( hash_size == 0 )
  3802. return( PSA_ERROR_NOT_SUPPORTED );
  3803. if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
  3804. PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
  3805. ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
  3806. {
  3807. return( PSA_ERROR_NOT_SUPPORTED );
  3808. }
  3809. operation->capacity = 255 * hash_size;
  3810. return( PSA_SUCCESS );
  3811. }
  3812. return( PSA_ERROR_NOT_SUPPORTED );
  3813. }
  3814. #endif /* AT_LEAST_ONE_BUILTIN_KDF */
  3815. psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
  3816. psa_algorithm_t alg )
  3817. {
  3818. psa_status_t status;
  3819. if( operation->alg != 0 )
  3820. return( PSA_ERROR_BAD_STATE );
  3821. if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
  3822. return( PSA_ERROR_INVALID_ARGUMENT );
  3823. else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
  3824. {
  3825. #if defined(AT_LEAST_ONE_BUILTIN_KDF)
  3826. psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
  3827. status = psa_key_derivation_setup_kdf( operation, kdf_alg );
  3828. #else
  3829. return( PSA_ERROR_NOT_SUPPORTED );
  3830. #endif /* AT_LEAST_ONE_BUILTIN_KDF */
  3831. }
  3832. else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
  3833. {
  3834. #if defined(AT_LEAST_ONE_BUILTIN_KDF)
  3835. status = psa_key_derivation_setup_kdf( operation, alg );
  3836. #else
  3837. return( PSA_ERROR_NOT_SUPPORTED );
  3838. #endif /* AT_LEAST_ONE_BUILTIN_KDF */
  3839. }
  3840. else
  3841. return( PSA_ERROR_INVALID_ARGUMENT );
  3842. if( status == PSA_SUCCESS )
  3843. operation->alg = alg;
  3844. return( status );
  3845. }
  3846. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
  3847. static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
  3848. psa_algorithm_t hash_alg,
  3849. psa_key_derivation_step_t step,
  3850. const uint8_t *data,
  3851. size_t data_length )
  3852. {
  3853. psa_status_t status;
  3854. switch( step )
  3855. {
  3856. case PSA_KEY_DERIVATION_INPUT_SALT:
  3857. if( hkdf->state != HKDF_STATE_INIT )
  3858. return( PSA_ERROR_BAD_STATE );
  3859. else
  3860. {
  3861. status = psa_key_derivation_start_hmac( &hkdf->hmac,
  3862. hash_alg,
  3863. data, data_length );
  3864. if( status != PSA_SUCCESS )
  3865. return( status );
  3866. hkdf->state = HKDF_STATE_STARTED;
  3867. return( PSA_SUCCESS );
  3868. }
  3869. case PSA_KEY_DERIVATION_INPUT_SECRET:
  3870. /* If no salt was provided, use an empty salt. */
  3871. if( hkdf->state == HKDF_STATE_INIT )
  3872. {
  3873. status = psa_key_derivation_start_hmac( &hkdf->hmac,
  3874. hash_alg,
  3875. NULL, 0 );
  3876. if( status != PSA_SUCCESS )
  3877. return( status );
  3878. hkdf->state = HKDF_STATE_STARTED;
  3879. }
  3880. if( hkdf->state != HKDF_STATE_STARTED )
  3881. return( PSA_ERROR_BAD_STATE );
  3882. status = psa_mac_update( &hkdf->hmac,
  3883. data, data_length );
  3884. if( status != PSA_SUCCESS )
  3885. return( status );
  3886. status = psa_mac_sign_finish( &hkdf->hmac,
  3887. hkdf->prk,
  3888. sizeof( hkdf->prk ),
  3889. &data_length );
  3890. if( status != PSA_SUCCESS )
  3891. return( status );
  3892. hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
  3893. hkdf->block_number = 0;
  3894. hkdf->state = HKDF_STATE_KEYED;
  3895. return( PSA_SUCCESS );
  3896. case PSA_KEY_DERIVATION_INPUT_INFO:
  3897. if( hkdf->state == HKDF_STATE_OUTPUT )
  3898. return( PSA_ERROR_BAD_STATE );
  3899. if( hkdf->info_set )
  3900. return( PSA_ERROR_BAD_STATE );
  3901. hkdf->info_length = data_length;
  3902. if( data_length != 0 )
  3903. {
  3904. hkdf->info = mbedtls_calloc( 1, data_length );
  3905. if( hkdf->info == NULL )
  3906. return( PSA_ERROR_INSUFFICIENT_MEMORY );
  3907. memcpy( hkdf->info, data, data_length );
  3908. }
  3909. hkdf->info_set = 1;
  3910. return( PSA_SUCCESS );
  3911. default:
  3912. return( PSA_ERROR_INVALID_ARGUMENT );
  3913. }
  3914. }
  3915. #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
  3916. #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
  3917. defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
  3918. static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
  3919. const uint8_t *data,
  3920. size_t data_length )
  3921. {
  3922. if( prf->state != PSA_TLS12_PRF_STATE_INIT )
  3923. return( PSA_ERROR_BAD_STATE );
  3924. if( data_length != 0 )
  3925. {
  3926. prf->seed = mbedtls_calloc( 1, data_length );
  3927. if( prf->seed == NULL )
  3928. return( PSA_ERROR_INSUFFICIENT_MEMORY );
  3929. memcpy( prf->seed, data, data_length );
  3930. prf->seed_length = data_length;
  3931. }
  3932. prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
  3933. return( PSA_SUCCESS );
  3934. }
  3935. static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
  3936. const uint8_t *data,
  3937. size_t data_length )
  3938. {
  3939. if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
  3940. return( PSA_ERROR_BAD_STATE );
  3941. if( data_length != 0 )
  3942. {
  3943. prf->secret = mbedtls_calloc( 1, data_length );
  3944. if( prf->secret == NULL )
  3945. return( PSA_ERROR_INSUFFICIENT_MEMORY );
  3946. memcpy( prf->secret, data, data_length );
  3947. prf->secret_length = data_length;
  3948. }
  3949. prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
  3950. return( PSA_SUCCESS );
  3951. }
  3952. static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
  3953. const uint8_t *data,
  3954. size_t data_length )
  3955. {
  3956. if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
  3957. return( PSA_ERROR_BAD_STATE );
  3958. if( data_length != 0 )
  3959. {
  3960. prf->label = mbedtls_calloc( 1, data_length );
  3961. if( prf->label == NULL )
  3962. return( PSA_ERROR_INSUFFICIENT_MEMORY );
  3963. memcpy( prf->label, data, data_length );
  3964. prf->label_length = data_length;
  3965. }
  3966. prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
  3967. return( PSA_SUCCESS );
  3968. }
  3969. static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
  3970. psa_key_derivation_step_t step,
  3971. const uint8_t *data,
  3972. size_t data_length )
  3973. {
  3974. switch( step )
  3975. {
  3976. case PSA_KEY_DERIVATION_INPUT_SEED:
  3977. return( psa_tls12_prf_set_seed( prf, data, data_length ) );
  3978. case PSA_KEY_DERIVATION_INPUT_SECRET:
  3979. return( psa_tls12_prf_set_key( prf, data, data_length ) );
  3980. case PSA_KEY_DERIVATION_INPUT_LABEL:
  3981. return( psa_tls12_prf_set_label( prf, data, data_length ) );
  3982. default:
  3983. return( PSA_ERROR_INVALID_ARGUMENT );
  3984. }
  3985. }
  3986. #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
  3987. * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
  3988. #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
  3989. static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
  3990. psa_tls12_prf_key_derivation_t *prf,
  3991. const uint8_t *data,
  3992. size_t data_length )
  3993. {
  3994. psa_status_t status;
  3995. uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
  3996. uint8_t *cur = pms;
  3997. if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
  3998. return( PSA_ERROR_INVALID_ARGUMENT );
  3999. /* Quoting RFC 4279, Section 2:
  4000. *
  4001. * The premaster secret is formed as follows: if the PSK is N octets
  4002. * long, concatenate a uint16 with the value N, N zero octets, a second
  4003. * uint16 with the value N, and the PSK itself.
  4004. */
  4005. *cur++ = ( data_length >> 8 ) & 0xff;
  4006. *cur++ = ( data_length >> 0 ) & 0xff;
  4007. memset( cur, 0, data_length );
  4008. cur += data_length;
  4009. *cur++ = pms[0];
  4010. *cur++ = pms[1];
  4011. memcpy( cur, data, data_length );
  4012. cur += data_length;
  4013. status = psa_tls12_prf_set_key( prf, pms, cur - pms );
  4014. mbedtls_platform_zeroize( pms, sizeof( pms ) );
  4015. return( status );
  4016. }
  4017. static psa_status_t psa_tls12_prf_psk_to_ms_input(
  4018. psa_tls12_prf_key_derivation_t *prf,
  4019. psa_key_derivation_step_t step,
  4020. const uint8_t *data,
  4021. size_t data_length )
  4022. {
  4023. if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
  4024. {
  4025. return( psa_tls12_prf_psk_to_ms_set_key( prf,
  4026. data, data_length ) );
  4027. }
  4028. return( psa_tls12_prf_input( prf, step, data, data_length ) );
  4029. }
  4030. #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
  4031. /** Check whether the given key type is acceptable for the given
  4032. * input step of a key derivation.
  4033. *
  4034. * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
  4035. * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
  4036. * Both secret and non-secret inputs can alternatively have the type
  4037. * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
  4038. * that the input was passed as a buffer rather than via a key object.
  4039. */
  4040. static int psa_key_derivation_check_input_type(
  4041. psa_key_derivation_step_t step,
  4042. psa_key_type_t key_type )
  4043. {
  4044. switch( step )
  4045. {
  4046. case PSA_KEY_DERIVATION_INPUT_SECRET:
  4047. if( key_type == PSA_KEY_TYPE_DERIVE )
  4048. return( PSA_SUCCESS );
  4049. if( key_type == PSA_KEY_TYPE_NONE )
  4050. return( PSA_SUCCESS );
  4051. break;
  4052. case PSA_KEY_DERIVATION_INPUT_LABEL:
  4053. case PSA_KEY_DERIVATION_INPUT_SALT:
  4054. case PSA_KEY_DERIVATION_INPUT_INFO:
  4055. case PSA_KEY_DERIVATION_INPUT_SEED:
  4056. if( key_type == PSA_KEY_TYPE_RAW_DATA )
  4057. return( PSA_SUCCESS );
  4058. if( key_type == PSA_KEY_TYPE_NONE )
  4059. return( PSA_SUCCESS );
  4060. break;
  4061. }
  4062. return( PSA_ERROR_INVALID_ARGUMENT );
  4063. }
  4064. static psa_status_t psa_key_derivation_input_internal(
  4065. psa_key_derivation_operation_t *operation,
  4066. psa_key_derivation_step_t step,
  4067. psa_key_type_t key_type,
  4068. const uint8_t *data,
  4069. size_t data_length )
  4070. {
  4071. psa_status_t status;
  4072. psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
  4073. status = psa_key_derivation_check_input_type( step, key_type );
  4074. if( status != PSA_SUCCESS )
  4075. goto exit;
  4076. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
  4077. if( PSA_ALG_IS_HKDF( kdf_alg ) )
  4078. {
  4079. status = psa_hkdf_input( &operation->ctx.hkdf,
  4080. PSA_ALG_HKDF_GET_HASH( kdf_alg ),
  4081. step, data, data_length );
  4082. }
  4083. else
  4084. #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
  4085. #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
  4086. if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
  4087. {
  4088. status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
  4089. step, data, data_length );
  4090. }
  4091. else
  4092. #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
  4093. #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
  4094. if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
  4095. {
  4096. status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
  4097. step, data, data_length );
  4098. }
  4099. else
  4100. #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
  4101. {
  4102. /* This can't happen unless the operation object was not initialized */
  4103. (void) data;
  4104. (void) data_length;
  4105. (void) kdf_alg;
  4106. return( PSA_ERROR_BAD_STATE );
  4107. }
  4108. exit:
  4109. if( status != PSA_SUCCESS )
  4110. psa_key_derivation_abort( operation );
  4111. return( status );
  4112. }
  4113. psa_status_t psa_key_derivation_input_bytes(
  4114. psa_key_derivation_operation_t *operation,
  4115. psa_key_derivation_step_t step,
  4116. const uint8_t *data,
  4117. size_t data_length )
  4118. {
  4119. return( psa_key_derivation_input_internal( operation, step,
  4120. PSA_KEY_TYPE_NONE,
  4121. data, data_length ) );
  4122. }
  4123. psa_status_t psa_key_derivation_input_key(
  4124. psa_key_derivation_operation_t *operation,
  4125. psa_key_derivation_step_t step,
  4126. mbedtls_svc_key_id_t key )
  4127. {
  4128. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  4129. psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
  4130. psa_key_slot_t *slot;
  4131. status = psa_get_and_lock_transparent_key_slot_with_policy(
  4132. key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
  4133. if( status != PSA_SUCCESS )
  4134. {
  4135. psa_key_derivation_abort( operation );
  4136. return( status );
  4137. }
  4138. /* Passing a key object as a SECRET input unlocks the permission
  4139. * to output to a key object. */
  4140. if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
  4141. operation->can_output_key = 1;
  4142. status = psa_key_derivation_input_internal( operation,
  4143. step, slot->attr.type,
  4144. slot->key.data,
  4145. slot->key.bytes );
  4146. unlock_status = psa_unlock_key_slot( slot );
  4147. return( ( status == PSA_SUCCESS ) ? unlock_status : status );
  4148. }
  4149. /****************************************************************/
  4150. /* Key agreement */
  4151. /****************************************************************/
  4152. #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
  4153. static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
  4154. size_t peer_key_length,
  4155. const mbedtls_ecp_keypair *our_key,
  4156. uint8_t *shared_secret,
  4157. size_t shared_secret_size,
  4158. size_t *shared_secret_length )
  4159. {
  4160. mbedtls_ecp_keypair *their_key = NULL;
  4161. mbedtls_ecdh_context ecdh;
  4162. psa_status_t status;
  4163. size_t bits = 0;
  4164. psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
  4165. mbedtls_ecdh_init( &ecdh );
  4166. status = mbedtls_psa_ecp_load_representation(
  4167. PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
  4168. bits,
  4169. peer_key,
  4170. peer_key_length,
  4171. &their_key );
  4172. if( status != PSA_SUCCESS )
  4173. goto exit;
  4174. status = mbedtls_to_psa_error(
  4175. mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
  4176. if( status != PSA_SUCCESS )
  4177. goto exit;
  4178. status = mbedtls_to_psa_error(
  4179. mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
  4180. if( status != PSA_SUCCESS )
  4181. goto exit;
  4182. status = mbedtls_to_psa_error(
  4183. mbedtls_ecdh_calc_secret( &ecdh,
  4184. shared_secret_length,
  4185. shared_secret, shared_secret_size,
  4186. mbedtls_psa_get_random,
  4187. MBEDTLS_PSA_RANDOM_STATE ) );
  4188. if( status != PSA_SUCCESS )
  4189. goto exit;
  4190. if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
  4191. status = PSA_ERROR_CORRUPTION_DETECTED;
  4192. exit:
  4193. if( status != PSA_SUCCESS )
  4194. mbedtls_platform_zeroize( shared_secret, shared_secret_size );
  4195. mbedtls_ecdh_free( &ecdh );
  4196. mbedtls_ecp_keypair_free( their_key );
  4197. mbedtls_free( their_key );
  4198. return( status );
  4199. }
  4200. #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
  4201. #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
  4202. static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
  4203. psa_key_slot_t *private_key,
  4204. const uint8_t *peer_key,
  4205. size_t peer_key_length,
  4206. uint8_t *shared_secret,
  4207. size_t shared_secret_size,
  4208. size_t *shared_secret_length )
  4209. {
  4210. switch( alg )
  4211. {
  4212. #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
  4213. case PSA_ALG_ECDH:
  4214. if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
  4215. return( PSA_ERROR_INVALID_ARGUMENT );
  4216. mbedtls_ecp_keypair *ecp = NULL;
  4217. psa_status_t status = mbedtls_psa_ecp_load_representation(
  4218. private_key->attr.type,
  4219. private_key->attr.bits,
  4220. private_key->key.data,
  4221. private_key->key.bytes,
  4222. &ecp );
  4223. if( status != PSA_SUCCESS )
  4224. return( status );
  4225. status = psa_key_agreement_ecdh( peer_key, peer_key_length,
  4226. ecp,
  4227. shared_secret, shared_secret_size,
  4228. shared_secret_length );
  4229. mbedtls_ecp_keypair_free( ecp );
  4230. mbedtls_free( ecp );
  4231. return( status );
  4232. #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
  4233. default:
  4234. (void) private_key;
  4235. (void) peer_key;
  4236. (void) peer_key_length;
  4237. (void) shared_secret;
  4238. (void) shared_secret_size;
  4239. (void) shared_secret_length;
  4240. return( PSA_ERROR_NOT_SUPPORTED );
  4241. }
  4242. }
  4243. /* Note that if this function fails, you must call psa_key_derivation_abort()
  4244. * to potentially free embedded data structures and wipe confidential data.
  4245. */
  4246. static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
  4247. psa_key_derivation_step_t step,
  4248. psa_key_slot_t *private_key,
  4249. const uint8_t *peer_key,
  4250. size_t peer_key_length )
  4251. {
  4252. psa_status_t status;
  4253. uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
  4254. size_t shared_secret_length = 0;
  4255. psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
  4256. /* Step 1: run the secret agreement algorithm to generate the shared
  4257. * secret. */
  4258. status = psa_key_agreement_raw_internal( ka_alg,
  4259. private_key,
  4260. peer_key, peer_key_length,
  4261. shared_secret,
  4262. sizeof( shared_secret ),
  4263. &shared_secret_length );
  4264. if( status != PSA_SUCCESS )
  4265. goto exit;
  4266. /* Step 2: set up the key derivation to generate key material from
  4267. * the shared secret. A shared secret is permitted wherever a key
  4268. * of type DERIVE is permitted. */
  4269. status = psa_key_derivation_input_internal( operation, step,
  4270. PSA_KEY_TYPE_DERIVE,
  4271. shared_secret,
  4272. shared_secret_length );
  4273. exit:
  4274. mbedtls_platform_zeroize( shared_secret, shared_secret_length );
  4275. return( status );
  4276. }
  4277. psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
  4278. psa_key_derivation_step_t step,
  4279. mbedtls_svc_key_id_t private_key,
  4280. const uint8_t *peer_key,
  4281. size_t peer_key_length )
  4282. {
  4283. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  4284. psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
  4285. psa_key_slot_t *slot;
  4286. if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
  4287. return( PSA_ERROR_INVALID_ARGUMENT );
  4288. status = psa_get_and_lock_transparent_key_slot_with_policy(
  4289. private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
  4290. if( status != PSA_SUCCESS )
  4291. return( status );
  4292. status = psa_key_agreement_internal( operation, step,
  4293. slot,
  4294. peer_key, peer_key_length );
  4295. if( status != PSA_SUCCESS )
  4296. psa_key_derivation_abort( operation );
  4297. else
  4298. {
  4299. /* If a private key has been added as SECRET, we allow the derived
  4300. * key material to be used as a key in PSA Crypto. */
  4301. if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
  4302. operation->can_output_key = 1;
  4303. }
  4304. unlock_status = psa_unlock_key_slot( slot );
  4305. return( ( status == PSA_SUCCESS ) ? unlock_status : status );
  4306. }
  4307. psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
  4308. mbedtls_svc_key_id_t private_key,
  4309. const uint8_t *peer_key,
  4310. size_t peer_key_length,
  4311. uint8_t *output,
  4312. size_t output_size,
  4313. size_t *output_length )
  4314. {
  4315. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  4316. psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
  4317. psa_key_slot_t *slot = NULL;
  4318. if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
  4319. {
  4320. status = PSA_ERROR_INVALID_ARGUMENT;
  4321. goto exit;
  4322. }
  4323. status = psa_get_and_lock_transparent_key_slot_with_policy(
  4324. private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
  4325. if( status != PSA_SUCCESS )
  4326. goto exit;
  4327. status = psa_key_agreement_raw_internal( alg, slot,
  4328. peer_key, peer_key_length,
  4329. output, output_size,
  4330. output_length );
  4331. exit:
  4332. if( status != PSA_SUCCESS )
  4333. {
  4334. /* If an error happens and is not handled properly, the output
  4335. * may be used as a key to protect sensitive data. Arrange for such
  4336. * a key to be random, which is likely to result in decryption or
  4337. * verification errors. This is better than filling the buffer with
  4338. * some constant data such as zeros, which would result in the data
  4339. * being protected with a reproducible, easily knowable key.
  4340. */
  4341. psa_generate_random( output, output_size );
  4342. *output_length = output_size;
  4343. }
  4344. unlock_status = psa_unlock_key_slot( slot );
  4345. return( ( status == PSA_SUCCESS ) ? unlock_status : status );
  4346. }
  4347. /****************************************************************/
  4348. /* Random generation */
  4349. /****************************************************************/
  4350. /** Initialize the PSA random generator.
  4351. */
  4352. static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
  4353. {
  4354. #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
  4355. memset( rng, 0, sizeof( *rng ) );
  4356. #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  4357. /* Set default configuration if
  4358. * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
  4359. if( rng->entropy_init == NULL )
  4360. rng->entropy_init = mbedtls_entropy_init;
  4361. if( rng->entropy_free == NULL )
  4362. rng->entropy_free = mbedtls_entropy_free;
  4363. rng->entropy_init( &rng->entropy );
  4364. #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
  4365. defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
  4366. /* The PSA entropy injection feature depends on using NV seed as an entropy
  4367. * source. Add NV seed as an entropy source for PSA entropy injection. */
  4368. mbedtls_entropy_add_source( &rng->entropy,
  4369. mbedtls_nv_seed_poll, NULL,
  4370. MBEDTLS_ENTROPY_BLOCK_SIZE,
  4371. MBEDTLS_ENTROPY_SOURCE_STRONG );
  4372. #endif
  4373. mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
  4374. #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  4375. }
  4376. /** Deinitialize the PSA random generator.
  4377. */
  4378. static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
  4379. {
  4380. #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
  4381. memset( rng, 0, sizeof( *rng ) );
  4382. #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  4383. mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
  4384. rng->entropy_free( &rng->entropy );
  4385. #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  4386. }
  4387. /** Seed the PSA random generator.
  4388. */
  4389. static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
  4390. {
  4391. #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
  4392. /* Do nothing: the external RNG seeds itself. */
  4393. (void) rng;
  4394. return( PSA_SUCCESS );
  4395. #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  4396. const unsigned char drbg_seed[] = "PSA";
  4397. int ret = mbedtls_psa_drbg_seed( &rng->entropy,
  4398. drbg_seed, sizeof( drbg_seed ) - 1 );
  4399. return mbedtls_to_psa_error( ret );
  4400. #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  4401. }
  4402. psa_status_t psa_generate_random( uint8_t *output,
  4403. size_t output_size )
  4404. {
  4405. GUARD_MODULE_INITIALIZED;
  4406. #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
  4407. size_t output_length = 0;
  4408. psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
  4409. output, output_size,
  4410. &output_length );
  4411. if( status != PSA_SUCCESS )
  4412. return( status );
  4413. /* Breaking up a request into smaller chunks is currently not supported
  4414. * for the extrernal RNG interface. */
  4415. if( output_length != output_size )
  4416. return( PSA_ERROR_INSUFFICIENT_ENTROPY );
  4417. return( PSA_SUCCESS );
  4418. #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  4419. while( output_size > 0 )
  4420. {
  4421. size_t request_size =
  4422. ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
  4423. MBEDTLS_PSA_RANDOM_MAX_REQUEST :
  4424. output_size );
  4425. int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
  4426. output, request_size );
  4427. if( ret != 0 )
  4428. return( mbedtls_to_psa_error( ret ) );
  4429. output_size -= request_size;
  4430. output += request_size;
  4431. }
  4432. return( PSA_SUCCESS );
  4433. #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  4434. }
  4435. /* Wrapper function allowing the classic API to use the PSA RNG.
  4436. *
  4437. * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
  4438. * `psa_generate_random(...)`. The state parameter is ignored since the
  4439. * PSA API doesn't support passing an explicit state.
  4440. *
  4441. * In the non-external case, psa_generate_random() calls an
  4442. * `mbedtls_xxx_drbg_random` function which has exactly the same signature
  4443. * and semantics as mbedtls_psa_get_random(). As an optimization,
  4444. * instead of doing this back-and-forth between the PSA API and the
  4445. * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
  4446. * as a constant function pointer to `mbedtls_xxx_drbg_random`.
  4447. */
  4448. #if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
  4449. int mbedtls_psa_get_random( void *p_rng,
  4450. unsigned char *output,
  4451. size_t output_size )
  4452. {
  4453. /* This function takes a pointer to the RNG state because that's what
  4454. * classic mbedtls functions using an RNG expect. The PSA RNG manages
  4455. * its own state internally and doesn't let the caller access that state.
  4456. * So we just ignore the state parameter, and in practice we'll pass
  4457. * NULL. */
  4458. (void) p_rng;
  4459. psa_status_t status = psa_generate_random( output, output_size );
  4460. if( status == PSA_SUCCESS )
  4461. return( 0 );
  4462. else
  4463. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  4464. }
  4465. #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  4466. #if defined(MBEDTLS_PSA_INJECT_ENTROPY)
  4467. #include "mbedtls/entropy_poll.h"
  4468. psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
  4469. size_t seed_size )
  4470. {
  4471. if( global_data.initialized )
  4472. return( PSA_ERROR_NOT_PERMITTED );
  4473. if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
  4474. ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
  4475. ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
  4476. return( PSA_ERROR_INVALID_ARGUMENT );
  4477. return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
  4478. }
  4479. #endif /* MBEDTLS_PSA_INJECT_ENTROPY */
  4480. /** Validate the key type and size for key generation
  4481. *
  4482. * \param type The key type
  4483. * \param bits The number of bits of the key
  4484. *
  4485. * \retval #PSA_SUCCESS
  4486. * The key type and size are valid.
  4487. * \retval #PSA_ERROR_INVALID_ARGUMENT
  4488. * The size in bits of the key is not valid.
  4489. * \retval #PSA_ERROR_NOT_SUPPORTED
  4490. * The type and/or the size in bits of the key or the combination of
  4491. * the two is not supported.
  4492. */
  4493. static psa_status_t psa_validate_key_type_and_size_for_key_generation(
  4494. psa_key_type_t type, size_t bits )
  4495. {
  4496. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  4497. if( key_type_is_raw_bytes( type ) )
  4498. {
  4499. status = validate_unstructured_key_bit_size( type, bits );
  4500. if( status != PSA_SUCCESS )
  4501. return( status );
  4502. }
  4503. else
  4504. #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
  4505. if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
  4506. {
  4507. if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
  4508. return( PSA_ERROR_NOT_SUPPORTED );
  4509. /* Accept only byte-aligned keys, for the same reasons as
  4510. * in psa_import_rsa_key(). */
  4511. if( bits % 8 != 0 )
  4512. return( PSA_ERROR_NOT_SUPPORTED );
  4513. }
  4514. else
  4515. #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
  4516. #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
  4517. if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
  4518. {
  4519. /* To avoid empty block, return successfully here. */
  4520. return( PSA_SUCCESS );
  4521. }
  4522. else
  4523. #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
  4524. {
  4525. return( PSA_ERROR_NOT_SUPPORTED );
  4526. }
  4527. return( PSA_SUCCESS );
  4528. }
  4529. psa_status_t psa_generate_key_internal(
  4530. const psa_key_attributes_t *attributes,
  4531. uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
  4532. {
  4533. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  4534. psa_key_type_t type = attributes->core.type;
  4535. if( ( attributes->domain_parameters == NULL ) &&
  4536. ( attributes->domain_parameters_size != 0 ) )
  4537. return( PSA_ERROR_INVALID_ARGUMENT );
  4538. if( key_type_is_raw_bytes( type ) )
  4539. {
  4540. status = psa_generate_random( key_buffer, key_buffer_size );
  4541. if( status != PSA_SUCCESS )
  4542. return( status );
  4543. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
  4544. if( type == PSA_KEY_TYPE_DES )
  4545. psa_des_set_key_parity( key_buffer, key_buffer_size );
  4546. #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
  4547. }
  4548. else
  4549. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
  4550. defined(MBEDTLS_GENPRIME)
  4551. if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
  4552. {
  4553. return( mbedtls_psa_rsa_generate_key( attributes,
  4554. key_buffer,
  4555. key_buffer_size,
  4556. key_buffer_length ) );
  4557. }
  4558. else
  4559. #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
  4560. * defined(MBEDTLS_GENPRIME) */
  4561. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
  4562. if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
  4563. {
  4564. return( mbedtls_psa_ecp_generate_key( attributes,
  4565. key_buffer,
  4566. key_buffer_size,
  4567. key_buffer_length ) );
  4568. }
  4569. else
  4570. #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
  4571. {
  4572. (void)key_buffer_length;
  4573. return( PSA_ERROR_NOT_SUPPORTED );
  4574. }
  4575. return( PSA_SUCCESS );
  4576. }
  4577. psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
  4578. mbedtls_svc_key_id_t *key )
  4579. {
  4580. psa_status_t status;
  4581. psa_key_slot_t *slot = NULL;
  4582. psa_se_drv_table_entry_t *driver = NULL;
  4583. size_t key_buffer_size;
  4584. *key = MBEDTLS_SVC_KEY_ID_INIT;
  4585. /* Reject any attempt to create a zero-length key so that we don't
  4586. * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
  4587. if( psa_get_key_bits( attributes ) == 0 )
  4588. return( PSA_ERROR_INVALID_ARGUMENT );
  4589. status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
  4590. &slot, &driver );
  4591. if( status != PSA_SUCCESS )
  4592. goto exit;
  4593. /* In the case of a transparent key or an opaque key stored in local
  4594. * storage (thus not in the case of generating a key in a secure element
  4595. * or cryptoprocessor with storage), we have to allocate a buffer to
  4596. * hold the generated key material. */
  4597. if( slot->key.data == NULL )
  4598. {
  4599. if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
  4600. PSA_KEY_LOCATION_LOCAL_STORAGE )
  4601. {
  4602. status = psa_validate_key_type_and_size_for_key_generation(
  4603. attributes->core.type, attributes->core.bits );
  4604. if( status != PSA_SUCCESS )
  4605. goto exit;
  4606. key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
  4607. attributes->core.type,
  4608. attributes->core.bits );
  4609. }
  4610. else
  4611. {
  4612. status = psa_driver_wrapper_get_key_buffer_size(
  4613. attributes, &key_buffer_size );
  4614. if( status != PSA_SUCCESS )
  4615. goto exit;
  4616. }
  4617. status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
  4618. if( status != PSA_SUCCESS )
  4619. goto exit;
  4620. }
  4621. status = psa_driver_wrapper_generate_key( attributes,
  4622. slot->key.data, slot->key.bytes, &slot->key.bytes );
  4623. if( status != PSA_SUCCESS )
  4624. psa_remove_key_data_from_memory( slot );
  4625. exit:
  4626. if( status == PSA_SUCCESS )
  4627. status = psa_finish_key_creation( slot, driver, key );
  4628. if( status != PSA_SUCCESS )
  4629. psa_fail_key_creation( slot, driver );
  4630. return( status );
  4631. }
  4632. /****************************************************************/
  4633. /* Module setup */
  4634. /****************************************************************/
  4635. #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
  4636. psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
  4637. void (* entropy_init )( mbedtls_entropy_context *ctx ),
  4638. void (* entropy_free )( mbedtls_entropy_context *ctx ) )
  4639. {
  4640. if( global_data.rng_state != RNG_NOT_INITIALIZED )
  4641. return( PSA_ERROR_BAD_STATE );
  4642. global_data.rng.entropy_init = entropy_init;
  4643. global_data.rng.entropy_free = entropy_free;
  4644. return( PSA_SUCCESS );
  4645. }
  4646. #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
  4647. void mbedtls_psa_crypto_free( void )
  4648. {
  4649. psa_wipe_all_key_slots( );
  4650. if( global_data.rng_state != RNG_NOT_INITIALIZED )
  4651. {
  4652. mbedtls_psa_random_free( &global_data.rng );
  4653. }
  4654. /* Wipe all remaining data, including configuration.
  4655. * In particular, this sets all state indicator to the value
  4656. * indicating "uninitialized". */
  4657. mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
  4658. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  4659. /* Unregister all secure element drivers, so that we restart from
  4660. * a pristine state. */
  4661. psa_unregister_all_se_drivers( );
  4662. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  4663. }
  4664. #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
  4665. /** Recover a transaction that was interrupted by a power failure.
  4666. *
  4667. * This function is called during initialization, before psa_crypto_init()
  4668. * returns. If this function returns a failure status, the initialization
  4669. * fails.
  4670. */
  4671. static psa_status_t psa_crypto_recover_transaction(
  4672. const psa_crypto_transaction_t *transaction )
  4673. {
  4674. switch( transaction->unknown.type )
  4675. {
  4676. case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
  4677. case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
  4678. /* TODO - fall through to the failure case until this
  4679. * is implemented.
  4680. * https://github.com/ARMmbed/mbed-crypto/issues/218
  4681. */
  4682. default:
  4683. /* We found an unsupported transaction in the storage.
  4684. * We don't know what state the storage is in. Give up. */
  4685. return( PSA_ERROR_DATA_INVALID );
  4686. }
  4687. }
  4688. #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
  4689. psa_status_t psa_crypto_init( void )
  4690. {
  4691. psa_status_t status;
  4692. /* Double initialization is explicitly allowed. */
  4693. if( global_data.initialized != 0 )
  4694. return( PSA_SUCCESS );
  4695. /* Initialize and seed the random generator. */
  4696. mbedtls_psa_random_init( &global_data.rng );
  4697. global_data.rng_state = RNG_INITIALIZED;
  4698. status = mbedtls_psa_random_seed( &global_data.rng );
  4699. if( status != PSA_SUCCESS )
  4700. goto exit;
  4701. global_data.rng_state = RNG_SEEDED;
  4702. status = psa_initialize_key_slots( );
  4703. if( status != PSA_SUCCESS )
  4704. goto exit;
  4705. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  4706. status = psa_init_all_se_drivers( );
  4707. if( status != PSA_SUCCESS )
  4708. goto exit;
  4709. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  4710. #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
  4711. status = psa_crypto_load_transaction( );
  4712. if( status == PSA_SUCCESS )
  4713. {
  4714. status = psa_crypto_recover_transaction( &psa_crypto_transaction );
  4715. if( status != PSA_SUCCESS )
  4716. goto exit;
  4717. status = psa_crypto_stop_transaction( );
  4718. }
  4719. else if( status == PSA_ERROR_DOES_NOT_EXIST )
  4720. {
  4721. /* There's no transaction to complete. It's all good. */
  4722. status = PSA_SUCCESS;
  4723. }
  4724. #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
  4725. /* All done. */
  4726. global_data.initialized = 1;
  4727. exit:
  4728. if( status != PSA_SUCCESS )
  4729. mbedtls_psa_crypto_free( );
  4730. return( status );
  4731. }
  4732. #endif /* MBEDTLS_PSA_CRYPTO_C */