Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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