blob: 98987b2fbe5148be54716bf77f85783633e12fd3 [file] [log] [blame]
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02001/*
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/*
27 * Definition of CCM:
28 * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
29 * RFC 3610 "Counter with CBC-MAC (CCM)"
30 *
31 * Related:
32 * RFC 5116 "An Interface and Algorithms for Authenticated Encryption"
33 */
34
35#if !defined(POLARSSL_CONFIG_FILE)
36#include "polarssl/config.h"
37#else
38#include POLARSSL_CONFIG_FILE
39#endif
40
41#if defined(POLARSSL_CCM_C)
42
43#include "polarssl/ccm.h"
44
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +020045#define CCM_ENCRYPT 0
46#define CCM_DECRYPT 1
47
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020048/*
49 * Initialize context
50 */
51int ccm_init( ccm_context *ctx, cipher_id_t cipher,
52 const unsigned char *key, unsigned int keysize )
53{
54 int ret;
55 const cipher_info_t *cipher_info;
56
57 memset( ctx, 0, sizeof( ccm_context ) );
58
59 cipher_info = cipher_info_from_values( cipher, keysize, POLARSSL_MODE_ECB );
60 if( cipher_info == NULL )
61 return( POLARSSL_ERR_CCM_BAD_INPUT );
62
63 if( cipher_info->block_size != 16 )
64 return( POLARSSL_ERR_CCM_BAD_INPUT );
65
66 if( ( ret = cipher_init_ctx( &ctx->cipher_ctx, cipher_info ) ) != 0 )
67 return( ret );
68
69 if( ( ret = cipher_setkey( &ctx->cipher_ctx, key, keysize,
70 POLARSSL_ENCRYPT ) ) != 0 )
71 {
72 return( ret );
73 }
74
75 return( 0 );
76}
77
78/*
79 * Free context
80 */
81void ccm_free( ccm_context *ctx )
82{
83 (void) cipher_free_ctx( &ctx->cipher_ctx );
84 memset( ctx, 0, sizeof( ccm_context ) );
85}
86
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +020087/*
88 * Macros for common operations.
89 * Results in smaller compiled code than static inline functions.
90 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020091
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +020092/*
93 * Update the CBC-MAC state in y using a block in b
94 * (Always using b as the source helps the compiler optimise a bit better.)
95 */
96#define UPDATE_CBC_MAC \
97 for( i = 0; i < 16; i++ ) \
98 y[i] ^= b[i]; \
99 \
100 if( ( ret = cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \
101 return( ret );
102
103/*
104 * Encrypt or decrypt a partial block with CTR
105 * Warning: using b for temporary storage! src and dst must not be b!
106 * (This avoids allocating one more 16 bytes buffer.)
107 */
108#define CTR_CRYPT( dst, src, len ) \
109 if( ( ret = cipher_update( &ctx->cipher_ctx, ctr, 16, b, &olen ) ) != 0 ) \
110 return( ret ); \
111 \
112 for( i = 0; i < len; i++ ) \
113 dst[i] = src[i] ^ b[i];
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200114
115/*
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200116 * Authenticated encryption or decryption
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200117 */
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200118static int ccm_auth_crypt( ccm_context *ctx, int mode, size_t length,
119 const unsigned char *iv, size_t iv_len,
120 const unsigned char *add, size_t add_len,
121 const unsigned char *input, unsigned char *output,
122 unsigned char *tag, size_t tag_len )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200123{
124 int ret;
125 unsigned char i;
126 unsigned char q = 16 - 1 - iv_len;
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200127 size_t len_left, olen;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200128 unsigned char b[16];
129 unsigned char y[16];
130 unsigned char ctr[16];
131 const unsigned char *src;
132 unsigned char *dst;
133
134 /*
135 * Check length requirements: SP800-38C A.1
136 * Additional requirement: a < 2^16 - 2^8 to simplify the code.
137 * 'length' checked later (when writing it to the first block)
138 */
139 if( tag_len < 4 || tag_len > 16 || tag_len % 2 != 0 )
140 return( POLARSSL_ERR_CCM_BAD_INPUT );
141
142 /* Also implies q is within bounds */
143 if( iv_len < 7 || iv_len > 13 )
144 return( POLARSSL_ERR_CCM_BAD_INPUT );
145
146 if( add_len > 0xFF00 )
147 return( POLARSSL_ERR_CCM_BAD_INPUT );
148
149 /*
150 * First block B_0:
151 * 0 .. 0 flags
152 * 1 .. iv_len nonce (aka iv)
153 * iv_len+1 .. 15 length
154 *
155 * With flags as (bits):
156 * 7 0
157 * 6 add present?
158 * 5 .. 3 (t - 2) / 2
159 * 2 .. 0 q - 1
160 */
161 b[0] = 0;
162 b[0] |= ( add_len > 0 ) << 6;
163 b[0] |= ( ( tag_len - 2 ) / 2 ) << 3;
164 b[0] |= q - 1;
165
166 memcpy( b + 1, iv, iv_len );
167
168 for( i = 0, len_left = length; i < q; i++, len_left >>= 8 )
169 b[15-i] = (unsigned char)( len_left & 0xFF );
170
171 if( len_left > 0 )
172 return( POLARSSL_ERR_CCM_BAD_INPUT );
173
174
175 /* Start CBC-MAC with first block */
176 memset( y, 0, 16 );
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200177 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200178
179 /*
180 * If there is additional data, update CBC-MAC with
181 * add_len, add, 0 (padding to a block boundary)
182 */
183 if( add_len > 0 )
184 {
185 size_t use_len;
186 len_left = add_len;
187 src = add;
188
189 memset( b, 0, 16 );
190 b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF );
191 b[1] = (unsigned char)( ( add_len ) & 0xFF );
192
193 use_len = len_left < 16 - 2 ? len_left : 16 - 2;
194 memcpy( b + 2, src, use_len );
195 len_left -= use_len;
196 src += use_len;
197
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200198 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200199
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200200 while( len_left > 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200201 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200202 use_len = len_left > 16 ? 16 : len_left;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200203
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200204 memset( b, 0, 16 );
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200205 memcpy( b, src, use_len );
206 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200207
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200208 len_left -= use_len;
209 src += use_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200210 }
211 }
212
213 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200214 * Prepare counter block for encryption:
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200215 * 0 .. 0 flags
216 * 1 .. iv_len nonce (aka iv)
217 * iv_len+1 .. 15 counter (initially 1)
218 *
219 * With flags as (bits):
220 * 7 .. 3 0
221 * 2 .. 0 q - 1
222 */
223 ctr[0] = q - 1;
224 memcpy( ctr + 1, iv, iv_len );
225 memset( ctr + 1 + iv_len, 0, q );
226 ctr[15] = 1;
227
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200228 /*
229 * Authenticate and {en,de}crypt the message.
230 *
231 * The only difference between encryption and decryption is
232 * the respective order of authentication and {en,de}cryption.
233 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200234 len_left = length;
235 src = input;
236 dst = output;
237
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200238 while( len_left > 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200239 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200240 unsigned char use_len = len_left > 16 ? 16 : len_left;
241
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200242 if( mode == CCM_ENCRYPT )
243 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200244 memset( b, 0, 16 );
245 memcpy( b, src, use_len );
246 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200247 }
248
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200249 CTR_CRYPT( dst, src, use_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200250
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200251 if( mode == CCM_DECRYPT )
252 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200253 memset( b, 0, 16 );
254 memcpy( b, dst, use_len );
255 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200256 }
257
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200258 dst += use_len;
259 src += use_len;
260 len_left -= use_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200261
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200262 /*
263 * Increment counter.
264 * No need to check for overflow thanks to the length check above.
265 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200266 for( i = 0; i < q; i++ )
267 if( ++ctr[15-i] != 0 )
268 break;
269 }
270
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200271 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200272 * Authentication: reset counter and crypt/mask internal tag
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200273 */
274 for( i = 0; i < q; i++ )
275 ctr[15-i] = 0;
276
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200277 CTR_CRYPT( y, y, 16 );
278 memcpy( tag, y, tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200279
280 return( 0 );
281}
282
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200283/*
284 * Authenticated encryption
285 */
286int ccm_encrypt_and_tag( ccm_context *ctx, size_t length,
287 const unsigned char *iv, size_t iv_len,
288 const unsigned char *add, size_t add_len,
289 const unsigned char *input, unsigned char *output,
290 unsigned char *tag, size_t tag_len )
291{
292 return( ccm_auth_crypt( ctx, CCM_ENCRYPT, length, iv, iv_len,
293 add, add_len, input, output, tag, tag_len ) );
294}
295
296/*
297 * Authenticated decryption
298 */
299int ccm_auth_decrypt( ccm_context *ctx, size_t length,
300 const unsigned char *iv, size_t iv_len,
301 const unsigned char *add, size_t add_len,
302 const unsigned char *input, unsigned char *output,
303 const unsigned char *tag, size_t tag_len )
304{
305 int ret;
306 unsigned char check_tag[16];
307 unsigned char i;
308 int diff;
309
310 if( ( ret = ccm_auth_crypt( ctx, CCM_DECRYPT, length,
311 iv, iv_len, add, add_len,
312 input, output, check_tag, tag_len ) ) != 0 )
313 {
314 return( ret );
315 }
316
317 /* Check tag in "constant-time" */
318 for( diff = 0, i = 0; i < tag_len; i++ )
319 diff |= tag[i] ^ check_tag[i];
320
321 if( diff != 0 )
322 {
323 memset( output, 0, length );
324 return( POLARSSL_ERR_CCM_AUTH_FAILED );
325 }
326
327 return( 0 );
328}
329
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200330
331#if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_AES_C)
332
333#if defined(POLARSSL_PLATFORM_C)
334#include "polarssl/platform.h"
335#else
336#define polarssl_printf printf
337#endif
338
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200339/*
340 * Examples 1 to 3 from SP800-38C Appendix C
341 */
342
343#define NB_TESTS 3
344
345/*
346 * The data is the same for all tests, only the used length changes
347 */
348static const unsigned char key[] = {
349 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
350 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
351};
352
353static const unsigned char iv[] = {
354 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
355 0x18, 0x19, 0x1a, 0x1b
356};
357
358static const unsigned char ad[] = {
359 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
360 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
361 0x10, 0x11, 0x12, 0x13
362};
363
364static const unsigned char msg[] = {
365 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
366 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
367 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
368};
369
370static const size_t iv_len [NB_TESTS] = { 7, 8, 12 };
371static const size_t add_len[NB_TESTS] = { 8, 16, 20 };
372static const size_t msg_len[NB_TESTS] = { 4, 16, 24 };
373static const size_t tag_len[NB_TESTS] = { 4, 6, 8 };
374
375static const unsigned char res[NB_TESTS][32] = {
376 { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
377 { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
378 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
379 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
380 { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
381 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
382 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
383 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
384};
385
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200386int ccm_self_test( int verbose )
387{
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200388 ccm_context ctx;
389 unsigned char out[32];
390 size_t i;
391 int ret;
392
393 if( ccm_init( &ctx, POLARSSL_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
394 {
395 if( verbose != 0 )
396 polarssl_printf( " CCM: setup failed" );
397
398 return( 1 );
399 }
400
401 for( i = 0; i < NB_TESTS; i++ )
402 {
403 if( verbose != 0 )
404 polarssl_printf( " CCM-AES #%u: ", (unsigned int) i + 1 );
405
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200406 ret = ccm_encrypt_and_tag( &ctx, msg_len[i],
407 iv, iv_len[i], ad, add_len[i],
408 msg, out,
409 out + msg_len[i], tag_len[i] );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200410
411 if( ret != 0 ||
412 memcmp( out, res[i], msg_len[i] + tag_len[i] ) != 0 )
413 {
414 if( verbose != 0 )
415 polarssl_printf( "failed\n" );
416
417 return( 1 );
418 }
419
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200420 ret = ccm_auth_decrypt( &ctx, msg_len[i],
421 iv, iv_len[i], ad, add_len[i],
422 res[i], out,
423 res[i] + msg_len[i], tag_len[i] );
424
425 if( ret != 0 ||
426 memcmp( out, msg, msg_len[i] ) != 0 )
427 {
428 if( verbose != 0 )
429 polarssl_printf( "failed\n" );
430
431 return( 1 );
432 }
433
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200434 if( verbose != 0 )
435 polarssl_printf( "passed\n" );
436 }
437
438 ccm_free( &ctx );
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200439
440 if( verbose != 0 )
441 polarssl_printf( "\n" );
442
443 return( 0 );
444}
445
446#endif /* POLARSSL_SELF_TEST && POLARSSL_AES_C */
447
448#endif /* POLARSSL_CCM_C */