cmac.c 33 KB

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