ccm.c 18 KB

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