mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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