cmac.c 32 KB

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