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