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