Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 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 | /* |
| 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é-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame^] | 45 | #define CCM_ENCRYPT 0 |
| 46 | #define CCM_DECRYPT 1 |
| 47 | |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 48 | /* |
| 49 | * Initialize context |
| 50 | */ |
| 51 | int 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 | */ |
| 81 | void 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é-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 87 | #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é-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame^] | 116 | * Authenticated encryption or decryption |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 117 | */ |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame^] | 118 | static 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é-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 123 | { |
| 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 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame^] | 149 | if( mode != CCM_ENCRYPT ) |
| 150 | return( POLARSSL_ERR_CCM_BAD_INPUT ); /* Not implemented yet */ |
| 151 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 152 | /* |
| 153 | * First block B_0: |
| 154 | * 0 .. 0 flags |
| 155 | * 1 .. iv_len nonce (aka iv) |
| 156 | * iv_len+1 .. 15 length |
| 157 | * |
| 158 | * With flags as (bits): |
| 159 | * 7 0 |
| 160 | * 6 add present? |
| 161 | * 5 .. 3 (t - 2) / 2 |
| 162 | * 2 .. 0 q - 1 |
| 163 | */ |
| 164 | b[0] = 0; |
| 165 | b[0] |= ( add_len > 0 ) << 6; |
| 166 | b[0] |= ( ( tag_len - 2 ) / 2 ) << 3; |
| 167 | b[0] |= q - 1; |
| 168 | |
| 169 | memcpy( b + 1, iv, iv_len ); |
| 170 | |
| 171 | for( i = 0, len_left = length; i < q; i++, len_left >>= 8 ) |
| 172 | b[15-i] = (unsigned char)( len_left & 0xFF ); |
| 173 | |
| 174 | if( len_left > 0 ) |
| 175 | return( POLARSSL_ERR_CCM_BAD_INPUT ); |
| 176 | |
| 177 | |
| 178 | /* Start CBC-MAC with first block */ |
| 179 | memset( y, 0, 16 ); |
| 180 | UPDATE_CBC_MAC( b ); |
| 181 | |
| 182 | /* |
| 183 | * If there is additional data, update CBC-MAC with |
| 184 | * add_len, add, 0 (padding to a block boundary) |
| 185 | */ |
| 186 | if( add_len > 0 ) |
| 187 | { |
| 188 | size_t use_len; |
| 189 | len_left = add_len; |
| 190 | src = add; |
| 191 | |
| 192 | memset( b, 0, 16 ); |
| 193 | b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF ); |
| 194 | b[1] = (unsigned char)( ( add_len ) & 0xFF ); |
| 195 | |
| 196 | use_len = len_left < 16 - 2 ? len_left : 16 - 2; |
| 197 | memcpy( b + 2, src, use_len ); |
| 198 | len_left -= use_len; |
| 199 | src += use_len; |
| 200 | |
| 201 | UPDATE_CBC_MAC( b ); |
| 202 | |
| 203 | while( len_left > 16 ) |
| 204 | { |
| 205 | memcpy( b, src, 16 ); |
| 206 | UPDATE_CBC_MAC( b ); |
| 207 | |
| 208 | len_left -= 16; |
| 209 | src += 16; |
| 210 | } |
| 211 | |
| 212 | if( len_left > 0 ) |
| 213 | { |
| 214 | memset( b, 0, 16 ); |
| 215 | memcpy( b, src, len_left ); |
| 216 | |
| 217 | UPDATE_CBC_MAC( b ); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Counter block: |
| 223 | * 0 .. 0 flags |
| 224 | * 1 .. iv_len nonce (aka iv) |
| 225 | * iv_len+1 .. 15 counter (initially 1) |
| 226 | * |
| 227 | * With flags as (bits): |
| 228 | * 7 .. 3 0 |
| 229 | * 2 .. 0 q - 1 |
| 230 | */ |
| 231 | ctr[0] = q - 1; |
| 232 | memcpy( ctr + 1, iv, iv_len ); |
| 233 | memset( ctr + 1 + iv_len, 0, q ); |
| 234 | ctr[15] = 1; |
| 235 | |
| 236 | len_left = length; |
| 237 | src = input; |
| 238 | dst = output; |
| 239 | |
| 240 | /* |
| 241 | * Authenticate and encrypt message |
| 242 | */ |
| 243 | while( len_left > 16 ) |
| 244 | { |
| 245 | memcpy( b, src, 16 ); |
| 246 | UPDATE_CBC_MAC( b ); |
| 247 | CTR_CRYPT_BLOCK( dst, src ); |
| 248 | |
| 249 | dst += 16; |
| 250 | src += 16; |
| 251 | len_left -= 16; |
| 252 | |
| 253 | for( i = 0; i < q; i++ ) |
| 254 | if( ++ctr[15-i] != 0 ) |
| 255 | break; |
| 256 | } |
| 257 | |
| 258 | if( len_left > 0 ) |
| 259 | { |
| 260 | unsigned char mask[16]; |
| 261 | size_t olen; |
| 262 | |
| 263 | memset( b, 0, 16 ); |
| 264 | memcpy( b, src, len_left ); |
| 265 | |
| 266 | UPDATE_CBC_MAC( b ); |
| 267 | |
| 268 | if( ( ret = cipher_update( &ctx->cipher_ctx, ctr, 16, |
| 269 | mask, &olen ) ) != 0 ) |
| 270 | { |
| 271 | return( ret ); |
| 272 | } |
| 273 | |
| 274 | for( i = 0; i < len_left; i++ ) |
| 275 | dst[i] = src[i] ^ mask[i]; |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * Authentication: reset counter and encrypt internal tag |
| 280 | */ |
| 281 | for( i = 0; i < q; i++ ) |
| 282 | ctr[15-i] = 0; |
| 283 | |
| 284 | CTR_CRYPT_BLOCK( b, y ); |
| 285 | memcpy( tag, b, tag_len ); |
| 286 | |
| 287 | return( 0 ); |
| 288 | } |
| 289 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame^] | 290 | /* |
| 291 | * Authenticated encryption |
| 292 | */ |
| 293 | int 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 | */ |
| 306 | int 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 | { |
| 330 | memset( output, 0, length ); |
| 331 | return( POLARSSL_ERR_CCM_AUTH_FAILED ); |
| 332 | } |
| 333 | |
| 334 | return( 0 ); |
| 335 | } |
| 336 | |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 337 | |
| 338 | #if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_AES_C) |
| 339 | |
| 340 | #if defined(POLARSSL_PLATFORM_C) |
| 341 | #include "polarssl/platform.h" |
| 342 | #else |
| 343 | #define polarssl_printf printf |
| 344 | #endif |
| 345 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 346 | /* |
| 347 | * Examples 1 to 3 from SP800-38C Appendix C |
| 348 | */ |
| 349 | |
| 350 | #define NB_TESTS 3 |
| 351 | |
| 352 | /* |
| 353 | * The data is the same for all tests, only the used length changes |
| 354 | */ |
| 355 | static const unsigned char key[] = { |
| 356 | 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, |
| 357 | 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f |
| 358 | }; |
| 359 | |
| 360 | static const unsigned char iv[] = { |
| 361 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 362 | 0x18, 0x19, 0x1a, 0x1b |
| 363 | }; |
| 364 | |
| 365 | static const unsigned char ad[] = { |
| 366 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 367 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 368 | 0x10, 0x11, 0x12, 0x13 |
| 369 | }; |
| 370 | |
| 371 | static const unsigned char msg[] = { |
| 372 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, |
| 373 | 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, |
| 374 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 375 | }; |
| 376 | |
| 377 | static const size_t iv_len [NB_TESTS] = { 7, 8, 12 }; |
| 378 | static const size_t add_len[NB_TESTS] = { 8, 16, 20 }; |
| 379 | static const size_t msg_len[NB_TESTS] = { 4, 16, 24 }; |
| 380 | static const size_t tag_len[NB_TESTS] = { 4, 6, 8 }; |
| 381 | |
| 382 | static const unsigned char res[NB_TESTS][32] = { |
| 383 | { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d }, |
| 384 | { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62, |
| 385 | 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d, |
| 386 | 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd }, |
| 387 | { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a, |
| 388 | 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b, |
| 389 | 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5, |
| 390 | 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 } |
| 391 | }; |
| 392 | |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 393 | int ccm_self_test( int verbose ) |
| 394 | { |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 395 | ccm_context ctx; |
| 396 | unsigned char out[32]; |
| 397 | size_t i; |
| 398 | int ret; |
| 399 | |
| 400 | if( ccm_init( &ctx, POLARSSL_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 ) |
| 401 | { |
| 402 | if( verbose != 0 ) |
| 403 | polarssl_printf( " CCM: setup failed" ); |
| 404 | |
| 405 | return( 1 ); |
| 406 | } |
| 407 | |
| 408 | for( i = 0; i < NB_TESTS; i++ ) |
| 409 | { |
| 410 | if( verbose != 0 ) |
| 411 | polarssl_printf( " CCM-AES #%u: ", (unsigned int) i + 1 ); |
| 412 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame^] | 413 | ret = ccm_encrypt_and_tag( &ctx, msg_len[i], |
| 414 | iv, iv_len[i], ad, add_len[i], |
| 415 | msg, out, |
| 416 | out + msg_len[i], tag_len[i] ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 417 | |
| 418 | if( ret != 0 || |
| 419 | memcmp( out, res[i], msg_len[i] + tag_len[i] ) != 0 ) |
| 420 | { |
| 421 | if( verbose != 0 ) |
| 422 | polarssl_printf( "failed\n" ); |
| 423 | |
| 424 | return( 1 ); |
| 425 | } |
| 426 | |
| 427 | if( verbose != 0 ) |
| 428 | polarssl_printf( "passed\n" ); |
| 429 | } |
| 430 | |
| 431 | ccm_free( &ctx ); |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 432 | |
| 433 | if( verbose != 0 ) |
| 434 | polarssl_printf( "\n" ); |
| 435 | |
| 436 | return( 0 ); |
| 437 | } |
| 438 | |
| 439 | #endif /* POLARSSL_SELF_TEST && POLARSSL_AES_C */ |
| 440 | |
| 441 | #endif /* POLARSSL_CCM_C */ |