blob: a9d2f1b024beab52c6a649810134c620fe10a177 [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Entropy accumulator implementation
3 *
Paul Bakker530927b2015-02-13 14:24:10 +01004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakker6083fd22011-12-03 21:45:14 +00005 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker6083fd22011-12-03 21:45:14 +00007 *
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 Bakker1e942372014-03-26 11:54:05 +010030#if defined(POLARSSL_FS_IO)
31#include <stdio.h>
32#endif
33
Paul Bakker28c7e7f2011-12-15 19:49:30 +000034#if defined(POLARSSL_HAVEGE_C)
35#include "polarssl/havege.h"
36#endif
37
Paul Bakker6083fd22011-12-03 21:45:14 +000038#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
39
40void entropy_init( entropy_context *ctx )
41{
42 memset( ctx, 0, sizeof(entropy_context) );
43
44 sha4_starts( &ctx->accumulator, 0 );
Paul Bakker43655f42011-12-15 20:11:16 +000045#if defined(POLARSSL_HAVEGE_C)
46 havege_init( &ctx->havege_data );
47#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000048
Paul Bakker43655f42011-12-15 20:11:16 +000049#if !defined(POLARSSL_NO_DEFAULT_ENTROPY_SOURCES)
Paul Bakker6083fd22011-12-03 21:45:14 +000050#if !defined(POLARSSL_NO_PLATFORM_ENTROPY)
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000051 entropy_add_source( ctx, platform_entropy_poll, NULL,
52 ENTROPY_MIN_PLATFORM );
Paul Bakker6083fd22011-12-03 21:45:14 +000053#endif
54#if defined(POLARSSL_TIMING_C)
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000055 entropy_add_source( ctx, hardclock_poll, NULL, ENTROPY_MIN_HARDCLOCK );
Paul Bakker6083fd22011-12-03 21:45:14 +000056#endif
Paul Bakker28c7e7f2011-12-15 19:49:30 +000057#if defined(POLARSSL_HAVEGE_C)
Paul Bakker28c7e7f2011-12-15 19:49:30 +000058 entropy_add_source( ctx, havege_poll, &ctx->havege_data,
59 ENTROPY_MIN_HAVEGE );
60#endif
Paul Bakker43655f42011-12-15 20:11:16 +000061#endif /* POLARSSL_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +000062}
63
64int entropy_add_source( entropy_context *ctx,
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000065 f_source_ptr f_source, void *p_source,
66 size_t threshold )
Paul Bakker6083fd22011-12-03 21:45:14 +000067{
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000068 int index = ctx->source_count;
69
70 if( index >= ENTROPY_MAX_SOURCES )
Paul Bakker6083fd22011-12-03 21:45:14 +000071 return( POLARSSL_ERR_ENTROPY_MAX_SOURCES );
72
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000073 ctx->source[index].f_source = f_source;
74 ctx->source[index].p_source = p_source;
75 ctx->source[index].threshold = threshold;
Paul Bakker6083fd22011-12-03 21:45:14 +000076
77 ctx->source_count++;
78
79 return( 0 );
80}
81
82/*
83 * Entropy accumulator update
84 */
Paul Bakker1d073c52014-07-08 20:15:51 +020085static int entropy_update( entropy_context *ctx, unsigned char source_id,
86 const unsigned char *data, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +000087{
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 Bakker6083fd22011-12-03 21:45:14 +0000107 return( 0 );
108}
109
110int 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 */
119int entropy_gather( entropy_context *ctx )
120{
121 int ret, i;
122 unsigned char buf[ENTROPY_MAX_GATHER];
123 size_t olen;
124
Paul Bakker43655f42011-12-15 20:11:16 +0000125 if( ctx->source_count == 0 )
126 return( POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED );
127
Paul Bakker6083fd22011-12-03 21:45:14 +0000128 /*
129 * Run through our entropy sources
130 */
131 for( i = 0; i < ctx->source_count; i++ )
132 {
133 olen = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000134 if ( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
Paul Bakker6083fd22011-12-03 21:45:14 +0000135 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 Bakkerbd4a9d02011-12-10 17:02:19 +0000144 {
Paul Bakker6083fd22011-12-03 21:45:14 +0000145 entropy_update( ctx, (unsigned char) i, buf, olen );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000146 ctx->source[i].size += olen;
147 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000148 }
149
150 return( 0 );
151}
152
153int entropy_func( void *data, unsigned char *output, size_t len )
154{
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000155 int ret, count = 0, i, reached;
Paul Bakker6083fd22011-12-03 21:45:14 +0000156 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 Bakkerbd4a9d02011-12-10 17:02:19 +0000172
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 Bakker6083fd22011-12-03 21:45:14 +0000178 }
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000179 while( reached != ctx->source_count );
Paul Bakker6083fd22011-12-03 21:45:14 +0000180
181 memset( buf, 0, ENTROPY_BLOCK_SIZE );
182
183 sha4_finish( &ctx->accumulator, buf );
Paul Bakker6083fd22011-12-03 21:45:14 +0000184
185 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000186 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000187 */
188 memset( &ctx->accumulator, 0, sizeof( sha4_context ) );
189 sha4_starts( &ctx->accumulator, 0 );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000190 sha4_update( &ctx->accumulator, buf, ENTROPY_BLOCK_SIZE );
191
Paul Bakkeref3cf702014-03-26 12:51:25 +0100192 /*
193 * Perform second SHA-512 on entropy
194 */
195 sha4( buf, ENTROPY_BLOCK_SIZE, buf, 0 );
196
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000197 for( i = 0; i < ctx->source_count; i++ )
198 ctx->source[i].size = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000199
200 memcpy( output, buf, len );
201
202 return( 0 );
203}
204
Paul Bakker1e942372014-03-26 11:54:05 +0100205#if defined(POLARSSL_FS_IO)
206int 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
226exit:
227 fclose( f );
228 return( ret );
229}
230
231int 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 Bakker6083fd22011-12-03 21:45:14 +0000261#endif