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