Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Entropy accumulator implementation |
| 3 | * |
| 4 | * Copyright (C) 2006-2011, Brainspark B.V. |
| 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 | |
| 33 | #define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */ |
| 34 | |
| 35 | void entropy_init( entropy_context *ctx ) |
| 36 | { |
| 37 | memset( ctx, 0, sizeof(entropy_context) ); |
| 38 | |
| 39 | sha4_starts( &ctx->accumulator, 0 ); |
| 40 | |
| 41 | #if !defined(POLARSSL_NO_PLATFORM_ENTROPY) |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame^] | 42 | entropy_add_source( ctx, platform_entropy_poll, NULL, |
| 43 | ENTROPY_MIN_PLATFORM ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 44 | #endif |
| 45 | #if defined(POLARSSL_TIMING_C) |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame^] | 46 | entropy_add_source( ctx, hardclock_poll, NULL, ENTROPY_MIN_HARDCLOCK ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 47 | #endif |
| 48 | } |
| 49 | |
| 50 | int entropy_add_source( entropy_context *ctx, |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame^] | 51 | f_source_ptr f_source, void *p_source, |
| 52 | size_t threshold ) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 53 | { |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame^] | 54 | int index = ctx->source_count; |
| 55 | |
| 56 | if( index >= ENTROPY_MAX_SOURCES ) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 57 | return( POLARSSL_ERR_ENTROPY_MAX_SOURCES ); |
| 58 | |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame^] | 59 | ctx->source[index].f_source = f_source; |
| 60 | ctx->source[index].p_source = p_source; |
| 61 | ctx->source[index].threshold = threshold; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 62 | |
| 63 | ctx->source_count++; |
| 64 | |
| 65 | return( 0 ); |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Entropy accumulator update |
| 70 | */ |
| 71 | int entropy_update( entropy_context *ctx, unsigned char source_id, |
| 72 | const unsigned char *data, size_t len ) |
| 73 | { |
| 74 | unsigned char header[2]; |
| 75 | unsigned char tmp[ENTROPY_BLOCK_SIZE]; |
| 76 | size_t use_len = len; |
| 77 | const unsigned char *p = data; |
| 78 | |
| 79 | if( use_len > ENTROPY_BLOCK_SIZE ) |
| 80 | { |
| 81 | sha4( data, len, tmp, 0 ); |
| 82 | |
| 83 | p = tmp; |
| 84 | use_len = ENTROPY_BLOCK_SIZE; |
| 85 | } |
| 86 | |
| 87 | header[0] = source_id; |
| 88 | header[1] = use_len & 0xFF; |
| 89 | |
| 90 | sha4_update( &ctx->accumulator, header, 2 ); |
| 91 | sha4_update( &ctx->accumulator, p, use_len ); |
| 92 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 93 | return( 0 ); |
| 94 | } |
| 95 | |
| 96 | int entropy_update_manual( entropy_context *ctx, |
| 97 | const unsigned char *data, size_t len ) |
| 98 | { |
| 99 | return entropy_update( ctx, ENTROPY_SOURCE_MANUAL, data, len ); |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Run through the different sources to add entropy to our accumulator |
| 104 | */ |
| 105 | int entropy_gather( entropy_context *ctx ) |
| 106 | { |
| 107 | int ret, i; |
| 108 | unsigned char buf[ENTROPY_MAX_GATHER]; |
| 109 | size_t olen; |
| 110 | |
| 111 | /* |
| 112 | * Run through our entropy sources |
| 113 | */ |
| 114 | for( i = 0; i < ctx->source_count; i++ ) |
| 115 | { |
| 116 | olen = 0; |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame^] | 117 | if ( ( ret = ctx->source[i].f_source( ctx->source[i].p_source, |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 118 | buf, ENTROPY_MAX_GATHER, &olen ) ) != 0 ) |
| 119 | { |
| 120 | return( ret ); |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Add if we actually gathered something |
| 125 | */ |
| 126 | if( olen > 0 ) |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame^] | 127 | { |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 128 | entropy_update( ctx, (unsigned char) i, buf, olen ); |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame^] | 129 | ctx->source[i].size += olen; |
| 130 | } |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | return( 0 ); |
| 134 | } |
| 135 | |
| 136 | int entropy_func( void *data, unsigned char *output, size_t len ) |
| 137 | { |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame^] | 138 | int ret, count = 0, i, reached; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 139 | entropy_context *ctx = (entropy_context *) data; |
| 140 | unsigned char buf[ENTROPY_BLOCK_SIZE]; |
| 141 | |
| 142 | if( len > ENTROPY_BLOCK_SIZE ) |
| 143 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
| 144 | |
| 145 | /* |
| 146 | * Always gather extra entropy before a call |
| 147 | */ |
| 148 | do |
| 149 | { |
| 150 | if( count++ > ENTROPY_MAX_LOOP ) |
| 151 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
| 152 | |
| 153 | if( ( ret = entropy_gather( ctx ) ) != 0 ) |
| 154 | return( ret ); |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame^] | 155 | |
| 156 | reached = 0; |
| 157 | |
| 158 | for( i = 0; i < ctx->source_count; i++ ) |
| 159 | if( ctx->source[i].size >= ctx->source[i].threshold ) |
| 160 | reached++; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 161 | } |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame^] | 162 | while( reached != ctx->source_count ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 163 | |
| 164 | memset( buf, 0, ENTROPY_BLOCK_SIZE ); |
| 165 | |
| 166 | sha4_finish( &ctx->accumulator, buf ); |
| 167 | |
| 168 | /* |
| 169 | * Perform second SHA-512 on entropy |
| 170 | */ |
| 171 | sha4( buf, ENTROPY_BLOCK_SIZE, buf, 0 ); |
| 172 | |
| 173 | /* |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame^] | 174 | * Reset accumulator and counters and recycle existing entropy |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 175 | */ |
| 176 | memset( &ctx->accumulator, 0, sizeof( sha4_context ) ); |
| 177 | sha4_starts( &ctx->accumulator, 0 ); |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame^] | 178 | sha4_update( &ctx->accumulator, buf, ENTROPY_BLOCK_SIZE ); |
| 179 | |
| 180 | for( i = 0; i < ctx->source_count; i++ ) |
| 181 | ctx->source[i].size = 0; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 182 | |
| 183 | memcpy( output, buf, len ); |
| 184 | |
| 185 | return( 0 ); |
| 186 | } |
| 187 | |
| 188 | #endif |