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