Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Simon Cooksey 0:fb7af294d5d9 1 /*
Simon Cooksey 0:fb7af294d5d9 2 * NIST SP800-38C compliant CCM implementation
Simon Cooksey 0:fb7af294d5d9 3 *
Simon Cooksey 0:fb7af294d5d9 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Simon Cooksey 0:fb7af294d5d9 5 * SPDX-License-Identifier: Apache-2.0
Simon Cooksey 0:fb7af294d5d9 6 *
Simon Cooksey 0:fb7af294d5d9 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Simon Cooksey 0:fb7af294d5d9 8 * not use this file except in compliance with the License.
Simon Cooksey 0:fb7af294d5d9 9 * You may obtain a copy of the License at
Simon Cooksey 0:fb7af294d5d9 10 *
Simon Cooksey 0:fb7af294d5d9 11 * http://www.apache.org/licenses/LICENSE-2.0
Simon Cooksey 0:fb7af294d5d9 12 *
Simon Cooksey 0:fb7af294d5d9 13 * Unless required by applicable law or agreed to in writing, software
Simon Cooksey 0:fb7af294d5d9 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Simon Cooksey 0:fb7af294d5d9 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Simon Cooksey 0:fb7af294d5d9 16 * See the License for the specific language governing permissions and
Simon Cooksey 0:fb7af294d5d9 17 * limitations under the License.
Simon Cooksey 0:fb7af294d5d9 18 *
Simon Cooksey 0:fb7af294d5d9 19 * This file is part of mbed TLS (https://tls.mbed.org)
Simon Cooksey 0:fb7af294d5d9 20 */
Simon Cooksey 0:fb7af294d5d9 21
Simon Cooksey 0:fb7af294d5d9 22 /*
Simon Cooksey 0:fb7af294d5d9 23 * Definition of CCM:
Simon Cooksey 0:fb7af294d5d9 24 * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
Simon Cooksey 0:fb7af294d5d9 25 * RFC 3610 "Counter with CBC-MAC (CCM)"
Simon Cooksey 0:fb7af294d5d9 26 *
Simon Cooksey 0:fb7af294d5d9 27 * Related:
Simon Cooksey 0:fb7af294d5d9 28 * RFC 5116 "An Interface and Algorithms for Authenticated Encryption"
Simon Cooksey 0:fb7af294d5d9 29 */
Simon Cooksey 0:fb7af294d5d9 30
Simon Cooksey 0:fb7af294d5d9 31 #if !defined(MBEDTLS_CONFIG_FILE)
Simon Cooksey 0:fb7af294d5d9 32 #include "mbedtls/config.h"
Simon Cooksey 0:fb7af294d5d9 33 #else
Simon Cooksey 0:fb7af294d5d9 34 #include MBEDTLS_CONFIG_FILE
Simon Cooksey 0:fb7af294d5d9 35 #endif
Simon Cooksey 0:fb7af294d5d9 36
Simon Cooksey 0:fb7af294d5d9 37 #if defined(MBEDTLS_CCM_C)
Simon Cooksey 0:fb7af294d5d9 38
Simon Cooksey 0:fb7af294d5d9 39 #include "mbedtls/ccm.h"
Simon Cooksey 0:fb7af294d5d9 40
Simon Cooksey 0:fb7af294d5d9 41 #include <string.h>
Simon Cooksey 0:fb7af294d5d9 42
Simon Cooksey 0:fb7af294d5d9 43 #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Simon Cooksey 0:fb7af294d5d9 44 #if defined(MBEDTLS_PLATFORM_C)
Simon Cooksey 0:fb7af294d5d9 45 #include "mbedtls/platform.h"
Simon Cooksey 0:fb7af294d5d9 46 #else
Simon Cooksey 0:fb7af294d5d9 47 #include <stdio.h>
Simon Cooksey 0:fb7af294d5d9 48 #define mbedtls_printf printf
Simon Cooksey 0:fb7af294d5d9 49 #endif /* MBEDTLS_PLATFORM_C */
Simon Cooksey 0:fb7af294d5d9 50 #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Simon Cooksey 0:fb7af294d5d9 51
Simon Cooksey 0:fb7af294d5d9 52 /* Implementation that should never be optimized out by the compiler */
Simon Cooksey 0:fb7af294d5d9 53 static void mbedtls_zeroize( void *v, size_t n ) {
Simon Cooksey 0:fb7af294d5d9 54 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
Simon Cooksey 0:fb7af294d5d9 55 }
Simon Cooksey 0:fb7af294d5d9 56
Simon Cooksey 0:fb7af294d5d9 57 #define CCM_ENCRYPT 0
Simon Cooksey 0:fb7af294d5d9 58 #define CCM_DECRYPT 1
Simon Cooksey 0:fb7af294d5d9 59
Simon Cooksey 0:fb7af294d5d9 60 /*
Simon Cooksey 0:fb7af294d5d9 61 * Initialize context
Simon Cooksey 0:fb7af294d5d9 62 */
Simon Cooksey 0:fb7af294d5d9 63 void mbedtls_ccm_init( mbedtls_ccm_context *ctx )
Simon Cooksey 0:fb7af294d5d9 64 {
Simon Cooksey 0:fb7af294d5d9 65 memset( ctx, 0, sizeof( mbedtls_ccm_context ) );
Simon Cooksey 0:fb7af294d5d9 66 }
Simon Cooksey 0:fb7af294d5d9 67
Simon Cooksey 0:fb7af294d5d9 68 int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
Simon Cooksey 0:fb7af294d5d9 69 mbedtls_cipher_id_t cipher,
Simon Cooksey 0:fb7af294d5d9 70 const unsigned char *key,
Simon Cooksey 0:fb7af294d5d9 71 unsigned int keybits )
Simon Cooksey 0:fb7af294d5d9 72 {
Simon Cooksey 0:fb7af294d5d9 73 int ret;
Simon Cooksey 0:fb7af294d5d9 74 const mbedtls_cipher_info_t *cipher_info;
Simon Cooksey 0:fb7af294d5d9 75
Simon Cooksey 0:fb7af294d5d9 76 cipher_info = mbedtls_cipher_info_from_values( cipher, keybits, MBEDTLS_MODE_ECB );
Simon Cooksey 0:fb7af294d5d9 77 if( cipher_info == NULL )
Simon Cooksey 0:fb7af294d5d9 78 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Simon Cooksey 0:fb7af294d5d9 79
Simon Cooksey 0:fb7af294d5d9 80 if( cipher_info->block_size != 16 )
Simon Cooksey 0:fb7af294d5d9 81 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Simon Cooksey 0:fb7af294d5d9 82
Simon Cooksey 0:fb7af294d5d9 83 mbedtls_cipher_free( &ctx->cipher_ctx );
Simon Cooksey 0:fb7af294d5d9 84
Simon Cooksey 0:fb7af294d5d9 85 if( ( ret = mbedtls_cipher_setup( &ctx->cipher_ctx, cipher_info ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 86 return( ret );
Simon Cooksey 0:fb7af294d5d9 87
Simon Cooksey 0:fb7af294d5d9 88 if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keybits,
Simon Cooksey 0:fb7af294d5d9 89 MBEDTLS_ENCRYPT ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 90 {
Simon Cooksey 0:fb7af294d5d9 91 return( ret );
Simon Cooksey 0:fb7af294d5d9 92 }
Simon Cooksey 0:fb7af294d5d9 93
Simon Cooksey 0:fb7af294d5d9 94 return( 0 );
Simon Cooksey 0:fb7af294d5d9 95 }
Simon Cooksey 0:fb7af294d5d9 96
Simon Cooksey 0:fb7af294d5d9 97 /*
Simon Cooksey 0:fb7af294d5d9 98 * Free context
Simon Cooksey 0:fb7af294d5d9 99 */
Simon Cooksey 0:fb7af294d5d9 100 void mbedtls_ccm_free( mbedtls_ccm_context *ctx )
Simon Cooksey 0:fb7af294d5d9 101 {
Simon Cooksey 0:fb7af294d5d9 102 mbedtls_cipher_free( &ctx->cipher_ctx );
Simon Cooksey 0:fb7af294d5d9 103 mbedtls_zeroize( ctx, sizeof( mbedtls_ccm_context ) );
Simon Cooksey 0:fb7af294d5d9 104 }
Simon Cooksey 0:fb7af294d5d9 105
Simon Cooksey 0:fb7af294d5d9 106 /*
Simon Cooksey 0:fb7af294d5d9 107 * Macros for common operations.
Simon Cooksey 0:fb7af294d5d9 108 * Results in smaller compiled code than static inline functions.
Simon Cooksey 0:fb7af294d5d9 109 */
Simon Cooksey 0:fb7af294d5d9 110
Simon Cooksey 0:fb7af294d5d9 111 /*
Simon Cooksey 0:fb7af294d5d9 112 * Update the CBC-MAC state in y using a block in b
Simon Cooksey 0:fb7af294d5d9 113 * (Always using b as the source helps the compiler optimise a bit better.)
Simon Cooksey 0:fb7af294d5d9 114 */
Simon Cooksey 0:fb7af294d5d9 115 #define UPDATE_CBC_MAC \
Simon Cooksey 0:fb7af294d5d9 116 for( i = 0; i < 16; i++ ) \
Simon Cooksey 0:fb7af294d5d9 117 y[i] ^= b[i]; \
Simon Cooksey 0:fb7af294d5d9 118 \
Simon Cooksey 0:fb7af294d5d9 119 if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \
Simon Cooksey 0:fb7af294d5d9 120 return( ret );
Simon Cooksey 0:fb7af294d5d9 121
Simon Cooksey 0:fb7af294d5d9 122 /*
Simon Cooksey 0:fb7af294d5d9 123 * Encrypt or decrypt a partial block with CTR
Simon Cooksey 0:fb7af294d5d9 124 * Warning: using b for temporary storage! src and dst must not be b!
Simon Cooksey 0:fb7af294d5d9 125 * This avoids allocating one more 16 bytes buffer while allowing src == dst.
Simon Cooksey 0:fb7af294d5d9 126 */
Simon Cooksey 0:fb7af294d5d9 127 #define CTR_CRYPT( dst, src, len ) \
Simon Cooksey 0:fb7af294d5d9 128 if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctr, 16, b, &olen ) ) != 0 ) \
Simon Cooksey 0:fb7af294d5d9 129 return( ret ); \
Simon Cooksey 0:fb7af294d5d9 130 \
Simon Cooksey 0:fb7af294d5d9 131 for( i = 0; i < len; i++ ) \
Simon Cooksey 0:fb7af294d5d9 132 dst[i] = src[i] ^ b[i];
Simon Cooksey 0:fb7af294d5d9 133
Simon Cooksey 0:fb7af294d5d9 134 /*
Simon Cooksey 0:fb7af294d5d9 135 * Authenticated encryption or decryption
Simon Cooksey 0:fb7af294d5d9 136 */
Simon Cooksey 0:fb7af294d5d9 137 static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
Simon Cooksey 0:fb7af294d5d9 138 const unsigned char *iv, size_t iv_len,
Simon Cooksey 0:fb7af294d5d9 139 const unsigned char *add, size_t add_len,
Simon Cooksey 0:fb7af294d5d9 140 const unsigned char *input, unsigned char *output,
Simon Cooksey 0:fb7af294d5d9 141 unsigned char *tag, size_t tag_len )
Simon Cooksey 0:fb7af294d5d9 142 {
Simon Cooksey 0:fb7af294d5d9 143 int ret;
Simon Cooksey 0:fb7af294d5d9 144 unsigned char i;
Simon Cooksey 0:fb7af294d5d9 145 unsigned char q;
Simon Cooksey 0:fb7af294d5d9 146 size_t len_left, olen;
Simon Cooksey 0:fb7af294d5d9 147 unsigned char b[16];
Simon Cooksey 0:fb7af294d5d9 148 unsigned char y[16];
Simon Cooksey 0:fb7af294d5d9 149 unsigned char ctr[16];
Simon Cooksey 0:fb7af294d5d9 150 const unsigned char *src;
Simon Cooksey 0:fb7af294d5d9 151 unsigned char *dst;
Simon Cooksey 0:fb7af294d5d9 152
Simon Cooksey 0:fb7af294d5d9 153 /*
Simon Cooksey 0:fb7af294d5d9 154 * Check length requirements: SP800-38C A.1
Simon Cooksey 0:fb7af294d5d9 155 * Additional requirement: a < 2^16 - 2^8 to simplify the code.
Simon Cooksey 0:fb7af294d5d9 156 * 'length' checked later (when writing it to the first block)
Simon Cooksey 0:fb7af294d5d9 157 */
Simon Cooksey 0:fb7af294d5d9 158 if( tag_len < 4 || tag_len > 16 || tag_len % 2 != 0 )
Simon Cooksey 0:fb7af294d5d9 159 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Simon Cooksey 0:fb7af294d5d9 160
Simon Cooksey 0:fb7af294d5d9 161 /* Also implies q is within bounds */
Simon Cooksey 0:fb7af294d5d9 162 if( iv_len < 7 || iv_len > 13 )
Simon Cooksey 0:fb7af294d5d9 163 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Simon Cooksey 0:fb7af294d5d9 164
Simon Cooksey 0:fb7af294d5d9 165 if( add_len > 0xFF00 )
Simon Cooksey 0:fb7af294d5d9 166 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Simon Cooksey 0:fb7af294d5d9 167
Simon Cooksey 0:fb7af294d5d9 168 q = 16 - 1 - (unsigned char) iv_len;
Simon Cooksey 0:fb7af294d5d9 169
Simon Cooksey 0:fb7af294d5d9 170 /*
Simon Cooksey 0:fb7af294d5d9 171 * First block B_0:
Simon Cooksey 0:fb7af294d5d9 172 * 0 .. 0 flags
Simon Cooksey 0:fb7af294d5d9 173 * 1 .. iv_len nonce (aka iv)
Simon Cooksey 0:fb7af294d5d9 174 * iv_len+1 .. 15 length
Simon Cooksey 0:fb7af294d5d9 175 *
Simon Cooksey 0:fb7af294d5d9 176 * With flags as (bits):
Simon Cooksey 0:fb7af294d5d9 177 * 7 0
Simon Cooksey 0:fb7af294d5d9 178 * 6 add present?
Simon Cooksey 0:fb7af294d5d9 179 * 5 .. 3 (t - 2) / 2
Simon Cooksey 0:fb7af294d5d9 180 * 2 .. 0 q - 1
Simon Cooksey 0:fb7af294d5d9 181 */
Simon Cooksey 0:fb7af294d5d9 182 b[0] = 0;
Simon Cooksey 0:fb7af294d5d9 183 b[0] |= ( add_len > 0 ) << 6;
Simon Cooksey 0:fb7af294d5d9 184 b[0] |= ( ( tag_len - 2 ) / 2 ) << 3;
Simon Cooksey 0:fb7af294d5d9 185 b[0] |= q - 1;
Simon Cooksey 0:fb7af294d5d9 186
Simon Cooksey 0:fb7af294d5d9 187 memcpy( b + 1, iv, iv_len );
Simon Cooksey 0:fb7af294d5d9 188
Simon Cooksey 0:fb7af294d5d9 189 for( i = 0, len_left = length; i < q; i++, len_left >>= 8 )
Simon Cooksey 0:fb7af294d5d9 190 b[15-i] = (unsigned char)( len_left & 0xFF );
Simon Cooksey 0:fb7af294d5d9 191
Simon Cooksey 0:fb7af294d5d9 192 if( len_left > 0 )
Simon Cooksey 0:fb7af294d5d9 193 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Simon Cooksey 0:fb7af294d5d9 194
Simon Cooksey 0:fb7af294d5d9 195
Simon Cooksey 0:fb7af294d5d9 196 /* Start CBC-MAC with first block */
Simon Cooksey 0:fb7af294d5d9 197 memset( y, 0, 16 );
Simon Cooksey 0:fb7af294d5d9 198 UPDATE_CBC_MAC;
Simon Cooksey 0:fb7af294d5d9 199
Simon Cooksey 0:fb7af294d5d9 200 /*
Simon Cooksey 0:fb7af294d5d9 201 * If there is additional data, update CBC-MAC with
Simon Cooksey 0:fb7af294d5d9 202 * add_len, add, 0 (padding to a block boundary)
Simon Cooksey 0:fb7af294d5d9 203 */
Simon Cooksey 0:fb7af294d5d9 204 if( add_len > 0 )
Simon Cooksey 0:fb7af294d5d9 205 {
Simon Cooksey 0:fb7af294d5d9 206 size_t use_len;
Simon Cooksey 0:fb7af294d5d9 207 len_left = add_len;
Simon Cooksey 0:fb7af294d5d9 208 src = add;
Simon Cooksey 0:fb7af294d5d9 209
Simon Cooksey 0:fb7af294d5d9 210 memset( b, 0, 16 );
Simon Cooksey 0:fb7af294d5d9 211 b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF );
Simon Cooksey 0:fb7af294d5d9 212 b[1] = (unsigned char)( ( add_len ) & 0xFF );
Simon Cooksey 0:fb7af294d5d9 213
Simon Cooksey 0:fb7af294d5d9 214 use_len = len_left < 16 - 2 ? len_left : 16 - 2;
Simon Cooksey 0:fb7af294d5d9 215 memcpy( b + 2, src, use_len );
Simon Cooksey 0:fb7af294d5d9 216 len_left -= use_len;
Simon Cooksey 0:fb7af294d5d9 217 src += use_len;
Simon Cooksey 0:fb7af294d5d9 218
Simon Cooksey 0:fb7af294d5d9 219 UPDATE_CBC_MAC;
Simon Cooksey 0:fb7af294d5d9 220
Simon Cooksey 0:fb7af294d5d9 221 while( len_left > 0 )
Simon Cooksey 0:fb7af294d5d9 222 {
Simon Cooksey 0:fb7af294d5d9 223 use_len = len_left > 16 ? 16 : len_left;
Simon Cooksey 0:fb7af294d5d9 224
Simon Cooksey 0:fb7af294d5d9 225 memset( b, 0, 16 );
Simon Cooksey 0:fb7af294d5d9 226 memcpy( b, src, use_len );
Simon Cooksey 0:fb7af294d5d9 227 UPDATE_CBC_MAC;
Simon Cooksey 0:fb7af294d5d9 228
Simon Cooksey 0:fb7af294d5d9 229 len_left -= use_len;
Simon Cooksey 0:fb7af294d5d9 230 src += use_len;
Simon Cooksey 0:fb7af294d5d9 231 }
Simon Cooksey 0:fb7af294d5d9 232 }
Simon Cooksey 0:fb7af294d5d9 233
Simon Cooksey 0:fb7af294d5d9 234 /*
Simon Cooksey 0:fb7af294d5d9 235 * Prepare counter block for encryption:
Simon Cooksey 0:fb7af294d5d9 236 * 0 .. 0 flags
Simon Cooksey 0:fb7af294d5d9 237 * 1 .. iv_len nonce (aka iv)
Simon Cooksey 0:fb7af294d5d9 238 * iv_len+1 .. 15 counter (initially 1)
Simon Cooksey 0:fb7af294d5d9 239 *
Simon Cooksey 0:fb7af294d5d9 240 * With flags as (bits):
Simon Cooksey 0:fb7af294d5d9 241 * 7 .. 3 0
Simon Cooksey 0:fb7af294d5d9 242 * 2 .. 0 q - 1
Simon Cooksey 0:fb7af294d5d9 243 */
Simon Cooksey 0:fb7af294d5d9 244 ctr[0] = q - 1;
Simon Cooksey 0:fb7af294d5d9 245 memcpy( ctr + 1, iv, iv_len );
Simon Cooksey 0:fb7af294d5d9 246 memset( ctr + 1 + iv_len, 0, q );
Simon Cooksey 0:fb7af294d5d9 247 ctr[15] = 1;
Simon Cooksey 0:fb7af294d5d9 248
Simon Cooksey 0:fb7af294d5d9 249 /*
Simon Cooksey 0:fb7af294d5d9 250 * Authenticate and {en,de}crypt the message.
Simon Cooksey 0:fb7af294d5d9 251 *
Simon Cooksey 0:fb7af294d5d9 252 * The only difference between encryption and decryption is
Simon Cooksey 0:fb7af294d5d9 253 * the respective order of authentication and {en,de}cryption.
Simon Cooksey 0:fb7af294d5d9 254 */
Simon Cooksey 0:fb7af294d5d9 255 len_left = length;
Simon Cooksey 0:fb7af294d5d9 256 src = input;
Simon Cooksey 0:fb7af294d5d9 257 dst = output;
Simon Cooksey 0:fb7af294d5d9 258
Simon Cooksey 0:fb7af294d5d9 259 while( len_left > 0 )
Simon Cooksey 0:fb7af294d5d9 260 {
Simon Cooksey 0:fb7af294d5d9 261 size_t use_len = len_left > 16 ? 16 : len_left;
Simon Cooksey 0:fb7af294d5d9 262
Simon Cooksey 0:fb7af294d5d9 263 if( mode == CCM_ENCRYPT )
Simon Cooksey 0:fb7af294d5d9 264 {
Simon Cooksey 0:fb7af294d5d9 265 memset( b, 0, 16 );
Simon Cooksey 0:fb7af294d5d9 266 memcpy( b, src, use_len );
Simon Cooksey 0:fb7af294d5d9 267 UPDATE_CBC_MAC;
Simon Cooksey 0:fb7af294d5d9 268 }
Simon Cooksey 0:fb7af294d5d9 269
Simon Cooksey 0:fb7af294d5d9 270 CTR_CRYPT( dst, src, use_len );
Simon Cooksey 0:fb7af294d5d9 271
Simon Cooksey 0:fb7af294d5d9 272 if( mode == CCM_DECRYPT )
Simon Cooksey 0:fb7af294d5d9 273 {
Simon Cooksey 0:fb7af294d5d9 274 memset( b, 0, 16 );
Simon Cooksey 0:fb7af294d5d9 275 memcpy( b, dst, use_len );
Simon Cooksey 0:fb7af294d5d9 276 UPDATE_CBC_MAC;
Simon Cooksey 0:fb7af294d5d9 277 }
Simon Cooksey 0:fb7af294d5d9 278
Simon Cooksey 0:fb7af294d5d9 279 dst += use_len;
Simon Cooksey 0:fb7af294d5d9 280 src += use_len;
Simon Cooksey 0:fb7af294d5d9 281 len_left -= use_len;
Simon Cooksey 0:fb7af294d5d9 282
Simon Cooksey 0:fb7af294d5d9 283 /*
Simon Cooksey 0:fb7af294d5d9 284 * Increment counter.
Simon Cooksey 0:fb7af294d5d9 285 * No need to check for overflow thanks to the length check above.
Simon Cooksey 0:fb7af294d5d9 286 */
Simon Cooksey 0:fb7af294d5d9 287 for( i = 0; i < q; i++ )
Simon Cooksey 0:fb7af294d5d9 288 if( ++ctr[15-i] != 0 )
Simon Cooksey 0:fb7af294d5d9 289 break;
Simon Cooksey 0:fb7af294d5d9 290 }
Simon Cooksey 0:fb7af294d5d9 291
Simon Cooksey 0:fb7af294d5d9 292 /*
Simon Cooksey 0:fb7af294d5d9 293 * Authentication: reset counter and crypt/mask internal tag
Simon Cooksey 0:fb7af294d5d9 294 */
Simon Cooksey 0:fb7af294d5d9 295 for( i = 0; i < q; i++ )
Simon Cooksey 0:fb7af294d5d9 296 ctr[15-i] = 0;
Simon Cooksey 0:fb7af294d5d9 297
Simon Cooksey 0:fb7af294d5d9 298 CTR_CRYPT( y, y, 16 );
Simon Cooksey 0:fb7af294d5d9 299 memcpy( tag, y, tag_len );
Simon Cooksey 0:fb7af294d5d9 300
Simon Cooksey 0:fb7af294d5d9 301 return( 0 );
Simon Cooksey 0:fb7af294d5d9 302 }
Simon Cooksey 0:fb7af294d5d9 303
Simon Cooksey 0:fb7af294d5d9 304 /*
Simon Cooksey 0:fb7af294d5d9 305 * Authenticated encryption
Simon Cooksey 0:fb7af294d5d9 306 */
Simon Cooksey 0:fb7af294d5d9 307 int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
Simon Cooksey 0:fb7af294d5d9 308 const unsigned char *iv, size_t iv_len,
Simon Cooksey 0:fb7af294d5d9 309 const unsigned char *add, size_t add_len,
Simon Cooksey 0:fb7af294d5d9 310 const unsigned char *input, unsigned char *output,
Simon Cooksey 0:fb7af294d5d9 311 unsigned char *tag, size_t tag_len )
Simon Cooksey 0:fb7af294d5d9 312 {
Simon Cooksey 0:fb7af294d5d9 313 return( ccm_auth_crypt( ctx, CCM_ENCRYPT, length, iv, iv_len,
Simon Cooksey 0:fb7af294d5d9 314 add, add_len, input, output, tag, tag_len ) );
Simon Cooksey 0:fb7af294d5d9 315 }
Simon Cooksey 0:fb7af294d5d9 316
Simon Cooksey 0:fb7af294d5d9 317 /*
Simon Cooksey 0:fb7af294d5d9 318 * Authenticated decryption
Simon Cooksey 0:fb7af294d5d9 319 */
Simon Cooksey 0:fb7af294d5d9 320 int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
Simon Cooksey 0:fb7af294d5d9 321 const unsigned char *iv, size_t iv_len,
Simon Cooksey 0:fb7af294d5d9 322 const unsigned char *add, size_t add_len,
Simon Cooksey 0:fb7af294d5d9 323 const unsigned char *input, unsigned char *output,
Simon Cooksey 0:fb7af294d5d9 324 const unsigned char *tag, size_t tag_len )
Simon Cooksey 0:fb7af294d5d9 325 {
Simon Cooksey 0:fb7af294d5d9 326 int ret;
Simon Cooksey 0:fb7af294d5d9 327 unsigned char check_tag[16];
Simon Cooksey 0:fb7af294d5d9 328 unsigned char i;
Simon Cooksey 0:fb7af294d5d9 329 int diff;
Simon Cooksey 0:fb7af294d5d9 330
Simon Cooksey 0:fb7af294d5d9 331 if( ( ret = ccm_auth_crypt( ctx, CCM_DECRYPT, length,
Simon Cooksey 0:fb7af294d5d9 332 iv, iv_len, add, add_len,
Simon Cooksey 0:fb7af294d5d9 333 input, output, check_tag, tag_len ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 334 {
Simon Cooksey 0:fb7af294d5d9 335 return( ret );
Simon Cooksey 0:fb7af294d5d9 336 }
Simon Cooksey 0:fb7af294d5d9 337
Simon Cooksey 0:fb7af294d5d9 338 /* Check tag in "constant-time" */
Simon Cooksey 0:fb7af294d5d9 339 for( diff = 0, i = 0; i < tag_len; i++ )
Simon Cooksey 0:fb7af294d5d9 340 diff |= tag[i] ^ check_tag[i];
Simon Cooksey 0:fb7af294d5d9 341
Simon Cooksey 0:fb7af294d5d9 342 if( diff != 0 )
Simon Cooksey 0:fb7af294d5d9 343 {
Simon Cooksey 0:fb7af294d5d9 344 mbedtls_zeroize( output, length );
Simon Cooksey 0:fb7af294d5d9 345 return( MBEDTLS_ERR_CCM_AUTH_FAILED );
Simon Cooksey 0:fb7af294d5d9 346 }
Simon Cooksey 0:fb7af294d5d9 347
Simon Cooksey 0:fb7af294d5d9 348 return( 0 );
Simon Cooksey 0:fb7af294d5d9 349 }
Simon Cooksey 0:fb7af294d5d9 350
Simon Cooksey 0:fb7af294d5d9 351
Simon Cooksey 0:fb7af294d5d9 352 #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Simon Cooksey 0:fb7af294d5d9 353 /*
Simon Cooksey 0:fb7af294d5d9 354 * Examples 1 to 3 from SP800-38C Appendix C
Simon Cooksey 0:fb7af294d5d9 355 */
Simon Cooksey 0:fb7af294d5d9 356
Simon Cooksey 0:fb7af294d5d9 357 #define NB_TESTS 3
Simon Cooksey 0:fb7af294d5d9 358
Simon Cooksey 0:fb7af294d5d9 359 /*
Simon Cooksey 0:fb7af294d5d9 360 * The data is the same for all tests, only the used length changes
Simon Cooksey 0:fb7af294d5d9 361 */
Simon Cooksey 0:fb7af294d5d9 362 static const unsigned char key[] = {
Simon Cooksey 0:fb7af294d5d9 363 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
Simon Cooksey 0:fb7af294d5d9 364 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
Simon Cooksey 0:fb7af294d5d9 365 };
Simon Cooksey 0:fb7af294d5d9 366
Simon Cooksey 0:fb7af294d5d9 367 static const unsigned char iv[] = {
Simon Cooksey 0:fb7af294d5d9 368 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
Simon Cooksey 0:fb7af294d5d9 369 0x18, 0x19, 0x1a, 0x1b
Simon Cooksey 0:fb7af294d5d9 370 };
Simon Cooksey 0:fb7af294d5d9 371
Simon Cooksey 0:fb7af294d5d9 372 static const unsigned char ad[] = {
Simon Cooksey 0:fb7af294d5d9 373 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
Simon Cooksey 0:fb7af294d5d9 374 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
Simon Cooksey 0:fb7af294d5d9 375 0x10, 0x11, 0x12, 0x13
Simon Cooksey 0:fb7af294d5d9 376 };
Simon Cooksey 0:fb7af294d5d9 377
Simon Cooksey 0:fb7af294d5d9 378 static const unsigned char msg[] = {
Simon Cooksey 0:fb7af294d5d9 379 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
Simon Cooksey 0:fb7af294d5d9 380 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
Simon Cooksey 0:fb7af294d5d9 381 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
Simon Cooksey 0:fb7af294d5d9 382 };
Simon Cooksey 0:fb7af294d5d9 383
Simon Cooksey 0:fb7af294d5d9 384 static const size_t iv_len [NB_TESTS] = { 7, 8, 12 };
Simon Cooksey 0:fb7af294d5d9 385 static const size_t add_len[NB_TESTS] = { 8, 16, 20 };
Simon Cooksey 0:fb7af294d5d9 386 static const size_t msg_len[NB_TESTS] = { 4, 16, 24 };
Simon Cooksey 0:fb7af294d5d9 387 static const size_t tag_len[NB_TESTS] = { 4, 6, 8 };
Simon Cooksey 0:fb7af294d5d9 388
Simon Cooksey 0:fb7af294d5d9 389 static const unsigned char res[NB_TESTS][32] = {
Simon Cooksey 0:fb7af294d5d9 390 { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
Simon Cooksey 0:fb7af294d5d9 391 { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
Simon Cooksey 0:fb7af294d5d9 392 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
Simon Cooksey 0:fb7af294d5d9 393 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
Simon Cooksey 0:fb7af294d5d9 394 { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
Simon Cooksey 0:fb7af294d5d9 395 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
Simon Cooksey 0:fb7af294d5d9 396 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
Simon Cooksey 0:fb7af294d5d9 397 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
Simon Cooksey 0:fb7af294d5d9 398 };
Simon Cooksey 0:fb7af294d5d9 399
Simon Cooksey 0:fb7af294d5d9 400 int mbedtls_ccm_self_test( int verbose )
Simon Cooksey 0:fb7af294d5d9 401 {
Simon Cooksey 0:fb7af294d5d9 402 mbedtls_ccm_context ctx;
Simon Cooksey 0:fb7af294d5d9 403 unsigned char out[32];
Simon Cooksey 0:fb7af294d5d9 404 size_t i;
Simon Cooksey 0:fb7af294d5d9 405 int ret;
Simon Cooksey 0:fb7af294d5d9 406
Simon Cooksey 0:fb7af294d5d9 407 mbedtls_ccm_init( &ctx );
Simon Cooksey 0:fb7af294d5d9 408
Simon Cooksey 0:fb7af294d5d9 409 if( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
Simon Cooksey 0:fb7af294d5d9 410 {
Simon Cooksey 0:fb7af294d5d9 411 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 412 mbedtls_printf( " CCM: setup failed" );
Simon Cooksey 0:fb7af294d5d9 413
Simon Cooksey 0:fb7af294d5d9 414 return( 1 );
Simon Cooksey 0:fb7af294d5d9 415 }
Simon Cooksey 0:fb7af294d5d9 416
Simon Cooksey 0:fb7af294d5d9 417 for( i = 0; i < NB_TESTS; i++ )
Simon Cooksey 0:fb7af294d5d9 418 {
Simon Cooksey 0:fb7af294d5d9 419 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 420 mbedtls_printf( " CCM-AES #%u: ", (unsigned int) i + 1 );
Simon Cooksey 0:fb7af294d5d9 421
Simon Cooksey 0:fb7af294d5d9 422 ret = mbedtls_ccm_encrypt_and_tag( &ctx, msg_len[i],
Simon Cooksey 0:fb7af294d5d9 423 iv, iv_len[i], ad, add_len[i],
Simon Cooksey 0:fb7af294d5d9 424 msg, out,
Simon Cooksey 0:fb7af294d5d9 425 out + msg_len[i], tag_len[i] );
Simon Cooksey 0:fb7af294d5d9 426
Simon Cooksey 0:fb7af294d5d9 427 if( ret != 0 ||
Simon Cooksey 0:fb7af294d5d9 428 memcmp( out, res[i], msg_len[i] + tag_len[i] ) != 0 )
Simon Cooksey 0:fb7af294d5d9 429 {
Simon Cooksey 0:fb7af294d5d9 430 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 431 mbedtls_printf( "failed\n" );
Simon Cooksey 0:fb7af294d5d9 432
Simon Cooksey 0:fb7af294d5d9 433 return( 1 );
Simon Cooksey 0:fb7af294d5d9 434 }
Simon Cooksey 0:fb7af294d5d9 435
Simon Cooksey 0:fb7af294d5d9 436 ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len[i],
Simon Cooksey 0:fb7af294d5d9 437 iv, iv_len[i], ad, add_len[i],
Simon Cooksey 0:fb7af294d5d9 438 res[i], out,
Simon Cooksey 0:fb7af294d5d9 439 res[i] + msg_len[i], tag_len[i] );
Simon Cooksey 0:fb7af294d5d9 440
Simon Cooksey 0:fb7af294d5d9 441 if( ret != 0 ||
Simon Cooksey 0:fb7af294d5d9 442 memcmp( out, msg, msg_len[i] ) != 0 )
Simon Cooksey 0:fb7af294d5d9 443 {
Simon Cooksey 0:fb7af294d5d9 444 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 445 mbedtls_printf( "failed\n" );
Simon Cooksey 0:fb7af294d5d9 446
Simon Cooksey 0:fb7af294d5d9 447 return( 1 );
Simon Cooksey 0:fb7af294d5d9 448 }
Simon Cooksey 0:fb7af294d5d9 449
Simon Cooksey 0:fb7af294d5d9 450 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 451 mbedtls_printf( "passed\n" );
Simon Cooksey 0:fb7af294d5d9 452 }
Simon Cooksey 0:fb7af294d5d9 453
Simon Cooksey 0:fb7af294d5d9 454 mbedtls_ccm_free( &ctx );
Simon Cooksey 0:fb7af294d5d9 455
Simon Cooksey 0:fb7af294d5d9 456 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 457 mbedtls_printf( "\n" );
Simon Cooksey 0:fb7af294d5d9 458
Simon Cooksey 0:fb7af294d5d9 459 return( 0 );
Simon Cooksey 0:fb7af294d5d9 460 }
Simon Cooksey 0:fb7af294d5d9 461
Simon Cooksey 0:fb7af294d5d9 462 #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Simon Cooksey 0:fb7af294d5d9 463
Simon Cooksey 0:fb7af294d5d9 464 #endif /* MBEDTLS_CCM_C */