mbed os with nrf51 internal bandgap enabled to read battery level
Dependents: BLE_file_test BLE_Blink ExternalEncoder
features/mbedtls/src/entropy.c@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* |
elessair | 0:f269e3021894 | 2 | * Entropy accumulator implementation |
elessair | 0:f269e3021894 | 3 | * |
elessair | 0:f269e3021894 | 4 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved |
elessair | 0:f269e3021894 | 5 | * SPDX-License-Identifier: Apache-2.0 |
elessair | 0:f269e3021894 | 6 | * |
elessair | 0:f269e3021894 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
elessair | 0:f269e3021894 | 8 | * not use this file except in compliance with the License. |
elessair | 0:f269e3021894 | 9 | * You may obtain a copy of the License at |
elessair | 0:f269e3021894 | 10 | * |
elessair | 0:f269e3021894 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
elessair | 0:f269e3021894 | 12 | * |
elessair | 0:f269e3021894 | 13 | * Unless required by applicable law or agreed to in writing, software |
elessair | 0:f269e3021894 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
elessair | 0:f269e3021894 | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elessair | 0:f269e3021894 | 16 | * See the License for the specific language governing permissions and |
elessair | 0:f269e3021894 | 17 | * limitations under the License. |
elessair | 0:f269e3021894 | 18 | * |
elessair | 0:f269e3021894 | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
elessair | 0:f269e3021894 | 20 | */ |
elessair | 0:f269e3021894 | 21 | |
elessair | 0:f269e3021894 | 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
elessair | 0:f269e3021894 | 23 | #include "mbedtls/config.h" |
elessair | 0:f269e3021894 | 24 | #else |
elessair | 0:f269e3021894 | 25 | #include MBEDTLS_CONFIG_FILE |
elessair | 0:f269e3021894 | 26 | #endif |
elessair | 0:f269e3021894 | 27 | |
elessair | 0:f269e3021894 | 28 | #if defined(MBEDTLS_ENTROPY_C) |
elessair | 0:f269e3021894 | 29 | |
elessair | 0:f269e3021894 | 30 | #if defined(MBEDTLS_TEST_NULL_ENTROPY) |
elessair | 0:f269e3021894 | 31 | #warning "**** WARNING! MBEDTLS_TEST_NULL_ENTROPY defined! " |
elessair | 0:f269e3021894 | 32 | #warning "**** THIS BUILD HAS NO DEFINED ENTROPY SOURCES " |
elessair | 0:f269e3021894 | 33 | #warning "**** THIS BUILD IS *NOT* SUITABLE FOR PRODUCTION USE " |
elessair | 0:f269e3021894 | 34 | #endif |
elessair | 0:f269e3021894 | 35 | |
elessair | 0:f269e3021894 | 36 | #include "mbedtls/entropy.h" |
elessair | 0:f269e3021894 | 37 | #include "mbedtls/entropy_poll.h" |
elessair | 0:f269e3021894 | 38 | |
elessair | 0:f269e3021894 | 39 | #include <string.h> |
elessair | 0:f269e3021894 | 40 | |
elessair | 0:f269e3021894 | 41 | #if defined(MBEDTLS_FS_IO) |
elessair | 0:f269e3021894 | 42 | #include <stdio.h> |
elessair | 0:f269e3021894 | 43 | #endif |
elessair | 0:f269e3021894 | 44 | |
elessair | 0:f269e3021894 | 45 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
elessair | 0:f269e3021894 | 46 | #include "mbedtls/platform.h" |
elessair | 0:f269e3021894 | 47 | #endif |
elessair | 0:f269e3021894 | 48 | |
elessair | 0:f269e3021894 | 49 | #if defined(MBEDTLS_SELF_TEST) |
elessair | 0:f269e3021894 | 50 | #if defined(MBEDTLS_PLATFORM_C) |
elessair | 0:f269e3021894 | 51 | #include "mbedtls/platform.h" |
elessair | 0:f269e3021894 | 52 | #else |
elessair | 0:f269e3021894 | 53 | #include <stdio.h> |
elessair | 0:f269e3021894 | 54 | #define mbedtls_printf printf |
elessair | 0:f269e3021894 | 55 | #endif /* MBEDTLS_PLATFORM_C */ |
elessair | 0:f269e3021894 | 56 | #endif /* MBEDTLS_SELF_TEST */ |
elessair | 0:f269e3021894 | 57 | |
elessair | 0:f269e3021894 | 58 | #if defined(MBEDTLS_HAVEGE_C) |
elessair | 0:f269e3021894 | 59 | #include "mbedtls/havege.h" |
elessair | 0:f269e3021894 | 60 | #endif |
elessair | 0:f269e3021894 | 61 | |
elessair | 0:f269e3021894 | 62 | /* Implementation that should never be optimized out by the compiler */ |
elessair | 0:f269e3021894 | 63 | static void mbedtls_zeroize( void *v, size_t n ) { |
elessair | 0:f269e3021894 | 64 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
elessair | 0:f269e3021894 | 65 | } |
elessair | 0:f269e3021894 | 66 | |
elessair | 0:f269e3021894 | 67 | #define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */ |
elessair | 0:f269e3021894 | 68 | |
elessair | 0:f269e3021894 | 69 | void mbedtls_entropy_init( mbedtls_entropy_context *ctx ) |
elessair | 0:f269e3021894 | 70 | { |
elessair | 0:f269e3021894 | 71 | memset( ctx, 0, sizeof(mbedtls_entropy_context) ); |
elessair | 0:f269e3021894 | 72 | |
elessair | 0:f269e3021894 | 73 | #if defined(MBEDTLS_THREADING_C) |
elessair | 0:f269e3021894 | 74 | mbedtls_mutex_init( &ctx->mutex ); |
elessair | 0:f269e3021894 | 75 | #endif |
elessair | 0:f269e3021894 | 76 | |
elessair | 0:f269e3021894 | 77 | #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) |
elessair | 0:f269e3021894 | 78 | mbedtls_sha512_starts( &ctx->accumulator, 0 ); |
elessair | 0:f269e3021894 | 79 | #else |
elessair | 0:f269e3021894 | 80 | mbedtls_sha256_starts( &ctx->accumulator, 0 ); |
elessair | 0:f269e3021894 | 81 | #endif |
elessair | 0:f269e3021894 | 82 | #if defined(MBEDTLS_HAVEGE_C) |
elessair | 0:f269e3021894 | 83 | mbedtls_havege_init( &ctx->havege_data ); |
elessair | 0:f269e3021894 | 84 | #endif |
elessair | 0:f269e3021894 | 85 | |
elessair | 0:f269e3021894 | 86 | #if defined(MBEDTLS_TEST_NULL_ENTROPY) |
elessair | 0:f269e3021894 | 87 | mbedtls_entropy_add_source( ctx, mbedtls_null_entropy_poll, NULL, |
elessair | 0:f269e3021894 | 88 | 1, MBEDTLS_ENTROPY_SOURCE_STRONG ); |
elessair | 0:f269e3021894 | 89 | #endif |
elessair | 0:f269e3021894 | 90 | |
elessair | 0:f269e3021894 | 91 | #if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
elessair | 0:f269e3021894 | 92 | #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY) |
elessair | 0:f269e3021894 | 93 | mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL, |
elessair | 0:f269e3021894 | 94 | MBEDTLS_ENTROPY_MIN_PLATFORM, |
elessair | 0:f269e3021894 | 95 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
elessair | 0:f269e3021894 | 96 | #endif |
elessair | 0:f269e3021894 | 97 | #if defined(MBEDTLS_TIMING_C) |
elessair | 0:f269e3021894 | 98 | mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL, |
elessair | 0:f269e3021894 | 99 | MBEDTLS_ENTROPY_MIN_HARDCLOCK, |
elessair | 0:f269e3021894 | 100 | MBEDTLS_ENTROPY_SOURCE_WEAK ); |
elessair | 0:f269e3021894 | 101 | #endif |
elessair | 0:f269e3021894 | 102 | #if defined(MBEDTLS_HAVEGE_C) |
elessair | 0:f269e3021894 | 103 | mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data, |
elessair | 0:f269e3021894 | 104 | MBEDTLS_ENTROPY_MIN_HAVEGE, |
elessair | 0:f269e3021894 | 105 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
elessair | 0:f269e3021894 | 106 | #endif |
elessair | 0:f269e3021894 | 107 | #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT) |
elessair | 0:f269e3021894 | 108 | mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL, |
elessair | 0:f269e3021894 | 109 | MBEDTLS_ENTROPY_MIN_HARDWARE, |
elessair | 0:f269e3021894 | 110 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
elessair | 0:f269e3021894 | 111 | #endif |
elessair | 0:f269e3021894 | 112 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
elessair | 0:f269e3021894 | 113 | mbedtls_entropy_add_source( ctx, mbedtls_nv_seed_poll, NULL, |
elessair | 0:f269e3021894 | 114 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
elessair | 0:f269e3021894 | 115 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
elessair | 0:f269e3021894 | 116 | #endif |
elessair | 0:f269e3021894 | 117 | #endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */ |
elessair | 0:f269e3021894 | 118 | } |
elessair | 0:f269e3021894 | 119 | |
elessair | 0:f269e3021894 | 120 | void mbedtls_entropy_free( mbedtls_entropy_context *ctx ) |
elessair | 0:f269e3021894 | 121 | { |
elessair | 0:f269e3021894 | 122 | #if defined(MBEDTLS_HAVEGE_C) |
elessair | 0:f269e3021894 | 123 | mbedtls_havege_free( &ctx->havege_data ); |
elessair | 0:f269e3021894 | 124 | #endif |
elessair | 0:f269e3021894 | 125 | #if defined(MBEDTLS_THREADING_C) |
elessair | 0:f269e3021894 | 126 | mbedtls_mutex_free( &ctx->mutex ); |
elessair | 0:f269e3021894 | 127 | #endif |
elessair | 0:f269e3021894 | 128 | mbedtls_zeroize( ctx, sizeof( mbedtls_entropy_context ) ); |
elessair | 0:f269e3021894 | 129 | } |
elessair | 0:f269e3021894 | 130 | |
elessair | 0:f269e3021894 | 131 | int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx, |
elessair | 0:f269e3021894 | 132 | mbedtls_entropy_f_source_ptr f_source, void *p_source, |
elessair | 0:f269e3021894 | 133 | size_t threshold, int strong ) |
elessair | 0:f269e3021894 | 134 | { |
elessair | 0:f269e3021894 | 135 | int index, ret = 0; |
elessair | 0:f269e3021894 | 136 | |
elessair | 0:f269e3021894 | 137 | #if defined(MBEDTLS_THREADING_C) |
elessair | 0:f269e3021894 | 138 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
elessair | 0:f269e3021894 | 139 | return( ret ); |
elessair | 0:f269e3021894 | 140 | #endif |
elessair | 0:f269e3021894 | 141 | |
elessair | 0:f269e3021894 | 142 | index = ctx->source_count; |
elessair | 0:f269e3021894 | 143 | if( index >= MBEDTLS_ENTROPY_MAX_SOURCES ) |
elessair | 0:f269e3021894 | 144 | { |
elessair | 0:f269e3021894 | 145 | ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES; |
elessair | 0:f269e3021894 | 146 | goto exit; |
elessair | 0:f269e3021894 | 147 | } |
elessair | 0:f269e3021894 | 148 | |
elessair | 0:f269e3021894 | 149 | ctx->source[index].f_source = f_source; |
elessair | 0:f269e3021894 | 150 | ctx->source[index].p_source = p_source; |
elessair | 0:f269e3021894 | 151 | ctx->source[index].threshold = threshold; |
elessair | 0:f269e3021894 | 152 | ctx->source[index].strong = strong; |
elessair | 0:f269e3021894 | 153 | |
elessair | 0:f269e3021894 | 154 | ctx->source_count++; |
elessair | 0:f269e3021894 | 155 | |
elessair | 0:f269e3021894 | 156 | exit: |
elessair | 0:f269e3021894 | 157 | #if defined(MBEDTLS_THREADING_C) |
elessair | 0:f269e3021894 | 158 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
elessair | 0:f269e3021894 | 159 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
elessair | 0:f269e3021894 | 160 | #endif |
elessair | 0:f269e3021894 | 161 | |
elessair | 0:f269e3021894 | 162 | return( ret ); |
elessair | 0:f269e3021894 | 163 | } |
elessair | 0:f269e3021894 | 164 | |
elessair | 0:f269e3021894 | 165 | /* |
elessair | 0:f269e3021894 | 166 | * Entropy accumulator update |
elessair | 0:f269e3021894 | 167 | */ |
elessair | 0:f269e3021894 | 168 | static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id, |
elessair | 0:f269e3021894 | 169 | const unsigned char *data, size_t len ) |
elessair | 0:f269e3021894 | 170 | { |
elessair | 0:f269e3021894 | 171 | unsigned char header[2]; |
elessair | 0:f269e3021894 | 172 | unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
elessair | 0:f269e3021894 | 173 | size_t use_len = len; |
elessair | 0:f269e3021894 | 174 | const unsigned char *p = data; |
elessair | 0:f269e3021894 | 175 | |
elessair | 0:f269e3021894 | 176 | if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE ) |
elessair | 0:f269e3021894 | 177 | { |
elessair | 0:f269e3021894 | 178 | #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) |
elessair | 0:f269e3021894 | 179 | mbedtls_sha512( data, len, tmp, 0 ); |
elessair | 0:f269e3021894 | 180 | #else |
elessair | 0:f269e3021894 | 181 | mbedtls_sha256( data, len, tmp, 0 ); |
elessair | 0:f269e3021894 | 182 | #endif |
elessair | 0:f269e3021894 | 183 | p = tmp; |
elessair | 0:f269e3021894 | 184 | use_len = MBEDTLS_ENTROPY_BLOCK_SIZE; |
elessair | 0:f269e3021894 | 185 | } |
elessair | 0:f269e3021894 | 186 | |
elessair | 0:f269e3021894 | 187 | header[0] = source_id; |
elessair | 0:f269e3021894 | 188 | header[1] = use_len & 0xFF; |
elessair | 0:f269e3021894 | 189 | |
elessair | 0:f269e3021894 | 190 | #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) |
elessair | 0:f269e3021894 | 191 | mbedtls_sha512_update( &ctx->accumulator, header, 2 ); |
elessair | 0:f269e3021894 | 192 | mbedtls_sha512_update( &ctx->accumulator, p, use_len ); |
elessair | 0:f269e3021894 | 193 | #else |
elessair | 0:f269e3021894 | 194 | mbedtls_sha256_update( &ctx->accumulator, header, 2 ); |
elessair | 0:f269e3021894 | 195 | mbedtls_sha256_update( &ctx->accumulator, p, use_len ); |
elessair | 0:f269e3021894 | 196 | #endif |
elessair | 0:f269e3021894 | 197 | |
elessair | 0:f269e3021894 | 198 | return( 0 ); |
elessair | 0:f269e3021894 | 199 | } |
elessair | 0:f269e3021894 | 200 | |
elessair | 0:f269e3021894 | 201 | int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx, |
elessair | 0:f269e3021894 | 202 | const unsigned char *data, size_t len ) |
elessair | 0:f269e3021894 | 203 | { |
elessair | 0:f269e3021894 | 204 | int ret; |
elessair | 0:f269e3021894 | 205 | |
elessair | 0:f269e3021894 | 206 | #if defined(MBEDTLS_THREADING_C) |
elessair | 0:f269e3021894 | 207 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
elessair | 0:f269e3021894 | 208 | return( ret ); |
elessair | 0:f269e3021894 | 209 | #endif |
elessair | 0:f269e3021894 | 210 | |
elessair | 0:f269e3021894 | 211 | ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len ); |
elessair | 0:f269e3021894 | 212 | |
elessair | 0:f269e3021894 | 213 | #if defined(MBEDTLS_THREADING_C) |
elessair | 0:f269e3021894 | 214 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
elessair | 0:f269e3021894 | 215 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
elessair | 0:f269e3021894 | 216 | #endif |
elessair | 0:f269e3021894 | 217 | |
elessair | 0:f269e3021894 | 218 | return( ret ); |
elessair | 0:f269e3021894 | 219 | } |
elessair | 0:f269e3021894 | 220 | |
elessair | 0:f269e3021894 | 221 | /* |
elessair | 0:f269e3021894 | 222 | * Run through the different sources to add entropy to our accumulator |
elessair | 0:f269e3021894 | 223 | */ |
elessair | 0:f269e3021894 | 224 | static int entropy_gather_internal( mbedtls_entropy_context *ctx ) |
elessair | 0:f269e3021894 | 225 | { |
elessair | 0:f269e3021894 | 226 | int ret, i, have_one_strong = 0; |
elessair | 0:f269e3021894 | 227 | unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER]; |
elessair | 0:f269e3021894 | 228 | size_t olen; |
elessair | 0:f269e3021894 | 229 | |
elessair | 0:f269e3021894 | 230 | if( ctx->source_count == 0 ) |
elessair | 0:f269e3021894 | 231 | return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED ); |
elessair | 0:f269e3021894 | 232 | |
elessair | 0:f269e3021894 | 233 | /* |
elessair | 0:f269e3021894 | 234 | * Run through our entropy sources |
elessair | 0:f269e3021894 | 235 | */ |
elessair | 0:f269e3021894 | 236 | for( i = 0; i < ctx->source_count; i++ ) |
elessair | 0:f269e3021894 | 237 | { |
elessair | 0:f269e3021894 | 238 | if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG ) |
elessair | 0:f269e3021894 | 239 | have_one_strong = 1; |
elessair | 0:f269e3021894 | 240 | |
elessair | 0:f269e3021894 | 241 | olen = 0; |
elessair | 0:f269e3021894 | 242 | if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source, |
elessair | 0:f269e3021894 | 243 | buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 ) |
elessair | 0:f269e3021894 | 244 | { |
elessair | 0:f269e3021894 | 245 | return( ret ); |
elessair | 0:f269e3021894 | 246 | } |
elessair | 0:f269e3021894 | 247 | |
elessair | 0:f269e3021894 | 248 | /* |
elessair | 0:f269e3021894 | 249 | * Add if we actually gathered something |
elessair | 0:f269e3021894 | 250 | */ |
elessair | 0:f269e3021894 | 251 | if( olen > 0 ) |
elessair | 0:f269e3021894 | 252 | { |
elessair | 0:f269e3021894 | 253 | entropy_update( ctx, (unsigned char) i, buf, olen ); |
elessair | 0:f269e3021894 | 254 | ctx->source[i].size += olen; |
elessair | 0:f269e3021894 | 255 | } |
elessair | 0:f269e3021894 | 256 | } |
elessair | 0:f269e3021894 | 257 | |
elessair | 0:f269e3021894 | 258 | if( have_one_strong == 0 ) |
elessair | 0:f269e3021894 | 259 | return( MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE ); |
elessair | 0:f269e3021894 | 260 | |
elessair | 0:f269e3021894 | 261 | return( 0 ); |
elessair | 0:f269e3021894 | 262 | } |
elessair | 0:f269e3021894 | 263 | |
elessair | 0:f269e3021894 | 264 | /* |
elessair | 0:f269e3021894 | 265 | * Thread-safe wrapper for entropy_gather_internal() |
elessair | 0:f269e3021894 | 266 | */ |
elessair | 0:f269e3021894 | 267 | int mbedtls_entropy_gather( mbedtls_entropy_context *ctx ) |
elessair | 0:f269e3021894 | 268 | { |
elessair | 0:f269e3021894 | 269 | int ret; |
elessair | 0:f269e3021894 | 270 | |
elessair | 0:f269e3021894 | 271 | #if defined(MBEDTLS_THREADING_C) |
elessair | 0:f269e3021894 | 272 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
elessair | 0:f269e3021894 | 273 | return( ret ); |
elessair | 0:f269e3021894 | 274 | #endif |
elessair | 0:f269e3021894 | 275 | |
elessair | 0:f269e3021894 | 276 | ret = entropy_gather_internal( ctx ); |
elessair | 0:f269e3021894 | 277 | |
elessair | 0:f269e3021894 | 278 | #if defined(MBEDTLS_THREADING_C) |
elessair | 0:f269e3021894 | 279 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
elessair | 0:f269e3021894 | 280 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
elessair | 0:f269e3021894 | 281 | #endif |
elessair | 0:f269e3021894 | 282 | |
elessair | 0:f269e3021894 | 283 | return( ret ); |
elessair | 0:f269e3021894 | 284 | } |
elessair | 0:f269e3021894 | 285 | |
elessair | 0:f269e3021894 | 286 | int mbedtls_entropy_func( void *data, unsigned char *output, size_t len ) |
elessair | 0:f269e3021894 | 287 | { |
elessair | 0:f269e3021894 | 288 | int ret, count = 0, i, done; |
elessair | 0:f269e3021894 | 289 | mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data; |
elessair | 0:f269e3021894 | 290 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
elessair | 0:f269e3021894 | 291 | |
elessair | 0:f269e3021894 | 292 | if( len > MBEDTLS_ENTROPY_BLOCK_SIZE ) |
elessair | 0:f269e3021894 | 293 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
elessair | 0:f269e3021894 | 294 | |
elessair | 0:f269e3021894 | 295 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
elessair | 0:f269e3021894 | 296 | /* Update the NV entropy seed before generating any entropy for outside |
elessair | 0:f269e3021894 | 297 | * use. |
elessair | 0:f269e3021894 | 298 | */ |
elessair | 0:f269e3021894 | 299 | if( ctx->initial_entropy_run == 0 ) |
elessair | 0:f269e3021894 | 300 | { |
elessair | 0:f269e3021894 | 301 | ctx->initial_entropy_run = 1; |
elessair | 0:f269e3021894 | 302 | if( ( ret = mbedtls_entropy_update_nv_seed( ctx ) ) != 0 ) |
elessair | 0:f269e3021894 | 303 | return( ret ); |
elessair | 0:f269e3021894 | 304 | } |
elessair | 0:f269e3021894 | 305 | #endif |
elessair | 0:f269e3021894 | 306 | |
elessair | 0:f269e3021894 | 307 | #if defined(MBEDTLS_THREADING_C) |
elessair | 0:f269e3021894 | 308 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
elessair | 0:f269e3021894 | 309 | return( ret ); |
elessair | 0:f269e3021894 | 310 | #endif |
elessair | 0:f269e3021894 | 311 | |
elessair | 0:f269e3021894 | 312 | /* |
elessair | 0:f269e3021894 | 313 | * Always gather extra entropy before a call |
elessair | 0:f269e3021894 | 314 | */ |
elessair | 0:f269e3021894 | 315 | do |
elessair | 0:f269e3021894 | 316 | { |
elessair | 0:f269e3021894 | 317 | if( count++ > ENTROPY_MAX_LOOP ) |
elessair | 0:f269e3021894 | 318 | { |
elessair | 0:f269e3021894 | 319 | ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
elessair | 0:f269e3021894 | 320 | goto exit; |
elessair | 0:f269e3021894 | 321 | } |
elessair | 0:f269e3021894 | 322 | |
elessair | 0:f269e3021894 | 323 | if( ( ret = entropy_gather_internal( ctx ) ) != 0 ) |
elessair | 0:f269e3021894 | 324 | goto exit; |
elessair | 0:f269e3021894 | 325 | |
elessair | 0:f269e3021894 | 326 | done = 1; |
elessair | 0:f269e3021894 | 327 | for( i = 0; i < ctx->source_count; i++ ) |
elessair | 0:f269e3021894 | 328 | if( ctx->source[i].size < ctx->source[i].threshold ) |
elessair | 0:f269e3021894 | 329 | done = 0; |
elessair | 0:f269e3021894 | 330 | } |
elessair | 0:f269e3021894 | 331 | while( ! done ); |
elessair | 0:f269e3021894 | 332 | |
elessair | 0:f269e3021894 | 333 | memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
elessair | 0:f269e3021894 | 334 | |
elessair | 0:f269e3021894 | 335 | #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) |
elessair | 0:f269e3021894 | 336 | mbedtls_sha512_finish( &ctx->accumulator, buf ); |
elessair | 0:f269e3021894 | 337 | |
elessair | 0:f269e3021894 | 338 | /* |
elessair | 0:f269e3021894 | 339 | * Reset accumulator and counters and recycle existing entropy |
elessair | 0:f269e3021894 | 340 | */ |
elessair | 0:f269e3021894 | 341 | memset( &ctx->accumulator, 0, sizeof( mbedtls_sha512_context ) ); |
elessair | 0:f269e3021894 | 342 | mbedtls_sha512_starts( &ctx->accumulator, 0 ); |
elessair | 0:f269e3021894 | 343 | mbedtls_sha512_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
elessair | 0:f269e3021894 | 344 | |
elessair | 0:f269e3021894 | 345 | /* |
elessair | 0:f269e3021894 | 346 | * Perform second SHA-512 on entropy |
elessair | 0:f269e3021894 | 347 | */ |
elessair | 0:f269e3021894 | 348 | mbedtls_sha512( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 ); |
elessair | 0:f269e3021894 | 349 | #else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */ |
elessair | 0:f269e3021894 | 350 | mbedtls_sha256_finish( &ctx->accumulator, buf ); |
elessair | 0:f269e3021894 | 351 | |
elessair | 0:f269e3021894 | 352 | /* |
elessair | 0:f269e3021894 | 353 | * Reset accumulator and counters and recycle existing entropy |
elessair | 0:f269e3021894 | 354 | */ |
elessair | 0:f269e3021894 | 355 | memset( &ctx->accumulator, 0, sizeof( mbedtls_sha256_context ) ); |
elessair | 0:f269e3021894 | 356 | mbedtls_sha256_starts( &ctx->accumulator, 0 ); |
elessair | 0:f269e3021894 | 357 | mbedtls_sha256_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
elessair | 0:f269e3021894 | 358 | |
elessair | 0:f269e3021894 | 359 | /* |
elessair | 0:f269e3021894 | 360 | * Perform second SHA-256 on entropy |
elessair | 0:f269e3021894 | 361 | */ |
elessair | 0:f269e3021894 | 362 | mbedtls_sha256( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 ); |
elessair | 0:f269e3021894 | 363 | #endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */ |
elessair | 0:f269e3021894 | 364 | |
elessair | 0:f269e3021894 | 365 | for( i = 0; i < ctx->source_count; i++ ) |
elessair | 0:f269e3021894 | 366 | ctx->source[i].size = 0; |
elessair | 0:f269e3021894 | 367 | |
elessair | 0:f269e3021894 | 368 | memcpy( output, buf, len ); |
elessair | 0:f269e3021894 | 369 | |
elessair | 0:f269e3021894 | 370 | ret = 0; |
elessair | 0:f269e3021894 | 371 | |
elessair | 0:f269e3021894 | 372 | exit: |
elessair | 0:f269e3021894 | 373 | #if defined(MBEDTLS_THREADING_C) |
elessair | 0:f269e3021894 | 374 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
elessair | 0:f269e3021894 | 375 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
elessair | 0:f269e3021894 | 376 | #endif |
elessair | 0:f269e3021894 | 377 | |
elessair | 0:f269e3021894 | 378 | return( ret ); |
elessair | 0:f269e3021894 | 379 | } |
elessair | 0:f269e3021894 | 380 | |
elessair | 0:f269e3021894 | 381 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
elessair | 0:f269e3021894 | 382 | int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx ) |
elessair | 0:f269e3021894 | 383 | { |
elessair | 0:f269e3021894 | 384 | int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR; |
elessair | 0:f269e3021894 | 385 | unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ]; |
elessair | 0:f269e3021894 | 386 | |
elessair | 0:f269e3021894 | 387 | /* Read new seed and write it to NV */ |
elessair | 0:f269e3021894 | 388 | if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 ) |
elessair | 0:f269e3021894 | 389 | return( ret ); |
elessair | 0:f269e3021894 | 390 | |
elessair | 0:f269e3021894 | 391 | if( mbedtls_nv_seed_write( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 ) |
elessair | 0:f269e3021894 | 392 | return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR ); |
elessair | 0:f269e3021894 | 393 | |
elessair | 0:f269e3021894 | 394 | /* Manually update the remaining stream with a separator value to diverge */ |
elessair | 0:f269e3021894 | 395 | memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
elessair | 0:f269e3021894 | 396 | mbedtls_entropy_update_manual( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
elessair | 0:f269e3021894 | 397 | |
elessair | 0:f269e3021894 | 398 | return( 0 ); |
elessair | 0:f269e3021894 | 399 | } |
elessair | 0:f269e3021894 | 400 | #endif /* MBEDTLS_ENTROPY_NV_SEED */ |
elessair | 0:f269e3021894 | 401 | |
elessair | 0:f269e3021894 | 402 | #if defined(MBEDTLS_FS_IO) |
elessair | 0:f269e3021894 | 403 | int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path ) |
elessair | 0:f269e3021894 | 404 | { |
elessair | 0:f269e3021894 | 405 | int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR; |
elessair | 0:f269e3021894 | 406 | FILE *f; |
elessair | 0:f269e3021894 | 407 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
elessair | 0:f269e3021894 | 408 | |
elessair | 0:f269e3021894 | 409 | if( ( f = fopen( path, "wb" ) ) == NULL ) |
elessair | 0:f269e3021894 | 410 | return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR ); |
elessair | 0:f269e3021894 | 411 | |
elessair | 0:f269e3021894 | 412 | if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 ) |
elessair | 0:f269e3021894 | 413 | goto exit; |
elessair | 0:f269e3021894 | 414 | |
elessair | 0:f269e3021894 | 415 | if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE ) |
elessair | 0:f269e3021894 | 416 | { |
elessair | 0:f269e3021894 | 417 | ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR; |
elessair | 0:f269e3021894 | 418 | goto exit; |
elessair | 0:f269e3021894 | 419 | } |
elessair | 0:f269e3021894 | 420 | |
elessair | 0:f269e3021894 | 421 | ret = 0; |
elessair | 0:f269e3021894 | 422 | |
elessair | 0:f269e3021894 | 423 | exit: |
elessair | 0:f269e3021894 | 424 | fclose( f ); |
elessair | 0:f269e3021894 | 425 | return( ret ); |
elessair | 0:f269e3021894 | 426 | } |
elessair | 0:f269e3021894 | 427 | |
elessair | 0:f269e3021894 | 428 | int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path ) |
elessair | 0:f269e3021894 | 429 | { |
elessair | 0:f269e3021894 | 430 | FILE *f; |
elessair | 0:f269e3021894 | 431 | size_t n; |
elessair | 0:f269e3021894 | 432 | unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ]; |
elessair | 0:f269e3021894 | 433 | |
elessair | 0:f269e3021894 | 434 | if( ( f = fopen( path, "rb" ) ) == NULL ) |
elessair | 0:f269e3021894 | 435 | return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR ); |
elessair | 0:f269e3021894 | 436 | |
elessair | 0:f269e3021894 | 437 | fseek( f, 0, SEEK_END ); |
elessair | 0:f269e3021894 | 438 | n = (size_t) ftell( f ); |
elessair | 0:f269e3021894 | 439 | fseek( f, 0, SEEK_SET ); |
elessair | 0:f269e3021894 | 440 | |
elessair | 0:f269e3021894 | 441 | if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) |
elessair | 0:f269e3021894 | 442 | n = MBEDTLS_ENTROPY_MAX_SEED_SIZE; |
elessair | 0:f269e3021894 | 443 | |
elessair | 0:f269e3021894 | 444 | if( fread( buf, 1, n, f ) != n ) |
elessair | 0:f269e3021894 | 445 | { |
elessair | 0:f269e3021894 | 446 | fclose( f ); |
elessair | 0:f269e3021894 | 447 | return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR ); |
elessair | 0:f269e3021894 | 448 | } |
elessair | 0:f269e3021894 | 449 | |
elessair | 0:f269e3021894 | 450 | fclose( f ); |
elessair | 0:f269e3021894 | 451 | |
elessair | 0:f269e3021894 | 452 | mbedtls_entropy_update_manual( ctx, buf, n ); |
elessair | 0:f269e3021894 | 453 | |
elessair | 0:f269e3021894 | 454 | return( mbedtls_entropy_write_seed_file( ctx, path ) ); |
elessair | 0:f269e3021894 | 455 | } |
elessair | 0:f269e3021894 | 456 | #endif /* MBEDTLS_FS_IO */ |
elessair | 0:f269e3021894 | 457 | |
elessair | 0:f269e3021894 | 458 | #if defined(MBEDTLS_SELF_TEST) |
elessair | 0:f269e3021894 | 459 | #if !defined(MBEDTLS_TEST_NULL_ENTROPY) |
elessair | 0:f269e3021894 | 460 | /* |
elessair | 0:f269e3021894 | 461 | * Dummy source function |
elessair | 0:f269e3021894 | 462 | */ |
elessair | 0:f269e3021894 | 463 | static int entropy_dummy_source( void *data, unsigned char *output, |
elessair | 0:f269e3021894 | 464 | size_t len, size_t *olen ) |
elessair | 0:f269e3021894 | 465 | { |
elessair | 0:f269e3021894 | 466 | ((void) data); |
elessair | 0:f269e3021894 | 467 | |
elessair | 0:f269e3021894 | 468 | memset( output, 0x2a, len ); |
elessair | 0:f269e3021894 | 469 | *olen = len; |
elessair | 0:f269e3021894 | 470 | |
elessair | 0:f269e3021894 | 471 | return( 0 ); |
elessair | 0:f269e3021894 | 472 | } |
elessair | 0:f269e3021894 | 473 | #endif /* !MBEDTLS_TEST_NULL_ENTROPY */ |
elessair | 0:f269e3021894 | 474 | |
elessair | 0:f269e3021894 | 475 | #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT) |
elessair | 0:f269e3021894 | 476 | |
elessair | 0:f269e3021894 | 477 | static int mbedtls_entropy_source_self_test_gather( unsigned char *buf, size_t buf_len ) |
elessair | 0:f269e3021894 | 478 | { |
elessair | 0:f269e3021894 | 479 | int ret = 0; |
elessair | 0:f269e3021894 | 480 | size_t entropy_len = 0; |
elessair | 0:f269e3021894 | 481 | size_t olen = 0; |
elessair | 0:f269e3021894 | 482 | size_t attempts = buf_len; |
elessair | 0:f269e3021894 | 483 | |
elessair | 0:f269e3021894 | 484 | while( attempts > 0 && entropy_len < buf_len ) |
elessair | 0:f269e3021894 | 485 | { |
elessair | 0:f269e3021894 | 486 | if( ( ret = mbedtls_hardware_poll( NULL, buf + entropy_len, |
elessair | 0:f269e3021894 | 487 | buf_len - entropy_len, &olen ) ) != 0 ) |
elessair | 0:f269e3021894 | 488 | return( ret ); |
elessair | 0:f269e3021894 | 489 | |
elessair | 0:f269e3021894 | 490 | entropy_len += olen; |
elessair | 0:f269e3021894 | 491 | attempts--; |
elessair | 0:f269e3021894 | 492 | } |
elessair | 0:f269e3021894 | 493 | |
elessair | 0:f269e3021894 | 494 | if( entropy_len < buf_len ) |
elessair | 0:f269e3021894 | 495 | { |
elessair | 0:f269e3021894 | 496 | ret = 1; |
elessair | 0:f269e3021894 | 497 | } |
elessair | 0:f269e3021894 | 498 | |
elessair | 0:f269e3021894 | 499 | return( ret ); |
elessair | 0:f269e3021894 | 500 | } |
elessair | 0:f269e3021894 | 501 | |
elessair | 0:f269e3021894 | 502 | |
elessair | 0:f269e3021894 | 503 | static int mbedtls_entropy_source_self_test_check_bits( const unsigned char *buf, |
elessair | 0:f269e3021894 | 504 | size_t buf_len ) |
elessair | 0:f269e3021894 | 505 | { |
elessair | 0:f269e3021894 | 506 | unsigned char set= 0xFF; |
elessair | 0:f269e3021894 | 507 | unsigned char unset = 0x00; |
elessair | 0:f269e3021894 | 508 | size_t i; |
elessair | 0:f269e3021894 | 509 | |
elessair | 0:f269e3021894 | 510 | for( i = 0; i < buf_len; i++ ) |
elessair | 0:f269e3021894 | 511 | { |
elessair | 0:f269e3021894 | 512 | set &= buf[i]; |
elessair | 0:f269e3021894 | 513 | unset |= buf[i]; |
elessair | 0:f269e3021894 | 514 | } |
elessair | 0:f269e3021894 | 515 | |
elessair | 0:f269e3021894 | 516 | return( set == 0xFF || unset == 0x00 ); |
elessair | 0:f269e3021894 | 517 | } |
elessair | 0:f269e3021894 | 518 | |
elessair | 0:f269e3021894 | 519 | /* |
elessair | 0:f269e3021894 | 520 | * A test to ensure hat the entropy sources are functioning correctly |
elessair | 0:f269e3021894 | 521 | * and there is no obvious failure. The test performs the following checks: |
elessair | 0:f269e3021894 | 522 | * - The entropy source is not providing only 0s (all bits unset) or 1s (all |
elessair | 0:f269e3021894 | 523 | * bits set). |
elessair | 0:f269e3021894 | 524 | * - The entropy source is not providing values in a pattern. Because the |
elessair | 0:f269e3021894 | 525 | * hardware could be providing data in an arbitrary length, this check polls |
elessair | 0:f269e3021894 | 526 | * the hardware entropy source twice and compares the result to ensure they |
elessair | 0:f269e3021894 | 527 | * are not equal. |
elessair | 0:f269e3021894 | 528 | * - The error code returned by the entropy source is not an error. |
elessair | 0:f269e3021894 | 529 | */ |
elessair | 0:f269e3021894 | 530 | int mbedtls_entropy_source_self_test( int verbose ) |
elessair | 0:f269e3021894 | 531 | { |
elessair | 0:f269e3021894 | 532 | int ret = 0; |
elessair | 0:f269e3021894 | 533 | unsigned char buf0[2 * sizeof( unsigned long long int )]; |
elessair | 0:f269e3021894 | 534 | unsigned char buf1[2 * sizeof( unsigned long long int )]; |
elessair | 0:f269e3021894 | 535 | |
elessair | 0:f269e3021894 | 536 | if( verbose != 0 ) |
elessair | 0:f269e3021894 | 537 | mbedtls_printf( " ENTROPY_BIAS test: " ); |
elessair | 0:f269e3021894 | 538 | |
elessair | 0:f269e3021894 | 539 | memset( buf0, 0x00, sizeof( buf0 ) ); |
elessair | 0:f269e3021894 | 540 | memset( buf1, 0x00, sizeof( buf1 ) ); |
elessair | 0:f269e3021894 | 541 | |
elessair | 0:f269e3021894 | 542 | if( ( ret = mbedtls_entropy_source_self_test_gather( buf0, sizeof( buf0 ) ) ) != 0 ) |
elessair | 0:f269e3021894 | 543 | goto cleanup; |
elessair | 0:f269e3021894 | 544 | if( ( ret = mbedtls_entropy_source_self_test_gather( buf1, sizeof( buf1 ) ) ) != 0 ) |
elessair | 0:f269e3021894 | 545 | goto cleanup; |
elessair | 0:f269e3021894 | 546 | |
elessair | 0:f269e3021894 | 547 | /* Make sure that the returned values are not all 0 or 1 */ |
elessair | 0:f269e3021894 | 548 | if( ( ret = mbedtls_entropy_source_self_test_check_bits( buf0, sizeof( buf0 ) ) ) != 0 ) |
elessair | 0:f269e3021894 | 549 | goto cleanup; |
elessair | 0:f269e3021894 | 550 | if( ( ret = mbedtls_entropy_source_self_test_check_bits( buf1, sizeof( buf1 ) ) ) != 0 ) |
elessair | 0:f269e3021894 | 551 | goto cleanup; |
elessair | 0:f269e3021894 | 552 | |
elessair | 0:f269e3021894 | 553 | /* Make sure that the entropy source is not returning values in a |
elessair | 0:f269e3021894 | 554 | * pattern */ |
elessair | 0:f269e3021894 | 555 | ret = memcmp( buf0, buf1, sizeof( buf0 ) ) == 0; |
elessair | 0:f269e3021894 | 556 | |
elessair | 0:f269e3021894 | 557 | cleanup: |
elessair | 0:f269e3021894 | 558 | if( verbose != 0 ) |
elessair | 0:f269e3021894 | 559 | { |
elessair | 0:f269e3021894 | 560 | if( ret != 0 ) |
elessair | 0:f269e3021894 | 561 | mbedtls_printf( "failed\n" ); |
elessair | 0:f269e3021894 | 562 | else |
elessair | 0:f269e3021894 | 563 | mbedtls_printf( "passed\n" ); |
elessair | 0:f269e3021894 | 564 | |
elessair | 0:f269e3021894 | 565 | mbedtls_printf( "\n" ); |
elessair | 0:f269e3021894 | 566 | } |
elessair | 0:f269e3021894 | 567 | |
elessair | 0:f269e3021894 | 568 | return( ret != 0 ); |
elessair | 0:f269e3021894 | 569 | } |
elessair | 0:f269e3021894 | 570 | |
elessair | 0:f269e3021894 | 571 | #endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */ |
elessair | 0:f269e3021894 | 572 | |
elessair | 0:f269e3021894 | 573 | /* |
elessair | 0:f269e3021894 | 574 | * The actual entropy quality is hard to test, but we can at least |
elessair | 0:f269e3021894 | 575 | * test that the functions don't cause errors and write the correct |
elessair | 0:f269e3021894 | 576 | * amount of data to buffers. |
elessair | 0:f269e3021894 | 577 | */ |
elessair | 0:f269e3021894 | 578 | int mbedtls_entropy_self_test( int verbose ) |
elessair | 0:f269e3021894 | 579 | { |
elessair | 0:f269e3021894 | 580 | int ret = 1; |
elessair | 0:f269e3021894 | 581 | #if !defined(MBEDTLS_TEST_NULL_ENTROPY) |
elessair | 0:f269e3021894 | 582 | mbedtls_entropy_context ctx; |
elessair | 0:f269e3021894 | 583 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 }; |
elessair | 0:f269e3021894 | 584 | unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 }; |
elessair | 0:f269e3021894 | 585 | size_t i, j; |
elessair | 0:f269e3021894 | 586 | #endif /* !MBEDTLS_TEST_NULL_ENTROPY */ |
elessair | 0:f269e3021894 | 587 | |
elessair | 0:f269e3021894 | 588 | if( verbose != 0 ) |
elessair | 0:f269e3021894 | 589 | mbedtls_printf( " ENTROPY test: " ); |
elessair | 0:f269e3021894 | 590 | |
elessair | 0:f269e3021894 | 591 | #if !defined(MBEDTLS_TEST_NULL_ENTROPY) |
elessair | 0:f269e3021894 | 592 | mbedtls_entropy_init( &ctx ); |
elessair | 0:f269e3021894 | 593 | |
elessair | 0:f269e3021894 | 594 | /* First do a gather to make sure we have default sources */ |
elessair | 0:f269e3021894 | 595 | if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 ) |
elessair | 0:f269e3021894 | 596 | goto cleanup; |
elessair | 0:f269e3021894 | 597 | |
elessair | 0:f269e3021894 | 598 | ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16, |
elessair | 0:f269e3021894 | 599 | MBEDTLS_ENTROPY_SOURCE_WEAK ); |
elessair | 0:f269e3021894 | 600 | if( ret != 0 ) |
elessair | 0:f269e3021894 | 601 | goto cleanup; |
elessair | 0:f269e3021894 | 602 | |
elessair | 0:f269e3021894 | 603 | if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 ) |
elessair | 0:f269e3021894 | 604 | goto cleanup; |
elessair | 0:f269e3021894 | 605 | |
elessair | 0:f269e3021894 | 606 | /* |
elessair | 0:f269e3021894 | 607 | * To test that mbedtls_entropy_func writes correct number of bytes: |
elessair | 0:f269e3021894 | 608 | * - use the whole buffer and rely on ASan to detect overruns |
elessair | 0:f269e3021894 | 609 | * - collect entropy 8 times and OR the result in an accumulator: |
elessair | 0:f269e3021894 | 610 | * any byte should then be 0 with probably 2^(-64), so requiring |
elessair | 0:f269e3021894 | 611 | * each of the 32 or 64 bytes to be non-zero has a false failure rate |
elessair | 0:f269e3021894 | 612 | * of at most 2^(-58) which is acceptable. |
elessair | 0:f269e3021894 | 613 | */ |
elessair | 0:f269e3021894 | 614 | for( i = 0; i < 8; i++ ) |
elessair | 0:f269e3021894 | 615 | { |
elessair | 0:f269e3021894 | 616 | if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 ) |
elessair | 0:f269e3021894 | 617 | goto cleanup; |
elessair | 0:f269e3021894 | 618 | |
elessair | 0:f269e3021894 | 619 | for( j = 0; j < sizeof( buf ); j++ ) |
elessair | 0:f269e3021894 | 620 | acc[j] |= buf[j]; |
elessair | 0:f269e3021894 | 621 | } |
elessair | 0:f269e3021894 | 622 | |
elessair | 0:f269e3021894 | 623 | for( j = 0; j < sizeof( buf ); j++ ) |
elessair | 0:f269e3021894 | 624 | { |
elessair | 0:f269e3021894 | 625 | if( acc[j] == 0 ) |
elessair | 0:f269e3021894 | 626 | { |
elessair | 0:f269e3021894 | 627 | ret = 1; |
elessair | 0:f269e3021894 | 628 | goto cleanup; |
elessair | 0:f269e3021894 | 629 | } |
elessair | 0:f269e3021894 | 630 | } |
elessair | 0:f269e3021894 | 631 | |
elessair | 0:f269e3021894 | 632 | #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT) |
elessair | 0:f269e3021894 | 633 | if( ( ret = mbedtls_entropy_source_self_test( 0 ) ) != 0 ) |
elessair | 0:f269e3021894 | 634 | goto cleanup; |
elessair | 0:f269e3021894 | 635 | #endif |
elessair | 0:f269e3021894 | 636 | |
elessair | 0:f269e3021894 | 637 | cleanup: |
elessair | 0:f269e3021894 | 638 | mbedtls_entropy_free( &ctx ); |
elessair | 0:f269e3021894 | 639 | #endif /* !MBEDTLS_TEST_NULL_ENTROPY */ |
elessair | 0:f269e3021894 | 640 | |
elessair | 0:f269e3021894 | 641 | if( verbose != 0 ) |
elessair | 0:f269e3021894 | 642 | { |
elessair | 0:f269e3021894 | 643 | if( ret != 0 ) |
elessair | 0:f269e3021894 | 644 | mbedtls_printf( "failed\n" ); |
elessair | 0:f269e3021894 | 645 | else |
elessair | 0:f269e3021894 | 646 | mbedtls_printf( "passed\n" ); |
elessair | 0:f269e3021894 | 647 | |
elessair | 0:f269e3021894 | 648 | mbedtls_printf( "\n" ); |
elessair | 0:f269e3021894 | 649 | } |
elessair | 0:f269e3021894 | 650 | |
elessair | 0:f269e3021894 | 651 | return( ret != 0 ); |
elessair | 0:f269e3021894 | 652 | } |
elessair | 0:f269e3021894 | 653 | #endif /* MBEDTLS_SELF_TEST */ |
elessair | 0:f269e3021894 | 654 | |
elessair | 0:f269e3021894 | 655 | #endif /* MBEDTLS_ENTROPY_C */ |