blob: c139e1db03ec835c75952a8fd941686c1b270ba8 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The HAVEGE RNG was designed by Andre Seznec in 2002.
23 *
24 * http://www.irisa.fr/caps/projects/hipsor/publi.php
25 *
26 * Contact: seznec(at)irisa_dot_fr - orocheco(at)irisa_dot_fr
27 */
28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_HAVEGE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/havege.h"
38#include "mbedtls/timing.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Gilles Peskine04659a02019-06-17 15:12:51 +020041#include <limits.h>
Paul Bakker23986e52011-04-24 08:57:21 +000042#include <string.h>
Paul Bakker23986e52011-04-24 08:57:21 +000043
Gilles Peskine04659a02019-06-17 15:12:51 +020044/* If int isn't capable of storing 2^32 distinct values, the code of this
45 * module may cause a processor trap or a miscalculation. If int is more
46 * than 32 bits, the code may not calculate the intended values. */
47#if INT_MIN + 1 != -0x7fffffff
48#error "The HAVEGE module requires int to be exactly 32 bits, with INT_MIN = -2^31."
49#endif
50#if UINT_MAX != 0xffffffff
51#error "The HAVEGE module requires unsigned to be exactly 32 bits."
52#endif
53
Paul Bakker5121ce52009-01-03 21:22:43 +000054/* ------------------------------------------------------------------------
55 * On average, one iteration accesses two 8-word blocks in the havege WALK
56 * table, and generates 16 words in the RES array.
57 *
58 * The data read in the WALK table is updated and permuted after each use.
59 * The result of the hardware clock counter read is used for this update.
60 *
61 * 25 conditional tests are present. The conditional tests are grouped in
62 * two nested groups of 12 conditional tests and 1 test that controls the
63 * permutation; on average, there should be 6 tests executed and 3 of them
64 * should be mispredicted.
65 * ------------------------------------------------------------------------
66 */
67
Gilles Peskine31a4ba72019-06-17 15:01:08 +020068#define SWAP(X,Y) { unsigned *T = (X); (X) = (Y); (Y) = T; }
Paul Bakker5121ce52009-01-03 21:22:43 +000069
70#define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
71#define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
72
73#define TST1_LEAVE U1++; }
74#define TST2_LEAVE U2++; }
75
76#define ONE_ITERATION \
77 \
78 PTEST = PT1 >> 20; \
79 \
80 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
81 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
82 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
83 \
84 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
85 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
86 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
87 \
88 PTX = (PT1 >> 18) & 7; \
89 PT1 &= 0x1FFF; \
90 PT2 &= 0x1FFF; \
Gilles Peskine31a4ba72019-06-17 15:01:08 +020091 CLK = (unsigned) mbedtls_timing_hardclock(); \
Paul Bakker5121ce52009-01-03 21:22:43 +000092 \
93 i = 0; \
94 A = &WALK[PT1 ]; RES[i++] ^= *A; \
95 B = &WALK[PT2 ]; RES[i++] ^= *B; \
96 C = &WALK[PT1 ^ 1]; RES[i++] ^= *C; \
97 D = &WALK[PT2 ^ 4]; RES[i++] ^= *D; \
98 \
99 IN = (*A >> (1)) ^ (*A << (31)) ^ CLK; \
100 *A = (*B >> (2)) ^ (*B << (30)) ^ CLK; \
101 *B = IN ^ U1; \
102 *C = (*C >> (3)) ^ (*C << (29)) ^ CLK; \
103 *D = (*D >> (4)) ^ (*D << (28)) ^ CLK; \
104 \
105 A = &WALK[PT1 ^ 2]; RES[i++] ^= *A; \
106 B = &WALK[PT2 ^ 2]; RES[i++] ^= *B; \
107 C = &WALK[PT1 ^ 3]; RES[i++] ^= *C; \
108 D = &WALK[PT2 ^ 6]; RES[i++] ^= *D; \
109 \
110 if( PTEST & 1 ) SWAP( A, C ); \
111 \
112 IN = (*A >> (5)) ^ (*A << (27)) ^ CLK; \
113 *A = (*B >> (6)) ^ (*B << (26)) ^ CLK; \
Gilles Peskine31a4ba72019-06-17 15:01:08 +0200114 *B = IN; CLK = (unsigned) mbedtls_timing_hardclock(); \
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 *C = (*C >> (7)) ^ (*C << (25)) ^ CLK; \
116 *D = (*D >> (8)) ^ (*D << (24)) ^ CLK; \
117 \
118 A = &WALK[PT1 ^ 4]; \
119 B = &WALK[PT2 ^ 1]; \
120 \
121 PTEST = PT2 >> 1; \
122 \
123 PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]); \
124 PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8); \
125 PTY = (PT2 >> 10) & 7; \
126 \
127 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
128 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
129 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
130 \
131 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
132 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
133 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
134 \
135 C = &WALK[PT1 ^ 5]; \
136 D = &WALK[PT2 ^ 5]; \
137 \
138 RES[i++] ^= *A; \
139 RES[i++] ^= *B; \
140 RES[i++] ^= *C; \
141 RES[i++] ^= *D; \
142 \
143 IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK; \
144 *A = (*B >> (10)) ^ (*B << (22)) ^ CLK; \
145 *B = IN ^ U2; \
146 *C = (*C >> (11)) ^ (*C << (21)) ^ CLK; \
147 *D = (*D >> (12)) ^ (*D << (20)) ^ CLK; \
148 \
149 A = &WALK[PT1 ^ 6]; RES[i++] ^= *A; \
150 B = &WALK[PT2 ^ 3]; RES[i++] ^= *B; \
151 C = &WALK[PT1 ^ 7]; RES[i++] ^= *C; \
152 D = &WALK[PT2 ^ 7]; RES[i++] ^= *D; \
153 \
154 IN = (*A >> (13)) ^ (*A << (19)) ^ CLK; \
155 *A = (*B >> (14)) ^ (*B << (18)) ^ CLK; \
156 *B = IN; \
157 *C = (*C >> (15)) ^ (*C << (17)) ^ CLK; \
158 *D = (*D >> (16)) ^ (*D << (16)) ^ CLK; \
159 \
Paul Bakker66d5d072014-06-17 16:39:18 +0200160 PT1 = ( RES[( i - 8 ) ^ PTX] ^ \
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 WALK[PT1 ^ PTX ^ 7] ) & (~1); \
162 PT1 ^= (PT2 ^ 0x10) & 0x10; \
163 \
164 for( n++, i = 0; i < 16; i++ ) \
Gilles Peskine31a4ba72019-06-17 15:01:08 +0200165 POOL[n % MBEDTLS_HAVEGE_COLLECT_SIZE] ^= RES[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
167/*
168 * Entropy gathering function
169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170static void havege_fill( mbedtls_havege_state *hs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000171{
Gilles Peskine31a4ba72019-06-17 15:01:08 +0200172 unsigned i, n = 0;
173 unsigned U1, U2, *A, *B, *C, *D;
174 unsigned PT1, PT2, *WALK, *POOL, RES[16];
175 unsigned PTX, PTY, CLK, PTEST, IN;
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
Gilles Peskine31a4ba72019-06-17 15:01:08 +0200177 WALK = (unsigned *) hs->WALK;
178 POOL = (unsigned *) hs->pool;
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 PT1 = hs->PT1;
180 PT2 = hs->PT2;
181
182 PTX = U1 = 0;
183 PTY = U2 = 0;
184
Simon Butcher65b1fa62016-05-23 23:18:26 +0100185 (void)PTX;
186
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 memset( RES, 0, sizeof( RES ) );
188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 while( n < MBEDTLS_HAVEGE_COLLECT_SIZE * 4 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 {
191 ONE_ITERATION
192 ONE_ITERATION
193 ONE_ITERATION
194 ONE_ITERATION
195 }
196
197 hs->PT1 = PT1;
198 hs->PT2 = PT2;
199
200 hs->offset[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 hs->offset[1] = MBEDTLS_HAVEGE_COLLECT_SIZE / 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000202}
203
204/*
205 * HAVEGE initialization
206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207void mbedtls_havege_init( mbedtls_havege_state *hs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000208{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 memset( hs, 0, sizeof( mbedtls_havege_state ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
211 havege_fill( hs );
212}
213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214void mbedtls_havege_free( mbedtls_havege_state *hs )
Paul Bakkera317a982014-06-18 16:44:11 +0200215{
216 if( hs == NULL )
217 return;
218
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500219 mbedtls_platform_zeroize( hs, sizeof( mbedtls_havege_state ) );
Paul Bakkera317a982014-06-18 16:44:11 +0200220}
221
Paul Bakker5121ce52009-01-03 21:22:43 +0000222/*
223 * HAVEGE rand function
224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225int mbedtls_havege_random( void *p_rng, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000226{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000227 int val;
228 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_havege_state *hs = (mbedtls_havege_state *) p_rng;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000230 unsigned char *p = buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
Paul Bakkera3d195c2011-11-27 21:07:34 +0000232 while( len > 0 )
233 {
234 use_len = len;
235 if( use_len > sizeof(int) )
236 use_len = sizeof(int);
Paul Bakker5121ce52009-01-03 21:22:43 +0000237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 if( hs->offset[1] >= MBEDTLS_HAVEGE_COLLECT_SIZE )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000239 havege_fill( hs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
Paul Bakkera3d195c2011-11-27 21:07:34 +0000241 val = hs->pool[hs->offset[0]++];
242 val ^= hs->pool[hs->offset[1]++];
243
244 memcpy( p, &val, use_len );
Paul Bakker9af723c2014-05-01 13:03:14 +0200245
Paul Bakkera3d195c2011-11-27 21:07:34 +0000246 len -= use_len;
247 p += use_len;
248 }
249
250 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251}
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#endif /* MBEDTLS_HAVEGE_C */