The WDCInterface is is a drop-in replacement for an EthernetInterface class that allows the user to connect to the Internet with a Wistron NeWeb Corporation (WNC) M14A2A Series data module using the standard network Socket API's. This interface class is used in the AT&T Cellular IoT Starter Kit which is sold by Avnet (http://cloudconnectkits.org/product/att-cellular-iot-starter-kit).

Dependencies:   WncControllerK64F

Dependents:   WNCProximityMqtt Pubnub_ATT_IoT_SK_WNC_sync BluemixDemo BluemixQS ... more

See the WNCInterface README in the Wiki tab for detailed information on this library.

Committer:
JMF
Date:
Fri Mar 24 22:26:23 2017 +0000
Revision:
29:b278b745fb4f
Parent:
12:0071cb144c7a
updated Class name of TCPSocketConnection to WncTCPSocketConnection;

Who changed what in which revision?

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