This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
timbeight
Date:
Thu May 19 16:02:10 2016 +0000
Revision:
1:0ddbe2d3319c
Parent:
0:f7c60d3e7b8a
This is my first commit while in the class.

Who changed what in which revision?

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