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