Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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