mbed TLS upgraded to 2.6.0

Fork of mbedtls by Mark Radbourne

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Child:
1:9ebc941037d5
Initial commit

Who changed what in which revision?

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