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