Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Entropy accumulator implementation |
| 3 | * |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, 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 | |
| 26 | #include "polarssl/config.h" |
| 27 | |
| 28 | #if defined(POLARSSL_ENTROPY_C) |
| 29 | |
| 30 | #include "polarssl/entropy.h" |
| 31 | #include "polarssl/entropy_poll.h" |
| 32 | |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 33 | #if defined(POLARSSL_FS_IO) |
| 34 | #include <stdio.h> |
| 35 | #endif |
| 36 | |
Paul Bakker | 28c7e7f | 2011-12-15 19:49:30 +0000 | [diff] [blame] | 37 | #if defined(POLARSSL_HAVEGE_C) |
| 38 | #include "polarssl/havege.h" |
| 39 | #endif |
| 40 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 41 | #define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */ |
| 42 | |
| 43 | void entropy_init( entropy_context *ctx ) |
| 44 | { |
| 45 | memset( ctx, 0, sizeof(entropy_context) ); |
| 46 | |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 47 | #if defined(POLARSSL_THREADING_C) |
| 48 | polarssl_mutex_init( &ctx->mutex ); |
| 49 | #endif |
| 50 | |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 51 | #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 52 | sha512_starts( &ctx->accumulator, 0 ); |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 53 | #else |
| 54 | sha256_starts( &ctx->accumulator, 0 ); |
| 55 | #endif |
Paul Bakker | 43655f4 | 2011-12-15 20:11:16 +0000 | [diff] [blame] | 56 | #if defined(POLARSSL_HAVEGE_C) |
| 57 | havege_init( &ctx->havege_data ); |
| 58 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 59 | |
Paul Bakker | 43655f4 | 2011-12-15 20:11:16 +0000 | [diff] [blame] | 60 | #if !defined(POLARSSL_NO_DEFAULT_ENTROPY_SOURCES) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 61 | #if !defined(POLARSSL_NO_PLATFORM_ENTROPY) |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 62 | entropy_add_source( ctx, platform_entropy_poll, NULL, |
| 63 | ENTROPY_MIN_PLATFORM ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 64 | #endif |
| 65 | #if defined(POLARSSL_TIMING_C) |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 66 | entropy_add_source( ctx, hardclock_poll, NULL, ENTROPY_MIN_HARDCLOCK ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 67 | #endif |
Paul Bakker | 28c7e7f | 2011-12-15 19:49:30 +0000 | [diff] [blame] | 68 | #if defined(POLARSSL_HAVEGE_C) |
Paul Bakker | 28c7e7f | 2011-12-15 19:49:30 +0000 | [diff] [blame] | 69 | entropy_add_source( ctx, havege_poll, &ctx->havege_data, |
| 70 | ENTROPY_MIN_HAVEGE ); |
| 71 | #endif |
Paul Bakker | 43655f4 | 2011-12-15 20:11:16 +0000 | [diff] [blame] | 72 | #endif /* POLARSSL_NO_DEFAULT_ENTROPY_SOURCES */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 75 | void entropy_free( entropy_context *ctx ) |
| 76 | { |
| 77 | ((void) ctx); |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 78 | #if defined(POLARSSL_THREADING_C) |
| 79 | polarssl_mutex_free( &ctx->mutex ); |
| 80 | #endif |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 83 | int entropy_add_source( entropy_context *ctx, |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 84 | f_source_ptr f_source, void *p_source, |
| 85 | size_t threshold ) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 86 | { |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 87 | int index, ret = 0; |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 88 | |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 89 | #if defined(POLARSSL_THREADING_C) |
| 90 | if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 91 | return( ret ); |
| 92 | #endif |
| 93 | |
| 94 | index = ctx->source_count; |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 95 | if( index >= ENTROPY_MAX_SOURCES ) |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 96 | { |
| 97 | ret = POLARSSL_ERR_ENTROPY_MAX_SOURCES; |
| 98 | goto exit; |
| 99 | } |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 100 | |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 101 | ctx->source[index].f_source = f_source; |
| 102 | ctx->source[index].p_source = p_source; |
| 103 | ctx->source[index].threshold = threshold; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 104 | |
| 105 | ctx->source_count++; |
| 106 | |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 107 | exit: |
| 108 | #if defined(POLARSSL_THREADING_C) |
| 109 | if( polarssl_mutex_unlock( &ctx->mutex ) != 0 ) |
| 110 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); |
| 111 | #endif |
| 112 | |
| 113 | return( ret ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Entropy accumulator update |
| 118 | */ |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 119 | static int entropy_update( entropy_context *ctx, unsigned char source_id, |
| 120 | const unsigned char *data, size_t len ) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 121 | { |
| 122 | unsigned char header[2]; |
| 123 | unsigned char tmp[ENTROPY_BLOCK_SIZE]; |
| 124 | size_t use_len = len; |
| 125 | const unsigned char *p = data; |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 126 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 127 | if( use_len > ENTROPY_BLOCK_SIZE ) |
| 128 | { |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 129 | #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 130 | sha512( data, len, tmp, 0 ); |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 131 | #else |
| 132 | sha256( data, len, tmp, 0 ); |
| 133 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 134 | p = tmp; |
| 135 | use_len = ENTROPY_BLOCK_SIZE; |
| 136 | } |
| 137 | |
| 138 | header[0] = source_id; |
| 139 | header[1] = use_len & 0xFF; |
| 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_update( &ctx->accumulator, header, 2 ); |
| 143 | sha512_update( &ctx->accumulator, p, use_len ); |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 144 | #else |
| 145 | sha256_update( &ctx->accumulator, header, 2 ); |
| 146 | sha256_update( &ctx->accumulator, p, use_len ); |
| 147 | #endif |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 148 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 149 | return( 0 ); |
| 150 | } |
| 151 | |
| 152 | int entropy_update_manual( entropy_context *ctx, |
| 153 | const unsigned char *data, size_t len ) |
| 154 | { |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 155 | int ret; |
| 156 | |
| 157 | #if defined(POLARSSL_THREADING_C) |
| 158 | if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 159 | return( ret ); |
| 160 | #endif |
| 161 | |
| 162 | ret = entropy_update( ctx, ENTROPY_SOURCE_MANUAL, data, len ); |
| 163 | |
| 164 | #if defined(POLARSSL_THREADING_C) |
| 165 | if( polarssl_mutex_unlock( &ctx->mutex ) != 0 ) |
| 166 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); |
| 167 | #endif |
| 168 | |
| 169 | return ( ret ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Run through the different sources to add entropy to our accumulator |
| 174 | */ |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 175 | static int entropy_gather_internal( entropy_context *ctx ) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 176 | { |
| 177 | int ret, i; |
| 178 | unsigned char buf[ENTROPY_MAX_GATHER]; |
| 179 | size_t olen; |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 180 | |
Paul Bakker | 43655f4 | 2011-12-15 20:11:16 +0000 | [diff] [blame] | 181 | if( ctx->source_count == 0 ) |
| 182 | return( POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED ); |
| 183 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 184 | /* |
| 185 | * Run through our entropy sources |
| 186 | */ |
| 187 | for( i = 0; i < ctx->source_count; i++ ) |
| 188 | { |
| 189 | olen = 0; |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 190 | if ( ( ret = ctx->source[i].f_source( ctx->source[i].p_source, |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 191 | buf, ENTROPY_MAX_GATHER, &olen ) ) != 0 ) |
| 192 | { |
| 193 | return( ret ); |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Add if we actually gathered something |
| 198 | */ |
| 199 | if( olen > 0 ) |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 200 | { |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 201 | entropy_update( ctx, (unsigned char) i, buf, olen ); |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 202 | ctx->source[i].size += olen; |
| 203 | } |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | return( 0 ); |
| 207 | } |
| 208 | |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 209 | /* |
| 210 | * Thread-safe wrapper for entropy_gather_internal() |
| 211 | */ |
| 212 | int entropy_gather( entropy_context *ctx ) |
| 213 | { |
| 214 | int ret; |
| 215 | |
| 216 | #if defined(POLARSSL_THREADING_C) |
| 217 | if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 218 | return( ret ); |
| 219 | #endif |
| 220 | |
| 221 | ret = entropy_gather_internal( ctx ); |
| 222 | |
| 223 | #if defined(POLARSSL_THREADING_C) |
| 224 | if( polarssl_mutex_unlock( &ctx->mutex ) != 0 ) |
| 225 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); |
| 226 | #endif |
| 227 | |
| 228 | return ( ret ); |
| 229 | } |
| 230 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 231 | int entropy_func( void *data, unsigned char *output, size_t len ) |
| 232 | { |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 233 | int ret, count = 0, i, reached; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 234 | entropy_context *ctx = (entropy_context *) data; |
| 235 | unsigned char buf[ENTROPY_BLOCK_SIZE]; |
| 236 | |
| 237 | if( len > ENTROPY_BLOCK_SIZE ) |
| 238 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
| 239 | |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 240 | #if defined(POLARSSL_THREADING_C) |
| 241 | if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 242 | return( ret ); |
| 243 | #endif |
| 244 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 245 | /* |
| 246 | * Always gather extra entropy before a call |
| 247 | */ |
| 248 | do |
| 249 | { |
| 250 | if( count++ > ENTROPY_MAX_LOOP ) |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 251 | { |
| 252 | ret = POLARSSL_ERR_ENTROPY_SOURCE_FAILED; |
| 253 | goto exit; |
| 254 | } |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 255 | |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 256 | if( ( ret = entropy_gather_internal( ctx ) ) != 0 ) |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 257 | goto exit; |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 258 | |
| 259 | reached = 0; |
| 260 | |
| 261 | for( i = 0; i < ctx->source_count; i++ ) |
| 262 | if( ctx->source[i].size >= ctx->source[i].threshold ) |
| 263 | reached++; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 264 | } |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 265 | while( reached != ctx->source_count ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 266 | |
| 267 | memset( buf, 0, ENTROPY_BLOCK_SIZE ); |
| 268 | |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 269 | #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 270 | sha512_finish( &ctx->accumulator, buf ); |
| 271 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 272 | /* |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 273 | * Reset accumulator and counters and recycle existing entropy |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 274 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 275 | memset( &ctx->accumulator, 0, sizeof( sha512_context ) ); |
| 276 | sha512_starts( &ctx->accumulator, 0 ); |
| 277 | sha512_update( &ctx->accumulator, buf, ENTROPY_BLOCK_SIZE ); |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 278 | |
| 279 | /* |
Paul Bakker | b13d3ff | 2014-03-26 12:51:25 +0100 | [diff] [blame] | 280 | * Perform second SHA-512 on entropy |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 281 | */ |
Paul Bakker | b13d3ff | 2014-03-26 12:51:25 +0100 | [diff] [blame] | 282 | sha512( buf, ENTROPY_BLOCK_SIZE, buf, 0 ); |
| 283 | #else /* POLARSSL_ENTROPY_SHA512_ACCUMULATOR */ |
| 284 | sha256_finish( &ctx->accumulator, buf ); |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 285 | |
| 286 | /* |
| 287 | * Reset accumulator and counters and recycle existing entropy |
| 288 | */ |
| 289 | memset( &ctx->accumulator, 0, sizeof( sha256_context ) ); |
| 290 | sha256_starts( &ctx->accumulator, 0 ); |
| 291 | sha256_update( &ctx->accumulator, buf, ENTROPY_BLOCK_SIZE ); |
Paul Bakker | b13d3ff | 2014-03-26 12:51:25 +0100 | [diff] [blame] | 292 | |
| 293 | /* |
| 294 | * Perform second SHA-256 on entropy |
| 295 | */ |
| 296 | sha256( buf, ENTROPY_BLOCK_SIZE, buf, 0 ); |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 297 | #endif /* POLARSSL_ENTROPY_SHA512_ACCUMULATOR */ |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 298 | |
| 299 | for( i = 0; i < ctx->source_count; i++ ) |
| 300 | ctx->source[i].size = 0; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 301 | |
| 302 | memcpy( output, buf, len ); |
| 303 | |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 304 | ret = 0; |
| 305 | |
| 306 | exit: |
| 307 | #if defined(POLARSSL_THREADING_C) |
| 308 | if( polarssl_mutex_unlock( &ctx->mutex ) != 0 ) |
| 309 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); |
| 310 | #endif |
| 311 | |
| 312 | return( ret ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 315 | #if defined(POLARSSL_FS_IO) |
| 316 | int entropy_write_seed_file( entropy_context *ctx, const char *path ) |
| 317 | { |
| 318 | int ret = POLARSSL_ERR_ENTROPY_FILE_IO_ERROR; |
| 319 | FILE *f; |
| 320 | unsigned char buf[ENTROPY_BLOCK_SIZE]; |
| 321 | |
| 322 | if( ( f = fopen( path, "wb" ) ) == NULL ) |
| 323 | return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR ); |
| 324 | |
| 325 | if( ( ret = entropy_func( ctx, buf, ENTROPY_BLOCK_SIZE ) ) != 0 ) |
| 326 | goto exit; |
| 327 | |
| 328 | if( fwrite( buf, 1, ENTROPY_BLOCK_SIZE, f ) != ENTROPY_BLOCK_SIZE ) |
| 329 | { |
| 330 | ret = POLARSSL_ERR_ENTROPY_FILE_IO_ERROR; |
| 331 | goto exit; |
| 332 | } |
| 333 | |
| 334 | ret = 0; |
| 335 | |
| 336 | exit: |
| 337 | fclose( f ); |
| 338 | return( ret ); |
| 339 | } |
| 340 | |
| 341 | int entropy_update_seed_file( entropy_context *ctx, const char *path ) |
| 342 | { |
| 343 | FILE *f; |
| 344 | size_t n; |
| 345 | unsigned char buf[ ENTROPY_MAX_SEED_SIZE ]; |
| 346 | |
| 347 | if( ( f = fopen( path, "rb" ) ) == NULL ) |
| 348 | return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR ); |
| 349 | |
| 350 | fseek( f, 0, SEEK_END ); |
| 351 | n = (size_t) ftell( f ); |
| 352 | fseek( f, 0, SEEK_SET ); |
| 353 | |
| 354 | if( n > ENTROPY_MAX_SEED_SIZE ) |
| 355 | n = ENTROPY_MAX_SEED_SIZE; |
| 356 | |
| 357 | if( fread( buf, 1, n, f ) != n ) |
| 358 | { |
| 359 | fclose( f ); |
| 360 | return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR ); |
| 361 | } |
| 362 | |
| 363 | fclose( f ); |
| 364 | |
| 365 | entropy_update_manual( ctx, buf, n ); |
| 366 | |
| 367 | return( entropy_write_seed_file( ctx, path ) ); |
| 368 | } |
| 369 | #endif /* POLARSSL_FS_IO */ |
| 370 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 371 | #endif |