psa_crypto_driver_wrappers.c 68 KB

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