cmac.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. /*
  2. * \file cmac.c
  3. *
  4. * \brief NIST SP800-38B compliant CMAC implementation for AES and 3DES
  5. *
  6. * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
  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. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. /*
  24. * References:
  25. *
  26. * - NIST SP 800-38B Recommendation for Block Cipher Modes of Operation: The
  27. * CMAC Mode for Authentication
  28. * http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38b.pdf
  29. *
  30. * - RFC 4493 - The AES-CMAC Algorithm
  31. * https://tools.ietf.org/html/rfc4493
  32. *
  33. * - RFC 4615 - The Advanced Encryption Standard-Cipher-based Message
  34. * Authentication Code-Pseudo-Random Function-128 (AES-CMAC-PRF-128)
  35. * Algorithm for the Internet Key Exchange Protocol (IKE)
  36. * https://tools.ietf.org/html/rfc4615
  37. *
  38. * Additional test vectors: ISO/IEC 9797-1
  39. *
  40. */
  41. #if !defined(MBEDTLS_CONFIG_FILE)
  42. #include "mbedtls/config.h"
  43. #else
  44. #include MBEDTLS_CONFIG_FILE
  45. #endif
  46. #if defined(MBEDTLS_CMAC_C)
  47. #include "mbedtls/cmac.h"
  48. #include <string.h>
  49. #if defined(MBEDTLS_PLATFORM_C)
  50. #include "mbedtls/platform.h"
  51. #else
  52. #include <stdlib.h>
  53. #define mbedtls_calloc calloc
  54. #define mbedtls_free free
  55. #if defined(MBEDTLS_SELF_TEST)
  56. #include <stdio.h>
  57. #define mbedtls_printf printf
  58. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C || MBEDTLS_DES_C */
  59. #endif /* MBEDTLS_PLATFORM_C */
  60. /* Implementation that should never be optimized out by the compiler */
  61. static void mbedtls_zeroize( void *v, size_t n ) {
  62. volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
  63. }
  64. /*
  65. * Multiplication by u in the Galois field of GF(2^n)
  66. *
  67. * As explained in NIST SP 800-38B, this can be computed:
  68. *
  69. * If MSB(p) = 0, then p = (p << 1)
  70. * If MSB(p) = 1, then p = (p << 1) ^ R_n
  71. * with R_64 = 0x1B and R_128 = 0x87
  72. *
  73. * Input and output MUST NOT point to the same buffer
  74. * Block size must be 8 byes or 16 bytes - the block sizes for DES and AES.
  75. */
  76. static int cmac_multiply_by_u( unsigned char *output,
  77. const unsigned char *input,
  78. size_t blocksize )
  79. {
  80. const unsigned char R_128 = 0x87;
  81. const unsigned char R_64 = 0x1B;
  82. unsigned char R_n, mask;
  83. unsigned char overflow = 0x00;
  84. int i;
  85. if( blocksize == MBEDTLS_AES_BLOCK_SIZE )
  86. {
  87. R_n = R_128;
  88. }
  89. else if( blocksize == MBEDTLS_DES3_BLOCK_SIZE )
  90. {
  91. R_n = R_64;
  92. }
  93. else
  94. {
  95. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  96. }
  97. for( i = blocksize - 1; i >= 0; i-- )
  98. {
  99. output[i] = input[i] << 1 | overflow;
  100. overflow = input[i] >> 7;
  101. }
  102. /* mask = ( input[0] >> 7 ) ? 0xff : 0x00
  103. * using bit operations to avoid branches */
  104. /* MSVC has a warning about unary minus on unsigned, but this is
  105. * well-defined and precisely what we want to do here */
  106. #if defined(_MSC_VER)
  107. #pragma warning( push )
  108. #pragma warning( disable : 4146 )
  109. #endif
  110. mask = - ( input[0] >> 7 );
  111. #if defined(_MSC_VER)
  112. #pragma warning( pop )
  113. #endif
  114. output[ blocksize - 1 ] ^= R_n & mask;
  115. return( 0 );
  116. }
  117. /*
  118. * Generate subkeys
  119. *
  120. * - as specified by RFC 4493, section 2.3 Subkey Generation Algorithm
  121. */
  122. static int cmac_generate_subkeys( mbedtls_cipher_context_t *ctx,
  123. unsigned char* K1, unsigned char* K2 )
  124. {
  125. int ret;
  126. unsigned char L[MBEDTLS_CIPHER_BLKSIZE_MAX];
  127. size_t olen, block_size;
  128. mbedtls_zeroize( L, sizeof( L ) );
  129. block_size = ctx->cipher_info->block_size;
  130. /* Calculate Ek(0) */
  131. if( ( ret = mbedtls_cipher_update( ctx, L, block_size, L, &olen ) ) != 0 )
  132. goto exit;
  133. /*
  134. * Generate K1 and K2
  135. */
  136. if( ( ret = cmac_multiply_by_u( K1, L , block_size ) ) != 0 )
  137. goto exit;
  138. if( ( ret = cmac_multiply_by_u( K2, K1 , block_size ) ) != 0 )
  139. goto exit;
  140. exit:
  141. mbedtls_zeroize( L, sizeof( L ) );
  142. return( ret );
  143. }
  144. static void cmac_xor_block( unsigned char *output, const unsigned char *input1,
  145. const unsigned char *input2,
  146. const size_t block_size )
  147. {
  148. size_t index;
  149. for( index = 0; index < block_size; index++ )
  150. output[ index ] = input1[ index ] ^ input2[ index ];
  151. }
  152. /*
  153. * Create padded last block from (partial) last block.
  154. *
  155. * We can't use the padding option from the cipher layer, as it only works for
  156. * CBC and we use ECB mode, and anyway we need to XOR K1 or K2 in addition.
  157. */
  158. static void cmac_pad( unsigned char padded_block[MBEDTLS_CIPHER_BLKSIZE_MAX],
  159. size_t padded_block_len,
  160. const unsigned char *last_block,
  161. size_t last_block_len )
  162. {
  163. size_t j;
  164. for( j = 0; j < padded_block_len; j++ )
  165. {
  166. if( j < last_block_len )
  167. padded_block[j] = last_block[j];
  168. else if( j == last_block_len )
  169. padded_block[j] = 0x80;
  170. else
  171. padded_block[j] = 0x00;
  172. }
  173. }
  174. int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
  175. const unsigned char *key, size_t keybits )
  176. {
  177. mbedtls_cipher_type_t type;
  178. mbedtls_cmac_context_t *cmac_ctx;
  179. int retval;
  180. if( ctx == NULL || ctx->cipher_info == NULL || key == NULL )
  181. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  182. if( ( retval = mbedtls_cipher_setkey( ctx, key, keybits,
  183. MBEDTLS_ENCRYPT ) ) != 0 )
  184. return( retval );
  185. type = ctx->cipher_info->type;
  186. switch( type )
  187. {
  188. case MBEDTLS_CIPHER_AES_128_ECB:
  189. case MBEDTLS_CIPHER_AES_192_ECB:
  190. case MBEDTLS_CIPHER_AES_256_ECB:
  191. case MBEDTLS_CIPHER_DES_EDE3_ECB:
  192. break;
  193. default:
  194. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  195. }
  196. /* Allocated and initialise in the cipher context memory for the CMAC
  197. * context */
  198. cmac_ctx = mbedtls_calloc( 1, sizeof( mbedtls_cmac_context_t ) );
  199. if( cmac_ctx == NULL )
  200. return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
  201. ctx->cmac_ctx = cmac_ctx;
  202. mbedtls_zeroize( cmac_ctx->state, sizeof( cmac_ctx->state ) );
  203. return 0;
  204. }
  205. int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
  206. const unsigned char *input, size_t ilen )
  207. {
  208. mbedtls_cmac_context_t* cmac_ctx;
  209. unsigned char *state;
  210. int n, j, ret = 0;
  211. size_t olen, block_size;
  212. if( ctx == NULL || ctx->cipher_info == NULL || input == NULL ||
  213. ctx->cmac_ctx == NULL )
  214. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  215. cmac_ctx = ctx->cmac_ctx;
  216. block_size = ctx->cipher_info->block_size;
  217. state = ctx->cmac_ctx->state;
  218. /* Is there data still to process from the last call, that's greater in
  219. * size than a block? */
  220. if( cmac_ctx->unprocessed_len > 0 &&
  221. ilen > block_size - cmac_ctx->unprocessed_len )
  222. {
  223. memcpy( &cmac_ctx->unprocessed_block[cmac_ctx->unprocessed_len],
  224. input,
  225. block_size - cmac_ctx->unprocessed_len );
  226. cmac_xor_block( state, cmac_ctx->unprocessed_block, state, block_size );
  227. if( ( ret = mbedtls_cipher_update( ctx, state, block_size, state,
  228. &olen ) ) != 0 )
  229. {
  230. goto exit;
  231. }
  232. input += block_size - cmac_ctx->unprocessed_len;
  233. ilen -= block_size - cmac_ctx->unprocessed_len;
  234. cmac_ctx->unprocessed_len = 0;
  235. }
  236. /* n is the number of blocks including any final partial block */
  237. n = ( ilen + block_size - 1 ) / block_size;
  238. /* Iterate across the input data in block sized chunks */
  239. for( j = 0; j < n - 1; j++ )
  240. {
  241. cmac_xor_block( state, input, state, block_size );
  242. if( ( ret = mbedtls_cipher_update( ctx, state, block_size, state,
  243. &olen ) ) != 0 )
  244. goto exit;
  245. ilen -= block_size;
  246. input += block_size;
  247. }
  248. /* If there is data left over that wasn't aligned to a block */
  249. if( ilen > 0 )
  250. {
  251. memcpy( &cmac_ctx->unprocessed_block[cmac_ctx->unprocessed_len],
  252. input,
  253. ilen );
  254. cmac_ctx->unprocessed_len += ilen;
  255. }
  256. exit:
  257. return( ret );
  258. }
  259. int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
  260. unsigned char *output )
  261. {
  262. mbedtls_cmac_context_t* cmac_ctx;
  263. unsigned char *state, *last_block;
  264. unsigned char K1[MBEDTLS_CIPHER_BLKSIZE_MAX];
  265. unsigned char K2[MBEDTLS_CIPHER_BLKSIZE_MAX];
  266. unsigned char M_last[MBEDTLS_CIPHER_BLKSIZE_MAX];
  267. int ret;
  268. size_t olen, block_size;
  269. if( ctx == NULL || ctx->cipher_info == NULL || ctx->cmac_ctx == NULL ||
  270. output == NULL )
  271. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  272. cmac_ctx = ctx->cmac_ctx;
  273. block_size = ctx->cipher_info->block_size;
  274. state = cmac_ctx->state;
  275. mbedtls_zeroize( K1, sizeof( K1 ) );
  276. mbedtls_zeroize( K2, sizeof( K2 ) );
  277. cmac_generate_subkeys( ctx, K1, K2 );
  278. last_block = cmac_ctx->unprocessed_block;
  279. /* Calculate last block */
  280. if( cmac_ctx->unprocessed_len < block_size )
  281. {
  282. cmac_pad( M_last, block_size, last_block, cmac_ctx->unprocessed_len );
  283. cmac_xor_block( M_last, M_last, K2, block_size );
  284. }
  285. else
  286. {
  287. /* Last block is complete block */
  288. cmac_xor_block( M_last, last_block, K1, block_size );
  289. }
  290. cmac_xor_block( state, M_last, state, block_size );
  291. if( ( ret = mbedtls_cipher_update( ctx, state, block_size, state,
  292. &olen ) ) != 0 )
  293. {
  294. goto exit;
  295. }
  296. memcpy( output, state, block_size );
  297. exit:
  298. /* Wipe the generated keys on the stack, and any other transients to avoid
  299. * side channel leakage */
  300. mbedtls_zeroize( K1, sizeof( K1 ) );
  301. mbedtls_zeroize( K2, sizeof( K2 ) );
  302. cmac_ctx->unprocessed_len = 0;
  303. mbedtls_zeroize( cmac_ctx->unprocessed_block,
  304. sizeof( cmac_ctx->unprocessed_block ) );
  305. mbedtls_zeroize( state, MBEDTLS_CIPHER_BLKSIZE_MAX );
  306. return( ret );
  307. }
  308. int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx )
  309. {
  310. mbedtls_cmac_context_t* cmac_ctx;
  311. if( ctx == NULL || ctx->cipher_info == NULL || ctx->cmac_ctx == NULL )
  312. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  313. cmac_ctx = ctx->cmac_ctx;
  314. /* Reset the internal state */
  315. cmac_ctx->unprocessed_len = 0;
  316. mbedtls_zeroize( cmac_ctx->unprocessed_block,
  317. sizeof( cmac_ctx->unprocessed_block ) );
  318. mbedtls_zeroize( cmac_ctx->state,
  319. sizeof( cmac_ctx->state ) );
  320. return( 0 );
  321. }
  322. int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
  323. const unsigned char *key, size_t keylen,
  324. const unsigned char *input, size_t ilen,
  325. unsigned char *output )
  326. {
  327. mbedtls_cipher_context_t ctx;
  328. int ret;
  329. if( cipher_info == NULL || key == NULL || input == NULL || output == NULL )
  330. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  331. mbedtls_cipher_init( &ctx );
  332. if( ( ret = mbedtls_cipher_setup( &ctx, cipher_info ) ) != 0 )
  333. goto exit;
  334. ret = mbedtls_cipher_cmac_starts( &ctx, key, keylen );
  335. if( ret != 0 )
  336. goto exit;
  337. ret = mbedtls_cipher_cmac_update( &ctx, input, ilen );
  338. if( ret != 0 )
  339. goto exit;
  340. ret = mbedtls_cipher_cmac_finish( &ctx, output );
  341. exit:
  342. mbedtls_cipher_free( &ctx );
  343. return( ret );
  344. }
  345. #if defined(MBEDTLS_AES_C)
  346. /*
  347. * Implementation of AES-CMAC-PRF-128 defined in RFC 4615
  348. */
  349. int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_length,
  350. const unsigned char *input, size_t in_len,
  351. unsigned char *output )
  352. {
  353. int ret;
  354. const mbedtls_cipher_info_t *cipher_info;
  355. unsigned char zero_key[MBEDTLS_AES_BLOCK_SIZE];
  356. unsigned char int_key[MBEDTLS_AES_BLOCK_SIZE];
  357. if( key == NULL || input == NULL || output == NULL )
  358. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  359. cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
  360. if( cipher_info == NULL )
  361. {
  362. /* Failing at this point must be due to a build issue */
  363. ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  364. goto exit;
  365. }
  366. if( key_length == MBEDTLS_AES_BLOCK_SIZE )
  367. {
  368. /* Use key as is */
  369. memcpy( int_key, key, MBEDTLS_AES_BLOCK_SIZE );
  370. }
  371. else
  372. {
  373. memset( zero_key, 0, MBEDTLS_AES_BLOCK_SIZE );
  374. ret = mbedtls_cipher_cmac( cipher_info, zero_key, 128, key,
  375. key_length, int_key );
  376. if( ret != 0 )
  377. goto exit;
  378. }
  379. ret = mbedtls_cipher_cmac( cipher_info, int_key, 128, input, in_len,
  380. output );
  381. exit:
  382. mbedtls_zeroize( int_key, sizeof( int_key ) );
  383. return( ret );
  384. }
  385. #endif /* MBEDTLS_AES_C */
  386. #if defined(MBEDTLS_SELF_TEST)
  387. /*
  388. * CMAC test data from SP800-38B Appendix D.1 (corrected)
  389. * http://csrc.nist.gov/publications/nistpubs/800-38B/Updated_CMAC_Examples.pdf
  390. *
  391. * AES-CMAC-PRF-128 test data from RFC 4615
  392. * https://tools.ietf.org/html/rfc4615#page-4
  393. */
  394. #define NB_CMAC_TESTS_PER_KEY 4
  395. #define NB_PRF_TESTS 3
  396. #if defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C)
  397. /* All CMAC test inputs are truncated from the same 64 byte buffer. */
  398. static const unsigned char test_message[] = {
  399. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  400. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  401. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  402. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  403. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  404. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  405. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  406. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  407. };
  408. #endif /* MBEDTLS_AES_C || MBEDTLS_DES_C */
  409. #if defined(MBEDTLS_AES_C)
  410. /* Truncation point of message for AES CMAC tests */
  411. static const unsigned int aes_message_lengths[NB_CMAC_TESTS_PER_KEY] = {
  412. 0,
  413. 16,
  414. 40,
  415. 64
  416. };
  417. /* AES 128 CMAC Test Data */
  418. static const unsigned char aes_128_key[16] = {
  419. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  420. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  421. };
  422. static const unsigned char aes_128_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = {
  423. {
  424. 0xfb, 0xee, 0xd6, 0x18, 0x35, 0x71, 0x33, 0x66,
  425. 0x7c, 0x85, 0xe0, 0x8f, 0x72, 0x36, 0xa8, 0xde
  426. },
  427. {
  428. 0xf7, 0xdd, 0xac, 0x30, 0x6a, 0xe2, 0x66, 0xcc,
  429. 0xf9, 0x0b, 0xc1, 0x1e, 0xe4, 0x6d, 0x51, 0x3b
  430. }
  431. };
  432. static const unsigned char aes_128_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] = {
  433. {
  434. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  435. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  436. },
  437. {
  438. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  439. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  440. },
  441. {
  442. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  443. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  444. },
  445. {
  446. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  447. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  448. }
  449. };
  450. /* AES 192 CMAC Test Data */
  451. static const unsigned char aes_192_key[24] = {
  452. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  453. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  454. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  455. };
  456. static const unsigned char aes_192_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = {
  457. {
  458. 0x44, 0x8a, 0x5b, 0x1c, 0x93, 0x51, 0x4b, 0x27,
  459. 0x3e, 0xe6, 0x43, 0x9d, 0xd4, 0xda, 0xa2, 0x96
  460. },
  461. {
  462. 0x89, 0x14, 0xb6, 0x39, 0x26, 0xa2, 0x96, 0x4e,
  463. 0x7d, 0xcc, 0x87, 0x3b, 0xa9, 0xb5, 0x45, 0x2c
  464. }
  465. };
  466. static const unsigned char aes_192_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] = {
  467. {
  468. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  469. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  470. },
  471. {
  472. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  473. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  474. },
  475. {
  476. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  477. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  478. },
  479. {
  480. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  481. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  482. }
  483. };
  484. /* AES 256 CMAC Test Data */
  485. static const unsigned char aes_256_key[32] = {
  486. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  487. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  488. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  489. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  490. };
  491. static const unsigned char aes_256_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = {
  492. {
  493. 0xca, 0xd1, 0xed, 0x03, 0x29, 0x9e, 0xed, 0xac,
  494. 0x2e, 0x9a, 0x99, 0x80, 0x86, 0x21, 0x50, 0x2f
  495. },
  496. {
  497. 0x95, 0xa3, 0xda, 0x06, 0x53, 0x3d, 0xdb, 0x58,
  498. 0x5d, 0x35, 0x33, 0x01, 0x0c, 0x42, 0xa0, 0xd9
  499. }
  500. };
  501. static const unsigned char aes_256_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] = {
  502. {
  503. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  504. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  505. },
  506. {
  507. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  508. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  509. },
  510. {
  511. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  512. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  513. },
  514. {
  515. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  516. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  517. }
  518. };
  519. #endif /* MBEDTLS_AES_C */
  520. #if defined(MBEDTLS_DES_C)
  521. /* Truncation point of message for 3DES CMAC tests */
  522. static const unsigned int des3_message_lengths[NB_CMAC_TESTS_PER_KEY] = {
  523. 0,
  524. 8,
  525. 20,
  526. 32
  527. };
  528. /* 3DES 2 Key CMAC Test Data */
  529. static const unsigned char des3_2key_key[24] = {
  530. 0x4c, 0xf1, 0x51, 0x34, 0xa2, 0x85, 0x0d, 0xd5,
  531. 0x8a, 0x3d, 0x10, 0xba, 0x80, 0x57, 0x0d, 0x38,
  532. 0x4c, 0xf1, 0x51, 0x34, 0xa2, 0x85, 0x0d, 0xd5
  533. };
  534. static const unsigned char des3_2key_subkeys[2][8] = {
  535. {
  536. 0x8e, 0xcf, 0x37, 0x3e, 0xd7, 0x1a, 0xfa, 0xef
  537. },
  538. {
  539. 0x1d, 0x9e, 0x6e, 0x7d, 0xae, 0x35, 0xf5, 0xc5
  540. }
  541. };
  542. static const unsigned char des3_2key_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_DES3_BLOCK_SIZE] = {
  543. {
  544. 0xbd, 0x2e, 0xbf, 0x9a, 0x3b, 0xa0, 0x03, 0x61
  545. },
  546. {
  547. 0x4f, 0xf2, 0xab, 0x81, 0x3c, 0x53, 0xce, 0x83
  548. },
  549. {
  550. 0x62, 0xdd, 0x1b, 0x47, 0x19, 0x02, 0xbd, 0x4e
  551. },
  552. {
  553. 0x31, 0xb1, 0xe4, 0x31, 0xda, 0xbc, 0x4e, 0xb8
  554. }
  555. };
  556. /* 3DES 3 Key CMAC Test Data */
  557. static const unsigned char des3_3key_key[24] = {
  558. 0x8a, 0xa8, 0x3b, 0xf8, 0xcb, 0xda, 0x10, 0x62,
  559. 0x0b, 0xc1, 0xbf, 0x19, 0xfb, 0xb6, 0xcd, 0x58,
  560. 0xbc, 0x31, 0x3d, 0x4a, 0x37, 0x1c, 0xa8, 0xb5
  561. };
  562. static const unsigned char des3_3key_subkeys[2][8] = {
  563. {
  564. 0x91, 0x98, 0xe9, 0xd3, 0x14, 0xe6, 0x53, 0x5f
  565. },
  566. {
  567. 0x23, 0x31, 0xd3, 0xa6, 0x29, 0xcc, 0xa6, 0xa5
  568. }
  569. };
  570. static const unsigned char des3_3key_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_DES3_BLOCK_SIZE] = {
  571. {
  572. 0xb7, 0xa6, 0x88, 0xe1, 0x22, 0xff, 0xaf, 0x95
  573. },
  574. {
  575. 0x8e, 0x8f, 0x29, 0x31, 0x36, 0x28, 0x37, 0x97
  576. },
  577. {
  578. 0x74, 0x3d, 0xdb, 0xe0, 0xce, 0x2d, 0xc2, 0xed
  579. },
  580. {
  581. 0x33, 0xe6, 0xb1, 0x09, 0x24, 0x00, 0xea, 0xe5
  582. }
  583. };
  584. #endif /* MBEDTLS_DES_C */
  585. #if defined(MBEDTLS_AES_C)
  586. /* AES AES-CMAC-PRF-128 Test Data */
  587. static const unsigned char PRFK[] = {
  588. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  589. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  590. 0xed, 0xcb
  591. };
  592. /* Sizes in bytes */
  593. static const size_t PRFKlen[NB_PRF_TESTS] = {
  594. 18,
  595. 16,
  596. 10
  597. };
  598. /* PRF M */
  599. static const unsigned char PRFM[] = {
  600. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  601. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  602. 0x10, 0x11, 0x12, 0x13
  603. };
  604. static const unsigned char PRFT[NB_PRF_TESTS][16] = {
  605. {
  606. 0x84, 0xa3, 0x48, 0xa4, 0xa4, 0x5d, 0x23, 0x5b,
  607. 0xab, 0xff, 0xfc, 0x0d, 0x2b, 0x4d, 0xa0, 0x9a
  608. },
  609. {
  610. 0x98, 0x0a, 0xe8, 0x7b, 0x5f, 0x4c, 0x9c, 0x52,
  611. 0x14, 0xf5, 0xb6, 0xa8, 0x45, 0x5e, 0x4c, 0x2d
  612. },
  613. {
  614. 0x29, 0x0d, 0x9e, 0x11, 0x2e, 0xdb, 0x09, 0xee,
  615. 0x14, 0x1f, 0xcf, 0x64, 0xc0, 0xb7, 0x2f, 0x3d
  616. }
  617. };
  618. #endif /* MBEDTLS_AES_C */
  619. static int cmac_test_subkeys( int verbose,
  620. const char* testname,
  621. const unsigned char* key,
  622. int keybits,
  623. const unsigned char* subkeys,
  624. mbedtls_cipher_type_t cipher_type,
  625. int block_size,
  626. int num_tests )
  627. {
  628. int i, ret;
  629. mbedtls_cipher_context_t ctx;
  630. const mbedtls_cipher_info_t *cipher_info;
  631. unsigned char K1[MBEDTLS_CIPHER_BLKSIZE_MAX];
  632. unsigned char K2[MBEDTLS_CIPHER_BLKSIZE_MAX];
  633. cipher_info = mbedtls_cipher_info_from_type( cipher_type );
  634. if( cipher_info == NULL )
  635. {
  636. /* Failing at this point must be due to a build issue */
  637. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  638. }
  639. for( i = 0; i < num_tests; i++ )
  640. {
  641. if( verbose != 0 )
  642. mbedtls_printf( " %s CMAC subkey #%u: ", testname, i + 1 );
  643. mbedtls_cipher_init( &ctx );
  644. if( ( ret = mbedtls_cipher_setup( &ctx, cipher_info ) ) != 0 )
  645. {
  646. if( verbose != 0 )
  647. mbedtls_printf( "test execution failed\n" );
  648. goto cleanup;
  649. }
  650. if( ( ret = mbedtls_cipher_setkey( &ctx, key, keybits,
  651. MBEDTLS_ENCRYPT ) ) != 0 )
  652. {
  653. if( verbose != 0 )
  654. mbedtls_printf( "test execution failed\n" );
  655. goto cleanup;
  656. }
  657. ret = cmac_generate_subkeys( &ctx, K1, K2 );
  658. if( ret != 0 )
  659. {
  660. if( verbose != 0 )
  661. mbedtls_printf( "failed\n" );
  662. goto cleanup;
  663. }
  664. if( ( ret = memcmp( K1, subkeys, block_size ) ) != 0 ||
  665. ( ret = memcmp( K2, &subkeys[block_size], block_size ) ) != 0 )
  666. {
  667. if( verbose != 0 )
  668. mbedtls_printf( "failed\n" );
  669. goto cleanup;
  670. }
  671. if( verbose != 0 )
  672. mbedtls_printf( "passed\n" );
  673. mbedtls_cipher_free( &ctx );
  674. }
  675. goto exit;
  676. cleanup:
  677. mbedtls_cipher_free( &ctx );
  678. exit:
  679. return( ret );
  680. }
  681. static int cmac_test_wth_cipher( int verbose,
  682. const char* testname,
  683. const unsigned char* key,
  684. int keybits,
  685. const unsigned char* messages,
  686. const unsigned int message_lengths[4],
  687. const unsigned char* expected_result,
  688. mbedtls_cipher_type_t cipher_type,
  689. int block_size,
  690. int num_tests )
  691. {
  692. const mbedtls_cipher_info_t *cipher_info;
  693. int i, ret;
  694. unsigned char output[MBEDTLS_CIPHER_BLKSIZE_MAX];
  695. cipher_info = mbedtls_cipher_info_from_type( cipher_type );
  696. if( cipher_info == NULL )
  697. {
  698. /* Failing at this point must be due to a build issue */
  699. ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  700. goto exit;
  701. }
  702. for( i = 0; i < num_tests; i++ )
  703. {
  704. if( verbose != 0 )
  705. mbedtls_printf( " %s CMAC #%u: ", testname, i + 1 );
  706. if( ( ret = mbedtls_cipher_cmac( cipher_info, key, keybits, messages,
  707. message_lengths[i], output ) ) != 0 )
  708. {
  709. if( verbose != 0 )
  710. mbedtls_printf( "failed\n" );
  711. goto exit;
  712. }
  713. if( ( ret = memcmp( output, &expected_result[i * block_size], block_size ) ) != 0 )
  714. {
  715. if( verbose != 0 )
  716. mbedtls_printf( "failed\n" );
  717. goto exit;
  718. }
  719. if( verbose != 0 )
  720. mbedtls_printf( "passed\n" );
  721. }
  722. exit:
  723. return( ret );
  724. }
  725. #if defined(MBEDTLS_AES_C)
  726. static int test_aes128_cmac_prf( int verbose )
  727. {
  728. int i;
  729. int ret;
  730. unsigned char output[MBEDTLS_AES_BLOCK_SIZE];
  731. for( i = 0; i < NB_PRF_TESTS; i++ )
  732. {
  733. mbedtls_printf( " AES CMAC 128 PRF #%u: ", i );
  734. ret = mbedtls_aes_cmac_prf_128( PRFK, PRFKlen[i], PRFM, 20, output );
  735. if( ret != 0 ||
  736. memcmp( output, PRFT[i], MBEDTLS_AES_BLOCK_SIZE ) != 0 )
  737. {
  738. if( verbose != 0 )
  739. mbedtls_printf( "failed\n" );
  740. return( ret );
  741. }
  742. else if( verbose != 0 )
  743. {
  744. mbedtls_printf( "passed\n" );
  745. }
  746. }
  747. return( ret );
  748. }
  749. #endif /* MBEDTLS_AES_C */
  750. int mbedtls_cmac_self_test( int verbose )
  751. {
  752. int ret;
  753. #if defined(MBEDTLS_AES_C)
  754. /* AES-128 */
  755. if( ( ret = cmac_test_subkeys( verbose,
  756. "AES 128",
  757. aes_128_key,
  758. 128,
  759. (const unsigned char*)aes_128_subkeys,
  760. MBEDTLS_CIPHER_AES_128_ECB,
  761. MBEDTLS_AES_BLOCK_SIZE,
  762. NB_CMAC_TESTS_PER_KEY ) ) != 0 )
  763. {
  764. return( ret );
  765. }
  766. if( ( ret = cmac_test_wth_cipher( verbose,
  767. "AES 128",
  768. aes_128_key,
  769. 128,
  770. test_message,
  771. aes_message_lengths,
  772. (const unsigned char*)aes_128_expected_result,
  773. MBEDTLS_CIPHER_AES_128_ECB,
  774. MBEDTLS_AES_BLOCK_SIZE,
  775. NB_CMAC_TESTS_PER_KEY ) ) != 0 )
  776. {
  777. return( ret );
  778. }
  779. /* AES-192 */
  780. if( ( ret = cmac_test_subkeys( verbose,
  781. "AES 192",
  782. aes_192_key,
  783. 192,
  784. (const unsigned char*)aes_192_subkeys,
  785. MBEDTLS_CIPHER_AES_192_ECB,
  786. MBEDTLS_AES_BLOCK_SIZE,
  787. NB_CMAC_TESTS_PER_KEY ) ) != 0 )
  788. {
  789. return( ret );
  790. }
  791. if( ( ret = cmac_test_wth_cipher( verbose,
  792. "AES 192",
  793. aes_192_key,
  794. 192,
  795. test_message,
  796. aes_message_lengths,
  797. (const unsigned char*)aes_192_expected_result,
  798. MBEDTLS_CIPHER_AES_192_ECB,
  799. MBEDTLS_AES_BLOCK_SIZE,
  800. NB_CMAC_TESTS_PER_KEY ) ) != 0 )
  801. {
  802. return( ret );
  803. }
  804. /* AES-256 */
  805. if( ( ret = cmac_test_subkeys( verbose,
  806. "AES 256",
  807. aes_256_key,
  808. 256,
  809. (const unsigned char*)aes_256_subkeys,
  810. MBEDTLS_CIPHER_AES_256_ECB,
  811. MBEDTLS_AES_BLOCK_SIZE,
  812. NB_CMAC_TESTS_PER_KEY ) ) != 0 )
  813. {
  814. return( ret );
  815. }
  816. if( ( ret = cmac_test_wth_cipher ( verbose,
  817. "AES 256",
  818. aes_256_key,
  819. 256,
  820. test_message,
  821. aes_message_lengths,
  822. (const unsigned char*)aes_256_expected_result,
  823. MBEDTLS_CIPHER_AES_256_ECB,
  824. MBEDTLS_AES_BLOCK_SIZE,
  825. NB_CMAC_TESTS_PER_KEY ) ) != 0 )
  826. {
  827. return( ret );
  828. }
  829. #endif /* MBEDTLS_AES_C */
  830. #if defined(MBEDTLS_DES_C)
  831. /* 3DES 2 key */
  832. if( ( ret = cmac_test_subkeys( verbose,
  833. "3DES 2 key",
  834. des3_2key_key,
  835. 192,
  836. (const unsigned char*)des3_2key_subkeys,
  837. MBEDTLS_CIPHER_DES_EDE3_ECB,
  838. MBEDTLS_DES3_BLOCK_SIZE,
  839. NB_CMAC_TESTS_PER_KEY ) ) != 0 )
  840. {
  841. return( ret );
  842. }
  843. if( ( ret = cmac_test_wth_cipher( verbose,
  844. "3DES 2 key",
  845. des3_2key_key,
  846. 192,
  847. test_message,
  848. des3_message_lengths,
  849. (const unsigned char*)des3_2key_expected_result,
  850. MBEDTLS_CIPHER_DES_EDE3_ECB,
  851. MBEDTLS_DES3_BLOCK_SIZE,
  852. NB_CMAC_TESTS_PER_KEY ) ) != 0 )
  853. {
  854. return( ret );
  855. }
  856. /* 3DES 3 key */
  857. if( ( ret = cmac_test_subkeys( verbose,
  858. "3DES 3 key",
  859. des3_3key_key,
  860. 192,
  861. (const unsigned char*)des3_3key_subkeys,
  862. MBEDTLS_CIPHER_DES_EDE3_ECB,
  863. MBEDTLS_DES3_BLOCK_SIZE,
  864. NB_CMAC_TESTS_PER_KEY ) ) != 0 )
  865. {
  866. return( ret );
  867. }
  868. if( ( ret = cmac_test_wth_cipher( verbose,
  869. "3DES 3 key",
  870. des3_3key_key,
  871. 192,
  872. test_message,
  873. des3_message_lengths,
  874. (const unsigned char*)des3_3key_expected_result,
  875. MBEDTLS_CIPHER_DES_EDE3_ECB,
  876. MBEDTLS_DES3_BLOCK_SIZE,
  877. NB_CMAC_TESTS_PER_KEY ) ) != 0 )
  878. {
  879. return( ret );
  880. }
  881. #endif /* MBEDTLS_DES_C */
  882. #if defined(MBEDTLS_AES_C)
  883. if( ( ret = test_aes128_cmac_prf( verbose ) ) != 0 )
  884. return( ret );
  885. #endif /* MBEDTLS_AES_C */
  886. if( verbose != 0 )
  887. mbedtls_printf( "\n" );
  888. return( 0 );
  889. }
  890. #endif /* MBEDTLS_SELF_TEST */
  891. #endif /* MBEDTLS_CMAC_C */