ccm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * NIST SP800-38C compliant CCM implementation
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  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. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. /*
  22. * Definition of CCM:
  23. * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
  24. * RFC 3610 "Counter with CBC-MAC (CCM)"
  25. *
  26. * Related:
  27. * RFC 5116 "An Interface and Algorithms for Authenticated Encryption"
  28. */
  29. #if !defined(MBEDTLS_CONFIG_FILE)
  30. #include "mbedtls/config.h"
  31. #else
  32. #include MBEDTLS_CONFIG_FILE
  33. #endif
  34. #if defined(MBEDTLS_CCM_C)
  35. #include "mbedtls/ccm.h"
  36. #include "mbedtls/platform_util.h"
  37. #include <string.h>
  38. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  39. #if defined(MBEDTLS_PLATFORM_C)
  40. #include "mbedtls/platform.h"
  41. #else
  42. #include <stdio.h>
  43. #define mbedtls_printf printf
  44. #endif /* MBEDTLS_PLATFORM_C */
  45. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  46. #if !defined(MBEDTLS_CCM_ALT)
  47. #define CCM_VALIDATE_RET( cond ) \
  48. MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CCM_BAD_INPUT )
  49. #define CCM_VALIDATE( cond ) \
  50. MBEDTLS_INTERNAL_VALIDATE( cond )
  51. #define CCM_ENCRYPT 0
  52. #define CCM_DECRYPT 1
  53. /*
  54. * Initialize context
  55. */
  56. void mbedtls_ccm_init( mbedtls_ccm_context *ctx )
  57. {
  58. CCM_VALIDATE( ctx != NULL );
  59. memset( ctx, 0, sizeof( mbedtls_ccm_context ) );
  60. }
  61. int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
  62. mbedtls_cipher_id_t cipher,
  63. const unsigned char *key,
  64. unsigned int keybits )
  65. {
  66. int ret;
  67. const mbedtls_cipher_info_t *cipher_info;
  68. CCM_VALIDATE_RET( ctx != NULL );
  69. CCM_VALIDATE_RET( key != NULL );
  70. cipher_info = mbedtls_cipher_info_from_values( cipher, keybits, MBEDTLS_MODE_ECB );
  71. if( cipher_info == NULL )
  72. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  73. if( cipher_info->block_size != 16 )
  74. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  75. mbedtls_cipher_free( &ctx->cipher_ctx );
  76. if( ( ret = mbedtls_cipher_setup( &ctx->cipher_ctx, cipher_info ) ) != 0 )
  77. return( ret );
  78. if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keybits,
  79. MBEDTLS_ENCRYPT ) ) != 0 )
  80. {
  81. return( ret );
  82. }
  83. return( 0 );
  84. }
  85. /*
  86. * Free context
  87. */
  88. void mbedtls_ccm_free( mbedtls_ccm_context *ctx )
  89. {
  90. if( ctx == NULL )
  91. return;
  92. mbedtls_cipher_free( &ctx->cipher_ctx );
  93. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ccm_context ) );
  94. }
  95. /*
  96. * Macros for common operations.
  97. * Results in smaller compiled code than static inline functions.
  98. */
  99. /*
  100. * Update the CBC-MAC state in y using a block in b
  101. * (Always using b as the source helps the compiler optimise a bit better.)
  102. */
  103. #define UPDATE_CBC_MAC \
  104. for( i = 0; i < 16; i++ ) \
  105. y[i] ^= b[i]; \
  106. \
  107. if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \
  108. return( ret );
  109. /*
  110. * Encrypt or decrypt a partial block with CTR
  111. * Warning: using b for temporary storage! src and dst must not be b!
  112. * This avoids allocating one more 16 bytes buffer while allowing src == dst.
  113. */
  114. #define CTR_CRYPT( dst, src, len ) \
  115. do \
  116. { \
  117. if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctr, \
  118. 16, b, &olen ) ) != 0 ) \
  119. { \
  120. return( ret ); \
  121. } \
  122. \
  123. for( i = 0; i < (len); i++ ) \
  124. (dst)[i] = (src)[i] ^ b[i]; \
  125. } while( 0 )
  126. /*
  127. * Authenticated encryption or decryption
  128. */
  129. static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
  130. const unsigned char *iv, size_t iv_len,
  131. const unsigned char *add, size_t add_len,
  132. const unsigned char *input, unsigned char *output,
  133. unsigned char *tag, size_t tag_len )
  134. {
  135. int ret;
  136. unsigned char i;
  137. unsigned char q;
  138. size_t len_left, olen;
  139. unsigned char b[16];
  140. unsigned char y[16];
  141. unsigned char ctr[16];
  142. const unsigned char *src;
  143. unsigned char *dst;
  144. /*
  145. * Check length requirements: SP800-38C A.1
  146. * Additional requirement: a < 2^16 - 2^8 to simplify the code.
  147. * 'length' checked later (when writing it to the first block)
  148. *
  149. * Also, loosen the requirements to enable support for CCM* (IEEE 802.15.4).
  150. */
  151. if( tag_len == 2 || tag_len > 16 || tag_len % 2 != 0 )
  152. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  153. /* Also implies q is within bounds */
  154. if( iv_len < 7 || iv_len > 13 )
  155. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  156. if( add_len > 0xFF00 )
  157. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  158. q = 16 - 1 - (unsigned char) iv_len;
  159. /*
  160. * First block B_0:
  161. * 0 .. 0 flags
  162. * 1 .. iv_len nonce (aka iv)
  163. * iv_len+1 .. 15 length
  164. *
  165. * With flags as (bits):
  166. * 7 0
  167. * 6 add present?
  168. * 5 .. 3 (t - 2) / 2
  169. * 2 .. 0 q - 1
  170. */
  171. b[0] = 0;
  172. b[0] |= ( add_len > 0 ) << 6;
  173. b[0] |= ( ( tag_len - 2 ) / 2 ) << 3;
  174. b[0] |= q - 1;
  175. memcpy( b + 1, iv, iv_len );
  176. for( i = 0, len_left = length; i < q; i++, len_left >>= 8 )
  177. b[15-i] = (unsigned char)( len_left & 0xFF );
  178. if( len_left > 0 )
  179. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  180. /* Start CBC-MAC with first block */
  181. memset( y, 0, 16 );
  182. UPDATE_CBC_MAC;
  183. /*
  184. * If there is additional data, update CBC-MAC with
  185. * add_len, add, 0 (padding to a block boundary)
  186. */
  187. if( add_len > 0 )
  188. {
  189. size_t use_len;
  190. len_left = add_len;
  191. src = add;
  192. memset( b, 0, 16 );
  193. b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF );
  194. b[1] = (unsigned char)( ( add_len ) & 0xFF );
  195. use_len = len_left < 16 - 2 ? len_left : 16 - 2;
  196. memcpy( b + 2, src, use_len );
  197. len_left -= use_len;
  198. src += use_len;
  199. UPDATE_CBC_MAC;
  200. while( len_left > 0 )
  201. {
  202. use_len = len_left > 16 ? 16 : len_left;
  203. memset( b, 0, 16 );
  204. memcpy( b, src, use_len );
  205. UPDATE_CBC_MAC;
  206. len_left -= use_len;
  207. src += use_len;
  208. }
  209. }
  210. /*
  211. * Prepare counter block for encryption:
  212. * 0 .. 0 flags
  213. * 1 .. iv_len nonce (aka iv)
  214. * iv_len+1 .. 15 counter (initially 1)
  215. *
  216. * With flags as (bits):
  217. * 7 .. 3 0
  218. * 2 .. 0 q - 1
  219. */
  220. ctr[0] = q - 1;
  221. memcpy( ctr + 1, iv, iv_len );
  222. memset( ctr + 1 + iv_len, 0, q );
  223. ctr[15] = 1;
  224. /*
  225. * Authenticate and {en,de}crypt the message.
  226. *
  227. * The only difference between encryption and decryption is
  228. * the respective order of authentication and {en,de}cryption.
  229. */
  230. len_left = length;
  231. src = input;
  232. dst = output;
  233. while( len_left > 0 )
  234. {
  235. size_t use_len = len_left > 16 ? 16 : len_left;
  236. if( mode == CCM_ENCRYPT )
  237. {
  238. memset( b, 0, 16 );
  239. memcpy( b, src, use_len );
  240. UPDATE_CBC_MAC;
  241. }
  242. CTR_CRYPT( dst, src, use_len );
  243. if( mode == CCM_DECRYPT )
  244. {
  245. memset( b, 0, 16 );
  246. memcpy( b, dst, use_len );
  247. UPDATE_CBC_MAC;
  248. }
  249. dst += use_len;
  250. src += use_len;
  251. len_left -= use_len;
  252. /*
  253. * Increment counter.
  254. * No need to check for overflow thanks to the length check above.
  255. */
  256. for( i = 0; i < q; i++ )
  257. if( ++ctr[15-i] != 0 )
  258. break;
  259. }
  260. /*
  261. * Authentication: reset counter and crypt/mask internal tag
  262. */
  263. for( i = 0; i < q; i++ )
  264. ctr[15-i] = 0;
  265. CTR_CRYPT( y, y, 16 );
  266. memcpy( tag, y, tag_len );
  267. return( 0 );
  268. }
  269. /*
  270. * Authenticated encryption
  271. */
  272. int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
  273. const unsigned char *iv, size_t iv_len,
  274. const unsigned char *add, size_t add_len,
  275. const unsigned char *input, unsigned char *output,
  276. unsigned char *tag, size_t tag_len )
  277. {
  278. CCM_VALIDATE_RET( ctx != NULL );
  279. CCM_VALIDATE_RET( iv != NULL );
  280. CCM_VALIDATE_RET( add_len == 0 || add != NULL );
  281. CCM_VALIDATE_RET( length == 0 || input != NULL );
  282. CCM_VALIDATE_RET( length == 0 || output != NULL );
  283. CCM_VALIDATE_RET( tag_len == 0 || tag != NULL );
  284. return( ccm_auth_crypt( ctx, CCM_ENCRYPT, length, iv, iv_len,
  285. add, add_len, input, output, tag, tag_len ) );
  286. }
  287. int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
  288. const unsigned char *iv, size_t iv_len,
  289. const unsigned char *add, size_t add_len,
  290. const unsigned char *input, unsigned char *output,
  291. unsigned char *tag, size_t tag_len )
  292. {
  293. CCM_VALIDATE_RET( ctx != NULL );
  294. CCM_VALIDATE_RET( iv != NULL );
  295. CCM_VALIDATE_RET( add_len == 0 || add != NULL );
  296. CCM_VALIDATE_RET( length == 0 || input != NULL );
  297. CCM_VALIDATE_RET( length == 0 || output != NULL );
  298. CCM_VALIDATE_RET( tag_len == 0 || tag != NULL );
  299. if( tag_len == 0 )
  300. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  301. return( mbedtls_ccm_star_encrypt_and_tag( ctx, length, iv, iv_len, add,
  302. add_len, input, output, tag, tag_len ) );
  303. }
  304. /*
  305. * Authenticated decryption
  306. */
  307. int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
  308. const unsigned char *iv, size_t iv_len,
  309. const unsigned char *add, size_t add_len,
  310. const unsigned char *input, unsigned char *output,
  311. const unsigned char *tag, size_t tag_len )
  312. {
  313. int ret;
  314. unsigned char check_tag[16];
  315. unsigned char i;
  316. int diff;
  317. CCM_VALIDATE_RET( ctx != NULL );
  318. CCM_VALIDATE_RET( iv != NULL );
  319. CCM_VALIDATE_RET( add_len == 0 || add != NULL );
  320. CCM_VALIDATE_RET( length == 0 || input != NULL );
  321. CCM_VALIDATE_RET( length == 0 || output != NULL );
  322. CCM_VALIDATE_RET( tag_len == 0 || tag != NULL );
  323. if( ( ret = ccm_auth_crypt( ctx, CCM_DECRYPT, length,
  324. iv, iv_len, add, add_len,
  325. input, output, check_tag, tag_len ) ) != 0 )
  326. {
  327. return( ret );
  328. }
  329. /* Check tag in "constant-time" */
  330. for( diff = 0, i = 0; i < tag_len; i++ )
  331. diff |= tag[i] ^ check_tag[i];
  332. if( diff != 0 )
  333. {
  334. mbedtls_platform_zeroize( output, length );
  335. return( MBEDTLS_ERR_CCM_AUTH_FAILED );
  336. }
  337. return( 0 );
  338. }
  339. int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
  340. const unsigned char *iv, size_t iv_len,
  341. const unsigned char *add, size_t add_len,
  342. const unsigned char *input, unsigned char *output,
  343. const unsigned char *tag, size_t tag_len )
  344. {
  345. CCM_VALIDATE_RET( ctx != NULL );
  346. CCM_VALIDATE_RET( iv != NULL );
  347. CCM_VALIDATE_RET( add_len == 0 || add != NULL );
  348. CCM_VALIDATE_RET( length == 0 || input != NULL );
  349. CCM_VALIDATE_RET( length == 0 || output != NULL );
  350. CCM_VALIDATE_RET( tag_len == 0 || tag != NULL );
  351. if( tag_len == 0 )
  352. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  353. return( mbedtls_ccm_star_auth_decrypt( ctx, length, iv, iv_len, add,
  354. add_len, input, output, tag, tag_len ) );
  355. }
  356. #endif /* !MBEDTLS_CCM_ALT */
  357. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  358. /*
  359. * Examples 1 to 3 from SP800-38C Appendix C
  360. */
  361. #define NB_TESTS 3
  362. #define CCM_SELFTEST_PT_MAX_LEN 24
  363. #define CCM_SELFTEST_CT_MAX_LEN 32
  364. /*
  365. * The data is the same for all tests, only the used length changes
  366. */
  367. static const unsigned char key[] = {
  368. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  369. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
  370. };
  371. static const unsigned char iv[] = {
  372. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  373. 0x18, 0x19, 0x1a, 0x1b
  374. };
  375. static const unsigned char ad[] = {
  376. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  377. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  378. 0x10, 0x11, 0x12, 0x13
  379. };
  380. static const unsigned char msg[CCM_SELFTEST_PT_MAX_LEN] = {
  381. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  382. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  383. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  384. };
  385. static const size_t iv_len [NB_TESTS] = { 7, 8, 12 };
  386. static const size_t add_len[NB_TESTS] = { 8, 16, 20 };
  387. static const size_t msg_len[NB_TESTS] = { 4, 16, 24 };
  388. static const size_t tag_len[NB_TESTS] = { 4, 6, 8 };
  389. static const unsigned char res[NB_TESTS][CCM_SELFTEST_CT_MAX_LEN] = {
  390. { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
  391. { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
  392. 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
  393. 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
  394. { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
  395. 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
  396. 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
  397. 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
  398. };
  399. int mbedtls_ccm_self_test( int verbose )
  400. {
  401. mbedtls_ccm_context ctx;
  402. /*
  403. * Some hardware accelerators require the input and output buffers
  404. * would be in RAM, because the flash is not accessible.
  405. * Use buffers on the stack to hold the test vectors data.
  406. */
  407. unsigned char plaintext[CCM_SELFTEST_PT_MAX_LEN];
  408. unsigned char ciphertext[CCM_SELFTEST_CT_MAX_LEN];
  409. size_t i;
  410. int ret;
  411. mbedtls_ccm_init( &ctx );
  412. if( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
  413. {
  414. if( verbose != 0 )
  415. mbedtls_printf( " CCM: setup failed" );
  416. return( 1 );
  417. }
  418. for( i = 0; i < NB_TESTS; i++ )
  419. {
  420. if( verbose != 0 )
  421. mbedtls_printf( " CCM-AES #%u: ", (unsigned int) i + 1 );
  422. memset( plaintext, 0, CCM_SELFTEST_PT_MAX_LEN );
  423. memset( ciphertext, 0, CCM_SELFTEST_CT_MAX_LEN );
  424. memcpy( plaintext, msg, msg_len[i] );
  425. ret = mbedtls_ccm_encrypt_and_tag( &ctx, msg_len[i],
  426. iv, iv_len[i], ad, add_len[i],
  427. plaintext, ciphertext,
  428. ciphertext + msg_len[i], tag_len[i] );
  429. if( ret != 0 ||
  430. memcmp( ciphertext, res[i], msg_len[i] + tag_len[i] ) != 0 )
  431. {
  432. if( verbose != 0 )
  433. mbedtls_printf( "failed\n" );
  434. return( 1 );
  435. }
  436. memset( plaintext, 0, CCM_SELFTEST_PT_MAX_LEN );
  437. ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len[i],
  438. iv, iv_len[i], ad, add_len[i],
  439. ciphertext, plaintext,
  440. ciphertext + msg_len[i], tag_len[i] );
  441. if( ret != 0 ||
  442. memcmp( plaintext, msg, msg_len[i] ) != 0 )
  443. {
  444. if( verbose != 0 )
  445. mbedtls_printf( "failed\n" );
  446. return( 1 );
  447. }
  448. if( verbose != 0 )
  449. mbedtls_printf( "passed\n" );
  450. }
  451. mbedtls_ccm_free( &ctx );
  452. if( verbose != 0 )
  453. mbedtls_printf( "\n" );
  454. return( 0 );
  455. }
  456. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  457. #endif /* MBEDTLS_CCM_C */