BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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