| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Entropy accumulator implementation |
| 3 | * |
| Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 5 | * |
| Manuel Pégourié-Gonnard | 967a2a5 | 2015-01-22 14:28:16 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (http://www.polarssl.org) |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 8 | * |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | */ |
| 23 | |
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #if !defined(POLARSSL_CONFIG_FILE) |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 25 | #include "polarssl/config.h" |
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #else |
| 27 | #include POLARSSL_CONFIG_FILE |
| 28 | #endif |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 29 | |
| 30 | #if defined(POLARSSL_ENTROPY_C) |
| 31 | |
| 32 | #include "polarssl/entropy.h" |
| 33 | #include "polarssl/entropy_poll.h" |
| 34 | |
| Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 35 | #if defined(POLARSSL_FS_IO) |
| 36 | #include <stdio.h> |
| 37 | #endif |
| 38 | |
| Paul Bakker | 28c7e7f | 2011-12-15 19:49:30 +0000 | [diff] [blame] | 39 | #if defined(POLARSSL_HAVEGE_C) |
| 40 | #include "polarssl/havege.h" |
| 41 | #endif |
| 42 | |
| Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 43 | /* Implementation that should never be optimized out by the compiler */ |
| 44 | static void polarssl_zeroize( void *v, size_t n ) { |
| 45 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 46 | } |
| 47 | |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 48 | #define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */ |
| 49 | |
| 50 | void entropy_init( entropy_context *ctx ) |
| 51 | { |
| 52 | memset( ctx, 0, sizeof(entropy_context) ); |
| 53 | |
| Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 54 | #if defined(POLARSSL_THREADING_C) |
| 55 | polarssl_mutex_init( &ctx->mutex ); |
| 56 | #endif |
| 57 | |
| Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 58 | #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR) |
| Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 59 | sha512_starts( &ctx->accumulator, 0 ); |
| Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 60 | #else |
| 61 | sha256_starts( &ctx->accumulator, 0 ); |
| 62 | #endif |
| Paul Bakker | 43655f4 | 2011-12-15 20:11:16 +0000 | [diff] [blame] | 63 | #if defined(POLARSSL_HAVEGE_C) |
| 64 | havege_init( &ctx->havege_data ); |
| 65 | #endif |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 66 | |
| Paul Bakker | 43655f4 | 2011-12-15 20:11:16 +0000 | [diff] [blame] | 67 | #if !defined(POLARSSL_NO_DEFAULT_ENTROPY_SOURCES) |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 68 | #if !defined(POLARSSL_NO_PLATFORM_ENTROPY) |
| Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 69 | entropy_add_source( ctx, platform_entropy_poll, NULL, |
| 70 | ENTROPY_MIN_PLATFORM ); |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 71 | #endif |
| 72 | #if defined(POLARSSL_TIMING_C) |
| Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 73 | entropy_add_source( ctx, hardclock_poll, NULL, ENTROPY_MIN_HARDCLOCK ); |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 74 | #endif |
| Paul Bakker | 28c7e7f | 2011-12-15 19:49:30 +0000 | [diff] [blame] | 75 | #if defined(POLARSSL_HAVEGE_C) |
| Paul Bakker | 28c7e7f | 2011-12-15 19:49:30 +0000 | [diff] [blame] | 76 | entropy_add_source( ctx, havege_poll, &ctx->havege_data, |
| 77 | ENTROPY_MIN_HAVEGE ); |
| 78 | #endif |
| Paul Bakker | 43655f4 | 2011-12-15 20:11:16 +0000 | [diff] [blame] | 79 | #endif /* POLARSSL_NO_DEFAULT_ENTROPY_SOURCES */ |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 82 | void entropy_free( entropy_context *ctx ) |
| 83 | { |
| Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 84 | #if defined(POLARSSL_HAVEGE_C) |
| 85 | havege_free( &ctx->havege_data ); |
| 86 | #endif |
| Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 87 | polarssl_zeroize( ctx, sizeof( entropy_context ) ); |
| Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 88 | #if defined(POLARSSL_THREADING_C) |
| 89 | polarssl_mutex_free( &ctx->mutex ); |
| 90 | #endif |
| Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 93 | int entropy_add_source( entropy_context *ctx, |
| Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 94 | f_source_ptr f_source, void *p_source, |
| 95 | size_t threshold ) |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 96 | { |
| Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 97 | int index, ret = 0; |
| Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 98 | |
| Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 99 | #if defined(POLARSSL_THREADING_C) |
| 100 | if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 101 | return( ret ); |
| 102 | #endif |
| 103 | |
| 104 | index = ctx->source_count; |
| Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 105 | if( index >= ENTROPY_MAX_SOURCES ) |
| Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 106 | { |
| 107 | ret = POLARSSL_ERR_ENTROPY_MAX_SOURCES; |
| 108 | goto exit; |
| 109 | } |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 110 | |
| Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 111 | ctx->source[index].f_source = f_source; |
| 112 | ctx->source[index].p_source = p_source; |
| 113 | ctx->source[index].threshold = threshold; |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 114 | |
| 115 | ctx->source_count++; |
| 116 | |
| Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 117 | exit: |
| 118 | #if defined(POLARSSL_THREADING_C) |
| 119 | if( polarssl_mutex_unlock( &ctx->mutex ) != 0 ) |
| 120 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); |
| 121 | #endif |
| 122 | |
| 123 | return( ret ); |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Entropy accumulator update |
| 128 | */ |
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 129 | static int entropy_update( entropy_context *ctx, unsigned char source_id, |
| 130 | const unsigned char *data, size_t len ) |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 131 | { |
| 132 | unsigned char header[2]; |
| 133 | unsigned char tmp[ENTROPY_BLOCK_SIZE]; |
| 134 | size_t use_len = len; |
| 135 | const unsigned char *p = data; |
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 136 | |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 137 | if( use_len > ENTROPY_BLOCK_SIZE ) |
| 138 | { |
| Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 139 | #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR) |
| Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 140 | sha512( data, len, tmp, 0 ); |
| Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 141 | #else |
| 142 | sha256( data, len, tmp, 0 ); |
| 143 | #endif |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 144 | p = tmp; |
| 145 | use_len = ENTROPY_BLOCK_SIZE; |
| 146 | } |
| 147 | |
| 148 | header[0] = source_id; |
| 149 | header[1] = use_len & 0xFF; |
| 150 | |
| Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 151 | #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR) |
| Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 152 | sha512_update( &ctx->accumulator, header, 2 ); |
| 153 | sha512_update( &ctx->accumulator, p, use_len ); |
| Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 154 | #else |
| 155 | sha256_update( &ctx->accumulator, header, 2 ); |
| 156 | sha256_update( &ctx->accumulator, p, use_len ); |
| 157 | #endif |
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 158 | |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 159 | return( 0 ); |
| 160 | } |
| 161 | |
| 162 | int entropy_update_manual( entropy_context *ctx, |
| 163 | const unsigned char *data, size_t len ) |
| 164 | { |
| Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 165 | int ret; |
| 166 | |
| 167 | #if defined(POLARSSL_THREADING_C) |
| 168 | if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 169 | return( ret ); |
| 170 | #endif |
| 171 | |
| 172 | ret = entropy_update( ctx, ENTROPY_SOURCE_MANUAL, data, len ); |
| 173 | |
| 174 | #if defined(POLARSSL_THREADING_C) |
| 175 | if( polarssl_mutex_unlock( &ctx->mutex ) != 0 ) |
| 176 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); |
| 177 | #endif |
| 178 | |
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 179 | return( ret ); |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Run through the different sources to add entropy to our accumulator |
| 184 | */ |
| Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 185 | static int entropy_gather_internal( entropy_context *ctx ) |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 186 | { |
| 187 | int ret, i; |
| 188 | unsigned char buf[ENTROPY_MAX_GATHER]; |
| 189 | size_t olen; |
| Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 190 | |
| Paul Bakker | 43655f4 | 2011-12-15 20:11:16 +0000 | [diff] [blame] | 191 | if( ctx->source_count == 0 ) |
| 192 | return( POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED ); |
| 193 | |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 194 | /* |
| 195 | * Run through our entropy sources |
| 196 | */ |
| 197 | for( i = 0; i < ctx->source_count; i++ ) |
| 198 | { |
| 199 | olen = 0; |
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 200 | if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source, |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 201 | buf, ENTROPY_MAX_GATHER, &olen ) ) != 0 ) |
| 202 | { |
| 203 | return( ret ); |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Add if we actually gathered something |
| 208 | */ |
| 209 | if( olen > 0 ) |
| Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 210 | { |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 211 | entropy_update( ctx, (unsigned char) i, buf, olen ); |
| Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 212 | ctx->source[i].size += olen; |
| 213 | } |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | return( 0 ); |
| 217 | } |
| 218 | |
| Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 219 | /* |
| 220 | * Thread-safe wrapper for entropy_gather_internal() |
| 221 | */ |
| 222 | int entropy_gather( entropy_context *ctx ) |
| 223 | { |
| Paul Bakker | ddd427a | 2014-04-09 14:47:58 +0200 | [diff] [blame] | 224 | int ret; |
| Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 225 | |
| 226 | #if defined(POLARSSL_THREADING_C) |
| Paul Bakker | ddd427a | 2014-04-09 14:47:58 +0200 | [diff] [blame] | 227 | if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 228 | return( ret ); |
| Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 229 | #endif |
| 230 | |
| Paul Bakker | ddd427a | 2014-04-09 14:47:58 +0200 | [diff] [blame] | 231 | ret = entropy_gather_internal( ctx ); |
| Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 232 | |
| 233 | #if defined(POLARSSL_THREADING_C) |
| Paul Bakker | ddd427a | 2014-04-09 14:47:58 +0200 | [diff] [blame] | 234 | if( polarssl_mutex_unlock( &ctx->mutex ) != 0 ) |
| 235 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); |
| Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 236 | #endif |
| 237 | |
| Paul Bakker | ddd427a | 2014-04-09 14:47:58 +0200 | [diff] [blame] | 238 | return( ret ); |
| Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 239 | } |
| 240 | |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 241 | int entropy_func( void *data, unsigned char *output, size_t len ) |
| 242 | { |
| Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 243 | int ret, count = 0, i, reached; |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 244 | entropy_context *ctx = (entropy_context *) data; |
| 245 | unsigned char buf[ENTROPY_BLOCK_SIZE]; |
| 246 | |
| 247 | if( len > ENTROPY_BLOCK_SIZE ) |
| 248 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
| 249 | |
| Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 250 | #if defined(POLARSSL_THREADING_C) |
| 251 | if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 252 | return( ret ); |
| 253 | #endif |
| 254 | |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 255 | /* |
| 256 | * Always gather extra entropy before a call |
| 257 | */ |
| 258 | do |
| 259 | { |
| 260 | if( count++ > ENTROPY_MAX_LOOP ) |
| Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 261 | { |
| 262 | ret = POLARSSL_ERR_ENTROPY_SOURCE_FAILED; |
| 263 | goto exit; |
| 264 | } |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 265 | |
| Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 266 | if( ( ret = entropy_gather_internal( ctx ) ) != 0 ) |
| Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 267 | goto exit; |
| Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 268 | |
| 269 | reached = 0; |
| 270 | |
| 271 | for( i = 0; i < ctx->source_count; i++ ) |
| 272 | if( ctx->source[i].size >= ctx->source[i].threshold ) |
| 273 | reached++; |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 274 | } |
| Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 275 | while( reached != ctx->source_count ); |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 276 | |
| 277 | memset( buf, 0, ENTROPY_BLOCK_SIZE ); |
| 278 | |
| Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 279 | #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR) |
| Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 280 | sha512_finish( &ctx->accumulator, buf ); |
| 281 | |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 282 | /* |
| Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 283 | * Reset accumulator and counters and recycle existing entropy |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 284 | */ |
| Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 285 | memset( &ctx->accumulator, 0, sizeof( sha512_context ) ); |
| 286 | sha512_starts( &ctx->accumulator, 0 ); |
| 287 | sha512_update( &ctx->accumulator, buf, ENTROPY_BLOCK_SIZE ); |
| Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 288 | |
| 289 | /* |
| Paul Bakker | b13d3ff | 2014-03-26 12:51:25 +0100 | [diff] [blame] | 290 | * Perform second SHA-512 on entropy |
| Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 291 | */ |
| Paul Bakker | b13d3ff | 2014-03-26 12:51:25 +0100 | [diff] [blame] | 292 | sha512( buf, ENTROPY_BLOCK_SIZE, buf, 0 ); |
| 293 | #else /* POLARSSL_ENTROPY_SHA512_ACCUMULATOR */ |
| 294 | sha256_finish( &ctx->accumulator, buf ); |
| Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 295 | |
| 296 | /* |
| 297 | * Reset accumulator and counters and recycle existing entropy |
| 298 | */ |
| 299 | memset( &ctx->accumulator, 0, sizeof( sha256_context ) ); |
| 300 | sha256_starts( &ctx->accumulator, 0 ); |
| 301 | sha256_update( &ctx->accumulator, buf, ENTROPY_BLOCK_SIZE ); |
| Paul Bakker | b13d3ff | 2014-03-26 12:51:25 +0100 | [diff] [blame] | 302 | |
| 303 | /* |
| 304 | * Perform second SHA-256 on entropy |
| 305 | */ |
| 306 | sha256( buf, ENTROPY_BLOCK_SIZE, buf, 0 ); |
| Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 307 | #endif /* POLARSSL_ENTROPY_SHA512_ACCUMULATOR */ |
| Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 308 | |
| 309 | for( i = 0; i < ctx->source_count; i++ ) |
| 310 | ctx->source[i].size = 0; |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 311 | |
| 312 | memcpy( output, buf, len ); |
| 313 | |
| Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 314 | ret = 0; |
| 315 | |
| 316 | exit: |
| 317 | #if defined(POLARSSL_THREADING_C) |
| 318 | if( polarssl_mutex_unlock( &ctx->mutex ) != 0 ) |
| 319 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); |
| 320 | #endif |
| 321 | |
| 322 | return( ret ); |
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 325 | #if defined(POLARSSL_FS_IO) |
| 326 | int entropy_write_seed_file( entropy_context *ctx, const char *path ) |
| 327 | { |
| 328 | int ret = POLARSSL_ERR_ENTROPY_FILE_IO_ERROR; |
| 329 | FILE *f; |
| 330 | unsigned char buf[ENTROPY_BLOCK_SIZE]; |
| 331 | |
| 332 | if( ( f = fopen( path, "wb" ) ) == NULL ) |
| 333 | return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR ); |
| 334 | |
| 335 | if( ( ret = entropy_func( ctx, buf, ENTROPY_BLOCK_SIZE ) ) != 0 ) |
| 336 | goto exit; |
| 337 | |
| 338 | if( fwrite( buf, 1, ENTROPY_BLOCK_SIZE, f ) != ENTROPY_BLOCK_SIZE ) |
| 339 | { |
| 340 | ret = POLARSSL_ERR_ENTROPY_FILE_IO_ERROR; |
| 341 | goto exit; |
| 342 | } |
| 343 | |
| 344 | ret = 0; |
| 345 | |
| 346 | exit: |
| 347 | fclose( f ); |
| 348 | return( ret ); |
| 349 | } |
| 350 | |
| 351 | int entropy_update_seed_file( entropy_context *ctx, const char *path ) |
| 352 | { |
| 353 | FILE *f; |
| 354 | size_t n; |
| 355 | unsigned char buf[ ENTROPY_MAX_SEED_SIZE ]; |
| 356 | |
| 357 | if( ( f = fopen( path, "rb" ) ) == NULL ) |
| 358 | return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR ); |
| 359 | |
| 360 | fseek( f, 0, SEEK_END ); |
| 361 | n = (size_t) ftell( f ); |
| 362 | fseek( f, 0, SEEK_SET ); |
| 363 | |
| 364 | if( n > ENTROPY_MAX_SEED_SIZE ) |
| 365 | n = ENTROPY_MAX_SEED_SIZE; |
| 366 | |
| 367 | if( fread( buf, 1, n, f ) != n ) |
| 368 | { |
| 369 | fclose( f ); |
| 370 | return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR ); |
| 371 | } |
| 372 | |
| 373 | fclose( f ); |
| 374 | |
| 375 | entropy_update_manual( ctx, buf, n ); |
| 376 | |
| 377 | return( entropy_write_seed_file( ctx, path ) ); |
| 378 | } |
| 379 | #endif /* POLARSSL_FS_IO */ |
| 380 | |
| Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 381 | #if defined(POLARSSL_SELF_TEST) |
| 382 | |
| 383 | #if defined(POLARSSL_PLATFORM_C) |
| 384 | #include "polarssl/platform.h" |
| 385 | #else |
| Paul Bakker | 5b11d02 | 2014-07-10 13:54:38 +0200 | [diff] [blame] | 386 | #include <stdio.h> |
| Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 387 | #define polarssl_printf printf |
| 388 | #endif |
| 389 | |
| 390 | /* |
| 391 | * Dummy source function |
| 392 | */ |
| 393 | static int entropy_dummy_source( void *data, unsigned char *output, |
| 394 | size_t len, size_t *olen ) |
| 395 | { |
| 396 | ((void) data); |
| 397 | |
| 398 | memset( output, 0x2a, len ); |
| 399 | *olen = len; |
| 400 | |
| 401 | return( 0 ); |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * The actual entropy quality is hard to test, but we can at least |
| 406 | * test that the functions don't cause errors and write the correct |
| 407 | * amount of data to buffers. |
| 408 | */ |
| 409 | int entropy_self_test( int verbose ) |
| 410 | { |
| 411 | int ret = 0; |
| 412 | entropy_context ctx; |
| 413 | unsigned char buf[ENTROPY_BLOCK_SIZE] = { 0 }; |
| 414 | unsigned char acc[ENTROPY_BLOCK_SIZE] = { 0 }; |
| 415 | size_t i, j; |
| 416 | |
| 417 | if( verbose != 0 ) |
| 418 | polarssl_printf( " ENTROPY test: " ); |
| 419 | |
| 420 | entropy_init( &ctx ); |
| 421 | |
| 422 | ret = entropy_add_source( &ctx, entropy_dummy_source, NULL, 16 ); |
| 423 | if( ret != 0 ) |
| 424 | goto cleanup; |
| 425 | |
| 426 | if( ( ret = entropy_gather( &ctx ) ) != 0 ) |
| 427 | goto cleanup; |
| 428 | |
| 429 | if( ( ret = entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 ) |
| 430 | goto cleanup; |
| 431 | |
| 432 | /* |
| 433 | * To test that entropy_func writes correct number of bytes: |
| 434 | * - use the whole buffer and rely on ASan to detect overruns |
| 435 | * - collect entropy 8 times and OR the result in an accumulator: |
| 436 | * any byte should then be 0 with probably 2^(-64), so requiring |
| 437 | * each of the 32 or 64 bytes to be non-zero has a false failure rate |
| 438 | * of at most 2^(-58) which is acceptable. |
| 439 | */ |
| 440 | for( i = 0; i < 8; i++ ) |
| 441 | { |
| 442 | if( ( ret = entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 ) |
| 443 | goto cleanup; |
| 444 | |
| 445 | for( j = 0; j < sizeof( buf ); j++ ) |
| 446 | acc[j] |= buf[j]; |
| 447 | } |
| 448 | |
| 449 | for( j = 0; j < sizeof( buf ); j++ ) |
| 450 | { |
| 451 | if( acc[j] == 0 ) |
| 452 | { |
| 453 | ret = 1; |
| 454 | goto cleanup; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | cleanup: |
| 459 | entropy_free( &ctx ); |
| 460 | |
| 461 | if( verbose != 0 ) |
| 462 | { |
| 463 | if( ret != 0 ) |
| 464 | polarssl_printf( "failed\n" ); |
| 465 | else |
| 466 | polarssl_printf( "passed\n" ); |
| 467 | |
| 468 | polarssl_printf( "\n" ); |
| 469 | } |
| 470 | |
| 471 | return( ret != 0 ); |
| 472 | } |
| 473 | #endif /* POLARSSL_SELF_TEST */ |
| 474 | |
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 475 | #endif /* POLARSSL_ENTROPY_C */ |