blob: 800a518a66ceb0100a1ccf21dee8f4017d2456e3 [file] [log] [blame]
Paul Bakkerf3b86c12011-01-27 15:24:17 +00001/**
2 * \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000047 */
48/*
49 * The HAVEGE RNG was designed by Andre Seznec in 2002.
50 *
51 * http://www.irisa.fr/caps/projects/hipsor/publi.php
52 *
53 * Contact: seznec(at)irisa_dot_fr - orocheco(at)irisa_dot_fr
54 */
55
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020058#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020060#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_HAVEGE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/havege.h"
65#include "mbedtls/timing.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050066#include "mbedtls/platform_util.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Gilles Peskine04659a02019-06-17 15:12:51 +020068#include <limits.h>
Paul Bakker23986e52011-04-24 08:57:21 +000069#include <string.h>
Paul Bakker23986e52011-04-24 08:57:21 +000070
Gilles Peskine04659a02019-06-17 15:12:51 +020071/* If int isn't capable of storing 2^32 distinct values, the code of this
72 * module may cause a processor trap or a miscalculation. If int is more
73 * than 32 bits, the code may not calculate the intended values. */
74#if INT_MIN + 1 != -0x7fffffff
75#error "The HAVEGE module requires int to be exactly 32 bits, with INT_MIN = -2^31."
76#endif
77#if UINT_MAX != 0xffffffff
78#error "The HAVEGE module requires unsigned to be exactly 32 bits."
79#endif
80
Paul Bakker5121ce52009-01-03 21:22:43 +000081/* ------------------------------------------------------------------------
82 * On average, one iteration accesses two 8-word blocks in the havege WALK
83 * table, and generates 16 words in the RES array.
84 *
85 * The data read in the WALK table is updated and permuted after each use.
86 * The result of the hardware clock counter read is used for this update.
87 *
88 * 25 conditional tests are present. The conditional tests are grouped in
89 * two nested groups of 12 conditional tests and 1 test that controls the
90 * permutation; on average, there should be 6 tests executed and 3 of them
91 * should be mispredicted.
92 * ------------------------------------------------------------------------
93 */
94
Gilles Peskine31a4ba72019-06-17 15:01:08 +020095#define SWAP(X,Y) { unsigned *T = (X); (X) = (Y); (Y) = T; }
Paul Bakker5121ce52009-01-03 21:22:43 +000096
97#define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
98#define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
99
100#define TST1_LEAVE U1++; }
101#define TST2_LEAVE U2++; }
102
103#define ONE_ITERATION \
104 \
105 PTEST = PT1 >> 20; \
106 \
107 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
108 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
109 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
110 \
111 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
112 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
113 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
114 \
115 PTX = (PT1 >> 18) & 7; \
116 PT1 &= 0x1FFF; \
117 PT2 &= 0x1FFF; \
Gilles Peskine31a4ba72019-06-17 15:01:08 +0200118 CLK = (unsigned) mbedtls_timing_hardclock(); \
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 \
120 i = 0; \
121 A = &WALK[PT1 ]; RES[i++] ^= *A; \
122 B = &WALK[PT2 ]; RES[i++] ^= *B; \
123 C = &WALK[PT1 ^ 1]; RES[i++] ^= *C; \
124 D = &WALK[PT2 ^ 4]; RES[i++] ^= *D; \
125 \
126 IN = (*A >> (1)) ^ (*A << (31)) ^ CLK; \
127 *A = (*B >> (2)) ^ (*B << (30)) ^ CLK; \
128 *B = IN ^ U1; \
129 *C = (*C >> (3)) ^ (*C << (29)) ^ CLK; \
130 *D = (*D >> (4)) ^ (*D << (28)) ^ CLK; \
131 \
132 A = &WALK[PT1 ^ 2]; RES[i++] ^= *A; \
133 B = &WALK[PT2 ^ 2]; RES[i++] ^= *B; \
134 C = &WALK[PT1 ^ 3]; RES[i++] ^= *C; \
135 D = &WALK[PT2 ^ 6]; RES[i++] ^= *D; \
136 \
137 if( PTEST & 1 ) SWAP( A, C ); \
138 \
139 IN = (*A >> (5)) ^ (*A << (27)) ^ CLK; \
140 *A = (*B >> (6)) ^ (*B << (26)) ^ CLK; \
Gilles Peskine31a4ba72019-06-17 15:01:08 +0200141 *B = IN; CLK = (unsigned) mbedtls_timing_hardclock(); \
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 *C = (*C >> (7)) ^ (*C << (25)) ^ CLK; \
143 *D = (*D >> (8)) ^ (*D << (24)) ^ CLK; \
144 \
145 A = &WALK[PT1 ^ 4]; \
146 B = &WALK[PT2 ^ 1]; \
147 \
148 PTEST = PT2 >> 1; \
149 \
150 PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]); \
151 PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8); \
152 PTY = (PT2 >> 10) & 7; \
153 \
154 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
155 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
156 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
157 \
158 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
159 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
160 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
161 \
162 C = &WALK[PT1 ^ 5]; \
163 D = &WALK[PT2 ^ 5]; \
164 \
165 RES[i++] ^= *A; \
166 RES[i++] ^= *B; \
167 RES[i++] ^= *C; \
168 RES[i++] ^= *D; \
169 \
170 IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK; \
171 *A = (*B >> (10)) ^ (*B << (22)) ^ CLK; \
172 *B = IN ^ U2; \
173 *C = (*C >> (11)) ^ (*C << (21)) ^ CLK; \
174 *D = (*D >> (12)) ^ (*D << (20)) ^ CLK; \
175 \
176 A = &WALK[PT1 ^ 6]; RES[i++] ^= *A; \
177 B = &WALK[PT2 ^ 3]; RES[i++] ^= *B; \
178 C = &WALK[PT1 ^ 7]; RES[i++] ^= *C; \
179 D = &WALK[PT2 ^ 7]; RES[i++] ^= *D; \
180 \
181 IN = (*A >> (13)) ^ (*A << (19)) ^ CLK; \
182 *A = (*B >> (14)) ^ (*B << (18)) ^ CLK; \
183 *B = IN; \
184 *C = (*C >> (15)) ^ (*C << (17)) ^ CLK; \
185 *D = (*D >> (16)) ^ (*D << (16)) ^ CLK; \
186 \
Paul Bakker66d5d072014-06-17 16:39:18 +0200187 PT1 = ( RES[( i - 8 ) ^ PTX] ^ \
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 WALK[PT1 ^ PTX ^ 7] ) & (~1); \
189 PT1 ^= (PT2 ^ 0x10) & 0x10; \
190 \
191 for( n++, i = 0; i < 16; i++ ) \
Gilles Peskine31a4ba72019-06-17 15:01:08 +0200192 POOL[n % MBEDTLS_HAVEGE_COLLECT_SIZE] ^= RES[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
194/*
195 * Entropy gathering function
196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197static void havege_fill( mbedtls_havege_state *hs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000198{
Gilles Peskine31a4ba72019-06-17 15:01:08 +0200199 unsigned i, n = 0;
200 unsigned U1, U2, *A, *B, *C, *D;
201 unsigned PT1, PT2, *WALK, *POOL, RES[16];
202 unsigned PTX, PTY, CLK, PTEST, IN;
Paul Bakker5121ce52009-01-03 21:22:43 +0000203
Gilles Peskine31a4ba72019-06-17 15:01:08 +0200204 WALK = (unsigned *) hs->WALK;
205 POOL = (unsigned *) hs->pool;
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 PT1 = hs->PT1;
207 PT2 = hs->PT2;
208
209 PTX = U1 = 0;
210 PTY = U2 = 0;
211
Simon Butcher65b1fa62016-05-23 23:18:26 +0100212 (void)PTX;
213
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 memset( RES, 0, sizeof( RES ) );
215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 while( n < MBEDTLS_HAVEGE_COLLECT_SIZE * 4 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 {
218 ONE_ITERATION
219 ONE_ITERATION
220 ONE_ITERATION
221 ONE_ITERATION
222 }
223
224 hs->PT1 = PT1;
225 hs->PT2 = PT2;
226
227 hs->offset[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 hs->offset[1] = MBEDTLS_HAVEGE_COLLECT_SIZE / 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000229}
230
231/*
232 * HAVEGE initialization
233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234void mbedtls_havege_init( mbedtls_havege_state *hs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 memset( hs, 0, sizeof( mbedtls_havege_state ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000237
238 havege_fill( hs );
239}
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241void mbedtls_havege_free( mbedtls_havege_state *hs )
Paul Bakkera317a982014-06-18 16:44:11 +0200242{
243 if( hs == NULL )
244 return;
245
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500246 mbedtls_platform_zeroize( hs, sizeof( mbedtls_havege_state ) );
Paul Bakkera317a982014-06-18 16:44:11 +0200247}
248
Paul Bakker5121ce52009-01-03 21:22:43 +0000249/*
250 * HAVEGE rand function
251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252int mbedtls_havege_random( void *p_rng, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000253{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000254 int val;
255 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_havege_state *hs = (mbedtls_havege_state *) p_rng;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000257 unsigned char *p = buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
Paul Bakkera3d195c2011-11-27 21:07:34 +0000259 while( len > 0 )
260 {
261 use_len = len;
262 if( use_len > sizeof(int) )
263 use_len = sizeof(int);
Paul Bakker5121ce52009-01-03 21:22:43 +0000264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 if( hs->offset[1] >= MBEDTLS_HAVEGE_COLLECT_SIZE )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000266 havege_fill( hs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000267
Paul Bakkera3d195c2011-11-27 21:07:34 +0000268 val = hs->pool[hs->offset[0]++];
269 val ^= hs->pool[hs->offset[1]++];
270
271 memcpy( p, &val, use_len );
Paul Bakker9af723c2014-05-01 13:03:14 +0200272
Paul Bakkera3d195c2011-11-27 21:07:34 +0000273 len -= use_len;
274 p += use_len;
275 }
276
277 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000278}
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#endif /* MBEDTLS_HAVEGE_C */