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