Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Entropy accumulator implementation |
| 3 | * |
Paul Bakker | 530927b | 2015-02-13 14:24:10 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | e12abf9 | 2015-01-28 17:13:45 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #include "polarssl/config.h" |
| 24 | |
| 25 | #if defined(POLARSSL_ENTROPY_C) |
| 26 | |
| 27 | #include "polarssl/entropy.h" |
| 28 | #include "polarssl/entropy_poll.h" |
| 29 | |
Paul Bakker | 1e94237 | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 30 | #if defined(POLARSSL_FS_IO) |
| 31 | #include <stdio.h> |
| 32 | #endif |
| 33 | |
Paul Bakker | 28c7e7f | 2011-12-15 19:49:30 +0000 | [diff] [blame] | 34 | #if defined(POLARSSL_HAVEGE_C) |
| 35 | #include "polarssl/havege.h" |
| 36 | #endif |
| 37 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 38 | #define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */ |
| 39 | |
| 40 | void entropy_init( entropy_context *ctx ) |
| 41 | { |
| 42 | memset( ctx, 0, sizeof(entropy_context) ); |
| 43 | |
| 44 | sha4_starts( &ctx->accumulator, 0 ); |
Paul Bakker | 43655f4 | 2011-12-15 20:11:16 +0000 | [diff] [blame] | 45 | #if defined(POLARSSL_HAVEGE_C) |
| 46 | havege_init( &ctx->havege_data ); |
| 47 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 48 | |
Paul Bakker | 43655f4 | 2011-12-15 20:11:16 +0000 | [diff] [blame] | 49 | #if !defined(POLARSSL_NO_DEFAULT_ENTROPY_SOURCES) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 50 | #if !defined(POLARSSL_NO_PLATFORM_ENTROPY) |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 51 | entropy_add_source( ctx, platform_entropy_poll, NULL, |
| 52 | ENTROPY_MIN_PLATFORM ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 53 | #endif |
| 54 | #if defined(POLARSSL_TIMING_C) |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 55 | entropy_add_source( ctx, hardclock_poll, NULL, ENTROPY_MIN_HARDCLOCK ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 56 | #endif |
Paul Bakker | 28c7e7f | 2011-12-15 19:49:30 +0000 | [diff] [blame] | 57 | #if defined(POLARSSL_HAVEGE_C) |
Paul Bakker | 28c7e7f | 2011-12-15 19:49:30 +0000 | [diff] [blame] | 58 | entropy_add_source( ctx, havege_poll, &ctx->havege_data, |
| 59 | ENTROPY_MIN_HAVEGE ); |
| 60 | #endif |
Paul Bakker | 43655f4 | 2011-12-15 20:11:16 +0000 | [diff] [blame] | 61 | #endif /* POLARSSL_NO_DEFAULT_ENTROPY_SOURCES */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | int entropy_add_source( entropy_context *ctx, |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 65 | f_source_ptr f_source, void *p_source, |
| 66 | size_t threshold ) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 67 | { |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 68 | int index = ctx->source_count; |
| 69 | |
| 70 | if( index >= ENTROPY_MAX_SOURCES ) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 71 | return( POLARSSL_ERR_ENTROPY_MAX_SOURCES ); |
| 72 | |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 73 | ctx->source[index].f_source = f_source; |
| 74 | ctx->source[index].p_source = p_source; |
| 75 | ctx->source[index].threshold = threshold; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 76 | |
| 77 | ctx->source_count++; |
| 78 | |
| 79 | return( 0 ); |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Entropy accumulator update |
| 84 | */ |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 85 | static int entropy_update( entropy_context *ctx, unsigned char source_id, |
| 86 | const unsigned char *data, size_t len ) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 87 | { |
| 88 | unsigned char header[2]; |
| 89 | unsigned char tmp[ENTROPY_BLOCK_SIZE]; |
| 90 | size_t use_len = len; |
| 91 | const unsigned char *p = data; |
| 92 | |
| 93 | if( use_len > ENTROPY_BLOCK_SIZE ) |
| 94 | { |
| 95 | sha4( data, len, tmp, 0 ); |
| 96 | |
| 97 | p = tmp; |
| 98 | use_len = ENTROPY_BLOCK_SIZE; |
| 99 | } |
| 100 | |
| 101 | header[0] = source_id; |
| 102 | header[1] = use_len & 0xFF; |
| 103 | |
| 104 | sha4_update( &ctx->accumulator, header, 2 ); |
| 105 | sha4_update( &ctx->accumulator, p, use_len ); |
| 106 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 107 | return( 0 ); |
| 108 | } |
| 109 | |
| 110 | int entropy_update_manual( entropy_context *ctx, |
| 111 | const unsigned char *data, size_t len ) |
| 112 | { |
| 113 | return entropy_update( ctx, ENTROPY_SOURCE_MANUAL, data, len ); |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Run through the different sources to add entropy to our accumulator |
| 118 | */ |
| 119 | int entropy_gather( entropy_context *ctx ) |
| 120 | { |
| 121 | int ret, i; |
| 122 | unsigned char buf[ENTROPY_MAX_GATHER]; |
| 123 | size_t olen; |
| 124 | |
Paul Bakker | 43655f4 | 2011-12-15 20:11:16 +0000 | [diff] [blame] | 125 | if( ctx->source_count == 0 ) |
| 126 | return( POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED ); |
| 127 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 128 | /* |
| 129 | * Run through our entropy sources |
| 130 | */ |
| 131 | for( i = 0; i < ctx->source_count; i++ ) |
| 132 | { |
| 133 | olen = 0; |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 134 | if ( ( ret = ctx->source[i].f_source( ctx->source[i].p_source, |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 135 | buf, ENTROPY_MAX_GATHER, &olen ) ) != 0 ) |
| 136 | { |
| 137 | return( ret ); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Add if we actually gathered something |
| 142 | */ |
| 143 | if( olen > 0 ) |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 144 | { |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 145 | entropy_update( ctx, (unsigned char) i, buf, olen ); |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 146 | ctx->source[i].size += olen; |
| 147 | } |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | return( 0 ); |
| 151 | } |
| 152 | |
| 153 | int entropy_func( void *data, unsigned char *output, size_t len ) |
| 154 | { |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 155 | int ret, count = 0, i, reached; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 156 | entropy_context *ctx = (entropy_context *) data; |
| 157 | unsigned char buf[ENTROPY_BLOCK_SIZE]; |
| 158 | |
| 159 | if( len > ENTROPY_BLOCK_SIZE ) |
| 160 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
| 161 | |
| 162 | /* |
| 163 | * Always gather extra entropy before a call |
| 164 | */ |
| 165 | do |
| 166 | { |
| 167 | if( count++ > ENTROPY_MAX_LOOP ) |
| 168 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
| 169 | |
| 170 | if( ( ret = entropy_gather( ctx ) ) != 0 ) |
| 171 | return( ret ); |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 172 | |
| 173 | reached = 0; |
| 174 | |
| 175 | for( i = 0; i < ctx->source_count; i++ ) |
| 176 | if( ctx->source[i].size >= ctx->source[i].threshold ) |
| 177 | reached++; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 178 | } |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 179 | while( reached != ctx->source_count ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 180 | |
| 181 | memset( buf, 0, ENTROPY_BLOCK_SIZE ); |
| 182 | |
| 183 | sha4_finish( &ctx->accumulator, buf ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 184 | |
| 185 | /* |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 186 | * Reset accumulator and counters and recycle existing entropy |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 187 | */ |
| 188 | memset( &ctx->accumulator, 0, sizeof( sha4_context ) ); |
| 189 | sha4_starts( &ctx->accumulator, 0 ); |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 190 | sha4_update( &ctx->accumulator, buf, ENTROPY_BLOCK_SIZE ); |
| 191 | |
Paul Bakker | ef3cf70 | 2014-03-26 12:51:25 +0100 | [diff] [blame] | 192 | /* |
| 193 | * Perform second SHA-512 on entropy |
| 194 | */ |
| 195 | sha4( buf, ENTROPY_BLOCK_SIZE, buf, 0 ); |
| 196 | |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 197 | for( i = 0; i < ctx->source_count; i++ ) |
| 198 | ctx->source[i].size = 0; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 199 | |
| 200 | memcpy( output, buf, len ); |
| 201 | |
| 202 | return( 0 ); |
| 203 | } |
| 204 | |
Paul Bakker | 1e94237 | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 205 | #if defined(POLARSSL_FS_IO) |
| 206 | int entropy_write_seed_file( entropy_context *ctx, const char *path ) |
| 207 | { |
| 208 | int ret = POLARSSL_ERR_ENTROPY_FILE_IO_ERROR; |
| 209 | FILE *f; |
| 210 | unsigned char buf[ENTROPY_BLOCK_SIZE]; |
| 211 | |
| 212 | if( ( f = fopen( path, "wb" ) ) == NULL ) |
| 213 | return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR ); |
| 214 | |
| 215 | if( ( ret = entropy_func( ctx, buf, ENTROPY_BLOCK_SIZE ) ) != 0 ) |
| 216 | goto exit; |
| 217 | |
| 218 | if( fwrite( buf, 1, ENTROPY_BLOCK_SIZE, f ) != ENTROPY_BLOCK_SIZE ) |
| 219 | { |
| 220 | ret = POLARSSL_ERR_ENTROPY_FILE_IO_ERROR; |
| 221 | goto exit; |
| 222 | } |
| 223 | |
| 224 | ret = 0; |
| 225 | |
| 226 | exit: |
| 227 | fclose( f ); |
| 228 | return( ret ); |
| 229 | } |
| 230 | |
| 231 | int entropy_update_seed_file( entropy_context *ctx, const char *path ) |
| 232 | { |
| 233 | FILE *f; |
| 234 | size_t n; |
| 235 | unsigned char buf[ ENTROPY_MAX_SEED_SIZE ]; |
| 236 | |
| 237 | if( ( f = fopen( path, "rb" ) ) == NULL ) |
| 238 | return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR ); |
| 239 | |
| 240 | fseek( f, 0, SEEK_END ); |
| 241 | n = (size_t) ftell( f ); |
| 242 | fseek( f, 0, SEEK_SET ); |
| 243 | |
| 244 | if( n > ENTROPY_MAX_SEED_SIZE ) |
| 245 | n = ENTROPY_MAX_SEED_SIZE; |
| 246 | |
| 247 | if( fread( buf, 1, n, f ) != n ) |
| 248 | { |
| 249 | fclose( f ); |
| 250 | return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR ); |
| 251 | } |
| 252 | |
| 253 | fclose( f ); |
| 254 | |
| 255 | entropy_update_manual( ctx, buf, n ); |
| 256 | |
| 257 | return( entropy_write_seed_file( ctx, path ) ); |
| 258 | } |
| 259 | #endif /* POLARSSL_FS_IO */ |
| 260 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 261 | #endif |