blob: 38171fc1eb9d78bc85ec5a042ab832b559b135ff [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Entropy accumulator implementation
3 *
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakker6083fd22011-12-03 21:45:14 +00005 *
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 Bakker28c7e7f2011-12-15 19:49:30 +000033#if defined(POLARSSL_HAVEGE_C)
34#include "polarssl/havege.h"
35#endif
36
Paul Bakker6083fd22011-12-03 21:45:14 +000037#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
38
39void entropy_init( entropy_context *ctx )
40{
41 memset( ctx, 0, sizeof(entropy_context) );
42
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020043#if defined(POLARSSL_THREADING_C)
44 polarssl_mutex_init( &ctx->mutex );
45#endif
46
Paul Bakkerfb08fd22013-08-27 15:06:26 +020047#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +020048 sha512_starts( &ctx->accumulator, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +020049#else
50 sha256_starts( &ctx->accumulator, 0 );
51#endif
Paul Bakker43655f42011-12-15 20:11:16 +000052#if defined(POLARSSL_HAVEGE_C)
53 havege_init( &ctx->havege_data );
54#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000055
Paul Bakker43655f42011-12-15 20:11:16 +000056#if !defined(POLARSSL_NO_DEFAULT_ENTROPY_SOURCES)
Paul Bakker6083fd22011-12-03 21:45:14 +000057#if !defined(POLARSSL_NO_PLATFORM_ENTROPY)
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000058 entropy_add_source( ctx, platform_entropy_poll, NULL,
59 ENTROPY_MIN_PLATFORM );
Paul Bakker6083fd22011-12-03 21:45:14 +000060#endif
61#if defined(POLARSSL_TIMING_C)
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000062 entropy_add_source( ctx, hardclock_poll, NULL, ENTROPY_MIN_HARDCLOCK );
Paul Bakker6083fd22011-12-03 21:45:14 +000063#endif
Paul Bakker28c7e7f2011-12-15 19:49:30 +000064#if defined(POLARSSL_HAVEGE_C)
Paul Bakker28c7e7f2011-12-15 19:49:30 +000065 entropy_add_source( ctx, havege_poll, &ctx->havege_data,
66 ENTROPY_MIN_HAVEGE );
67#endif
Paul Bakker43655f42011-12-15 20:11:16 +000068#endif /* POLARSSL_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +000069}
70
Paul Bakker1ffefac2013-09-28 15:23:03 +020071void entropy_free( entropy_context *ctx )
72{
73 ((void) ctx);
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020074#if defined(POLARSSL_THREADING_C)
75 polarssl_mutex_free( &ctx->mutex );
76#endif
Paul Bakker1ffefac2013-09-28 15:23:03 +020077}
78
Paul Bakker6083fd22011-12-03 21:45:14 +000079int entropy_add_source( entropy_context *ctx,
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000080 f_source_ptr f_source, void *p_source,
81 size_t threshold )
Paul Bakker6083fd22011-12-03 21:45:14 +000082{
Paul Bakker47703a02014-02-06 15:01:20 +010083 int index, ret = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000084
Paul Bakker47703a02014-02-06 15:01:20 +010085#if defined(POLARSSL_THREADING_C)
86 if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 )
87 return( ret );
88#endif
89
90 index = ctx->source_count;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000091 if( index >= ENTROPY_MAX_SOURCES )
Paul Bakker47703a02014-02-06 15:01:20 +010092 {
93 ret = POLARSSL_ERR_ENTROPY_MAX_SOURCES;
94 goto exit;
95 }
Paul Bakker6083fd22011-12-03 21:45:14 +000096
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000097 ctx->source[index].f_source = f_source;
98 ctx->source[index].p_source = p_source;
99 ctx->source[index].threshold = threshold;
Paul Bakker6083fd22011-12-03 21:45:14 +0000100
101 ctx->source_count++;
102
Paul Bakker47703a02014-02-06 15:01:20 +0100103exit:
104#if defined(POLARSSL_THREADING_C)
105 if( polarssl_mutex_unlock( &ctx->mutex ) != 0 )
106 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
107#endif
108
109 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000110}
111
112/*
113 * Entropy accumulator update
114 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200115static int entropy_update( entropy_context *ctx, unsigned char source_id,
116 const unsigned char *data, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000117{
118 unsigned char header[2];
119 unsigned char tmp[ENTROPY_BLOCK_SIZE];
120 size_t use_len = len;
121 const unsigned char *p = data;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200122
Paul Bakker6083fd22011-12-03 21:45:14 +0000123 if( use_len > ENTROPY_BLOCK_SIZE )
124 {
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200125#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +0200126 sha512( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200127#else
128 sha256( data, len, tmp, 0 );
129#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000130 p = tmp;
131 use_len = ENTROPY_BLOCK_SIZE;
132 }
133
134 header[0] = source_id;
135 header[1] = use_len & 0xFF;
136
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200137#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +0200138 sha512_update( &ctx->accumulator, header, 2 );
139 sha512_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200140#else
141 sha256_update( &ctx->accumulator, header, 2 );
142 sha256_update( &ctx->accumulator, p, use_len );
143#endif
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200144
Paul Bakker6083fd22011-12-03 21:45:14 +0000145 return( 0 );
146}
147
148int entropy_update_manual( entropy_context *ctx,
149 const unsigned char *data, size_t len )
150{
Paul Bakker47703a02014-02-06 15:01:20 +0100151 int ret;
152
153#if defined(POLARSSL_THREADING_C)
154 if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 )
155 return( ret );
156#endif
157
158 ret = entropy_update( ctx, ENTROPY_SOURCE_MANUAL, data, len );
159
160#if defined(POLARSSL_THREADING_C)
161 if( polarssl_mutex_unlock( &ctx->mutex ) != 0 )
162 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
163#endif
164
165 return ( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000166}
167
168/*
169 * Run through the different sources to add entropy to our accumulator
170 */
Paul Bakker47703a02014-02-06 15:01:20 +0100171static int entropy_gather_internal( entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +0000172{
173 int ret, i;
174 unsigned char buf[ENTROPY_MAX_GATHER];
175 size_t olen;
Paul Bakker47703a02014-02-06 15:01:20 +0100176
Paul Bakker43655f42011-12-15 20:11:16 +0000177 if( ctx->source_count == 0 )
178 return( POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED );
179
Paul Bakker6083fd22011-12-03 21:45:14 +0000180 /*
181 * Run through our entropy sources
182 */
183 for( i = 0; i < ctx->source_count; i++ )
184 {
185 olen = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000186 if ( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
Paul Bakker6083fd22011-12-03 21:45:14 +0000187 buf, ENTROPY_MAX_GATHER, &olen ) ) != 0 )
188 {
189 return( ret );
190 }
191
192 /*
193 * Add if we actually gathered something
194 */
195 if( olen > 0 )
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000196 {
Paul Bakker6083fd22011-12-03 21:45:14 +0000197 entropy_update( ctx, (unsigned char) i, buf, olen );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000198 ctx->source[i].size += olen;
199 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000200 }
201
202 return( 0 );
203}
204
Paul Bakker47703a02014-02-06 15:01:20 +0100205/*
206 * Thread-safe wrapper for entropy_gather_internal()
207 */
208int entropy_gather( entropy_context *ctx )
209{
210 int ret;
211
212#if defined(POLARSSL_THREADING_C)
213 if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 )
214 return( ret );
215#endif
216
217 ret = entropy_gather_internal( ctx );
218
219#if defined(POLARSSL_THREADING_C)
220 if( polarssl_mutex_unlock( &ctx->mutex ) != 0 )
221 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
222#endif
223
224 return ( ret );
225}
226
Paul Bakker6083fd22011-12-03 21:45:14 +0000227int entropy_func( void *data, unsigned char *output, size_t len )
228{
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000229 int ret, count = 0, i, reached;
Paul Bakker6083fd22011-12-03 21:45:14 +0000230 entropy_context *ctx = (entropy_context *) data;
231 unsigned char buf[ENTROPY_BLOCK_SIZE];
232
233 if( len > ENTROPY_BLOCK_SIZE )
234 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
235
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200236#if defined(POLARSSL_THREADING_C)
237 if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 )
238 return( ret );
239#endif
240
Paul Bakker6083fd22011-12-03 21:45:14 +0000241 /*
242 * Always gather extra entropy before a call
243 */
244 do
245 {
246 if( count++ > ENTROPY_MAX_LOOP )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200247 {
248 ret = POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
249 goto exit;
250 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000251
Paul Bakker47703a02014-02-06 15:01:20 +0100252 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200253 goto exit;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000254
255 reached = 0;
256
257 for( i = 0; i < ctx->source_count; i++ )
258 if( ctx->source[i].size >= ctx->source[i].threshold )
259 reached++;
Paul Bakker6083fd22011-12-03 21:45:14 +0000260 }
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000261 while( reached != ctx->source_count );
Paul Bakker6083fd22011-12-03 21:45:14 +0000262
263 memset( buf, 0, ENTROPY_BLOCK_SIZE );
264
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200265#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +0200266 sha512_finish( &ctx->accumulator, buf );
267
Paul Bakker6083fd22011-12-03 21:45:14 +0000268 /*
269 * Perform second SHA-512 on entropy
270 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200271 sha512( buf, ENTROPY_BLOCK_SIZE, buf, 0 );
Paul Bakker6083fd22011-12-03 21:45:14 +0000272
273 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000274 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000275 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200276 memset( &ctx->accumulator, 0, sizeof( sha512_context ) );
277 sha512_starts( &ctx->accumulator, 0 );
278 sha512_update( &ctx->accumulator, buf, ENTROPY_BLOCK_SIZE );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200279#else /* POLARSSL_ENTROPY_SHA512_ACCUMULATOR */
280 sha256_finish( &ctx->accumulator, buf );
281
282 /*
283 * Perform second SHA-256 on entropy
284 */
285 sha256( buf, ENTROPY_BLOCK_SIZE, buf, 0 );
286
287 /*
288 * Reset accumulator and counters and recycle existing entropy
289 */
290 memset( &ctx->accumulator, 0, sizeof( sha256_context ) );
291 sha256_starts( &ctx->accumulator, 0 );
292 sha256_update( &ctx->accumulator, buf, ENTROPY_BLOCK_SIZE );
293#endif /* POLARSSL_ENTROPY_SHA512_ACCUMULATOR */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000294
295 for( i = 0; i < ctx->source_count; i++ )
296 ctx->source[i].size = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000297
298 memcpy( output, buf, len );
299
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200300 ret = 0;
301
302exit:
303#if defined(POLARSSL_THREADING_C)
304 if( polarssl_mutex_unlock( &ctx->mutex ) != 0 )
305 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
306#endif
307
308 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000309}
310
311#endif