mbedtls ported to mbed-classic

Fork of mbedtls by Christopher Haster

Committer:
Brian Daniels
Date:
Thu Apr 07 11:11:18 2016 +0100
Revision:
4:bef26f687287
Parent:
1:24750b9ad5ef
Adding ported selftest test case

Who changed what in which revision?

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