cmac.c 32 KB

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