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 | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 86 | polarssl_zeroize( ctx, sizeof( entropy_context ) ); |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 87 | #if defined(POLARSSL_THREADING_C) |
| 88 | polarssl_mutex_free( &ctx->mutex ); |
| 89 | #endif |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 92 | int entropy_add_source( entropy_context *ctx, |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 93 | f_source_ptr f_source, void *p_source, |
| 94 | size_t threshold ) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 95 | { |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 96 | int index, ret = 0; |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 97 | |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 98 | #if defined(POLARSSL_THREADING_C) |
| 99 | if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 100 | return( ret ); |
| 101 | #endif |
| 102 | |
| 103 | index = ctx->source_count; |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 104 | if( index >= ENTROPY_MAX_SOURCES ) |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 105 | { |
| 106 | ret = POLARSSL_ERR_ENTROPY_MAX_SOURCES; |
| 107 | goto exit; |
| 108 | } |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 109 | |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 110 | ctx->source[index].f_source = f_source; |
| 111 | ctx->source[index].p_source = p_source; |
| 112 | ctx->source[index].threshold = threshold; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 113 | |
| 114 | ctx->source_count++; |
| 115 | |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 116 | exit: |
| 117 | #if defined(POLARSSL_THREADING_C) |
| 118 | if( polarssl_mutex_unlock( &ctx->mutex ) != 0 ) |
| 119 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); |
| 120 | #endif |
| 121 | |
| 122 | return( ret ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Entropy accumulator update |
| 127 | */ |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 128 | static int entropy_update( entropy_context *ctx, unsigned char source_id, |
| 129 | const unsigned char *data, size_t len ) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 130 | { |
| 131 | unsigned char header[2]; |
| 132 | unsigned char tmp[ENTROPY_BLOCK_SIZE]; |
| 133 | size_t use_len = len; |
| 134 | const unsigned char *p = data; |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 135 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 136 | if( use_len > ENTROPY_BLOCK_SIZE ) |
| 137 | { |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 138 | #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 139 | sha512( data, len, tmp, 0 ); |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 140 | #else |
| 141 | sha256( data, len, tmp, 0 ); |
| 142 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 143 | p = tmp; |
| 144 | use_len = ENTROPY_BLOCK_SIZE; |
| 145 | } |
| 146 | |
| 147 | header[0] = source_id; |
| 148 | header[1] = use_len & 0xFF; |
| 149 | |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 150 | #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 151 | sha512_update( &ctx->accumulator, header, 2 ); |
| 152 | sha512_update( &ctx->accumulator, p, use_len ); |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 153 | #else |
| 154 | sha256_update( &ctx->accumulator, header, 2 ); |
| 155 | sha256_update( &ctx->accumulator, p, use_len ); |
| 156 | #endif |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 157 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 158 | return( 0 ); |
| 159 | } |
| 160 | |
| 161 | int entropy_update_manual( entropy_context *ctx, |
| 162 | const unsigned char *data, size_t len ) |
| 163 | { |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 164 | int ret; |
| 165 | |
| 166 | #if defined(POLARSSL_THREADING_C) |
| 167 | if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 168 | return( ret ); |
| 169 | #endif |
| 170 | |
| 171 | ret = entropy_update( ctx, ENTROPY_SOURCE_MANUAL, data, len ); |
| 172 | |
| 173 | #if defined(POLARSSL_THREADING_C) |
| 174 | if( polarssl_mutex_unlock( &ctx->mutex ) != 0 ) |
| 175 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); |
| 176 | #endif |
| 177 | |
| 178 | return ( ret ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Run through the different sources to add entropy to our accumulator |
| 183 | */ |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 184 | static int entropy_gather_internal( entropy_context *ctx ) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 185 | { |
| 186 | int ret, i; |
| 187 | unsigned char buf[ENTROPY_MAX_GATHER]; |
| 188 | size_t olen; |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 189 | |
Paul Bakker | 43655f4 | 2011-12-15 20:11:16 +0000 | [diff] [blame] | 190 | if( ctx->source_count == 0 ) |
| 191 | return( POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED ); |
| 192 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 193 | /* |
| 194 | * Run through our entropy sources |
| 195 | */ |
| 196 | for( i = 0; i < ctx->source_count; i++ ) |
| 197 | { |
| 198 | olen = 0; |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 199 | if ( ( ret = ctx->source[i].f_source( ctx->source[i].p_source, |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 200 | buf, ENTROPY_MAX_GATHER, &olen ) ) != 0 ) |
| 201 | { |
| 202 | return( ret ); |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Add if we actually gathered something |
| 207 | */ |
| 208 | if( olen > 0 ) |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 209 | { |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 210 | entropy_update( ctx, (unsigned char) i, buf, olen ); |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 211 | ctx->source[i].size += olen; |
| 212 | } |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | return( 0 ); |
| 216 | } |
| 217 | |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 218 | /* |
| 219 | * Thread-safe wrapper for entropy_gather_internal() |
| 220 | */ |
| 221 | int entropy_gather( entropy_context *ctx ) |
| 222 | { |
Paul Bakker | ddd427a | 2014-04-09 14:47:58 +0200 | [diff] [blame] | 223 | int ret; |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 224 | |
| 225 | #if defined(POLARSSL_THREADING_C) |
Paul Bakker | ddd427a | 2014-04-09 14:47:58 +0200 | [diff] [blame] | 226 | if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 227 | return( ret ); |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 228 | #endif |
| 229 | |
Paul Bakker | ddd427a | 2014-04-09 14:47:58 +0200 | [diff] [blame] | 230 | ret = entropy_gather_internal( ctx ); |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 231 | |
| 232 | #if defined(POLARSSL_THREADING_C) |
Paul Bakker | ddd427a | 2014-04-09 14:47:58 +0200 | [diff] [blame] | 233 | if( polarssl_mutex_unlock( &ctx->mutex ) != 0 ) |
| 234 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 235 | #endif |
| 236 | |
Paul Bakker | ddd427a | 2014-04-09 14:47:58 +0200 | [diff] [blame] | 237 | return( ret ); |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 240 | int entropy_func( void *data, unsigned char *output, size_t len ) |
| 241 | { |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 242 | int ret, count = 0, i, reached; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 243 | entropy_context *ctx = (entropy_context *) data; |
| 244 | unsigned char buf[ENTROPY_BLOCK_SIZE]; |
| 245 | |
| 246 | if( len > ENTROPY_BLOCK_SIZE ) |
| 247 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
| 248 | |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 249 | #if defined(POLARSSL_THREADING_C) |
| 250 | if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 251 | return( ret ); |
| 252 | #endif |
| 253 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 254 | /* |
| 255 | * Always gather extra entropy before a call |
| 256 | */ |
| 257 | do |
| 258 | { |
| 259 | if( count++ > ENTROPY_MAX_LOOP ) |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 260 | { |
| 261 | ret = POLARSSL_ERR_ENTROPY_SOURCE_FAILED; |
| 262 | goto exit; |
| 263 | } |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 264 | |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 265 | if( ( ret = entropy_gather_internal( ctx ) ) != 0 ) |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 266 | goto exit; |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 267 | |
| 268 | reached = 0; |
| 269 | |
| 270 | for( i = 0; i < ctx->source_count; i++ ) |
| 271 | if( ctx->source[i].size >= ctx->source[i].threshold ) |
| 272 | reached++; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 273 | } |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 274 | while( reached != ctx->source_count ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 275 | |
| 276 | memset( buf, 0, ENTROPY_BLOCK_SIZE ); |
| 277 | |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 278 | #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 279 | sha512_finish( &ctx->accumulator, buf ); |
| 280 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 281 | /* |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 282 | * Reset accumulator and counters and recycle existing entropy |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 283 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 284 | memset( &ctx->accumulator, 0, sizeof( sha512_context ) ); |
| 285 | sha512_starts( &ctx->accumulator, 0 ); |
| 286 | sha512_update( &ctx->accumulator, buf, ENTROPY_BLOCK_SIZE ); |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 287 | |
| 288 | /* |
Paul Bakker | b13d3ff | 2014-03-26 12:51:25 +0100 | [diff] [blame] | 289 | * Perform second SHA-512 on entropy |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 290 | */ |
Paul Bakker | b13d3ff | 2014-03-26 12:51:25 +0100 | [diff] [blame] | 291 | sha512( buf, ENTROPY_BLOCK_SIZE, buf, 0 ); |
| 292 | #else /* POLARSSL_ENTROPY_SHA512_ACCUMULATOR */ |
| 293 | sha256_finish( &ctx->accumulator, buf ); |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 294 | |
| 295 | /* |
| 296 | * Reset accumulator and counters and recycle existing entropy |
| 297 | */ |
| 298 | memset( &ctx->accumulator, 0, sizeof( sha256_context ) ); |
| 299 | sha256_starts( &ctx->accumulator, 0 ); |
| 300 | sha256_update( &ctx->accumulator, buf, ENTROPY_BLOCK_SIZE ); |
Paul Bakker | b13d3ff | 2014-03-26 12:51:25 +0100 | [diff] [blame] | 301 | |
| 302 | /* |
| 303 | * Perform second SHA-256 on entropy |
| 304 | */ |
| 305 | sha256( buf, ENTROPY_BLOCK_SIZE, buf, 0 ); |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 306 | #endif /* POLARSSL_ENTROPY_SHA512_ACCUMULATOR */ |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 307 | |
| 308 | for( i = 0; i < ctx->source_count; i++ ) |
| 309 | ctx->source[i].size = 0; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 310 | |
| 311 | memcpy( output, buf, len ); |
| 312 | |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 313 | ret = 0; |
| 314 | |
| 315 | exit: |
| 316 | #if defined(POLARSSL_THREADING_C) |
| 317 | if( polarssl_mutex_unlock( &ctx->mutex ) != 0 ) |
| 318 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); |
| 319 | #endif |
| 320 | |
| 321 | return( ret ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 324 | #if defined(POLARSSL_FS_IO) |
| 325 | int entropy_write_seed_file( entropy_context *ctx, const char *path ) |
| 326 | { |
| 327 | int ret = POLARSSL_ERR_ENTROPY_FILE_IO_ERROR; |
| 328 | FILE *f; |
| 329 | unsigned char buf[ENTROPY_BLOCK_SIZE]; |
| 330 | |
| 331 | if( ( f = fopen( path, "wb" ) ) == NULL ) |
| 332 | return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR ); |
| 333 | |
| 334 | if( ( ret = entropy_func( ctx, buf, ENTROPY_BLOCK_SIZE ) ) != 0 ) |
| 335 | goto exit; |
| 336 | |
| 337 | if( fwrite( buf, 1, ENTROPY_BLOCK_SIZE, f ) != ENTROPY_BLOCK_SIZE ) |
| 338 | { |
| 339 | ret = POLARSSL_ERR_ENTROPY_FILE_IO_ERROR; |
| 340 | goto exit; |
| 341 | } |
| 342 | |
| 343 | ret = 0; |
| 344 | |
| 345 | exit: |
| 346 | fclose( f ); |
| 347 | return( ret ); |
| 348 | } |
| 349 | |
| 350 | int entropy_update_seed_file( entropy_context *ctx, const char *path ) |
| 351 | { |
| 352 | FILE *f; |
| 353 | size_t n; |
| 354 | unsigned char buf[ ENTROPY_MAX_SEED_SIZE ]; |
| 355 | |
| 356 | if( ( f = fopen( path, "rb" ) ) == NULL ) |
| 357 | return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR ); |
| 358 | |
| 359 | fseek( f, 0, SEEK_END ); |
| 360 | n = (size_t) ftell( f ); |
| 361 | fseek( f, 0, SEEK_SET ); |
| 362 | |
| 363 | if( n > ENTROPY_MAX_SEED_SIZE ) |
| 364 | n = ENTROPY_MAX_SEED_SIZE; |
| 365 | |
| 366 | if( fread( buf, 1, n, f ) != n ) |
| 367 | { |
| 368 | fclose( f ); |
| 369 | return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR ); |
| 370 | } |
| 371 | |
| 372 | fclose( f ); |
| 373 | |
| 374 | entropy_update_manual( ctx, buf, n ); |
| 375 | |
| 376 | return( entropy_write_seed_file( ctx, path ) ); |
| 377 | } |
| 378 | #endif /* POLARSSL_FS_IO */ |
| 379 | |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 380 | #if defined(POLARSSL_SELF_TEST) |
| 381 | |
| 382 | #if defined(POLARSSL_PLATFORM_C) |
| 383 | #include "polarssl/platform.h" |
| 384 | #else |
| 385 | #define polarssl_printf printf |
| 386 | #endif |
| 387 | |
| 388 | /* |
| 389 | * Dummy source function |
| 390 | */ |
| 391 | static int entropy_dummy_source( void *data, unsigned char *output, |
| 392 | size_t len, size_t *olen ) |
| 393 | { |
| 394 | ((void) data); |
| 395 | |
| 396 | memset( output, 0x2a, len ); |
| 397 | *olen = len; |
| 398 | |
| 399 | return( 0 ); |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | * The actual entropy quality is hard to test, but we can at least |
| 404 | * test that the functions don't cause errors and write the correct |
| 405 | * amount of data to buffers. |
| 406 | */ |
| 407 | int entropy_self_test( int verbose ) |
| 408 | { |
| 409 | int ret = 0; |
| 410 | entropy_context ctx; |
| 411 | unsigned char buf[ENTROPY_BLOCK_SIZE] = { 0 }; |
| 412 | unsigned char acc[ENTROPY_BLOCK_SIZE] = { 0 }; |
| 413 | size_t i, j; |
| 414 | |
| 415 | if( verbose != 0 ) |
| 416 | polarssl_printf( " ENTROPY test: " ); |
| 417 | |
| 418 | entropy_init( &ctx ); |
| 419 | |
| 420 | ret = entropy_add_source( &ctx, entropy_dummy_source, NULL, 16 ); |
| 421 | if( ret != 0 ) |
| 422 | goto cleanup; |
| 423 | |
| 424 | if( ( ret = entropy_gather( &ctx ) ) != 0 ) |
| 425 | goto cleanup; |
| 426 | |
| 427 | if( ( ret = entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 ) |
| 428 | goto cleanup; |
| 429 | |
| 430 | /* |
| 431 | * To test that entropy_func writes correct number of bytes: |
| 432 | * - use the whole buffer and rely on ASan to detect overruns |
| 433 | * - collect entropy 8 times and OR the result in an accumulator: |
| 434 | * any byte should then be 0 with probably 2^(-64), so requiring |
| 435 | * each of the 32 or 64 bytes to be non-zero has a false failure rate |
| 436 | * of at most 2^(-58) which is acceptable. |
| 437 | */ |
| 438 | for( i = 0; i < 8; i++ ) |
| 439 | { |
| 440 | if( ( ret = entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 ) |
| 441 | goto cleanup; |
| 442 | |
| 443 | for( j = 0; j < sizeof( buf ); j++ ) |
| 444 | acc[j] |= buf[j]; |
| 445 | } |
| 446 | |
| 447 | for( j = 0; j < sizeof( buf ); j++ ) |
| 448 | { |
| 449 | if( acc[j] == 0 ) |
| 450 | { |
| 451 | ret = 1; |
| 452 | goto cleanup; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | cleanup: |
| 457 | entropy_free( &ctx ); |
| 458 | |
| 459 | if( verbose != 0 ) |
| 460 | { |
| 461 | if( ret != 0 ) |
| 462 | polarssl_printf( "failed\n" ); |
| 463 | else |
| 464 | polarssl_printf( "passed\n" ); |
| 465 | |
| 466 | polarssl_printf( "\n" ); |
| 467 | } |
| 468 | |
| 469 | return( ret != 0 ); |
| 470 | } |
| 471 | #endif /* POLARSSL_SELF_TEST */ |
| 472 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 473 | #endif /* POLARSSL_ENTROPY_C */ |