mbed client on ethernet with LWIP
Dependencies: mbed Socket lwip-eth lwip-sys lwip
Fork of mbed-client-classic-example-lwip by
mbedtls/source/entropy.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 | * Entropy accumulator implementation |
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 | #if !defined(MBEDTLS_CONFIG_FILE) |
mbedAustin | 11:cada08fc8a70 | 23 | #include "mbedtls/config.h" |
mbedAustin | 11:cada08fc8a70 | 24 | #else |
mbedAustin | 11:cada08fc8a70 | 25 | #include MBEDTLS_CONFIG_FILE |
mbedAustin | 11:cada08fc8a70 | 26 | #endif |
mbedAustin | 11:cada08fc8a70 | 27 | |
mbedAustin | 11:cada08fc8a70 | 28 | #if defined(MBEDTLS_ENTROPY_C) |
mbedAustin | 11:cada08fc8a70 | 29 | |
mbedAustin | 11:cada08fc8a70 | 30 | #include "mbedtls/entropy.h" |
mbedAustin | 11:cada08fc8a70 | 31 | #include "mbedtls/entropy_poll.h" |
mbedAustin | 11:cada08fc8a70 | 32 | |
mbedAustin | 11:cada08fc8a70 | 33 | #include <string.h> |
mbedAustin | 11:cada08fc8a70 | 34 | |
mbedAustin | 11:cada08fc8a70 | 35 | #if defined(MBEDTLS_FS_IO) |
mbedAustin | 11:cada08fc8a70 | 36 | #include <stdio.h> |
mbedAustin | 11:cada08fc8a70 | 37 | #endif |
mbedAustin | 11:cada08fc8a70 | 38 | |
mbedAustin | 11:cada08fc8a70 | 39 | #if defined(MBEDTLS_SELF_TEST) |
mbedAustin | 11:cada08fc8a70 | 40 | #if defined(MBEDTLS_PLATFORM_C) |
mbedAustin | 11:cada08fc8a70 | 41 | #include "mbedtls/platform.h" |
mbedAustin | 11:cada08fc8a70 | 42 | #else |
mbedAustin | 11:cada08fc8a70 | 43 | #include <stdio.h> |
mbedAustin | 11:cada08fc8a70 | 44 | #define mbedtls_printf printf |
mbedAustin | 11:cada08fc8a70 | 45 | #endif /* MBEDTLS_PLATFORM_C */ |
mbedAustin | 11:cada08fc8a70 | 46 | #endif /* MBEDTLS_SELF_TEST */ |
mbedAustin | 11:cada08fc8a70 | 47 | |
mbedAustin | 11:cada08fc8a70 | 48 | #if defined(MBEDTLS_HAVEGE_C) |
mbedAustin | 11:cada08fc8a70 | 49 | #include "mbedtls/havege.h" |
mbedAustin | 11:cada08fc8a70 | 50 | #endif |
mbedAustin | 11:cada08fc8a70 | 51 | |
mbedAustin | 11:cada08fc8a70 | 52 | /* Implementation that should never be optimized out by the compiler */ |
mbedAustin | 11:cada08fc8a70 | 53 | static void mbedtls_zeroize( void *v, size_t n ) { |
mbedAustin | 11:cada08fc8a70 | 54 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
mbedAustin | 11:cada08fc8a70 | 55 | } |
mbedAustin | 11:cada08fc8a70 | 56 | |
mbedAustin | 11:cada08fc8a70 | 57 | #define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */ |
mbedAustin | 11:cada08fc8a70 | 58 | |
mbedAustin | 11:cada08fc8a70 | 59 | void mbedtls_entropy_init( mbedtls_entropy_context *ctx ) |
mbedAustin | 11:cada08fc8a70 | 60 | { |
mbedAustin | 11:cada08fc8a70 | 61 | memset( ctx, 0, sizeof(mbedtls_entropy_context) ); |
mbedAustin | 11:cada08fc8a70 | 62 | |
mbedAustin | 11:cada08fc8a70 | 63 | #if defined(MBEDTLS_THREADING_C) |
mbedAustin | 11:cada08fc8a70 | 64 | mbedtls_mutex_init( &ctx->mutex ); |
mbedAustin | 11:cada08fc8a70 | 65 | #endif |
mbedAustin | 11:cada08fc8a70 | 66 | |
mbedAustin | 11:cada08fc8a70 | 67 | #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) |
mbedAustin | 11:cada08fc8a70 | 68 | mbedtls_sha512_starts( &ctx->accumulator, 0 ); |
mbedAustin | 11:cada08fc8a70 | 69 | #else |
mbedAustin | 11:cada08fc8a70 | 70 | mbedtls_sha256_starts( &ctx->accumulator, 0 ); |
mbedAustin | 11:cada08fc8a70 | 71 | #endif |
mbedAustin | 11:cada08fc8a70 | 72 | #if defined(MBEDTLS_HAVEGE_C) |
mbedAustin | 11:cada08fc8a70 | 73 | mbedtls_havege_init( &ctx->havege_data ); |
mbedAustin | 11:cada08fc8a70 | 74 | #endif |
mbedAustin | 11:cada08fc8a70 | 75 | |
mbedAustin | 11:cada08fc8a70 | 76 | #if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
mbedAustin | 11:cada08fc8a70 | 77 | #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY) |
mbedAustin | 11:cada08fc8a70 | 78 | mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL, |
mbedAustin | 11:cada08fc8a70 | 79 | MBEDTLS_ENTROPY_MIN_PLATFORM, |
mbedAustin | 11:cada08fc8a70 | 80 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
mbedAustin | 11:cada08fc8a70 | 81 | #endif |
mbedAustin | 11:cada08fc8a70 | 82 | #if defined(MBEDTLS_TIMING_C) |
mbedAustin | 11:cada08fc8a70 | 83 | mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL, |
mbedAustin | 11:cada08fc8a70 | 84 | MBEDTLS_ENTROPY_MIN_HARDCLOCK, |
mbedAustin | 11:cada08fc8a70 | 85 | MBEDTLS_ENTROPY_SOURCE_WEAK ); |
mbedAustin | 11:cada08fc8a70 | 86 | #endif |
mbedAustin | 11:cada08fc8a70 | 87 | #if defined(MBEDTLS_HAVEGE_C) |
mbedAustin | 11:cada08fc8a70 | 88 | mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data, |
mbedAustin | 11:cada08fc8a70 | 89 | MBEDTLS_ENTROPY_MIN_HAVEGE, |
mbedAustin | 11:cada08fc8a70 | 90 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
mbedAustin | 11:cada08fc8a70 | 91 | #endif |
mbedAustin | 11:cada08fc8a70 | 92 | #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT) |
mbedAustin | 11:cada08fc8a70 | 93 | mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL, |
mbedAustin | 11:cada08fc8a70 | 94 | MBEDTLS_ENTROPY_MIN_HARDWARE, |
mbedAustin | 11:cada08fc8a70 | 95 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
mbedAustin | 11:cada08fc8a70 | 96 | #endif |
mbedAustin | 11:cada08fc8a70 | 97 | #endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */ |
mbedAustin | 11:cada08fc8a70 | 98 | } |
mbedAustin | 11:cada08fc8a70 | 99 | |
mbedAustin | 11:cada08fc8a70 | 100 | void mbedtls_entropy_free( mbedtls_entropy_context *ctx ) |
mbedAustin | 11:cada08fc8a70 | 101 | { |
mbedAustin | 11:cada08fc8a70 | 102 | #if defined(MBEDTLS_HAVEGE_C) |
mbedAustin | 11:cada08fc8a70 | 103 | mbedtls_havege_free( &ctx->havege_data ); |
mbedAustin | 11:cada08fc8a70 | 104 | #endif |
mbedAustin | 11:cada08fc8a70 | 105 | #if defined(MBEDTLS_THREADING_C) |
mbedAustin | 11:cada08fc8a70 | 106 | mbedtls_mutex_free( &ctx->mutex ); |
mbedAustin | 11:cada08fc8a70 | 107 | #endif |
mbedAustin | 11:cada08fc8a70 | 108 | mbedtls_zeroize( ctx, sizeof( mbedtls_entropy_context ) ); |
mbedAustin | 11:cada08fc8a70 | 109 | } |
mbedAustin | 11:cada08fc8a70 | 110 | |
mbedAustin | 11:cada08fc8a70 | 111 | int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx, |
mbedAustin | 11:cada08fc8a70 | 112 | mbedtls_entropy_f_source_ptr f_source, void *p_source, |
mbedAustin | 11:cada08fc8a70 | 113 | size_t threshold, int strong ) |
mbedAustin | 11:cada08fc8a70 | 114 | { |
mbedAustin | 11:cada08fc8a70 | 115 | int index, ret = 0; |
mbedAustin | 11:cada08fc8a70 | 116 | |
mbedAustin | 11:cada08fc8a70 | 117 | #if defined(MBEDTLS_THREADING_C) |
mbedAustin | 11:cada08fc8a70 | 118 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
mbedAustin | 11:cada08fc8a70 | 119 | return( ret ); |
mbedAustin | 11:cada08fc8a70 | 120 | #endif |
mbedAustin | 11:cada08fc8a70 | 121 | |
mbedAustin | 11:cada08fc8a70 | 122 | index = ctx->source_count; |
mbedAustin | 11:cada08fc8a70 | 123 | if( index >= MBEDTLS_ENTROPY_MAX_SOURCES ) |
mbedAustin | 11:cada08fc8a70 | 124 | { |
mbedAustin | 11:cada08fc8a70 | 125 | ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES; |
mbedAustin | 11:cada08fc8a70 | 126 | goto exit; |
mbedAustin | 11:cada08fc8a70 | 127 | } |
mbedAustin | 11:cada08fc8a70 | 128 | |
mbedAustin | 11:cada08fc8a70 | 129 | ctx->source[index].f_source = f_source; |
mbedAustin | 11:cada08fc8a70 | 130 | ctx->source[index].p_source = p_source; |
mbedAustin | 11:cada08fc8a70 | 131 | ctx->source[index].threshold = threshold; |
mbedAustin | 11:cada08fc8a70 | 132 | ctx->source[index].strong = strong; |
mbedAustin | 11:cada08fc8a70 | 133 | |
mbedAustin | 11:cada08fc8a70 | 134 | ctx->source_count++; |
mbedAustin | 11:cada08fc8a70 | 135 | |
mbedAustin | 11:cada08fc8a70 | 136 | exit: |
mbedAustin | 11:cada08fc8a70 | 137 | #if defined(MBEDTLS_THREADING_C) |
mbedAustin | 11:cada08fc8a70 | 138 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
mbedAustin | 11:cada08fc8a70 | 139 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
mbedAustin | 11:cada08fc8a70 | 140 | #endif |
mbedAustin | 11:cada08fc8a70 | 141 | |
mbedAustin | 11:cada08fc8a70 | 142 | return( ret ); |
mbedAustin | 11:cada08fc8a70 | 143 | } |
mbedAustin | 11:cada08fc8a70 | 144 | |
mbedAustin | 11:cada08fc8a70 | 145 | /* |
mbedAustin | 11:cada08fc8a70 | 146 | * Entropy accumulator update |
mbedAustin | 11:cada08fc8a70 | 147 | */ |
mbedAustin | 11:cada08fc8a70 | 148 | static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id, |
mbedAustin | 11:cada08fc8a70 | 149 | const unsigned char *data, size_t len ) |
mbedAustin | 11:cada08fc8a70 | 150 | { |
mbedAustin | 11:cada08fc8a70 | 151 | unsigned char header[2]; |
mbedAustin | 11:cada08fc8a70 | 152 | unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
mbedAustin | 11:cada08fc8a70 | 153 | size_t use_len = len; |
mbedAustin | 11:cada08fc8a70 | 154 | const unsigned char *p = data; |
mbedAustin | 11:cada08fc8a70 | 155 | |
mbedAustin | 11:cada08fc8a70 | 156 | if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE ) |
mbedAustin | 11:cada08fc8a70 | 157 | { |
mbedAustin | 11:cada08fc8a70 | 158 | #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) |
mbedAustin | 11:cada08fc8a70 | 159 | mbedtls_sha512( data, len, tmp, 0 ); |
mbedAustin | 11:cada08fc8a70 | 160 | #else |
mbedAustin | 11:cada08fc8a70 | 161 | mbedtls_sha256( data, len, tmp, 0 ); |
mbedAustin | 11:cada08fc8a70 | 162 | #endif |
mbedAustin | 11:cada08fc8a70 | 163 | p = tmp; |
mbedAustin | 11:cada08fc8a70 | 164 | use_len = MBEDTLS_ENTROPY_BLOCK_SIZE; |
mbedAustin | 11:cada08fc8a70 | 165 | } |
mbedAustin | 11:cada08fc8a70 | 166 | |
mbedAustin | 11:cada08fc8a70 | 167 | header[0] = source_id; |
mbedAustin | 11:cada08fc8a70 | 168 | header[1] = use_len & 0xFF; |
mbedAustin | 11:cada08fc8a70 | 169 | |
mbedAustin | 11:cada08fc8a70 | 170 | #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) |
mbedAustin | 11:cada08fc8a70 | 171 | mbedtls_sha512_update( &ctx->accumulator, header, 2 ); |
mbedAustin | 11:cada08fc8a70 | 172 | mbedtls_sha512_update( &ctx->accumulator, p, use_len ); |
mbedAustin | 11:cada08fc8a70 | 173 | #else |
mbedAustin | 11:cada08fc8a70 | 174 | mbedtls_sha256_update( &ctx->accumulator, header, 2 ); |
mbedAustin | 11:cada08fc8a70 | 175 | mbedtls_sha256_update( &ctx->accumulator, p, use_len ); |
mbedAustin | 11:cada08fc8a70 | 176 | #endif |
mbedAustin | 11:cada08fc8a70 | 177 | |
mbedAustin | 11:cada08fc8a70 | 178 | return( 0 ); |
mbedAustin | 11:cada08fc8a70 | 179 | } |
mbedAustin | 11:cada08fc8a70 | 180 | |
mbedAustin | 11:cada08fc8a70 | 181 | int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx, |
mbedAustin | 11:cada08fc8a70 | 182 | const unsigned char *data, size_t len ) |
mbedAustin | 11:cada08fc8a70 | 183 | { |
mbedAustin | 11:cada08fc8a70 | 184 | int ret; |
mbedAustin | 11:cada08fc8a70 | 185 | |
mbedAustin | 11:cada08fc8a70 | 186 | #if defined(MBEDTLS_THREADING_C) |
mbedAustin | 11:cada08fc8a70 | 187 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
mbedAustin | 11:cada08fc8a70 | 188 | return( ret ); |
mbedAustin | 11:cada08fc8a70 | 189 | #endif |
mbedAustin | 11:cada08fc8a70 | 190 | |
mbedAustin | 11:cada08fc8a70 | 191 | ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len ); |
mbedAustin | 11:cada08fc8a70 | 192 | |
mbedAustin | 11:cada08fc8a70 | 193 | #if defined(MBEDTLS_THREADING_C) |
mbedAustin | 11:cada08fc8a70 | 194 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
mbedAustin | 11:cada08fc8a70 | 195 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
mbedAustin | 11:cada08fc8a70 | 196 | #endif |
mbedAustin | 11:cada08fc8a70 | 197 | |
mbedAustin | 11:cada08fc8a70 | 198 | return( ret ); |
mbedAustin | 11:cada08fc8a70 | 199 | } |
mbedAustin | 11:cada08fc8a70 | 200 | |
mbedAustin | 11:cada08fc8a70 | 201 | /* |
mbedAustin | 11:cada08fc8a70 | 202 | * Run through the different sources to add entropy to our accumulator |
mbedAustin | 11:cada08fc8a70 | 203 | */ |
mbedAustin | 11:cada08fc8a70 | 204 | static int entropy_gather_internal( mbedtls_entropy_context *ctx ) |
mbedAustin | 11:cada08fc8a70 | 205 | { |
mbedAustin | 11:cada08fc8a70 | 206 | int ret, i, have_one_strong = 0; |
mbedAustin | 11:cada08fc8a70 | 207 | unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER]; |
mbedAustin | 11:cada08fc8a70 | 208 | size_t olen; |
mbedAustin | 11:cada08fc8a70 | 209 | |
mbedAustin | 11:cada08fc8a70 | 210 | if( ctx->source_count == 0 ) |
mbedAustin | 11:cada08fc8a70 | 211 | return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED ); |
mbedAustin | 11:cada08fc8a70 | 212 | |
mbedAustin | 11:cada08fc8a70 | 213 | /* |
mbedAustin | 11:cada08fc8a70 | 214 | * Run through our entropy sources |
mbedAustin | 11:cada08fc8a70 | 215 | */ |
mbedAustin | 11:cada08fc8a70 | 216 | for( i = 0; i < ctx->source_count; i++ ) |
mbedAustin | 11:cada08fc8a70 | 217 | { |
mbedAustin | 11:cada08fc8a70 | 218 | if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG ) |
mbedAustin | 11:cada08fc8a70 | 219 | have_one_strong = 1; |
mbedAustin | 11:cada08fc8a70 | 220 | |
mbedAustin | 11:cada08fc8a70 | 221 | olen = 0; |
mbedAustin | 11:cada08fc8a70 | 222 | if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source, |
mbedAustin | 11:cada08fc8a70 | 223 | buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 ) |
mbedAustin | 11:cada08fc8a70 | 224 | { |
mbedAustin | 11:cada08fc8a70 | 225 | return( ret ); |
mbedAustin | 11:cada08fc8a70 | 226 | } |
mbedAustin | 11:cada08fc8a70 | 227 | |
mbedAustin | 11:cada08fc8a70 | 228 | /* |
mbedAustin | 11:cada08fc8a70 | 229 | * Add if we actually gathered something |
mbedAustin | 11:cada08fc8a70 | 230 | */ |
mbedAustin | 11:cada08fc8a70 | 231 | if( olen > 0 ) |
mbedAustin | 11:cada08fc8a70 | 232 | { |
mbedAustin | 11:cada08fc8a70 | 233 | entropy_update( ctx, (unsigned char) i, buf, olen ); |
mbedAustin | 11:cada08fc8a70 | 234 | ctx->source[i].size += olen; |
mbedAustin | 11:cada08fc8a70 | 235 | } |
mbedAustin | 11:cada08fc8a70 | 236 | } |
mbedAustin | 11:cada08fc8a70 | 237 | |
mbedAustin | 11:cada08fc8a70 | 238 | if( have_one_strong == 0 ) |
mbedAustin | 11:cada08fc8a70 | 239 | return( MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE ); |
mbedAustin | 11:cada08fc8a70 | 240 | |
mbedAustin | 11:cada08fc8a70 | 241 | return( 0 ); |
mbedAustin | 11:cada08fc8a70 | 242 | } |
mbedAustin | 11:cada08fc8a70 | 243 | |
mbedAustin | 11:cada08fc8a70 | 244 | /* |
mbedAustin | 11:cada08fc8a70 | 245 | * Thread-safe wrapper for entropy_gather_internal() |
mbedAustin | 11:cada08fc8a70 | 246 | */ |
mbedAustin | 11:cada08fc8a70 | 247 | int mbedtls_entropy_gather( mbedtls_entropy_context *ctx ) |
mbedAustin | 11:cada08fc8a70 | 248 | { |
mbedAustin | 11:cada08fc8a70 | 249 | int ret; |
mbedAustin | 11:cada08fc8a70 | 250 | |
mbedAustin | 11:cada08fc8a70 | 251 | #if defined(MBEDTLS_THREADING_C) |
mbedAustin | 11:cada08fc8a70 | 252 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
mbedAustin | 11:cada08fc8a70 | 253 | return( ret ); |
mbedAustin | 11:cada08fc8a70 | 254 | #endif |
mbedAustin | 11:cada08fc8a70 | 255 | |
mbedAustin | 11:cada08fc8a70 | 256 | ret = entropy_gather_internal( ctx ); |
mbedAustin | 11:cada08fc8a70 | 257 | |
mbedAustin | 11:cada08fc8a70 | 258 | #if defined(MBEDTLS_THREADING_C) |
mbedAustin | 11:cada08fc8a70 | 259 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
mbedAustin | 11:cada08fc8a70 | 260 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
mbedAustin | 11:cada08fc8a70 | 261 | #endif |
mbedAustin | 11:cada08fc8a70 | 262 | |
mbedAustin | 11:cada08fc8a70 | 263 | return( ret ); |
mbedAustin | 11:cada08fc8a70 | 264 | } |
mbedAustin | 11:cada08fc8a70 | 265 | |
mbedAustin | 11:cada08fc8a70 | 266 | int mbedtls_entropy_func( void *data, unsigned char *output, size_t len ) |
mbedAustin | 11:cada08fc8a70 | 267 | { |
mbedAustin | 11:cada08fc8a70 | 268 | int ret, count = 0, i, done; |
mbedAustin | 11:cada08fc8a70 | 269 | mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data; |
mbedAustin | 11:cada08fc8a70 | 270 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
mbedAustin | 11:cada08fc8a70 | 271 | |
mbedAustin | 11:cada08fc8a70 | 272 | if( len > MBEDTLS_ENTROPY_BLOCK_SIZE ) |
mbedAustin | 11:cada08fc8a70 | 273 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
mbedAustin | 11:cada08fc8a70 | 274 | |
mbedAustin | 11:cada08fc8a70 | 275 | #if defined(MBEDTLS_THREADING_C) |
mbedAustin | 11:cada08fc8a70 | 276 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
mbedAustin | 11:cada08fc8a70 | 277 | return( ret ); |
mbedAustin | 11:cada08fc8a70 | 278 | #endif |
mbedAustin | 11:cada08fc8a70 | 279 | |
mbedAustin | 11:cada08fc8a70 | 280 | /* |
mbedAustin | 11:cada08fc8a70 | 281 | * Always gather extra entropy before a call |
mbedAustin | 11:cada08fc8a70 | 282 | */ |
mbedAustin | 11:cada08fc8a70 | 283 | do |
mbedAustin | 11:cada08fc8a70 | 284 | { |
mbedAustin | 11:cada08fc8a70 | 285 | if( count++ > ENTROPY_MAX_LOOP ) |
mbedAustin | 11:cada08fc8a70 | 286 | { |
mbedAustin | 11:cada08fc8a70 | 287 | ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
mbedAustin | 11:cada08fc8a70 | 288 | goto exit; |
mbedAustin | 11:cada08fc8a70 | 289 | } |
mbedAustin | 11:cada08fc8a70 | 290 | |
mbedAustin | 11:cada08fc8a70 | 291 | if( ( ret = entropy_gather_internal( ctx ) ) != 0 ) |
mbedAustin | 11:cada08fc8a70 | 292 | goto exit; |
mbedAustin | 11:cada08fc8a70 | 293 | |
mbedAustin | 11:cada08fc8a70 | 294 | done = 1; |
mbedAustin | 11:cada08fc8a70 | 295 | for( i = 0; i < ctx->source_count; i++ ) |
mbedAustin | 11:cada08fc8a70 | 296 | if( ctx->source[i].size < ctx->source[i].threshold ) |
mbedAustin | 11:cada08fc8a70 | 297 | done = 0; |
mbedAustin | 11:cada08fc8a70 | 298 | } |
mbedAustin | 11:cada08fc8a70 | 299 | while( ! done ); |
mbedAustin | 11:cada08fc8a70 | 300 | |
mbedAustin | 11:cada08fc8a70 | 301 | memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
mbedAustin | 11:cada08fc8a70 | 302 | |
mbedAustin | 11:cada08fc8a70 | 303 | #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) |
mbedAustin | 11:cada08fc8a70 | 304 | mbedtls_sha512_finish( &ctx->accumulator, buf ); |
mbedAustin | 11:cada08fc8a70 | 305 | |
mbedAustin | 11:cada08fc8a70 | 306 | /* |
mbedAustin | 11:cada08fc8a70 | 307 | * Reset accumulator and counters and recycle existing entropy |
mbedAustin | 11:cada08fc8a70 | 308 | */ |
mbedAustin | 11:cada08fc8a70 | 309 | memset( &ctx->accumulator, 0, sizeof( mbedtls_sha512_context ) ); |
mbedAustin | 11:cada08fc8a70 | 310 | mbedtls_sha512_starts( &ctx->accumulator, 0 ); |
mbedAustin | 11:cada08fc8a70 | 311 | mbedtls_sha512_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
mbedAustin | 11:cada08fc8a70 | 312 | |
mbedAustin | 11:cada08fc8a70 | 313 | /* |
mbedAustin | 11:cada08fc8a70 | 314 | * Perform second SHA-512 on entropy |
mbedAustin | 11:cada08fc8a70 | 315 | */ |
mbedAustin | 11:cada08fc8a70 | 316 | mbedtls_sha512( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 ); |
mbedAustin | 11:cada08fc8a70 | 317 | #else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */ |
mbedAustin | 11:cada08fc8a70 | 318 | mbedtls_sha256_finish( &ctx->accumulator, buf ); |
mbedAustin | 11:cada08fc8a70 | 319 | |
mbedAustin | 11:cada08fc8a70 | 320 | /* |
mbedAustin | 11:cada08fc8a70 | 321 | * Reset accumulator and counters and recycle existing entropy |
mbedAustin | 11:cada08fc8a70 | 322 | */ |
mbedAustin | 11:cada08fc8a70 | 323 | memset( &ctx->accumulator, 0, sizeof( mbedtls_sha256_context ) ); |
mbedAustin | 11:cada08fc8a70 | 324 | mbedtls_sha256_starts( &ctx->accumulator, 0 ); |
mbedAustin | 11:cada08fc8a70 | 325 | mbedtls_sha256_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
mbedAustin | 11:cada08fc8a70 | 326 | |
mbedAustin | 11:cada08fc8a70 | 327 | /* |
mbedAustin | 11:cada08fc8a70 | 328 | * Perform second SHA-256 on entropy |
mbedAustin | 11:cada08fc8a70 | 329 | */ |
mbedAustin | 11:cada08fc8a70 | 330 | mbedtls_sha256( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 ); |
mbedAustin | 11:cada08fc8a70 | 331 | #endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */ |
mbedAustin | 11:cada08fc8a70 | 332 | |
mbedAustin | 11:cada08fc8a70 | 333 | for( i = 0; i < ctx->source_count; i++ ) |
mbedAustin | 11:cada08fc8a70 | 334 | ctx->source[i].size = 0; |
mbedAustin | 11:cada08fc8a70 | 335 | |
mbedAustin | 11:cada08fc8a70 | 336 | memcpy( output, buf, len ); |
mbedAustin | 11:cada08fc8a70 | 337 | |
mbedAustin | 11:cada08fc8a70 | 338 | ret = 0; |
mbedAustin | 11:cada08fc8a70 | 339 | |
mbedAustin | 11:cada08fc8a70 | 340 | exit: |
mbedAustin | 11:cada08fc8a70 | 341 | #if defined(MBEDTLS_THREADING_C) |
mbedAustin | 11:cada08fc8a70 | 342 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
mbedAustin | 11:cada08fc8a70 | 343 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
mbedAustin | 11:cada08fc8a70 | 344 | #endif |
mbedAustin | 11:cada08fc8a70 | 345 | |
mbedAustin | 11:cada08fc8a70 | 346 | return( ret ); |
mbedAustin | 11:cada08fc8a70 | 347 | } |
mbedAustin | 11:cada08fc8a70 | 348 | |
mbedAustin | 11:cada08fc8a70 | 349 | #if defined(MBEDTLS_FS_IO) |
mbedAustin | 11:cada08fc8a70 | 350 | int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path ) |
mbedAustin | 11:cada08fc8a70 | 351 | { |
mbedAustin | 11:cada08fc8a70 | 352 | int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR; |
mbedAustin | 11:cada08fc8a70 | 353 | FILE *f; |
mbedAustin | 11:cada08fc8a70 | 354 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
mbedAustin | 11:cada08fc8a70 | 355 | |
mbedAustin | 11:cada08fc8a70 | 356 | if( ( f = fopen( path, "wb" ) ) == NULL ) |
mbedAustin | 11:cada08fc8a70 | 357 | return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR ); |
mbedAustin | 11:cada08fc8a70 | 358 | |
mbedAustin | 11:cada08fc8a70 | 359 | if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 ) |
mbedAustin | 11:cada08fc8a70 | 360 | goto exit; |
mbedAustin | 11:cada08fc8a70 | 361 | |
mbedAustin | 11:cada08fc8a70 | 362 | if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE ) |
mbedAustin | 11:cada08fc8a70 | 363 | { |
mbedAustin | 11:cada08fc8a70 | 364 | ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR; |
mbedAustin | 11:cada08fc8a70 | 365 | goto exit; |
mbedAustin | 11:cada08fc8a70 | 366 | } |
mbedAustin | 11:cada08fc8a70 | 367 | |
mbedAustin | 11:cada08fc8a70 | 368 | ret = 0; |
mbedAustin | 11:cada08fc8a70 | 369 | |
mbedAustin | 11:cada08fc8a70 | 370 | exit: |
mbedAustin | 11:cada08fc8a70 | 371 | fclose( f ); |
mbedAustin | 11:cada08fc8a70 | 372 | return( ret ); |
mbedAustin | 11:cada08fc8a70 | 373 | } |
mbedAustin | 11:cada08fc8a70 | 374 | |
mbedAustin | 11:cada08fc8a70 | 375 | int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path ) |
mbedAustin | 11:cada08fc8a70 | 376 | { |
mbedAustin | 11:cada08fc8a70 | 377 | FILE *f; |
mbedAustin | 11:cada08fc8a70 | 378 | size_t n; |
mbedAustin | 11:cada08fc8a70 | 379 | unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ]; |
mbedAustin | 11:cada08fc8a70 | 380 | |
mbedAustin | 11:cada08fc8a70 | 381 | if( ( f = fopen( path, "rb" ) ) == NULL ) |
mbedAustin | 11:cada08fc8a70 | 382 | return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR ); |
mbedAustin | 11:cada08fc8a70 | 383 | |
mbedAustin | 11:cada08fc8a70 | 384 | fseek( f, 0, SEEK_END ); |
mbedAustin | 11:cada08fc8a70 | 385 | n = (size_t) ftell( f ); |
mbedAustin | 11:cada08fc8a70 | 386 | fseek( f, 0, SEEK_SET ); |
mbedAustin | 11:cada08fc8a70 | 387 | |
mbedAustin | 11:cada08fc8a70 | 388 | if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) |
mbedAustin | 11:cada08fc8a70 | 389 | n = MBEDTLS_ENTROPY_MAX_SEED_SIZE; |
mbedAustin | 11:cada08fc8a70 | 390 | |
mbedAustin | 11:cada08fc8a70 | 391 | if( fread( buf, 1, n, f ) != n ) |
mbedAustin | 11:cada08fc8a70 | 392 | { |
mbedAustin | 11:cada08fc8a70 | 393 | fclose( f ); |
mbedAustin | 11:cada08fc8a70 | 394 | return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR ); |
mbedAustin | 11:cada08fc8a70 | 395 | } |
mbedAustin | 11:cada08fc8a70 | 396 | |
mbedAustin | 11:cada08fc8a70 | 397 | fclose( f ); |
mbedAustin | 11:cada08fc8a70 | 398 | |
mbedAustin | 11:cada08fc8a70 | 399 | mbedtls_entropy_update_manual( ctx, buf, n ); |
mbedAustin | 11:cada08fc8a70 | 400 | |
mbedAustin | 11:cada08fc8a70 | 401 | return( mbedtls_entropy_write_seed_file( ctx, path ) ); |
mbedAustin | 11:cada08fc8a70 | 402 | } |
mbedAustin | 11:cada08fc8a70 | 403 | #endif /* MBEDTLS_FS_IO */ |
mbedAustin | 11:cada08fc8a70 | 404 | |
mbedAustin | 11:cada08fc8a70 | 405 | #if defined(MBEDTLS_SELF_TEST) |
mbedAustin | 11:cada08fc8a70 | 406 | /* |
mbedAustin | 11:cada08fc8a70 | 407 | * Dummy source function |
mbedAustin | 11:cada08fc8a70 | 408 | */ |
mbedAustin | 11:cada08fc8a70 | 409 | static int entropy_dummy_source( void *data, unsigned char *output, |
mbedAustin | 11:cada08fc8a70 | 410 | size_t len, size_t *olen ) |
mbedAustin | 11:cada08fc8a70 | 411 | { |
mbedAustin | 11:cada08fc8a70 | 412 | ((void) data); |
mbedAustin | 11:cada08fc8a70 | 413 | |
mbedAustin | 11:cada08fc8a70 | 414 | memset( output, 0x2a, len ); |
mbedAustin | 11:cada08fc8a70 | 415 | *olen = len; |
mbedAustin | 11:cada08fc8a70 | 416 | |
mbedAustin | 11:cada08fc8a70 | 417 | return( 0 ); |
mbedAustin | 11:cada08fc8a70 | 418 | } |
mbedAustin | 11:cada08fc8a70 | 419 | |
mbedAustin | 11:cada08fc8a70 | 420 | /* |
mbedAustin | 11:cada08fc8a70 | 421 | * The actual entropy quality is hard to test, but we can at least |
mbedAustin | 11:cada08fc8a70 | 422 | * test that the functions don't cause errors and write the correct |
mbedAustin | 11:cada08fc8a70 | 423 | * amount of data to buffers. |
mbedAustin | 11:cada08fc8a70 | 424 | */ |
mbedAustin | 11:cada08fc8a70 | 425 | int mbedtls_entropy_self_test( int verbose ) |
mbedAustin | 11:cada08fc8a70 | 426 | { |
mbedAustin | 11:cada08fc8a70 | 427 | int ret = 0; |
mbedAustin | 11:cada08fc8a70 | 428 | mbedtls_entropy_context ctx; |
mbedAustin | 11:cada08fc8a70 | 429 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 }; |
mbedAustin | 11:cada08fc8a70 | 430 | unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 }; |
mbedAustin | 11:cada08fc8a70 | 431 | size_t i, j; |
mbedAustin | 11:cada08fc8a70 | 432 | |
mbedAustin | 11:cada08fc8a70 | 433 | if( verbose != 0 ) |
mbedAustin | 11:cada08fc8a70 | 434 | mbedtls_printf( " ENTROPY test: " ); |
mbedAustin | 11:cada08fc8a70 | 435 | |
mbedAustin | 11:cada08fc8a70 | 436 | mbedtls_entropy_init( &ctx ); |
mbedAustin | 11:cada08fc8a70 | 437 | |
mbedAustin | 11:cada08fc8a70 | 438 | /* First do a gather to make sure we have default sources */ |
mbedAustin | 11:cada08fc8a70 | 439 | if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 ) |
mbedAustin | 11:cada08fc8a70 | 440 | goto cleanup; |
mbedAustin | 11:cada08fc8a70 | 441 | |
mbedAustin | 11:cada08fc8a70 | 442 | ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16, |
mbedAustin | 11:cada08fc8a70 | 443 | MBEDTLS_ENTROPY_SOURCE_WEAK ); |
mbedAustin | 11:cada08fc8a70 | 444 | if( ret != 0 ) |
mbedAustin | 11:cada08fc8a70 | 445 | goto cleanup; |
mbedAustin | 11:cada08fc8a70 | 446 | |
mbedAustin | 11:cada08fc8a70 | 447 | if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 ) |
mbedAustin | 11:cada08fc8a70 | 448 | goto cleanup; |
mbedAustin | 11:cada08fc8a70 | 449 | |
mbedAustin | 11:cada08fc8a70 | 450 | /* |
mbedAustin | 11:cada08fc8a70 | 451 | * To test that mbedtls_entropy_func writes correct number of bytes: |
mbedAustin | 11:cada08fc8a70 | 452 | * - use the whole buffer and rely on ASan to detect overruns |
mbedAustin | 11:cada08fc8a70 | 453 | * - collect entropy 8 times and OR the result in an accumulator: |
mbedAustin | 11:cada08fc8a70 | 454 | * any byte should then be 0 with probably 2^(-64), so requiring |
mbedAustin | 11:cada08fc8a70 | 455 | * each of the 32 or 64 bytes to be non-zero has a false failure rate |
mbedAustin | 11:cada08fc8a70 | 456 | * of at most 2^(-58) which is acceptable. |
mbedAustin | 11:cada08fc8a70 | 457 | */ |
mbedAustin | 11:cada08fc8a70 | 458 | for( i = 0; i < 8; i++ ) |
mbedAustin | 11:cada08fc8a70 | 459 | { |
mbedAustin | 11:cada08fc8a70 | 460 | if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 ) |
mbedAustin | 11:cada08fc8a70 | 461 | goto cleanup; |
mbedAustin | 11:cada08fc8a70 | 462 | |
mbedAustin | 11:cada08fc8a70 | 463 | for( j = 0; j < sizeof( buf ); j++ ) |
mbedAustin | 11:cada08fc8a70 | 464 | acc[j] |= buf[j]; |
mbedAustin | 11:cada08fc8a70 | 465 | } |
mbedAustin | 11:cada08fc8a70 | 466 | |
mbedAustin | 11:cada08fc8a70 | 467 | for( j = 0; j < sizeof( buf ); j++ ) |
mbedAustin | 11:cada08fc8a70 | 468 | { |
mbedAustin | 11:cada08fc8a70 | 469 | if( acc[j] == 0 ) |
mbedAustin | 11:cada08fc8a70 | 470 | { |
mbedAustin | 11:cada08fc8a70 | 471 | ret = 1; |
mbedAustin | 11:cada08fc8a70 | 472 | goto cleanup; |
mbedAustin | 11:cada08fc8a70 | 473 | } |
mbedAustin | 11:cada08fc8a70 | 474 | } |
mbedAustin | 11:cada08fc8a70 | 475 | |
mbedAustin | 11:cada08fc8a70 | 476 | cleanup: |
mbedAustin | 11:cada08fc8a70 | 477 | mbedtls_entropy_free( &ctx ); |
mbedAustin | 11:cada08fc8a70 | 478 | |
mbedAustin | 11:cada08fc8a70 | 479 | if( verbose != 0 ) |
mbedAustin | 11:cada08fc8a70 | 480 | { |
mbedAustin | 11:cada08fc8a70 | 481 | if( ret != 0 ) |
mbedAustin | 11:cada08fc8a70 | 482 | mbedtls_printf( "failed\n" ); |
mbedAustin | 11:cada08fc8a70 | 483 | else |
mbedAustin | 11:cada08fc8a70 | 484 | mbedtls_printf( "passed\n" ); |
mbedAustin | 11:cada08fc8a70 | 485 | |
mbedAustin | 11:cada08fc8a70 | 486 | mbedtls_printf( "\n" ); |
mbedAustin | 11:cada08fc8a70 | 487 | } |
mbedAustin | 11:cada08fc8a70 | 488 | |
mbedAustin | 11:cada08fc8a70 | 489 | return( ret != 0 ); |
mbedAustin | 11:cada08fc8a70 | 490 | } |
mbedAustin | 11:cada08fc8a70 | 491 | #endif /* MBEDTLS_SELF_TEST */ |
mbedAustin | 11:cada08fc8a70 | 492 | |
mbedAustin | 11:cada08fc8a70 | 493 | #endif /* MBEDTLS_ENTROPY_C */ |