psa_crypto_driver_wrappers.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  1. /*
  2. * Functions to delegate cryptographic operations to an available
  3. * and appropriate accelerator.
  4. * Warning: This file will be auto-generated in the future.
  5. */
  6. /* Copyright The Mbed TLS Contributors
  7. * SPDX-License-Identifier: Apache-2.0
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10. * not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. #include "psa_crypto_aead.h"
  22. #include "psa_crypto_cipher.h"
  23. #include "psa_crypto_core.h"
  24. #include "psa_crypto_driver_wrappers.h"
  25. #include "psa_crypto_hash.h"
  26. #include "psa_crypto_mac.h"
  27. #include "mbedtls/platform.h"
  28. #if defined(MBEDTLS_PSA_CRYPTO_C)
  29. #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
  30. /* Include test driver definition when running tests */
  31. #if defined(PSA_CRYPTO_DRIVER_TEST)
  32. #ifndef PSA_CRYPTO_DRIVER_PRESENT
  33. #define PSA_CRYPTO_DRIVER_PRESENT
  34. #endif
  35. #ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
  36. #define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
  37. #endif
  38. #include "test/drivers/test_driver.h"
  39. #endif /* PSA_CRYPTO_DRIVER_TEST */
  40. /* Repeat above block for each JSON-declared driver during autogeneration */
  41. #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
  42. /* Auto-generated values depending on which drivers are registered.
  43. * ID 0 is reserved for unallocated operations.
  44. * ID 1 is reserved for the Mbed TLS software driver. */
  45. #define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
  46. #if defined(PSA_CRYPTO_DRIVER_TEST)
  47. #define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2)
  48. #define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3)
  49. #endif /* PSA_CRYPTO_DRIVER_TEST */
  50. /* Support the 'old' SE interface when asked to */
  51. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  52. /* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
  53. * SE driver is present, to avoid unused argument errors at compile time. */
  54. #ifndef PSA_CRYPTO_DRIVER_PRESENT
  55. #define PSA_CRYPTO_DRIVER_PRESENT
  56. #endif
  57. #include "psa_crypto_se.h"
  58. #endif
  59. psa_status_t psa_driver_wrapper_init( void )
  60. {
  61. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  62. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  63. status = psa_init_all_se_drivers( );
  64. if( status != PSA_SUCCESS )
  65. return( status );
  66. #endif
  67. #if defined(PSA_CRYPTO_DRIVER_TEST)
  68. status = mbedtls_test_transparent_init( );
  69. if( status != PSA_SUCCESS )
  70. return( status );
  71. status = mbedtls_test_opaque_init( );
  72. if( status != PSA_SUCCESS )
  73. return( status );
  74. #endif
  75. (void) status;
  76. return( PSA_SUCCESS );
  77. }
  78. void psa_driver_wrapper_free( void )
  79. {
  80. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  81. /* Unregister all secure element drivers, so that we restart from
  82. * a pristine state. */
  83. psa_unregister_all_se_drivers( );
  84. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  85. #if defined(PSA_CRYPTO_DRIVER_TEST)
  86. mbedtls_test_transparent_free( );
  87. mbedtls_test_opaque_free( );
  88. #endif
  89. }
  90. /* Start delegation functions */
  91. psa_status_t psa_driver_wrapper_sign_message(
  92. const psa_key_attributes_t *attributes,
  93. const uint8_t *key_buffer,
  94. size_t key_buffer_size,
  95. psa_algorithm_t alg,
  96. const uint8_t *input,
  97. size_t input_length,
  98. uint8_t *signature,
  99. size_t signature_size,
  100. size_t *signature_length )
  101. {
  102. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  103. psa_key_location_t location =
  104. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  105. switch( location )
  106. {
  107. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  108. /* Key is stored in the slot in export representation, so
  109. * cycle through all known transparent accelerators */
  110. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  111. #if defined(PSA_CRYPTO_DRIVER_TEST)
  112. status = mbedtls_test_transparent_signature_sign_message(
  113. attributes,
  114. key_buffer,
  115. key_buffer_size,
  116. alg,
  117. input,
  118. input_length,
  119. signature,
  120. signature_size,
  121. signature_length );
  122. /* Declared with fallback == true */
  123. if( status != PSA_ERROR_NOT_SUPPORTED )
  124. return( status );
  125. #endif /* PSA_CRYPTO_DRIVER_TEST */
  126. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  127. break;
  128. /* Add cases for opaque driver here */
  129. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  130. #if defined(PSA_CRYPTO_DRIVER_TEST)
  131. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  132. status = mbedtls_test_opaque_signature_sign_message(
  133. attributes,
  134. key_buffer,
  135. key_buffer_size,
  136. alg,
  137. input,
  138. input_length,
  139. signature,
  140. signature_size,
  141. signature_length );
  142. if( status != PSA_ERROR_NOT_SUPPORTED )
  143. return( status );
  144. break;
  145. #endif /* PSA_CRYPTO_DRIVER_TEST */
  146. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  147. default:
  148. /* Key is declared with a lifetime not known to us */
  149. (void)status;
  150. break;
  151. }
  152. return( psa_sign_message_builtin( attributes,
  153. key_buffer,
  154. key_buffer_size,
  155. alg,
  156. input,
  157. input_length,
  158. signature,
  159. signature_size,
  160. signature_length ) );
  161. }
  162. psa_status_t psa_driver_wrapper_verify_message(
  163. const psa_key_attributes_t *attributes,
  164. const uint8_t *key_buffer,
  165. size_t key_buffer_size,
  166. psa_algorithm_t alg,
  167. const uint8_t *input,
  168. size_t input_length,
  169. const uint8_t *signature,
  170. size_t signature_length )
  171. {
  172. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  173. psa_key_location_t location =
  174. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  175. switch( location )
  176. {
  177. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  178. /* Key is stored in the slot in export representation, so
  179. * cycle through all known transparent accelerators */
  180. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  181. #if defined(PSA_CRYPTO_DRIVER_TEST)
  182. status = mbedtls_test_transparent_signature_verify_message(
  183. attributes,
  184. key_buffer,
  185. key_buffer_size,
  186. alg,
  187. input,
  188. input_length,
  189. signature,
  190. signature_length );
  191. /* Declared with fallback == true */
  192. if( status != PSA_ERROR_NOT_SUPPORTED )
  193. return( status );
  194. #endif /* PSA_CRYPTO_DRIVER_TEST */
  195. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  196. break;
  197. /* Add cases for opaque driver here */
  198. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  199. #if defined(PSA_CRYPTO_DRIVER_TEST)
  200. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  201. return( mbedtls_test_opaque_signature_verify_message(
  202. attributes,
  203. key_buffer,
  204. key_buffer_size,
  205. alg,
  206. input,
  207. input_length,
  208. signature,
  209. signature_length ) );
  210. if( status != PSA_ERROR_NOT_SUPPORTED )
  211. return( status );
  212. break;
  213. #endif /* PSA_CRYPTO_DRIVER_TEST */
  214. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  215. default:
  216. /* Key is declared with a lifetime not known to us */
  217. (void)status;
  218. break;
  219. }
  220. return( psa_verify_message_builtin( attributes,
  221. key_buffer,
  222. key_buffer_size,
  223. alg,
  224. input,
  225. input_length,
  226. signature,
  227. signature_length ) );
  228. }
  229. psa_status_t psa_driver_wrapper_sign_hash(
  230. const psa_key_attributes_t *attributes,
  231. const uint8_t *key_buffer, size_t key_buffer_size,
  232. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  233. uint8_t *signature, size_t signature_size, size_t *signature_length )
  234. {
  235. /* Try dynamically-registered SE interface first */
  236. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  237. const psa_drv_se_t *drv;
  238. psa_drv_se_context_t *drv_context;
  239. if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
  240. {
  241. if( drv->asymmetric == NULL ||
  242. drv->asymmetric->p_sign == NULL )
  243. {
  244. /* Key is defined in SE, but we have no way to exercise it */
  245. return( PSA_ERROR_NOT_SUPPORTED );
  246. }
  247. return( drv->asymmetric->p_sign(
  248. drv_context, *( (psa_key_slot_number_t *)key_buffer ),
  249. alg, hash, hash_length,
  250. signature, signature_size, signature_length ) );
  251. }
  252. #endif /* PSA_CRYPTO_SE_C */
  253. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  254. psa_key_location_t location =
  255. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  256. switch( location )
  257. {
  258. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  259. /* Key is stored in the slot in export representation, so
  260. * cycle through all known transparent accelerators */
  261. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  262. #if defined(PSA_CRYPTO_DRIVER_TEST)
  263. status = mbedtls_test_transparent_signature_sign_hash( attributes,
  264. key_buffer,
  265. key_buffer_size,
  266. alg,
  267. hash,
  268. hash_length,
  269. signature,
  270. signature_size,
  271. signature_length );
  272. /* Declared with fallback == true */
  273. if( status != PSA_ERROR_NOT_SUPPORTED )
  274. return( status );
  275. #endif /* PSA_CRYPTO_DRIVER_TEST */
  276. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  277. /* Fell through, meaning no accelerator supports this operation */
  278. return( psa_sign_hash_builtin( attributes,
  279. key_buffer,
  280. key_buffer_size,
  281. alg,
  282. hash,
  283. hash_length,
  284. signature,
  285. signature_size,
  286. signature_length ) );
  287. /* Add cases for opaque driver here */
  288. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  289. #if defined(PSA_CRYPTO_DRIVER_TEST)
  290. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  291. return( mbedtls_test_opaque_signature_sign_hash( attributes,
  292. key_buffer,
  293. key_buffer_size,
  294. alg,
  295. hash,
  296. hash_length,
  297. signature,
  298. signature_size,
  299. signature_length ) );
  300. #endif /* PSA_CRYPTO_DRIVER_TEST */
  301. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  302. default:
  303. /* Key is declared with a lifetime not known to us */
  304. (void)status;
  305. return( PSA_ERROR_INVALID_ARGUMENT );
  306. }
  307. }
  308. psa_status_t psa_driver_wrapper_verify_hash(
  309. const psa_key_attributes_t *attributes,
  310. const uint8_t *key_buffer, size_t key_buffer_size,
  311. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  312. const uint8_t *signature, size_t signature_length )
  313. {
  314. /* Try dynamically-registered SE interface first */
  315. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  316. const psa_drv_se_t *drv;
  317. psa_drv_se_context_t *drv_context;
  318. if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
  319. {
  320. if( drv->asymmetric == NULL ||
  321. drv->asymmetric->p_verify == NULL )
  322. {
  323. /* Key is defined in SE, but we have no way to exercise it */
  324. return( PSA_ERROR_NOT_SUPPORTED );
  325. }
  326. return( drv->asymmetric->p_verify(
  327. drv_context, *( (psa_key_slot_number_t *)key_buffer ),
  328. alg, hash, hash_length,
  329. signature, signature_length ) );
  330. }
  331. #endif /* PSA_CRYPTO_SE_C */
  332. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  333. psa_key_location_t location =
  334. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  335. switch( location )
  336. {
  337. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  338. /* Key is stored in the slot in export representation, so
  339. * cycle through all known transparent accelerators */
  340. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  341. #if defined(PSA_CRYPTO_DRIVER_TEST)
  342. status = mbedtls_test_transparent_signature_verify_hash(
  343. attributes,
  344. key_buffer,
  345. key_buffer_size,
  346. alg,
  347. hash,
  348. hash_length,
  349. signature,
  350. signature_length );
  351. /* Declared with fallback == true */
  352. if( status != PSA_ERROR_NOT_SUPPORTED )
  353. return( status );
  354. #endif /* PSA_CRYPTO_DRIVER_TEST */
  355. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  356. return( psa_verify_hash_builtin( attributes,
  357. key_buffer,
  358. key_buffer_size,
  359. alg,
  360. hash,
  361. hash_length,
  362. signature,
  363. signature_length ) );
  364. /* Add cases for opaque driver here */
  365. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  366. #if defined(PSA_CRYPTO_DRIVER_TEST)
  367. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  368. return( mbedtls_test_opaque_signature_verify_hash( attributes,
  369. key_buffer,
  370. key_buffer_size,
  371. alg,
  372. hash,
  373. hash_length,
  374. signature,
  375. signature_length ) );
  376. #endif /* PSA_CRYPTO_DRIVER_TEST */
  377. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  378. default:
  379. /* Key is declared with a lifetime not known to us */
  380. (void)status;
  381. return( PSA_ERROR_INVALID_ARGUMENT );
  382. }
  383. }
  384. /** Get the key buffer size required to store the key material of a key
  385. * associated with an opaque driver without storage.
  386. *
  387. * \param[in] attributes The key attributes.
  388. * \param[out] key_buffer_size Minimum buffer size to contain the key material
  389. *
  390. * \retval #PSA_SUCCESS
  391. * The minimum size for a buffer to contain the key material has been
  392. * returned successfully.
  393. * \retval #PSA_ERROR_INVALID_ARGUMENT
  394. * The size in bits of the key is not valid.
  395. * \retval #PSA_ERROR_NOT_SUPPORTED
  396. * The type and/or the size in bits of the key or the combination of
  397. * the two is not supported.
  398. */
  399. psa_status_t psa_driver_wrapper_get_key_buffer_size(
  400. const psa_key_attributes_t *attributes,
  401. size_t *key_buffer_size )
  402. {
  403. psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  404. psa_key_type_t key_type = attributes->core.type;
  405. size_t key_bits = attributes->core.bits;
  406. *key_buffer_size = 0;
  407. switch( location )
  408. {
  409. #if defined(PSA_CRYPTO_DRIVER_TEST)
  410. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  411. #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
  412. /* Emulate property 'builtin_key_size' */
  413. if( psa_key_id_is_builtin(
  414. MBEDTLS_SVC_KEY_ID_GET_KEY_ID(
  415. psa_get_key_id( attributes ) ) ) )
  416. {
  417. *key_buffer_size = sizeof( psa_drv_slot_number_t );
  418. return( PSA_SUCCESS );
  419. }
  420. #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
  421. *key_buffer_size = mbedtls_test_size_function( key_type, key_bits );
  422. return( ( *key_buffer_size != 0 ) ?
  423. PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
  424. #endif /* PSA_CRYPTO_DRIVER_TEST */
  425. default:
  426. (void)key_type;
  427. (void)key_bits;
  428. return( PSA_ERROR_NOT_SUPPORTED );
  429. }
  430. }
  431. psa_status_t psa_driver_wrapper_generate_key(
  432. const psa_key_attributes_t *attributes,
  433. uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
  434. {
  435. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  436. psa_key_location_t location =
  437. PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
  438. /* Try dynamically-registered SE interface first */
  439. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  440. const psa_drv_se_t *drv;
  441. psa_drv_se_context_t *drv_context;
  442. if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
  443. {
  444. size_t pubkey_length = 0; /* We don't support this feature yet */
  445. if( drv->key_management == NULL ||
  446. drv->key_management->p_generate == NULL )
  447. {
  448. /* Key is defined as being in SE, but we have no way to generate it */
  449. return( PSA_ERROR_NOT_SUPPORTED );
  450. }
  451. return( drv->key_management->p_generate(
  452. drv_context,
  453. *( (psa_key_slot_number_t *)key_buffer ),
  454. attributes, NULL, 0, &pubkey_length ) );
  455. }
  456. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  457. switch( location )
  458. {
  459. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  460. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  461. /* Transparent drivers are limited to generating asymmetric keys */
  462. if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
  463. {
  464. /* Cycle through all known transparent accelerators */
  465. #if defined(PSA_CRYPTO_DRIVER_TEST)
  466. status = mbedtls_test_transparent_generate_key(
  467. attributes, key_buffer, key_buffer_size,
  468. key_buffer_length );
  469. /* Declared with fallback == true */
  470. if( status != PSA_ERROR_NOT_SUPPORTED )
  471. break;
  472. #endif /* PSA_CRYPTO_DRIVER_TEST */
  473. }
  474. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  475. /* Software fallback */
  476. status = psa_generate_key_internal(
  477. attributes, key_buffer, key_buffer_size, key_buffer_length );
  478. break;
  479. /* Add cases for opaque driver here */
  480. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  481. #if defined(PSA_CRYPTO_DRIVER_TEST)
  482. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  483. status = mbedtls_test_opaque_generate_key(
  484. attributes, key_buffer, key_buffer_size, key_buffer_length );
  485. break;
  486. #endif /* PSA_CRYPTO_DRIVER_TEST */
  487. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  488. default:
  489. /* Key is declared with a lifetime not known to us */
  490. status = PSA_ERROR_INVALID_ARGUMENT;
  491. break;
  492. }
  493. return( status );
  494. }
  495. psa_status_t psa_driver_wrapper_import_key(
  496. const psa_key_attributes_t *attributes,
  497. const uint8_t *data,
  498. size_t data_length,
  499. uint8_t *key_buffer,
  500. size_t key_buffer_size,
  501. size_t *key_buffer_length,
  502. size_t *bits )
  503. {
  504. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  505. psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
  506. psa_get_key_lifetime( attributes ) );
  507. /* Try dynamically-registered SE interface first */
  508. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  509. const psa_drv_se_t *drv;
  510. psa_drv_se_context_t *drv_context;
  511. if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
  512. {
  513. if( drv->key_management == NULL ||
  514. drv->key_management->p_import == NULL )
  515. return( PSA_ERROR_NOT_SUPPORTED );
  516. /* The driver should set the number of key bits, however in
  517. * case it doesn't, we initialize bits to an invalid value. */
  518. *bits = PSA_MAX_KEY_BITS + 1;
  519. status = drv->key_management->p_import(
  520. drv_context,
  521. *( (psa_key_slot_number_t *)key_buffer ),
  522. attributes, data, data_length, bits );
  523. if( status != PSA_SUCCESS )
  524. return( status );
  525. if( (*bits) > PSA_MAX_KEY_BITS )
  526. return( PSA_ERROR_NOT_SUPPORTED );
  527. return( PSA_SUCCESS );
  528. }
  529. #endif /* PSA_CRYPTO_SE_C */
  530. switch( location )
  531. {
  532. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  533. /* Key is stored in the slot in export representation, so
  534. * cycle through all known transparent accelerators */
  535. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  536. #if defined(PSA_CRYPTO_DRIVER_TEST)
  537. status = mbedtls_test_transparent_import_key(
  538. attributes,
  539. data, data_length,
  540. key_buffer, key_buffer_size,
  541. key_buffer_length, bits );
  542. /* Declared with fallback == true */
  543. if( status != PSA_ERROR_NOT_SUPPORTED )
  544. return( status );
  545. #endif /* PSA_CRYPTO_DRIVER_TEST */
  546. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  547. /* Fell through, meaning no accelerator supports this operation */
  548. return( psa_import_key_into_slot( attributes,
  549. data, data_length,
  550. key_buffer, key_buffer_size,
  551. key_buffer_length, bits ) );
  552. default:
  553. /* Importing a key with external storage in not yet supported.
  554. * Return in error indicating that the lifetime is not valid. */
  555. (void)status;
  556. return( PSA_ERROR_INVALID_ARGUMENT );
  557. }
  558. }
  559. psa_status_t psa_driver_wrapper_export_key(
  560. const psa_key_attributes_t *attributes,
  561. const uint8_t *key_buffer, size_t key_buffer_size,
  562. uint8_t *data, size_t data_size, size_t *data_length )
  563. {
  564. psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
  565. psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
  566. psa_get_key_lifetime( attributes ) );
  567. /* Try dynamically-registered SE interface first */
  568. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  569. const psa_drv_se_t *drv;
  570. psa_drv_se_context_t *drv_context;
  571. if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
  572. {
  573. if( ( drv->key_management == NULL ) ||
  574. ( drv->key_management->p_export == NULL ) )
  575. {
  576. return( PSA_ERROR_NOT_SUPPORTED );
  577. }
  578. return( drv->key_management->p_export(
  579. drv_context,
  580. *( (psa_key_slot_number_t *)key_buffer ),
  581. data, data_size, data_length ) );
  582. }
  583. #endif /* PSA_CRYPTO_SE_C */
  584. switch( location )
  585. {
  586. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  587. return( psa_export_key_internal( attributes,
  588. key_buffer,
  589. key_buffer_size,
  590. data,
  591. data_size,
  592. data_length ) );
  593. /* Add cases for opaque driver here */
  594. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  595. #if defined(PSA_CRYPTO_DRIVER_TEST)
  596. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  597. return( mbedtls_test_opaque_export_key( attributes,
  598. key_buffer,
  599. key_buffer_size,
  600. data,
  601. data_size,
  602. data_length ) );
  603. #endif /* PSA_CRYPTO_DRIVER_TEST */
  604. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  605. default:
  606. /* Key is declared with a lifetime not known to us */
  607. return( status );
  608. }
  609. }
  610. psa_status_t psa_driver_wrapper_export_public_key(
  611. const psa_key_attributes_t *attributes,
  612. const uint8_t *key_buffer, size_t key_buffer_size,
  613. uint8_t *data, size_t data_size, size_t *data_length )
  614. {
  615. psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
  616. psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
  617. psa_get_key_lifetime( attributes ) );
  618. /* Try dynamically-registered SE interface first */
  619. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  620. const psa_drv_se_t *drv;
  621. psa_drv_se_context_t *drv_context;
  622. if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
  623. {
  624. if( ( drv->key_management == NULL ) ||
  625. ( drv->key_management->p_export_public == NULL ) )
  626. {
  627. return( PSA_ERROR_NOT_SUPPORTED );
  628. }
  629. return( drv->key_management->p_export_public(
  630. drv_context,
  631. *( (psa_key_slot_number_t *)key_buffer ),
  632. data, data_size, data_length ) );
  633. }
  634. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  635. switch( location )
  636. {
  637. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  638. /* Key is stored in the slot in export representation, so
  639. * cycle through all known transparent accelerators */
  640. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  641. #if defined(PSA_CRYPTO_DRIVER_TEST)
  642. status = mbedtls_test_transparent_export_public_key(
  643. attributes,
  644. key_buffer,
  645. key_buffer_size,
  646. data,
  647. data_size,
  648. data_length );
  649. /* Declared with fallback == true */
  650. if( status != PSA_ERROR_NOT_SUPPORTED )
  651. return( status );
  652. #endif /* PSA_CRYPTO_DRIVER_TEST */
  653. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  654. /* Fell through, meaning no accelerator supports this operation */
  655. return( psa_export_public_key_internal( attributes,
  656. key_buffer,
  657. key_buffer_size,
  658. data,
  659. data_size,
  660. data_length ) );
  661. /* Add cases for opaque driver here */
  662. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  663. #if defined(PSA_CRYPTO_DRIVER_TEST)
  664. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  665. return( mbedtls_test_opaque_export_public_key( attributes,
  666. key_buffer,
  667. key_buffer_size,
  668. data,
  669. data_size,
  670. data_length ) );
  671. #endif /* PSA_CRYPTO_DRIVER_TEST */
  672. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  673. default:
  674. /* Key is declared with a lifetime not known to us */
  675. return( status );
  676. }
  677. }
  678. psa_status_t psa_driver_wrapper_get_builtin_key(
  679. psa_drv_slot_number_t slot_number,
  680. psa_key_attributes_t *attributes,
  681. uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
  682. {
  683. psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  684. switch( location )
  685. {
  686. #if defined(PSA_CRYPTO_DRIVER_TEST)
  687. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  688. return( mbedtls_test_opaque_get_builtin_key(
  689. slot_number,
  690. attributes,
  691. key_buffer, key_buffer_size, key_buffer_length ) );
  692. #endif /* PSA_CRYPTO_DRIVER_TEST */
  693. default:
  694. (void) slot_number;
  695. (void) key_buffer;
  696. (void) key_buffer_size;
  697. (void) key_buffer_length;
  698. return( PSA_ERROR_DOES_NOT_EXIST );
  699. }
  700. }
  701. /*
  702. * Cipher functions
  703. */
  704. psa_status_t psa_driver_wrapper_cipher_encrypt(
  705. const psa_key_attributes_t *attributes,
  706. const uint8_t *key_buffer,
  707. size_t key_buffer_size,
  708. psa_algorithm_t alg,
  709. const uint8_t *iv,
  710. size_t iv_length,
  711. const uint8_t *input,
  712. size_t input_length,
  713. uint8_t *output,
  714. size_t output_size,
  715. size_t *output_length )
  716. {
  717. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  718. psa_key_location_t location =
  719. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  720. switch( location )
  721. {
  722. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  723. /* Key is stored in the slot in export representation, so
  724. * cycle through all known transparent accelerators */
  725. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  726. #if defined(PSA_CRYPTO_DRIVER_TEST)
  727. status = mbedtls_test_transparent_cipher_encrypt( attributes,
  728. key_buffer,
  729. key_buffer_size,
  730. alg,
  731. iv,
  732. iv_length,
  733. input,
  734. input_length,
  735. output,
  736. output_size,
  737. output_length );
  738. /* Declared with fallback == true */
  739. if( status != PSA_ERROR_NOT_SUPPORTED )
  740. return( status );
  741. #endif /* PSA_CRYPTO_DRIVER_TEST */
  742. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  743. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  744. return( mbedtls_psa_cipher_encrypt( attributes,
  745. key_buffer,
  746. key_buffer_size,
  747. alg,
  748. iv,
  749. iv_length,
  750. input,
  751. input_length,
  752. output,
  753. output_size,
  754. output_length ) );
  755. #else
  756. return( PSA_ERROR_NOT_SUPPORTED );
  757. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  758. /* Add cases for opaque driver here */
  759. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  760. #if defined(PSA_CRYPTO_DRIVER_TEST)
  761. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  762. return( mbedtls_test_opaque_cipher_encrypt( attributes,
  763. key_buffer,
  764. key_buffer_size,
  765. alg,
  766. iv,
  767. iv_length,
  768. input,
  769. input_length,
  770. output,
  771. output_size,
  772. output_length ) );
  773. #endif /* PSA_CRYPTO_DRIVER_TEST */
  774. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  775. default:
  776. /* Key is declared with a lifetime not known to us */
  777. (void)status;
  778. (void)key_buffer;
  779. (void)key_buffer_size;
  780. (void)alg;
  781. (void)input;
  782. (void)input_length;
  783. (void)output;
  784. (void)output_size;
  785. (void)output_length;
  786. return( PSA_ERROR_INVALID_ARGUMENT );
  787. }
  788. }
  789. psa_status_t psa_driver_wrapper_cipher_decrypt(
  790. const psa_key_attributes_t *attributes,
  791. const uint8_t *key_buffer,
  792. size_t key_buffer_size,
  793. psa_algorithm_t alg,
  794. const uint8_t *input,
  795. size_t input_length,
  796. uint8_t *output,
  797. size_t output_size,
  798. size_t *output_length )
  799. {
  800. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  801. psa_key_location_t location =
  802. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  803. switch( location )
  804. {
  805. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  806. /* Key is stored in the slot in export representation, so
  807. * cycle through all known transparent accelerators */
  808. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  809. #if defined(PSA_CRYPTO_DRIVER_TEST)
  810. status = mbedtls_test_transparent_cipher_decrypt( attributes,
  811. key_buffer,
  812. key_buffer_size,
  813. alg,
  814. input,
  815. input_length,
  816. output,
  817. output_size,
  818. output_length );
  819. /* Declared with fallback == true */
  820. if( status != PSA_ERROR_NOT_SUPPORTED )
  821. return( status );
  822. #endif /* PSA_CRYPTO_DRIVER_TEST */
  823. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  824. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  825. return( mbedtls_psa_cipher_decrypt( attributes,
  826. key_buffer,
  827. key_buffer_size,
  828. alg,
  829. input,
  830. input_length,
  831. output,
  832. output_size,
  833. output_length ) );
  834. #else
  835. return( PSA_ERROR_NOT_SUPPORTED );
  836. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  837. /* Add cases for opaque driver here */
  838. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  839. #if defined(PSA_CRYPTO_DRIVER_TEST)
  840. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  841. return( mbedtls_test_opaque_cipher_decrypt( attributes,
  842. key_buffer,
  843. key_buffer_size,
  844. alg,
  845. input,
  846. input_length,
  847. output,
  848. output_size,
  849. output_length ) );
  850. #endif /* PSA_CRYPTO_DRIVER_TEST */
  851. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  852. default:
  853. /* Key is declared with a lifetime not known to us */
  854. (void)status;
  855. (void)key_buffer;
  856. (void)key_buffer_size;
  857. (void)alg;
  858. (void)input;
  859. (void)input_length;
  860. (void)output;
  861. (void)output_size;
  862. (void)output_length;
  863. return( PSA_ERROR_INVALID_ARGUMENT );
  864. }
  865. }
  866. psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
  867. psa_cipher_operation_t *operation,
  868. const psa_key_attributes_t *attributes,
  869. const uint8_t *key_buffer, size_t key_buffer_size,
  870. psa_algorithm_t alg )
  871. {
  872. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  873. psa_key_location_t location =
  874. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  875. switch( location )
  876. {
  877. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  878. /* Key is stored in the slot in export representation, so
  879. * cycle through all known transparent accelerators */
  880. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  881. #if defined(PSA_CRYPTO_DRIVER_TEST)
  882. status = mbedtls_test_transparent_cipher_encrypt_setup(
  883. &operation->ctx.transparent_test_driver_ctx,
  884. attributes,
  885. key_buffer,
  886. key_buffer_size,
  887. alg );
  888. /* Declared with fallback == true */
  889. if( status == PSA_SUCCESS )
  890. operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
  891. if( status != PSA_ERROR_NOT_SUPPORTED )
  892. return( status );
  893. #endif /* PSA_CRYPTO_DRIVER_TEST */
  894. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  895. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  896. /* Fell through, meaning no accelerator supports this operation */
  897. status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
  898. attributes,
  899. key_buffer,
  900. key_buffer_size,
  901. alg );
  902. if( status == PSA_SUCCESS )
  903. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  904. if( status != PSA_ERROR_NOT_SUPPORTED )
  905. return( status );
  906. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  907. return( PSA_ERROR_NOT_SUPPORTED );
  908. /* Add cases for opaque driver here */
  909. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  910. #if defined(PSA_CRYPTO_DRIVER_TEST)
  911. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  912. status = mbedtls_test_opaque_cipher_encrypt_setup(
  913. &operation->ctx.opaque_test_driver_ctx,
  914. attributes,
  915. key_buffer, key_buffer_size,
  916. alg );
  917. if( status == PSA_SUCCESS )
  918. operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
  919. return( status );
  920. #endif /* PSA_CRYPTO_DRIVER_TEST */
  921. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  922. default:
  923. /* Key is declared with a lifetime not known to us */
  924. (void)status;
  925. (void)operation;
  926. (void)key_buffer;
  927. (void)key_buffer_size;
  928. (void)alg;
  929. return( PSA_ERROR_INVALID_ARGUMENT );
  930. }
  931. }
  932. psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
  933. psa_cipher_operation_t *operation,
  934. const psa_key_attributes_t *attributes,
  935. const uint8_t *key_buffer, size_t key_buffer_size,
  936. psa_algorithm_t alg )
  937. {
  938. psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
  939. psa_key_location_t location =
  940. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  941. switch( location )
  942. {
  943. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  944. /* Key is stored in the slot in export representation, so
  945. * cycle through all known transparent accelerators */
  946. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  947. #if defined(PSA_CRYPTO_DRIVER_TEST)
  948. status = mbedtls_test_transparent_cipher_decrypt_setup(
  949. &operation->ctx.transparent_test_driver_ctx,
  950. attributes,
  951. key_buffer,
  952. key_buffer_size,
  953. alg );
  954. /* Declared with fallback == true */
  955. if( status == PSA_SUCCESS )
  956. operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
  957. if( status != PSA_ERROR_NOT_SUPPORTED )
  958. return( status );
  959. #endif /* PSA_CRYPTO_DRIVER_TEST */
  960. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  961. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  962. /* Fell through, meaning no accelerator supports this operation */
  963. status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
  964. attributes,
  965. key_buffer,
  966. key_buffer_size,
  967. alg );
  968. if( status == PSA_SUCCESS )
  969. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  970. return( status );
  971. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  972. return( PSA_ERROR_NOT_SUPPORTED );
  973. /* Add cases for opaque driver here */
  974. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  975. #if defined(PSA_CRYPTO_DRIVER_TEST)
  976. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  977. status = mbedtls_test_opaque_cipher_decrypt_setup(
  978. &operation->ctx.opaque_test_driver_ctx,
  979. attributes,
  980. key_buffer, key_buffer_size,
  981. alg );
  982. if( status == PSA_SUCCESS )
  983. operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
  984. return( status );
  985. #endif /* PSA_CRYPTO_DRIVER_TEST */
  986. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  987. default:
  988. /* Key is declared with a lifetime not known to us */
  989. (void)status;
  990. (void)operation;
  991. (void)key_buffer;
  992. (void)key_buffer_size;
  993. (void)alg;
  994. return( PSA_ERROR_INVALID_ARGUMENT );
  995. }
  996. }
  997. psa_status_t psa_driver_wrapper_cipher_set_iv(
  998. psa_cipher_operation_t *operation,
  999. const uint8_t *iv,
  1000. size_t iv_length )
  1001. {
  1002. switch( operation->id )
  1003. {
  1004. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  1005. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1006. return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
  1007. iv,
  1008. iv_length ) );
  1009. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1010. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1011. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1012. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1013. return( mbedtls_test_transparent_cipher_set_iv(
  1014. &operation->ctx.transparent_test_driver_ctx,
  1015. iv, iv_length ) );
  1016. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1017. return( mbedtls_test_opaque_cipher_set_iv(
  1018. &operation->ctx.opaque_test_driver_ctx,
  1019. iv, iv_length ) );
  1020. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1021. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1022. }
  1023. (void)iv;
  1024. (void)iv_length;
  1025. return( PSA_ERROR_INVALID_ARGUMENT );
  1026. }
  1027. psa_status_t psa_driver_wrapper_cipher_update(
  1028. psa_cipher_operation_t *operation,
  1029. const uint8_t *input,
  1030. size_t input_length,
  1031. uint8_t *output,
  1032. size_t output_size,
  1033. size_t *output_length )
  1034. {
  1035. switch( operation->id )
  1036. {
  1037. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  1038. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1039. return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
  1040. input,
  1041. input_length,
  1042. output,
  1043. output_size,
  1044. output_length ) );
  1045. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1046. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1047. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1048. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1049. return( mbedtls_test_transparent_cipher_update(
  1050. &operation->ctx.transparent_test_driver_ctx,
  1051. input, input_length,
  1052. output, output_size, output_length ) );
  1053. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1054. return( mbedtls_test_opaque_cipher_update(
  1055. &operation->ctx.opaque_test_driver_ctx,
  1056. input, input_length,
  1057. output, output_size, output_length ) );
  1058. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1059. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1060. }
  1061. (void)input;
  1062. (void)input_length;
  1063. (void)output;
  1064. (void)output_size;
  1065. (void)output_length;
  1066. return( PSA_ERROR_INVALID_ARGUMENT );
  1067. }
  1068. psa_status_t psa_driver_wrapper_cipher_finish(
  1069. psa_cipher_operation_t *operation,
  1070. uint8_t *output,
  1071. size_t output_size,
  1072. size_t *output_length )
  1073. {
  1074. switch( operation->id )
  1075. {
  1076. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  1077. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1078. return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
  1079. output,
  1080. output_size,
  1081. output_length ) );
  1082. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1083. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1084. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1085. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1086. return( mbedtls_test_transparent_cipher_finish(
  1087. &operation->ctx.transparent_test_driver_ctx,
  1088. output, output_size, output_length ) );
  1089. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1090. return( mbedtls_test_opaque_cipher_finish(
  1091. &operation->ctx.opaque_test_driver_ctx,
  1092. output, output_size, output_length ) );
  1093. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1094. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1095. }
  1096. (void)output;
  1097. (void)output_size;
  1098. (void)output_length;
  1099. return( PSA_ERROR_INVALID_ARGUMENT );
  1100. }
  1101. psa_status_t psa_driver_wrapper_cipher_abort(
  1102. psa_cipher_operation_t *operation )
  1103. {
  1104. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1105. switch( operation->id )
  1106. {
  1107. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  1108. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1109. return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
  1110. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1111. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1112. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1113. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1114. status = mbedtls_test_transparent_cipher_abort(
  1115. &operation->ctx.transparent_test_driver_ctx );
  1116. mbedtls_platform_zeroize(
  1117. &operation->ctx.transparent_test_driver_ctx,
  1118. sizeof( operation->ctx.transparent_test_driver_ctx ) );
  1119. return( status );
  1120. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1121. status = mbedtls_test_opaque_cipher_abort(
  1122. &operation->ctx.opaque_test_driver_ctx );
  1123. mbedtls_platform_zeroize(
  1124. &operation->ctx.opaque_test_driver_ctx,
  1125. sizeof( operation->ctx.opaque_test_driver_ctx ) );
  1126. return( status );
  1127. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1128. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1129. }
  1130. (void)status;
  1131. return( PSA_ERROR_INVALID_ARGUMENT );
  1132. }
  1133. /*
  1134. * Hashing functions
  1135. */
  1136. psa_status_t psa_driver_wrapper_hash_compute(
  1137. psa_algorithm_t alg,
  1138. const uint8_t *input,
  1139. size_t input_length,
  1140. uint8_t *hash,
  1141. size_t hash_size,
  1142. size_t *hash_length)
  1143. {
  1144. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1145. /* Try accelerators first */
  1146. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1147. status = mbedtls_test_transparent_hash_compute(
  1148. alg, input, input_length, hash, hash_size, hash_length );
  1149. if( status != PSA_ERROR_NOT_SUPPORTED )
  1150. return( status );
  1151. #endif
  1152. /* If software fallback is compiled in, try fallback */
  1153. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1154. status = mbedtls_psa_hash_compute( alg, input, input_length,
  1155. hash, hash_size, hash_length );
  1156. if( status != PSA_ERROR_NOT_SUPPORTED )
  1157. return( status );
  1158. #endif
  1159. (void) status;
  1160. (void) alg;
  1161. (void) input;
  1162. (void) input_length;
  1163. (void) hash;
  1164. (void) hash_size;
  1165. (void) hash_length;
  1166. return( PSA_ERROR_NOT_SUPPORTED );
  1167. }
  1168. psa_status_t psa_driver_wrapper_hash_setup(
  1169. psa_hash_operation_t *operation,
  1170. psa_algorithm_t alg )
  1171. {
  1172. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1173. /* Try setup on accelerators first */
  1174. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1175. status = mbedtls_test_transparent_hash_setup(
  1176. &operation->ctx.test_driver_ctx, alg );
  1177. if( status == PSA_SUCCESS )
  1178. operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
  1179. if( status != PSA_ERROR_NOT_SUPPORTED )
  1180. return( status );
  1181. #endif
  1182. /* If software fallback is compiled in, try fallback */
  1183. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1184. status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
  1185. if( status == PSA_SUCCESS )
  1186. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1187. if( status != PSA_ERROR_NOT_SUPPORTED )
  1188. return( status );
  1189. #endif
  1190. /* Nothing left to try if we fall through here */
  1191. (void) status;
  1192. (void) operation;
  1193. (void) alg;
  1194. return( PSA_ERROR_NOT_SUPPORTED );
  1195. }
  1196. psa_status_t psa_driver_wrapper_hash_clone(
  1197. const psa_hash_operation_t *source_operation,
  1198. psa_hash_operation_t *target_operation )
  1199. {
  1200. switch( source_operation->id )
  1201. {
  1202. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1203. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1204. target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1205. return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
  1206. &target_operation->ctx.mbedtls_ctx ) );
  1207. #endif
  1208. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1209. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1210. target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
  1211. return( mbedtls_test_transparent_hash_clone(
  1212. &source_operation->ctx.test_driver_ctx,
  1213. &target_operation->ctx.test_driver_ctx ) );
  1214. #endif
  1215. default:
  1216. (void) target_operation;
  1217. return( PSA_ERROR_BAD_STATE );
  1218. }
  1219. }
  1220. psa_status_t psa_driver_wrapper_hash_update(
  1221. psa_hash_operation_t *operation,
  1222. const uint8_t *input,
  1223. size_t input_length )
  1224. {
  1225. switch( operation->id )
  1226. {
  1227. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1228. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1229. return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
  1230. input, input_length ) );
  1231. #endif
  1232. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1233. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1234. return( mbedtls_test_transparent_hash_update(
  1235. &operation->ctx.test_driver_ctx,
  1236. input, input_length ) );
  1237. #endif
  1238. default:
  1239. (void) input;
  1240. (void) input_length;
  1241. return( PSA_ERROR_BAD_STATE );
  1242. }
  1243. }
  1244. psa_status_t psa_driver_wrapper_hash_finish(
  1245. psa_hash_operation_t *operation,
  1246. uint8_t *hash,
  1247. size_t hash_size,
  1248. size_t *hash_length )
  1249. {
  1250. switch( operation->id )
  1251. {
  1252. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1253. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1254. return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
  1255. hash, hash_size, hash_length ) );
  1256. #endif
  1257. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1258. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1259. return( mbedtls_test_transparent_hash_finish(
  1260. &operation->ctx.test_driver_ctx,
  1261. hash, hash_size, hash_length ) );
  1262. #endif
  1263. default:
  1264. (void) hash;
  1265. (void) hash_size;
  1266. (void) hash_length;
  1267. return( PSA_ERROR_BAD_STATE );
  1268. }
  1269. }
  1270. psa_status_t psa_driver_wrapper_hash_abort(
  1271. psa_hash_operation_t *operation )
  1272. {
  1273. switch( operation->id )
  1274. {
  1275. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1276. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1277. return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
  1278. #endif
  1279. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1280. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1281. return( mbedtls_test_transparent_hash_abort(
  1282. &operation->ctx.test_driver_ctx ) );
  1283. #endif
  1284. default:
  1285. return( PSA_ERROR_BAD_STATE );
  1286. }
  1287. }
  1288. psa_status_t psa_driver_wrapper_aead_encrypt(
  1289. const psa_key_attributes_t *attributes,
  1290. const uint8_t *key_buffer, size_t key_buffer_size,
  1291. psa_algorithm_t alg,
  1292. const uint8_t *nonce, size_t nonce_length,
  1293. const uint8_t *additional_data, size_t additional_data_length,
  1294. const uint8_t *plaintext, size_t plaintext_length,
  1295. uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
  1296. {
  1297. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1298. psa_key_location_t location =
  1299. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  1300. switch( location )
  1301. {
  1302. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1303. /* Key is stored in the slot in export representation, so
  1304. * cycle through all known transparent accelerators */
  1305. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1306. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1307. status = mbedtls_test_transparent_aead_encrypt(
  1308. attributes, key_buffer, key_buffer_size,
  1309. alg,
  1310. nonce, nonce_length,
  1311. additional_data, additional_data_length,
  1312. plaintext, plaintext_length,
  1313. ciphertext, ciphertext_size, ciphertext_length );
  1314. /* Declared with fallback == true */
  1315. if( status != PSA_ERROR_NOT_SUPPORTED )
  1316. return( status );
  1317. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1318. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1319. /* Fell through, meaning no accelerator supports this operation */
  1320. return( mbedtls_psa_aead_encrypt(
  1321. attributes, key_buffer, key_buffer_size,
  1322. alg,
  1323. nonce, nonce_length,
  1324. additional_data, additional_data_length,
  1325. plaintext, plaintext_length,
  1326. ciphertext, ciphertext_size, ciphertext_length ) );
  1327. /* Add cases for opaque driver here */
  1328. default:
  1329. /* Key is declared with a lifetime not known to us */
  1330. (void)status;
  1331. return( PSA_ERROR_INVALID_ARGUMENT );
  1332. }
  1333. }
  1334. psa_status_t psa_driver_wrapper_aead_decrypt(
  1335. const psa_key_attributes_t *attributes,
  1336. const uint8_t *key_buffer, size_t key_buffer_size,
  1337. psa_algorithm_t alg,
  1338. const uint8_t *nonce, size_t nonce_length,
  1339. const uint8_t *additional_data, size_t additional_data_length,
  1340. const uint8_t *ciphertext, size_t ciphertext_length,
  1341. uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
  1342. {
  1343. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1344. psa_key_location_t location =
  1345. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  1346. switch( location )
  1347. {
  1348. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1349. /* Key is stored in the slot in export representation, so
  1350. * cycle through all known transparent accelerators */
  1351. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1352. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1353. status = mbedtls_test_transparent_aead_decrypt(
  1354. attributes, key_buffer, key_buffer_size,
  1355. alg,
  1356. nonce, nonce_length,
  1357. additional_data, additional_data_length,
  1358. ciphertext, ciphertext_length,
  1359. plaintext, plaintext_size, plaintext_length );
  1360. /* Declared with fallback == true */
  1361. if( status != PSA_ERROR_NOT_SUPPORTED )
  1362. return( status );
  1363. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1364. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1365. /* Fell through, meaning no accelerator supports this operation */
  1366. return( mbedtls_psa_aead_decrypt(
  1367. attributes, key_buffer, key_buffer_size,
  1368. alg,
  1369. nonce, nonce_length,
  1370. additional_data, additional_data_length,
  1371. ciphertext, ciphertext_length,
  1372. plaintext, plaintext_size, plaintext_length ) );
  1373. /* Add cases for opaque driver here */
  1374. default:
  1375. /* Key is declared with a lifetime not known to us */
  1376. (void)status;
  1377. return( PSA_ERROR_INVALID_ARGUMENT );
  1378. }
  1379. }
  1380. /*
  1381. * MAC functions
  1382. */
  1383. psa_status_t psa_driver_wrapper_mac_compute(
  1384. const psa_key_attributes_t *attributes,
  1385. const uint8_t *key_buffer,
  1386. size_t key_buffer_size,
  1387. psa_algorithm_t alg,
  1388. const uint8_t *input,
  1389. size_t input_length,
  1390. uint8_t *mac,
  1391. size_t mac_size,
  1392. size_t *mac_length )
  1393. {
  1394. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1395. psa_key_location_t location =
  1396. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  1397. switch( location )
  1398. {
  1399. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1400. /* Key is stored in the slot in export representation, so
  1401. * cycle through all known transparent accelerators */
  1402. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1403. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1404. status = mbedtls_test_transparent_mac_compute(
  1405. attributes, key_buffer, key_buffer_size, alg,
  1406. input, input_length,
  1407. mac, mac_size, mac_length );
  1408. /* Declared with fallback == true */
  1409. if( status != PSA_ERROR_NOT_SUPPORTED )
  1410. return( status );
  1411. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1412. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1413. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  1414. /* Fell through, meaning no accelerator supports this operation */
  1415. status = mbedtls_psa_mac_compute(
  1416. attributes, key_buffer, key_buffer_size, alg,
  1417. input, input_length,
  1418. mac, mac_size, mac_length );
  1419. if( status != PSA_ERROR_NOT_SUPPORTED )
  1420. return( status );
  1421. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  1422. return( PSA_ERROR_NOT_SUPPORTED );
  1423. /* Add cases for opaque driver here */
  1424. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1425. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1426. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  1427. status = mbedtls_test_opaque_mac_compute(
  1428. attributes, key_buffer, key_buffer_size, alg,
  1429. input, input_length,
  1430. mac, mac_size, mac_length );
  1431. return( status );
  1432. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1433. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1434. default:
  1435. /* Key is declared with a lifetime not known to us */
  1436. (void) key_buffer;
  1437. (void) key_buffer_size;
  1438. (void) alg;
  1439. (void) input;
  1440. (void) input_length;
  1441. (void) mac;
  1442. (void) mac_size;
  1443. (void) mac_length;
  1444. (void) status;
  1445. return( PSA_ERROR_INVALID_ARGUMENT );
  1446. }
  1447. }
  1448. psa_status_t psa_driver_wrapper_mac_sign_setup(
  1449. psa_mac_operation_t *operation,
  1450. const psa_key_attributes_t *attributes,
  1451. const uint8_t *key_buffer,
  1452. size_t key_buffer_size,
  1453. psa_algorithm_t alg )
  1454. {
  1455. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1456. psa_key_location_t location =
  1457. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  1458. switch( location )
  1459. {
  1460. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1461. /* Key is stored in the slot in export representation, so
  1462. * cycle through all known transparent accelerators */
  1463. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1464. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1465. status = mbedtls_test_transparent_mac_sign_setup(
  1466. &operation->ctx.transparent_test_driver_ctx,
  1467. attributes,
  1468. key_buffer, key_buffer_size,
  1469. alg );
  1470. /* Declared with fallback == true */
  1471. if( status == PSA_SUCCESS )
  1472. operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
  1473. if( status != PSA_ERROR_NOT_SUPPORTED )
  1474. return( status );
  1475. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1476. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1477. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  1478. /* Fell through, meaning no accelerator supports this operation */
  1479. status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx,
  1480. attributes,
  1481. key_buffer, key_buffer_size,
  1482. alg );
  1483. if( status == PSA_SUCCESS )
  1484. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1485. if( status != PSA_ERROR_NOT_SUPPORTED )
  1486. return( status );
  1487. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  1488. return( PSA_ERROR_NOT_SUPPORTED );
  1489. /* Add cases for opaque driver here */
  1490. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1491. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1492. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  1493. status = mbedtls_test_opaque_mac_sign_setup(
  1494. &operation->ctx.opaque_test_driver_ctx,
  1495. attributes,
  1496. key_buffer, key_buffer_size,
  1497. alg );
  1498. if( status == PSA_SUCCESS )
  1499. operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
  1500. return( status );
  1501. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1502. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1503. default:
  1504. /* Key is declared with a lifetime not known to us */
  1505. (void) status;
  1506. (void) operation;
  1507. (void) key_buffer;
  1508. (void) key_buffer_size;
  1509. (void) alg;
  1510. return( PSA_ERROR_INVALID_ARGUMENT );
  1511. }
  1512. }
  1513. psa_status_t psa_driver_wrapper_mac_verify_setup(
  1514. psa_mac_operation_t *operation,
  1515. const psa_key_attributes_t *attributes,
  1516. const uint8_t *key_buffer,
  1517. size_t key_buffer_size,
  1518. psa_algorithm_t alg )
  1519. {
  1520. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1521. psa_key_location_t location =
  1522. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  1523. switch( location )
  1524. {
  1525. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1526. /* Key is stored in the slot in export representation, so
  1527. * cycle through all known transparent accelerators */
  1528. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1529. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1530. status = mbedtls_test_transparent_mac_verify_setup(
  1531. &operation->ctx.transparent_test_driver_ctx,
  1532. attributes,
  1533. key_buffer, key_buffer_size,
  1534. alg );
  1535. /* Declared with fallback == true */
  1536. if( status == PSA_SUCCESS )
  1537. operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
  1538. if( status != PSA_ERROR_NOT_SUPPORTED )
  1539. return( status );
  1540. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1541. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1542. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  1543. /* Fell through, meaning no accelerator supports this operation */
  1544. status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx,
  1545. attributes,
  1546. key_buffer, key_buffer_size,
  1547. alg );
  1548. if( status == PSA_SUCCESS )
  1549. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1550. if( status != PSA_ERROR_NOT_SUPPORTED )
  1551. return( status );
  1552. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  1553. return( PSA_ERROR_NOT_SUPPORTED );
  1554. /* Add cases for opaque driver here */
  1555. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1556. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1557. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  1558. status = mbedtls_test_opaque_mac_verify_setup(
  1559. &operation->ctx.opaque_test_driver_ctx,
  1560. attributes,
  1561. key_buffer, key_buffer_size,
  1562. alg );
  1563. if( status == PSA_SUCCESS )
  1564. operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
  1565. return( status );
  1566. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1567. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1568. default:
  1569. /* Key is declared with a lifetime not known to us */
  1570. (void) status;
  1571. (void) operation;
  1572. (void) key_buffer;
  1573. (void) key_buffer_size;
  1574. (void) alg;
  1575. return( PSA_ERROR_INVALID_ARGUMENT );
  1576. }
  1577. }
  1578. psa_status_t psa_driver_wrapper_mac_update(
  1579. psa_mac_operation_t *operation,
  1580. const uint8_t *input,
  1581. size_t input_length )
  1582. {
  1583. switch( operation->id )
  1584. {
  1585. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  1586. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1587. return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx,
  1588. input, input_length ) );
  1589. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  1590. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1591. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1592. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1593. return( mbedtls_test_transparent_mac_update(
  1594. &operation->ctx.transparent_test_driver_ctx,
  1595. input, input_length ) );
  1596. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1597. return( mbedtls_test_opaque_mac_update(
  1598. &operation->ctx.opaque_test_driver_ctx,
  1599. input, input_length ) );
  1600. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1601. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1602. default:
  1603. (void) input;
  1604. (void) input_length;
  1605. return( PSA_ERROR_INVALID_ARGUMENT );
  1606. }
  1607. }
  1608. psa_status_t psa_driver_wrapper_mac_sign_finish(
  1609. psa_mac_operation_t *operation,
  1610. uint8_t *mac,
  1611. size_t mac_size,
  1612. size_t *mac_length )
  1613. {
  1614. switch( operation->id )
  1615. {
  1616. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  1617. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1618. return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx,
  1619. mac, mac_size, mac_length ) );
  1620. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  1621. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1622. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1623. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1624. return( mbedtls_test_transparent_mac_sign_finish(
  1625. &operation->ctx.transparent_test_driver_ctx,
  1626. mac, mac_size, mac_length ) );
  1627. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1628. return( mbedtls_test_opaque_mac_sign_finish(
  1629. &operation->ctx.opaque_test_driver_ctx,
  1630. mac, mac_size, mac_length ) );
  1631. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1632. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1633. default:
  1634. (void) mac;
  1635. (void) mac_size;
  1636. (void) mac_length;
  1637. return( PSA_ERROR_INVALID_ARGUMENT );
  1638. }
  1639. }
  1640. psa_status_t psa_driver_wrapper_mac_verify_finish(
  1641. psa_mac_operation_t *operation,
  1642. const uint8_t *mac,
  1643. size_t mac_length )
  1644. {
  1645. switch( operation->id )
  1646. {
  1647. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  1648. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1649. return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx,
  1650. mac, mac_length ) );
  1651. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  1652. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1653. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1654. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1655. return( mbedtls_test_transparent_mac_verify_finish(
  1656. &operation->ctx.transparent_test_driver_ctx,
  1657. mac, mac_length ) );
  1658. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1659. return( mbedtls_test_opaque_mac_verify_finish(
  1660. &operation->ctx.opaque_test_driver_ctx,
  1661. mac, mac_length ) );
  1662. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1663. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1664. default:
  1665. (void) mac;
  1666. (void) mac_length;
  1667. return( PSA_ERROR_INVALID_ARGUMENT );
  1668. }
  1669. }
  1670. psa_status_t psa_driver_wrapper_mac_abort(
  1671. psa_mac_operation_t *operation )
  1672. {
  1673. switch( operation->id )
  1674. {
  1675. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  1676. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1677. return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) );
  1678. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  1679. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1680. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1681. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1682. return( mbedtls_test_transparent_mac_abort(
  1683. &operation->ctx.transparent_test_driver_ctx ) );
  1684. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1685. return( mbedtls_test_opaque_mac_abort(
  1686. &operation->ctx.opaque_test_driver_ctx ) );
  1687. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1688. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1689. default:
  1690. return( PSA_ERROR_INVALID_ARGUMENT );
  1691. }
  1692. }
  1693. #endif /* MBEDTLS_PSA_CRYPTO_C */