psa_crypto_driver_wrappers.h 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897
  1. /*
  2. * Functions to delegate cryptographic operations to an available
  3. * and appropriate accelerator.
  4. * Warning: This file is now auto-generated.
  5. */
  6. /* Copyright The Mbed TLS Contributors
  7. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  8. */
  9. /* BEGIN-common headers */
  10. #include "common.h"
  11. #include "psa_crypto_aead.h"
  12. #include "psa_crypto_cipher.h"
  13. #include "psa_crypto_core.h"
  14. #include "psa_crypto_driver_wrappers_no_static.h"
  15. #include "psa_crypto_hash.h"
  16. #include "psa_crypto_mac.h"
  17. #include "psa_crypto_pake.h"
  18. #include "psa_crypto_rsa.h"
  19. #include "mbedtls/platform.h"
  20. #include "mbedtls/constant_time.h"
  21. /* END-common headers */
  22. #if defined(MBEDTLS_PSA_CRYPTO_C)
  23. /* BEGIN-driver headers */
  24. /* Headers for mbedtls_test opaque driver */
  25. #if defined(PSA_CRYPTO_DRIVER_TEST)
  26. #include "test/drivers/test_driver.h"
  27. #endif
  28. /* Headers for mbedtls_test transparent driver */
  29. #if defined(PSA_CRYPTO_DRIVER_TEST)
  30. #include "test/drivers/test_driver.h"
  31. #endif
  32. /* Headers for p256 transparent driver */
  33. #if defined(MBEDTLS_PSA_P256M_DRIVER_ENABLED)
  34. #include "../3rdparty/p256-m/p256-m_driver_entrypoints.h"
  35. #endif
  36. /* END-driver headers */
  37. /* Auto-generated values depending on which drivers are registered.
  38. * ID 0 is reserved for unallocated operations.
  39. * ID 1 is reserved for the Mbed TLS software driver. */
  40. /* BEGIN-driver id definition */
  41. #define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
  42. #define MBEDTLS_TEST_OPAQUE_DRIVER_ID (2)
  43. #define MBEDTLS_TEST_TRANSPARENT_DRIVER_ID (3)
  44. #define P256_TRANSPARENT_DRIVER_ID (4)
  45. /* END-driver id */
  46. /* BEGIN-Common Macro definitions */
  47. /* END-Common Macro definitions */
  48. /* Support the 'old' SE interface when asked to */
  49. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  50. /* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
  51. * SE driver is present, to avoid unused argument errors at compile time. */
  52. #ifndef PSA_CRYPTO_DRIVER_PRESENT
  53. #define PSA_CRYPTO_DRIVER_PRESENT
  54. #endif
  55. #include "psa_crypto_se.h"
  56. #endif
  57. static inline psa_status_t psa_driver_wrapper_init( void )
  58. {
  59. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  60. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  61. status = psa_init_all_se_drivers( );
  62. if( status != PSA_SUCCESS )
  63. return( status );
  64. #endif
  65. #if defined(PSA_CRYPTO_DRIVER_TEST)
  66. status = mbedtls_test_transparent_init( );
  67. if( status != PSA_SUCCESS )
  68. return( status );
  69. status = mbedtls_test_opaque_init( );
  70. if( status != PSA_SUCCESS )
  71. return( status );
  72. #endif
  73. (void) status;
  74. return( PSA_SUCCESS );
  75. }
  76. static inline void psa_driver_wrapper_free( void )
  77. {
  78. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  79. /* Unregister all secure element drivers, so that we restart from
  80. * a pristine state. */
  81. psa_unregister_all_se_drivers( );
  82. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  83. #if defined(PSA_CRYPTO_DRIVER_TEST)
  84. mbedtls_test_transparent_free( );
  85. mbedtls_test_opaque_free( );
  86. #endif
  87. }
  88. /* Start delegation functions */
  89. static inline psa_status_t psa_driver_wrapper_sign_message(
  90. const psa_key_attributes_t *attributes,
  91. const uint8_t *key_buffer,
  92. size_t key_buffer_size,
  93. psa_algorithm_t alg,
  94. const uint8_t *input,
  95. size_t input_length,
  96. uint8_t *signature,
  97. size_t signature_size,
  98. size_t *signature_length )
  99. {
  100. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  101. psa_key_location_t location =
  102. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  103. switch( location )
  104. {
  105. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  106. /* Key is stored in the slot in export representation, so
  107. * cycle through all known transparent accelerators */
  108. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  109. #if defined(PSA_CRYPTO_DRIVER_TEST)
  110. status = mbedtls_test_transparent_signature_sign_message(
  111. attributes,
  112. key_buffer,
  113. key_buffer_size,
  114. alg,
  115. input,
  116. input_length,
  117. signature,
  118. signature_size,
  119. signature_length );
  120. /* Declared with fallback == true */
  121. if( status != PSA_ERROR_NOT_SUPPORTED )
  122. return( status );
  123. #endif /* PSA_CRYPTO_DRIVER_TEST */
  124. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  125. break;
  126. /* Add cases for opaque driver here */
  127. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  128. #if defined(PSA_CRYPTO_DRIVER_TEST)
  129. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  130. status = mbedtls_test_opaque_signature_sign_message(
  131. attributes,
  132. key_buffer,
  133. key_buffer_size,
  134. alg,
  135. input,
  136. input_length,
  137. signature,
  138. signature_size,
  139. signature_length );
  140. if( status != PSA_ERROR_NOT_SUPPORTED )
  141. return( status );
  142. break;
  143. #endif /* PSA_CRYPTO_DRIVER_TEST */
  144. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  145. default:
  146. /* Key is declared with a lifetime not known to us */
  147. (void)status;
  148. break;
  149. }
  150. return( psa_sign_message_builtin( attributes,
  151. key_buffer,
  152. key_buffer_size,
  153. alg,
  154. input,
  155. input_length,
  156. signature,
  157. signature_size,
  158. signature_length ) );
  159. }
  160. static inline psa_status_t psa_driver_wrapper_verify_message(
  161. const psa_key_attributes_t *attributes,
  162. const uint8_t *key_buffer,
  163. size_t key_buffer_size,
  164. psa_algorithm_t alg,
  165. const uint8_t *input,
  166. size_t input_length,
  167. const uint8_t *signature,
  168. size_t signature_length )
  169. {
  170. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  171. psa_key_location_t location =
  172. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  173. switch( location )
  174. {
  175. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  176. /* Key is stored in the slot in export representation, so
  177. * cycle through all known transparent accelerators */
  178. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  179. #if defined(PSA_CRYPTO_DRIVER_TEST)
  180. status = mbedtls_test_transparent_signature_verify_message(
  181. attributes,
  182. key_buffer,
  183. key_buffer_size,
  184. alg,
  185. input,
  186. input_length,
  187. signature,
  188. signature_length );
  189. /* Declared with fallback == true */
  190. if( status != PSA_ERROR_NOT_SUPPORTED )
  191. return( status );
  192. #endif /* PSA_CRYPTO_DRIVER_TEST */
  193. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  194. break;
  195. /* Add cases for opaque driver here */
  196. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  197. #if defined(PSA_CRYPTO_DRIVER_TEST)
  198. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  199. return( mbedtls_test_opaque_signature_verify_message(
  200. attributes,
  201. key_buffer,
  202. key_buffer_size,
  203. alg,
  204. input,
  205. input_length,
  206. signature,
  207. signature_length ) );
  208. if( status != PSA_ERROR_NOT_SUPPORTED )
  209. return( status );
  210. break;
  211. #endif /* PSA_CRYPTO_DRIVER_TEST */
  212. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  213. default:
  214. /* Key is declared with a lifetime not known to us */
  215. (void)status;
  216. break;
  217. }
  218. return( psa_verify_message_builtin( attributes,
  219. key_buffer,
  220. key_buffer_size,
  221. alg,
  222. input,
  223. input_length,
  224. signature,
  225. signature_length ) );
  226. }
  227. static inline psa_status_t psa_driver_wrapper_sign_hash(
  228. const psa_key_attributes_t *attributes,
  229. const uint8_t *key_buffer, size_t key_buffer_size,
  230. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  231. uint8_t *signature, size_t signature_size, size_t *signature_length )
  232. {
  233. /* Try dynamically-registered SE interface first */
  234. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  235. const psa_drv_se_t *drv;
  236. psa_drv_se_context_t *drv_context;
  237. if( psa_get_se_driver( psa_get_key_lifetime(attributes), &drv, &drv_context ) )
  238. {
  239. if( drv->asymmetric == NULL ||
  240. drv->asymmetric->p_sign == NULL )
  241. {
  242. /* Key is defined in SE, but we have no way to exercise it */
  243. return( PSA_ERROR_NOT_SUPPORTED );
  244. }
  245. return( drv->asymmetric->p_sign(
  246. drv_context, *( (psa_key_slot_number_t *)key_buffer ),
  247. alg, hash, hash_length,
  248. signature, signature_size, signature_length ) );
  249. }
  250. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  251. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  252. psa_key_location_t location =
  253. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  254. switch( location )
  255. {
  256. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  257. /* Key is stored in the slot in export representation, so
  258. * cycle through all known transparent accelerators */
  259. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  260. #if defined(PSA_CRYPTO_DRIVER_TEST)
  261. status = mbedtls_test_transparent_signature_sign_hash( attributes,
  262. key_buffer,
  263. key_buffer_size,
  264. alg,
  265. hash,
  266. hash_length,
  267. signature,
  268. signature_size,
  269. signature_length );
  270. /* Declared with fallback == true */
  271. if( status != PSA_ERROR_NOT_SUPPORTED )
  272. return( status );
  273. #endif /* PSA_CRYPTO_DRIVER_TEST */
  274. #if defined (MBEDTLS_PSA_P256M_DRIVER_ENABLED)
  275. if( PSA_KEY_TYPE_IS_ECC( psa_get_key_type(attributes) ) &&
  276. PSA_ALG_IS_RANDOMIZED_ECDSA(alg) &&
  277. PSA_KEY_TYPE_ECC_GET_FAMILY(psa_get_key_type(attributes)) == PSA_ECC_FAMILY_SECP_R1 &&
  278. psa_get_key_bits(attributes) == 256 )
  279. {
  280. status = p256_transparent_sign_hash( attributes,
  281. key_buffer,
  282. key_buffer_size,
  283. alg,
  284. hash,
  285. hash_length,
  286. signature,
  287. signature_size,
  288. signature_length );
  289. if( status != PSA_ERROR_NOT_SUPPORTED )
  290. return( status );
  291. }
  292. #endif /* MBEDTLS_PSA_P256M_DRIVER_ENABLED */
  293. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  294. /* Fell through, meaning no accelerator supports this operation */
  295. return( psa_sign_hash_builtin( attributes,
  296. key_buffer,
  297. key_buffer_size,
  298. alg,
  299. hash,
  300. hash_length,
  301. signature,
  302. signature_size,
  303. signature_length ) );
  304. /* Add cases for opaque driver here */
  305. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  306. #if defined(PSA_CRYPTO_DRIVER_TEST)
  307. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  308. return( mbedtls_test_opaque_signature_sign_hash( attributes,
  309. key_buffer,
  310. key_buffer_size,
  311. alg,
  312. hash,
  313. hash_length,
  314. signature,
  315. signature_size,
  316. signature_length ) );
  317. #endif /* PSA_CRYPTO_DRIVER_TEST */
  318. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  319. default:
  320. /* Key is declared with a lifetime not known to us */
  321. (void)status;
  322. return( PSA_ERROR_INVALID_ARGUMENT );
  323. }
  324. }
  325. static inline psa_status_t psa_driver_wrapper_verify_hash(
  326. const psa_key_attributes_t *attributes,
  327. const uint8_t *key_buffer, size_t key_buffer_size,
  328. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  329. const uint8_t *signature, size_t signature_length )
  330. {
  331. /* Try dynamically-registered SE interface first */
  332. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  333. const psa_drv_se_t *drv;
  334. psa_drv_se_context_t *drv_context;
  335. if( psa_get_se_driver( psa_get_key_lifetime(attributes), &drv, &drv_context ) )
  336. {
  337. if( drv->asymmetric == NULL ||
  338. drv->asymmetric->p_verify == NULL )
  339. {
  340. /* Key is defined in SE, but we have no way to exercise it */
  341. return( PSA_ERROR_NOT_SUPPORTED );
  342. }
  343. return( drv->asymmetric->p_verify(
  344. drv_context, *( (psa_key_slot_number_t *)key_buffer ),
  345. alg, hash, hash_length,
  346. signature, signature_length ) );
  347. }
  348. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  349. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  350. psa_key_location_t location =
  351. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  352. switch( location )
  353. {
  354. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  355. /* Key is stored in the slot in export representation, so
  356. * cycle through all known transparent accelerators */
  357. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  358. #if defined(PSA_CRYPTO_DRIVER_TEST)
  359. status = mbedtls_test_transparent_signature_verify_hash(
  360. attributes,
  361. key_buffer,
  362. key_buffer_size,
  363. alg,
  364. hash,
  365. hash_length,
  366. signature,
  367. signature_length );
  368. /* Declared with fallback == true */
  369. if( status != PSA_ERROR_NOT_SUPPORTED )
  370. return( status );
  371. #endif /* PSA_CRYPTO_DRIVER_TEST */
  372. #if defined (MBEDTLS_PSA_P256M_DRIVER_ENABLED)
  373. if( PSA_KEY_TYPE_IS_ECC( psa_get_key_type(attributes) ) &&
  374. PSA_ALG_IS_ECDSA(alg) &&
  375. PSA_KEY_TYPE_ECC_GET_FAMILY(psa_get_key_type(attributes)) == PSA_ECC_FAMILY_SECP_R1 &&
  376. psa_get_key_bits(attributes) == 256 )
  377. {
  378. status = p256_transparent_verify_hash( attributes,
  379. key_buffer,
  380. key_buffer_size,
  381. alg,
  382. hash,
  383. hash_length,
  384. signature,
  385. signature_length );
  386. if( status != PSA_ERROR_NOT_SUPPORTED )
  387. return( status );
  388. }
  389. #endif /* MBEDTLS_PSA_P256M_DRIVER_ENABLED */
  390. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  391. return( psa_verify_hash_builtin( attributes,
  392. key_buffer,
  393. key_buffer_size,
  394. alg,
  395. hash,
  396. hash_length,
  397. signature,
  398. signature_length ) );
  399. /* Add cases for opaque driver here */
  400. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  401. #if defined(PSA_CRYPTO_DRIVER_TEST)
  402. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  403. return( mbedtls_test_opaque_signature_verify_hash( attributes,
  404. key_buffer,
  405. key_buffer_size,
  406. alg,
  407. hash,
  408. hash_length,
  409. signature,
  410. signature_length ) );
  411. #endif /* PSA_CRYPTO_DRIVER_TEST */
  412. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  413. default:
  414. /* Key is declared with a lifetime not known to us */
  415. (void)status;
  416. return( PSA_ERROR_INVALID_ARGUMENT );
  417. }
  418. }
  419. static inline uint32_t psa_driver_wrapper_sign_hash_get_num_ops(
  420. psa_sign_hash_interruptible_operation_t *operation )
  421. {
  422. switch( operation->id )
  423. {
  424. /* If uninitialised, return 0, as no work can have been done. */
  425. case 0:
  426. return 0;
  427. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  428. return(mbedtls_psa_sign_hash_get_num_ops(&operation->ctx.mbedtls_ctx));
  429. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  430. #if defined(PSA_CRYPTO_DRIVER_TEST)
  431. /* Add test driver tests here */
  432. #endif /* PSA_CRYPTO_DRIVER_TEST */
  433. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  434. }
  435. /* Can't happen (see discussion in #8271) */
  436. return 0;
  437. }
  438. static inline uint32_t psa_driver_wrapper_verify_hash_get_num_ops(
  439. psa_verify_hash_interruptible_operation_t *operation )
  440. {
  441. switch( operation->id )
  442. {
  443. /* If uninitialised, return 0, as no work can have been done. */
  444. case 0:
  445. return 0;
  446. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  447. return (mbedtls_psa_verify_hash_get_num_ops(&operation->ctx.mbedtls_ctx));
  448. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  449. #if defined(PSA_CRYPTO_DRIVER_TEST)
  450. /* Add test driver tests here */
  451. #endif /* PSA_CRYPTO_DRIVER_TEST */
  452. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  453. }
  454. /* Can't happen (see discussion in #8271) */
  455. return 0;
  456. }
  457. static inline psa_status_t psa_driver_wrapper_sign_hash_start(
  458. psa_sign_hash_interruptible_operation_t *operation,
  459. const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
  460. size_t key_buffer_size, psa_algorithm_t alg,
  461. const uint8_t *hash, size_t hash_length )
  462. {
  463. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  464. psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
  465. psa_get_key_lifetime(attributes) );
  466. switch( location )
  467. {
  468. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  469. /* Key is stored in the slot in export representation, so
  470. * cycle through all known transparent accelerators */
  471. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  472. #if defined(PSA_CRYPTO_DRIVER_TEST)
  473. /* Add test driver tests here */
  474. /* Declared with fallback == true */
  475. #endif /* PSA_CRYPTO_DRIVER_TEST */
  476. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  477. /* Fell through, meaning no accelerator supports this operation */
  478. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  479. status = mbedtls_psa_sign_hash_start( &operation->ctx.mbedtls_ctx,
  480. attributes,
  481. key_buffer, key_buffer_size,
  482. alg, hash, hash_length );
  483. break;
  484. /* Add cases for opaque driver here */
  485. default:
  486. /* Key is declared with a lifetime not known to us */
  487. status = PSA_ERROR_INVALID_ARGUMENT;
  488. break;
  489. }
  490. return( status );
  491. }
  492. static inline psa_status_t psa_driver_wrapper_sign_hash_complete(
  493. psa_sign_hash_interruptible_operation_t *operation,
  494. uint8_t *signature, size_t signature_size,
  495. size_t *signature_length )
  496. {
  497. switch( operation->id )
  498. {
  499. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  500. return( mbedtls_psa_sign_hash_complete( &operation->ctx.mbedtls_ctx,
  501. signature, signature_size,
  502. signature_length ) );
  503. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  504. #if defined(PSA_CRYPTO_DRIVER_TEST)
  505. /* Add test driver tests here */
  506. #endif /* PSA_CRYPTO_DRIVER_TEST */
  507. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  508. }
  509. ( void ) signature;
  510. ( void ) signature_size;
  511. ( void ) signature_length;
  512. return( PSA_ERROR_INVALID_ARGUMENT );
  513. }
  514. static inline psa_status_t psa_driver_wrapper_sign_hash_abort(
  515. psa_sign_hash_interruptible_operation_t *operation )
  516. {
  517. switch( operation->id )
  518. {
  519. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  520. return( mbedtls_psa_sign_hash_abort( &operation->ctx.mbedtls_ctx ) );
  521. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  522. #if defined(PSA_CRYPTO_DRIVER_TEST)
  523. /* Add test driver tests here */
  524. #endif /* PSA_CRYPTO_DRIVER_TEST */
  525. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  526. }
  527. return( PSA_ERROR_INVALID_ARGUMENT );
  528. }
  529. static inline psa_status_t psa_driver_wrapper_verify_hash_start(
  530. psa_verify_hash_interruptible_operation_t *operation,
  531. const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
  532. size_t key_buffer_size, psa_algorithm_t alg,
  533. const uint8_t *hash, size_t hash_length,
  534. const uint8_t *signature, size_t signature_length )
  535. {
  536. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  537. psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
  538. psa_get_key_lifetime(attributes) );
  539. switch( location )
  540. {
  541. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  542. /* Key is stored in the slot in export representation, so
  543. * cycle through all known transparent accelerators */
  544. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  545. #if defined(PSA_CRYPTO_DRIVER_TEST)
  546. /* Add test driver tests here */
  547. /* Declared with fallback == true */
  548. #endif /* PSA_CRYPTO_DRIVER_TEST */
  549. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  550. /* Fell through, meaning no accelerator supports this operation */
  551. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  552. status = mbedtls_psa_verify_hash_start( &operation->ctx.mbedtls_ctx,
  553. attributes,
  554. key_buffer, key_buffer_size,
  555. alg, hash, hash_length,
  556. signature, signature_length );
  557. break;
  558. /* Add cases for opaque driver here */
  559. default:
  560. /* Key is declared with a lifetime not known to us */
  561. status = PSA_ERROR_INVALID_ARGUMENT;
  562. break;
  563. }
  564. return( status );
  565. }
  566. static inline psa_status_t psa_driver_wrapper_verify_hash_complete(
  567. psa_verify_hash_interruptible_operation_t *operation )
  568. {
  569. switch( operation->id )
  570. {
  571. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  572. return( mbedtls_psa_verify_hash_complete(
  573. &operation->ctx.mbedtls_ctx
  574. ) );
  575. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  576. #if defined(PSA_CRYPTO_DRIVER_TEST)
  577. /* Add test driver tests here */
  578. #endif /* PSA_CRYPTO_DRIVER_TEST */
  579. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  580. }
  581. return( PSA_ERROR_INVALID_ARGUMENT );
  582. }
  583. static inline psa_status_t psa_driver_wrapper_verify_hash_abort(
  584. psa_verify_hash_interruptible_operation_t *operation )
  585. {
  586. switch( operation->id )
  587. {
  588. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  589. return( mbedtls_psa_verify_hash_abort( &operation->ctx.mbedtls_ctx
  590. ) );
  591. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  592. #if defined(PSA_CRYPTO_DRIVER_TEST)
  593. /* Add test driver tests here */
  594. #endif /* PSA_CRYPTO_DRIVER_TEST */
  595. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  596. }
  597. return( PSA_ERROR_INVALID_ARGUMENT );
  598. }
  599. /** Calculate the key buffer size required to store the key material of a key
  600. * associated with an opaque driver from input key data.
  601. *
  602. * \param[in] attributes The key attributes
  603. * \param[in] data The input key data.
  604. * \param[in] data_length The input data length.
  605. * \param[out] key_buffer_size Minimum buffer size to contain the key material.
  606. *
  607. * \retval #PSA_SUCCESS \emptydescription
  608. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  609. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  610. */
  611. static inline psa_status_t psa_driver_wrapper_get_key_buffer_size_from_key_data(
  612. const psa_key_attributes_t *attributes,
  613. const uint8_t *data,
  614. size_t data_length,
  615. size_t *key_buffer_size )
  616. {
  617. psa_key_location_t location =
  618. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  619. psa_key_type_t key_type = psa_get_key_type(attributes);
  620. *key_buffer_size = 0;
  621. switch( location )
  622. {
  623. #if defined(PSA_CRYPTO_DRIVER_TEST)
  624. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  625. *key_buffer_size = mbedtls_test_opaque_size_function( key_type,
  626. PSA_BYTES_TO_BITS( data_length ) );
  627. return( ( *key_buffer_size != 0 ) ?
  628. PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
  629. #endif /* PSA_CRYPTO_DRIVER_TEST */
  630. default:
  631. (void)key_type;
  632. (void)data;
  633. (void)data_length;
  634. return( PSA_ERROR_INVALID_ARGUMENT );
  635. }
  636. }
  637. static inline psa_status_t psa_driver_wrapper_generate_key(
  638. const psa_key_attributes_t *attributes,
  639. const psa_custom_key_parameters_t *custom,
  640. const uint8_t *custom_data, size_t custom_data_length,
  641. uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
  642. {
  643. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  644. psa_key_location_t location =
  645. PSA_KEY_LIFETIME_GET_LOCATION(psa_get_key_lifetime(attributes));
  646. #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
  647. int is_default_production =
  648. psa_custom_key_parameters_are_default(custom, custom_data_length);
  649. if( location != PSA_KEY_LOCATION_LOCAL_STORAGE && !is_default_production )
  650. {
  651. /* We don't support passing custom production parameters
  652. * to drivers yet. */
  653. return PSA_ERROR_NOT_SUPPORTED;
  654. }
  655. #else
  656. int is_default_production = 1;
  657. (void) is_default_production;
  658. #endif
  659. /* Try dynamically-registered SE interface first */
  660. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  661. const psa_drv_se_t *drv;
  662. psa_drv_se_context_t *drv_context;
  663. if( psa_get_se_driver( psa_get_key_lifetime(attributes), &drv, &drv_context ) )
  664. {
  665. size_t pubkey_length = 0; /* We don't support this feature yet */
  666. if( drv->key_management == NULL ||
  667. drv->key_management->p_generate == NULL )
  668. {
  669. /* Key is defined as being in SE, but we have no way to generate it */
  670. return( PSA_ERROR_NOT_SUPPORTED );
  671. }
  672. return( drv->key_management->p_generate(
  673. drv_context,
  674. *( (psa_key_slot_number_t *)key_buffer ),
  675. attributes, NULL, 0, &pubkey_length ) );
  676. }
  677. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  678. switch( location )
  679. {
  680. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  681. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  682. /* Transparent drivers are limited to generating asymmetric keys. */
  683. /* We don't support passing custom production parameters
  684. * to drivers yet. */
  685. if( PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type(attributes) ) &&
  686. is_default_production )
  687. {
  688. /* Cycle through all known transparent accelerators */
  689. #if defined(PSA_CRYPTO_DRIVER_TEST)
  690. status = mbedtls_test_transparent_generate_key(
  691. attributes, key_buffer, key_buffer_size,
  692. key_buffer_length );
  693. /* Declared with fallback == true */
  694. if( status != PSA_ERROR_NOT_SUPPORTED )
  695. break;
  696. #endif /* PSA_CRYPTO_DRIVER_TEST */
  697. #if defined(MBEDTLS_PSA_P256M_DRIVER_ENABLED)
  698. if( PSA_KEY_TYPE_IS_ECC( psa_get_key_type(attributes) ) &&
  699. psa_get_key_type(attributes) == PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) &&
  700. psa_get_key_bits(attributes) == 256 )
  701. {
  702. status = p256_transparent_generate_key( attributes,
  703. key_buffer,
  704. key_buffer_size,
  705. key_buffer_length );
  706. if( status != PSA_ERROR_NOT_SUPPORTED )
  707. break;
  708. }
  709. #endif /* MBEDTLS_PSA_P256M_DRIVER_ENABLED */
  710. }
  711. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  712. /* Software fallback */
  713. status = psa_generate_key_internal(
  714. attributes, custom, custom_data, custom_data_length,
  715. key_buffer, key_buffer_size, key_buffer_length );
  716. break;
  717. /* Add cases for opaque driver here */
  718. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  719. #if defined(PSA_CRYPTO_DRIVER_TEST)
  720. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  721. status = mbedtls_test_opaque_generate_key(
  722. attributes, key_buffer, key_buffer_size, key_buffer_length );
  723. break;
  724. #endif /* PSA_CRYPTO_DRIVER_TEST */
  725. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  726. default:
  727. /* Key is declared with a lifetime not known to us */
  728. status = PSA_ERROR_INVALID_ARGUMENT;
  729. break;
  730. }
  731. return( status );
  732. }
  733. static inline psa_status_t psa_driver_wrapper_import_key(
  734. const psa_key_attributes_t *attributes,
  735. const uint8_t *data,
  736. size_t data_length,
  737. uint8_t *key_buffer,
  738. size_t key_buffer_size,
  739. size_t *key_buffer_length,
  740. size_t *bits )
  741. {
  742. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  743. psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
  744. psa_get_key_lifetime( attributes ) );
  745. /* Try dynamically-registered SE interface first */
  746. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  747. const psa_drv_se_t *drv;
  748. psa_drv_se_context_t *drv_context;
  749. if( psa_get_se_driver( psa_get_key_lifetime(attributes), &drv, &drv_context ) )
  750. {
  751. if( drv->key_management == NULL ||
  752. drv->key_management->p_import == NULL )
  753. return( PSA_ERROR_NOT_SUPPORTED );
  754. /* The driver should set the number of key bits, however in
  755. * case it doesn't, we initialize bits to an invalid value. */
  756. *bits = PSA_MAX_KEY_BITS + 1;
  757. status = drv->key_management->p_import(
  758. drv_context,
  759. *( (psa_key_slot_number_t *)key_buffer ),
  760. attributes, data, data_length, bits );
  761. if( status != PSA_SUCCESS )
  762. return( status );
  763. if( (*bits) > PSA_MAX_KEY_BITS )
  764. return( PSA_ERROR_NOT_SUPPORTED );
  765. return( PSA_SUCCESS );
  766. }
  767. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  768. switch( location )
  769. {
  770. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  771. /* Key is stored in the slot in export representation, so
  772. * cycle through all known transparent accelerators */
  773. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  774. #if (defined(PSA_CRYPTO_DRIVER_TEST) )
  775. status = mbedtls_test_transparent_import_key
  776. (attributes,
  777. data,
  778. data_length,
  779. key_buffer,
  780. key_buffer_size,
  781. key_buffer_length,
  782. bits
  783. );
  784. if( status != PSA_ERROR_NOT_SUPPORTED )
  785. return( status );
  786. #endif
  787. #if (defined(MBEDTLS_PSA_P256M_DRIVER_ENABLED) )
  788. status = p256_transparent_import_key
  789. (attributes,
  790. data,
  791. data_length,
  792. key_buffer,
  793. key_buffer_size,
  794. key_buffer_length,
  795. bits
  796. );
  797. if( status != PSA_ERROR_NOT_SUPPORTED )
  798. return( status );
  799. #endif
  800. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  801. /* Fell through, meaning no accelerator supports this operation */
  802. return( psa_import_key_into_slot( attributes,
  803. data, data_length,
  804. key_buffer, key_buffer_size,
  805. key_buffer_length, bits ) );
  806. /* Add cases for opaque driver here */
  807. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  808. #if (defined(PSA_CRYPTO_DRIVER_TEST) )
  809. case 0x7fffff:
  810. return( mbedtls_test_opaque_import_key
  811. (attributes,
  812. data,
  813. data_length,
  814. key_buffer,
  815. key_buffer_size,
  816. key_buffer_length,
  817. bits
  818. ));
  819. #endif
  820. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  821. default:
  822. (void)status;
  823. return( PSA_ERROR_INVALID_ARGUMENT );
  824. }
  825. }
  826. static inline psa_status_t psa_driver_wrapper_export_key(
  827. const psa_key_attributes_t *attributes,
  828. const uint8_t *key_buffer, size_t key_buffer_size,
  829. uint8_t *data, size_t data_size, size_t *data_length )
  830. {
  831. psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
  832. psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
  833. psa_get_key_lifetime( attributes ) );
  834. /* Try dynamically-registered SE interface first */
  835. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  836. const psa_drv_se_t *drv;
  837. psa_drv_se_context_t *drv_context;
  838. if( psa_get_se_driver( psa_get_key_lifetime(attributes), &drv, &drv_context ) )
  839. {
  840. if( ( drv->key_management == NULL ) ||
  841. ( drv->key_management->p_export == NULL ) )
  842. {
  843. return( PSA_ERROR_NOT_SUPPORTED );
  844. }
  845. return( drv->key_management->p_export(
  846. drv_context,
  847. *( (psa_key_slot_number_t *)key_buffer ),
  848. data, data_size, data_length ) );
  849. }
  850. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  851. switch( location )
  852. {
  853. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  854. return( psa_export_key_internal( attributes,
  855. key_buffer,
  856. key_buffer_size,
  857. data,
  858. data_size,
  859. data_length ) );
  860. /* Add cases for opaque driver here */
  861. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  862. #if (defined(PSA_CRYPTO_DRIVER_TEST) )
  863. case 0x7fffff:
  864. return( mbedtls_test_opaque_export_key
  865. (attributes,
  866. key_buffer,
  867. key_buffer_size,
  868. data,
  869. data_size,
  870. data_length
  871. ));
  872. #endif
  873. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  874. default:
  875. /* Key is declared with a lifetime not known to us */
  876. return( status );
  877. }
  878. }
  879. static inline psa_status_t psa_driver_wrapper_copy_key(
  880. psa_key_attributes_t *attributes,
  881. const uint8_t *source_key, size_t source_key_length,
  882. uint8_t *target_key_buffer, size_t target_key_buffer_size,
  883. size_t *target_key_buffer_length )
  884. {
  885. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  886. psa_key_location_t location =
  887. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  888. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  889. const psa_drv_se_t *drv;
  890. psa_drv_se_context_t *drv_context;
  891. if( psa_get_se_driver( psa_get_key_lifetime(attributes), &drv, &drv_context ) )
  892. {
  893. /* Copying to a secure element is not implemented yet. */
  894. return( PSA_ERROR_NOT_SUPPORTED );
  895. }
  896. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  897. switch( location )
  898. {
  899. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  900. #if (defined(PSA_CRYPTO_DRIVER_TEST) )
  901. case 0x7fffff:
  902. return( mbedtls_test_opaque_copy_key
  903. (attributes,
  904. source_key,
  905. source_key_length,
  906. target_key_buffer,
  907. target_key_buffer_size,
  908. target_key_buffer_length
  909. ));
  910. #endif
  911. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  912. default:
  913. (void)source_key;
  914. (void)source_key_length;
  915. (void)target_key_buffer;
  916. (void)target_key_buffer_size;
  917. (void)target_key_buffer_length;
  918. status = PSA_ERROR_INVALID_ARGUMENT;
  919. }
  920. return( status );
  921. }
  922. /*
  923. * Cipher functions
  924. */
  925. static inline psa_status_t psa_driver_wrapper_cipher_encrypt(
  926. const psa_key_attributes_t *attributes,
  927. const uint8_t *key_buffer,
  928. size_t key_buffer_size,
  929. psa_algorithm_t alg,
  930. const uint8_t *iv,
  931. size_t iv_length,
  932. const uint8_t *input,
  933. size_t input_length,
  934. uint8_t *output,
  935. size_t output_size,
  936. size_t *output_length )
  937. {
  938. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  939. psa_key_location_t location =
  940. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  941. switch( location )
  942. {
  943. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  944. /* Key is stored in the slot in export representation, so
  945. * cycle through all known transparent accelerators */
  946. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  947. #if defined(PSA_CRYPTO_DRIVER_TEST)
  948. status = mbedtls_test_transparent_cipher_encrypt( attributes,
  949. key_buffer,
  950. key_buffer_size,
  951. alg,
  952. iv,
  953. iv_length,
  954. input,
  955. input_length,
  956. output,
  957. output_size,
  958. output_length );
  959. /* Declared with fallback == true */
  960. if( status != PSA_ERROR_NOT_SUPPORTED )
  961. return( status );
  962. #endif /* PSA_CRYPTO_DRIVER_TEST */
  963. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  964. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  965. return( mbedtls_psa_cipher_encrypt( attributes,
  966. key_buffer,
  967. key_buffer_size,
  968. alg,
  969. iv,
  970. iv_length,
  971. input,
  972. input_length,
  973. output,
  974. output_size,
  975. output_length ) );
  976. #else
  977. return( PSA_ERROR_NOT_SUPPORTED );
  978. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  979. /* Add cases for opaque driver here */
  980. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  981. #if defined(PSA_CRYPTO_DRIVER_TEST)
  982. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  983. return( mbedtls_test_opaque_cipher_encrypt( attributes,
  984. key_buffer,
  985. key_buffer_size,
  986. alg,
  987. iv,
  988. iv_length,
  989. input,
  990. input_length,
  991. output,
  992. output_size,
  993. output_length ) );
  994. #endif /* PSA_CRYPTO_DRIVER_TEST */
  995. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  996. default:
  997. /* Key is declared with a lifetime not known to us */
  998. (void)status;
  999. (void)key_buffer;
  1000. (void)key_buffer_size;
  1001. (void)alg;
  1002. (void)iv;
  1003. (void)iv_length;
  1004. (void)input;
  1005. (void)input_length;
  1006. (void)output;
  1007. (void)output_size;
  1008. (void)output_length;
  1009. return( PSA_ERROR_INVALID_ARGUMENT );
  1010. }
  1011. }
  1012. static inline psa_status_t psa_driver_wrapper_cipher_decrypt(
  1013. const psa_key_attributes_t *attributes,
  1014. const uint8_t *key_buffer,
  1015. size_t key_buffer_size,
  1016. psa_algorithm_t alg,
  1017. const uint8_t *input,
  1018. size_t input_length,
  1019. uint8_t *output,
  1020. size_t output_size,
  1021. size_t *output_length )
  1022. {
  1023. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1024. psa_key_location_t location =
  1025. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  1026. switch( location )
  1027. {
  1028. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1029. /* Key is stored in the slot in export representation, so
  1030. * cycle through all known transparent accelerators */
  1031. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1032. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1033. status = mbedtls_test_transparent_cipher_decrypt( attributes,
  1034. key_buffer,
  1035. key_buffer_size,
  1036. alg,
  1037. input,
  1038. input_length,
  1039. output,
  1040. output_size,
  1041. output_length );
  1042. /* Declared with fallback == true */
  1043. if( status != PSA_ERROR_NOT_SUPPORTED )
  1044. return( status );
  1045. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1046. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1047. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  1048. return( mbedtls_psa_cipher_decrypt( attributes,
  1049. key_buffer,
  1050. key_buffer_size,
  1051. alg,
  1052. input,
  1053. input_length,
  1054. output,
  1055. output_size,
  1056. output_length ) );
  1057. #else
  1058. return( PSA_ERROR_NOT_SUPPORTED );
  1059. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1060. /* Add cases for opaque driver here */
  1061. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1062. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1063. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  1064. return( mbedtls_test_opaque_cipher_decrypt( attributes,
  1065. key_buffer,
  1066. key_buffer_size,
  1067. alg,
  1068. input,
  1069. input_length,
  1070. output,
  1071. output_size,
  1072. output_length ) );
  1073. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1074. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1075. default:
  1076. /* Key is declared with a lifetime not known to us */
  1077. (void)status;
  1078. (void)key_buffer;
  1079. (void)key_buffer_size;
  1080. (void)alg;
  1081. (void)input;
  1082. (void)input_length;
  1083. (void)output;
  1084. (void)output_size;
  1085. (void)output_length;
  1086. return( PSA_ERROR_INVALID_ARGUMENT );
  1087. }
  1088. }
  1089. static inline psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
  1090. psa_cipher_operation_t *operation,
  1091. const psa_key_attributes_t *attributes,
  1092. const uint8_t *key_buffer, size_t key_buffer_size,
  1093. psa_algorithm_t alg )
  1094. {
  1095. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1096. psa_key_location_t location =
  1097. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  1098. switch( location )
  1099. {
  1100. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1101. /* Key is stored in the slot in export representation, so
  1102. * cycle through all known transparent accelerators */
  1103. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1104. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1105. status = mbedtls_test_transparent_cipher_encrypt_setup(
  1106. &operation->ctx.transparent_test_driver_ctx,
  1107. attributes,
  1108. key_buffer,
  1109. key_buffer_size,
  1110. alg );
  1111. /* Declared with fallback == true */
  1112. if( status == PSA_SUCCESS )
  1113. operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
  1114. if( status != PSA_ERROR_NOT_SUPPORTED )
  1115. return( status );
  1116. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1117. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1118. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  1119. /* Fell through, meaning no accelerator supports this operation */
  1120. status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
  1121. attributes,
  1122. key_buffer,
  1123. key_buffer_size,
  1124. alg );
  1125. if( status == PSA_SUCCESS )
  1126. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1127. if( status != PSA_ERROR_NOT_SUPPORTED )
  1128. return( status );
  1129. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1130. return( PSA_ERROR_NOT_SUPPORTED );
  1131. /* Add cases for opaque driver here */
  1132. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1133. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1134. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  1135. status = mbedtls_test_opaque_cipher_encrypt_setup(
  1136. &operation->ctx.opaque_test_driver_ctx,
  1137. attributes,
  1138. key_buffer, key_buffer_size,
  1139. alg );
  1140. if( status == PSA_SUCCESS )
  1141. operation->id = MBEDTLS_TEST_OPAQUE_DRIVER_ID;
  1142. return( status );
  1143. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1144. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1145. default:
  1146. /* Key is declared with a lifetime not known to us */
  1147. (void)status;
  1148. (void)operation;
  1149. (void)key_buffer;
  1150. (void)key_buffer_size;
  1151. (void)alg;
  1152. return( PSA_ERROR_INVALID_ARGUMENT );
  1153. }
  1154. }
  1155. static inline psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
  1156. psa_cipher_operation_t *operation,
  1157. const psa_key_attributes_t *attributes,
  1158. const uint8_t *key_buffer, size_t key_buffer_size,
  1159. psa_algorithm_t alg )
  1160. {
  1161. psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
  1162. psa_key_location_t location =
  1163. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  1164. switch( location )
  1165. {
  1166. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1167. /* Key is stored in the slot in export representation, so
  1168. * cycle through all known transparent accelerators */
  1169. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1170. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1171. status = mbedtls_test_transparent_cipher_decrypt_setup(
  1172. &operation->ctx.transparent_test_driver_ctx,
  1173. attributes,
  1174. key_buffer,
  1175. key_buffer_size,
  1176. alg );
  1177. /* Declared with fallback == true */
  1178. if( status == PSA_SUCCESS )
  1179. operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
  1180. if( status != PSA_ERROR_NOT_SUPPORTED )
  1181. return( status );
  1182. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1183. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1184. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  1185. /* Fell through, meaning no accelerator supports this operation */
  1186. status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
  1187. attributes,
  1188. key_buffer,
  1189. key_buffer_size,
  1190. alg );
  1191. if( status == PSA_SUCCESS )
  1192. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1193. return( status );
  1194. #else /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1195. return( PSA_ERROR_NOT_SUPPORTED );
  1196. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1197. /* Add cases for opaque driver here */
  1198. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1199. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1200. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  1201. status = mbedtls_test_opaque_cipher_decrypt_setup(
  1202. &operation->ctx.opaque_test_driver_ctx,
  1203. attributes,
  1204. key_buffer, key_buffer_size,
  1205. alg );
  1206. if( status == PSA_SUCCESS )
  1207. operation->id = MBEDTLS_TEST_OPAQUE_DRIVER_ID;
  1208. return( status );
  1209. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1210. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1211. default:
  1212. /* Key is declared with a lifetime not known to us */
  1213. (void)status;
  1214. (void)operation;
  1215. (void)key_buffer;
  1216. (void)key_buffer_size;
  1217. (void)alg;
  1218. return( PSA_ERROR_INVALID_ARGUMENT );
  1219. }
  1220. }
  1221. static inline psa_status_t psa_driver_wrapper_cipher_set_iv(
  1222. psa_cipher_operation_t *operation,
  1223. const uint8_t *iv,
  1224. size_t iv_length )
  1225. {
  1226. switch( operation->id )
  1227. {
  1228. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  1229. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1230. return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
  1231. iv,
  1232. iv_length ) );
  1233. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1234. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1235. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1236. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  1237. return( mbedtls_test_transparent_cipher_set_iv(
  1238. &operation->ctx.transparent_test_driver_ctx,
  1239. iv, iv_length ) );
  1240. case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
  1241. return( mbedtls_test_opaque_cipher_set_iv(
  1242. &operation->ctx.opaque_test_driver_ctx,
  1243. iv, iv_length ) );
  1244. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1245. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1246. }
  1247. (void)iv;
  1248. (void)iv_length;
  1249. return( PSA_ERROR_INVALID_ARGUMENT );
  1250. }
  1251. static inline psa_status_t psa_driver_wrapper_cipher_update(
  1252. psa_cipher_operation_t *operation,
  1253. const uint8_t *input,
  1254. size_t input_length,
  1255. uint8_t *output,
  1256. size_t output_size,
  1257. size_t *output_length )
  1258. {
  1259. switch( operation->id )
  1260. {
  1261. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  1262. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1263. return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
  1264. input,
  1265. input_length,
  1266. output,
  1267. output_size,
  1268. output_length ) );
  1269. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1270. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1271. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1272. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  1273. return( mbedtls_test_transparent_cipher_update(
  1274. &operation->ctx.transparent_test_driver_ctx,
  1275. input, input_length,
  1276. output, output_size, output_length ) );
  1277. case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
  1278. return( mbedtls_test_opaque_cipher_update(
  1279. &operation->ctx.opaque_test_driver_ctx,
  1280. input, input_length,
  1281. output, output_size, output_length ) );
  1282. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1283. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1284. }
  1285. (void)input;
  1286. (void)input_length;
  1287. (void)output;
  1288. (void)output_size;
  1289. (void)output_length;
  1290. return( PSA_ERROR_INVALID_ARGUMENT );
  1291. }
  1292. static inline psa_status_t psa_driver_wrapper_cipher_finish(
  1293. psa_cipher_operation_t *operation,
  1294. uint8_t *output,
  1295. size_t output_size,
  1296. size_t *output_length )
  1297. {
  1298. switch( operation->id )
  1299. {
  1300. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  1301. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1302. return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
  1303. output,
  1304. output_size,
  1305. output_length ) );
  1306. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1307. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1308. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1309. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  1310. return( mbedtls_test_transparent_cipher_finish(
  1311. &operation->ctx.transparent_test_driver_ctx,
  1312. output, output_size, output_length ) );
  1313. case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
  1314. return( mbedtls_test_opaque_cipher_finish(
  1315. &operation->ctx.opaque_test_driver_ctx,
  1316. output, output_size, output_length ) );
  1317. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1318. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1319. }
  1320. (void)output;
  1321. (void)output_size;
  1322. (void)output_length;
  1323. return( PSA_ERROR_INVALID_ARGUMENT );
  1324. }
  1325. static inline psa_status_t psa_driver_wrapper_cipher_abort(
  1326. psa_cipher_operation_t *operation )
  1327. {
  1328. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1329. switch( operation->id )
  1330. {
  1331. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  1332. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1333. return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
  1334. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1335. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1336. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1337. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  1338. status = mbedtls_test_transparent_cipher_abort(
  1339. &operation->ctx.transparent_test_driver_ctx );
  1340. mbedtls_platform_zeroize(
  1341. &operation->ctx.transparent_test_driver_ctx,
  1342. sizeof( operation->ctx.transparent_test_driver_ctx ) );
  1343. return( status );
  1344. case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
  1345. status = mbedtls_test_opaque_cipher_abort(
  1346. &operation->ctx.opaque_test_driver_ctx );
  1347. mbedtls_platform_zeroize(
  1348. &operation->ctx.opaque_test_driver_ctx,
  1349. sizeof( operation->ctx.opaque_test_driver_ctx ) );
  1350. return( status );
  1351. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1352. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1353. }
  1354. (void)status;
  1355. return( PSA_ERROR_INVALID_ARGUMENT );
  1356. }
  1357. /*
  1358. * Hashing functions
  1359. */
  1360. static inline psa_status_t psa_driver_wrapper_hash_compute(
  1361. psa_algorithm_t alg,
  1362. const uint8_t *input,
  1363. size_t input_length,
  1364. uint8_t *hash,
  1365. size_t hash_size,
  1366. size_t *hash_length)
  1367. {
  1368. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1369. /* Try accelerators first */
  1370. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1371. status = mbedtls_test_transparent_hash_compute(
  1372. alg, input, input_length, hash, hash_size, hash_length );
  1373. if( status != PSA_ERROR_NOT_SUPPORTED )
  1374. return( status );
  1375. #endif
  1376. /* If software fallback is compiled in, try fallback */
  1377. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1378. status = mbedtls_psa_hash_compute( alg, input, input_length,
  1379. hash, hash_size, hash_length );
  1380. if( status != PSA_ERROR_NOT_SUPPORTED )
  1381. return( status );
  1382. #endif
  1383. (void) status;
  1384. (void) alg;
  1385. (void) input;
  1386. (void) input_length;
  1387. (void) hash;
  1388. (void) hash_size;
  1389. (void) hash_length;
  1390. return( PSA_ERROR_NOT_SUPPORTED );
  1391. }
  1392. static inline psa_status_t psa_driver_wrapper_hash_setup(
  1393. psa_hash_operation_t *operation,
  1394. psa_algorithm_t alg )
  1395. {
  1396. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1397. /* Try setup on accelerators first */
  1398. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1399. status = mbedtls_test_transparent_hash_setup(
  1400. &operation->ctx.test_driver_ctx, alg );
  1401. if( status == PSA_SUCCESS )
  1402. operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
  1403. if( status != PSA_ERROR_NOT_SUPPORTED )
  1404. return( status );
  1405. #endif
  1406. /* If software fallback is compiled in, try fallback */
  1407. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1408. status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
  1409. if( status == PSA_SUCCESS )
  1410. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1411. if( status != PSA_ERROR_NOT_SUPPORTED )
  1412. return( status );
  1413. #endif
  1414. /* Nothing left to try if we fall through here */
  1415. (void) status;
  1416. (void) operation;
  1417. (void) alg;
  1418. return( PSA_ERROR_NOT_SUPPORTED );
  1419. }
  1420. static inline psa_status_t psa_driver_wrapper_hash_clone(
  1421. const psa_hash_operation_t *source_operation,
  1422. psa_hash_operation_t *target_operation )
  1423. {
  1424. switch( source_operation->id )
  1425. {
  1426. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1427. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1428. target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1429. return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
  1430. &target_operation->ctx.mbedtls_ctx ) );
  1431. #endif
  1432. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1433. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  1434. target_operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
  1435. return( mbedtls_test_transparent_hash_clone(
  1436. &source_operation->ctx.test_driver_ctx,
  1437. &target_operation->ctx.test_driver_ctx ) );
  1438. #endif
  1439. default:
  1440. (void) target_operation;
  1441. return( PSA_ERROR_BAD_STATE );
  1442. }
  1443. }
  1444. static inline psa_status_t psa_driver_wrapper_hash_update(
  1445. psa_hash_operation_t *operation,
  1446. const uint8_t *input,
  1447. size_t input_length )
  1448. {
  1449. switch( operation->id )
  1450. {
  1451. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1452. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1453. return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
  1454. input, input_length ) );
  1455. #endif
  1456. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1457. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  1458. return( mbedtls_test_transparent_hash_update(
  1459. &operation->ctx.test_driver_ctx,
  1460. input, input_length ) );
  1461. #endif
  1462. default:
  1463. (void) input;
  1464. (void) input_length;
  1465. return( PSA_ERROR_BAD_STATE );
  1466. }
  1467. }
  1468. static inline psa_status_t psa_driver_wrapper_hash_finish(
  1469. psa_hash_operation_t *operation,
  1470. uint8_t *hash,
  1471. size_t hash_size,
  1472. size_t *hash_length )
  1473. {
  1474. switch( operation->id )
  1475. {
  1476. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1477. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1478. return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
  1479. hash, hash_size, hash_length ) );
  1480. #endif
  1481. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1482. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  1483. return( mbedtls_test_transparent_hash_finish(
  1484. &operation->ctx.test_driver_ctx,
  1485. hash, hash_size, hash_length ) );
  1486. #endif
  1487. default:
  1488. (void) hash;
  1489. (void) hash_size;
  1490. (void) hash_length;
  1491. return( PSA_ERROR_BAD_STATE );
  1492. }
  1493. }
  1494. static inline psa_status_t psa_driver_wrapper_hash_abort(
  1495. psa_hash_operation_t *operation )
  1496. {
  1497. switch( operation->id )
  1498. {
  1499. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1500. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1501. return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
  1502. #endif
  1503. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1504. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  1505. return( mbedtls_test_transparent_hash_abort(
  1506. &operation->ctx.test_driver_ctx ) );
  1507. #endif
  1508. default:
  1509. return( PSA_ERROR_BAD_STATE );
  1510. }
  1511. }
  1512. static inline psa_status_t psa_driver_wrapper_aead_encrypt(
  1513. const psa_key_attributes_t *attributes,
  1514. const uint8_t *key_buffer, size_t key_buffer_size,
  1515. psa_algorithm_t alg,
  1516. const uint8_t *nonce, size_t nonce_length,
  1517. const uint8_t *additional_data, size_t additional_data_length,
  1518. const uint8_t *plaintext, size_t plaintext_length,
  1519. uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
  1520. {
  1521. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1522. psa_key_location_t location =
  1523. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  1524. switch( location )
  1525. {
  1526. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1527. /* Key is stored in the slot in export representation, so
  1528. * cycle through all known transparent accelerators */
  1529. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1530. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1531. status = mbedtls_test_transparent_aead_encrypt(
  1532. attributes, key_buffer, key_buffer_size,
  1533. alg,
  1534. nonce, nonce_length,
  1535. additional_data, additional_data_length,
  1536. plaintext, plaintext_length,
  1537. ciphertext, ciphertext_size, ciphertext_length );
  1538. /* Declared with fallback == true */
  1539. if( status != PSA_ERROR_NOT_SUPPORTED )
  1540. return( status );
  1541. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1542. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1543. /* Fell through, meaning no accelerator supports this operation */
  1544. return( mbedtls_psa_aead_encrypt(
  1545. attributes, key_buffer, key_buffer_size,
  1546. alg,
  1547. nonce, nonce_length,
  1548. additional_data, additional_data_length,
  1549. plaintext, plaintext_length,
  1550. ciphertext, ciphertext_size, ciphertext_length ) );
  1551. /* Add cases for opaque driver here */
  1552. default:
  1553. /* Key is declared with a lifetime not known to us */
  1554. (void)status;
  1555. return( PSA_ERROR_INVALID_ARGUMENT );
  1556. }
  1557. }
  1558. static inline psa_status_t psa_driver_wrapper_aead_decrypt(
  1559. const psa_key_attributes_t *attributes,
  1560. const uint8_t *key_buffer, size_t key_buffer_size,
  1561. psa_algorithm_t alg,
  1562. const uint8_t *nonce, size_t nonce_length,
  1563. const uint8_t *additional_data, size_t additional_data_length,
  1564. const uint8_t *ciphertext, size_t ciphertext_length,
  1565. uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
  1566. {
  1567. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1568. psa_key_location_t location =
  1569. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  1570. switch( location )
  1571. {
  1572. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1573. /* Key is stored in the slot in export representation, so
  1574. * cycle through all known transparent accelerators */
  1575. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1576. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1577. status = mbedtls_test_transparent_aead_decrypt(
  1578. attributes, key_buffer, key_buffer_size,
  1579. alg,
  1580. nonce, nonce_length,
  1581. additional_data, additional_data_length,
  1582. ciphertext, ciphertext_length,
  1583. plaintext, plaintext_size, plaintext_length );
  1584. /* Declared with fallback == true */
  1585. if( status != PSA_ERROR_NOT_SUPPORTED )
  1586. return( status );
  1587. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1588. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1589. /* Fell through, meaning no accelerator supports this operation */
  1590. return( mbedtls_psa_aead_decrypt(
  1591. attributes, key_buffer, key_buffer_size,
  1592. alg,
  1593. nonce, nonce_length,
  1594. additional_data, additional_data_length,
  1595. ciphertext, ciphertext_length,
  1596. plaintext, plaintext_size, plaintext_length ) );
  1597. /* Add cases for opaque driver here */
  1598. default:
  1599. /* Key is declared with a lifetime not known to us */
  1600. (void)status;
  1601. return( PSA_ERROR_INVALID_ARGUMENT );
  1602. }
  1603. }
  1604. static inline psa_status_t psa_driver_wrapper_aead_encrypt_setup(
  1605. psa_aead_operation_t *operation,
  1606. const psa_key_attributes_t *attributes,
  1607. const uint8_t *key_buffer, size_t key_buffer_size,
  1608. psa_algorithm_t alg )
  1609. {
  1610. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1611. psa_key_location_t location =
  1612. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  1613. switch( location )
  1614. {
  1615. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1616. /* Key is stored in the slot in export representation, so
  1617. * cycle through all known transparent accelerators */
  1618. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1619. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1620. operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
  1621. status = mbedtls_test_transparent_aead_encrypt_setup(
  1622. &operation->ctx.transparent_test_driver_ctx,
  1623. attributes, key_buffer, key_buffer_size,
  1624. alg );
  1625. /* Declared with fallback == true */
  1626. if( status != PSA_ERROR_NOT_SUPPORTED )
  1627. return( status );
  1628. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1629. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1630. /* Fell through, meaning no accelerator supports this operation */
  1631. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1632. status = mbedtls_psa_aead_encrypt_setup(
  1633. &operation->ctx.mbedtls_ctx, attributes,
  1634. key_buffer, key_buffer_size,
  1635. alg );
  1636. return( status );
  1637. /* Add cases for opaque driver here */
  1638. default:
  1639. /* Key is declared with a lifetime not known to us */
  1640. (void)status;
  1641. return( PSA_ERROR_INVALID_ARGUMENT );
  1642. }
  1643. }
  1644. static inline psa_status_t psa_driver_wrapper_aead_decrypt_setup(
  1645. psa_aead_operation_t *operation,
  1646. const psa_key_attributes_t *attributes,
  1647. const uint8_t *key_buffer, size_t key_buffer_size,
  1648. psa_algorithm_t alg )
  1649. {
  1650. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1651. psa_key_location_t location =
  1652. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  1653. switch( location )
  1654. {
  1655. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1656. /* Key is stored in the slot in export representation, so
  1657. * cycle through all known transparent accelerators */
  1658. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1659. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1660. operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
  1661. status = mbedtls_test_transparent_aead_decrypt_setup(
  1662. &operation->ctx.transparent_test_driver_ctx,
  1663. attributes,
  1664. key_buffer, key_buffer_size,
  1665. alg );
  1666. /* Declared with fallback == true */
  1667. if( status != PSA_ERROR_NOT_SUPPORTED )
  1668. return( status );
  1669. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1670. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1671. /* Fell through, meaning no accelerator supports this operation */
  1672. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1673. status = mbedtls_psa_aead_decrypt_setup(
  1674. &operation->ctx.mbedtls_ctx,
  1675. attributes,
  1676. key_buffer, key_buffer_size,
  1677. alg );
  1678. return( status );
  1679. /* Add cases for opaque driver here */
  1680. default:
  1681. /* Key is declared with a lifetime not known to us */
  1682. (void)status;
  1683. return( PSA_ERROR_INVALID_ARGUMENT );
  1684. }
  1685. }
  1686. static inline psa_status_t psa_driver_wrapper_aead_set_nonce(
  1687. psa_aead_operation_t *operation,
  1688. const uint8_t *nonce,
  1689. size_t nonce_length )
  1690. {
  1691. switch( operation->id )
  1692. {
  1693. #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
  1694. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1695. return( mbedtls_psa_aead_set_nonce( &operation->ctx.mbedtls_ctx,
  1696. nonce,
  1697. nonce_length ) );
  1698. #endif /* MBEDTLS_PSA_BUILTIN_AEAD */
  1699. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1700. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1701. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  1702. return( mbedtls_test_transparent_aead_set_nonce(
  1703. &operation->ctx.transparent_test_driver_ctx,
  1704. nonce, nonce_length ) );
  1705. /* Add cases for opaque driver here */
  1706. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1707. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1708. }
  1709. (void)nonce;
  1710. (void)nonce_length;
  1711. return( PSA_ERROR_INVALID_ARGUMENT );
  1712. }
  1713. static inline psa_status_t psa_driver_wrapper_aead_set_lengths(
  1714. psa_aead_operation_t *operation,
  1715. size_t ad_length,
  1716. size_t plaintext_length )
  1717. {
  1718. switch( operation->id )
  1719. {
  1720. #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
  1721. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1722. return( mbedtls_psa_aead_set_lengths( &operation->ctx.mbedtls_ctx,
  1723. ad_length,
  1724. plaintext_length ) );
  1725. #endif /* MBEDTLS_PSA_BUILTIN_AEAD */
  1726. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1727. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1728. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  1729. return( mbedtls_test_transparent_aead_set_lengths(
  1730. &operation->ctx.transparent_test_driver_ctx,
  1731. ad_length, plaintext_length ) );
  1732. /* Add cases for opaque driver here */
  1733. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1734. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1735. }
  1736. (void)ad_length;
  1737. (void)plaintext_length;
  1738. return( PSA_ERROR_INVALID_ARGUMENT );
  1739. }
  1740. static inline psa_status_t psa_driver_wrapper_aead_update_ad(
  1741. psa_aead_operation_t *operation,
  1742. const uint8_t *input,
  1743. size_t input_length )
  1744. {
  1745. switch( operation->id )
  1746. {
  1747. #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
  1748. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1749. return( mbedtls_psa_aead_update_ad( &operation->ctx.mbedtls_ctx,
  1750. input,
  1751. input_length ) );
  1752. #endif /* MBEDTLS_PSA_BUILTIN_AEAD */
  1753. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1754. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1755. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  1756. return( mbedtls_test_transparent_aead_update_ad(
  1757. &operation->ctx.transparent_test_driver_ctx,
  1758. input, input_length ) );
  1759. /* Add cases for opaque driver here */
  1760. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1761. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1762. }
  1763. (void)input;
  1764. (void)input_length;
  1765. return( PSA_ERROR_INVALID_ARGUMENT );
  1766. }
  1767. static inline psa_status_t psa_driver_wrapper_aead_update(
  1768. psa_aead_operation_t *operation,
  1769. const uint8_t *input,
  1770. size_t input_length,
  1771. uint8_t *output,
  1772. size_t output_size,
  1773. size_t *output_length )
  1774. {
  1775. switch( operation->id )
  1776. {
  1777. #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
  1778. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1779. return( mbedtls_psa_aead_update( &operation->ctx.mbedtls_ctx,
  1780. input, input_length,
  1781. output, output_size,
  1782. output_length ) );
  1783. #endif /* MBEDTLS_PSA_BUILTIN_AEAD */
  1784. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1785. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1786. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  1787. return( mbedtls_test_transparent_aead_update(
  1788. &operation->ctx.transparent_test_driver_ctx,
  1789. input, input_length, output, output_size,
  1790. output_length ) );
  1791. /* Add cases for opaque driver here */
  1792. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1793. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1794. }
  1795. (void)input;
  1796. (void)input_length;
  1797. (void)output;
  1798. (void)output_size;
  1799. (void)output_length;
  1800. return( PSA_ERROR_INVALID_ARGUMENT );
  1801. }
  1802. static inline psa_status_t psa_driver_wrapper_aead_finish(
  1803. psa_aead_operation_t *operation,
  1804. uint8_t *ciphertext,
  1805. size_t ciphertext_size,
  1806. size_t *ciphertext_length,
  1807. uint8_t *tag,
  1808. size_t tag_size,
  1809. size_t *tag_length )
  1810. {
  1811. switch( operation->id )
  1812. {
  1813. #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
  1814. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1815. return( mbedtls_psa_aead_finish( &operation->ctx.mbedtls_ctx,
  1816. ciphertext,
  1817. ciphertext_size,
  1818. ciphertext_length, tag,
  1819. tag_size, tag_length ) );
  1820. #endif /* MBEDTLS_PSA_BUILTIN_AEAD */
  1821. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1822. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1823. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  1824. return( mbedtls_test_transparent_aead_finish(
  1825. &operation->ctx.transparent_test_driver_ctx,
  1826. ciphertext, ciphertext_size,
  1827. ciphertext_length, tag, tag_size, tag_length ) );
  1828. /* Add cases for opaque driver here */
  1829. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1830. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1831. }
  1832. (void)ciphertext;
  1833. (void)ciphertext_size;
  1834. (void)ciphertext_length;
  1835. (void)tag;
  1836. (void)tag_size;
  1837. (void)tag_length;
  1838. return( PSA_ERROR_INVALID_ARGUMENT );
  1839. }
  1840. static inline psa_status_t psa_driver_wrapper_aead_verify(
  1841. psa_aead_operation_t *operation,
  1842. uint8_t *plaintext,
  1843. size_t plaintext_size,
  1844. size_t *plaintext_length,
  1845. const uint8_t *tag,
  1846. size_t tag_length )
  1847. {
  1848. switch( operation->id )
  1849. {
  1850. #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
  1851. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1852. {
  1853. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1854. uint8_t check_tag[PSA_AEAD_TAG_MAX_SIZE];
  1855. size_t check_tag_length;
  1856. status = mbedtls_psa_aead_finish( &operation->ctx.mbedtls_ctx,
  1857. plaintext,
  1858. plaintext_size,
  1859. plaintext_length,
  1860. check_tag,
  1861. sizeof( check_tag ),
  1862. &check_tag_length );
  1863. if( status == PSA_SUCCESS )
  1864. {
  1865. if( tag_length != check_tag_length ||
  1866. mbedtls_ct_memcmp( tag, check_tag, tag_length )
  1867. != 0 )
  1868. status = PSA_ERROR_INVALID_SIGNATURE;
  1869. }
  1870. mbedtls_platform_zeroize( check_tag, sizeof( check_tag ) );
  1871. return( status );
  1872. }
  1873. #endif /* MBEDTLS_PSA_BUILTIN_AEAD */
  1874. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1875. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1876. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  1877. return( mbedtls_test_transparent_aead_verify(
  1878. &operation->ctx.transparent_test_driver_ctx,
  1879. plaintext, plaintext_size,
  1880. plaintext_length, tag, tag_length ) );
  1881. /* Add cases for opaque driver here */
  1882. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1883. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1884. }
  1885. (void)plaintext;
  1886. (void)plaintext_size;
  1887. (void)plaintext_length;
  1888. (void)tag;
  1889. (void)tag_length;
  1890. return( PSA_ERROR_INVALID_ARGUMENT );
  1891. }
  1892. static inline psa_status_t psa_driver_wrapper_aead_abort(
  1893. psa_aead_operation_t *operation )
  1894. {
  1895. switch( operation->id )
  1896. {
  1897. #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
  1898. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1899. return( mbedtls_psa_aead_abort( &operation->ctx.mbedtls_ctx ) );
  1900. #endif /* MBEDTLS_PSA_BUILTIN_AEAD */
  1901. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1902. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1903. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  1904. return( mbedtls_test_transparent_aead_abort(
  1905. &operation->ctx.transparent_test_driver_ctx ) );
  1906. /* Add cases for opaque driver here */
  1907. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1908. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1909. }
  1910. return( PSA_ERROR_INVALID_ARGUMENT );
  1911. }
  1912. /*
  1913. * MAC functions
  1914. */
  1915. static inline psa_status_t psa_driver_wrapper_mac_compute(
  1916. const psa_key_attributes_t *attributes,
  1917. const uint8_t *key_buffer,
  1918. size_t key_buffer_size,
  1919. psa_algorithm_t alg,
  1920. const uint8_t *input,
  1921. size_t input_length,
  1922. uint8_t *mac,
  1923. size_t mac_size,
  1924. size_t *mac_length )
  1925. {
  1926. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1927. psa_key_location_t location =
  1928. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  1929. switch( location )
  1930. {
  1931. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1932. /* Key is stored in the slot in export representation, so
  1933. * cycle through all known transparent accelerators */
  1934. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1935. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1936. status = mbedtls_test_transparent_mac_compute(
  1937. attributes, key_buffer, key_buffer_size, alg,
  1938. input, input_length,
  1939. mac, mac_size, mac_length );
  1940. /* Declared with fallback == true */
  1941. if( status != PSA_ERROR_NOT_SUPPORTED )
  1942. return( status );
  1943. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1944. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1945. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  1946. /* Fell through, meaning no accelerator supports this operation */
  1947. status = mbedtls_psa_mac_compute(
  1948. attributes, key_buffer, key_buffer_size, alg,
  1949. input, input_length,
  1950. mac, mac_size, mac_length );
  1951. if( status != PSA_ERROR_NOT_SUPPORTED )
  1952. return( status );
  1953. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  1954. return( PSA_ERROR_NOT_SUPPORTED );
  1955. /* Add cases for opaque driver here */
  1956. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1957. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1958. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  1959. status = mbedtls_test_opaque_mac_compute(
  1960. attributes, key_buffer, key_buffer_size, alg,
  1961. input, input_length,
  1962. mac, mac_size, mac_length );
  1963. return( status );
  1964. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1965. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1966. default:
  1967. /* Key is declared with a lifetime not known to us */
  1968. (void) key_buffer;
  1969. (void) key_buffer_size;
  1970. (void) alg;
  1971. (void) input;
  1972. (void) input_length;
  1973. (void) mac;
  1974. (void) mac_size;
  1975. (void) mac_length;
  1976. (void) status;
  1977. return( PSA_ERROR_INVALID_ARGUMENT );
  1978. }
  1979. }
  1980. static inline psa_status_t psa_driver_wrapper_mac_sign_setup(
  1981. psa_mac_operation_t *operation,
  1982. const psa_key_attributes_t *attributes,
  1983. const uint8_t *key_buffer,
  1984. size_t key_buffer_size,
  1985. psa_algorithm_t alg )
  1986. {
  1987. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1988. psa_key_location_t location =
  1989. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  1990. switch( location )
  1991. {
  1992. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1993. /* Key is stored in the slot in export representation, so
  1994. * cycle through all known transparent accelerators */
  1995. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1996. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1997. status = mbedtls_test_transparent_mac_sign_setup(
  1998. &operation->ctx.transparent_test_driver_ctx,
  1999. attributes,
  2000. key_buffer, key_buffer_size,
  2001. alg );
  2002. /* Declared with fallback == true */
  2003. if( status == PSA_SUCCESS )
  2004. operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
  2005. if( status != PSA_ERROR_NOT_SUPPORTED )
  2006. return( status );
  2007. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2008. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2009. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  2010. /* Fell through, meaning no accelerator supports this operation */
  2011. status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx,
  2012. attributes,
  2013. key_buffer, key_buffer_size,
  2014. alg );
  2015. if( status == PSA_SUCCESS )
  2016. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  2017. if( status != PSA_ERROR_NOT_SUPPORTED )
  2018. return( status );
  2019. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  2020. return( PSA_ERROR_NOT_SUPPORTED );
  2021. /* Add cases for opaque driver here */
  2022. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  2023. #if defined(PSA_CRYPTO_DRIVER_TEST)
  2024. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  2025. status = mbedtls_test_opaque_mac_sign_setup(
  2026. &operation->ctx.opaque_test_driver_ctx,
  2027. attributes,
  2028. key_buffer, key_buffer_size,
  2029. alg );
  2030. if( status == PSA_SUCCESS )
  2031. operation->id = MBEDTLS_TEST_OPAQUE_DRIVER_ID;
  2032. return( status );
  2033. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2034. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2035. default:
  2036. /* Key is declared with a lifetime not known to us */
  2037. (void) status;
  2038. (void) operation;
  2039. (void) key_buffer;
  2040. (void) key_buffer_size;
  2041. (void) alg;
  2042. return( PSA_ERROR_INVALID_ARGUMENT );
  2043. }
  2044. }
  2045. static inline psa_status_t psa_driver_wrapper_mac_verify_setup(
  2046. psa_mac_operation_t *operation,
  2047. const psa_key_attributes_t *attributes,
  2048. const uint8_t *key_buffer,
  2049. size_t key_buffer_size,
  2050. psa_algorithm_t alg )
  2051. {
  2052. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2053. psa_key_location_t location =
  2054. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  2055. switch( location )
  2056. {
  2057. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  2058. /* Key is stored in the slot in export representation, so
  2059. * cycle through all known transparent accelerators */
  2060. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  2061. #if defined(PSA_CRYPTO_DRIVER_TEST)
  2062. status = mbedtls_test_transparent_mac_verify_setup(
  2063. &operation->ctx.transparent_test_driver_ctx,
  2064. attributes,
  2065. key_buffer, key_buffer_size,
  2066. alg );
  2067. /* Declared with fallback == true */
  2068. if( status == PSA_SUCCESS )
  2069. operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
  2070. if( status != PSA_ERROR_NOT_SUPPORTED )
  2071. return( status );
  2072. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2073. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2074. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  2075. /* Fell through, meaning no accelerator supports this operation */
  2076. status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx,
  2077. attributes,
  2078. key_buffer, key_buffer_size,
  2079. alg );
  2080. if( status == PSA_SUCCESS )
  2081. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  2082. if( status != PSA_ERROR_NOT_SUPPORTED )
  2083. return( status );
  2084. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  2085. return( PSA_ERROR_NOT_SUPPORTED );
  2086. /* Add cases for opaque driver here */
  2087. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  2088. #if defined(PSA_CRYPTO_DRIVER_TEST)
  2089. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  2090. status = mbedtls_test_opaque_mac_verify_setup(
  2091. &operation->ctx.opaque_test_driver_ctx,
  2092. attributes,
  2093. key_buffer, key_buffer_size,
  2094. alg );
  2095. if( status == PSA_SUCCESS )
  2096. operation->id = MBEDTLS_TEST_OPAQUE_DRIVER_ID;
  2097. return( status );
  2098. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2099. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2100. default:
  2101. /* Key is declared with a lifetime not known to us */
  2102. (void) status;
  2103. (void) operation;
  2104. (void) key_buffer;
  2105. (void) key_buffer_size;
  2106. (void) alg;
  2107. return( PSA_ERROR_INVALID_ARGUMENT );
  2108. }
  2109. }
  2110. static inline psa_status_t psa_driver_wrapper_mac_update(
  2111. psa_mac_operation_t *operation,
  2112. const uint8_t *input,
  2113. size_t input_length )
  2114. {
  2115. switch( operation->id )
  2116. {
  2117. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  2118. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  2119. return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx,
  2120. input, input_length ) );
  2121. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  2122. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  2123. #if defined(PSA_CRYPTO_DRIVER_TEST)
  2124. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  2125. return( mbedtls_test_transparent_mac_update(
  2126. &operation->ctx.transparent_test_driver_ctx,
  2127. input, input_length ) );
  2128. case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
  2129. return( mbedtls_test_opaque_mac_update(
  2130. &operation->ctx.opaque_test_driver_ctx,
  2131. input, input_length ) );
  2132. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2133. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2134. default:
  2135. (void) input;
  2136. (void) input_length;
  2137. return( PSA_ERROR_INVALID_ARGUMENT );
  2138. }
  2139. }
  2140. static inline psa_status_t psa_driver_wrapper_mac_sign_finish(
  2141. psa_mac_operation_t *operation,
  2142. uint8_t *mac,
  2143. size_t mac_size,
  2144. size_t *mac_length )
  2145. {
  2146. switch( operation->id )
  2147. {
  2148. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  2149. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  2150. return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx,
  2151. mac, mac_size, mac_length ) );
  2152. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  2153. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  2154. #if defined(PSA_CRYPTO_DRIVER_TEST)
  2155. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  2156. return( mbedtls_test_transparent_mac_sign_finish(
  2157. &operation->ctx.transparent_test_driver_ctx,
  2158. mac, mac_size, mac_length ) );
  2159. case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
  2160. return( mbedtls_test_opaque_mac_sign_finish(
  2161. &operation->ctx.opaque_test_driver_ctx,
  2162. mac, mac_size, mac_length ) );
  2163. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2164. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2165. default:
  2166. (void) mac;
  2167. (void) mac_size;
  2168. (void) mac_length;
  2169. return( PSA_ERROR_INVALID_ARGUMENT );
  2170. }
  2171. }
  2172. static inline psa_status_t psa_driver_wrapper_mac_verify_finish(
  2173. psa_mac_operation_t *operation,
  2174. const uint8_t *mac,
  2175. size_t mac_length )
  2176. {
  2177. switch( operation->id )
  2178. {
  2179. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  2180. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  2181. return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx,
  2182. mac, mac_length ) );
  2183. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  2184. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  2185. #if defined(PSA_CRYPTO_DRIVER_TEST)
  2186. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  2187. return( mbedtls_test_transparent_mac_verify_finish(
  2188. &operation->ctx.transparent_test_driver_ctx,
  2189. mac, mac_length ) );
  2190. case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
  2191. return( mbedtls_test_opaque_mac_verify_finish(
  2192. &operation->ctx.opaque_test_driver_ctx,
  2193. mac, mac_length ) );
  2194. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2195. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2196. default:
  2197. (void) mac;
  2198. (void) mac_length;
  2199. return( PSA_ERROR_INVALID_ARGUMENT );
  2200. }
  2201. }
  2202. static inline psa_status_t psa_driver_wrapper_mac_abort(
  2203. psa_mac_operation_t *operation )
  2204. {
  2205. switch( operation->id )
  2206. {
  2207. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  2208. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  2209. return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) );
  2210. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  2211. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  2212. #if defined(PSA_CRYPTO_DRIVER_TEST)
  2213. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  2214. return( mbedtls_test_transparent_mac_abort(
  2215. &operation->ctx.transparent_test_driver_ctx ) );
  2216. case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
  2217. return( mbedtls_test_opaque_mac_abort(
  2218. &operation->ctx.opaque_test_driver_ctx ) );
  2219. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2220. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2221. default:
  2222. return( PSA_ERROR_INVALID_ARGUMENT );
  2223. }
  2224. }
  2225. /*
  2226. * Asymmetric cryptography
  2227. */
  2228. static inline psa_status_t psa_driver_wrapper_asymmetric_encrypt(
  2229. const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
  2230. size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *input,
  2231. size_t input_length, const uint8_t *salt, size_t salt_length,
  2232. uint8_t *output, size_t output_size, size_t *output_length )
  2233. {
  2234. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2235. psa_key_location_t location =
  2236. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  2237. switch( location )
  2238. {
  2239. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  2240. /* Key is stored in the slot in export representation, so
  2241. * cycle through all known transparent accelerators */
  2242. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  2243. #if defined(PSA_CRYPTO_DRIVER_TEST)
  2244. status = mbedtls_test_transparent_asymmetric_encrypt( attributes,
  2245. key_buffer, key_buffer_size, alg, input, input_length,
  2246. salt, salt_length, output, output_size,
  2247. output_length );
  2248. /* Declared with fallback == true */
  2249. if( status != PSA_ERROR_NOT_SUPPORTED )
  2250. return( status );
  2251. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2252. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2253. return( mbedtls_psa_asymmetric_encrypt( attributes,
  2254. key_buffer, key_buffer_size, alg, input, input_length,
  2255. salt, salt_length, output, output_size, output_length )
  2256. );
  2257. /* Add cases for opaque driver here */
  2258. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  2259. #if defined(PSA_CRYPTO_DRIVER_TEST)
  2260. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  2261. return( mbedtls_test_opaque_asymmetric_encrypt( attributes,
  2262. key_buffer, key_buffer_size, alg, input, input_length,
  2263. salt, salt_length, output, output_size, output_length )
  2264. );
  2265. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2266. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2267. default:
  2268. /* Key is declared with a lifetime not known to us */
  2269. (void)status;
  2270. (void)key_buffer;
  2271. (void)key_buffer_size;
  2272. (void)alg;
  2273. (void)input;
  2274. (void)input_length;
  2275. (void)salt;
  2276. (void)salt_length;
  2277. (void)output;
  2278. (void)output_size;
  2279. (void)output_length;
  2280. return( PSA_ERROR_INVALID_ARGUMENT );
  2281. }
  2282. }
  2283. static inline psa_status_t psa_driver_wrapper_asymmetric_decrypt(
  2284. const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
  2285. size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *input,
  2286. size_t input_length, const uint8_t *salt, size_t salt_length,
  2287. uint8_t *output, size_t output_size, size_t *output_length )
  2288. {
  2289. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2290. psa_key_location_t location =
  2291. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  2292. switch( location )
  2293. {
  2294. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  2295. /* Key is stored in the slot in export representation, so
  2296. * cycle through all known transparent accelerators */
  2297. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  2298. #if defined(PSA_CRYPTO_DRIVER_TEST)
  2299. status = mbedtls_test_transparent_asymmetric_decrypt( attributes,
  2300. key_buffer, key_buffer_size, alg, input, input_length,
  2301. salt, salt_length, output, output_size,
  2302. output_length );
  2303. /* Declared with fallback == true */
  2304. if( status != PSA_ERROR_NOT_SUPPORTED )
  2305. return( status );
  2306. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2307. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2308. return( mbedtls_psa_asymmetric_decrypt( attributes,
  2309. key_buffer, key_buffer_size, alg,input, input_length,
  2310. salt, salt_length, output, output_size,
  2311. output_length ) );
  2312. /* Add cases for opaque driver here */
  2313. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  2314. #if defined(PSA_CRYPTO_DRIVER_TEST)
  2315. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  2316. return( mbedtls_test_opaque_asymmetric_decrypt( attributes,
  2317. key_buffer, key_buffer_size, alg, input, input_length,
  2318. salt, salt_length, output, output_size,
  2319. output_length ) );
  2320. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2321. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2322. default:
  2323. /* Key is declared with a lifetime not known to us */
  2324. (void)status;
  2325. (void)key_buffer;
  2326. (void)key_buffer_size;
  2327. (void)alg;
  2328. (void)input;
  2329. (void)input_length;
  2330. (void)salt;
  2331. (void)salt_length;
  2332. (void)output;
  2333. (void)output_size;
  2334. (void)output_length;
  2335. return( PSA_ERROR_INVALID_ARGUMENT );
  2336. }
  2337. }
  2338. static inline psa_status_t psa_driver_wrapper_key_agreement(
  2339. const psa_key_attributes_t *attributes,
  2340. const uint8_t *key_buffer,
  2341. size_t key_buffer_size,
  2342. psa_algorithm_t alg,
  2343. const uint8_t *peer_key,
  2344. size_t peer_key_length,
  2345. uint8_t *shared_secret,
  2346. size_t shared_secret_size,
  2347. size_t *shared_secret_length
  2348. )
  2349. {
  2350. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2351. psa_key_location_t location =
  2352. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  2353. switch( location )
  2354. {
  2355. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  2356. /* Key is stored in the slot in export representation, so
  2357. * cycle through all known transparent accelerators */
  2358. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  2359. #if defined(PSA_CRYPTO_DRIVER_TEST)
  2360. status =
  2361. mbedtls_test_transparent_key_agreement( attributes,
  2362. key_buffer, key_buffer_size, alg, peer_key,
  2363. peer_key_length, shared_secret, shared_secret_size,
  2364. shared_secret_length );
  2365. if( status != PSA_ERROR_NOT_SUPPORTED )
  2366. return( status );
  2367. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2368. #if defined(MBEDTLS_PSA_P256M_DRIVER_ENABLED)
  2369. if( PSA_KEY_TYPE_IS_ECC( psa_get_key_type(attributes) ) &&
  2370. PSA_ALG_IS_ECDH(alg) &&
  2371. PSA_KEY_TYPE_ECC_GET_FAMILY(psa_get_key_type(attributes)) == PSA_ECC_FAMILY_SECP_R1 &&
  2372. psa_get_key_bits(attributes) == 256 )
  2373. {
  2374. status = p256_transparent_key_agreement( attributes,
  2375. key_buffer,
  2376. key_buffer_size,
  2377. alg,
  2378. peer_key,
  2379. peer_key_length,
  2380. shared_secret,
  2381. shared_secret_size,
  2382. shared_secret_length );
  2383. if( status != PSA_ERROR_NOT_SUPPORTED)
  2384. return( status );
  2385. }
  2386. #endif /* MBEDTLS_PSA_P256M_DRIVER_ENABLED */
  2387. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2388. /* Software Fallback */
  2389. status = psa_key_agreement_raw_builtin( attributes,
  2390. key_buffer,
  2391. key_buffer_size,
  2392. alg,
  2393. peer_key,
  2394. peer_key_length,
  2395. shared_secret,
  2396. shared_secret_size,
  2397. shared_secret_length );
  2398. return( status );
  2399. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  2400. #if defined(PSA_CRYPTO_DRIVER_TEST)
  2401. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  2402. return( mbedtls_test_opaque_key_agreement( attributes,
  2403. key_buffer, key_buffer_size, alg, peer_key,
  2404. peer_key_length, shared_secret, shared_secret_size,
  2405. shared_secret_length ) );
  2406. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2407. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2408. default:
  2409. (void) attributes;
  2410. (void) key_buffer;
  2411. (void) key_buffer_size;
  2412. (void) peer_key;
  2413. (void) peer_key_length;
  2414. (void) shared_secret;
  2415. (void) shared_secret_size;
  2416. (void) shared_secret_length;
  2417. return( PSA_ERROR_NOT_SUPPORTED );
  2418. }
  2419. }
  2420. static inline psa_status_t psa_driver_wrapper_pake_setup(
  2421. psa_pake_operation_t *operation,
  2422. const psa_crypto_driver_pake_inputs_t *inputs )
  2423. {
  2424. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2425. psa_key_location_t location =
  2426. PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime( &inputs->attributes ) );
  2427. switch( location )
  2428. {
  2429. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  2430. /* Key is stored in the slot in export representation, so
  2431. * cycle through all known transparent accelerators */
  2432. status = PSA_ERROR_NOT_SUPPORTED;
  2433. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  2434. #if defined(PSA_CRYPTO_DRIVER_TEST)
  2435. status = mbedtls_test_transparent_pake_setup(
  2436. &operation->data.ctx.transparent_test_driver_ctx,
  2437. inputs );
  2438. if( status == PSA_SUCCESS )
  2439. operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
  2440. /* Declared with fallback == true */
  2441. if( status != PSA_ERROR_NOT_SUPPORTED )
  2442. return( status );
  2443. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2444. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2445. #if defined(MBEDTLS_PSA_BUILTIN_PAKE)
  2446. status = mbedtls_psa_pake_setup( &operation->data.ctx.mbedtls_ctx,
  2447. inputs );
  2448. if( status == PSA_SUCCESS )
  2449. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  2450. #endif
  2451. return status;
  2452. /* Add cases for opaque driver here */
  2453. default:
  2454. /* Key is declared with a lifetime not known to us */
  2455. (void)operation;
  2456. return( PSA_ERROR_INVALID_ARGUMENT );
  2457. }
  2458. }
  2459. static inline psa_status_t psa_driver_wrapper_pake_output(
  2460. psa_pake_operation_t *operation,
  2461. psa_crypto_driver_pake_step_t step,
  2462. uint8_t *output,
  2463. size_t output_size,
  2464. size_t *output_length )
  2465. {
  2466. switch( operation->id )
  2467. {
  2468. #if defined(MBEDTLS_PSA_BUILTIN_PAKE)
  2469. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  2470. return( mbedtls_psa_pake_output( &operation->data.ctx.mbedtls_ctx, step,
  2471. output, output_size, output_length ) );
  2472. #endif /* MBEDTLS_PSA_BUILTIN_PAKE */
  2473. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  2474. #if defined(PSA_CRYPTO_DRIVER_TEST)
  2475. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  2476. return( mbedtls_test_transparent_pake_output(
  2477. &operation->data.ctx.transparent_test_driver_ctx,
  2478. step, output, output_size, output_length ) );
  2479. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2480. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2481. default:
  2482. (void) step;
  2483. (void) output;
  2484. (void) output_size;
  2485. (void) output_length;
  2486. return( PSA_ERROR_INVALID_ARGUMENT );
  2487. }
  2488. }
  2489. static inline psa_status_t psa_driver_wrapper_pake_input(
  2490. psa_pake_operation_t *operation,
  2491. psa_crypto_driver_pake_step_t step,
  2492. const uint8_t *input,
  2493. size_t input_length )
  2494. {
  2495. switch( operation->id )
  2496. {
  2497. #if defined(MBEDTLS_PSA_BUILTIN_PAKE)
  2498. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  2499. return( mbedtls_psa_pake_input( &operation->data.ctx.mbedtls_ctx,
  2500. step, input,
  2501. input_length ) );
  2502. #endif /* MBEDTLS_PSA_BUILTIN_PAKE */
  2503. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  2504. #if defined(PSA_CRYPTO_DRIVER_TEST)
  2505. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  2506. return( mbedtls_test_transparent_pake_input(
  2507. &operation->data.ctx.transparent_test_driver_ctx,
  2508. step,
  2509. input, input_length ) );
  2510. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2511. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2512. default:
  2513. (void) step;
  2514. (void) input;
  2515. (void) input_length;
  2516. return( PSA_ERROR_INVALID_ARGUMENT );
  2517. }
  2518. }
  2519. static inline psa_status_t psa_driver_wrapper_pake_get_implicit_key(
  2520. psa_pake_operation_t *operation,
  2521. uint8_t *output, size_t output_size,
  2522. size_t *output_length )
  2523. {
  2524. switch( operation->id )
  2525. {
  2526. #if defined(MBEDTLS_PSA_BUILTIN_PAKE)
  2527. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  2528. return( mbedtls_psa_pake_get_implicit_key( &operation->data.ctx.mbedtls_ctx,
  2529. output, output_size, output_length ) );
  2530. #endif /* MBEDTLS_PSA_BUILTIN_PAKE */
  2531. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  2532. #if defined(PSA_CRYPTO_DRIVER_TEST)
  2533. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  2534. return( mbedtls_test_transparent_pake_get_implicit_key(
  2535. &operation->data.ctx.transparent_test_driver_ctx,
  2536. output, output_size, output_length ) );
  2537. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2538. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2539. default:
  2540. (void) output;
  2541. (void) output_size;
  2542. (void) output_length;
  2543. return( PSA_ERROR_INVALID_ARGUMENT );
  2544. }
  2545. }
  2546. static inline psa_status_t psa_driver_wrapper_pake_abort(
  2547. psa_pake_operation_t * operation )
  2548. {
  2549. switch( operation->id )
  2550. {
  2551. #if defined(MBEDTLS_PSA_BUILTIN_PAKE)
  2552. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  2553. return( mbedtls_psa_pake_abort( &operation->data.ctx.mbedtls_ctx ) );
  2554. #endif /* MBEDTLS_PSA_BUILTIN_PAKE */
  2555. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  2556. #if defined(PSA_CRYPTO_DRIVER_TEST)
  2557. case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
  2558. return( mbedtls_test_transparent_pake_abort(
  2559. &operation->data.ctx.transparent_test_driver_ctx ) );
  2560. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2561. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2562. default:
  2563. return( PSA_ERROR_INVALID_ARGUMENT );
  2564. }
  2565. }
  2566. #endif /* MBEDTLS_PSA_CRYPTO_C */