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