psa_crypto_driver_wrappers.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  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 */