ccm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*
  2. * NIST SP800-38C compliant CCM implementation
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /*
  20. * Definition of CCM:
  21. * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
  22. * RFC 3610 "Counter with CBC-MAC (CCM)"
  23. *
  24. * Related:
  25. * RFC 5116 "An Interface and Algorithms for Authenticated Encryption"
  26. */
  27. #include "common.h"
  28. #if defined(MBEDTLS_CCM_C)
  29. #include "mbedtls/ccm.h"
  30. #include "mbedtls/platform_util.h"
  31. #include "mbedtls/error.h"
  32. #include <string.h>
  33. #if defined(MBEDTLS_PLATFORM_C)
  34. #include "mbedtls/platform.h"
  35. #else
  36. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  37. #include <stdio.h>
  38. #define mbedtls_printf printf
  39. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  40. #endif /* MBEDTLS_PLATFORM_C */
  41. #if !defined(MBEDTLS_CCM_ALT)
  42. /*
  43. * Initialize context
  44. */
  45. void mbedtls_ccm_init( mbedtls_ccm_context *ctx )
  46. {
  47. memset( ctx, 0, sizeof( mbedtls_ccm_context ) );
  48. }
  49. int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
  50. mbedtls_cipher_id_t cipher,
  51. const unsigned char *key,
  52. unsigned int keybits )
  53. {
  54. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  55. const mbedtls_cipher_info_t *cipher_info;
  56. cipher_info = mbedtls_cipher_info_from_values( cipher, keybits,
  57. MBEDTLS_MODE_ECB );
  58. if( cipher_info == NULL )
  59. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  60. if( cipher_info->block_size != 16 )
  61. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  62. mbedtls_cipher_free( &ctx->cipher_ctx );
  63. if( ( ret = mbedtls_cipher_setup( &ctx->cipher_ctx, cipher_info ) ) != 0 )
  64. return( ret );
  65. if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keybits,
  66. MBEDTLS_ENCRYPT ) ) != 0 )
  67. {
  68. return( ret );
  69. }
  70. return( 0 );
  71. }
  72. /*
  73. * Free context
  74. */
  75. void mbedtls_ccm_free( mbedtls_ccm_context *ctx )
  76. {
  77. if( ctx == NULL )
  78. return;
  79. mbedtls_cipher_free( &ctx->cipher_ctx );
  80. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ccm_context ) );
  81. }
  82. #define CCM_STATE__CLEAR 0
  83. #define CCM_STATE__STARTED (1 << 0)
  84. #define CCM_STATE__LENGHTS_SET (1 << 1)
  85. #define CCM_STATE__AUTH_DATA_STARTED (1 << 2)
  86. #define CCM_STATE__AUTH_DATA_FINISHED (1 << 3)
  87. #define CCM_STATE__ERROR (1 << 4)
  88. /*
  89. * Encrypt or decrypt a partial block with CTR
  90. */
  91. static int mbedtls_ccm_crypt( mbedtls_ccm_context *ctx,
  92. size_t offset, size_t use_len,
  93. const unsigned char *input,
  94. unsigned char *output )
  95. {
  96. size_t i;
  97. size_t olen = 0;
  98. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  99. unsigned char tmp_buf[16] = {0};
  100. if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctx->ctr, 16, tmp_buf,
  101. &olen ) ) != 0 )
  102. {
  103. ctx->state |= CCM_STATE__ERROR;
  104. mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf));
  105. return ret;
  106. }
  107. for( i = 0; i < use_len; i++ )
  108. output[i] = input[i] ^ tmp_buf[offset + i];
  109. mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf));
  110. return ret;
  111. }
  112. static void mbedtls_ccm_clear_state(mbedtls_ccm_context *ctx) {
  113. ctx->state = CCM_STATE__CLEAR;
  114. memset( ctx->y, 0, 16);
  115. memset( ctx->ctr, 0, 16);
  116. }
  117. static int ccm_calculate_first_block_if_ready(mbedtls_ccm_context *ctx)
  118. {
  119. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  120. unsigned char i;
  121. size_t len_left, olen;
  122. /* length calulcation can be done only after both
  123. * mbedtls_ccm_starts() and mbedtls_ccm_set_lengths() have been executed
  124. */
  125. if( !(ctx->state & CCM_STATE__STARTED) || !(ctx->state & CCM_STATE__LENGHTS_SET) )
  126. return 0;
  127. /* CCM expects non-empty tag.
  128. * CCM* allows empty tag. For CCM* without tag, ignore plaintext length.
  129. */
  130. if( ctx->tag_len == 0 )
  131. {
  132. if( ctx->mode == MBEDTLS_CCM_STAR_ENCRYPT || ctx->mode == MBEDTLS_CCM_STAR_DECRYPT )
  133. {
  134. ctx->plaintext_len = 0;
  135. }
  136. else
  137. {
  138. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  139. }
  140. }
  141. /*
  142. * First block:
  143. * 0 .. 0 flags
  144. * 1 .. iv_len nonce (aka iv) - set by: mbedtls_ccm_starts()
  145. * iv_len+1 .. 15 length
  146. *
  147. * With flags as (bits):
  148. * 7 0
  149. * 6 add present?
  150. * 5 .. 3 (t - 2) / 2
  151. * 2 .. 0 q - 1
  152. */
  153. ctx->y[0] |= ( ctx->add_len > 0 ) << 6;
  154. ctx->y[0] |= ( ( ctx->tag_len - 2 ) / 2 ) << 3;
  155. ctx->y[0] |= ctx->q - 1;
  156. for( i = 0, len_left = ctx->plaintext_len; i < ctx->q; i++, len_left >>= 8 )
  157. ctx->y[15-i] = MBEDTLS_BYTE_0( len_left );
  158. if( len_left > 0 )
  159. {
  160. ctx->state |= CCM_STATE__ERROR;
  161. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  162. }
  163. /* Start CBC-MAC with first block*/
  164. if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen ) ) != 0 )
  165. {
  166. ctx->state |= CCM_STATE__ERROR;
  167. return( ret );
  168. }
  169. return (0);
  170. }
  171. int mbedtls_ccm_starts( mbedtls_ccm_context *ctx,
  172. int mode,
  173. const unsigned char *iv,
  174. size_t iv_len )
  175. {
  176. /* Also implies q is within bounds */
  177. if( iv_len < 7 || iv_len > 13 )
  178. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  179. ctx->mode = mode;
  180. ctx->q = 16 - 1 - (unsigned char) iv_len;
  181. /*
  182. * Prepare counter block for encryption:
  183. * 0 .. 0 flags
  184. * 1 .. iv_len nonce (aka iv)
  185. * iv_len+1 .. 15 counter (initially 1)
  186. *
  187. * With flags as (bits):
  188. * 7 .. 3 0
  189. * 2 .. 0 q - 1
  190. */
  191. memset( ctx->ctr, 0, 16);
  192. ctx->ctr[0] = ctx->q - 1;
  193. memcpy( ctx->ctr + 1, iv, iv_len );
  194. memset( ctx->ctr + 1 + iv_len, 0, ctx->q );
  195. ctx->ctr[15] = 1;
  196. /*
  197. * See ccm_calculate_first_block_if_ready() for block layout description
  198. */
  199. memcpy( ctx->y + 1, iv, iv_len );
  200. ctx->state |= CCM_STATE__STARTED;
  201. return ccm_calculate_first_block_if_ready(ctx);
  202. }
  203. int mbedtls_ccm_set_lengths( mbedtls_ccm_context *ctx,
  204. size_t total_ad_len,
  205. size_t plaintext_len,
  206. size_t tag_len )
  207. {
  208. /*
  209. * Check length requirements: SP800-38C A.1
  210. * Additional requirement: a < 2^16 - 2^8 to simplify the code.
  211. * 'length' checked later (when writing it to the first block)
  212. *
  213. * Also, loosen the requirements to enable support for CCM* (IEEE 802.15.4).
  214. */
  215. if( tag_len == 2 || tag_len > 16 || tag_len % 2 != 0 )
  216. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  217. if( total_ad_len >= 0xFF00 )
  218. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  219. ctx->plaintext_len = plaintext_len;
  220. ctx->add_len = total_ad_len;
  221. ctx->tag_len = tag_len;
  222. ctx->processed = 0;
  223. ctx->state |= CCM_STATE__LENGHTS_SET;
  224. return ccm_calculate_first_block_if_ready(ctx);
  225. }
  226. int mbedtls_ccm_update_ad( mbedtls_ccm_context *ctx,
  227. const unsigned char *add,
  228. size_t add_len )
  229. {
  230. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  231. unsigned char i;
  232. size_t olen, use_len, offset;
  233. if( ctx->state & CCM_STATE__ERROR )
  234. {
  235. return MBEDTLS_ERR_CCM_BAD_INPUT;
  236. }
  237. if( add_len > 0 )
  238. {
  239. if( ctx->state & CCM_STATE__AUTH_DATA_FINISHED )
  240. {
  241. return MBEDTLS_ERR_CCM_BAD_INPUT;
  242. }
  243. if( !(ctx->state & CCM_STATE__AUTH_DATA_STARTED) )
  244. {
  245. if ( add_len > ctx->add_len )
  246. {
  247. return MBEDTLS_ERR_CCM_BAD_INPUT;
  248. }
  249. ctx->y[0] ^= (unsigned char)( ( ctx->add_len >> 8 ) & 0xFF );
  250. ctx->y[1] ^= (unsigned char)( ( ctx->add_len ) & 0xFF );
  251. ctx->state |= CCM_STATE__AUTH_DATA_STARTED;
  252. }
  253. else if ( ctx->processed + add_len > ctx->add_len )
  254. {
  255. return MBEDTLS_ERR_CCM_BAD_INPUT;
  256. }
  257. while( add_len > 0 )
  258. {
  259. offset = (ctx->processed + 2) % 16; /* account for y[0] and y[1]
  260. * holding total auth data length */
  261. use_len = 16 - offset;
  262. if( use_len > add_len )
  263. use_len = add_len;
  264. for( i = 0; i < use_len; i++ )
  265. ctx->y[i + offset] ^= add[i];
  266. ctx->processed += use_len;
  267. add_len -= use_len;
  268. add += use_len;
  269. if( use_len + offset == 16 || ctx->processed == ctx->add_len )
  270. {
  271. if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen ) ) != 0 )
  272. {
  273. ctx->state |= CCM_STATE__ERROR;
  274. return( ret );
  275. }
  276. }
  277. }
  278. if( ctx->processed == ctx->add_len )
  279. {
  280. ctx->state |= CCM_STATE__AUTH_DATA_FINISHED;
  281. ctx->processed = 0; // prepare for mbedtls_ccm_update()
  282. }
  283. }
  284. return (0);
  285. }
  286. int mbedtls_ccm_update( mbedtls_ccm_context *ctx,
  287. const unsigned char *input, size_t input_len,
  288. unsigned char *output, size_t output_size,
  289. size_t *output_len )
  290. {
  291. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  292. unsigned char i;
  293. size_t use_len, offset, olen;
  294. unsigned char local_output[16];
  295. if( ctx->state & CCM_STATE__ERROR )
  296. {
  297. return MBEDTLS_ERR_CCM_BAD_INPUT;
  298. }
  299. /* Check against plaintext length only if performing operation with
  300. * authentication
  301. */
  302. if( ctx->tag_len != 0 && ctx->processed + input_len > ctx->plaintext_len )
  303. {
  304. return MBEDTLS_ERR_CCM_BAD_INPUT;
  305. }
  306. if( output_size < input_len )
  307. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  308. *output_len = input_len;
  309. ret = 0;
  310. while ( input_len > 0 )
  311. {
  312. offset = ctx->processed % 16;
  313. use_len = 16 - offset;
  314. if( use_len > input_len )
  315. use_len = input_len;
  316. ctx->processed += use_len;
  317. if( ctx->mode == MBEDTLS_CCM_ENCRYPT || \
  318. ctx->mode == MBEDTLS_CCM_STAR_ENCRYPT )
  319. {
  320. for( i = 0; i < use_len; i++ )
  321. ctx->y[i + offset] ^= input[i];
  322. if( use_len + offset == 16 || ctx->processed == ctx->plaintext_len )
  323. {
  324. if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen ) ) != 0 )
  325. {
  326. ctx->state |= CCM_STATE__ERROR;
  327. goto exit;
  328. }
  329. }
  330. ret = mbedtls_ccm_crypt( ctx, offset, use_len, input, output );
  331. if( ret != 0 )
  332. goto exit;
  333. }
  334. if( ctx->mode == MBEDTLS_CCM_DECRYPT || \
  335. ctx->mode == MBEDTLS_CCM_STAR_DECRYPT )
  336. {
  337. /* Since output may be in shared memory, we cannot be sure that
  338. * it will contain what we wrote to it. Therefore, we should avoid using
  339. * it as input to any operations.
  340. * Write decrypted data to local_output to avoid using output variable as
  341. * input in the XOR operation for Y.
  342. */
  343. ret = mbedtls_ccm_crypt( ctx, offset, use_len, input, local_output );
  344. if( ret != 0 )
  345. goto exit;
  346. for( i = 0; i < use_len; i++ )
  347. ctx->y[i + offset] ^= local_output[i];
  348. memcpy( output, local_output, use_len );
  349. mbedtls_platform_zeroize( local_output, 16 );
  350. if( use_len + offset == 16 || ctx->processed == ctx->plaintext_len )
  351. {
  352. if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen ) ) != 0 )
  353. {
  354. ctx->state |= CCM_STATE__ERROR;
  355. goto exit;
  356. }
  357. }
  358. }
  359. if( use_len + offset == 16 || ctx->processed == ctx->plaintext_len )
  360. {
  361. for( i = 0; i < ctx->q; i++ )
  362. if( ++(ctx->ctr)[15-i] != 0 )
  363. break;
  364. }
  365. input_len -= use_len;
  366. input += use_len;
  367. output += use_len;
  368. }
  369. exit:
  370. mbedtls_platform_zeroize( local_output, 16 );
  371. return ret;
  372. }
  373. int mbedtls_ccm_finish( mbedtls_ccm_context *ctx,
  374. unsigned char *tag, size_t tag_len )
  375. {
  376. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  377. unsigned char i;
  378. if( ctx->state & CCM_STATE__ERROR )
  379. {
  380. return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  381. }
  382. if( ctx->add_len > 0 && !( ctx->state & CCM_STATE__AUTH_DATA_FINISHED ) )
  383. {
  384. return MBEDTLS_ERR_CCM_BAD_INPUT;
  385. }
  386. if( ctx->plaintext_len > 0 && ctx->processed != ctx->plaintext_len )
  387. {
  388. return MBEDTLS_ERR_CCM_BAD_INPUT;
  389. }
  390. /*
  391. * Authentication: reset counter and crypt/mask internal tag
  392. */
  393. for( i = 0; i < ctx->q; i++ )
  394. ctx->ctr[15-i] = 0;
  395. ret = mbedtls_ccm_crypt( ctx, 0, 16, ctx->y, ctx->y );
  396. if( ret != 0 )
  397. return ret;
  398. if( tag != NULL )
  399. memcpy( tag, ctx->y, tag_len );
  400. mbedtls_ccm_clear_state(ctx);
  401. return( 0 );
  402. }
  403. /*
  404. * Authenticated encryption or decryption
  405. */
  406. static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
  407. const unsigned char *iv, size_t iv_len,
  408. const unsigned char *add, size_t add_len,
  409. const unsigned char *input, unsigned char *output,
  410. unsigned char *tag, size_t tag_len )
  411. {
  412. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  413. size_t olen;
  414. if( ( ret = mbedtls_ccm_starts( ctx, mode, iv, iv_len ) ) != 0 )
  415. return( ret );
  416. if( ( ret = mbedtls_ccm_set_lengths( ctx, add_len, length, tag_len ) ) != 0 )
  417. return( ret );
  418. if( ( ret = mbedtls_ccm_update_ad( ctx, add, add_len ) ) != 0 )
  419. return( ret );
  420. if( ( ret = mbedtls_ccm_update( ctx, input, length,
  421. output, length, &olen ) ) != 0 )
  422. return( ret );
  423. if( ( ret = mbedtls_ccm_finish( ctx, tag, tag_len ) ) != 0 )
  424. return( ret );
  425. return( 0 );
  426. }
  427. /*
  428. * Authenticated encryption
  429. */
  430. int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
  431. const unsigned char *iv, size_t iv_len,
  432. const unsigned char *add, size_t add_len,
  433. const unsigned char *input, unsigned char *output,
  434. unsigned char *tag, size_t tag_len )
  435. {
  436. return( ccm_auth_crypt( ctx, MBEDTLS_CCM_STAR_ENCRYPT, length, iv, iv_len,
  437. add, add_len, input, output, tag, tag_len ) );
  438. }
  439. int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
  440. const unsigned char *iv, size_t iv_len,
  441. const unsigned char *add, size_t add_len,
  442. const unsigned char *input, unsigned char *output,
  443. unsigned char *tag, size_t tag_len )
  444. {
  445. return( ccm_auth_crypt( ctx, MBEDTLS_CCM_ENCRYPT, length, iv, iv_len,
  446. add, add_len, input, output, tag, tag_len ) );
  447. }
  448. /*
  449. * Authenticated decryption
  450. */
  451. static int mbedtls_ccm_compare_tags(const unsigned char *tag1, const unsigned char *tag2, size_t tag_len)
  452. {
  453. unsigned char i;
  454. int diff;
  455. /* Check tag in "constant-time" */
  456. for( diff = 0, i = 0; i < tag_len; i++ )
  457. diff |= tag1[i] ^ tag2[i];
  458. if( diff != 0 )
  459. {
  460. return( MBEDTLS_ERR_CCM_AUTH_FAILED );
  461. }
  462. return( 0 );
  463. }
  464. static int ccm_auth_decrypt( mbedtls_ccm_context *ctx, int mode, size_t length,
  465. const unsigned char *iv, size_t iv_len,
  466. const unsigned char *add, size_t add_len,
  467. const unsigned char *input, unsigned char *output,
  468. const unsigned char *tag, size_t tag_len )
  469. {
  470. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  471. unsigned char check_tag[16];
  472. if( ( ret = ccm_auth_crypt( ctx, mode, length,
  473. iv, iv_len, add, add_len,
  474. input, output, check_tag, tag_len ) ) != 0 )
  475. {
  476. return( ret );
  477. }
  478. if( ( ret = mbedtls_ccm_compare_tags( tag, check_tag, tag_len ) ) != 0 )
  479. {
  480. mbedtls_platform_zeroize( output, length );
  481. return( ret );
  482. }
  483. return( 0 );
  484. }
  485. int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
  486. const unsigned char *iv, size_t iv_len,
  487. const unsigned char *add, size_t add_len,
  488. const unsigned char *input, unsigned char *output,
  489. const unsigned char *tag, size_t tag_len )
  490. {
  491. return ccm_auth_decrypt( ctx, MBEDTLS_CCM_STAR_DECRYPT, length,
  492. iv, iv_len, add, add_len,
  493. input, output, tag, tag_len );
  494. }
  495. int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
  496. const unsigned char *iv, size_t iv_len,
  497. const unsigned char *add, size_t add_len,
  498. const unsigned char *input, unsigned char *output,
  499. const unsigned char *tag, size_t tag_len )
  500. {
  501. return ccm_auth_decrypt( ctx, MBEDTLS_CCM_DECRYPT, length,
  502. iv, iv_len, add, add_len,
  503. input, output, tag, tag_len );
  504. }
  505. #endif /* !MBEDTLS_CCM_ALT */
  506. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  507. /*
  508. * Examples 1 to 3 from SP800-38C Appendix C
  509. */
  510. #define NB_TESTS 3
  511. #define CCM_SELFTEST_PT_MAX_LEN 24
  512. #define CCM_SELFTEST_CT_MAX_LEN 32
  513. /*
  514. * The data is the same for all tests, only the used length changes
  515. */
  516. static const unsigned char key_test_data[] = {
  517. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  518. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
  519. };
  520. static const unsigned char iv_test_data[] = {
  521. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  522. 0x18, 0x19, 0x1a, 0x1b
  523. };
  524. static const unsigned char ad_test_data[] = {
  525. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  526. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  527. 0x10, 0x11, 0x12, 0x13
  528. };
  529. static const unsigned char msg_test_data[CCM_SELFTEST_PT_MAX_LEN] = {
  530. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  531. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  532. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  533. };
  534. static const size_t iv_len_test_data [NB_TESTS] = { 7, 8, 12 };
  535. static const size_t add_len_test_data[NB_TESTS] = { 8, 16, 20 };
  536. static const size_t msg_len_test_data[NB_TESTS] = { 4, 16, 24 };
  537. static const size_t tag_len_test_data[NB_TESTS] = { 4, 6, 8 };
  538. static const unsigned char res_test_data[NB_TESTS][CCM_SELFTEST_CT_MAX_LEN] = {
  539. { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
  540. { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
  541. 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
  542. 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
  543. { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
  544. 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
  545. 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
  546. 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
  547. };
  548. int mbedtls_ccm_self_test( int verbose )
  549. {
  550. mbedtls_ccm_context ctx;
  551. /*
  552. * Some hardware accelerators require the input and output buffers
  553. * would be in RAM, because the flash is not accessible.
  554. * Use buffers on the stack to hold the test vectors data.
  555. */
  556. unsigned char plaintext[CCM_SELFTEST_PT_MAX_LEN];
  557. unsigned char ciphertext[CCM_SELFTEST_CT_MAX_LEN];
  558. size_t i;
  559. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  560. mbedtls_ccm_init( &ctx );
  561. if( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, key_test_data,
  562. 8 * sizeof key_test_data ) != 0 )
  563. {
  564. if( verbose != 0 )
  565. mbedtls_printf( " CCM: setup failed" );
  566. return( 1 );
  567. }
  568. for( i = 0; i < NB_TESTS; i++ )
  569. {
  570. if( verbose != 0 )
  571. mbedtls_printf( " CCM-AES #%u: ", (unsigned int) i + 1 );
  572. memset( plaintext, 0, CCM_SELFTEST_PT_MAX_LEN );
  573. memset( ciphertext, 0, CCM_SELFTEST_CT_MAX_LEN );
  574. memcpy( plaintext, msg_test_data, msg_len_test_data[i] );
  575. ret = mbedtls_ccm_encrypt_and_tag( &ctx, msg_len_test_data[i],
  576. iv_test_data, iv_len_test_data[i],
  577. ad_test_data, add_len_test_data[i],
  578. plaintext, ciphertext,
  579. ciphertext + msg_len_test_data[i],
  580. tag_len_test_data[i] );
  581. if( ret != 0 ||
  582. memcmp( ciphertext, res_test_data[i],
  583. msg_len_test_data[i] + tag_len_test_data[i] ) != 0 )
  584. {
  585. if( verbose != 0 )
  586. mbedtls_printf( "failed\n" );
  587. return( 1 );
  588. }
  589. memset( plaintext, 0, CCM_SELFTEST_PT_MAX_LEN );
  590. ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len_test_data[i],
  591. iv_test_data, iv_len_test_data[i],
  592. ad_test_data, add_len_test_data[i],
  593. ciphertext, plaintext,
  594. ciphertext + msg_len_test_data[i],
  595. tag_len_test_data[i] );
  596. if( ret != 0 ||
  597. memcmp( plaintext, msg_test_data, msg_len_test_data[i] ) != 0 )
  598. {
  599. if( verbose != 0 )
  600. mbedtls_printf( "failed\n" );
  601. return( 1 );
  602. }
  603. if( verbose != 0 )
  604. mbedtls_printf( "passed\n" );
  605. }
  606. mbedtls_ccm_free( &ctx );
  607. if( verbose != 0 )
  608. mbedtls_printf( "\n" );
  609. return( 0 );
  610. }
  611. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  612. #endif /* MBEDTLS_CCM_C */