psa_crypto_mac.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /*
  2. * PSA MAC layer on top of Mbed TLS software crypto
  3. */
  4. /*
  5. * Copyright The Mbed TLS Contributors
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. * not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include "common.h"
  21. #if defined(MBEDTLS_PSA_CRYPTO_C)
  22. #include <psa/crypto.h>
  23. #include "psa_crypto_core.h"
  24. #include "psa_crypto_mac.h"
  25. #include <mbedtls/md.h>
  26. #include <mbedtls/error.h>
  27. #include <string.h>
  28. /* Use builtin defines specific to this compilation unit, since the test driver
  29. * relies on the software driver. */
  30. #if( defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) || \
  31. ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_CMAC) ) )
  32. #define BUILTIN_ALG_CMAC 1
  33. #endif
  34. #if( defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || \
  35. ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_HMAC) ) )
  36. #define BUILTIN_ALG_HMAC 1
  37. #endif
  38. #if defined(BUILTIN_ALG_HMAC)
  39. static size_t psa_get_hash_block_size( psa_algorithm_t alg )
  40. {
  41. switch( alg )
  42. {
  43. case PSA_ALG_MD2:
  44. return( 16 );
  45. case PSA_ALG_MD4:
  46. return( 64 );
  47. case PSA_ALG_MD5:
  48. return( 64 );
  49. case PSA_ALG_RIPEMD160:
  50. return( 64 );
  51. case PSA_ALG_SHA_1:
  52. return( 64 );
  53. case PSA_ALG_SHA_224:
  54. return( 64 );
  55. case PSA_ALG_SHA_256:
  56. return( 64 );
  57. case PSA_ALG_SHA_384:
  58. return( 128 );
  59. case PSA_ALG_SHA_512:
  60. return( 128 );
  61. default:
  62. return( 0 );
  63. }
  64. }
  65. static psa_status_t psa_hmac_abort_internal(
  66. mbedtls_psa_hmac_operation_t *hmac )
  67. {
  68. mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
  69. return( psa_hash_abort( &hmac->hash_ctx ) );
  70. }
  71. static psa_status_t psa_hmac_setup_internal(
  72. mbedtls_psa_hmac_operation_t *hmac,
  73. const uint8_t *key,
  74. size_t key_length,
  75. psa_algorithm_t hash_alg )
  76. {
  77. uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
  78. size_t i;
  79. size_t hash_size = PSA_HASH_LENGTH( hash_alg );
  80. size_t block_size = psa_get_hash_block_size( hash_alg );
  81. psa_status_t status;
  82. hmac->alg = hash_alg;
  83. /* Sanity checks on block_size, to guarantee that there won't be a buffer
  84. * overflow below. This should never trigger if the hash algorithm
  85. * is implemented correctly. */
  86. /* The size checks against the ipad and opad buffers cannot be written
  87. * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
  88. * because that triggers -Wlogical-op on GCC 7.3. */
  89. if( block_size > sizeof( ipad ) )
  90. return( PSA_ERROR_NOT_SUPPORTED );
  91. if( block_size > sizeof( hmac->opad ) )
  92. return( PSA_ERROR_NOT_SUPPORTED );
  93. if( block_size < hash_size )
  94. return( PSA_ERROR_NOT_SUPPORTED );
  95. if( key_length > block_size )
  96. {
  97. status = psa_hash_compute( hash_alg, key, key_length,
  98. ipad, sizeof( ipad ), &key_length );
  99. if( status != PSA_SUCCESS )
  100. goto cleanup;
  101. }
  102. /* A 0-length key is not commonly used in HMAC when used as a MAC,
  103. * but it is permitted. It is common when HMAC is used in HKDF, for
  104. * example. Don't call `memcpy` in the 0-length because `key` could be
  105. * an invalid pointer which would make the behavior undefined. */
  106. else if( key_length != 0 )
  107. memcpy( ipad, key, key_length );
  108. /* ipad contains the key followed by garbage. Xor and fill with 0x36
  109. * to create the ipad value. */
  110. for( i = 0; i < key_length; i++ )
  111. ipad[i] ^= 0x36;
  112. memset( ipad + key_length, 0x36, block_size - key_length );
  113. /* Copy the key material from ipad to opad, flipping the requisite bits,
  114. * and filling the rest of opad with the requisite constant. */
  115. for( i = 0; i < key_length; i++ )
  116. hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
  117. memset( hmac->opad + key_length, 0x5C, block_size - key_length );
  118. status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
  119. if( status != PSA_SUCCESS )
  120. goto cleanup;
  121. status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
  122. cleanup:
  123. mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
  124. return( status );
  125. }
  126. static psa_status_t psa_hmac_update_internal(
  127. mbedtls_psa_hmac_operation_t *hmac,
  128. const uint8_t *data,
  129. size_t data_length )
  130. {
  131. return( psa_hash_update( &hmac->hash_ctx, data, data_length ) );
  132. }
  133. static psa_status_t psa_hmac_finish_internal(
  134. mbedtls_psa_hmac_operation_t *hmac,
  135. uint8_t *mac,
  136. size_t mac_size )
  137. {
  138. uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
  139. psa_algorithm_t hash_alg = hmac->alg;
  140. size_t hash_size = 0;
  141. size_t block_size = psa_get_hash_block_size( hash_alg );
  142. psa_status_t status;
  143. status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
  144. if( status != PSA_SUCCESS )
  145. return( status );
  146. /* From here on, tmp needs to be wiped. */
  147. status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
  148. if( status != PSA_SUCCESS )
  149. goto exit;
  150. status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
  151. if( status != PSA_SUCCESS )
  152. goto exit;
  153. status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
  154. if( status != PSA_SUCCESS )
  155. goto exit;
  156. status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
  157. if( status != PSA_SUCCESS )
  158. goto exit;
  159. memcpy( mac, tmp, mac_size );
  160. exit:
  161. mbedtls_platform_zeroize( tmp, hash_size );
  162. return( status );
  163. }
  164. #endif /* BUILTIN_ALG_HMAC */
  165. #if defined(BUILTIN_ALG_CMAC)
  166. static psa_status_t cmac_setup( mbedtls_psa_mac_operation_t *operation,
  167. const psa_key_attributes_t *attributes,
  168. const uint8_t *key_buffer )
  169. {
  170. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  171. #if defined(PSA_WANT_KEY_TYPE_DES)
  172. /* Mbed TLS CMAC does not accept 3DES with only two keys, nor does it accept
  173. * to do CMAC with pure DES, so return NOT_SUPPORTED here. */
  174. if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_DES &&
  175. ( psa_get_key_bits( attributes ) == 64 ||
  176. psa_get_key_bits( attributes ) == 128 ) )
  177. return( PSA_ERROR_NOT_SUPPORTED );
  178. #endif
  179. const mbedtls_cipher_info_t * cipher_info =
  180. mbedtls_cipher_info_from_psa(
  181. PSA_ALG_CMAC,
  182. psa_get_key_type( attributes ),
  183. psa_get_key_bits( attributes ),
  184. NULL );
  185. if( cipher_info == NULL )
  186. return( PSA_ERROR_NOT_SUPPORTED );
  187. ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
  188. if( ret != 0 )
  189. goto exit;
  190. ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
  191. key_buffer,
  192. psa_get_key_bits( attributes ) );
  193. exit:
  194. return( mbedtls_to_psa_error( ret ) );
  195. }
  196. #endif /* BUILTIN_ALG_CMAC */
  197. /* Implement the PSA driver MAC interface on top of mbed TLS if either the
  198. * software driver or the test driver requires it. */
  199. #if defined(BUILTIN_ALG_HMAC) || defined(BUILTIN_ALG_CMAC)
  200. /* Initialize this driver's MAC operation structure. Once this function has been
  201. * called, mbedtls_psa_mac_abort can run and will do the right thing. */
  202. static psa_status_t mac_init(
  203. mbedtls_psa_mac_operation_t *operation,
  204. psa_algorithm_t alg )
  205. {
  206. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  207. operation->alg = alg;
  208. #if defined(BUILTIN_ALG_CMAC)
  209. if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
  210. {
  211. mbedtls_cipher_init( &operation->ctx.cmac );
  212. status = PSA_SUCCESS;
  213. }
  214. else
  215. #endif /* BUILTIN_ALG_CMAC */
  216. #if defined(BUILTIN_ALG_HMAC)
  217. if( PSA_ALG_IS_HMAC( operation->alg ) )
  218. {
  219. /* We'll set up the hash operation later in psa_hmac_setup_internal. */
  220. operation->ctx.hmac.alg = 0;
  221. status = PSA_SUCCESS;
  222. }
  223. else
  224. #endif /* BUILTIN_ALG_HMAC */
  225. {
  226. status = PSA_ERROR_NOT_SUPPORTED;
  227. }
  228. if( status != PSA_SUCCESS )
  229. memset( operation, 0, sizeof( *operation ) );
  230. return( status );
  231. }
  232. static psa_status_t mac_abort( mbedtls_psa_mac_operation_t *operation )
  233. {
  234. if( operation->alg == 0 )
  235. {
  236. /* The object has (apparently) been initialized but it is not
  237. * in use. It's ok to call abort on such an object, and there's
  238. * nothing to do. */
  239. return( PSA_SUCCESS );
  240. }
  241. else
  242. #if defined(BUILTIN_ALG_CMAC)
  243. if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
  244. {
  245. mbedtls_cipher_free( &operation->ctx.cmac );
  246. }
  247. else
  248. #endif /* BUILTIN_ALG_CMAC */
  249. #if defined(BUILTIN_ALG_HMAC)
  250. if( PSA_ALG_IS_HMAC( operation->alg ) )
  251. {
  252. psa_hmac_abort_internal( &operation->ctx.hmac );
  253. }
  254. else
  255. #endif /* BUILTIN_ALG_HMAC */
  256. {
  257. /* Sanity check (shouldn't happen: operation->alg should
  258. * always have been initialized to a valid value). */
  259. goto bad_state;
  260. }
  261. operation->alg = 0;
  262. return( PSA_SUCCESS );
  263. bad_state:
  264. /* If abort is called on an uninitialized object, we can't trust
  265. * anything. Wipe the object in case it contains confidential data.
  266. * This may result in a memory leak if a pointer gets overwritten,
  267. * but it's too late to do anything about this. */
  268. memset( operation, 0, sizeof( *operation ) );
  269. return( PSA_ERROR_BAD_STATE );
  270. }
  271. static psa_status_t mac_setup( mbedtls_psa_mac_operation_t *operation,
  272. const psa_key_attributes_t *attributes,
  273. const uint8_t *key_buffer,
  274. size_t key_buffer_size,
  275. psa_algorithm_t alg )
  276. {
  277. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  278. /* A context must be freshly initialized before it can be set up. */
  279. if( operation->alg != 0 )
  280. return( PSA_ERROR_BAD_STATE );
  281. status = mac_init( operation, alg );
  282. if( status != PSA_SUCCESS )
  283. return( status );
  284. #if defined(BUILTIN_ALG_CMAC)
  285. if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
  286. {
  287. /* Key buffer size for CMAC is dictated by the key bits set on the
  288. * attributes, and previously validated by the core on key import. */
  289. (void) key_buffer_size;
  290. status = cmac_setup( operation, attributes, key_buffer );
  291. }
  292. else
  293. #endif /* BUILTIN_ALG_CMAC */
  294. #if defined(BUILTIN_ALG_HMAC)
  295. if( PSA_ALG_IS_HMAC( alg ) )
  296. {
  297. status = psa_hmac_setup_internal( &operation->ctx.hmac,
  298. key_buffer,
  299. key_buffer_size,
  300. PSA_ALG_HMAC_GET_HASH( alg ) );
  301. }
  302. else
  303. #endif /* BUILTIN_ALG_HMAC */
  304. {
  305. (void) attributes;
  306. (void) key_buffer;
  307. (void) key_buffer_size;
  308. status = PSA_ERROR_NOT_SUPPORTED;
  309. }
  310. if( status != PSA_SUCCESS )
  311. mac_abort( operation );
  312. return( status );
  313. }
  314. static psa_status_t mac_update(
  315. mbedtls_psa_mac_operation_t *operation,
  316. const uint8_t *input,
  317. size_t input_length )
  318. {
  319. if( operation->alg == 0 )
  320. return( PSA_ERROR_BAD_STATE );
  321. #if defined(BUILTIN_ALG_CMAC)
  322. if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
  323. {
  324. return( mbedtls_to_psa_error(
  325. mbedtls_cipher_cmac_update( &operation->ctx.cmac,
  326. input, input_length ) ) );
  327. }
  328. else
  329. #endif /* BUILTIN_ALG_CMAC */
  330. #if defined(BUILTIN_ALG_HMAC)
  331. if( PSA_ALG_IS_HMAC( operation->alg ) )
  332. {
  333. return( psa_hmac_update_internal( &operation->ctx.hmac,
  334. input, input_length ) );
  335. }
  336. else
  337. #endif /* BUILTIN_ALG_HMAC */
  338. {
  339. /* This shouldn't happen if `operation` was initialized by
  340. * a setup function. */
  341. (void) input;
  342. (void) input_length;
  343. return( PSA_ERROR_BAD_STATE );
  344. }
  345. }
  346. static psa_status_t mac_finish_internal( mbedtls_psa_mac_operation_t *operation,
  347. uint8_t *mac,
  348. size_t mac_size )
  349. {
  350. #if defined(BUILTIN_ALG_CMAC)
  351. if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
  352. {
  353. uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
  354. int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
  355. if( ret == 0 )
  356. memcpy( mac, tmp, mac_size );
  357. mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
  358. return( mbedtls_to_psa_error( ret ) );
  359. }
  360. else
  361. #endif /* BUILTIN_ALG_CMAC */
  362. #if defined(BUILTIN_ALG_HMAC)
  363. if( PSA_ALG_IS_HMAC( operation->alg ) )
  364. {
  365. return( psa_hmac_finish_internal( &operation->ctx.hmac,
  366. mac, mac_size ) );
  367. }
  368. else
  369. #endif /* BUILTIN_ALG_HMAC */
  370. {
  371. /* This shouldn't happen if `operation` was initialized by
  372. * a setup function. */
  373. (void) operation;
  374. (void) mac;
  375. (void) mac_size;
  376. return( PSA_ERROR_BAD_STATE );
  377. }
  378. }
  379. static psa_status_t mac_sign_finish(
  380. mbedtls_psa_mac_operation_t *operation,
  381. uint8_t *mac,
  382. size_t mac_size,
  383. size_t *mac_length )
  384. {
  385. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  386. if( operation->alg == 0 )
  387. return( PSA_ERROR_BAD_STATE );
  388. status = mac_finish_internal( operation, mac, mac_size );
  389. if( status == PSA_SUCCESS )
  390. *mac_length = mac_size;
  391. return( status );
  392. }
  393. static psa_status_t mac_verify_finish(
  394. mbedtls_psa_mac_operation_t *operation,
  395. const uint8_t *mac,
  396. size_t mac_length )
  397. {
  398. uint8_t actual_mac[PSA_MAC_MAX_SIZE];
  399. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  400. if( operation->alg == 0 )
  401. return( PSA_ERROR_BAD_STATE );
  402. /* Consistency check: requested MAC length fits our local buffer */
  403. if( mac_length > sizeof( actual_mac ) )
  404. return( PSA_ERROR_INVALID_ARGUMENT );
  405. status = mac_finish_internal( operation, actual_mac, mac_length );
  406. if( status != PSA_SUCCESS )
  407. goto cleanup;
  408. if( mbedtls_psa_safer_memcmp( mac, actual_mac, mac_length ) != 0 )
  409. status = PSA_ERROR_INVALID_SIGNATURE;
  410. cleanup:
  411. mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
  412. return( status );
  413. }
  414. static psa_status_t mac_compute(
  415. const psa_key_attributes_t *attributes,
  416. const uint8_t *key_buffer,
  417. size_t key_buffer_size,
  418. psa_algorithm_t alg,
  419. const uint8_t *input,
  420. size_t input_length,
  421. uint8_t *mac,
  422. size_t mac_size,
  423. size_t *mac_length )
  424. {
  425. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  426. mbedtls_psa_mac_operation_t operation = MBEDTLS_PSA_MAC_OPERATION_INIT;
  427. status = mac_setup( &operation,
  428. attributes, key_buffer, key_buffer_size,
  429. alg );
  430. if( status != PSA_SUCCESS )
  431. goto exit;
  432. if( input_length > 0 )
  433. {
  434. status = mac_update( &operation, input, input_length );
  435. if( status != PSA_SUCCESS )
  436. goto exit;
  437. }
  438. status = mac_finish_internal( &operation, mac, mac_size );
  439. if( status == PSA_SUCCESS )
  440. *mac_length = mac_size;
  441. exit:
  442. mac_abort( &operation );
  443. return( status );
  444. }
  445. #endif /* BUILTIN_ALG_HMAC || BUILTIN_ALG_CMAC */
  446. #if defined(MBEDTLS_PSA_BUILTIN_MAC)
  447. psa_status_t mbedtls_psa_mac_compute(
  448. const psa_key_attributes_t *attributes,
  449. const uint8_t *key_buffer,
  450. size_t key_buffer_size,
  451. psa_algorithm_t alg,
  452. const uint8_t *input,
  453. size_t input_length,
  454. uint8_t *mac,
  455. size_t mac_size,
  456. size_t *mac_length )
  457. {
  458. return( mac_compute( attributes, key_buffer, key_buffer_size, alg,
  459. input, input_length,
  460. mac, mac_size, mac_length ) );
  461. }
  462. psa_status_t mbedtls_psa_mac_sign_setup(
  463. mbedtls_psa_mac_operation_t *operation,
  464. const psa_key_attributes_t *attributes,
  465. const uint8_t *key_buffer,
  466. size_t key_buffer_size,
  467. psa_algorithm_t alg )
  468. {
  469. return( mac_setup( operation, attributes,
  470. key_buffer, key_buffer_size, alg ) );
  471. }
  472. psa_status_t mbedtls_psa_mac_verify_setup(
  473. mbedtls_psa_mac_operation_t *operation,
  474. const psa_key_attributes_t *attributes,
  475. const uint8_t *key_buffer,
  476. size_t key_buffer_size,
  477. psa_algorithm_t alg )
  478. {
  479. return( mac_setup( operation, attributes,
  480. key_buffer, key_buffer_size, alg ) );
  481. }
  482. psa_status_t mbedtls_psa_mac_update(
  483. mbedtls_psa_mac_operation_t *operation,
  484. const uint8_t *input,
  485. size_t input_length )
  486. {
  487. return( mac_update( operation, input, input_length ) );
  488. }
  489. psa_status_t mbedtls_psa_mac_sign_finish(
  490. mbedtls_psa_mac_operation_t *operation,
  491. uint8_t *mac,
  492. size_t mac_size,
  493. size_t *mac_length )
  494. {
  495. return( mac_sign_finish( operation, mac, mac_size, mac_length ) );
  496. }
  497. psa_status_t mbedtls_psa_mac_verify_finish(
  498. mbedtls_psa_mac_operation_t *operation,
  499. const uint8_t *mac,
  500. size_t mac_length )
  501. {
  502. return( mac_verify_finish( operation, mac, mac_length ) );
  503. }
  504. psa_status_t mbedtls_psa_mac_abort(
  505. mbedtls_psa_mac_operation_t *operation )
  506. {
  507. return( mac_abort( operation ) );
  508. }
  509. #endif /* MBEDTLS_PSA_BUILTIN_MAC */
  510. /*
  511. * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
  512. */
  513. #if defined(PSA_CRYPTO_DRIVER_TEST)
  514. static int is_mac_accelerated( psa_algorithm_t alg )
  515. {
  516. #if defined(MBEDTLS_PSA_ACCEL_ALG_HMAC)
  517. if( PSA_ALG_IS_HMAC( alg ) )
  518. return( 1 );
  519. #endif
  520. switch( PSA_ALG_FULL_LENGTH_MAC( alg ) )
  521. {
  522. #if defined(MBEDTLS_PSA_ACCEL_ALG_CMAC)
  523. case PSA_ALG_CMAC:
  524. return( 1 );
  525. #endif
  526. default:
  527. return( 0 );
  528. }
  529. }
  530. psa_status_t mbedtls_transparent_test_driver_mac_compute(
  531. const psa_key_attributes_t *attributes,
  532. const uint8_t *key_buffer,
  533. size_t key_buffer_size,
  534. psa_algorithm_t alg,
  535. const uint8_t *input,
  536. size_t input_length,
  537. uint8_t *mac,
  538. size_t mac_size,
  539. size_t *mac_length )
  540. {
  541. if( is_mac_accelerated( alg ) )
  542. return( mac_compute( attributes, key_buffer, key_buffer_size, alg,
  543. input, input_length,
  544. mac, mac_size, mac_length ) );
  545. else
  546. return( PSA_ERROR_NOT_SUPPORTED );
  547. }
  548. psa_status_t mbedtls_transparent_test_driver_mac_sign_setup(
  549. mbedtls_transparent_test_driver_mac_operation_t *operation,
  550. const psa_key_attributes_t *attributes,
  551. const uint8_t *key_buffer,
  552. size_t key_buffer_size,
  553. psa_algorithm_t alg )
  554. {
  555. if( is_mac_accelerated( alg ) )
  556. return( mac_setup( operation, attributes,
  557. key_buffer, key_buffer_size, alg ) );
  558. else
  559. return( PSA_ERROR_NOT_SUPPORTED );
  560. }
  561. psa_status_t mbedtls_transparent_test_driver_mac_verify_setup(
  562. mbedtls_transparent_test_driver_mac_operation_t *operation,
  563. const psa_key_attributes_t *attributes,
  564. const uint8_t *key_buffer,
  565. size_t key_buffer_size,
  566. psa_algorithm_t alg )
  567. {
  568. if( is_mac_accelerated( alg ) )
  569. return( mac_setup( operation, attributes,
  570. key_buffer, key_buffer_size, alg ) );
  571. else
  572. return( PSA_ERROR_NOT_SUPPORTED );
  573. }
  574. psa_status_t mbedtls_transparent_test_driver_mac_update(
  575. mbedtls_transparent_test_driver_mac_operation_t *operation,
  576. const uint8_t *input,
  577. size_t input_length )
  578. {
  579. if( is_mac_accelerated( operation->alg ) )
  580. return( mac_update( operation, input, input_length ) );
  581. else
  582. return( PSA_ERROR_BAD_STATE );
  583. }
  584. psa_status_t mbedtls_transparent_test_driver_mac_sign_finish(
  585. mbedtls_transparent_test_driver_mac_operation_t *operation,
  586. uint8_t *mac,
  587. size_t mac_size,
  588. size_t *mac_length )
  589. {
  590. if( is_mac_accelerated( operation->alg ) )
  591. return( mac_sign_finish( operation, mac, mac_size, mac_length ) );
  592. else
  593. return( PSA_ERROR_BAD_STATE );
  594. }
  595. psa_status_t mbedtls_transparent_test_driver_mac_verify_finish(
  596. mbedtls_transparent_test_driver_mac_operation_t *operation,
  597. const uint8_t *mac,
  598. size_t mac_length )
  599. {
  600. if( is_mac_accelerated( operation->alg ) )
  601. return( mac_verify_finish( operation, mac, mac_length ) );
  602. else
  603. return( PSA_ERROR_BAD_STATE );
  604. }
  605. psa_status_t mbedtls_transparent_test_driver_mac_abort(
  606. mbedtls_transparent_test_driver_mac_operation_t *operation )
  607. {
  608. return( mac_abort( operation ) );
  609. }
  610. psa_status_t mbedtls_opaque_test_driver_mac_compute(
  611. const psa_key_attributes_t *attributes,
  612. const uint8_t *key_buffer,
  613. size_t key_buffer_size,
  614. psa_algorithm_t alg,
  615. const uint8_t *input,
  616. size_t input_length,
  617. uint8_t *mac,
  618. size_t mac_size,
  619. size_t *mac_length )
  620. {
  621. /* Opaque driver testing is not implemented yet through this mechanism. */
  622. (void) attributes;
  623. (void) key_buffer;
  624. (void) key_buffer_size;
  625. (void) alg;
  626. (void) input;
  627. (void) input_length;
  628. (void) mac;
  629. (void) mac_size;
  630. (void) mac_length;
  631. return( PSA_ERROR_NOT_SUPPORTED );
  632. }
  633. psa_status_t mbedtls_opaque_test_driver_mac_sign_setup(
  634. mbedtls_opaque_test_driver_mac_operation_t *operation,
  635. const psa_key_attributes_t *attributes,
  636. const uint8_t *key_buffer,
  637. size_t key_buffer_size,
  638. psa_algorithm_t alg )
  639. {
  640. /* Opaque driver testing is not implemented yet through this mechanism. */
  641. (void) operation;
  642. (void) attributes;
  643. (void) key_buffer;
  644. (void) key_buffer_size;
  645. (void) alg;
  646. return( PSA_ERROR_NOT_SUPPORTED );
  647. }
  648. psa_status_t mbedtls_opaque_test_driver_mac_verify_setup(
  649. mbedtls_opaque_test_driver_mac_operation_t *operation,
  650. const psa_key_attributes_t *attributes,
  651. const uint8_t *key_buffer,
  652. size_t key_buffer_size,
  653. psa_algorithm_t alg )
  654. {
  655. /* Opaque driver testing is not implemented yet through this mechanism. */
  656. (void) operation;
  657. (void) attributes;
  658. (void) key_buffer;
  659. (void) key_buffer_size;
  660. (void) alg;
  661. return( PSA_ERROR_NOT_SUPPORTED );
  662. }
  663. psa_status_t mbedtls_opaque_test_driver_mac_update(
  664. mbedtls_opaque_test_driver_mac_operation_t *operation,
  665. const uint8_t *input,
  666. size_t input_length )
  667. {
  668. /* Opaque driver testing is not implemented yet through this mechanism. */
  669. (void) operation;
  670. (void) input;
  671. (void) input_length;
  672. return( PSA_ERROR_NOT_SUPPORTED );
  673. }
  674. psa_status_t mbedtls_opaque_test_driver_mac_sign_finish(
  675. mbedtls_opaque_test_driver_mac_operation_t *operation,
  676. uint8_t *mac,
  677. size_t mac_size,
  678. size_t *mac_length )
  679. {
  680. /* Opaque driver testing is not implemented yet through this mechanism. */
  681. (void) operation;
  682. (void) mac;
  683. (void) mac_size;
  684. (void) mac_length;
  685. return( PSA_ERROR_NOT_SUPPORTED );
  686. }
  687. psa_status_t mbedtls_opaque_test_driver_mac_verify_finish(
  688. mbedtls_opaque_test_driver_mac_operation_t *operation,
  689. const uint8_t *mac,
  690. size_t mac_length )
  691. {
  692. /* Opaque driver testing is not implemented yet through this mechanism. */
  693. (void) operation;
  694. (void) mac;
  695. (void) mac_length;
  696. return( PSA_ERROR_NOT_SUPPORTED );
  697. }
  698. psa_status_t mbedtls_opaque_test_driver_mac_abort(
  699. mbedtls_opaque_test_driver_mac_operation_t *operation )
  700. {
  701. /* Opaque driver testing is not implemented yet through this mechanism. */
  702. (void) operation;
  703. return( PSA_ERROR_NOT_SUPPORTED );
  704. }
  705. #endif /* PSA_CRYPTO_DRIVER_TEST */
  706. #endif /* MBEDTLS_PSA_CRYPTO_C */