cmac.c 32 KB

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