mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:137634ff4186 1 /*
ansond 0:137634ff4186 2 * HMAC_DRBG implementation (NIST SP 800-90)
ansond 0:137634ff4186 3 *
ansond 0:137634ff4186 4 * Copyright (C) 2014, ARM Limited, All Rights Reserved
ansond 0:137634ff4186 5 *
ansond 0:137634ff4186 6 * This file is part of mbed TLS (https://tls.mbed.org)
ansond 0:137634ff4186 7 *
ansond 0:137634ff4186 8 * This program is free software; you can redistribute it and/or modify
ansond 0:137634ff4186 9 * it under the terms of the GNU General Public License as published by
ansond 0:137634ff4186 10 * the Free Software Foundation; either version 2 of the License, or
ansond 0:137634ff4186 11 * (at your option) any later version.
ansond 0:137634ff4186 12 *
ansond 0:137634ff4186 13 * This program is distributed in the hope that it will be useful,
ansond 0:137634ff4186 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ansond 0:137634ff4186 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ansond 0:137634ff4186 16 * GNU General Public License for more details.
ansond 0:137634ff4186 17 *
ansond 0:137634ff4186 18 * You should have received a copy of the GNU General Public License along
ansond 0:137634ff4186 19 * with this program; if not, write to the Free Software Foundation, Inc.,
ansond 0:137634ff4186 20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ansond 0:137634ff4186 21 */
ansond 0:137634ff4186 22
ansond 0:137634ff4186 23 /*
ansond 0:137634ff4186 24 * The NIST SP 800-90A DRBGs are described in the following publication.
ansond 0:137634ff4186 25 * http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf
ansond 0:137634ff4186 26 * References below are based on rev. 1 (January 2012).
ansond 0:137634ff4186 27 */
ansond 0:137634ff4186 28
ansond 0:137634ff4186 29 #if !defined(POLARSSL_CONFIG_FILE)
ansond 0:137634ff4186 30 #include "polarssl/config.h"
ansond 0:137634ff4186 31 #else
ansond 0:137634ff4186 32 #include POLARSSL_CONFIG_FILE
ansond 0:137634ff4186 33 #endif
ansond 0:137634ff4186 34
ansond 0:137634ff4186 35 #if defined(POLARSSL_HMAC_DRBG_C)
ansond 0:137634ff4186 36
ansond 0:137634ff4186 37 #include "polarssl/hmac_drbg.h"
ansond 0:137634ff4186 38
ansond 0:137634ff4186 39 #include <string.h>
ansond 0:137634ff4186 40
ansond 0:137634ff4186 41 #if defined(POLARSSL_FS_IO)
ansond 0:137634ff4186 42 #include <stdio.h>
ansond 0:137634ff4186 43 #endif
ansond 0:137634ff4186 44
ansond 0:137634ff4186 45 #if defined(POLARSSL_SELF_TEST)
ansond 0:137634ff4186 46 #if defined(POLARSSL_PLATFORM_C)
ansond 0:137634ff4186 47 #include "polarssl/platform.h"
ansond 0:137634ff4186 48 #else
ansond 0:137634ff4186 49 #include <stdio.h>
ansond 0:137634ff4186 50 #define polarssl_printf printf
ansond 0:137634ff4186 51 #endif /* POLARSSL_SELF_TEST */
ansond 0:137634ff4186 52 #endif /* POLARSSL_PLATFORM_C */
ansond 0:137634ff4186 53
ansond 0:137634ff4186 54 /* Implementation that should never be optimized out by the compiler */
ansond 0:137634ff4186 55 static void polarssl_zeroize( void *v, size_t n ) {
ansond 0:137634ff4186 56 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
ansond 0:137634ff4186 57 }
ansond 0:137634ff4186 58
ansond 0:137634ff4186 59 /*
ansond 0:137634ff4186 60 * HMAC_DRBG update, using optional additional data (10.1.2.2)
ansond 0:137634ff4186 61 */
ansond 0:137634ff4186 62 void hmac_drbg_update( hmac_drbg_context *ctx,
ansond 0:137634ff4186 63 const unsigned char *additional, size_t add_len )
ansond 0:137634ff4186 64 {
ansond 0:137634ff4186 65 size_t md_len = ctx->md_ctx.md_info->size;
ansond 0:137634ff4186 66 unsigned char rounds = ( additional != NULL && add_len != 0 ) ? 2 : 1;
ansond 0:137634ff4186 67 unsigned char sep[1];
ansond 0:137634ff4186 68 unsigned char K[POLARSSL_MD_MAX_SIZE];
ansond 0:137634ff4186 69
ansond 0:137634ff4186 70 for( sep[0] = 0; sep[0] < rounds; sep[0]++ )
ansond 0:137634ff4186 71 {
ansond 0:137634ff4186 72 /* Step 1 or 4 */
ansond 0:137634ff4186 73 md_hmac_reset( &ctx->md_ctx );
ansond 0:137634ff4186 74 md_hmac_update( &ctx->md_ctx, ctx->V, md_len );
ansond 0:137634ff4186 75 md_hmac_update( &ctx->md_ctx, sep, 1 );
ansond 0:137634ff4186 76 if( rounds == 2 )
ansond 0:137634ff4186 77 md_hmac_update( &ctx->md_ctx, additional, add_len );
ansond 0:137634ff4186 78 md_hmac_finish( &ctx->md_ctx, K );
ansond 0:137634ff4186 79
ansond 0:137634ff4186 80 /* Step 2 or 5 */
ansond 0:137634ff4186 81 md_hmac_starts( &ctx->md_ctx, K, md_len );
ansond 0:137634ff4186 82 md_hmac_update( &ctx->md_ctx, ctx->V, md_len );
ansond 0:137634ff4186 83 md_hmac_finish( &ctx->md_ctx, ctx->V );
ansond 0:137634ff4186 84 }
ansond 0:137634ff4186 85 }
ansond 0:137634ff4186 86
ansond 0:137634ff4186 87 /*
ansond 0:137634ff4186 88 * Simplified HMAC_DRBG initialisation (for use with deterministic ECDSA)
ansond 0:137634ff4186 89 */
ansond 0:137634ff4186 90 int hmac_drbg_init_buf( hmac_drbg_context *ctx,
ansond 0:137634ff4186 91 const md_info_t * md_info,
ansond 0:137634ff4186 92 const unsigned char *data, size_t data_len )
ansond 0:137634ff4186 93 {
ansond 0:137634ff4186 94 int ret;
ansond 0:137634ff4186 95
ansond 0:137634ff4186 96 memset( ctx, 0, sizeof( hmac_drbg_context ) );
ansond 0:137634ff4186 97
ansond 0:137634ff4186 98 md_init( &ctx->md_ctx );
ansond 0:137634ff4186 99
ansond 0:137634ff4186 100 if( ( ret = md_init_ctx( &ctx->md_ctx, md_info ) ) != 0 )
ansond 0:137634ff4186 101 return( ret );
ansond 0:137634ff4186 102
ansond 0:137634ff4186 103 /*
ansond 0:137634ff4186 104 * Set initial working state.
ansond 0:137634ff4186 105 * Use the V memory location, which is currently all 0, to initialize the
ansond 0:137634ff4186 106 * MD context with an all-zero key. Then set V to its initial value.
ansond 0:137634ff4186 107 */
ansond 0:137634ff4186 108 md_hmac_starts( &ctx->md_ctx, ctx->V, md_info->size );
ansond 0:137634ff4186 109 memset( ctx->V, 0x01, md_info->size );
ansond 0:137634ff4186 110
ansond 0:137634ff4186 111 hmac_drbg_update( ctx, data, data_len );
ansond 0:137634ff4186 112
ansond 0:137634ff4186 113 return( 0 );
ansond 0:137634ff4186 114 }
ansond 0:137634ff4186 115
ansond 0:137634ff4186 116 /*
ansond 0:137634ff4186 117 * HMAC_DRBG reseeding: 10.1.2.4 (arabic) + 9.2 (Roman)
ansond 0:137634ff4186 118 */
ansond 0:137634ff4186 119 int hmac_drbg_reseed( hmac_drbg_context *ctx,
ansond 0:137634ff4186 120 const unsigned char *additional, size_t len )
ansond 0:137634ff4186 121 {
ansond 0:137634ff4186 122 unsigned char seed[POLARSSL_HMAC_DRBG_MAX_SEED_INPUT];
ansond 0:137634ff4186 123 size_t seedlen;
ansond 0:137634ff4186 124
ansond 0:137634ff4186 125 /* III. Check input length */
ansond 0:137634ff4186 126 if( len > POLARSSL_HMAC_DRBG_MAX_INPUT ||
ansond 0:137634ff4186 127 ctx->entropy_len + len > POLARSSL_HMAC_DRBG_MAX_SEED_INPUT )
ansond 0:137634ff4186 128 {
ansond 0:137634ff4186 129 return( POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG );
ansond 0:137634ff4186 130 }
ansond 0:137634ff4186 131
ansond 0:137634ff4186 132 memset( seed, 0, POLARSSL_HMAC_DRBG_MAX_SEED_INPUT );
ansond 0:137634ff4186 133
ansond 0:137634ff4186 134 /* IV. Gather entropy_len bytes of entropy for the seed */
ansond 0:137634ff4186 135 if( ctx->f_entropy( ctx->p_entropy, seed, ctx->entropy_len ) != 0 )
ansond 0:137634ff4186 136 return( POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED );
ansond 0:137634ff4186 137
ansond 0:137634ff4186 138 seedlen = ctx->entropy_len;
ansond 0:137634ff4186 139
ansond 0:137634ff4186 140 /* 1. Concatenate entropy and additional data if any */
ansond 0:137634ff4186 141 if( additional != NULL && len != 0 )
ansond 0:137634ff4186 142 {
ansond 0:137634ff4186 143 memcpy( seed + seedlen, additional, len );
ansond 0:137634ff4186 144 seedlen += len;
ansond 0:137634ff4186 145 }
ansond 0:137634ff4186 146
ansond 0:137634ff4186 147 /* 2. Update state */
ansond 0:137634ff4186 148 hmac_drbg_update( ctx, seed, seedlen );
ansond 0:137634ff4186 149
ansond 0:137634ff4186 150 /* 3. Reset reseed_counter */
ansond 0:137634ff4186 151 ctx->reseed_counter = 1;
ansond 0:137634ff4186 152
ansond 0:137634ff4186 153 /* 4. Done */
ansond 0:137634ff4186 154 return( 0 );
ansond 0:137634ff4186 155 }
ansond 0:137634ff4186 156
ansond 0:137634ff4186 157 /*
ansond 0:137634ff4186 158 * HMAC_DRBG initialisation (10.1.2.3 + 9.1)
ansond 0:137634ff4186 159 */
ansond 0:137634ff4186 160 int hmac_drbg_init( hmac_drbg_context *ctx,
ansond 0:137634ff4186 161 const md_info_t * md_info,
ansond 0:137634ff4186 162 int (*f_entropy)(void *, unsigned char *, size_t),
ansond 0:137634ff4186 163 void *p_entropy,
ansond 0:137634ff4186 164 const unsigned char *custom,
ansond 0:137634ff4186 165 size_t len )
ansond 0:137634ff4186 166 {
ansond 0:137634ff4186 167 int ret;
ansond 0:137634ff4186 168 size_t entropy_len;
ansond 0:137634ff4186 169
ansond 0:137634ff4186 170 memset( ctx, 0, sizeof( hmac_drbg_context ) );
ansond 0:137634ff4186 171
ansond 0:137634ff4186 172 md_init( &ctx->md_ctx );
ansond 0:137634ff4186 173
ansond 0:137634ff4186 174 if( ( ret = md_init_ctx( &ctx->md_ctx, md_info ) ) != 0 )
ansond 0:137634ff4186 175 return( ret );
ansond 0:137634ff4186 176
ansond 0:137634ff4186 177 /*
ansond 0:137634ff4186 178 * Set initial working state.
ansond 0:137634ff4186 179 * Use the V memory location, which is currently all 0, to initialize the
ansond 0:137634ff4186 180 * MD context with an all-zero key. Then set V to its initial value.
ansond 0:137634ff4186 181 */
ansond 0:137634ff4186 182 md_hmac_starts( &ctx->md_ctx, ctx->V, md_info->size );
ansond 0:137634ff4186 183 memset( ctx->V, 0x01, md_info->size );
ansond 0:137634ff4186 184
ansond 0:137634ff4186 185 ctx->f_entropy = f_entropy;
ansond 0:137634ff4186 186 ctx->p_entropy = p_entropy;
ansond 0:137634ff4186 187
ansond 0:137634ff4186 188 ctx->reseed_interval = POLARSSL_HMAC_DRBG_RESEED_INTERVAL;
ansond 0:137634ff4186 189
ansond 0:137634ff4186 190 /*
ansond 0:137634ff4186 191 * See SP800-57 5.6.1 (p. 65-66) for the security strength provided by
ansond 0:137634ff4186 192 * each hash function, then according to SP800-90A rev1 10.1 table 2,
ansond 0:137634ff4186 193 * min_entropy_len (in bits) is security_strength.
ansond 0:137634ff4186 194 *
ansond 0:137634ff4186 195 * (This also matches the sizes used in the NIST test vectors.)
ansond 0:137634ff4186 196 */
ansond 0:137634ff4186 197 entropy_len = md_info->size <= 20 ? 16 : /* 160-bits hash -> 128 bits */
ansond 0:137634ff4186 198 md_info->size <= 28 ? 24 : /* 224-bits hash -> 192 bits */
ansond 0:137634ff4186 199 32; /* better (256+) -> 256 bits */
ansond 0:137634ff4186 200
ansond 0:137634ff4186 201 /*
ansond 0:137634ff4186 202 * For initialisation, use more entropy to emulate a nonce
ansond 0:137634ff4186 203 * (Again, matches test vectors.)
ansond 0:137634ff4186 204 */
ansond 0:137634ff4186 205 ctx->entropy_len = entropy_len * 3 / 2;
ansond 0:137634ff4186 206
ansond 0:137634ff4186 207 if( ( ret = hmac_drbg_reseed( ctx, custom, len ) ) != 0 )
ansond 0:137634ff4186 208 return( ret );
ansond 0:137634ff4186 209
ansond 0:137634ff4186 210 ctx->entropy_len = entropy_len;
ansond 0:137634ff4186 211
ansond 0:137634ff4186 212 return( 0 );
ansond 0:137634ff4186 213 }
ansond 0:137634ff4186 214
ansond 0:137634ff4186 215 /*
ansond 0:137634ff4186 216 * Set prediction resistance
ansond 0:137634ff4186 217 */
ansond 0:137634ff4186 218 void hmac_drbg_set_prediction_resistance( hmac_drbg_context *ctx,
ansond 0:137634ff4186 219 int resistance )
ansond 0:137634ff4186 220 {
ansond 0:137634ff4186 221 ctx->prediction_resistance = resistance;
ansond 0:137634ff4186 222 }
ansond 0:137634ff4186 223
ansond 0:137634ff4186 224 /*
ansond 0:137634ff4186 225 * Set entropy length grabbed for reseeds
ansond 0:137634ff4186 226 */
ansond 0:137634ff4186 227 void hmac_drbg_set_entropy_len( hmac_drbg_context *ctx, size_t len )
ansond 0:137634ff4186 228 {
ansond 0:137634ff4186 229 ctx->entropy_len = len;
ansond 0:137634ff4186 230 }
ansond 0:137634ff4186 231
ansond 0:137634ff4186 232 /*
ansond 0:137634ff4186 233 * Set reseed interval
ansond 0:137634ff4186 234 */
ansond 0:137634ff4186 235 void hmac_drbg_set_reseed_interval( hmac_drbg_context *ctx, int interval )
ansond 0:137634ff4186 236 {
ansond 0:137634ff4186 237 ctx->reseed_interval = interval;
ansond 0:137634ff4186 238 }
ansond 0:137634ff4186 239
ansond 0:137634ff4186 240 /*
ansond 0:137634ff4186 241 * HMAC_DRBG random function with optional additional data:
ansond 0:137634ff4186 242 * 10.1.2.5 (arabic) + 9.3 (Roman)
ansond 0:137634ff4186 243 */
ansond 0:137634ff4186 244 int hmac_drbg_random_with_add( void *p_rng,
ansond 0:137634ff4186 245 unsigned char *output, size_t out_len,
ansond 0:137634ff4186 246 const unsigned char *additional, size_t add_len )
ansond 0:137634ff4186 247 {
ansond 0:137634ff4186 248 int ret;
ansond 0:137634ff4186 249 hmac_drbg_context *ctx = (hmac_drbg_context *) p_rng;
ansond 0:137634ff4186 250 size_t md_len = md_get_size( ctx->md_ctx.md_info );
ansond 0:137634ff4186 251 size_t left = out_len;
ansond 0:137634ff4186 252 unsigned char *out = output;
ansond 0:137634ff4186 253
ansond 0:137634ff4186 254 /* II. Check request length */
ansond 0:137634ff4186 255 if( out_len > POLARSSL_HMAC_DRBG_MAX_REQUEST )
ansond 0:137634ff4186 256 return( POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG );
ansond 0:137634ff4186 257
ansond 0:137634ff4186 258 /* III. Check input length */
ansond 0:137634ff4186 259 if( add_len > POLARSSL_HMAC_DRBG_MAX_INPUT )
ansond 0:137634ff4186 260 return( POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG );
ansond 0:137634ff4186 261
ansond 0:137634ff4186 262 /* 1. (aka VII and IX) Check reseed counter and PR */
ansond 0:137634ff4186 263 if( ctx->f_entropy != NULL && /* For no-reseeding instances */
ansond 0:137634ff4186 264 ( ctx->prediction_resistance == POLARSSL_HMAC_DRBG_PR_ON ||
ansond 0:137634ff4186 265 ctx->reseed_counter > ctx->reseed_interval ) )
ansond 0:137634ff4186 266 {
ansond 0:137634ff4186 267 if( ( ret = hmac_drbg_reseed( ctx, additional, add_len ) ) != 0 )
ansond 0:137634ff4186 268 return( ret );
ansond 0:137634ff4186 269
ansond 0:137634ff4186 270 add_len = 0; /* VII.4 */
ansond 0:137634ff4186 271 }
ansond 0:137634ff4186 272
ansond 0:137634ff4186 273 /* 2. Use additional data if any */
ansond 0:137634ff4186 274 if( additional != NULL && add_len != 0 )
ansond 0:137634ff4186 275 hmac_drbg_update( ctx, additional, add_len );
ansond 0:137634ff4186 276
ansond 0:137634ff4186 277 /* 3, 4, 5. Generate bytes */
ansond 0:137634ff4186 278 while( left != 0 )
ansond 0:137634ff4186 279 {
ansond 0:137634ff4186 280 size_t use_len = left > md_len ? md_len : left;
ansond 0:137634ff4186 281
ansond 0:137634ff4186 282 md_hmac_reset( &ctx->md_ctx );
ansond 0:137634ff4186 283 md_hmac_update( &ctx->md_ctx, ctx->V, md_len );
ansond 0:137634ff4186 284 md_hmac_finish( &ctx->md_ctx, ctx->V );
ansond 0:137634ff4186 285
ansond 0:137634ff4186 286 memcpy( out, ctx->V, use_len );
ansond 0:137634ff4186 287 out += use_len;
ansond 0:137634ff4186 288 left -= use_len;
ansond 0:137634ff4186 289 }
ansond 0:137634ff4186 290
ansond 0:137634ff4186 291 /* 6. Update */
ansond 0:137634ff4186 292 hmac_drbg_update( ctx, additional, add_len );
ansond 0:137634ff4186 293
ansond 0:137634ff4186 294 /* 7. Update reseed counter */
ansond 0:137634ff4186 295 ctx->reseed_counter++;
ansond 0:137634ff4186 296
ansond 0:137634ff4186 297 /* 8. Done */
ansond 0:137634ff4186 298 return( 0 );
ansond 0:137634ff4186 299 }
ansond 0:137634ff4186 300
ansond 0:137634ff4186 301 /*
ansond 0:137634ff4186 302 * HMAC_DRBG random function
ansond 0:137634ff4186 303 */
ansond 0:137634ff4186 304 int hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len )
ansond 0:137634ff4186 305 {
ansond 0:137634ff4186 306 return( hmac_drbg_random_with_add( p_rng, output, out_len, NULL, 0 ) );
ansond 0:137634ff4186 307 }
ansond 0:137634ff4186 308
ansond 0:137634ff4186 309 /*
ansond 0:137634ff4186 310 * Free an HMAC_DRBG context
ansond 0:137634ff4186 311 */
ansond 0:137634ff4186 312 void hmac_drbg_free( hmac_drbg_context *ctx )
ansond 0:137634ff4186 313 {
ansond 0:137634ff4186 314 if( ctx == NULL )
ansond 0:137634ff4186 315 return;
ansond 0:137634ff4186 316
ansond 0:137634ff4186 317 md_free( &ctx->md_ctx );
ansond 0:137634ff4186 318
ansond 0:137634ff4186 319 polarssl_zeroize( ctx, sizeof( hmac_drbg_context ) );
ansond 0:137634ff4186 320 }
ansond 0:137634ff4186 321
ansond 0:137634ff4186 322 #if defined(POLARSSL_FS_IO)
ansond 0:137634ff4186 323 int hmac_drbg_write_seed_file( hmac_drbg_context *ctx, const char *path )
ansond 0:137634ff4186 324 {
ansond 0:137634ff4186 325 int ret;
ansond 0:137634ff4186 326 FILE *f;
ansond 0:137634ff4186 327 unsigned char buf[ POLARSSL_HMAC_DRBG_MAX_INPUT ];
ansond 0:137634ff4186 328
ansond 0:137634ff4186 329 if( ( f = fopen( path, "wb" ) ) == NULL )
ansond 0:137634ff4186 330 return( POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR );
ansond 0:137634ff4186 331
ansond 0:137634ff4186 332 if( ( ret = hmac_drbg_random( ctx, buf, sizeof( buf ) ) ) != 0 )
ansond 0:137634ff4186 333 goto exit;
ansond 0:137634ff4186 334
ansond 0:137634ff4186 335 if( fwrite( buf, 1, sizeof( buf ), f ) != sizeof( buf ) )
ansond 0:137634ff4186 336 {
ansond 0:137634ff4186 337 ret = POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR;
ansond 0:137634ff4186 338 goto exit;
ansond 0:137634ff4186 339 }
ansond 0:137634ff4186 340
ansond 0:137634ff4186 341 ret = 0;
ansond 0:137634ff4186 342
ansond 0:137634ff4186 343 exit:
ansond 0:137634ff4186 344 fclose( f );
ansond 0:137634ff4186 345 return( ret );
ansond 0:137634ff4186 346 }
ansond 0:137634ff4186 347
ansond 0:137634ff4186 348 int hmac_drbg_update_seed_file( hmac_drbg_context *ctx, const char *path )
ansond 0:137634ff4186 349 {
ansond 0:137634ff4186 350 FILE *f;
ansond 0:137634ff4186 351 size_t n;
ansond 0:137634ff4186 352 unsigned char buf[ POLARSSL_HMAC_DRBG_MAX_INPUT ];
ansond 0:137634ff4186 353
ansond 0:137634ff4186 354 if( ( f = fopen( path, "rb" ) ) == NULL )
ansond 0:137634ff4186 355 return( POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR );
ansond 0:137634ff4186 356
ansond 0:137634ff4186 357 fseek( f, 0, SEEK_END );
ansond 0:137634ff4186 358 n = (size_t) ftell( f );
ansond 0:137634ff4186 359 fseek( f, 0, SEEK_SET );
ansond 0:137634ff4186 360
ansond 0:137634ff4186 361 if( n > POLARSSL_HMAC_DRBG_MAX_INPUT )
ansond 0:137634ff4186 362 {
ansond 0:137634ff4186 363 fclose( f );
ansond 0:137634ff4186 364 return( POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG );
ansond 0:137634ff4186 365 }
ansond 0:137634ff4186 366
ansond 0:137634ff4186 367 if( fread( buf, 1, n, f ) != n )
ansond 0:137634ff4186 368 {
ansond 0:137634ff4186 369 fclose( f );
ansond 0:137634ff4186 370 return( POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR );
ansond 0:137634ff4186 371 }
ansond 0:137634ff4186 372
ansond 0:137634ff4186 373 fclose( f );
ansond 0:137634ff4186 374
ansond 0:137634ff4186 375 hmac_drbg_update( ctx, buf, n );
ansond 0:137634ff4186 376
ansond 0:137634ff4186 377 return( hmac_drbg_write_seed_file( ctx, path ) );
ansond 0:137634ff4186 378 }
ansond 0:137634ff4186 379 #endif /* POLARSSL_FS_IO */
ansond 0:137634ff4186 380
ansond 0:137634ff4186 381
ansond 0:137634ff4186 382 #if defined(POLARSSL_SELF_TEST)
ansond 0:137634ff4186 383
ansond 0:137634ff4186 384 #if !defined(POLARSSL_SHA1_C)
ansond 0:137634ff4186 385 /* Dummy checkup routine */
ansond 0:137634ff4186 386 int hmac_drbg_self_test( int verbose )
ansond 0:137634ff4186 387 {
ansond 0:137634ff4186 388
ansond 0:137634ff4186 389 if( verbose != 0 )
ansond 0:137634ff4186 390 polarssl_printf( "\n" );
ansond 0:137634ff4186 391
ansond 0:137634ff4186 392 return( 0 );
ansond 0:137634ff4186 393 }
ansond 0:137634ff4186 394 #else
ansond 0:137634ff4186 395
ansond 0:137634ff4186 396 #define OUTPUT_LEN 80
ansond 0:137634ff4186 397
ansond 0:137634ff4186 398 /* From a NIST PR=true test vector */
ansond 0:137634ff4186 399 static const unsigned char entropy_pr[] = {
ansond 0:137634ff4186 400 0xa0, 0xc9, 0xab, 0x58, 0xf1, 0xe2, 0xe5, 0xa4, 0xde, 0x3e, 0xbd, 0x4f,
ansond 0:137634ff4186 401 0xf7, 0x3e, 0x9c, 0x5b, 0x64, 0xef, 0xd8, 0xca, 0x02, 0x8c, 0xf8, 0x11,
ansond 0:137634ff4186 402 0x48, 0xa5, 0x84, 0xfe, 0x69, 0xab, 0x5a, 0xee, 0x42, 0xaa, 0x4d, 0x42,
ansond 0:137634ff4186 403 0x17, 0x60, 0x99, 0xd4, 0x5e, 0x13, 0x97, 0xdc, 0x40, 0x4d, 0x86, 0xa3,
ansond 0:137634ff4186 404 0x7b, 0xf5, 0x59, 0x54, 0x75, 0x69, 0x51, 0xe4 };
ansond 0:137634ff4186 405 static const unsigned char result_pr[OUTPUT_LEN] = {
ansond 0:137634ff4186 406 0x9a, 0x00, 0xa2, 0xd0, 0x0e, 0xd5, 0x9b, 0xfe, 0x31, 0xec, 0xb1, 0x39,
ansond 0:137634ff4186 407 0x9b, 0x60, 0x81, 0x48, 0xd1, 0x96, 0x9d, 0x25, 0x0d, 0x3c, 0x1e, 0x94,
ansond 0:137634ff4186 408 0x10, 0x10, 0x98, 0x12, 0x93, 0x25, 0xca, 0xb8, 0xfc, 0xcc, 0x2d, 0x54,
ansond 0:137634ff4186 409 0x73, 0x19, 0x70, 0xc0, 0x10, 0x7a, 0xa4, 0x89, 0x25, 0x19, 0x95, 0x5e,
ansond 0:137634ff4186 410 0x4b, 0xc6, 0x00, 0x1d, 0x7f, 0x4e, 0x6a, 0x2b, 0xf8, 0xa3, 0x01, 0xab,
ansond 0:137634ff4186 411 0x46, 0x05, 0x5c, 0x09, 0xa6, 0x71, 0x88, 0xf1, 0xa7, 0x40, 0xee, 0xf3,
ansond 0:137634ff4186 412 0xe1, 0x5c, 0x02, 0x9b, 0x44, 0xaf, 0x03, 0x44 };
ansond 0:137634ff4186 413
ansond 0:137634ff4186 414 /* From a NIST PR=false test vector */
ansond 0:137634ff4186 415 static const unsigned char entropy_nopr[] = {
ansond 0:137634ff4186 416 0x79, 0x34, 0x9b, 0xbf, 0x7c, 0xdd, 0xa5, 0x79, 0x95, 0x57, 0x86, 0x66,
ansond 0:137634ff4186 417 0x21, 0xc9, 0x13, 0x83, 0x11, 0x46, 0x73, 0x3a, 0xbf, 0x8c, 0x35, 0xc8,
ansond 0:137634ff4186 418 0xc7, 0x21, 0x5b, 0x5b, 0x96, 0xc4, 0x8e, 0x9b, 0x33, 0x8c, 0x74, 0xe3,
ansond 0:137634ff4186 419 0xe9, 0x9d, 0xfe, 0xdf };
ansond 0:137634ff4186 420 static const unsigned char result_nopr[OUTPUT_LEN] = {
ansond 0:137634ff4186 421 0xc6, 0xa1, 0x6a, 0xb8, 0xd4, 0x20, 0x70, 0x6f, 0x0f, 0x34, 0xab, 0x7f,
ansond 0:137634ff4186 422 0xec, 0x5a, 0xdc, 0xa9, 0xd8, 0xca, 0x3a, 0x13, 0x3e, 0x15, 0x9c, 0xa6,
ansond 0:137634ff4186 423 0xac, 0x43, 0xc6, 0xf8, 0xa2, 0xbe, 0x22, 0x83, 0x4a, 0x4c, 0x0a, 0x0a,
ansond 0:137634ff4186 424 0xff, 0xb1, 0x0d, 0x71, 0x94, 0xf1, 0xc1, 0xa5, 0xcf, 0x73, 0x22, 0xec,
ansond 0:137634ff4186 425 0x1a, 0xe0, 0x96, 0x4e, 0xd4, 0xbf, 0x12, 0x27, 0x46, 0xe0, 0x87, 0xfd,
ansond 0:137634ff4186 426 0xb5, 0xb3, 0xe9, 0x1b, 0x34, 0x93, 0xd5, 0xbb, 0x98, 0xfa, 0xed, 0x49,
ansond 0:137634ff4186 427 0xe8, 0x5f, 0x13, 0x0f, 0xc8, 0xa4, 0x59, 0xb7 };
ansond 0:137634ff4186 428
ansond 0:137634ff4186 429 /* "Entropy" from buffer */
ansond 0:137634ff4186 430 static size_t test_offset;
ansond 0:137634ff4186 431 static int hmac_drbg_self_test_entropy( void *data,
ansond 0:137634ff4186 432 unsigned char *buf, size_t len )
ansond 0:137634ff4186 433 {
ansond 0:137634ff4186 434 const unsigned char *p = data;
ansond 0:137634ff4186 435 memcpy( buf, p + test_offset, len );
ansond 0:137634ff4186 436 test_offset += len;
ansond 0:137634ff4186 437 return( 0 );
ansond 0:137634ff4186 438 }
ansond 0:137634ff4186 439
ansond 0:137634ff4186 440 #define CHK( c ) if( (c) != 0 ) \
ansond 0:137634ff4186 441 { \
ansond 0:137634ff4186 442 if( verbose != 0 ) \
ansond 0:137634ff4186 443 polarssl_printf( "failed\n" ); \
ansond 0:137634ff4186 444 return( 1 ); \
ansond 0:137634ff4186 445 }
ansond 0:137634ff4186 446
ansond 0:137634ff4186 447 /*
ansond 0:137634ff4186 448 * Checkup routine for HMAC_DRBG with SHA-1
ansond 0:137634ff4186 449 */
ansond 0:137634ff4186 450 int hmac_drbg_self_test( int verbose )
ansond 0:137634ff4186 451 {
ansond 0:137634ff4186 452 hmac_drbg_context ctx;
ansond 0:137634ff4186 453 unsigned char buf[OUTPUT_LEN];
ansond 0:137634ff4186 454 const md_info_t *md_info = md_info_from_type( POLARSSL_MD_SHA1 );
ansond 0:137634ff4186 455
ansond 0:137634ff4186 456 /*
ansond 0:137634ff4186 457 * PR = True
ansond 0:137634ff4186 458 */
ansond 0:137634ff4186 459 if( verbose != 0 )
ansond 0:137634ff4186 460 polarssl_printf( " HMAC_DRBG (PR = True) : " );
ansond 0:137634ff4186 461
ansond 0:137634ff4186 462 test_offset = 0;
ansond 0:137634ff4186 463 CHK( hmac_drbg_init( &ctx, md_info,
ansond 0:137634ff4186 464 hmac_drbg_self_test_entropy, (void *) entropy_pr,
ansond 0:137634ff4186 465 NULL, 0 ) );
ansond 0:137634ff4186 466 hmac_drbg_set_prediction_resistance( &ctx, POLARSSL_HMAC_DRBG_PR_ON );
ansond 0:137634ff4186 467 CHK( hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
ansond 0:137634ff4186 468 CHK( hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
ansond 0:137634ff4186 469 CHK( memcmp( buf, result_pr, OUTPUT_LEN ) );
ansond 0:137634ff4186 470 hmac_drbg_free( &ctx );
ansond 0:137634ff4186 471
ansond 0:137634ff4186 472 if( verbose != 0 )
ansond 0:137634ff4186 473 polarssl_printf( "passed\n" );
ansond 0:137634ff4186 474
ansond 0:137634ff4186 475 /*
ansond 0:137634ff4186 476 * PR = False
ansond 0:137634ff4186 477 */
ansond 0:137634ff4186 478 if( verbose != 0 )
ansond 0:137634ff4186 479 polarssl_printf( " HMAC_DRBG (PR = False) : " );
ansond 0:137634ff4186 480
ansond 0:137634ff4186 481 test_offset = 0;
ansond 0:137634ff4186 482 CHK( hmac_drbg_init( &ctx, md_info,
ansond 0:137634ff4186 483 hmac_drbg_self_test_entropy, (void *) entropy_nopr,
ansond 0:137634ff4186 484 NULL, 0 ) );
ansond 0:137634ff4186 485 CHK( hmac_drbg_reseed( &ctx, NULL, 0 ) );
ansond 0:137634ff4186 486 CHK( hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
ansond 0:137634ff4186 487 CHK( hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
ansond 0:137634ff4186 488 CHK( memcmp( buf, result_nopr, OUTPUT_LEN ) );
ansond 0:137634ff4186 489 hmac_drbg_free( &ctx );
ansond 0:137634ff4186 490
ansond 0:137634ff4186 491 if( verbose != 0 )
ansond 0:137634ff4186 492 polarssl_printf( "passed\n" );
ansond 0:137634ff4186 493
ansond 0:137634ff4186 494 if( verbose != 0 )
ansond 0:137634ff4186 495 polarssl_printf( "\n" );
ansond 0:137634ff4186 496
ansond 0:137634ff4186 497 return( 0 );
ansond 0:137634ff4186 498 }
ansond 0:137634ff4186 499 #endif /* POLARSSL_SHA1_C */
ansond 0:137634ff4186 500 #endif /* POLARSSL_SELF_TEST */
ansond 0:137634ff4186 501
ansond 0:137634ff4186 502 #endif /* POLARSSL_HMAC_DRBG_C */
ansond 0:137634ff4186 503