mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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