psa_crypto_driver_wrappers.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  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)iv;
  782. (void)iv_length;
  783. (void)input;
  784. (void)input_length;
  785. (void)output;
  786. (void)output_size;
  787. (void)output_length;
  788. return( PSA_ERROR_INVALID_ARGUMENT );
  789. }
  790. }
  791. psa_status_t psa_driver_wrapper_cipher_decrypt(
  792. const psa_key_attributes_t *attributes,
  793. const uint8_t *key_buffer,
  794. size_t key_buffer_size,
  795. psa_algorithm_t alg,
  796. const uint8_t *input,
  797. size_t input_length,
  798. uint8_t *output,
  799. size_t output_size,
  800. size_t *output_length )
  801. {
  802. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  803. psa_key_location_t location =
  804. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  805. switch( location )
  806. {
  807. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  808. /* Key is stored in the slot in export representation, so
  809. * cycle through all known transparent accelerators */
  810. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  811. #if defined(PSA_CRYPTO_DRIVER_TEST)
  812. status = mbedtls_test_transparent_cipher_decrypt( attributes,
  813. key_buffer,
  814. key_buffer_size,
  815. alg,
  816. input,
  817. input_length,
  818. output,
  819. output_size,
  820. output_length );
  821. /* Declared with fallback == true */
  822. if( status != PSA_ERROR_NOT_SUPPORTED )
  823. return( status );
  824. #endif /* PSA_CRYPTO_DRIVER_TEST */
  825. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  826. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  827. return( mbedtls_psa_cipher_decrypt( attributes,
  828. key_buffer,
  829. key_buffer_size,
  830. alg,
  831. input,
  832. input_length,
  833. output,
  834. output_size,
  835. output_length ) );
  836. #else
  837. return( PSA_ERROR_NOT_SUPPORTED );
  838. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  839. /* Add cases for opaque driver here */
  840. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  841. #if defined(PSA_CRYPTO_DRIVER_TEST)
  842. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  843. return( mbedtls_test_opaque_cipher_decrypt( attributes,
  844. key_buffer,
  845. key_buffer_size,
  846. alg,
  847. input,
  848. input_length,
  849. output,
  850. output_size,
  851. output_length ) );
  852. #endif /* PSA_CRYPTO_DRIVER_TEST */
  853. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  854. default:
  855. /* Key is declared with a lifetime not known to us */
  856. (void)status;
  857. (void)key_buffer;
  858. (void)key_buffer_size;
  859. (void)alg;
  860. (void)input;
  861. (void)input_length;
  862. (void)output;
  863. (void)output_size;
  864. (void)output_length;
  865. return( PSA_ERROR_INVALID_ARGUMENT );
  866. }
  867. }
  868. psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
  869. psa_cipher_operation_t *operation,
  870. const psa_key_attributes_t *attributes,
  871. const uint8_t *key_buffer, size_t key_buffer_size,
  872. psa_algorithm_t alg )
  873. {
  874. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  875. psa_key_location_t location =
  876. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  877. switch( location )
  878. {
  879. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  880. /* Key is stored in the slot in export representation, so
  881. * cycle through all known transparent accelerators */
  882. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  883. #if defined(PSA_CRYPTO_DRIVER_TEST)
  884. status = mbedtls_test_transparent_cipher_encrypt_setup(
  885. &operation->ctx.transparent_test_driver_ctx,
  886. attributes,
  887. key_buffer,
  888. key_buffer_size,
  889. alg );
  890. /* Declared with fallback == true */
  891. if( status == PSA_SUCCESS )
  892. operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
  893. if( status != PSA_ERROR_NOT_SUPPORTED )
  894. return( status );
  895. #endif /* PSA_CRYPTO_DRIVER_TEST */
  896. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  897. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  898. /* Fell through, meaning no accelerator supports this operation */
  899. status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
  900. attributes,
  901. key_buffer,
  902. key_buffer_size,
  903. alg );
  904. if( status == PSA_SUCCESS )
  905. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  906. if( status != PSA_ERROR_NOT_SUPPORTED )
  907. return( status );
  908. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  909. return( PSA_ERROR_NOT_SUPPORTED );
  910. /* Add cases for opaque driver here */
  911. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  912. #if defined(PSA_CRYPTO_DRIVER_TEST)
  913. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  914. status = mbedtls_test_opaque_cipher_encrypt_setup(
  915. &operation->ctx.opaque_test_driver_ctx,
  916. attributes,
  917. key_buffer, key_buffer_size,
  918. alg );
  919. if( status == PSA_SUCCESS )
  920. operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
  921. return( status );
  922. #endif /* PSA_CRYPTO_DRIVER_TEST */
  923. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  924. default:
  925. /* Key is declared with a lifetime not known to us */
  926. (void)status;
  927. (void)operation;
  928. (void)key_buffer;
  929. (void)key_buffer_size;
  930. (void)alg;
  931. return( PSA_ERROR_INVALID_ARGUMENT );
  932. }
  933. }
  934. psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
  935. psa_cipher_operation_t *operation,
  936. const psa_key_attributes_t *attributes,
  937. const uint8_t *key_buffer, size_t key_buffer_size,
  938. psa_algorithm_t alg )
  939. {
  940. psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
  941. psa_key_location_t location =
  942. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  943. switch( location )
  944. {
  945. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  946. /* Key is stored in the slot in export representation, so
  947. * cycle through all known transparent accelerators */
  948. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  949. #if defined(PSA_CRYPTO_DRIVER_TEST)
  950. status = mbedtls_test_transparent_cipher_decrypt_setup(
  951. &operation->ctx.transparent_test_driver_ctx,
  952. attributes,
  953. key_buffer,
  954. key_buffer_size,
  955. alg );
  956. /* Declared with fallback == true */
  957. if( status == PSA_SUCCESS )
  958. operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
  959. if( status != PSA_ERROR_NOT_SUPPORTED )
  960. return( status );
  961. #endif /* PSA_CRYPTO_DRIVER_TEST */
  962. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  963. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  964. /* Fell through, meaning no accelerator supports this operation */
  965. status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
  966. attributes,
  967. key_buffer,
  968. key_buffer_size,
  969. alg );
  970. if( status == PSA_SUCCESS )
  971. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  972. return( status );
  973. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  974. return( PSA_ERROR_NOT_SUPPORTED );
  975. /* Add cases for opaque driver here */
  976. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  977. #if defined(PSA_CRYPTO_DRIVER_TEST)
  978. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  979. status = mbedtls_test_opaque_cipher_decrypt_setup(
  980. &operation->ctx.opaque_test_driver_ctx,
  981. attributes,
  982. key_buffer, key_buffer_size,
  983. alg );
  984. if( status == PSA_SUCCESS )
  985. operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
  986. return( status );
  987. #endif /* PSA_CRYPTO_DRIVER_TEST */
  988. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  989. default:
  990. /* Key is declared with a lifetime not known to us */
  991. (void)status;
  992. (void)operation;
  993. (void)key_buffer;
  994. (void)key_buffer_size;
  995. (void)alg;
  996. return( PSA_ERROR_INVALID_ARGUMENT );
  997. }
  998. }
  999. psa_status_t psa_driver_wrapper_cipher_set_iv(
  1000. psa_cipher_operation_t *operation,
  1001. const uint8_t *iv,
  1002. size_t iv_length )
  1003. {
  1004. switch( operation->id )
  1005. {
  1006. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  1007. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1008. return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
  1009. iv,
  1010. iv_length ) );
  1011. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1012. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1013. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1014. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1015. return( mbedtls_test_transparent_cipher_set_iv(
  1016. &operation->ctx.transparent_test_driver_ctx,
  1017. iv, iv_length ) );
  1018. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1019. return( mbedtls_test_opaque_cipher_set_iv(
  1020. &operation->ctx.opaque_test_driver_ctx,
  1021. iv, iv_length ) );
  1022. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1023. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1024. }
  1025. (void)iv;
  1026. (void)iv_length;
  1027. return( PSA_ERROR_INVALID_ARGUMENT );
  1028. }
  1029. psa_status_t psa_driver_wrapper_cipher_update(
  1030. psa_cipher_operation_t *operation,
  1031. const uint8_t *input,
  1032. size_t input_length,
  1033. uint8_t *output,
  1034. size_t output_size,
  1035. size_t *output_length )
  1036. {
  1037. switch( operation->id )
  1038. {
  1039. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  1040. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1041. return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
  1042. input,
  1043. input_length,
  1044. output,
  1045. output_size,
  1046. output_length ) );
  1047. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1048. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1049. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1050. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1051. return( mbedtls_test_transparent_cipher_update(
  1052. &operation->ctx.transparent_test_driver_ctx,
  1053. input, input_length,
  1054. output, output_size, output_length ) );
  1055. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1056. return( mbedtls_test_opaque_cipher_update(
  1057. &operation->ctx.opaque_test_driver_ctx,
  1058. input, input_length,
  1059. output, output_size, output_length ) );
  1060. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1061. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1062. }
  1063. (void)input;
  1064. (void)input_length;
  1065. (void)output;
  1066. (void)output_size;
  1067. (void)output_length;
  1068. return( PSA_ERROR_INVALID_ARGUMENT );
  1069. }
  1070. psa_status_t psa_driver_wrapper_cipher_finish(
  1071. psa_cipher_operation_t *operation,
  1072. uint8_t *output,
  1073. size_t output_size,
  1074. size_t *output_length )
  1075. {
  1076. switch( operation->id )
  1077. {
  1078. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  1079. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1080. return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
  1081. output,
  1082. output_size,
  1083. output_length ) );
  1084. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1085. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1086. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1087. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1088. return( mbedtls_test_transparent_cipher_finish(
  1089. &operation->ctx.transparent_test_driver_ctx,
  1090. output, output_size, output_length ) );
  1091. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1092. return( mbedtls_test_opaque_cipher_finish(
  1093. &operation->ctx.opaque_test_driver_ctx,
  1094. output, output_size, output_length ) );
  1095. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1096. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1097. }
  1098. (void)output;
  1099. (void)output_size;
  1100. (void)output_length;
  1101. return( PSA_ERROR_INVALID_ARGUMENT );
  1102. }
  1103. psa_status_t psa_driver_wrapper_cipher_abort(
  1104. psa_cipher_operation_t *operation )
  1105. {
  1106. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1107. switch( operation->id )
  1108. {
  1109. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  1110. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1111. return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
  1112. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1113. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1114. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1115. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1116. status = mbedtls_test_transparent_cipher_abort(
  1117. &operation->ctx.transparent_test_driver_ctx );
  1118. mbedtls_platform_zeroize(
  1119. &operation->ctx.transparent_test_driver_ctx,
  1120. sizeof( operation->ctx.transparent_test_driver_ctx ) );
  1121. return( status );
  1122. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1123. status = mbedtls_test_opaque_cipher_abort(
  1124. &operation->ctx.opaque_test_driver_ctx );
  1125. mbedtls_platform_zeroize(
  1126. &operation->ctx.opaque_test_driver_ctx,
  1127. sizeof( operation->ctx.opaque_test_driver_ctx ) );
  1128. return( status );
  1129. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1130. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1131. }
  1132. (void)status;
  1133. return( PSA_ERROR_INVALID_ARGUMENT );
  1134. }
  1135. /*
  1136. * Hashing functions
  1137. */
  1138. psa_status_t psa_driver_wrapper_hash_compute(
  1139. psa_algorithm_t alg,
  1140. const uint8_t *input,
  1141. size_t input_length,
  1142. uint8_t *hash,
  1143. size_t hash_size,
  1144. size_t *hash_length)
  1145. {
  1146. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1147. /* Try accelerators first */
  1148. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1149. status = mbedtls_test_transparent_hash_compute(
  1150. alg, input, input_length, hash, hash_size, hash_length );
  1151. if( status != PSA_ERROR_NOT_SUPPORTED )
  1152. return( status );
  1153. #endif
  1154. /* If software fallback is compiled in, try fallback */
  1155. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1156. status = mbedtls_psa_hash_compute( alg, input, input_length,
  1157. hash, hash_size, hash_length );
  1158. if( status != PSA_ERROR_NOT_SUPPORTED )
  1159. return( status );
  1160. #endif
  1161. (void) status;
  1162. (void) alg;
  1163. (void) input;
  1164. (void) input_length;
  1165. (void) hash;
  1166. (void) hash_size;
  1167. (void) hash_length;
  1168. return( PSA_ERROR_NOT_SUPPORTED );
  1169. }
  1170. psa_status_t psa_driver_wrapper_hash_setup(
  1171. psa_hash_operation_t *operation,
  1172. psa_algorithm_t alg )
  1173. {
  1174. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1175. /* Try setup on accelerators first */
  1176. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1177. status = mbedtls_test_transparent_hash_setup(
  1178. &operation->ctx.test_driver_ctx, alg );
  1179. if( status == PSA_SUCCESS )
  1180. operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
  1181. if( status != PSA_ERROR_NOT_SUPPORTED )
  1182. return( status );
  1183. #endif
  1184. /* If software fallback is compiled in, try fallback */
  1185. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1186. status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
  1187. if( status == PSA_SUCCESS )
  1188. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1189. if( status != PSA_ERROR_NOT_SUPPORTED )
  1190. return( status );
  1191. #endif
  1192. /* Nothing left to try if we fall through here */
  1193. (void) status;
  1194. (void) operation;
  1195. (void) alg;
  1196. return( PSA_ERROR_NOT_SUPPORTED );
  1197. }
  1198. psa_status_t psa_driver_wrapper_hash_clone(
  1199. const psa_hash_operation_t *source_operation,
  1200. psa_hash_operation_t *target_operation )
  1201. {
  1202. switch( source_operation->id )
  1203. {
  1204. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1205. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1206. target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1207. return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
  1208. &target_operation->ctx.mbedtls_ctx ) );
  1209. #endif
  1210. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1211. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1212. target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
  1213. return( mbedtls_test_transparent_hash_clone(
  1214. &source_operation->ctx.test_driver_ctx,
  1215. &target_operation->ctx.test_driver_ctx ) );
  1216. #endif
  1217. default:
  1218. (void) target_operation;
  1219. return( PSA_ERROR_BAD_STATE );
  1220. }
  1221. }
  1222. psa_status_t psa_driver_wrapper_hash_update(
  1223. psa_hash_operation_t *operation,
  1224. const uint8_t *input,
  1225. size_t input_length )
  1226. {
  1227. switch( operation->id )
  1228. {
  1229. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1230. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1231. return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
  1232. input, input_length ) );
  1233. #endif
  1234. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1235. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1236. return( mbedtls_test_transparent_hash_update(
  1237. &operation->ctx.test_driver_ctx,
  1238. input, input_length ) );
  1239. #endif
  1240. default:
  1241. (void) input;
  1242. (void) input_length;
  1243. return( PSA_ERROR_BAD_STATE );
  1244. }
  1245. }
  1246. psa_status_t psa_driver_wrapper_hash_finish(
  1247. psa_hash_operation_t *operation,
  1248. uint8_t *hash,
  1249. size_t hash_size,
  1250. size_t *hash_length )
  1251. {
  1252. switch( operation->id )
  1253. {
  1254. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1255. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1256. return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
  1257. hash, hash_size, hash_length ) );
  1258. #endif
  1259. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1260. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1261. return( mbedtls_test_transparent_hash_finish(
  1262. &operation->ctx.test_driver_ctx,
  1263. hash, hash_size, hash_length ) );
  1264. #endif
  1265. default:
  1266. (void) hash;
  1267. (void) hash_size;
  1268. (void) hash_length;
  1269. return( PSA_ERROR_BAD_STATE );
  1270. }
  1271. }
  1272. psa_status_t psa_driver_wrapper_hash_abort(
  1273. psa_hash_operation_t *operation )
  1274. {
  1275. switch( operation->id )
  1276. {
  1277. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1278. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1279. return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
  1280. #endif
  1281. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1282. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1283. return( mbedtls_test_transparent_hash_abort(
  1284. &operation->ctx.test_driver_ctx ) );
  1285. #endif
  1286. default:
  1287. return( PSA_ERROR_BAD_STATE );
  1288. }
  1289. }
  1290. psa_status_t psa_driver_wrapper_aead_encrypt(
  1291. const psa_key_attributes_t *attributes,
  1292. const uint8_t *key_buffer, size_t key_buffer_size,
  1293. psa_algorithm_t alg,
  1294. const uint8_t *nonce, size_t nonce_length,
  1295. const uint8_t *additional_data, size_t additional_data_length,
  1296. const uint8_t *plaintext, size_t plaintext_length,
  1297. uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
  1298. {
  1299. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1300. psa_key_location_t location =
  1301. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  1302. switch( location )
  1303. {
  1304. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1305. /* Key is stored in the slot in export representation, so
  1306. * cycle through all known transparent accelerators */
  1307. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1308. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1309. status = mbedtls_test_transparent_aead_encrypt(
  1310. attributes, key_buffer, key_buffer_size,
  1311. alg,
  1312. nonce, nonce_length,
  1313. additional_data, additional_data_length,
  1314. plaintext, plaintext_length,
  1315. ciphertext, ciphertext_size, ciphertext_length );
  1316. /* Declared with fallback == true */
  1317. if( status != PSA_ERROR_NOT_SUPPORTED )
  1318. return( status );
  1319. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1320. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1321. /* Fell through, meaning no accelerator supports this operation */
  1322. return( mbedtls_psa_aead_encrypt(
  1323. attributes, key_buffer, key_buffer_size,
  1324. alg,
  1325. nonce, nonce_length,
  1326. additional_data, additional_data_length,
  1327. plaintext, plaintext_length,
  1328. ciphertext, ciphertext_size, ciphertext_length ) );
  1329. /* Add cases for opaque driver here */
  1330. default:
  1331. /* Key is declared with a lifetime not known to us */
  1332. (void)status;
  1333. return( PSA_ERROR_INVALID_ARGUMENT );
  1334. }
  1335. }
  1336. psa_status_t psa_driver_wrapper_aead_decrypt(
  1337. const psa_key_attributes_t *attributes,
  1338. const uint8_t *key_buffer, size_t key_buffer_size,
  1339. psa_algorithm_t alg,
  1340. const uint8_t *nonce, size_t nonce_length,
  1341. const uint8_t *additional_data, size_t additional_data_length,
  1342. const uint8_t *ciphertext, size_t ciphertext_length,
  1343. uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
  1344. {
  1345. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1346. psa_key_location_t location =
  1347. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  1348. switch( location )
  1349. {
  1350. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1351. /* Key is stored in the slot in export representation, so
  1352. * cycle through all known transparent accelerators */
  1353. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1354. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1355. status = mbedtls_test_transparent_aead_decrypt(
  1356. attributes, key_buffer, key_buffer_size,
  1357. alg,
  1358. nonce, nonce_length,
  1359. additional_data, additional_data_length,
  1360. ciphertext, ciphertext_length,
  1361. plaintext, plaintext_size, plaintext_length );
  1362. /* Declared with fallback == true */
  1363. if( status != PSA_ERROR_NOT_SUPPORTED )
  1364. return( status );
  1365. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1366. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1367. /* Fell through, meaning no accelerator supports this operation */
  1368. return( mbedtls_psa_aead_decrypt(
  1369. attributes, key_buffer, key_buffer_size,
  1370. alg,
  1371. nonce, nonce_length,
  1372. additional_data, additional_data_length,
  1373. ciphertext, ciphertext_length,
  1374. plaintext, plaintext_size, plaintext_length ) );
  1375. /* Add cases for opaque driver here */
  1376. default:
  1377. /* Key is declared with a lifetime not known to us */
  1378. (void)status;
  1379. return( PSA_ERROR_INVALID_ARGUMENT );
  1380. }
  1381. }
  1382. /*
  1383. * MAC functions
  1384. */
  1385. psa_status_t psa_driver_wrapper_mac_compute(
  1386. const psa_key_attributes_t *attributes,
  1387. const uint8_t *key_buffer,
  1388. size_t key_buffer_size,
  1389. psa_algorithm_t alg,
  1390. const uint8_t *input,
  1391. size_t input_length,
  1392. uint8_t *mac,
  1393. size_t mac_size,
  1394. size_t *mac_length )
  1395. {
  1396. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1397. psa_key_location_t location =
  1398. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  1399. switch( location )
  1400. {
  1401. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1402. /* Key is stored in the slot in export representation, so
  1403. * cycle through all known transparent accelerators */
  1404. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1405. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1406. status = mbedtls_test_transparent_mac_compute(
  1407. attributes, key_buffer, key_buffer_size, alg,
  1408. input, input_length,
  1409. mac, mac_size, mac_length );
  1410. /* Declared with fallback == true */
  1411. if( status != PSA_ERROR_NOT_SUPPORTED )
  1412. return( status );
  1413. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1414. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1415. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  1416. /* Fell through, meaning no accelerator supports this operation */
  1417. status = mbedtls_psa_mac_compute(
  1418. attributes, key_buffer, key_buffer_size, alg,
  1419. input, input_length,
  1420. mac, mac_size, mac_length );
  1421. if( status != PSA_ERROR_NOT_SUPPORTED )
  1422. return( status );
  1423. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  1424. return( PSA_ERROR_NOT_SUPPORTED );
  1425. /* Add cases for opaque driver here */
  1426. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1427. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1428. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  1429. status = mbedtls_test_opaque_mac_compute(
  1430. attributes, key_buffer, key_buffer_size, alg,
  1431. input, input_length,
  1432. mac, mac_size, mac_length );
  1433. return( status );
  1434. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1435. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1436. default:
  1437. /* Key is declared with a lifetime not known to us */
  1438. (void) key_buffer;
  1439. (void) key_buffer_size;
  1440. (void) alg;
  1441. (void) input;
  1442. (void) input_length;
  1443. (void) mac;
  1444. (void) mac_size;
  1445. (void) mac_length;
  1446. (void) status;
  1447. return( PSA_ERROR_INVALID_ARGUMENT );
  1448. }
  1449. }
  1450. psa_status_t psa_driver_wrapper_mac_sign_setup(
  1451. psa_mac_operation_t *operation,
  1452. const psa_key_attributes_t *attributes,
  1453. const uint8_t *key_buffer,
  1454. size_t key_buffer_size,
  1455. psa_algorithm_t alg )
  1456. {
  1457. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1458. psa_key_location_t location =
  1459. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  1460. switch( location )
  1461. {
  1462. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1463. /* Key is stored in the slot in export representation, so
  1464. * cycle through all known transparent accelerators */
  1465. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1466. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1467. status = mbedtls_test_transparent_mac_sign_setup(
  1468. &operation->ctx.transparent_test_driver_ctx,
  1469. attributes,
  1470. key_buffer, key_buffer_size,
  1471. alg );
  1472. /* Declared with fallback == true */
  1473. if( status == PSA_SUCCESS )
  1474. operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
  1475. if( status != PSA_ERROR_NOT_SUPPORTED )
  1476. return( status );
  1477. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1478. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1479. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  1480. /* Fell through, meaning no accelerator supports this operation */
  1481. status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx,
  1482. attributes,
  1483. key_buffer, key_buffer_size,
  1484. alg );
  1485. if( status == PSA_SUCCESS )
  1486. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1487. if( status != PSA_ERROR_NOT_SUPPORTED )
  1488. return( status );
  1489. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  1490. return( PSA_ERROR_NOT_SUPPORTED );
  1491. /* Add cases for opaque driver here */
  1492. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1493. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1494. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  1495. status = mbedtls_test_opaque_mac_sign_setup(
  1496. &operation->ctx.opaque_test_driver_ctx,
  1497. attributes,
  1498. key_buffer, key_buffer_size,
  1499. alg );
  1500. if( status == PSA_SUCCESS )
  1501. operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
  1502. return( status );
  1503. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1504. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1505. default:
  1506. /* Key is declared with a lifetime not known to us */
  1507. (void) status;
  1508. (void) operation;
  1509. (void) key_buffer;
  1510. (void) key_buffer_size;
  1511. (void) alg;
  1512. return( PSA_ERROR_INVALID_ARGUMENT );
  1513. }
  1514. }
  1515. psa_status_t psa_driver_wrapper_mac_verify_setup(
  1516. psa_mac_operation_t *operation,
  1517. const psa_key_attributes_t *attributes,
  1518. const uint8_t *key_buffer,
  1519. size_t key_buffer_size,
  1520. psa_algorithm_t alg )
  1521. {
  1522. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1523. psa_key_location_t location =
  1524. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  1525. switch( location )
  1526. {
  1527. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1528. /* Key is stored in the slot in export representation, so
  1529. * cycle through all known transparent accelerators */
  1530. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1531. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1532. status = mbedtls_test_transparent_mac_verify_setup(
  1533. &operation->ctx.transparent_test_driver_ctx,
  1534. attributes,
  1535. key_buffer, key_buffer_size,
  1536. alg );
  1537. /* Declared with fallback == true */
  1538. if( status == PSA_SUCCESS )
  1539. operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
  1540. if( status != PSA_ERROR_NOT_SUPPORTED )
  1541. return( status );
  1542. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1543. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1544. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  1545. /* Fell through, meaning no accelerator supports this operation */
  1546. status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx,
  1547. attributes,
  1548. key_buffer, key_buffer_size,
  1549. alg );
  1550. if( status == PSA_SUCCESS )
  1551. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1552. if( status != PSA_ERROR_NOT_SUPPORTED )
  1553. return( status );
  1554. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  1555. return( PSA_ERROR_NOT_SUPPORTED );
  1556. /* Add cases for opaque driver here */
  1557. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1558. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1559. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  1560. status = mbedtls_test_opaque_mac_verify_setup(
  1561. &operation->ctx.opaque_test_driver_ctx,
  1562. attributes,
  1563. key_buffer, key_buffer_size,
  1564. alg );
  1565. if( status == PSA_SUCCESS )
  1566. operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
  1567. return( status );
  1568. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1569. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1570. default:
  1571. /* Key is declared with a lifetime not known to us */
  1572. (void) status;
  1573. (void) operation;
  1574. (void) key_buffer;
  1575. (void) key_buffer_size;
  1576. (void) alg;
  1577. return( PSA_ERROR_INVALID_ARGUMENT );
  1578. }
  1579. }
  1580. psa_status_t psa_driver_wrapper_mac_update(
  1581. psa_mac_operation_t *operation,
  1582. const uint8_t *input,
  1583. size_t input_length )
  1584. {
  1585. switch( operation->id )
  1586. {
  1587. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  1588. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1589. return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx,
  1590. input, input_length ) );
  1591. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  1592. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1593. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1594. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1595. return( mbedtls_test_transparent_mac_update(
  1596. &operation->ctx.transparent_test_driver_ctx,
  1597. input, input_length ) );
  1598. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1599. return( mbedtls_test_opaque_mac_update(
  1600. &operation->ctx.opaque_test_driver_ctx,
  1601. input, input_length ) );
  1602. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1603. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1604. default:
  1605. (void) input;
  1606. (void) input_length;
  1607. return( PSA_ERROR_INVALID_ARGUMENT );
  1608. }
  1609. }
  1610. psa_status_t psa_driver_wrapper_mac_sign_finish(
  1611. psa_mac_operation_t *operation,
  1612. uint8_t *mac,
  1613. size_t mac_size,
  1614. size_t *mac_length )
  1615. {
  1616. switch( operation->id )
  1617. {
  1618. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  1619. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1620. return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx,
  1621. mac, mac_size, mac_length ) );
  1622. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  1623. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1624. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1625. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1626. return( mbedtls_test_transparent_mac_sign_finish(
  1627. &operation->ctx.transparent_test_driver_ctx,
  1628. mac, mac_size, mac_length ) );
  1629. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1630. return( mbedtls_test_opaque_mac_sign_finish(
  1631. &operation->ctx.opaque_test_driver_ctx,
  1632. mac, mac_size, mac_length ) );
  1633. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1634. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1635. default:
  1636. (void) mac;
  1637. (void) mac_size;
  1638. (void) mac_length;
  1639. return( PSA_ERROR_INVALID_ARGUMENT );
  1640. }
  1641. }
  1642. psa_status_t psa_driver_wrapper_mac_verify_finish(
  1643. psa_mac_operation_t *operation,
  1644. const uint8_t *mac,
  1645. size_t mac_length )
  1646. {
  1647. switch( operation->id )
  1648. {
  1649. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  1650. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1651. return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx,
  1652. mac, mac_length ) );
  1653. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  1654. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1655. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1656. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1657. return( mbedtls_test_transparent_mac_verify_finish(
  1658. &operation->ctx.transparent_test_driver_ctx,
  1659. mac, mac_length ) );
  1660. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1661. return( mbedtls_test_opaque_mac_verify_finish(
  1662. &operation->ctx.opaque_test_driver_ctx,
  1663. mac, mac_length ) );
  1664. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1665. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1666. default:
  1667. (void) mac;
  1668. (void) mac_length;
  1669. return( PSA_ERROR_INVALID_ARGUMENT );
  1670. }
  1671. }
  1672. psa_status_t psa_driver_wrapper_mac_abort(
  1673. psa_mac_operation_t *operation )
  1674. {
  1675. switch( operation->id )
  1676. {
  1677. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  1678. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1679. return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) );
  1680. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  1681. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1682. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1683. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1684. return( mbedtls_test_transparent_mac_abort(
  1685. &operation->ctx.transparent_test_driver_ctx ) );
  1686. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1687. return( mbedtls_test_opaque_mac_abort(
  1688. &operation->ctx.opaque_test_driver_ctx ) );
  1689. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1690. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1691. default:
  1692. return( PSA_ERROR_INVALID_ARGUMENT );
  1693. }
  1694. }
  1695. #endif /* MBEDTLS_PSA_CRYPTO_C */