Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

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