psa_crypto_driver_wrappers.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283
  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 "common.h"
  22. #include "psa_crypto_aead.h"
  23. #include "psa_crypto_cipher.h"
  24. #include "psa_crypto_core.h"
  25. #include "psa_crypto_driver_wrappers.h"
  26. #include "psa_crypto_hash.h"
  27. #include "psa_crypto_mac.h"
  28. #include "mbedtls/platform.h"
  29. #if defined(MBEDTLS_PSA_CRYPTO_C)
  30. #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
  31. /* Include test driver definition when running tests */
  32. #if defined(PSA_CRYPTO_DRIVER_TEST)
  33. #ifndef PSA_CRYPTO_DRIVER_PRESENT
  34. #define PSA_CRYPTO_DRIVER_PRESENT
  35. #endif
  36. #ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
  37. #define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
  38. #endif
  39. #include "test/drivers/test_driver.h"
  40. #endif /* PSA_CRYPTO_DRIVER_TEST */
  41. /* Repeat above block for each JSON-declared driver during autogeneration */
  42. #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
  43. /* Auto-generated values depending on which drivers are registered.
  44. * ID 0 is reserved for unallocated operations.
  45. * ID 1 is reserved for the Mbed TLS software driver. */
  46. #define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
  47. #if defined(PSA_CRYPTO_DRIVER_TEST)
  48. #define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2)
  49. #define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3)
  50. #endif /* PSA_CRYPTO_DRIVER_TEST */
  51. /* Support the 'old' SE interface when asked to */
  52. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  53. /* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
  54. * SE driver is present, to avoid unused argument errors at compile time. */
  55. #ifndef PSA_CRYPTO_DRIVER_PRESENT
  56. #define PSA_CRYPTO_DRIVER_PRESENT
  57. #endif
  58. #include "psa_crypto_se.h"
  59. #endif
  60. /* Start delegation functions */
  61. psa_status_t psa_driver_wrapper_sign_message(
  62. const psa_key_attributes_t *attributes,
  63. const uint8_t *key_buffer,
  64. size_t key_buffer_size,
  65. psa_algorithm_t alg,
  66. const uint8_t *input,
  67. size_t input_length,
  68. uint8_t *signature,
  69. size_t signature_size,
  70. size_t *signature_length )
  71. {
  72. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  73. psa_key_location_t location =
  74. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  75. switch( location )
  76. {
  77. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  78. /* Key is stored in the slot in export representation, so
  79. * cycle through all known transparent accelerators */
  80. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  81. #if defined(PSA_CRYPTO_DRIVER_TEST)
  82. status = mbedtls_test_transparent_signature_sign_message(
  83. attributes,
  84. key_buffer,
  85. key_buffer_size,
  86. alg,
  87. input,
  88. input_length,
  89. signature,
  90. signature_size,
  91. signature_length );
  92. /* Declared with fallback == true */
  93. if( status != PSA_ERROR_NOT_SUPPORTED )
  94. return( status );
  95. #endif /* PSA_CRYPTO_DRIVER_TEST */
  96. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  97. break;
  98. /* Add cases for opaque driver here */
  99. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  100. #if defined(PSA_CRYPTO_DRIVER_TEST)
  101. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  102. status = mbedtls_test_opaque_signature_sign_message(
  103. attributes,
  104. key_buffer,
  105. key_buffer_size,
  106. alg,
  107. input,
  108. input_length,
  109. signature,
  110. signature_size,
  111. signature_length );
  112. if( status != PSA_ERROR_NOT_SUPPORTED )
  113. return( status );
  114. break;
  115. #endif /* PSA_CRYPTO_DRIVER_TEST */
  116. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  117. default:
  118. /* Key is declared with a lifetime not known to us */
  119. (void)status;
  120. break;
  121. }
  122. return( psa_sign_message_builtin( attributes,
  123. key_buffer,
  124. key_buffer_size,
  125. alg,
  126. input,
  127. input_length,
  128. signature,
  129. signature_size,
  130. signature_length ) );
  131. }
  132. psa_status_t psa_driver_wrapper_verify_message(
  133. const psa_key_attributes_t *attributes,
  134. const uint8_t *key_buffer,
  135. size_t key_buffer_size,
  136. psa_algorithm_t alg,
  137. const uint8_t *input,
  138. size_t input_length,
  139. const uint8_t *signature,
  140. size_t signature_length )
  141. {
  142. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  143. psa_key_location_t location =
  144. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  145. switch( location )
  146. {
  147. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  148. /* Key is stored in the slot in export representation, so
  149. * cycle through all known transparent accelerators */
  150. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  151. #if defined(PSA_CRYPTO_DRIVER_TEST)
  152. status = mbedtls_test_transparent_signature_verify_message(
  153. attributes,
  154. key_buffer,
  155. key_buffer_size,
  156. alg,
  157. input,
  158. input_length,
  159. signature,
  160. signature_length );
  161. /* Declared with fallback == true */
  162. if( status != PSA_ERROR_NOT_SUPPORTED )
  163. return( status );
  164. #endif /* PSA_CRYPTO_DRIVER_TEST */
  165. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  166. break;
  167. /* Add cases for opaque driver here */
  168. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  169. #if defined(PSA_CRYPTO_DRIVER_TEST)
  170. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  171. return( mbedtls_test_opaque_signature_verify_message(
  172. attributes,
  173. key_buffer,
  174. key_buffer_size,
  175. alg,
  176. input,
  177. input_length,
  178. signature,
  179. signature_length ) );
  180. if( status != PSA_ERROR_NOT_SUPPORTED )
  181. return( status );
  182. break;
  183. #endif /* PSA_CRYPTO_DRIVER_TEST */
  184. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  185. default:
  186. /* Key is declared with a lifetime not known to us */
  187. (void)status;
  188. break;
  189. }
  190. return( psa_verify_message_builtin( attributes,
  191. key_buffer,
  192. key_buffer_size,
  193. alg,
  194. input,
  195. input_length,
  196. signature,
  197. signature_length ) );
  198. }
  199. psa_status_t psa_driver_wrapper_sign_hash(
  200. const psa_key_attributes_t *attributes,
  201. const uint8_t *key_buffer, size_t key_buffer_size,
  202. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  203. uint8_t *signature, size_t signature_size, size_t *signature_length )
  204. {
  205. /* Try dynamically-registered SE interface first */
  206. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  207. const psa_drv_se_t *drv;
  208. psa_drv_se_context_t *drv_context;
  209. if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
  210. {
  211. if( drv->asymmetric == NULL ||
  212. drv->asymmetric->p_sign == NULL )
  213. {
  214. /* Key is defined in SE, but we have no way to exercise it */
  215. return( PSA_ERROR_NOT_SUPPORTED );
  216. }
  217. return( drv->asymmetric->p_sign(
  218. drv_context, *( (psa_key_slot_number_t *)key_buffer ),
  219. alg, hash, hash_length,
  220. signature, signature_size, signature_length ) );
  221. }
  222. #endif /* PSA_CRYPTO_SE_C */
  223. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  224. psa_key_location_t location =
  225. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  226. switch( location )
  227. {
  228. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  229. /* Key is stored in the slot in export representation, so
  230. * cycle through all known transparent accelerators */
  231. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  232. #if defined(PSA_CRYPTO_DRIVER_TEST)
  233. status = mbedtls_test_transparent_signature_sign_hash( attributes,
  234. key_buffer,
  235. key_buffer_size,
  236. alg,
  237. hash,
  238. hash_length,
  239. signature,
  240. signature_size,
  241. signature_length );
  242. /* Declared with fallback == true */
  243. if( status != PSA_ERROR_NOT_SUPPORTED )
  244. return( status );
  245. #endif /* PSA_CRYPTO_DRIVER_TEST */
  246. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  247. /* Fell through, meaning no accelerator supports this operation */
  248. return( psa_sign_hash_builtin( attributes,
  249. key_buffer,
  250. key_buffer_size,
  251. alg,
  252. hash,
  253. hash_length,
  254. signature,
  255. signature_size,
  256. signature_length ) );
  257. /* Add cases for opaque driver here */
  258. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  259. #if defined(PSA_CRYPTO_DRIVER_TEST)
  260. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  261. return( mbedtls_test_opaque_signature_sign_hash( attributes,
  262. key_buffer,
  263. key_buffer_size,
  264. alg,
  265. hash,
  266. hash_length,
  267. signature,
  268. signature_size,
  269. signature_length ) );
  270. #endif /* PSA_CRYPTO_DRIVER_TEST */
  271. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  272. default:
  273. /* Key is declared with a lifetime not known to us */
  274. (void)status;
  275. return( PSA_ERROR_INVALID_ARGUMENT );
  276. }
  277. }
  278. psa_status_t psa_driver_wrapper_verify_hash(
  279. const psa_key_attributes_t *attributes,
  280. const uint8_t *key_buffer, size_t key_buffer_size,
  281. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  282. const uint8_t *signature, size_t signature_length )
  283. {
  284. /* Try dynamically-registered SE interface first */
  285. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  286. const psa_drv_se_t *drv;
  287. psa_drv_se_context_t *drv_context;
  288. if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
  289. {
  290. if( drv->asymmetric == NULL ||
  291. drv->asymmetric->p_verify == NULL )
  292. {
  293. /* Key is defined in SE, but we have no way to exercise it */
  294. return( PSA_ERROR_NOT_SUPPORTED );
  295. }
  296. return( drv->asymmetric->p_verify(
  297. drv_context, *( (psa_key_slot_number_t *)key_buffer ),
  298. alg, hash, hash_length,
  299. signature, signature_length ) );
  300. }
  301. #endif /* PSA_CRYPTO_SE_C */
  302. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  303. psa_key_location_t location =
  304. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  305. switch( location )
  306. {
  307. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  308. /* Key is stored in the slot in export representation, so
  309. * cycle through all known transparent accelerators */
  310. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  311. #if defined(PSA_CRYPTO_DRIVER_TEST)
  312. status = mbedtls_test_transparent_signature_verify_hash(
  313. attributes,
  314. key_buffer,
  315. key_buffer_size,
  316. alg,
  317. hash,
  318. hash_length,
  319. signature,
  320. signature_length );
  321. /* Declared with fallback == true */
  322. if( status != PSA_ERROR_NOT_SUPPORTED )
  323. return( status );
  324. #endif /* PSA_CRYPTO_DRIVER_TEST */
  325. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  326. return( psa_verify_hash_builtin( attributes,
  327. key_buffer,
  328. key_buffer_size,
  329. alg,
  330. hash,
  331. hash_length,
  332. signature,
  333. signature_length ) );
  334. /* Add cases for opaque driver here */
  335. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  336. #if defined(PSA_CRYPTO_DRIVER_TEST)
  337. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  338. return( mbedtls_test_opaque_signature_verify_hash( attributes,
  339. key_buffer,
  340. key_buffer_size,
  341. alg,
  342. hash,
  343. hash_length,
  344. signature,
  345. signature_length ) );
  346. #endif /* PSA_CRYPTO_DRIVER_TEST */
  347. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  348. default:
  349. /* Key is declared with a lifetime not known to us */
  350. (void)status;
  351. return( PSA_ERROR_INVALID_ARGUMENT );
  352. }
  353. }
  354. /** Calculate the key buffer size required to store the key material of a key
  355. * associated with an opaque driver from input key data.
  356. *
  357. * \param[in] attributes The key attributes
  358. * \param[in] data The input key data.
  359. * \param[in] data_length The input data length.
  360. * \param[out] key_buffer_size Minimum buffer size to contain the key material.
  361. *
  362. * \retval #PSA_SUCCESS
  363. * \retval #PSA_ERROR_INVALID_ARGUMENT
  364. * \retval #PSA_ERROR_NOT_SUPPORTED
  365. */
  366. psa_status_t psa_driver_wrapper_get_key_buffer_size_from_key_data(
  367. const psa_key_attributes_t *attributes,
  368. const uint8_t *data,
  369. size_t data_length,
  370. size_t *key_buffer_size )
  371. {
  372. psa_key_location_t location =
  373. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  374. psa_key_type_t key_type = attributes->core.type;
  375. *key_buffer_size = 0;
  376. switch( location )
  377. {
  378. #if defined(PSA_CRYPTO_DRIVER_TEST)
  379. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  380. *key_buffer_size = mbedtls_test_opaque_size_function( key_type,
  381. PSA_BYTES_TO_BITS( data_length ) );
  382. return( ( *key_buffer_size != 0 ) ?
  383. PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
  384. #endif /* PSA_CRYPTO_DRIVER_TEST */
  385. default:
  386. (void)key_type;
  387. (void)data;
  388. (void)data_length;
  389. return( PSA_ERROR_INVALID_ARGUMENT );
  390. }
  391. }
  392. /** Get the key buffer size required to store the key material of a key
  393. * associated with an opaque driver.
  394. *
  395. * \param[in] attributes The key attributes.
  396. * \param[out] key_buffer_size Minimum buffer size to contain the key material
  397. *
  398. * \retval #PSA_SUCCESS
  399. * The minimum size for a buffer to contain the key material has been
  400. * returned successfully.
  401. * \retval #PSA_ERROR_NOT_SUPPORTED
  402. * The type and/or the size in bits of the key or the combination of
  403. * the two is not supported.
  404. * \retval #PSA_ERROR_INVALID_ARGUMENT
  405. * The key is declared with a lifetime not known to us.
  406. */
  407. psa_status_t psa_driver_wrapper_get_key_buffer_size(
  408. const psa_key_attributes_t *attributes,
  409. size_t *key_buffer_size )
  410. {
  411. psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  412. psa_key_type_t key_type = attributes->core.type;
  413. size_t key_bits = attributes->core.bits;
  414. *key_buffer_size = 0;
  415. switch( location )
  416. {
  417. #if defined(PSA_CRYPTO_DRIVER_TEST)
  418. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  419. #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
  420. /* Emulate property 'builtin_key_size' */
  421. if( psa_key_id_is_builtin(
  422. MBEDTLS_SVC_KEY_ID_GET_KEY_ID(
  423. psa_get_key_id( attributes ) ) ) )
  424. {
  425. *key_buffer_size = sizeof( psa_drv_slot_number_t );
  426. return( PSA_SUCCESS );
  427. }
  428. #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
  429. *key_buffer_size = mbedtls_test_opaque_size_function( key_type,
  430. key_bits );
  431. return( ( *key_buffer_size != 0 ) ?
  432. PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
  433. #endif /* PSA_CRYPTO_DRIVER_TEST */
  434. default:
  435. (void)key_type;
  436. (void)key_bits;
  437. return( PSA_ERROR_INVALID_ARGUMENT );
  438. }
  439. }
  440. psa_status_t psa_driver_wrapper_generate_key(
  441. const psa_key_attributes_t *attributes,
  442. uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
  443. {
  444. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  445. psa_key_location_t location =
  446. PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
  447. /* Try dynamically-registered SE interface first */
  448. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  449. const psa_drv_se_t *drv;
  450. psa_drv_se_context_t *drv_context;
  451. if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
  452. {
  453. size_t pubkey_length = 0; /* We don't support this feature yet */
  454. if( drv->key_management == NULL ||
  455. drv->key_management->p_generate == NULL )
  456. {
  457. /* Key is defined as being in SE, but we have no way to generate it */
  458. return( PSA_ERROR_NOT_SUPPORTED );
  459. }
  460. return( drv->key_management->p_generate(
  461. drv_context,
  462. *( (psa_key_slot_number_t *)key_buffer ),
  463. attributes, NULL, 0, &pubkey_length ) );
  464. }
  465. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  466. switch( location )
  467. {
  468. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  469. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  470. /* Transparent drivers are limited to generating asymmetric keys */
  471. if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
  472. {
  473. /* Cycle through all known transparent accelerators */
  474. #if defined(PSA_CRYPTO_DRIVER_TEST)
  475. status = mbedtls_test_transparent_generate_key(
  476. attributes, key_buffer, key_buffer_size,
  477. key_buffer_length );
  478. /* Declared with fallback == true */
  479. if( status != PSA_ERROR_NOT_SUPPORTED )
  480. break;
  481. #endif /* PSA_CRYPTO_DRIVER_TEST */
  482. }
  483. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  484. /* Software fallback */
  485. status = psa_generate_key_internal(
  486. attributes, key_buffer, key_buffer_size, key_buffer_length );
  487. break;
  488. /* Add cases for opaque driver here */
  489. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  490. #if defined(PSA_CRYPTO_DRIVER_TEST)
  491. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  492. status = mbedtls_test_opaque_generate_key(
  493. attributes, key_buffer, key_buffer_size, key_buffer_length );
  494. break;
  495. #endif /* PSA_CRYPTO_DRIVER_TEST */
  496. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  497. default:
  498. /* Key is declared with a lifetime not known to us */
  499. status = PSA_ERROR_INVALID_ARGUMENT;
  500. break;
  501. }
  502. return( status );
  503. }
  504. psa_status_t psa_driver_wrapper_import_key(
  505. const psa_key_attributes_t *attributes,
  506. const uint8_t *data,
  507. size_t data_length,
  508. uint8_t *key_buffer,
  509. size_t key_buffer_size,
  510. size_t *key_buffer_length,
  511. size_t *bits )
  512. {
  513. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  514. psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
  515. psa_get_key_lifetime( attributes ) );
  516. /* Try dynamically-registered SE interface first */
  517. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  518. const psa_drv_se_t *drv;
  519. psa_drv_se_context_t *drv_context;
  520. if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
  521. {
  522. if( drv->key_management == NULL ||
  523. drv->key_management->p_import == NULL )
  524. return( PSA_ERROR_NOT_SUPPORTED );
  525. /* The driver should set the number of key bits, however in
  526. * case it doesn't, we initialize bits to an invalid value. */
  527. *bits = PSA_MAX_KEY_BITS + 1;
  528. status = drv->key_management->p_import(
  529. drv_context,
  530. *( (psa_key_slot_number_t *)key_buffer ),
  531. attributes, data, data_length, bits );
  532. if( status != PSA_SUCCESS )
  533. return( status );
  534. if( (*bits) > PSA_MAX_KEY_BITS )
  535. return( PSA_ERROR_NOT_SUPPORTED );
  536. return( PSA_SUCCESS );
  537. }
  538. #endif /* PSA_CRYPTO_SE_C */
  539. switch( location )
  540. {
  541. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  542. /* Key is stored in the slot in export representation, so
  543. * cycle through all known transparent accelerators */
  544. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  545. #if defined(PSA_CRYPTO_DRIVER_TEST)
  546. status = mbedtls_test_transparent_import_key(
  547. attributes,
  548. data, data_length,
  549. key_buffer, key_buffer_size,
  550. key_buffer_length, bits );
  551. /* Declared with fallback == true */
  552. if( status != PSA_ERROR_NOT_SUPPORTED )
  553. return( status );
  554. #endif /* PSA_CRYPTO_DRIVER_TEST */
  555. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  556. /* Fell through, meaning no accelerator supports this operation */
  557. return( psa_import_key_into_slot( attributes,
  558. data, data_length,
  559. key_buffer, key_buffer_size,
  560. key_buffer_length, bits ) );
  561. /* Add cases for opaque driver here */
  562. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  563. #if defined(PSA_CRYPTO_DRIVER_TEST)
  564. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  565. return( mbedtls_test_opaque_import_key(
  566. attributes,
  567. data, data_length,
  568. key_buffer, key_buffer_size,
  569. key_buffer_length, bits ) );
  570. #endif /* PSA_CRYPTO_DRIVER_TEST */
  571. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  572. default:
  573. (void)status;
  574. return( PSA_ERROR_INVALID_ARGUMENT );
  575. }
  576. }
  577. psa_status_t psa_driver_wrapper_export_key(
  578. const psa_key_attributes_t *attributes,
  579. const uint8_t *key_buffer, size_t key_buffer_size,
  580. uint8_t *data, size_t data_size, size_t *data_length )
  581. {
  582. psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
  583. psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
  584. psa_get_key_lifetime( attributes ) );
  585. /* Try dynamically-registered SE interface first */
  586. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  587. const psa_drv_se_t *drv;
  588. psa_drv_se_context_t *drv_context;
  589. if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
  590. {
  591. if( ( drv->key_management == NULL ) ||
  592. ( drv->key_management->p_export == NULL ) )
  593. {
  594. return( PSA_ERROR_NOT_SUPPORTED );
  595. }
  596. return( drv->key_management->p_export(
  597. drv_context,
  598. *( (psa_key_slot_number_t *)key_buffer ),
  599. data, data_size, data_length ) );
  600. }
  601. #endif /* PSA_CRYPTO_SE_C */
  602. switch( location )
  603. {
  604. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  605. return( psa_export_key_internal( attributes,
  606. key_buffer,
  607. key_buffer_size,
  608. data,
  609. data_size,
  610. data_length ) );
  611. /* Add cases for opaque driver here */
  612. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  613. #if defined(PSA_CRYPTO_DRIVER_TEST)
  614. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  615. return( mbedtls_test_opaque_export_key( attributes,
  616. key_buffer,
  617. key_buffer_size,
  618. data,
  619. data_size,
  620. data_length ) );
  621. #endif /* PSA_CRYPTO_DRIVER_TEST */
  622. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  623. default:
  624. /* Key is declared with a lifetime not known to us */
  625. return( status );
  626. }
  627. }
  628. psa_status_t psa_driver_wrapper_export_public_key(
  629. const psa_key_attributes_t *attributes,
  630. const uint8_t *key_buffer, size_t key_buffer_size,
  631. uint8_t *data, size_t data_size, size_t *data_length )
  632. {
  633. psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
  634. psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
  635. psa_get_key_lifetime( attributes ) );
  636. /* Try dynamically-registered SE interface first */
  637. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  638. const psa_drv_se_t *drv;
  639. psa_drv_se_context_t *drv_context;
  640. if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
  641. {
  642. if( ( drv->key_management == NULL ) ||
  643. ( drv->key_management->p_export_public == NULL ) )
  644. {
  645. return( PSA_ERROR_NOT_SUPPORTED );
  646. }
  647. return( drv->key_management->p_export_public(
  648. drv_context,
  649. *( (psa_key_slot_number_t *)key_buffer ),
  650. data, data_size, data_length ) );
  651. }
  652. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  653. switch( location )
  654. {
  655. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  656. /* Key is stored in the slot in export representation, so
  657. * cycle through all known transparent accelerators */
  658. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  659. #if defined(PSA_CRYPTO_DRIVER_TEST)
  660. status = mbedtls_test_transparent_export_public_key(
  661. attributes,
  662. key_buffer,
  663. key_buffer_size,
  664. data,
  665. data_size,
  666. data_length );
  667. /* Declared with fallback == true */
  668. if( status != PSA_ERROR_NOT_SUPPORTED )
  669. return( status );
  670. #endif /* PSA_CRYPTO_DRIVER_TEST */
  671. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  672. /* Fell through, meaning no accelerator supports this operation */
  673. return( psa_export_public_key_internal( attributes,
  674. key_buffer,
  675. key_buffer_size,
  676. data,
  677. data_size,
  678. data_length ) );
  679. /* Add cases for opaque driver here */
  680. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  681. #if defined(PSA_CRYPTO_DRIVER_TEST)
  682. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  683. return( mbedtls_test_opaque_export_public_key( attributes,
  684. key_buffer,
  685. key_buffer_size,
  686. data,
  687. data_size,
  688. data_length ) );
  689. #endif /* PSA_CRYPTO_DRIVER_TEST */
  690. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  691. default:
  692. /* Key is declared with a lifetime not known to us */
  693. return( status );
  694. }
  695. }
  696. psa_status_t psa_driver_wrapper_get_builtin_key(
  697. psa_drv_slot_number_t slot_number,
  698. psa_key_attributes_t *attributes,
  699. uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
  700. {
  701. psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  702. switch( location )
  703. {
  704. #if defined(PSA_CRYPTO_DRIVER_TEST)
  705. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  706. return( mbedtls_test_opaque_get_builtin_key(
  707. slot_number,
  708. attributes,
  709. key_buffer, key_buffer_size, key_buffer_length ) );
  710. #endif /* PSA_CRYPTO_DRIVER_TEST */
  711. default:
  712. (void) slot_number;
  713. (void) key_buffer;
  714. (void) key_buffer_size;
  715. (void) key_buffer_length;
  716. return( PSA_ERROR_DOES_NOT_EXIST );
  717. }
  718. }
  719. psa_status_t psa_driver_wrapper_copy_key(
  720. psa_key_attributes_t *attributes,
  721. const uint8_t *source_key, size_t source_key_length,
  722. uint8_t *target_key_buffer, size_t target_key_buffer_size,
  723. size_t *target_key_buffer_length )
  724. {
  725. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  726. psa_key_location_t location =
  727. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  728. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  729. const psa_drv_se_t *drv;
  730. psa_drv_se_context_t *drv_context;
  731. if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
  732. {
  733. /* Copying to a secure element is not implemented yet. */
  734. return( PSA_ERROR_NOT_SUPPORTED );
  735. }
  736. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  737. switch( location )
  738. {
  739. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  740. #if defined(PSA_CRYPTO_DRIVER_TEST)
  741. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  742. return( mbedtls_test_opaque_copy_key( attributes, source_key,
  743. source_key_length,
  744. target_key_buffer,
  745. target_key_buffer_size,
  746. target_key_buffer_length) );
  747. #endif /* PSA_CRYPTO_DRIVER_TEST */
  748. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  749. default:
  750. (void)source_key;
  751. (void)source_key_length;
  752. (void)target_key_buffer;
  753. (void)target_key_buffer_size;
  754. (void)target_key_buffer_length;
  755. status = PSA_ERROR_INVALID_ARGUMENT;
  756. }
  757. return( status );
  758. }
  759. /*
  760. * Cipher functions
  761. */
  762. psa_status_t psa_driver_wrapper_cipher_encrypt(
  763. const psa_key_attributes_t *attributes,
  764. const uint8_t *key_buffer,
  765. size_t key_buffer_size,
  766. psa_algorithm_t alg,
  767. const uint8_t *input,
  768. size_t input_length,
  769. uint8_t *output,
  770. size_t output_size,
  771. size_t *output_length )
  772. {
  773. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  774. psa_key_location_t location =
  775. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  776. switch( location )
  777. {
  778. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  779. /* Key is stored in the slot in export representation, so
  780. * cycle through all known transparent accelerators */
  781. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  782. #if defined(PSA_CRYPTO_DRIVER_TEST)
  783. status = mbedtls_test_transparent_cipher_encrypt( attributes,
  784. key_buffer,
  785. key_buffer_size,
  786. alg,
  787. input,
  788. input_length,
  789. output,
  790. output_size,
  791. output_length );
  792. /* Declared with fallback == true */
  793. if( status != PSA_ERROR_NOT_SUPPORTED )
  794. return( status );
  795. #endif /* PSA_CRYPTO_DRIVER_TEST */
  796. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  797. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  798. return( mbedtls_psa_cipher_encrypt( attributes,
  799. key_buffer,
  800. key_buffer_size,
  801. alg,
  802. input,
  803. input_length,
  804. output,
  805. output_size,
  806. output_length ) );
  807. #else
  808. return( PSA_ERROR_NOT_SUPPORTED );
  809. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  810. /* Add cases for opaque driver here */
  811. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  812. #if defined(PSA_CRYPTO_DRIVER_TEST)
  813. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  814. return( mbedtls_test_opaque_cipher_encrypt( attributes,
  815. key_buffer,
  816. key_buffer_size,
  817. alg,
  818. input,
  819. input_length,
  820. output,
  821. output_size,
  822. output_length ) );
  823. #endif /* PSA_CRYPTO_DRIVER_TEST */
  824. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  825. default:
  826. /* Key is declared with a lifetime not known to us */
  827. (void)status;
  828. (void)key_buffer;
  829. (void)key_buffer_size;
  830. (void)alg;
  831. (void)input;
  832. (void)input_length;
  833. (void)output;
  834. (void)output_size;
  835. (void)output_length;
  836. return( PSA_ERROR_INVALID_ARGUMENT );
  837. }
  838. }
  839. psa_status_t psa_driver_wrapper_cipher_decrypt(
  840. const psa_key_attributes_t *attributes,
  841. const uint8_t *key_buffer,
  842. size_t key_buffer_size,
  843. psa_algorithm_t alg,
  844. const uint8_t *input,
  845. size_t input_length,
  846. uint8_t *output,
  847. size_t output_size,
  848. size_t *output_length )
  849. {
  850. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  851. psa_key_location_t location =
  852. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  853. switch( location )
  854. {
  855. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  856. /* Key is stored in the slot in export representation, so
  857. * cycle through all known transparent accelerators */
  858. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  859. #if defined(PSA_CRYPTO_DRIVER_TEST)
  860. status = mbedtls_test_transparent_cipher_decrypt( attributes,
  861. key_buffer,
  862. key_buffer_size,
  863. alg,
  864. input,
  865. input_length,
  866. output,
  867. output_size,
  868. output_length );
  869. /* Declared with fallback == true */
  870. if( status != PSA_ERROR_NOT_SUPPORTED )
  871. return( status );
  872. #endif /* PSA_CRYPTO_DRIVER_TEST */
  873. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  874. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  875. return( mbedtls_psa_cipher_decrypt( attributes,
  876. key_buffer,
  877. key_buffer_size,
  878. alg,
  879. input,
  880. input_length,
  881. output,
  882. output_size,
  883. output_length ) );
  884. #else
  885. return( PSA_ERROR_NOT_SUPPORTED );
  886. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  887. /* Add cases for opaque driver here */
  888. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  889. #if defined(PSA_CRYPTO_DRIVER_TEST)
  890. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  891. return( mbedtls_test_opaque_cipher_decrypt( attributes,
  892. key_buffer,
  893. key_buffer_size,
  894. alg,
  895. input,
  896. input_length,
  897. output,
  898. output_size,
  899. output_length ) );
  900. #endif /* PSA_CRYPTO_DRIVER_TEST */
  901. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  902. default:
  903. /* Key is declared with a lifetime not known to us */
  904. (void)status;
  905. (void)key_buffer;
  906. (void)key_buffer_size;
  907. (void)alg;
  908. (void)input;
  909. (void)input_length;
  910. (void)output;
  911. (void)output_size;
  912. (void)output_length;
  913. return( PSA_ERROR_INVALID_ARGUMENT );
  914. }
  915. }
  916. psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
  917. psa_cipher_operation_t *operation,
  918. const psa_key_attributes_t *attributes,
  919. const uint8_t *key_buffer, size_t key_buffer_size,
  920. psa_algorithm_t alg )
  921. {
  922. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  923. psa_key_location_t location =
  924. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  925. switch( location )
  926. {
  927. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  928. /* Key is stored in the slot in export representation, so
  929. * cycle through all known transparent accelerators */
  930. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  931. #if defined(PSA_CRYPTO_DRIVER_TEST)
  932. status = mbedtls_test_transparent_cipher_encrypt_setup(
  933. &operation->ctx.transparent_test_driver_ctx,
  934. attributes,
  935. key_buffer,
  936. key_buffer_size,
  937. alg );
  938. /* Declared with fallback == true */
  939. if( status == PSA_SUCCESS )
  940. operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
  941. if( status != PSA_ERROR_NOT_SUPPORTED )
  942. return( status );
  943. #endif /* PSA_CRYPTO_DRIVER_TEST */
  944. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  945. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  946. /* Fell through, meaning no accelerator supports this operation */
  947. status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
  948. attributes,
  949. key_buffer,
  950. key_buffer_size,
  951. alg );
  952. if( status == PSA_SUCCESS )
  953. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  954. if( status != PSA_ERROR_NOT_SUPPORTED )
  955. return( status );
  956. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  957. return( PSA_ERROR_NOT_SUPPORTED );
  958. /* Add cases for opaque driver here */
  959. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  960. #if defined(PSA_CRYPTO_DRIVER_TEST)
  961. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  962. status = mbedtls_test_opaque_cipher_encrypt_setup(
  963. &operation->ctx.opaque_test_driver_ctx,
  964. attributes,
  965. key_buffer, key_buffer_size,
  966. alg );
  967. if( status == PSA_SUCCESS )
  968. operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
  969. return( status );
  970. #endif /* PSA_CRYPTO_DRIVER_TEST */
  971. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  972. default:
  973. /* Key is declared with a lifetime not known to us */
  974. (void)status;
  975. (void)key_buffer;
  976. (void)key_buffer_size;
  977. (void)alg;
  978. return( PSA_ERROR_INVALID_ARGUMENT );
  979. }
  980. }
  981. psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
  982. psa_cipher_operation_t *operation,
  983. const psa_key_attributes_t *attributes,
  984. const uint8_t *key_buffer, size_t key_buffer_size,
  985. psa_algorithm_t alg )
  986. {
  987. psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
  988. psa_key_location_t location =
  989. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  990. switch( location )
  991. {
  992. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  993. /* Key is stored in the slot in export representation, so
  994. * cycle through all known transparent accelerators */
  995. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  996. #if defined(PSA_CRYPTO_DRIVER_TEST)
  997. status = mbedtls_test_transparent_cipher_decrypt_setup(
  998. &operation->ctx.transparent_test_driver_ctx,
  999. attributes,
  1000. key_buffer,
  1001. key_buffer_size,
  1002. alg );
  1003. /* Declared with fallback == true */
  1004. if( status == PSA_SUCCESS )
  1005. operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
  1006. if( status != PSA_ERROR_NOT_SUPPORTED )
  1007. return( status );
  1008. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1009. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1010. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  1011. /* Fell through, meaning no accelerator supports this operation */
  1012. status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
  1013. attributes,
  1014. key_buffer,
  1015. key_buffer_size,
  1016. alg );
  1017. if( status == PSA_SUCCESS )
  1018. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1019. return( status );
  1020. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1021. return( PSA_ERROR_NOT_SUPPORTED );
  1022. /* Add cases for opaque driver here */
  1023. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1024. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1025. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  1026. status = mbedtls_test_opaque_cipher_decrypt_setup(
  1027. &operation->ctx.opaque_test_driver_ctx,
  1028. attributes,
  1029. key_buffer, key_buffer_size,
  1030. alg );
  1031. if( status == PSA_SUCCESS )
  1032. operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
  1033. return( status );
  1034. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1035. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1036. default:
  1037. /* Key is declared with a lifetime not known to us */
  1038. (void)status;
  1039. (void)key_buffer;
  1040. (void)key_buffer_size;
  1041. (void)alg;
  1042. return( PSA_ERROR_INVALID_ARGUMENT );
  1043. }
  1044. }
  1045. psa_status_t psa_driver_wrapper_cipher_set_iv(
  1046. psa_cipher_operation_t *operation,
  1047. const uint8_t *iv,
  1048. size_t iv_length )
  1049. {
  1050. switch( operation->id )
  1051. {
  1052. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  1053. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1054. return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
  1055. iv,
  1056. iv_length ) );
  1057. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1058. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1059. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1060. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1061. return( mbedtls_test_transparent_cipher_set_iv(
  1062. &operation->ctx.transparent_test_driver_ctx,
  1063. iv, iv_length ) );
  1064. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1065. return( mbedtls_test_opaque_cipher_set_iv(
  1066. &operation->ctx.opaque_test_driver_ctx,
  1067. iv, iv_length ) );
  1068. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1069. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1070. }
  1071. (void)iv;
  1072. (void)iv_length;
  1073. return( PSA_ERROR_INVALID_ARGUMENT );
  1074. }
  1075. psa_status_t psa_driver_wrapper_cipher_update(
  1076. psa_cipher_operation_t *operation,
  1077. const uint8_t *input,
  1078. size_t input_length,
  1079. uint8_t *output,
  1080. size_t output_size,
  1081. size_t *output_length )
  1082. {
  1083. switch( operation->id )
  1084. {
  1085. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  1086. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1087. return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
  1088. input,
  1089. input_length,
  1090. output,
  1091. output_size,
  1092. output_length ) );
  1093. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1094. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1095. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1096. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1097. return( mbedtls_test_transparent_cipher_update(
  1098. &operation->ctx.transparent_test_driver_ctx,
  1099. input, input_length,
  1100. output, output_size, output_length ) );
  1101. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1102. return( mbedtls_test_opaque_cipher_update(
  1103. &operation->ctx.opaque_test_driver_ctx,
  1104. input, input_length,
  1105. output, output_size, output_length ) );
  1106. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1107. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1108. }
  1109. (void)input;
  1110. (void)input_length;
  1111. (void)output;
  1112. (void)output_size;
  1113. (void)output_length;
  1114. return( PSA_ERROR_INVALID_ARGUMENT );
  1115. }
  1116. psa_status_t psa_driver_wrapper_cipher_finish(
  1117. psa_cipher_operation_t *operation,
  1118. uint8_t *output,
  1119. size_t output_size,
  1120. size_t *output_length )
  1121. {
  1122. switch( operation->id )
  1123. {
  1124. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  1125. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1126. return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
  1127. output,
  1128. output_size,
  1129. output_length ) );
  1130. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1131. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1132. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1133. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1134. return( mbedtls_test_transparent_cipher_finish(
  1135. &operation->ctx.transparent_test_driver_ctx,
  1136. output, output_size, output_length ) );
  1137. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1138. return( mbedtls_test_opaque_cipher_finish(
  1139. &operation->ctx.opaque_test_driver_ctx,
  1140. output, output_size, output_length ) );
  1141. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1142. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1143. }
  1144. (void)output;
  1145. (void)output_size;
  1146. (void)output_length;
  1147. return( PSA_ERROR_INVALID_ARGUMENT );
  1148. }
  1149. psa_status_t psa_driver_wrapper_cipher_abort(
  1150. psa_cipher_operation_t *operation )
  1151. {
  1152. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1153. switch( operation->id )
  1154. {
  1155. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  1156. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1157. return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
  1158. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  1159. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1160. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1161. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1162. status = mbedtls_test_transparent_cipher_abort(
  1163. &operation->ctx.transparent_test_driver_ctx );
  1164. mbedtls_platform_zeroize(
  1165. &operation->ctx.transparent_test_driver_ctx,
  1166. sizeof( operation->ctx.transparent_test_driver_ctx ) );
  1167. return( status );
  1168. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1169. status = mbedtls_test_opaque_cipher_abort(
  1170. &operation->ctx.opaque_test_driver_ctx );
  1171. mbedtls_platform_zeroize(
  1172. &operation->ctx.opaque_test_driver_ctx,
  1173. sizeof( operation->ctx.opaque_test_driver_ctx ) );
  1174. return( status );
  1175. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1176. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1177. }
  1178. (void)status;
  1179. return( PSA_ERROR_INVALID_ARGUMENT );
  1180. }
  1181. /*
  1182. * Hashing functions
  1183. */
  1184. psa_status_t psa_driver_wrapper_hash_compute(
  1185. psa_algorithm_t alg,
  1186. const uint8_t *input,
  1187. size_t input_length,
  1188. uint8_t *hash,
  1189. size_t hash_size,
  1190. size_t *hash_length)
  1191. {
  1192. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1193. /* Try accelerators first */
  1194. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1195. status = mbedtls_test_transparent_hash_compute(
  1196. alg, input, input_length, hash, hash_size, hash_length );
  1197. if( status != PSA_ERROR_NOT_SUPPORTED )
  1198. return( status );
  1199. #endif
  1200. /* If software fallback is compiled in, try fallback */
  1201. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1202. status = mbedtls_psa_hash_compute( alg, input, input_length,
  1203. hash, hash_size, hash_length );
  1204. if( status != PSA_ERROR_NOT_SUPPORTED )
  1205. return( status );
  1206. #endif
  1207. (void) status;
  1208. (void) alg;
  1209. (void) input;
  1210. (void) input_length;
  1211. (void) hash;
  1212. (void) hash_size;
  1213. (void) hash_length;
  1214. return( PSA_ERROR_NOT_SUPPORTED );
  1215. }
  1216. psa_status_t psa_driver_wrapper_hash_setup(
  1217. psa_hash_operation_t *operation,
  1218. psa_algorithm_t alg )
  1219. {
  1220. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1221. /* Try setup on accelerators first */
  1222. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1223. status = mbedtls_test_transparent_hash_setup(
  1224. &operation->ctx.test_driver_ctx, alg );
  1225. if( status == PSA_SUCCESS )
  1226. operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
  1227. if( status != PSA_ERROR_NOT_SUPPORTED )
  1228. return( status );
  1229. #endif
  1230. /* If software fallback is compiled in, try fallback */
  1231. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1232. status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
  1233. if( status == PSA_SUCCESS )
  1234. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1235. if( status != PSA_ERROR_NOT_SUPPORTED )
  1236. return( status );
  1237. #endif
  1238. /* Nothing left to try if we fall through here */
  1239. (void) status;
  1240. (void) operation;
  1241. (void) alg;
  1242. return( PSA_ERROR_NOT_SUPPORTED );
  1243. }
  1244. psa_status_t psa_driver_wrapper_hash_clone(
  1245. const psa_hash_operation_t *source_operation,
  1246. psa_hash_operation_t *target_operation )
  1247. {
  1248. switch( source_operation->id )
  1249. {
  1250. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1251. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1252. target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1253. return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
  1254. &target_operation->ctx.mbedtls_ctx ) );
  1255. #endif
  1256. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1257. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1258. target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
  1259. return( mbedtls_test_transparent_hash_clone(
  1260. &source_operation->ctx.test_driver_ctx,
  1261. &target_operation->ctx.test_driver_ctx ) );
  1262. #endif
  1263. default:
  1264. (void) target_operation;
  1265. return( PSA_ERROR_BAD_STATE );
  1266. }
  1267. }
  1268. psa_status_t psa_driver_wrapper_hash_update(
  1269. psa_hash_operation_t *operation,
  1270. const uint8_t *input,
  1271. size_t input_length )
  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_update( &operation->ctx.mbedtls_ctx,
  1278. input, input_length ) );
  1279. #endif
  1280. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1281. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1282. return( mbedtls_test_transparent_hash_update(
  1283. &operation->ctx.test_driver_ctx,
  1284. input, input_length ) );
  1285. #endif
  1286. default:
  1287. (void) input;
  1288. (void) input_length;
  1289. return( PSA_ERROR_BAD_STATE );
  1290. }
  1291. }
  1292. psa_status_t psa_driver_wrapper_hash_finish(
  1293. psa_hash_operation_t *operation,
  1294. uint8_t *hash,
  1295. size_t hash_size,
  1296. size_t *hash_length )
  1297. {
  1298. switch( operation->id )
  1299. {
  1300. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1301. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1302. return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
  1303. hash, hash_size, hash_length ) );
  1304. #endif
  1305. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1306. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1307. return( mbedtls_test_transparent_hash_finish(
  1308. &operation->ctx.test_driver_ctx,
  1309. hash, hash_size, hash_length ) );
  1310. #endif
  1311. default:
  1312. (void) hash;
  1313. (void) hash_size;
  1314. (void) hash_length;
  1315. return( PSA_ERROR_BAD_STATE );
  1316. }
  1317. }
  1318. psa_status_t psa_driver_wrapper_hash_abort(
  1319. psa_hash_operation_t *operation )
  1320. {
  1321. switch( operation->id )
  1322. {
  1323. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  1324. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1325. return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
  1326. #endif
  1327. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1328. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1329. return( mbedtls_test_transparent_hash_abort(
  1330. &operation->ctx.test_driver_ctx ) );
  1331. #endif
  1332. default:
  1333. return( PSA_ERROR_BAD_STATE );
  1334. }
  1335. }
  1336. psa_status_t psa_driver_wrapper_aead_encrypt(
  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 *plaintext, size_t plaintext_length,
  1343. uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_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_encrypt(
  1356. attributes, key_buffer, key_buffer_size,
  1357. alg,
  1358. nonce, nonce_length,
  1359. additional_data, additional_data_length,
  1360. plaintext, plaintext_length,
  1361. ciphertext, ciphertext_size, ciphertext_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_encrypt(
  1369. attributes, key_buffer, key_buffer_size,
  1370. alg,
  1371. nonce, nonce_length,
  1372. additional_data, additional_data_length,
  1373. plaintext, plaintext_length,
  1374. ciphertext, ciphertext_size, ciphertext_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. psa_status_t psa_driver_wrapper_aead_decrypt(
  1383. const psa_key_attributes_t *attributes,
  1384. const uint8_t *key_buffer, size_t key_buffer_size,
  1385. psa_algorithm_t alg,
  1386. const uint8_t *nonce, size_t nonce_length,
  1387. const uint8_t *additional_data, size_t additional_data_length,
  1388. const uint8_t *ciphertext, size_t ciphertext_length,
  1389. uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
  1390. {
  1391. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1392. psa_key_location_t location =
  1393. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  1394. switch( location )
  1395. {
  1396. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1397. /* Key is stored in the slot in export representation, so
  1398. * cycle through all known transparent accelerators */
  1399. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1400. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1401. status = mbedtls_test_transparent_aead_decrypt(
  1402. attributes, key_buffer, key_buffer_size,
  1403. alg,
  1404. nonce, nonce_length,
  1405. additional_data, additional_data_length,
  1406. ciphertext, ciphertext_length,
  1407. plaintext, plaintext_size, plaintext_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. /* Fell through, meaning no accelerator supports this operation */
  1414. return( mbedtls_psa_aead_decrypt(
  1415. attributes, key_buffer, key_buffer_size,
  1416. alg,
  1417. nonce, nonce_length,
  1418. additional_data, additional_data_length,
  1419. ciphertext, ciphertext_length,
  1420. plaintext, plaintext_size, plaintext_length ) );
  1421. /* Add cases for opaque driver here */
  1422. default:
  1423. /* Key is declared with a lifetime not known to us */
  1424. (void)status;
  1425. return( PSA_ERROR_INVALID_ARGUMENT );
  1426. }
  1427. }
  1428. psa_status_t psa_driver_wrapper_aead_encrypt_setup(
  1429. psa_aead_operation_t *operation,
  1430. const psa_key_attributes_t *attributes,
  1431. const uint8_t *key_buffer, size_t key_buffer_size,
  1432. psa_algorithm_t alg )
  1433. {
  1434. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1435. psa_key_location_t location =
  1436. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  1437. switch( location )
  1438. {
  1439. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1440. /* Key is stored in the slot in export representation, so
  1441. * cycle through all known transparent accelerators */
  1442. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1443. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1444. operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
  1445. status = mbedtls_test_transparent_aead_encrypt_setup(
  1446. &operation->ctx.transparent_test_driver_ctx,
  1447. attributes, key_buffer, key_buffer_size,
  1448. alg );
  1449. /* Declared with fallback == true */
  1450. if( status != PSA_ERROR_NOT_SUPPORTED )
  1451. return( status );
  1452. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1453. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1454. /* Fell through, meaning no accelerator supports this operation */
  1455. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1456. status = mbedtls_psa_aead_encrypt_setup(
  1457. &operation->ctx.mbedtls_ctx, attributes,
  1458. key_buffer, key_buffer_size,
  1459. alg );
  1460. return( status );
  1461. /* Add cases for opaque driver here */
  1462. default:
  1463. /* Key is declared with a lifetime not known to us */
  1464. (void)status;
  1465. return( PSA_ERROR_INVALID_ARGUMENT );
  1466. }
  1467. }
  1468. psa_status_t psa_driver_wrapper_aead_decrypt_setup(
  1469. psa_aead_operation_t *operation,
  1470. const psa_key_attributes_t *attributes,
  1471. const uint8_t *key_buffer, size_t key_buffer_size,
  1472. psa_algorithm_t alg )
  1473. {
  1474. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1475. psa_key_location_t location =
  1476. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  1477. switch( location )
  1478. {
  1479. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1480. /* Key is stored in the slot in export representation, so
  1481. * cycle through all known transparent accelerators */
  1482. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1483. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1484. operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
  1485. status = mbedtls_test_transparent_aead_decrypt_setup(
  1486. &operation->ctx.transparent_test_driver_ctx,
  1487. attributes,
  1488. key_buffer, key_buffer_size,
  1489. alg );
  1490. /* Declared with fallback == true */
  1491. if( status != PSA_ERROR_NOT_SUPPORTED )
  1492. return( status );
  1493. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1494. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1495. /* Fell through, meaning no accelerator supports this operation */
  1496. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1497. status = mbedtls_psa_aead_decrypt_setup(
  1498. &operation->ctx.mbedtls_ctx,
  1499. attributes,
  1500. key_buffer, key_buffer_size,
  1501. alg );
  1502. return( status );
  1503. /* Add cases for opaque driver here */
  1504. default:
  1505. /* Key is declared with a lifetime not known to us */
  1506. (void)status;
  1507. return( PSA_ERROR_INVALID_ARGUMENT );
  1508. }
  1509. }
  1510. psa_status_t psa_driver_wrapper_aead_set_nonce(
  1511. psa_aead_operation_t *operation,
  1512. const uint8_t *nonce,
  1513. size_t nonce_length )
  1514. {
  1515. switch( operation->id )
  1516. {
  1517. #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
  1518. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1519. return( mbedtls_psa_aead_set_nonce( &operation->ctx.mbedtls_ctx,
  1520. nonce,
  1521. nonce_length ) );
  1522. #endif /* MBEDTLS_PSA_BUILTIN_AEAD */
  1523. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1524. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1525. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1526. return( mbedtls_test_transparent_aead_set_nonce(
  1527. &operation->ctx.transparent_test_driver_ctx,
  1528. nonce, nonce_length ) );
  1529. /* Add cases for opaque driver here */
  1530. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1531. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1532. }
  1533. (void)nonce;
  1534. (void)nonce_length;
  1535. return( PSA_ERROR_INVALID_ARGUMENT );
  1536. }
  1537. psa_status_t psa_driver_wrapper_aead_set_lengths(
  1538. psa_aead_operation_t *operation,
  1539. size_t ad_length,
  1540. size_t plaintext_length )
  1541. {
  1542. switch( operation->id )
  1543. {
  1544. #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
  1545. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1546. return( mbedtls_psa_aead_set_lengths( &operation->ctx.mbedtls_ctx,
  1547. ad_length,
  1548. plaintext_length ) );
  1549. #endif /* MBEDTLS_PSA_BUILTIN_AEAD */
  1550. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1551. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1552. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1553. return( mbedtls_test_transparent_aead_set_lengths(
  1554. &operation->ctx.transparent_test_driver_ctx,
  1555. ad_length, plaintext_length ) );
  1556. /* Add cases for opaque driver here */
  1557. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1558. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1559. }
  1560. (void)ad_length;
  1561. (void)plaintext_length;
  1562. return( PSA_ERROR_INVALID_ARGUMENT );
  1563. }
  1564. psa_status_t psa_driver_wrapper_aead_update_ad(
  1565. psa_aead_operation_t *operation,
  1566. const uint8_t *input,
  1567. size_t input_length )
  1568. {
  1569. switch( operation->id )
  1570. {
  1571. #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
  1572. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1573. return( mbedtls_psa_aead_update_ad( &operation->ctx.mbedtls_ctx,
  1574. input,
  1575. input_length ) );
  1576. #endif /* MBEDTLS_PSA_BUILTIN_AEAD */
  1577. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1578. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1579. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1580. return( mbedtls_test_transparent_aead_update_ad(
  1581. &operation->ctx.transparent_test_driver_ctx,
  1582. input, input_length ) );
  1583. /* Add cases for opaque driver here */
  1584. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1585. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1586. }
  1587. (void)input;
  1588. (void)input_length;
  1589. return( PSA_ERROR_INVALID_ARGUMENT );
  1590. }
  1591. psa_status_t psa_driver_wrapper_aead_update(
  1592. psa_aead_operation_t *operation,
  1593. const uint8_t *input,
  1594. size_t input_length,
  1595. uint8_t *output,
  1596. size_t output_size,
  1597. size_t *output_length )
  1598. {
  1599. switch( operation->id )
  1600. {
  1601. #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
  1602. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1603. return( mbedtls_psa_aead_update( &operation->ctx.mbedtls_ctx,
  1604. input, input_length,
  1605. output, output_size,
  1606. output_length ) );
  1607. #endif /* MBEDTLS_PSA_BUILTIN_AEAD */
  1608. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1609. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1610. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1611. return( mbedtls_test_transparent_aead_update(
  1612. &operation->ctx.transparent_test_driver_ctx,
  1613. input, input_length, output, output_size,
  1614. output_length ) );
  1615. /* Add cases for opaque driver here */
  1616. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1617. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1618. }
  1619. (void)input;
  1620. (void)input_length;
  1621. (void)output;
  1622. (void)output_size;
  1623. (void)output_length;
  1624. return( PSA_ERROR_INVALID_ARGUMENT );
  1625. }
  1626. psa_status_t psa_driver_wrapper_aead_finish(
  1627. psa_aead_operation_t *operation,
  1628. uint8_t *ciphertext,
  1629. size_t ciphertext_size,
  1630. size_t *ciphertext_length,
  1631. uint8_t *tag,
  1632. size_t tag_size,
  1633. size_t *tag_length )
  1634. {
  1635. switch( operation->id )
  1636. {
  1637. #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
  1638. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1639. return( mbedtls_psa_aead_finish( &operation->ctx.mbedtls_ctx,
  1640. ciphertext,
  1641. ciphertext_size,
  1642. ciphertext_length, tag,
  1643. tag_size, tag_length ) );
  1644. #endif /* MBEDTLS_PSA_BUILTIN_AEAD */
  1645. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1646. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1647. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1648. return( mbedtls_test_transparent_aead_finish(
  1649. &operation->ctx.transparent_test_driver_ctx,
  1650. ciphertext, ciphertext_size,
  1651. ciphertext_length, tag, tag_size, tag_length ) );
  1652. /* Add cases for opaque driver here */
  1653. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1654. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1655. }
  1656. (void)ciphertext;
  1657. (void)ciphertext_size;
  1658. (void)ciphertext_length;
  1659. (void)tag;
  1660. (void)tag_size;
  1661. (void)tag_length;
  1662. return( PSA_ERROR_INVALID_ARGUMENT );
  1663. }
  1664. psa_status_t psa_driver_wrapper_aead_verify(
  1665. psa_aead_operation_t *operation,
  1666. uint8_t *plaintext,
  1667. size_t plaintext_size,
  1668. size_t *plaintext_length,
  1669. const uint8_t *tag,
  1670. size_t tag_length )
  1671. {
  1672. switch( operation->id )
  1673. {
  1674. #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
  1675. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1676. {
  1677. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1678. uint8_t check_tag[PSA_AEAD_TAG_MAX_SIZE];
  1679. size_t check_tag_length;
  1680. status = mbedtls_psa_aead_finish( &operation->ctx.mbedtls_ctx,
  1681. plaintext,
  1682. plaintext_size,
  1683. plaintext_length,
  1684. check_tag,
  1685. sizeof( check_tag ),
  1686. &check_tag_length );
  1687. if( status == PSA_SUCCESS )
  1688. {
  1689. if( tag_length != check_tag_length ||
  1690. mbedtls_psa_safer_memcmp( tag, check_tag, tag_length )
  1691. != 0 )
  1692. status = PSA_ERROR_INVALID_SIGNATURE;
  1693. }
  1694. mbedtls_platform_zeroize( check_tag, sizeof( check_tag ) );
  1695. return( status );
  1696. }
  1697. #endif /* MBEDTLS_PSA_BUILTIN_AEAD */
  1698. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1699. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1700. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1701. return( mbedtls_test_transparent_aead_verify(
  1702. &operation->ctx.transparent_test_driver_ctx,
  1703. plaintext, plaintext_size,
  1704. plaintext_length, tag, tag_length ) );
  1705. /* Add cases for opaque driver here */
  1706. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1707. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1708. }
  1709. (void)plaintext;
  1710. (void)plaintext_size;
  1711. (void)plaintext_length;
  1712. (void)tag;
  1713. (void)tag_length;
  1714. return( PSA_ERROR_INVALID_ARGUMENT );
  1715. }
  1716. psa_status_t psa_driver_wrapper_aead_abort(
  1717. psa_aead_operation_t *operation )
  1718. {
  1719. switch( operation->id )
  1720. {
  1721. #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
  1722. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1723. return( mbedtls_psa_aead_abort( &operation->ctx.mbedtls_ctx ) );
  1724. #endif /* MBEDTLS_PSA_BUILTIN_AEAD */
  1725. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1726. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1727. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1728. return( mbedtls_test_transparent_aead_abort(
  1729. &operation->ctx.transparent_test_driver_ctx ) );
  1730. /* Add cases for opaque driver here */
  1731. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1732. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1733. }
  1734. return( PSA_ERROR_INVALID_ARGUMENT );
  1735. }
  1736. /*
  1737. * MAC functions
  1738. */
  1739. psa_status_t psa_driver_wrapper_mac_compute(
  1740. const psa_key_attributes_t *attributes,
  1741. const uint8_t *key_buffer,
  1742. size_t key_buffer_size,
  1743. psa_algorithm_t alg,
  1744. const uint8_t *input,
  1745. size_t input_length,
  1746. uint8_t *mac,
  1747. size_t mac_size,
  1748. size_t *mac_length )
  1749. {
  1750. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1751. psa_key_location_t location =
  1752. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  1753. switch( location )
  1754. {
  1755. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1756. /* Key is stored in the slot in export representation, so
  1757. * cycle through all known transparent accelerators */
  1758. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1759. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1760. status = mbedtls_test_transparent_mac_compute(
  1761. attributes, key_buffer, key_buffer_size, alg,
  1762. input, input_length,
  1763. mac, mac_size, mac_length );
  1764. /* Declared with fallback == true */
  1765. if( status != PSA_ERROR_NOT_SUPPORTED )
  1766. return( status );
  1767. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1768. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1769. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  1770. /* Fell through, meaning no accelerator supports this operation */
  1771. status = mbedtls_psa_mac_compute(
  1772. attributes, key_buffer, key_buffer_size, alg,
  1773. input, input_length,
  1774. mac, mac_size, mac_length );
  1775. if( status != PSA_ERROR_NOT_SUPPORTED )
  1776. return( status );
  1777. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  1778. return( PSA_ERROR_NOT_SUPPORTED );
  1779. /* Add cases for opaque driver here */
  1780. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1781. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1782. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  1783. status = mbedtls_test_opaque_mac_compute(
  1784. attributes, key_buffer, key_buffer_size, alg,
  1785. input, input_length,
  1786. mac, mac_size, mac_length );
  1787. return( status );
  1788. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1789. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1790. default:
  1791. /* Key is declared with a lifetime not known to us */
  1792. (void) key_buffer;
  1793. (void) key_buffer_size;
  1794. (void) alg;
  1795. (void) input;
  1796. (void) input_length;
  1797. (void) mac;
  1798. (void) mac_size;
  1799. (void) mac_length;
  1800. (void) status;
  1801. return( PSA_ERROR_INVALID_ARGUMENT );
  1802. }
  1803. }
  1804. psa_status_t psa_driver_wrapper_mac_sign_setup(
  1805. psa_mac_operation_t *operation,
  1806. const psa_key_attributes_t *attributes,
  1807. const uint8_t *key_buffer,
  1808. size_t key_buffer_size,
  1809. psa_algorithm_t alg )
  1810. {
  1811. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1812. psa_key_location_t location =
  1813. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  1814. switch( location )
  1815. {
  1816. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1817. /* Key is stored in the slot in export representation, so
  1818. * cycle through all known transparent accelerators */
  1819. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1820. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1821. status = mbedtls_test_transparent_mac_sign_setup(
  1822. &operation->ctx.transparent_test_driver_ctx,
  1823. attributes,
  1824. key_buffer, key_buffer_size,
  1825. alg );
  1826. /* Declared with fallback == true */
  1827. if( status == PSA_SUCCESS )
  1828. operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
  1829. if( status != PSA_ERROR_NOT_SUPPORTED )
  1830. return( status );
  1831. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1832. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1833. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  1834. /* Fell through, meaning no accelerator supports this operation */
  1835. status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx,
  1836. attributes,
  1837. key_buffer, key_buffer_size,
  1838. alg );
  1839. if( status == PSA_SUCCESS )
  1840. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1841. if( status != PSA_ERROR_NOT_SUPPORTED )
  1842. return( status );
  1843. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  1844. return( PSA_ERROR_NOT_SUPPORTED );
  1845. /* Add cases for opaque driver here */
  1846. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1847. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1848. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  1849. status = mbedtls_test_opaque_mac_sign_setup(
  1850. &operation->ctx.opaque_test_driver_ctx,
  1851. attributes,
  1852. key_buffer, key_buffer_size,
  1853. alg );
  1854. if( status == PSA_SUCCESS )
  1855. operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
  1856. return( status );
  1857. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1858. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1859. default:
  1860. /* Key is declared with a lifetime not known to us */
  1861. (void) status;
  1862. (void) key_buffer;
  1863. (void) key_buffer_size;
  1864. (void) alg;
  1865. return( PSA_ERROR_INVALID_ARGUMENT );
  1866. }
  1867. }
  1868. psa_status_t psa_driver_wrapper_mac_verify_setup(
  1869. psa_mac_operation_t *operation,
  1870. const psa_key_attributes_t *attributes,
  1871. const uint8_t *key_buffer,
  1872. size_t key_buffer_size,
  1873. psa_algorithm_t alg )
  1874. {
  1875. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  1876. psa_key_location_t location =
  1877. PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
  1878. switch( location )
  1879. {
  1880. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  1881. /* Key is stored in the slot in export representation, so
  1882. * cycle through all known transparent accelerators */
  1883. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1884. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1885. status = mbedtls_test_transparent_mac_verify_setup(
  1886. &operation->ctx.transparent_test_driver_ctx,
  1887. attributes,
  1888. key_buffer, key_buffer_size,
  1889. alg );
  1890. /* Declared with fallback == true */
  1891. if( status == PSA_SUCCESS )
  1892. operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
  1893. if( status != PSA_ERROR_NOT_SUPPORTED )
  1894. return( status );
  1895. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1896. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1897. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  1898. /* Fell through, meaning no accelerator supports this operation */
  1899. status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx,
  1900. attributes,
  1901. key_buffer, key_buffer_size,
  1902. alg );
  1903. if( status == PSA_SUCCESS )
  1904. operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
  1905. if( status != PSA_ERROR_NOT_SUPPORTED )
  1906. return( status );
  1907. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  1908. return( PSA_ERROR_NOT_SUPPORTED );
  1909. /* Add cases for opaque driver here */
  1910. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1911. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1912. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  1913. status = mbedtls_test_opaque_mac_verify_setup(
  1914. &operation->ctx.opaque_test_driver_ctx,
  1915. attributes,
  1916. key_buffer, key_buffer_size,
  1917. alg );
  1918. if( status == PSA_SUCCESS )
  1919. operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
  1920. return( status );
  1921. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1922. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1923. default:
  1924. /* Key is declared with a lifetime not known to us */
  1925. (void) status;
  1926. (void) key_buffer;
  1927. (void) key_buffer_size;
  1928. (void) alg;
  1929. return( PSA_ERROR_INVALID_ARGUMENT );
  1930. }
  1931. }
  1932. psa_status_t psa_driver_wrapper_mac_update(
  1933. psa_mac_operation_t *operation,
  1934. const uint8_t *input,
  1935. size_t input_length )
  1936. {
  1937. switch( operation->id )
  1938. {
  1939. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  1940. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1941. return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx,
  1942. input, input_length ) );
  1943. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  1944. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1945. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1946. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1947. return( mbedtls_test_transparent_mac_update(
  1948. &operation->ctx.transparent_test_driver_ctx,
  1949. input, input_length ) );
  1950. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1951. return( mbedtls_test_opaque_mac_update(
  1952. &operation->ctx.opaque_test_driver_ctx,
  1953. input, input_length ) );
  1954. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1955. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1956. default:
  1957. (void) input;
  1958. (void) input_length;
  1959. return( PSA_ERROR_INVALID_ARGUMENT );
  1960. }
  1961. }
  1962. psa_status_t psa_driver_wrapper_mac_sign_finish(
  1963. psa_mac_operation_t *operation,
  1964. uint8_t *mac,
  1965. size_t mac_size,
  1966. size_t *mac_length )
  1967. {
  1968. switch( operation->id )
  1969. {
  1970. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  1971. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  1972. return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx,
  1973. mac, mac_size, mac_length ) );
  1974. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  1975. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  1976. #if defined(PSA_CRYPTO_DRIVER_TEST)
  1977. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  1978. return( mbedtls_test_transparent_mac_sign_finish(
  1979. &operation->ctx.transparent_test_driver_ctx,
  1980. mac, mac_size, mac_length ) );
  1981. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  1982. return( mbedtls_test_opaque_mac_sign_finish(
  1983. &operation->ctx.opaque_test_driver_ctx,
  1984. mac, mac_size, mac_length ) );
  1985. #endif /* PSA_CRYPTO_DRIVER_TEST */
  1986. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  1987. default:
  1988. (void) mac;
  1989. (void) mac_size;
  1990. (void) mac_length;
  1991. return( PSA_ERROR_INVALID_ARGUMENT );
  1992. }
  1993. }
  1994. psa_status_t psa_driver_wrapper_mac_verify_finish(
  1995. psa_mac_operation_t *operation,
  1996. const uint8_t *mac,
  1997. size_t mac_length )
  1998. {
  1999. switch( operation->id )
  2000. {
  2001. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  2002. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  2003. return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx,
  2004. mac, mac_length ) );
  2005. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  2006. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  2007. #if defined(PSA_CRYPTO_DRIVER_TEST)
  2008. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  2009. return( mbedtls_test_transparent_mac_verify_finish(
  2010. &operation->ctx.transparent_test_driver_ctx,
  2011. mac, mac_length ) );
  2012. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  2013. return( mbedtls_test_opaque_mac_verify_finish(
  2014. &operation->ctx.opaque_test_driver_ctx,
  2015. mac, mac_length ) );
  2016. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2017. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2018. default:
  2019. (void) mac;
  2020. (void) mac_length;
  2021. return( PSA_ERROR_INVALID_ARGUMENT );
  2022. }
  2023. }
  2024. psa_status_t psa_driver_wrapper_mac_abort(
  2025. psa_mac_operation_t *operation )
  2026. {
  2027. switch( operation->id )
  2028. {
  2029. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  2030. case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
  2031. return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) );
  2032. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  2033. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  2034. #if defined(PSA_CRYPTO_DRIVER_TEST)
  2035. case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
  2036. return( mbedtls_test_transparent_mac_abort(
  2037. &operation->ctx.transparent_test_driver_ctx ) );
  2038. case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
  2039. return( mbedtls_test_opaque_mac_abort(
  2040. &operation->ctx.opaque_test_driver_ctx ) );
  2041. #endif /* PSA_CRYPTO_DRIVER_TEST */
  2042. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  2043. default:
  2044. return( PSA_ERROR_INVALID_ARGUMENT );
  2045. }
  2046. }
  2047. #endif /* MBEDTLS_PSA_CRYPTO_C */