cmac.c 35 KB

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