blob: 2a360a150ce0b2995e246d48255f8806c3a71d3a [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 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
20 * The HAVEGE RNG was designed by Andre Seznec in 2002.
21 *
22 * http://www.irisa.fr/caps/projects/hipsor/publi.php
23 *
24 * Contact: seznec(at)irisa_dot_fr - orocheco(at)irisa_dot_fr
25 */
26
Gilles Peskinedb09ef62020-06-03 01:43:33 +020027#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_HAVEGE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/havege.h"
32#include "mbedtls/timing.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050033#include "mbedtls/platform_util.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Gilles Peskine78462992019-06-07 16:38:28 +020035#include <stdint.h>
Paul Bakker23986e52011-04-24 08:57:21 +000036#include <string.h>
Paul Bakker23986e52011-04-24 08:57:21 +000037
Paul Bakker5121ce52009-01-03 21:22:43 +000038/* ------------------------------------------------------------------------
39 * On average, one iteration accesses two 8-word blocks in the havege WALK
40 * table, and generates 16 words in the RES array.
41 *
42 * The data read in the WALK table is updated and permuted after each use.
43 * The result of the hardware clock counter read is used for this update.
44 *
45 * 25 conditional tests are present. The conditional tests are grouped in
46 * two nested groups of 12 conditional tests and 1 test that controls the
47 * permutation; on average, there should be 6 tests executed and 3 of them
48 * should be mispredicted.
49 * ------------------------------------------------------------------------
50 */
51
Gilles Peskine78462992019-06-07 16:38:28 +020052#define SWAP(X,Y) { uint32_t *T = (X); (X) = (Y); (Y) = T; }
Paul Bakker5121ce52009-01-03 21:22:43 +000053
54#define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
55#define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
56
57#define TST1_LEAVE U1++; }
58#define TST2_LEAVE U2++; }
59
60#define ONE_ITERATION \
61 \
62 PTEST = PT1 >> 20; \
63 \
64 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
65 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
66 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
67 \
68 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
69 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
70 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
71 \
72 PTX = (PT1 >> 18) & 7; \
73 PT1 &= 0x1FFF; \
74 PT2 &= 0x1FFF; \
Gilles Peskine78462992019-06-07 16:38:28 +020075 CLK = (uint32_t) mbedtls_timing_hardclock(); \
Paul Bakker5121ce52009-01-03 21:22:43 +000076 \
77 i = 0; \
78 A = &WALK[PT1 ]; RES[i++] ^= *A; \
79 B = &WALK[PT2 ]; RES[i++] ^= *B; \
80 C = &WALK[PT1 ^ 1]; RES[i++] ^= *C; \
81 D = &WALK[PT2 ^ 4]; RES[i++] ^= *D; \
82 \
83 IN = (*A >> (1)) ^ (*A << (31)) ^ CLK; \
84 *A = (*B >> (2)) ^ (*B << (30)) ^ CLK; \
85 *B = IN ^ U1; \
86 *C = (*C >> (3)) ^ (*C << (29)) ^ CLK; \
87 *D = (*D >> (4)) ^ (*D << (28)) ^ CLK; \
88 \
89 A = &WALK[PT1 ^ 2]; RES[i++] ^= *A; \
90 B = &WALK[PT2 ^ 2]; RES[i++] ^= *B; \
91 C = &WALK[PT1 ^ 3]; RES[i++] ^= *C; \
92 D = &WALK[PT2 ^ 6]; RES[i++] ^= *D; \
93 \
94 if( PTEST & 1 ) SWAP( A, C ); \
95 \
96 IN = (*A >> (5)) ^ (*A << (27)) ^ CLK; \
97 *A = (*B >> (6)) ^ (*B << (26)) ^ CLK; \
Gilles Peskine78462992019-06-07 16:38:28 +020098 *B = IN; CLK = (uint32_t) mbedtls_timing_hardclock(); \
Paul Bakker5121ce52009-01-03 21:22:43 +000099 *C = (*C >> (7)) ^ (*C << (25)) ^ CLK; \
100 *D = (*D >> (8)) ^ (*D << (24)) ^ CLK; \
101 \
102 A = &WALK[PT1 ^ 4]; \
103 B = &WALK[PT2 ^ 1]; \
104 \
105 PTEST = PT2 >> 1; \
106 \
107 PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]); \
108 PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8); \
109 PTY = (PT2 >> 10) & 7; \
110 \
111 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
112 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
113 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
114 \
115 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
116 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
117 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
118 \
119 C = &WALK[PT1 ^ 5]; \
120 D = &WALK[PT2 ^ 5]; \
121 \
122 RES[i++] ^= *A; \
123 RES[i++] ^= *B; \
124 RES[i++] ^= *C; \
125 RES[i++] ^= *D; \
126 \
127 IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK; \
128 *A = (*B >> (10)) ^ (*B << (22)) ^ CLK; \
129 *B = IN ^ U2; \
130 *C = (*C >> (11)) ^ (*C << (21)) ^ CLK; \
131 *D = (*D >> (12)) ^ (*D << (20)) ^ CLK; \
132 \
133 A = &WALK[PT1 ^ 6]; RES[i++] ^= *A; \
134 B = &WALK[PT2 ^ 3]; RES[i++] ^= *B; \
135 C = &WALK[PT1 ^ 7]; RES[i++] ^= *C; \
136 D = &WALK[PT2 ^ 7]; RES[i++] ^= *D; \
137 \
138 IN = (*A >> (13)) ^ (*A << (19)) ^ CLK; \
139 *A = (*B >> (14)) ^ (*B << (18)) ^ CLK; \
140 *B = IN; \
141 *C = (*C >> (15)) ^ (*C << (17)) ^ CLK; \
142 *D = (*D >> (16)) ^ (*D << (16)) ^ CLK; \
143 \
Paul Bakker66d5d072014-06-17 16:39:18 +0200144 PT1 = ( RES[( i - 8 ) ^ PTX] ^ \
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 WALK[PT1 ^ PTX ^ 7] ) & (~1); \
146 PT1 ^= (PT2 ^ 0x10) & 0x10; \
147 \
148 for( n++, i = 0; i < 16; i++ ) \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 hs->pool[n % MBEDTLS_HAVEGE_COLLECT_SIZE] ^= RES[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151/*
152 * Entropy gathering function
153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154static void havege_fill( mbedtls_havege_state *hs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000155{
Gilles Peskine78462992019-06-07 16:38:28 +0200156 size_t n = 0;
Gilles Peskinebc2adf92019-06-24 15:45:09 +0200157 size_t i;
Gilles Peskine78462992019-06-07 16:38:28 +0200158 uint32_t U1, U2, *A, *B, *C, *D;
159 uint32_t PT1, PT2, *WALK, RES[16];
160 uint32_t PTX, PTY, CLK, PTEST, IN;
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
162 WALK = hs->WALK;
163 PT1 = hs->PT1;
164 PT2 = hs->PT2;
165
166 PTX = U1 = 0;
167 PTY = U2 = 0;
168
Simon Butcher65b1fa62016-05-23 23:18:26 +0100169 (void)PTX;
170
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 memset( RES, 0, sizeof( RES ) );
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 while( n < MBEDTLS_HAVEGE_COLLECT_SIZE * 4 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 {
175 ONE_ITERATION
176 ONE_ITERATION
177 ONE_ITERATION
178 ONE_ITERATION
179 }
180
181 hs->PT1 = PT1;
182 hs->PT2 = PT2;
183
184 hs->offset[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 hs->offset[1] = MBEDTLS_HAVEGE_COLLECT_SIZE / 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000186}
187
188/*
189 * HAVEGE initialization
190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191void mbedtls_havege_init( mbedtls_havege_state *hs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000192{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 memset( hs, 0, sizeof( mbedtls_havege_state ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
195 havege_fill( hs );
196}
197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198void mbedtls_havege_free( mbedtls_havege_state *hs )
Paul Bakkera317a982014-06-18 16:44:11 +0200199{
200 if( hs == NULL )
201 return;
202
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500203 mbedtls_platform_zeroize( hs, sizeof( mbedtls_havege_state ) );
Paul Bakkera317a982014-06-18 16:44:11 +0200204}
205
Paul Bakker5121ce52009-01-03 21:22:43 +0000206/*
207 * HAVEGE rand function
208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209int mbedtls_havege_random( void *p_rng, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000210{
Gilles Peskine78462992019-06-07 16:38:28 +0200211 uint32_t val;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000212 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_havege_state *hs = (mbedtls_havege_state *) p_rng;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000214 unsigned char *p = buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000215
Paul Bakkera3d195c2011-11-27 21:07:34 +0000216 while( len > 0 )
217 {
218 use_len = len;
Gilles Peskine78462992019-06-07 16:38:28 +0200219 if( use_len > sizeof( val ) )
220 use_len = sizeof( val );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 if( hs->offset[1] >= MBEDTLS_HAVEGE_COLLECT_SIZE )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000223 havege_fill( hs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000224
Paul Bakkera3d195c2011-11-27 21:07:34 +0000225 val = hs->pool[hs->offset[0]++];
226 val ^= hs->pool[hs->offset[1]++];
227
228 memcpy( p, &val, use_len );
Paul Bakker9af723c2014-05-01 13:03:14 +0200229
Paul Bakkera3d195c2011-11-27 21:07:34 +0000230 len -= use_len;
231 p += use_len;
232 }
233
234 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000235}
236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#endif /* MBEDTLS_HAVEGE_C */