blob: c5fac26e9c7da8cd1d9703e331888bfa50759576 [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 Bakkerbd4a9d02011-12-10 17:02:19 +000083 int index = ctx->source_count;
84
85 if( index >= ENTROPY_MAX_SOURCES )
Paul Bakker6083fd22011-12-03 21:45:14 +000086 return( POLARSSL_ERR_ENTROPY_MAX_SOURCES );
87
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000088 ctx->source[index].f_source = f_source;
89 ctx->source[index].p_source = p_source;
90 ctx->source[index].threshold = threshold;
Paul Bakker6083fd22011-12-03 21:45:14 +000091
92 ctx->source_count++;
93
94 return( 0 );
95}
96
97/*
98 * Entropy accumulator update
99 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200100static int entropy_update( entropy_context *ctx, unsigned char source_id,
101 const unsigned char *data, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000102{
103 unsigned char header[2];
104 unsigned char tmp[ENTROPY_BLOCK_SIZE];
105 size_t use_len = len;
106 const unsigned char *p = data;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200107
Paul Bakker6083fd22011-12-03 21:45:14 +0000108 if( use_len > ENTROPY_BLOCK_SIZE )
109 {
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200110#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +0200111 sha512( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200112#else
113 sha256( data, len, tmp, 0 );
114#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000115 p = tmp;
116 use_len = ENTROPY_BLOCK_SIZE;
117 }
118
119 header[0] = source_id;
120 header[1] = use_len & 0xFF;
121
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200122#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +0200123 sha512_update( &ctx->accumulator, header, 2 );
124 sha512_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200125#else
126 sha256_update( &ctx->accumulator, header, 2 );
127 sha256_update( &ctx->accumulator, p, use_len );
128#endif
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200129
Paul Bakker6083fd22011-12-03 21:45:14 +0000130 return( 0 );
131}
132
133int entropy_update_manual( entropy_context *ctx,
134 const unsigned char *data, size_t len )
135{
136 return entropy_update( ctx, ENTROPY_SOURCE_MANUAL, data, len );
137}
138
139/*
140 * Run through the different sources to add entropy to our accumulator
141 */
142int entropy_gather( entropy_context *ctx )
143{
144 int ret, i;
145 unsigned char buf[ENTROPY_MAX_GATHER];
146 size_t olen;
147
Paul Bakker43655f42011-12-15 20:11:16 +0000148 if( ctx->source_count == 0 )
149 return( POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED );
150
Paul Bakker6083fd22011-12-03 21:45:14 +0000151 /*
152 * Run through our entropy sources
153 */
154 for( i = 0; i < ctx->source_count; i++ )
155 {
156 olen = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000157 if ( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
Paul Bakker6083fd22011-12-03 21:45:14 +0000158 buf, ENTROPY_MAX_GATHER, &olen ) ) != 0 )
159 {
160 return( ret );
161 }
162
163 /*
164 * Add if we actually gathered something
165 */
166 if( olen > 0 )
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000167 {
Paul Bakker6083fd22011-12-03 21:45:14 +0000168 entropy_update( ctx, (unsigned char) i, buf, olen );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000169 ctx->source[i].size += olen;
170 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000171 }
172
173 return( 0 );
174}
175
176int entropy_func( void *data, unsigned char *output, size_t len )
177{
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000178 int ret, count = 0, i, reached;
Paul Bakker6083fd22011-12-03 21:45:14 +0000179 entropy_context *ctx = (entropy_context *) data;
180 unsigned char buf[ENTROPY_BLOCK_SIZE];
181
182 if( len > ENTROPY_BLOCK_SIZE )
183 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
184
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200185#if defined(POLARSSL_THREADING_C)
186 if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 )
187 return( ret );
188#endif
189
Paul Bakker6083fd22011-12-03 21:45:14 +0000190 /*
191 * Always gather extra entropy before a call
192 */
193 do
194 {
195 if( count++ > ENTROPY_MAX_LOOP )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200196 {
197 ret = POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
198 goto exit;
199 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000200
201 if( ( ret = entropy_gather( ctx ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200202 goto exit;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000203
204 reached = 0;
205
206 for( i = 0; i < ctx->source_count; i++ )
207 if( ctx->source[i].size >= ctx->source[i].threshold )
208 reached++;
Paul Bakker6083fd22011-12-03 21:45:14 +0000209 }
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000210 while( reached != ctx->source_count );
Paul Bakker6083fd22011-12-03 21:45:14 +0000211
212 memset( buf, 0, ENTROPY_BLOCK_SIZE );
213
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200214#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +0200215 sha512_finish( &ctx->accumulator, buf );
216
Paul Bakker6083fd22011-12-03 21:45:14 +0000217 /*
218 * Perform second SHA-512 on entropy
219 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200220 sha512( buf, ENTROPY_BLOCK_SIZE, buf, 0 );
Paul Bakker6083fd22011-12-03 21:45:14 +0000221
222 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000223 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000224 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200225 memset( &ctx->accumulator, 0, sizeof( sha512_context ) );
226 sha512_starts( &ctx->accumulator, 0 );
227 sha512_update( &ctx->accumulator, buf, ENTROPY_BLOCK_SIZE );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200228#else /* POLARSSL_ENTROPY_SHA512_ACCUMULATOR */
229 sha256_finish( &ctx->accumulator, buf );
230
231 /*
232 * Perform second SHA-256 on entropy
233 */
234 sha256( buf, ENTROPY_BLOCK_SIZE, buf, 0 );
235
236 /*
237 * Reset accumulator and counters and recycle existing entropy
238 */
239 memset( &ctx->accumulator, 0, sizeof( sha256_context ) );
240 sha256_starts( &ctx->accumulator, 0 );
241 sha256_update( &ctx->accumulator, buf, ENTROPY_BLOCK_SIZE );
242#endif /* POLARSSL_ENTROPY_SHA512_ACCUMULATOR */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000243
244 for( i = 0; i < ctx->source_count; i++ )
245 ctx->source[i].size = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000246
247 memcpy( output, buf, len );
248
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200249 ret = 0;
250
251exit:
252#if defined(POLARSSL_THREADING_C)
253 if( polarssl_mutex_unlock( &ctx->mutex ) != 0 )
254 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
255#endif
256
257 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000258}
259
260#endif