psa_crypto_driver_wrappers.h 106 KB

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