blob: 72d766b9a50fdf8c70ac1fd0d7cd2bbae8b83e78 [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
Paul Bakker34617722014-06-13 17:20:13 +020045/* Implementation that should never be optimized out by the compiler */
46static void polarssl_zeroize( void *v, size_t n ) {
47 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
48}
49
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +020050#define CCM_ENCRYPT 0
51#define CCM_DECRYPT 1
52
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020053/*
54 * Initialize context
55 */
56int ccm_init( ccm_context *ctx, cipher_id_t cipher,
57 const unsigned char *key, unsigned int keysize )
58{
59 int ret;
60 const cipher_info_t *cipher_info;
61
62 memset( ctx, 0, sizeof( ccm_context ) );
63
Paul Bakker84bbeb52014-07-01 14:53:22 +020064 cipher_init( &ctx->cipher_ctx );
65
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020066 cipher_info = cipher_info_from_values( cipher, keysize, POLARSSL_MODE_ECB );
67 if( cipher_info == NULL )
68 return( POLARSSL_ERR_CCM_BAD_INPUT );
69
70 if( cipher_info->block_size != 16 )
71 return( POLARSSL_ERR_CCM_BAD_INPUT );
72
73 if( ( ret = cipher_init_ctx( &ctx->cipher_ctx, cipher_info ) ) != 0 )
74 return( ret );
75
76 if( ( ret = cipher_setkey( &ctx->cipher_ctx, key, keysize,
77 POLARSSL_ENCRYPT ) ) != 0 )
78 {
79 return( ret );
80 }
81
82 return( 0 );
83}
84
85/*
86 * Free context
87 */
88void ccm_free( ccm_context *ctx )
89{
Paul Bakker84bbeb52014-07-01 14:53:22 +020090 cipher_free( &ctx->cipher_ctx );
Paul Bakker34617722014-06-13 17:20:13 +020091 polarssl_zeroize( ctx, sizeof( ccm_context ) );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020092}
93
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +020094/*
95 * Macros for common operations.
96 * Results in smaller compiled code than static inline functions.
97 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020098
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +020099/*
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 = cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \
108 return( ret );
109
110/*
111 * Encrypt or decrypt a partial block with CTR
112 * Warning: using b for temporary storage! src and dst must not be b!
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200113 * This avoids allocating one more 16 bytes buffer while allowing src == dst.
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200114 */
115#define CTR_CRYPT( dst, src, len ) \
116 if( ( ret = cipher_update( &ctx->cipher_ctx, ctr, 16, b, &olen ) ) != 0 ) \
117 return( ret ); \
118 \
119 for( i = 0; i < len; i++ ) \
120 dst[i] = src[i] ^ b[i];
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200121
122/*
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200123 * Authenticated encryption or decryption
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200124 */
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200125static int ccm_auth_crypt( ccm_context *ctx, int mode, size_t length,
126 const unsigned char *iv, size_t iv_len,
127 const unsigned char *add, size_t add_len,
128 const unsigned char *input, unsigned char *output,
129 unsigned char *tag, size_t tag_len )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200130{
131 int ret;
132 unsigned char i;
133 unsigned char q = 16 - 1 - iv_len;
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200134 size_t len_left, olen;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200135 unsigned char b[16];
136 unsigned char y[16];
137 unsigned char ctr[16];
138 const unsigned char *src;
139 unsigned char *dst;
140
141 /*
142 * Check length requirements: SP800-38C A.1
143 * Additional requirement: a < 2^16 - 2^8 to simplify the code.
144 * 'length' checked later (when writing it to the first block)
145 */
146 if( tag_len < 4 || tag_len > 16 || tag_len % 2 != 0 )
147 return( POLARSSL_ERR_CCM_BAD_INPUT );
148
149 /* Also implies q is within bounds */
150 if( iv_len < 7 || iv_len > 13 )
151 return( POLARSSL_ERR_CCM_BAD_INPUT );
152
153 if( add_len > 0xFF00 )
154 return( POLARSSL_ERR_CCM_BAD_INPUT );
155
156 /*
157 * First block B_0:
158 * 0 .. 0 flags
159 * 1 .. iv_len nonce (aka iv)
160 * iv_len+1 .. 15 length
161 *
162 * With flags as (bits):
163 * 7 0
164 * 6 add present?
165 * 5 .. 3 (t - 2) / 2
166 * 2 .. 0 q - 1
167 */
168 b[0] = 0;
169 b[0] |= ( add_len > 0 ) << 6;
170 b[0] |= ( ( tag_len - 2 ) / 2 ) << 3;
171 b[0] |= q - 1;
172
173 memcpy( b + 1, iv, iv_len );
174
175 for( i = 0, len_left = length; i < q; i++, len_left >>= 8 )
176 b[15-i] = (unsigned char)( len_left & 0xFF );
177
178 if( len_left > 0 )
179 return( POLARSSL_ERR_CCM_BAD_INPUT );
180
181
182 /* Start CBC-MAC with first block */
183 memset( y, 0, 16 );
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200184 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200185
186 /*
187 * If there is additional data, update CBC-MAC with
188 * add_len, add, 0 (padding to a block boundary)
189 */
190 if( add_len > 0 )
191 {
192 size_t use_len;
193 len_left = add_len;
194 src = add;
195
196 memset( b, 0, 16 );
197 b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF );
198 b[1] = (unsigned char)( ( add_len ) & 0xFF );
199
200 use_len = len_left < 16 - 2 ? len_left : 16 - 2;
201 memcpy( b + 2, src, use_len );
202 len_left -= use_len;
203 src += use_len;
204
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200205 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200206
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200207 while( len_left > 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200208 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200209 use_len = len_left > 16 ? 16 : len_left;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200210
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200211 memset( b, 0, 16 );
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200212 memcpy( b, src, use_len );
213 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200214
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200215 len_left -= use_len;
216 src += use_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200217 }
218 }
219
220 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200221 * Prepare counter block for encryption:
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200222 * 0 .. 0 flags
223 * 1 .. iv_len nonce (aka iv)
224 * iv_len+1 .. 15 counter (initially 1)
225 *
226 * With flags as (bits):
227 * 7 .. 3 0
228 * 2 .. 0 q - 1
229 */
230 ctr[0] = q - 1;
231 memcpy( ctr + 1, iv, iv_len );
232 memset( ctr + 1 + iv_len, 0, q );
233 ctr[15] = 1;
234
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200235 /*
236 * Authenticate and {en,de}crypt the message.
237 *
238 * The only difference between encryption and decryption is
239 * the respective order of authentication and {en,de}cryption.
240 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200241 len_left = length;
242 src = input;
243 dst = output;
244
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200245 while( len_left > 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200246 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200247 unsigned char use_len = len_left > 16 ? 16 : len_left;
248
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200249 if( mode == CCM_ENCRYPT )
250 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200251 memset( b, 0, 16 );
252 memcpy( b, src, use_len );
253 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200254 }
255
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200256 CTR_CRYPT( dst, src, use_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200257
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200258 if( mode == CCM_DECRYPT )
259 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200260 memset( b, 0, 16 );
261 memcpy( b, dst, use_len );
262 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200263 }
264
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200265 dst += use_len;
266 src += use_len;
267 len_left -= use_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200268
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200269 /*
270 * Increment counter.
271 * No need to check for overflow thanks to the length check above.
272 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200273 for( i = 0; i < q; i++ )
274 if( ++ctr[15-i] != 0 )
275 break;
276 }
277
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200278 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200279 * Authentication: reset counter and crypt/mask internal tag
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200280 */
281 for( i = 0; i < q; i++ )
282 ctr[15-i] = 0;
283
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200284 CTR_CRYPT( y, y, 16 );
285 memcpy( tag, y, tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200286
287 return( 0 );
288}
289
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200290/*
291 * Authenticated encryption
292 */
293int ccm_encrypt_and_tag( ccm_context *ctx, size_t length,
294 const unsigned char *iv, size_t iv_len,
295 const unsigned char *add, size_t add_len,
296 const unsigned char *input, unsigned char *output,
297 unsigned char *tag, size_t tag_len )
298{
299 return( ccm_auth_crypt( ctx, CCM_ENCRYPT, length, iv, iv_len,
300 add, add_len, input, output, tag, tag_len ) );
301}
302
303/*
304 * Authenticated decryption
305 */
306int ccm_auth_decrypt( ccm_context *ctx, size_t length,
307 const unsigned char *iv, size_t iv_len,
308 const unsigned char *add, size_t add_len,
309 const unsigned char *input, unsigned char *output,
310 const unsigned char *tag, size_t tag_len )
311{
312 int ret;
313 unsigned char check_tag[16];
314 unsigned char i;
315 int diff;
316
317 if( ( ret = ccm_auth_crypt( ctx, CCM_DECRYPT, length,
318 iv, iv_len, add, add_len,
319 input, output, check_tag, tag_len ) ) != 0 )
320 {
321 return( ret );
322 }
323
324 /* Check tag in "constant-time" */
325 for( diff = 0, i = 0; i < tag_len; i++ )
326 diff |= tag[i] ^ check_tag[i];
327
328 if( diff != 0 )
329 {
Paul Bakker34617722014-06-13 17:20:13 +0200330 polarssl_zeroize( output, length );
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200331 return( POLARSSL_ERR_CCM_AUTH_FAILED );
332 }
333
334 return( 0 );
335}
336
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200337
338#if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_AES_C)
339
340#if defined(POLARSSL_PLATFORM_C)
341#include "polarssl/platform.h"
342#else
Paul Bakker5b11d022014-07-10 13:54:38 +0200343#include <stdio.h>
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200344#define polarssl_printf printf
345#endif
346
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200347/*
348 * Examples 1 to 3 from SP800-38C Appendix C
349 */
350
351#define NB_TESTS 3
352
353/*
354 * The data is the same for all tests, only the used length changes
355 */
356static const unsigned char key[] = {
357 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
358 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
359};
360
361static const unsigned char iv[] = {
362 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
363 0x18, 0x19, 0x1a, 0x1b
364};
365
366static const unsigned char ad[] = {
367 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
368 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
369 0x10, 0x11, 0x12, 0x13
370};
371
372static const unsigned char msg[] = {
373 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
374 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
375 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
376};
377
378static const size_t iv_len [NB_TESTS] = { 7, 8, 12 };
379static const size_t add_len[NB_TESTS] = { 8, 16, 20 };
380static const size_t msg_len[NB_TESTS] = { 4, 16, 24 };
381static const size_t tag_len[NB_TESTS] = { 4, 6, 8 };
382
383static const unsigned char res[NB_TESTS][32] = {
384 { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
385 { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
386 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
387 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
388 { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
389 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
390 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
391 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
392};
393
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200394int ccm_self_test( int verbose )
395{
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200396 ccm_context ctx;
397 unsigned char out[32];
398 size_t i;
399 int ret;
400
401 if( ccm_init( &ctx, POLARSSL_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
402 {
403 if( verbose != 0 )
404 polarssl_printf( " CCM: setup failed" );
405
406 return( 1 );
407 }
408
409 for( i = 0; i < NB_TESTS; i++ )
410 {
411 if( verbose != 0 )
412 polarssl_printf( " CCM-AES #%u: ", (unsigned int) i + 1 );
413
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200414 ret = ccm_encrypt_and_tag( &ctx, msg_len[i],
415 iv, iv_len[i], ad, add_len[i],
416 msg, out,
417 out + msg_len[i], tag_len[i] );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200418
419 if( ret != 0 ||
420 memcmp( out, res[i], msg_len[i] + tag_len[i] ) != 0 )
421 {
422 if( verbose != 0 )
423 polarssl_printf( "failed\n" );
424
425 return( 1 );
426 }
427
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200428 ret = ccm_auth_decrypt( &ctx, msg_len[i],
429 iv, iv_len[i], ad, add_len[i],
430 res[i], out,
431 res[i] + msg_len[i], tag_len[i] );
432
433 if( ret != 0 ||
434 memcmp( out, msg, msg_len[i] ) != 0 )
435 {
436 if( verbose != 0 )
437 polarssl_printf( "failed\n" );
438
439 return( 1 );
440 }
441
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200442 if( verbose != 0 )
443 polarssl_printf( "passed\n" );
444 }
445
446 ccm_free( &ctx );
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200447
448 if( verbose != 0 )
449 polarssl_printf( "\n" );
450
451 return( 0 );
452}
453
454#endif /* POLARSSL_SELF_TEST && POLARSSL_AES_C */
455
456#endif /* POLARSSL_CCM_C */