blob: fafa2386979f35efb102357e16a4e9b679386a04 [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é-Gonnard637eb3d2014-05-06 12:13:09 +020087#define UPDATE_CBC_MAC( src ) \
88{ \
89 size_t olen; \
90 \
91 for( i = 0; i < 16; i++ ) \
92 src[i] ^= y[i]; \
93 \
94 if( ( ret = cipher_update( &ctx->cipher_ctx, src, 16, \
95 y, &olen ) ) != 0 ) \
96 { \
97 return( ret ); \
98 } \
99}
100
101#define CTR_CRYPT_BLOCK( dst, src ) \
102{ \
103 size_t olen; \
104 \
105 if( ( ret = cipher_update( &ctx->cipher_ctx, ctr, 16, \
106 dst, &olen ) ) != 0 ) \
107 { \
108 return( ret ); \
109 } \
110 \
111 for( i = 0; i < 16; i++ ) \
112 dst[i] ^= src[i]; \
113}
114
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;
127 size_t len_left;
128 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 );
177 UPDATE_CBC_MAC( b );
178
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
198 UPDATE_CBC_MAC( b );
199
200 while( len_left > 16 )
201 {
202 memcpy( b, src, 16 );
203 UPDATE_CBC_MAC( b );
204
205 len_left -= 16;
206 src += 16;
207 }
208
209 if( len_left > 0 )
210 {
211 memset( b, 0, 16 );
212 memcpy( b, src, len_left );
213
214 UPDATE_CBC_MAC( b );
215 }
216 }
217
218 /*
219 * Counter block:
220 * 0 .. 0 flags
221 * 1 .. iv_len nonce (aka iv)
222 * iv_len+1 .. 15 counter (initially 1)
223 *
224 * With flags as (bits):
225 * 7 .. 3 0
226 * 2 .. 0 q - 1
227 */
228 ctr[0] = q - 1;
229 memcpy( ctr + 1, iv, iv_len );
230 memset( ctr + 1 + iv_len, 0, q );
231 ctr[15] = 1;
232
233 len_left = length;
234 src = input;
235 dst = output;
236
237 /*
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200238 * Authenticate and crypt message
239 * The only difference between encryption and decryption is
240 * the respective order of authentication and {en,de}cryption
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200241 */
242 while( len_left > 16 )
243 {
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200244 if( mode == CCM_ENCRYPT )
245 {
246 memcpy( b, src, 16 );
247 UPDATE_CBC_MAC( b );
248 }
249
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200250 CTR_CRYPT_BLOCK( dst, src );
251
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200252 if( mode == CCM_DECRYPT )
253 {
254 memcpy( b, dst, 16 );
255 UPDATE_CBC_MAC( b );
256 }
257
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200258 dst += 16;
259 src += 16;
260 len_left -= 16;
261
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
271 if( len_left > 0 )
272 {
273 unsigned char mask[16];
274 size_t olen;
275
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200276 if( mode == CCM_ENCRYPT )
277 {
278 memset( b, 0, 16 );
279 memcpy( b, src, len_left );
280 UPDATE_CBC_MAC( b );
281 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200282
283 if( ( ret = cipher_update( &ctx->cipher_ctx, ctr, 16,
284 mask, &olen ) ) != 0 )
285 {
286 return( ret );
287 }
288
289 for( i = 0; i < len_left; i++ )
290 dst[i] = src[i] ^ mask[i];
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200291
292 if( mode == CCM_DECRYPT )
293 {
294 memset( b, 0, 16 );
295 memcpy( b, dst, len_left );
296 UPDATE_CBC_MAC( b );
297 }
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200298 }
299
300 /*
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200301 * Authentication: reset counter and {en,de}crypt internal tag
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200302 */
303 for( i = 0; i < q; i++ )
304 ctr[15-i] = 0;
305
306 CTR_CRYPT_BLOCK( b, y );
307 memcpy( tag, b, tag_len );
308
309 return( 0 );
310}
311
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200312/*
313 * Authenticated encryption
314 */
315int ccm_encrypt_and_tag( ccm_context *ctx, size_t length,
316 const unsigned char *iv, size_t iv_len,
317 const unsigned char *add, size_t add_len,
318 const unsigned char *input, unsigned char *output,
319 unsigned char *tag, size_t tag_len )
320{
321 return( ccm_auth_crypt( ctx, CCM_ENCRYPT, length, iv, iv_len,
322 add, add_len, input, output, tag, tag_len ) );
323}
324
325/*
326 * Authenticated decryption
327 */
328int ccm_auth_decrypt( ccm_context *ctx, size_t length,
329 const unsigned char *iv, size_t iv_len,
330 const unsigned char *add, size_t add_len,
331 const unsigned char *input, unsigned char *output,
332 const unsigned char *tag, size_t tag_len )
333{
334 int ret;
335 unsigned char check_tag[16];
336 unsigned char i;
337 int diff;
338
339 if( ( ret = ccm_auth_crypt( ctx, CCM_DECRYPT, length,
340 iv, iv_len, add, add_len,
341 input, output, check_tag, tag_len ) ) != 0 )
342 {
343 return( ret );
344 }
345
346 /* Check tag in "constant-time" */
347 for( diff = 0, i = 0; i < tag_len; i++ )
348 diff |= tag[i] ^ check_tag[i];
349
350 if( diff != 0 )
351 {
352 memset( output, 0, length );
353 return( POLARSSL_ERR_CCM_AUTH_FAILED );
354 }
355
356 return( 0 );
357}
358
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200359
360#if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_AES_C)
361
362#if defined(POLARSSL_PLATFORM_C)
363#include "polarssl/platform.h"
364#else
365#define polarssl_printf printf
366#endif
367
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200368/*
369 * Examples 1 to 3 from SP800-38C Appendix C
370 */
371
372#define NB_TESTS 3
373
374/*
375 * The data is the same for all tests, only the used length changes
376 */
377static const unsigned char key[] = {
378 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
379 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
380};
381
382static const unsigned char iv[] = {
383 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
384 0x18, 0x19, 0x1a, 0x1b
385};
386
387static const unsigned char ad[] = {
388 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
389 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
390 0x10, 0x11, 0x12, 0x13
391};
392
393static const unsigned char msg[] = {
394 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
395 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
396 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
397};
398
399static const size_t iv_len [NB_TESTS] = { 7, 8, 12 };
400static const size_t add_len[NB_TESTS] = { 8, 16, 20 };
401static const size_t msg_len[NB_TESTS] = { 4, 16, 24 };
402static const size_t tag_len[NB_TESTS] = { 4, 6, 8 };
403
404static const unsigned char res[NB_TESTS][32] = {
405 { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
406 { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
407 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
408 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
409 { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
410 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
411 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
412 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
413};
414
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200415int ccm_self_test( int verbose )
416{
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200417 ccm_context ctx;
418 unsigned char out[32];
419 size_t i;
420 int ret;
421
422 if( ccm_init( &ctx, POLARSSL_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
423 {
424 if( verbose != 0 )
425 polarssl_printf( " CCM: setup failed" );
426
427 return( 1 );
428 }
429
430 for( i = 0; i < NB_TESTS; i++ )
431 {
432 if( verbose != 0 )
433 polarssl_printf( " CCM-AES #%u: ", (unsigned int) i + 1 );
434
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200435 ret = ccm_encrypt_and_tag( &ctx, msg_len[i],
436 iv, iv_len[i], ad, add_len[i],
437 msg, out,
438 out + msg_len[i], tag_len[i] );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200439
440 if( ret != 0 ||
441 memcmp( out, res[i], msg_len[i] + tag_len[i] ) != 0 )
442 {
443 if( verbose != 0 )
444 polarssl_printf( "failed\n" );
445
446 return( 1 );
447 }
448
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200449 ret = ccm_auth_decrypt( &ctx, msg_len[i],
450 iv, iv_len[i], ad, add_len[i],
451 res[i], out,
452 res[i] + msg_len[i], tag_len[i] );
453
454 if( ret != 0 ||
455 memcmp( out, msg, msg_len[i] ) != 0 )
456 {
457 if( verbose != 0 )
458 polarssl_printf( "failed\n" );
459
460 return( 1 );
461 }
462
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200463 if( verbose != 0 )
464 polarssl_printf( "passed\n" );
465 }
466
467 ccm_free( &ctx );
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200468
469 if( verbose != 0 )
470 polarssl_printf( "\n" );
471
472 return( 0 );
473}
474
475#endif /* POLARSSL_SELF_TEST && POLARSSL_AES_C */
476
477#endif /* POLARSSL_CCM_C */