I added functionality to get the RSSI, BER, and Cell Neighbor for reporting connection issues to M2X

Dependencies:   WncControllerK64F

Committer:
JMF
Date:
Mon Nov 14 21:15:42 2016 +0000
Revision:
16:8a4105d407d3
Parent:
12:0071cb144c7a
updated to ensure it builds with TLS by correcting config defaults.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 12:0071cb144c7a 1 /*
JMF 12:0071cb144c7a 2 * CTR_DRBG implementation based on AES-256 (NIST SP 800-90)
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 * The NIST SP 800-90 DRBGs are described in the following publucation.
JMF 12:0071cb144c7a 23 *
JMF 12:0071cb144c7a 24 * http://csrc.nist.gov/publications/nistpubs/800-90/SP800-90revised_March2007.pdf
JMF 12:0071cb144c7a 25 */
JMF 12:0071cb144c7a 26
JMF 12:0071cb144c7a 27 #if !defined(MBEDTLS_CONFIG_FILE)
JMF 12:0071cb144c7a 28 #include "mbedtls/config.h"
JMF 12:0071cb144c7a 29 #else
JMF 12:0071cb144c7a 30 #include MBEDTLS_CONFIG_FILE
JMF 12:0071cb144c7a 31 #endif
JMF 12:0071cb144c7a 32
JMF 12:0071cb144c7a 33 #if defined(MBEDTLS_CTR_DRBG_C)
JMF 12:0071cb144c7a 34
JMF 12:0071cb144c7a 35 #include "mbedtls/ctr_drbg.h"
JMF 12:0071cb144c7a 36
JMF 12:0071cb144c7a 37 #include <string.h>
JMF 12:0071cb144c7a 38
JMF 12:0071cb144c7a 39 #if defined(MBEDTLS_FS_IO)
JMF 12:0071cb144c7a 40 #include <stdio.h>
JMF 12:0071cb144c7a 41 #endif
JMF 12:0071cb144c7a 42
JMF 12:0071cb144c7a 43 #if defined(MBEDTLS_SELF_TEST)
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 */
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 = v; while( n-- ) *p++ = 0;
JMF 12:0071cb144c7a 55 }
JMF 12:0071cb144c7a 56
JMF 12:0071cb144c7a 57 /*
JMF 12:0071cb144c7a 58 * CTR_DRBG context initialization
JMF 12:0071cb144c7a 59 */
JMF 12:0071cb144c7a 60 void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx )
JMF 12:0071cb144c7a 61 {
JMF 12:0071cb144c7a 62 memset( ctx, 0, sizeof( mbedtls_ctr_drbg_context ) );
JMF 12:0071cb144c7a 63
JMF 12:0071cb144c7a 64 #if defined(MBEDTLS_THREADING_C)
JMF 12:0071cb144c7a 65 mbedtls_mutex_init( &ctx->mutex );
JMF 12:0071cb144c7a 66 #endif
JMF 12:0071cb144c7a 67 }
JMF 12:0071cb144c7a 68
JMF 12:0071cb144c7a 69 /*
JMF 12:0071cb144c7a 70 * Non-public function wrapped by mbedtls_ctr_drbg_seed(). Necessary to allow
JMF 12:0071cb144c7a 71 * NIST tests to succeed (which require known length fixed entropy)
JMF 12:0071cb144c7a 72 */
JMF 12:0071cb144c7a 73 int mbedtls_ctr_drbg_seed_entropy_len(
JMF 12:0071cb144c7a 74 mbedtls_ctr_drbg_context *ctx,
JMF 12:0071cb144c7a 75 int (*f_entropy)(void *, unsigned char *, size_t),
JMF 12:0071cb144c7a 76 void *p_entropy,
JMF 12:0071cb144c7a 77 const unsigned char *custom,
JMF 12:0071cb144c7a 78 size_t len,
JMF 12:0071cb144c7a 79 size_t entropy_len )
JMF 12:0071cb144c7a 80 {
JMF 12:0071cb144c7a 81 int ret;
JMF 12:0071cb144c7a 82 unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
JMF 12:0071cb144c7a 83
JMF 12:0071cb144c7a 84 memset( key, 0, MBEDTLS_CTR_DRBG_KEYSIZE );
JMF 12:0071cb144c7a 85
JMF 12:0071cb144c7a 86 mbedtls_aes_init( &ctx->aes_ctx );
JMF 12:0071cb144c7a 87
JMF 12:0071cb144c7a 88 ctx->f_entropy = f_entropy;
JMF 12:0071cb144c7a 89 ctx->p_entropy = p_entropy;
JMF 12:0071cb144c7a 90
JMF 12:0071cb144c7a 91 ctx->entropy_len = entropy_len;
JMF 12:0071cb144c7a 92 ctx->reseed_interval = MBEDTLS_CTR_DRBG_RESEED_INTERVAL;
JMF 12:0071cb144c7a 93
JMF 12:0071cb144c7a 94 /*
JMF 12:0071cb144c7a 95 * Initialize with an empty key
JMF 12:0071cb144c7a 96 */
JMF 12:0071cb144c7a 97 mbedtls_aes_setkey_enc( &ctx->aes_ctx, key, MBEDTLS_CTR_DRBG_KEYBITS );
JMF 12:0071cb144c7a 98
JMF 12:0071cb144c7a 99 if( ( ret = mbedtls_ctr_drbg_reseed( ctx, custom, len ) ) != 0 )
JMF 12:0071cb144c7a 100 return( ret );
JMF 12:0071cb144c7a 101
JMF 12:0071cb144c7a 102 return( 0 );
JMF 12:0071cb144c7a 103 }
JMF 12:0071cb144c7a 104
JMF 12:0071cb144c7a 105 int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
JMF 12:0071cb144c7a 106 int (*f_entropy)(void *, unsigned char *, size_t),
JMF 12:0071cb144c7a 107 void *p_entropy,
JMF 12:0071cb144c7a 108 const unsigned char *custom,
JMF 12:0071cb144c7a 109 size_t len )
JMF 12:0071cb144c7a 110 {
JMF 12:0071cb144c7a 111 return( mbedtls_ctr_drbg_seed_entropy_len( ctx, f_entropy, p_entropy, custom, len,
JMF 12:0071cb144c7a 112 MBEDTLS_CTR_DRBG_ENTROPY_LEN ) );
JMF 12:0071cb144c7a 113 }
JMF 12:0071cb144c7a 114
JMF 12:0071cb144c7a 115 void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx )
JMF 12:0071cb144c7a 116 {
JMF 12:0071cb144c7a 117 if( ctx == NULL )
JMF 12:0071cb144c7a 118 return;
JMF 12:0071cb144c7a 119
JMF 12:0071cb144c7a 120 #if defined(MBEDTLS_THREADING_C)
JMF 12:0071cb144c7a 121 mbedtls_mutex_free( &ctx->mutex );
JMF 12:0071cb144c7a 122 #endif
JMF 12:0071cb144c7a 123 mbedtls_aes_free( &ctx->aes_ctx );
JMF 12:0071cb144c7a 124 mbedtls_zeroize( ctx, sizeof( mbedtls_ctr_drbg_context ) );
JMF 12:0071cb144c7a 125 }
JMF 12:0071cb144c7a 126
JMF 12:0071cb144c7a 127 void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx, int resistance )
JMF 12:0071cb144c7a 128 {
JMF 12:0071cb144c7a 129 ctx->prediction_resistance = resistance;
JMF 12:0071cb144c7a 130 }
JMF 12:0071cb144c7a 131
JMF 12:0071cb144c7a 132 void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx, size_t len )
JMF 12:0071cb144c7a 133 {
JMF 12:0071cb144c7a 134 ctx->entropy_len = len;
JMF 12:0071cb144c7a 135 }
JMF 12:0071cb144c7a 136
JMF 12:0071cb144c7a 137 void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx, int interval )
JMF 12:0071cb144c7a 138 {
JMF 12:0071cb144c7a 139 ctx->reseed_interval = interval;
JMF 12:0071cb144c7a 140 }
JMF 12:0071cb144c7a 141
JMF 12:0071cb144c7a 142 static int block_cipher_df( unsigned char *output,
JMF 12:0071cb144c7a 143 const unsigned char *data, size_t data_len )
JMF 12:0071cb144c7a 144 {
JMF 12:0071cb144c7a 145 unsigned char buf[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + MBEDTLS_CTR_DRBG_BLOCKSIZE + 16];
JMF 12:0071cb144c7a 146 unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN];
JMF 12:0071cb144c7a 147 unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
JMF 12:0071cb144c7a 148 unsigned char chain[MBEDTLS_CTR_DRBG_BLOCKSIZE];
JMF 12:0071cb144c7a 149 unsigned char *p, *iv;
JMF 12:0071cb144c7a 150 mbedtls_aes_context aes_ctx;
JMF 12:0071cb144c7a 151
JMF 12:0071cb144c7a 152 int i, j;
JMF 12:0071cb144c7a 153 size_t buf_len, use_len;
JMF 12:0071cb144c7a 154
JMF 12:0071cb144c7a 155 if( data_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT )
JMF 12:0071cb144c7a 156 return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
JMF 12:0071cb144c7a 157
JMF 12:0071cb144c7a 158 memset( buf, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + MBEDTLS_CTR_DRBG_BLOCKSIZE + 16 );
JMF 12:0071cb144c7a 159 mbedtls_aes_init( &aes_ctx );
JMF 12:0071cb144c7a 160
JMF 12:0071cb144c7a 161 /*
JMF 12:0071cb144c7a 162 * Construct IV (16 bytes) and S in buffer
JMF 12:0071cb144c7a 163 * IV = Counter (in 32-bits) padded to 16 with zeroes
JMF 12:0071cb144c7a 164 * S = Length input string (in 32-bits) || Length of output (in 32-bits) ||
JMF 12:0071cb144c7a 165 * data || 0x80
JMF 12:0071cb144c7a 166 * (Total is padded to a multiple of 16-bytes with zeroes)
JMF 12:0071cb144c7a 167 */
JMF 12:0071cb144c7a 168 p = buf + MBEDTLS_CTR_DRBG_BLOCKSIZE;
JMF 12:0071cb144c7a 169 *p++ = ( data_len >> 24 ) & 0xff;
JMF 12:0071cb144c7a 170 *p++ = ( data_len >> 16 ) & 0xff;
JMF 12:0071cb144c7a 171 *p++ = ( data_len >> 8 ) & 0xff;
JMF 12:0071cb144c7a 172 *p++ = ( data_len ) & 0xff;
JMF 12:0071cb144c7a 173 p += 3;
JMF 12:0071cb144c7a 174 *p++ = MBEDTLS_CTR_DRBG_SEEDLEN;
JMF 12:0071cb144c7a 175 memcpy( p, data, data_len );
JMF 12:0071cb144c7a 176 p[data_len] = 0x80;
JMF 12:0071cb144c7a 177
JMF 12:0071cb144c7a 178 buf_len = MBEDTLS_CTR_DRBG_BLOCKSIZE + 8 + data_len + 1;
JMF 12:0071cb144c7a 179
JMF 12:0071cb144c7a 180 for( i = 0; i < MBEDTLS_CTR_DRBG_KEYSIZE; i++ )
JMF 12:0071cb144c7a 181 key[i] = i;
JMF 12:0071cb144c7a 182
JMF 12:0071cb144c7a 183 mbedtls_aes_setkey_enc( &aes_ctx, key, MBEDTLS_CTR_DRBG_KEYBITS );
JMF 12:0071cb144c7a 184
JMF 12:0071cb144c7a 185 /*
JMF 12:0071cb144c7a 186 * Reduce data to MBEDTLS_CTR_DRBG_SEEDLEN bytes of data
JMF 12:0071cb144c7a 187 */
JMF 12:0071cb144c7a 188 for( j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE )
JMF 12:0071cb144c7a 189 {
JMF 12:0071cb144c7a 190 p = buf;
JMF 12:0071cb144c7a 191 memset( chain, 0, MBEDTLS_CTR_DRBG_BLOCKSIZE );
JMF 12:0071cb144c7a 192 use_len = buf_len;
JMF 12:0071cb144c7a 193
JMF 12:0071cb144c7a 194 while( use_len > 0 )
JMF 12:0071cb144c7a 195 {
JMF 12:0071cb144c7a 196 for( i = 0; i < MBEDTLS_CTR_DRBG_BLOCKSIZE; i++ )
JMF 12:0071cb144c7a 197 chain[i] ^= p[i];
JMF 12:0071cb144c7a 198 p += MBEDTLS_CTR_DRBG_BLOCKSIZE;
JMF 12:0071cb144c7a 199 use_len -= ( use_len >= MBEDTLS_CTR_DRBG_BLOCKSIZE ) ?
JMF 12:0071cb144c7a 200 MBEDTLS_CTR_DRBG_BLOCKSIZE : use_len;
JMF 12:0071cb144c7a 201
JMF 12:0071cb144c7a 202 mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, chain, chain );
JMF 12:0071cb144c7a 203 }
JMF 12:0071cb144c7a 204
JMF 12:0071cb144c7a 205 memcpy( tmp + j, chain, MBEDTLS_CTR_DRBG_BLOCKSIZE );
JMF 12:0071cb144c7a 206
JMF 12:0071cb144c7a 207 /*
JMF 12:0071cb144c7a 208 * Update IV
JMF 12:0071cb144c7a 209 */
JMF 12:0071cb144c7a 210 buf[3]++;
JMF 12:0071cb144c7a 211 }
JMF 12:0071cb144c7a 212
JMF 12:0071cb144c7a 213 /*
JMF 12:0071cb144c7a 214 * Do final encryption with reduced data
JMF 12:0071cb144c7a 215 */
JMF 12:0071cb144c7a 216 mbedtls_aes_setkey_enc( &aes_ctx, tmp, MBEDTLS_CTR_DRBG_KEYBITS );
JMF 12:0071cb144c7a 217 iv = tmp + MBEDTLS_CTR_DRBG_KEYSIZE;
JMF 12:0071cb144c7a 218 p = output;
JMF 12:0071cb144c7a 219
JMF 12:0071cb144c7a 220 for( j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE )
JMF 12:0071cb144c7a 221 {
JMF 12:0071cb144c7a 222 mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
JMF 12:0071cb144c7a 223 memcpy( p, iv, MBEDTLS_CTR_DRBG_BLOCKSIZE );
JMF 12:0071cb144c7a 224 p += MBEDTLS_CTR_DRBG_BLOCKSIZE;
JMF 12:0071cb144c7a 225 }
JMF 12:0071cb144c7a 226
JMF 12:0071cb144c7a 227 mbedtls_aes_free( &aes_ctx );
JMF 12:0071cb144c7a 228
JMF 12:0071cb144c7a 229 return( 0 );
JMF 12:0071cb144c7a 230 }
JMF 12:0071cb144c7a 231
JMF 12:0071cb144c7a 232 static int ctr_drbg_update_internal( mbedtls_ctr_drbg_context *ctx,
JMF 12:0071cb144c7a 233 const unsigned char data[MBEDTLS_CTR_DRBG_SEEDLEN] )
JMF 12:0071cb144c7a 234 {
JMF 12:0071cb144c7a 235 unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN];
JMF 12:0071cb144c7a 236 unsigned char *p = tmp;
JMF 12:0071cb144c7a 237 int i, j;
JMF 12:0071cb144c7a 238
JMF 12:0071cb144c7a 239 memset( tmp, 0, MBEDTLS_CTR_DRBG_SEEDLEN );
JMF 12:0071cb144c7a 240
JMF 12:0071cb144c7a 241 for( j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE )
JMF 12:0071cb144c7a 242 {
JMF 12:0071cb144c7a 243 /*
JMF 12:0071cb144c7a 244 * Increase counter
JMF 12:0071cb144c7a 245 */
JMF 12:0071cb144c7a 246 for( i = MBEDTLS_CTR_DRBG_BLOCKSIZE; i > 0; i-- )
JMF 12:0071cb144c7a 247 if( ++ctx->counter[i - 1] != 0 )
JMF 12:0071cb144c7a 248 break;
JMF 12:0071cb144c7a 249
JMF 12:0071cb144c7a 250 /*
JMF 12:0071cb144c7a 251 * Crypt counter block
JMF 12:0071cb144c7a 252 */
JMF 12:0071cb144c7a 253 mbedtls_aes_crypt_ecb( &ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, ctx->counter, p );
JMF 12:0071cb144c7a 254
JMF 12:0071cb144c7a 255 p += MBEDTLS_CTR_DRBG_BLOCKSIZE;
JMF 12:0071cb144c7a 256 }
JMF 12:0071cb144c7a 257
JMF 12:0071cb144c7a 258 for( i = 0; i < MBEDTLS_CTR_DRBG_SEEDLEN; i++ )
JMF 12:0071cb144c7a 259 tmp[i] ^= data[i];
JMF 12:0071cb144c7a 260
JMF 12:0071cb144c7a 261 /*
JMF 12:0071cb144c7a 262 * Update key and counter
JMF 12:0071cb144c7a 263 */
JMF 12:0071cb144c7a 264 mbedtls_aes_setkey_enc( &ctx->aes_ctx, tmp, MBEDTLS_CTR_DRBG_KEYBITS );
JMF 12:0071cb144c7a 265 memcpy( ctx->counter, tmp + MBEDTLS_CTR_DRBG_KEYSIZE, MBEDTLS_CTR_DRBG_BLOCKSIZE );
JMF 12:0071cb144c7a 266
JMF 12:0071cb144c7a 267 return( 0 );
JMF 12:0071cb144c7a 268 }
JMF 12:0071cb144c7a 269
JMF 12:0071cb144c7a 270 void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
JMF 12:0071cb144c7a 271 const unsigned char *additional, size_t add_len )
JMF 12:0071cb144c7a 272 {
JMF 12:0071cb144c7a 273 unsigned char add_input[MBEDTLS_CTR_DRBG_SEEDLEN];
JMF 12:0071cb144c7a 274
JMF 12:0071cb144c7a 275 if( add_len > 0 )
JMF 12:0071cb144c7a 276 {
JMF 12:0071cb144c7a 277 /* MAX_INPUT would be more logical here, but we have to match
JMF 12:0071cb144c7a 278 * block_cipher_df()'s limits since we can't propagate errors */
JMF 12:0071cb144c7a 279 if( add_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT )
JMF 12:0071cb144c7a 280 add_len = MBEDTLS_CTR_DRBG_MAX_SEED_INPUT;
JMF 12:0071cb144c7a 281
JMF 12:0071cb144c7a 282 block_cipher_df( add_input, additional, add_len );
JMF 12:0071cb144c7a 283 ctr_drbg_update_internal( ctx, add_input );
JMF 12:0071cb144c7a 284 }
JMF 12:0071cb144c7a 285 }
JMF 12:0071cb144c7a 286
JMF 12:0071cb144c7a 287 int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
JMF 12:0071cb144c7a 288 const unsigned char *additional, size_t len )
JMF 12:0071cb144c7a 289 {
JMF 12:0071cb144c7a 290 unsigned char seed[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT];
JMF 12:0071cb144c7a 291 size_t seedlen = 0;
JMF 12:0071cb144c7a 292
JMF 12:0071cb144c7a 293 if( ctx->entropy_len + len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT )
JMF 12:0071cb144c7a 294 return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
JMF 12:0071cb144c7a 295
JMF 12:0071cb144c7a 296 memset( seed, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT );
JMF 12:0071cb144c7a 297
JMF 12:0071cb144c7a 298 /*
JMF 12:0071cb144c7a 299 * Gather entropy_len bytes of entropy to seed state
JMF 12:0071cb144c7a 300 */
JMF 12:0071cb144c7a 301 if( 0 != ctx->f_entropy( ctx->p_entropy, seed,
JMF 12:0071cb144c7a 302 ctx->entropy_len ) )
JMF 12:0071cb144c7a 303 {
JMF 12:0071cb144c7a 304 return( MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED );
JMF 12:0071cb144c7a 305 }
JMF 12:0071cb144c7a 306
JMF 12:0071cb144c7a 307 seedlen += ctx->entropy_len;
JMF 12:0071cb144c7a 308
JMF 12:0071cb144c7a 309 /*
JMF 12:0071cb144c7a 310 * Add additional data
JMF 12:0071cb144c7a 311 */
JMF 12:0071cb144c7a 312 if( additional && len )
JMF 12:0071cb144c7a 313 {
JMF 12:0071cb144c7a 314 memcpy( seed + seedlen, additional, len );
JMF 12:0071cb144c7a 315 seedlen += len;
JMF 12:0071cb144c7a 316 }
JMF 12:0071cb144c7a 317
JMF 12:0071cb144c7a 318 /*
JMF 12:0071cb144c7a 319 * Reduce to 384 bits
JMF 12:0071cb144c7a 320 */
JMF 12:0071cb144c7a 321 block_cipher_df( seed, seed, seedlen );
JMF 12:0071cb144c7a 322
JMF 12:0071cb144c7a 323 /*
JMF 12:0071cb144c7a 324 * Update state
JMF 12:0071cb144c7a 325 */
JMF 12:0071cb144c7a 326 ctr_drbg_update_internal( ctx, seed );
JMF 12:0071cb144c7a 327 ctx->reseed_counter = 1;
JMF 12:0071cb144c7a 328
JMF 12:0071cb144c7a 329 return( 0 );
JMF 12:0071cb144c7a 330 }
JMF 12:0071cb144c7a 331
JMF 12:0071cb144c7a 332 int mbedtls_ctr_drbg_random_with_add( void *p_rng,
JMF 12:0071cb144c7a 333 unsigned char *output, size_t output_len,
JMF 12:0071cb144c7a 334 const unsigned char *additional, size_t add_len )
JMF 12:0071cb144c7a 335 {
JMF 12:0071cb144c7a 336 int ret = 0;
JMF 12:0071cb144c7a 337 mbedtls_ctr_drbg_context *ctx = (mbedtls_ctr_drbg_context *) p_rng;
JMF 12:0071cb144c7a 338 unsigned char add_input[MBEDTLS_CTR_DRBG_SEEDLEN];
JMF 12:0071cb144c7a 339 unsigned char *p = output;
JMF 12:0071cb144c7a 340 unsigned char tmp[MBEDTLS_CTR_DRBG_BLOCKSIZE];
JMF 12:0071cb144c7a 341 int i;
JMF 12:0071cb144c7a 342 size_t use_len;
JMF 12:0071cb144c7a 343
JMF 12:0071cb144c7a 344 if( output_len > MBEDTLS_CTR_DRBG_MAX_REQUEST )
JMF 12:0071cb144c7a 345 return( MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG );
JMF 12:0071cb144c7a 346
JMF 12:0071cb144c7a 347 if( add_len > MBEDTLS_CTR_DRBG_MAX_INPUT )
JMF 12:0071cb144c7a 348 return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
JMF 12:0071cb144c7a 349
JMF 12:0071cb144c7a 350 memset( add_input, 0, MBEDTLS_CTR_DRBG_SEEDLEN );
JMF 12:0071cb144c7a 351
JMF 12:0071cb144c7a 352 if( ctx->reseed_counter > ctx->reseed_interval ||
JMF 12:0071cb144c7a 353 ctx->prediction_resistance )
JMF 12:0071cb144c7a 354 {
JMF 12:0071cb144c7a 355 if( ( ret = mbedtls_ctr_drbg_reseed( ctx, additional, add_len ) ) != 0 )
JMF 12:0071cb144c7a 356 return( ret );
JMF 12:0071cb144c7a 357
JMF 12:0071cb144c7a 358 add_len = 0;
JMF 12:0071cb144c7a 359 }
JMF 12:0071cb144c7a 360
JMF 12:0071cb144c7a 361 if( add_len > 0 )
JMF 12:0071cb144c7a 362 {
JMF 12:0071cb144c7a 363 block_cipher_df( add_input, additional, add_len );
JMF 12:0071cb144c7a 364 ctr_drbg_update_internal( ctx, add_input );
JMF 12:0071cb144c7a 365 }
JMF 12:0071cb144c7a 366
JMF 12:0071cb144c7a 367 while( output_len > 0 )
JMF 12:0071cb144c7a 368 {
JMF 12:0071cb144c7a 369 /*
JMF 12:0071cb144c7a 370 * Increase counter
JMF 12:0071cb144c7a 371 */
JMF 12:0071cb144c7a 372 for( i = MBEDTLS_CTR_DRBG_BLOCKSIZE; i > 0; i-- )
JMF 12:0071cb144c7a 373 if( ++ctx->counter[i - 1] != 0 )
JMF 12:0071cb144c7a 374 break;
JMF 12:0071cb144c7a 375
JMF 12:0071cb144c7a 376 /*
JMF 12:0071cb144c7a 377 * Crypt counter block
JMF 12:0071cb144c7a 378 */
JMF 12:0071cb144c7a 379 mbedtls_aes_crypt_ecb( &ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, ctx->counter, tmp );
JMF 12:0071cb144c7a 380
JMF 12:0071cb144c7a 381 use_len = ( output_len > MBEDTLS_CTR_DRBG_BLOCKSIZE ) ? MBEDTLS_CTR_DRBG_BLOCKSIZE :
JMF 12:0071cb144c7a 382 output_len;
JMF 12:0071cb144c7a 383 /*
JMF 12:0071cb144c7a 384 * Copy random block to destination
JMF 12:0071cb144c7a 385 */
JMF 12:0071cb144c7a 386 memcpy( p, tmp, use_len );
JMF 12:0071cb144c7a 387 p += use_len;
JMF 12:0071cb144c7a 388 output_len -= use_len;
JMF 12:0071cb144c7a 389 }
JMF 12:0071cb144c7a 390
JMF 12:0071cb144c7a 391 ctr_drbg_update_internal( ctx, add_input );
JMF 12:0071cb144c7a 392
JMF 12:0071cb144c7a 393 ctx->reseed_counter++;
JMF 12:0071cb144c7a 394
JMF 12:0071cb144c7a 395 return( 0 );
JMF 12:0071cb144c7a 396 }
JMF 12:0071cb144c7a 397
JMF 12:0071cb144c7a 398 int mbedtls_ctr_drbg_random( void *p_rng, unsigned char *output, size_t output_len )
JMF 12:0071cb144c7a 399 {
JMF 12:0071cb144c7a 400 int ret;
JMF 12:0071cb144c7a 401 mbedtls_ctr_drbg_context *ctx = (mbedtls_ctr_drbg_context *) p_rng;
JMF 12:0071cb144c7a 402
JMF 12:0071cb144c7a 403 #if defined(MBEDTLS_THREADING_C)
JMF 12:0071cb144c7a 404 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
JMF 12:0071cb144c7a 405 return( ret );
JMF 12:0071cb144c7a 406 #endif
JMF 12:0071cb144c7a 407
JMF 12:0071cb144c7a 408 ret = mbedtls_ctr_drbg_random_with_add( ctx, output, output_len, NULL, 0 );
JMF 12:0071cb144c7a 409
JMF 12:0071cb144c7a 410 #if defined(MBEDTLS_THREADING_C)
JMF 12:0071cb144c7a 411 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
JMF 12:0071cb144c7a 412 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
JMF 12:0071cb144c7a 413 #endif
JMF 12:0071cb144c7a 414
JMF 12:0071cb144c7a 415 return( ret );
JMF 12:0071cb144c7a 416 }
JMF 12:0071cb144c7a 417
JMF 12:0071cb144c7a 418 #if defined(MBEDTLS_FS_IO)
JMF 12:0071cb144c7a 419 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path )
JMF 12:0071cb144c7a 420 {
JMF 12:0071cb144c7a 421 int ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR;
JMF 12:0071cb144c7a 422 FILE *f;
JMF 12:0071cb144c7a 423 unsigned char buf[ MBEDTLS_CTR_DRBG_MAX_INPUT ];
JMF 12:0071cb144c7a 424
JMF 12:0071cb144c7a 425 if( ( f = fopen( path, "wb" ) ) == NULL )
JMF 12:0071cb144c7a 426 return( MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR );
JMF 12:0071cb144c7a 427
JMF 12:0071cb144c7a 428 if( ( ret = mbedtls_ctr_drbg_random( ctx, buf, MBEDTLS_CTR_DRBG_MAX_INPUT ) ) != 0 )
JMF 12:0071cb144c7a 429 goto exit;
JMF 12:0071cb144c7a 430
JMF 12:0071cb144c7a 431 if( fwrite( buf, 1, MBEDTLS_CTR_DRBG_MAX_INPUT, f ) != MBEDTLS_CTR_DRBG_MAX_INPUT )
JMF 12:0071cb144c7a 432 {
JMF 12:0071cb144c7a 433 ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR;
JMF 12:0071cb144c7a 434 goto exit;
JMF 12:0071cb144c7a 435 }
JMF 12:0071cb144c7a 436
JMF 12:0071cb144c7a 437 ret = 0;
JMF 12:0071cb144c7a 438
JMF 12:0071cb144c7a 439 exit:
JMF 12:0071cb144c7a 440 fclose( f );
JMF 12:0071cb144c7a 441 return( ret );
JMF 12:0071cb144c7a 442 }
JMF 12:0071cb144c7a 443
JMF 12:0071cb144c7a 444 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path )
JMF 12:0071cb144c7a 445 {
JMF 12:0071cb144c7a 446 FILE *f;
JMF 12:0071cb144c7a 447 size_t n;
JMF 12:0071cb144c7a 448 unsigned char buf[ MBEDTLS_CTR_DRBG_MAX_INPUT ];
JMF 12:0071cb144c7a 449
JMF 12:0071cb144c7a 450 if( ( f = fopen( path, "rb" ) ) == NULL )
JMF 12:0071cb144c7a 451 return( MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR );
JMF 12:0071cb144c7a 452
JMF 12:0071cb144c7a 453 fseek( f, 0, SEEK_END );
JMF 12:0071cb144c7a 454 n = (size_t) ftell( f );
JMF 12:0071cb144c7a 455 fseek( f, 0, SEEK_SET );
JMF 12:0071cb144c7a 456
JMF 12:0071cb144c7a 457 if( n > MBEDTLS_CTR_DRBG_MAX_INPUT )
JMF 12:0071cb144c7a 458 {
JMF 12:0071cb144c7a 459 fclose( f );
JMF 12:0071cb144c7a 460 return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
JMF 12:0071cb144c7a 461 }
JMF 12:0071cb144c7a 462
JMF 12:0071cb144c7a 463 if( fread( buf, 1, n, f ) != n )
JMF 12:0071cb144c7a 464 {
JMF 12:0071cb144c7a 465 fclose( f );
JMF 12:0071cb144c7a 466 return( MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR );
JMF 12:0071cb144c7a 467 }
JMF 12:0071cb144c7a 468
JMF 12:0071cb144c7a 469 fclose( f );
JMF 12:0071cb144c7a 470
JMF 12:0071cb144c7a 471 mbedtls_ctr_drbg_update( ctx, buf, n );
JMF 12:0071cb144c7a 472
JMF 12:0071cb144c7a 473 return( mbedtls_ctr_drbg_write_seed_file( ctx, path ) );
JMF 12:0071cb144c7a 474 }
JMF 12:0071cb144c7a 475 #endif /* MBEDTLS_FS_IO */
JMF 12:0071cb144c7a 476
JMF 12:0071cb144c7a 477 #if defined(MBEDTLS_SELF_TEST)
JMF 12:0071cb144c7a 478
JMF 12:0071cb144c7a 479 static const unsigned char entropy_source_pr[96] =
JMF 12:0071cb144c7a 480 { 0xc1, 0x80, 0x81, 0xa6, 0x5d, 0x44, 0x02, 0x16,
JMF 12:0071cb144c7a 481 0x19, 0xb3, 0xf1, 0x80, 0xb1, 0xc9, 0x20, 0x02,
JMF 12:0071cb144c7a 482 0x6a, 0x54, 0x6f, 0x0c, 0x70, 0x81, 0x49, 0x8b,
JMF 12:0071cb144c7a 483 0x6e, 0xa6, 0x62, 0x52, 0x6d, 0x51, 0xb1, 0xcb,
JMF 12:0071cb144c7a 484 0x58, 0x3b, 0xfa, 0xd5, 0x37, 0x5f, 0xfb, 0xc9,
JMF 12:0071cb144c7a 485 0xff, 0x46, 0xd2, 0x19, 0xc7, 0x22, 0x3e, 0x95,
JMF 12:0071cb144c7a 486 0x45, 0x9d, 0x82, 0xe1, 0xe7, 0x22, 0x9f, 0x63,
JMF 12:0071cb144c7a 487 0x31, 0x69, 0xd2, 0x6b, 0x57, 0x47, 0x4f, 0xa3,
JMF 12:0071cb144c7a 488 0x37, 0xc9, 0x98, 0x1c, 0x0b, 0xfb, 0x91, 0x31,
JMF 12:0071cb144c7a 489 0x4d, 0x55, 0xb9, 0xe9, 0x1c, 0x5a, 0x5e, 0xe4,
JMF 12:0071cb144c7a 490 0x93, 0x92, 0xcf, 0xc5, 0x23, 0x12, 0xd5, 0x56,
JMF 12:0071cb144c7a 491 0x2c, 0x4a, 0x6e, 0xff, 0xdc, 0x10, 0xd0, 0x68 };
JMF 12:0071cb144c7a 492
JMF 12:0071cb144c7a 493 static const unsigned char entropy_source_nopr[64] =
JMF 12:0071cb144c7a 494 { 0x5a, 0x19, 0x4d, 0x5e, 0x2b, 0x31, 0x58, 0x14,
JMF 12:0071cb144c7a 495 0x54, 0xde, 0xf6, 0x75, 0xfb, 0x79, 0x58, 0xfe,
JMF 12:0071cb144c7a 496 0xc7, 0xdb, 0x87, 0x3e, 0x56, 0x89, 0xfc, 0x9d,
JMF 12:0071cb144c7a 497 0x03, 0x21, 0x7c, 0x68, 0xd8, 0x03, 0x38, 0x20,
JMF 12:0071cb144c7a 498 0xf9, 0xe6, 0x5e, 0x04, 0xd8, 0x56, 0xf3, 0xa9,
JMF 12:0071cb144c7a 499 0xc4, 0x4a, 0x4c, 0xbd, 0xc1, 0xd0, 0x08, 0x46,
JMF 12:0071cb144c7a 500 0xf5, 0x98, 0x3d, 0x77, 0x1c, 0x1b, 0x13, 0x7e,
JMF 12:0071cb144c7a 501 0x4e, 0x0f, 0x9d, 0x8e, 0xf4, 0x09, 0xf9, 0x2e };
JMF 12:0071cb144c7a 502
JMF 12:0071cb144c7a 503 static const unsigned char nonce_pers_pr[16] =
JMF 12:0071cb144c7a 504 { 0xd2, 0x54, 0xfc, 0xff, 0x02, 0x1e, 0x69, 0xd2,
JMF 12:0071cb144c7a 505 0x29, 0xc9, 0xcf, 0xad, 0x85, 0xfa, 0x48, 0x6c };
JMF 12:0071cb144c7a 506
JMF 12:0071cb144c7a 507 static const unsigned char nonce_pers_nopr[16] =
JMF 12:0071cb144c7a 508 { 0x1b, 0x54, 0xb8, 0xff, 0x06, 0x42, 0xbf, 0xf5,
JMF 12:0071cb144c7a 509 0x21, 0xf1, 0x5c, 0x1c, 0x0b, 0x66, 0x5f, 0x3f };
JMF 12:0071cb144c7a 510
JMF 12:0071cb144c7a 511 static const unsigned char result_pr[16] =
JMF 12:0071cb144c7a 512 { 0x34, 0x01, 0x16, 0x56, 0xb4, 0x29, 0x00, 0x8f,
JMF 12:0071cb144c7a 513 0x35, 0x63, 0xec, 0xb5, 0xf2, 0x59, 0x07, 0x23 };
JMF 12:0071cb144c7a 514
JMF 12:0071cb144c7a 515 static const unsigned char result_nopr[16] =
JMF 12:0071cb144c7a 516 { 0xa0, 0x54, 0x30, 0x3d, 0x8a, 0x7e, 0xa9, 0x88,
JMF 12:0071cb144c7a 517 0x9d, 0x90, 0x3e, 0x07, 0x7c, 0x6f, 0x21, 0x8f };
JMF 12:0071cb144c7a 518
JMF 12:0071cb144c7a 519 static size_t test_offset;
JMF 12:0071cb144c7a 520 static int ctr_drbg_self_test_entropy( void *data, unsigned char *buf,
JMF 12:0071cb144c7a 521 size_t len )
JMF 12:0071cb144c7a 522 {
JMF 12:0071cb144c7a 523 const unsigned char *p = data;
JMF 12:0071cb144c7a 524 memcpy( buf, p + test_offset, len );
JMF 12:0071cb144c7a 525 test_offset += len;
JMF 12:0071cb144c7a 526 return( 0 );
JMF 12:0071cb144c7a 527 }
JMF 12:0071cb144c7a 528
JMF 12:0071cb144c7a 529 #define CHK( c ) if( (c) != 0 ) \
JMF 12:0071cb144c7a 530 { \
JMF 12:0071cb144c7a 531 if( verbose != 0 ) \
JMF 12:0071cb144c7a 532 mbedtls_printf( "failed\n" ); \
JMF 12:0071cb144c7a 533 return( 1 ); \
JMF 12:0071cb144c7a 534 }
JMF 12:0071cb144c7a 535
JMF 12:0071cb144c7a 536 /*
JMF 12:0071cb144c7a 537 * Checkup routine
JMF 12:0071cb144c7a 538 */
JMF 12:0071cb144c7a 539 int mbedtls_ctr_drbg_self_test( int verbose )
JMF 12:0071cb144c7a 540 {
JMF 12:0071cb144c7a 541 mbedtls_ctr_drbg_context ctx;
JMF 12:0071cb144c7a 542 unsigned char buf[16];
JMF 12:0071cb144c7a 543
JMF 12:0071cb144c7a 544 mbedtls_ctr_drbg_init( &ctx );
JMF 12:0071cb144c7a 545
JMF 12:0071cb144c7a 546 /*
JMF 12:0071cb144c7a 547 * Based on a NIST CTR_DRBG test vector (PR = True)
JMF 12:0071cb144c7a 548 */
JMF 12:0071cb144c7a 549 if( verbose != 0 )
JMF 12:0071cb144c7a 550 mbedtls_printf( " CTR_DRBG (PR = TRUE) : " );
JMF 12:0071cb144c7a 551
JMF 12:0071cb144c7a 552 test_offset = 0;
JMF 12:0071cb144c7a 553 CHK( mbedtls_ctr_drbg_seed_entropy_len( &ctx, ctr_drbg_self_test_entropy,
JMF 12:0071cb144c7a 554 (void *) entropy_source_pr, nonce_pers_pr, 16, 32 ) );
JMF 12:0071cb144c7a 555 mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
JMF 12:0071cb144c7a 556 CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
JMF 12:0071cb144c7a 557 CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
JMF 12:0071cb144c7a 558 CHK( memcmp( buf, result_pr, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
JMF 12:0071cb144c7a 559
JMF 12:0071cb144c7a 560 mbedtls_ctr_drbg_free( &ctx );
JMF 12:0071cb144c7a 561
JMF 12:0071cb144c7a 562 if( verbose != 0 )
JMF 12:0071cb144c7a 563 mbedtls_printf( "passed\n" );
JMF 12:0071cb144c7a 564
JMF 12:0071cb144c7a 565 /*
JMF 12:0071cb144c7a 566 * Based on a NIST CTR_DRBG test vector (PR = FALSE)
JMF 12:0071cb144c7a 567 */
JMF 12:0071cb144c7a 568 if( verbose != 0 )
JMF 12:0071cb144c7a 569 mbedtls_printf( " CTR_DRBG (PR = FALSE): " );
JMF 12:0071cb144c7a 570
JMF 12:0071cb144c7a 571 mbedtls_ctr_drbg_init( &ctx );
JMF 12:0071cb144c7a 572
JMF 12:0071cb144c7a 573 test_offset = 0;
JMF 12:0071cb144c7a 574 CHK( mbedtls_ctr_drbg_seed_entropy_len( &ctx, ctr_drbg_self_test_entropy,
JMF 12:0071cb144c7a 575 (void *) entropy_source_nopr, nonce_pers_nopr, 16, 32 ) );
JMF 12:0071cb144c7a 576 CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) );
JMF 12:0071cb144c7a 577 CHK( mbedtls_ctr_drbg_reseed( &ctx, NULL, 0 ) );
JMF 12:0071cb144c7a 578 CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) );
JMF 12:0071cb144c7a 579 CHK( memcmp( buf, result_nopr, 16 ) );
JMF 12:0071cb144c7a 580
JMF 12:0071cb144c7a 581 mbedtls_ctr_drbg_free( &ctx );
JMF 12:0071cb144c7a 582
JMF 12:0071cb144c7a 583 if( verbose != 0 )
JMF 12:0071cb144c7a 584 mbedtls_printf( "passed\n" );
JMF 12:0071cb144c7a 585
JMF 12:0071cb144c7a 586 if( verbose != 0 )
JMF 12:0071cb144c7a 587 mbedtls_printf( "\n" );
JMF 12:0071cb144c7a 588
JMF 12:0071cb144c7a 589 return( 0 );
JMF 12:0071cb144c7a 590 }
JMF 12:0071cb144c7a 591 #endif /* MBEDTLS_SELF_TEST */
JMF 12:0071cb144c7a 592
JMF 12:0071cb144c7a 593 #endif /* MBEDTLS_CTR_DRBG_C */