ccm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * NIST SP800-38C compliant CCM implementation
  3. *
  4. * Copyright (C) 2014, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. /*
  26. * Definition of CCM:
  27. * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
  28. * RFC 3610 "Counter with CBC-MAC (CCM)"
  29. *
  30. * Related:
  31. * RFC 5116 "An Interface and Algorithms for Authenticated Encryption"
  32. */
  33. #if !defined(POLARSSL_CONFIG_FILE)
  34. #include "polarssl/config.h"
  35. #else
  36. #include POLARSSL_CONFIG_FILE
  37. #endif
  38. #if defined(POLARSSL_CCM_C)
  39. #include "polarssl/ccm.h"
  40. /* Implementation that should never be optimized out by the compiler */
  41. static void polarssl_zeroize( void *v, size_t n ) {
  42. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  43. }
  44. #define CCM_ENCRYPT 0
  45. #define CCM_DECRYPT 1
  46. /*
  47. * Initialize context
  48. */
  49. int ccm_init( ccm_context *ctx, cipher_id_t cipher,
  50. const unsigned char *key, unsigned int keysize )
  51. {
  52. int ret;
  53. const cipher_info_t *cipher_info;
  54. memset( ctx, 0, sizeof( ccm_context ) );
  55. cipher_init( &ctx->cipher_ctx );
  56. cipher_info = cipher_info_from_values( cipher, keysize, POLARSSL_MODE_ECB );
  57. if( cipher_info == NULL )
  58. return( POLARSSL_ERR_CCM_BAD_INPUT );
  59. if( cipher_info->block_size != 16 )
  60. return( POLARSSL_ERR_CCM_BAD_INPUT );
  61. if( ( ret = cipher_init_ctx( &ctx->cipher_ctx, cipher_info ) ) != 0 )
  62. return( ret );
  63. if( ( ret = cipher_setkey( &ctx->cipher_ctx, key, keysize,
  64. POLARSSL_ENCRYPT ) ) != 0 )
  65. {
  66. return( ret );
  67. }
  68. return( 0 );
  69. }
  70. /*
  71. * Free context
  72. */
  73. void ccm_free( ccm_context *ctx )
  74. {
  75. cipher_free( &ctx->cipher_ctx );
  76. polarssl_zeroize( ctx, sizeof( ccm_context ) );
  77. }
  78. /*
  79. * Macros for common operations.
  80. * Results in smaller compiled code than static inline functions.
  81. */
  82. /*
  83. * Update the CBC-MAC state in y using a block in b
  84. * (Always using b as the source helps the compiler optimise a bit better.)
  85. */
  86. #define UPDATE_CBC_MAC \
  87. for( i = 0; i < 16; i++ ) \
  88. y[i] ^= b[i]; \
  89. \
  90. if( ( ret = cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \
  91. return( ret );
  92. /*
  93. * Encrypt or decrypt a partial block with CTR
  94. * Warning: using b for temporary storage! src and dst must not be b!
  95. * This avoids allocating one more 16 bytes buffer while allowing src == dst.
  96. */
  97. #define CTR_CRYPT( dst, src, len ) \
  98. if( ( ret = cipher_update( &ctx->cipher_ctx, ctr, 16, b, &olen ) ) != 0 ) \
  99. return( ret ); \
  100. \
  101. for( i = 0; i < len; i++ ) \
  102. dst[i] = src[i] ^ b[i];
  103. /*
  104. * Authenticated encryption or decryption
  105. */
  106. static int ccm_auth_crypt( ccm_context *ctx, int mode, size_t length,
  107. const unsigned char *iv, size_t iv_len,
  108. const unsigned char *add, size_t add_len,
  109. const unsigned char *input, unsigned char *output,
  110. unsigned char *tag, size_t tag_len )
  111. {
  112. int ret;
  113. unsigned char i;
  114. unsigned char q = 16 - 1 - iv_len;
  115. size_t len_left, olen;
  116. unsigned char b[16];
  117. unsigned char y[16];
  118. unsigned char ctr[16];
  119. const unsigned char *src;
  120. unsigned char *dst;
  121. /*
  122. * Check length requirements: SP800-38C A.1
  123. * Additional requirement: a < 2^16 - 2^8 to simplify the code.
  124. * 'length' checked later (when writing it to the first block)
  125. */
  126. if( tag_len < 4 || tag_len > 16 || tag_len % 2 != 0 )
  127. return( POLARSSL_ERR_CCM_BAD_INPUT );
  128. /* Also implies q is within bounds */
  129. if( iv_len < 7 || iv_len > 13 )
  130. return( POLARSSL_ERR_CCM_BAD_INPUT );
  131. if( add_len > 0xFF00 )
  132. return( POLARSSL_ERR_CCM_BAD_INPUT );
  133. /*
  134. * First block B_0:
  135. * 0 .. 0 flags
  136. * 1 .. iv_len nonce (aka iv)
  137. * iv_len+1 .. 15 length
  138. *
  139. * With flags as (bits):
  140. * 7 0
  141. * 6 add present?
  142. * 5 .. 3 (t - 2) / 2
  143. * 2 .. 0 q - 1
  144. */
  145. b[0] = 0;
  146. b[0] |= ( add_len > 0 ) << 6;
  147. b[0] |= ( ( tag_len - 2 ) / 2 ) << 3;
  148. b[0] |= q - 1;
  149. memcpy( b + 1, iv, iv_len );
  150. for( i = 0, len_left = length; i < q; i++, len_left >>= 8 )
  151. b[15-i] = (unsigned char)( len_left & 0xFF );
  152. if( len_left > 0 )
  153. return( POLARSSL_ERR_CCM_BAD_INPUT );
  154. /* Start CBC-MAC with first block */
  155. memset( y, 0, 16 );
  156. UPDATE_CBC_MAC;
  157. /*
  158. * If there is additional data, update CBC-MAC with
  159. * add_len, add, 0 (padding to a block boundary)
  160. */
  161. if( add_len > 0 )
  162. {
  163. size_t use_len;
  164. len_left = add_len;
  165. src = add;
  166. memset( b, 0, 16 );
  167. b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF );
  168. b[1] = (unsigned char)( ( add_len ) & 0xFF );
  169. use_len = len_left < 16 - 2 ? len_left : 16 - 2;
  170. memcpy( b + 2, src, use_len );
  171. len_left -= use_len;
  172. src += use_len;
  173. UPDATE_CBC_MAC;
  174. while( len_left > 0 )
  175. {
  176. use_len = len_left > 16 ? 16 : len_left;
  177. memset( b, 0, 16 );
  178. memcpy( b, src, use_len );
  179. UPDATE_CBC_MAC;
  180. len_left -= use_len;
  181. src += use_len;
  182. }
  183. }
  184. /*
  185. * Prepare counter block for encryption:
  186. * 0 .. 0 flags
  187. * 1 .. iv_len nonce (aka iv)
  188. * iv_len+1 .. 15 counter (initially 1)
  189. *
  190. * With flags as (bits):
  191. * 7 .. 3 0
  192. * 2 .. 0 q - 1
  193. */
  194. ctr[0] = q - 1;
  195. memcpy( ctr + 1, iv, iv_len );
  196. memset( ctr + 1 + iv_len, 0, q );
  197. ctr[15] = 1;
  198. /*
  199. * Authenticate and {en,de}crypt the message.
  200. *
  201. * The only difference between encryption and decryption is
  202. * the respective order of authentication and {en,de}cryption.
  203. */
  204. len_left = length;
  205. src = input;
  206. dst = output;
  207. while( len_left > 0 )
  208. {
  209. unsigned char use_len = len_left > 16 ? 16 : len_left;
  210. if( mode == CCM_ENCRYPT )
  211. {
  212. memset( b, 0, 16 );
  213. memcpy( b, src, use_len );
  214. UPDATE_CBC_MAC;
  215. }
  216. CTR_CRYPT( dst, src, use_len );
  217. if( mode == CCM_DECRYPT )
  218. {
  219. memset( b, 0, 16 );
  220. memcpy( b, dst, use_len );
  221. UPDATE_CBC_MAC;
  222. }
  223. dst += use_len;
  224. src += use_len;
  225. len_left -= use_len;
  226. /*
  227. * Increment counter.
  228. * No need to check for overflow thanks to the length check above.
  229. */
  230. for( i = 0; i < q; i++ )
  231. if( ++ctr[15-i] != 0 )
  232. break;
  233. }
  234. /*
  235. * Authentication: reset counter and crypt/mask internal tag
  236. */
  237. for( i = 0; i < q; i++ )
  238. ctr[15-i] = 0;
  239. CTR_CRYPT( y, y, 16 );
  240. memcpy( tag, y, tag_len );
  241. return( 0 );
  242. }
  243. /*
  244. * Authenticated encryption
  245. */
  246. int ccm_encrypt_and_tag( ccm_context *ctx, size_t length,
  247. const unsigned char *iv, size_t iv_len,
  248. const unsigned char *add, size_t add_len,
  249. const unsigned char *input, unsigned char *output,
  250. unsigned char *tag, size_t tag_len )
  251. {
  252. return( ccm_auth_crypt( ctx, CCM_ENCRYPT, length, iv, iv_len,
  253. add, add_len, input, output, tag, tag_len ) );
  254. }
  255. /*
  256. * Authenticated decryption
  257. */
  258. int ccm_auth_decrypt( ccm_context *ctx, size_t length,
  259. const unsigned char *iv, size_t iv_len,
  260. const unsigned char *add, size_t add_len,
  261. const unsigned char *input, unsigned char *output,
  262. const unsigned char *tag, size_t tag_len )
  263. {
  264. int ret;
  265. unsigned char check_tag[16];
  266. unsigned char i;
  267. int diff;
  268. if( ( ret = ccm_auth_crypt( ctx, CCM_DECRYPT, length,
  269. iv, iv_len, add, add_len,
  270. input, output, check_tag, tag_len ) ) != 0 )
  271. {
  272. return( ret );
  273. }
  274. /* Check tag in "constant-time" */
  275. for( diff = 0, i = 0; i < tag_len; i++ )
  276. diff |= tag[i] ^ check_tag[i];
  277. if( diff != 0 )
  278. {
  279. polarssl_zeroize( output, length );
  280. return( POLARSSL_ERR_CCM_AUTH_FAILED );
  281. }
  282. return( 0 );
  283. }
  284. #if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_AES_C)
  285. #if defined(POLARSSL_PLATFORM_C)
  286. #include "polarssl/platform.h"
  287. #else
  288. #include <stdio.h>
  289. #define polarssl_printf printf
  290. #endif
  291. /*
  292. * Examples 1 to 3 from SP800-38C Appendix C
  293. */
  294. #define NB_TESTS 3
  295. /*
  296. * The data is the same for all tests, only the used length changes
  297. */
  298. static const unsigned char key[] = {
  299. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  300. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
  301. };
  302. static const unsigned char iv[] = {
  303. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  304. 0x18, 0x19, 0x1a, 0x1b
  305. };
  306. static const unsigned char ad[] = {
  307. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  308. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  309. 0x10, 0x11, 0x12, 0x13
  310. };
  311. static const unsigned char msg[] = {
  312. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  313. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  314. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  315. };
  316. static const size_t iv_len [NB_TESTS] = { 7, 8, 12 };
  317. static const size_t add_len[NB_TESTS] = { 8, 16, 20 };
  318. static const size_t msg_len[NB_TESTS] = { 4, 16, 24 };
  319. static const size_t tag_len[NB_TESTS] = { 4, 6, 8 };
  320. static const unsigned char res[NB_TESTS][32] = {
  321. { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
  322. { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
  323. 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
  324. 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
  325. { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
  326. 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
  327. 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
  328. 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
  329. };
  330. int ccm_self_test( int verbose )
  331. {
  332. ccm_context ctx;
  333. unsigned char out[32];
  334. size_t i;
  335. int ret;
  336. if( ccm_init( &ctx, POLARSSL_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
  337. {
  338. if( verbose != 0 )
  339. polarssl_printf( " CCM: setup failed" );
  340. return( 1 );
  341. }
  342. for( i = 0; i < NB_TESTS; i++ )
  343. {
  344. if( verbose != 0 )
  345. polarssl_printf( " CCM-AES #%u: ", (unsigned int) i + 1 );
  346. ret = ccm_encrypt_and_tag( &ctx, msg_len[i],
  347. iv, iv_len[i], ad, add_len[i],
  348. msg, out,
  349. out + msg_len[i], tag_len[i] );
  350. if( ret != 0 ||
  351. memcmp( out, res[i], msg_len[i] + tag_len[i] ) != 0 )
  352. {
  353. if( verbose != 0 )
  354. polarssl_printf( "failed\n" );
  355. return( 1 );
  356. }
  357. ret = ccm_auth_decrypt( &ctx, msg_len[i],
  358. iv, iv_len[i], ad, add_len[i],
  359. res[i], out,
  360. res[i] + msg_len[i], tag_len[i] );
  361. if( ret != 0 ||
  362. memcmp( out, msg, msg_len[i] ) != 0 )
  363. {
  364. if( verbose != 0 )
  365. polarssl_printf( "failed\n" );
  366. return( 1 );
  367. }
  368. if( verbose != 0 )
  369. polarssl_printf( "passed\n" );
  370. }
  371. ccm_free( &ctx );
  372. if( verbose != 0 )
  373. polarssl_printf( "\n" );
  374. return( 0 );
  375. }
  376. #endif /* POLARSSL_SELF_TEST && POLARSSL_AES_C */
  377. #endif /* POLARSSL_CCM_C */