This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
maclobdell
Date:
Wed May 18 19:06:32 2016 +0000
Revision:
0:f7c60d3e7b8a
clean version

Who changed what in which revision?

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