psa_crypto_mac.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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_cipher.h"
  25. #include "psa_crypto_mac.h"
  26. #include <mbedtls/md.h>
  27. #include <mbedtls/error.h>
  28. #include <string.h>
  29. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
  30. static psa_status_t psa_hmac_abort_internal(
  31. mbedtls_psa_hmac_operation_t *hmac )
  32. {
  33. mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
  34. return( psa_hash_abort( &hmac->hash_ctx ) );
  35. }
  36. static psa_status_t psa_hmac_setup_internal(
  37. mbedtls_psa_hmac_operation_t *hmac,
  38. const uint8_t *key,
  39. size_t key_length,
  40. psa_algorithm_t hash_alg )
  41. {
  42. uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
  43. size_t i;
  44. size_t hash_size = PSA_HASH_LENGTH( hash_alg );
  45. size_t block_size = PSA_HASH_BLOCK_LENGTH( hash_alg );
  46. psa_status_t status;
  47. hmac->alg = hash_alg;
  48. /* Sanity checks on block_size, to guarantee that there won't be a buffer
  49. * overflow below. This should never trigger if the hash algorithm
  50. * is implemented correctly. */
  51. /* The size checks against the ipad and opad buffers cannot be written
  52. * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
  53. * because that triggers -Wlogical-op on GCC 7.3. */
  54. if( block_size > sizeof( ipad ) )
  55. return( PSA_ERROR_NOT_SUPPORTED );
  56. if( block_size > sizeof( hmac->opad ) )
  57. return( PSA_ERROR_NOT_SUPPORTED );
  58. if( block_size < hash_size )
  59. return( PSA_ERROR_NOT_SUPPORTED );
  60. if( key_length > block_size )
  61. {
  62. status = psa_hash_compute( hash_alg, key, key_length,
  63. ipad, sizeof( ipad ), &key_length );
  64. if( status != PSA_SUCCESS )
  65. goto cleanup;
  66. }
  67. /* A 0-length key is not commonly used in HMAC when used as a MAC,
  68. * but it is permitted. It is common when HMAC is used in HKDF, for
  69. * example. Don't call `memcpy` in the 0-length because `key` could be
  70. * an invalid pointer which would make the behavior undefined. */
  71. else if( key_length != 0 )
  72. memcpy( ipad, key, key_length );
  73. /* ipad contains the key followed by garbage. Xor and fill with 0x36
  74. * to create the ipad value. */
  75. for( i = 0; i < key_length; i++ )
  76. ipad[i] ^= 0x36;
  77. memset( ipad + key_length, 0x36, block_size - key_length );
  78. /* Copy the key material from ipad to opad, flipping the requisite bits,
  79. * and filling the rest of opad with the requisite constant. */
  80. for( i = 0; i < key_length; i++ )
  81. hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
  82. memset( hmac->opad + key_length, 0x5C, block_size - key_length );
  83. status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
  84. if( status != PSA_SUCCESS )
  85. goto cleanup;
  86. status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
  87. cleanup:
  88. mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
  89. return( status );
  90. }
  91. static psa_status_t psa_hmac_update_internal(
  92. mbedtls_psa_hmac_operation_t *hmac,
  93. const uint8_t *data,
  94. size_t data_length )
  95. {
  96. return( psa_hash_update( &hmac->hash_ctx, data, data_length ) );
  97. }
  98. static psa_status_t psa_hmac_finish_internal(
  99. mbedtls_psa_hmac_operation_t *hmac,
  100. uint8_t *mac,
  101. size_t mac_size )
  102. {
  103. uint8_t tmp[PSA_HASH_MAX_SIZE];
  104. psa_algorithm_t hash_alg = hmac->alg;
  105. size_t hash_size = 0;
  106. size_t block_size = PSA_HASH_BLOCK_LENGTH( hash_alg );
  107. psa_status_t status;
  108. status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
  109. if( status != PSA_SUCCESS )
  110. return( status );
  111. /* From here on, tmp needs to be wiped. */
  112. status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
  113. if( status != PSA_SUCCESS )
  114. goto exit;
  115. status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
  116. if( status != PSA_SUCCESS )
  117. goto exit;
  118. status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
  119. if( status != PSA_SUCCESS )
  120. goto exit;
  121. status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
  122. if( status != PSA_SUCCESS )
  123. goto exit;
  124. memcpy( mac, tmp, mac_size );
  125. exit:
  126. mbedtls_platform_zeroize( tmp, hash_size );
  127. return( status );
  128. }
  129. #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
  130. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
  131. static psa_status_t cmac_setup( mbedtls_psa_mac_operation_t *operation,
  132. const psa_key_attributes_t *attributes,
  133. const uint8_t *key_buffer )
  134. {
  135. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  136. #if defined(PSA_WANT_KEY_TYPE_DES)
  137. /* Mbed TLS CMAC does not accept 3DES with only two keys, nor does it accept
  138. * to do CMAC with pure DES, so return NOT_SUPPORTED here. */
  139. if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_DES &&
  140. ( psa_get_key_bits( attributes ) == 64 ||
  141. psa_get_key_bits( attributes ) == 128 ) )
  142. return( PSA_ERROR_NOT_SUPPORTED );
  143. #endif
  144. const mbedtls_cipher_info_t * cipher_info =
  145. mbedtls_cipher_info_from_psa(
  146. PSA_ALG_CMAC,
  147. psa_get_key_type( attributes ),
  148. psa_get_key_bits( attributes ),
  149. NULL );
  150. if( cipher_info == NULL )
  151. return( PSA_ERROR_NOT_SUPPORTED );
  152. ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
  153. if( ret != 0 )
  154. goto exit;
  155. ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
  156. key_buffer,
  157. psa_get_key_bits( attributes ) );
  158. exit:
  159. return( mbedtls_to_psa_error( ret ) );
  160. }
  161. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
  162. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || \
  163. defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
  164. /* Initialize this driver's MAC operation structure. Once this function has been
  165. * called, mbedtls_psa_mac_abort can run and will do the right thing. */
  166. static psa_status_t mac_init(
  167. mbedtls_psa_mac_operation_t *operation,
  168. psa_algorithm_t alg )
  169. {
  170. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  171. operation->alg = alg;
  172. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
  173. if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
  174. {
  175. mbedtls_cipher_init( &operation->ctx.cmac );
  176. status = PSA_SUCCESS;
  177. }
  178. else
  179. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
  180. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
  181. if( PSA_ALG_IS_HMAC( operation->alg ) )
  182. {
  183. /* We'll set up the hash operation later in psa_hmac_setup_internal. */
  184. operation->ctx.hmac.alg = 0;
  185. status = PSA_SUCCESS;
  186. }
  187. else
  188. #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
  189. {
  190. (void) operation;
  191. status = PSA_ERROR_NOT_SUPPORTED;
  192. }
  193. if( status != PSA_SUCCESS )
  194. memset( operation, 0, sizeof( *operation ) );
  195. return( status );
  196. }
  197. psa_status_t mbedtls_psa_mac_abort( mbedtls_psa_mac_operation_t *operation )
  198. {
  199. if( operation->alg == 0 )
  200. {
  201. /* The object has (apparently) been initialized but it is not
  202. * in use. It's ok to call abort on such an object, and there's
  203. * nothing to do. */
  204. return( PSA_SUCCESS );
  205. }
  206. else
  207. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
  208. if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
  209. {
  210. mbedtls_cipher_free( &operation->ctx.cmac );
  211. }
  212. else
  213. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
  214. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
  215. if( PSA_ALG_IS_HMAC( operation->alg ) )
  216. {
  217. psa_hmac_abort_internal( &operation->ctx.hmac );
  218. }
  219. else
  220. #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
  221. {
  222. /* Sanity check (shouldn't happen: operation->alg should
  223. * always have been initialized to a valid value). */
  224. goto bad_state;
  225. }
  226. operation->alg = 0;
  227. return( PSA_SUCCESS );
  228. bad_state:
  229. /* If abort is called on an uninitialized object, we can't trust
  230. * anything. Wipe the object in case it contains confidential data.
  231. * This may result in a memory leak if a pointer gets overwritten,
  232. * but it's too late to do anything about this. */
  233. memset( operation, 0, sizeof( *operation ) );
  234. return( PSA_ERROR_BAD_STATE );
  235. }
  236. static psa_status_t psa_mac_setup( mbedtls_psa_mac_operation_t *operation,
  237. const psa_key_attributes_t *attributes,
  238. const uint8_t *key_buffer,
  239. size_t key_buffer_size,
  240. psa_algorithm_t alg )
  241. {
  242. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  243. /* A context must be freshly initialized before it can be set up. */
  244. if( operation->alg != 0 )
  245. return( PSA_ERROR_BAD_STATE );
  246. status = mac_init( operation, alg );
  247. if( status != PSA_SUCCESS )
  248. return( status );
  249. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
  250. if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
  251. {
  252. /* Key buffer size for CMAC is dictated by the key bits set on the
  253. * attributes, and previously validated by the core on key import. */
  254. (void) key_buffer_size;
  255. status = cmac_setup( operation, attributes, key_buffer );
  256. }
  257. else
  258. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
  259. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
  260. if( PSA_ALG_IS_HMAC( alg ) )
  261. {
  262. status = psa_hmac_setup_internal( &operation->ctx.hmac,
  263. key_buffer,
  264. key_buffer_size,
  265. PSA_ALG_HMAC_GET_HASH( alg ) );
  266. }
  267. else
  268. #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
  269. {
  270. (void) attributes;
  271. (void) key_buffer;
  272. (void) key_buffer_size;
  273. status = PSA_ERROR_NOT_SUPPORTED;
  274. }
  275. if( status != PSA_SUCCESS )
  276. mbedtls_psa_mac_abort( operation );
  277. return( status );
  278. }
  279. psa_status_t mbedtls_psa_mac_sign_setup(
  280. mbedtls_psa_mac_operation_t *operation,
  281. const psa_key_attributes_t *attributes,
  282. const uint8_t *key_buffer,
  283. size_t key_buffer_size,
  284. psa_algorithm_t alg )
  285. {
  286. return( psa_mac_setup( operation, attributes,
  287. key_buffer, key_buffer_size, alg ) );
  288. }
  289. psa_status_t mbedtls_psa_mac_verify_setup(
  290. mbedtls_psa_mac_operation_t *operation,
  291. const psa_key_attributes_t *attributes,
  292. const uint8_t *key_buffer,
  293. size_t key_buffer_size,
  294. psa_algorithm_t alg )
  295. {
  296. return( psa_mac_setup( operation, attributes,
  297. key_buffer, key_buffer_size, alg ) );
  298. }
  299. psa_status_t mbedtls_psa_mac_update(
  300. mbedtls_psa_mac_operation_t *operation,
  301. const uint8_t *input,
  302. size_t input_length )
  303. {
  304. if( operation->alg == 0 )
  305. return( PSA_ERROR_BAD_STATE );
  306. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
  307. if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
  308. {
  309. return( mbedtls_to_psa_error(
  310. mbedtls_cipher_cmac_update( &operation->ctx.cmac,
  311. input, input_length ) ) );
  312. }
  313. else
  314. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
  315. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
  316. if( PSA_ALG_IS_HMAC( operation->alg ) )
  317. {
  318. return( psa_hmac_update_internal( &operation->ctx.hmac,
  319. input, input_length ) );
  320. }
  321. else
  322. #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
  323. {
  324. /* This shouldn't happen if `operation` was initialized by
  325. * a setup function. */
  326. (void) input;
  327. (void) input_length;
  328. return( PSA_ERROR_BAD_STATE );
  329. }
  330. }
  331. static psa_status_t psa_mac_finish_internal(
  332. mbedtls_psa_mac_operation_t *operation,
  333. uint8_t *mac, size_t mac_size )
  334. {
  335. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
  336. if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
  337. {
  338. uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
  339. int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
  340. if( ret == 0 )
  341. memcpy( mac, tmp, mac_size );
  342. mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
  343. return( mbedtls_to_psa_error( ret ) );
  344. }
  345. else
  346. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
  347. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
  348. if( PSA_ALG_IS_HMAC( operation->alg ) )
  349. {
  350. return( psa_hmac_finish_internal( &operation->ctx.hmac,
  351. mac, mac_size ) );
  352. }
  353. else
  354. #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
  355. {
  356. /* This shouldn't happen if `operation` was initialized by
  357. * a setup function. */
  358. (void) operation;
  359. (void) mac;
  360. (void) mac_size;
  361. return( PSA_ERROR_BAD_STATE );
  362. }
  363. }
  364. psa_status_t mbedtls_psa_mac_sign_finish(
  365. mbedtls_psa_mac_operation_t *operation,
  366. uint8_t *mac,
  367. size_t mac_size,
  368. size_t *mac_length )
  369. {
  370. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  371. if( operation->alg == 0 )
  372. return( PSA_ERROR_BAD_STATE );
  373. status = psa_mac_finish_internal( operation, mac, mac_size );
  374. if( status == PSA_SUCCESS )
  375. *mac_length = mac_size;
  376. return( status );
  377. }
  378. psa_status_t mbedtls_psa_mac_verify_finish(
  379. mbedtls_psa_mac_operation_t *operation,
  380. const uint8_t *mac,
  381. size_t mac_length )
  382. {
  383. uint8_t actual_mac[PSA_MAC_MAX_SIZE];
  384. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  385. if( operation->alg == 0 )
  386. return( PSA_ERROR_BAD_STATE );
  387. /* Consistency check: requested MAC length fits our local buffer */
  388. if( mac_length > sizeof( actual_mac ) )
  389. return( PSA_ERROR_INVALID_ARGUMENT );
  390. status = psa_mac_finish_internal( operation, actual_mac, mac_length );
  391. if( status != PSA_SUCCESS )
  392. goto cleanup;
  393. if( mbedtls_psa_safer_memcmp( mac, actual_mac, mac_length ) != 0 )
  394. status = PSA_ERROR_INVALID_SIGNATURE;
  395. cleanup:
  396. mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
  397. return( status );
  398. }
  399. psa_status_t mbedtls_psa_mac_compute(
  400. const psa_key_attributes_t *attributes,
  401. const uint8_t *key_buffer,
  402. size_t key_buffer_size,
  403. psa_algorithm_t alg,
  404. const uint8_t *input,
  405. size_t input_length,
  406. uint8_t *mac,
  407. size_t mac_size,
  408. size_t *mac_length )
  409. {
  410. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  411. mbedtls_psa_mac_operation_t operation = MBEDTLS_PSA_MAC_OPERATION_INIT;
  412. status = psa_mac_setup( &operation,
  413. attributes, key_buffer, key_buffer_size,
  414. alg );
  415. if( status != PSA_SUCCESS )
  416. goto exit;
  417. if( input_length > 0 )
  418. {
  419. status = mbedtls_psa_mac_update( &operation, input, input_length );
  420. if( status != PSA_SUCCESS )
  421. goto exit;
  422. }
  423. status = psa_mac_finish_internal( &operation, mac, mac_size );
  424. if( status == PSA_SUCCESS )
  425. *mac_length = mac_size;
  426. exit:
  427. mbedtls_psa_mac_abort( &operation );
  428. return( status );
  429. }
  430. #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || MBEDTLS_PSA_BUILTIN_ALG_CMAC */
  431. #endif /* MBEDTLS_PSA_CRYPTO_C */