blob: 75e0e84f7a605c2394e820ae64027bf3e3fb090b [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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020029#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_HAVEGE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/havege.h"
34#include "mbedtls/timing.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050035#include "mbedtls/platform_util.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Gilles Peskine78462992019-06-07 16:38:28 +020037#include <stdint.h>
Paul Bakker23986e52011-04-24 08:57:21 +000038#include <string.h>
Paul Bakker23986e52011-04-24 08:57:21 +000039
Paul Bakker5121ce52009-01-03 21:22:43 +000040/* ------------------------------------------------------------------------
41 * On average, one iteration accesses two 8-word blocks in the havege WALK
42 * table, and generates 16 words in the RES array.
43 *
44 * The data read in the WALK table is updated and permuted after each use.
45 * The result of the hardware clock counter read is used for this update.
46 *
47 * 25 conditional tests are present. The conditional tests are grouped in
48 * two nested groups of 12 conditional tests and 1 test that controls the
49 * permutation; on average, there should be 6 tests executed and 3 of them
50 * should be mispredicted.
51 * ------------------------------------------------------------------------
52 */
53
Gilles Peskine78462992019-06-07 16:38:28 +020054#define SWAP(X,Y) { uint32_t *T = (X); (X) = (Y); (Y) = T; }
Paul Bakker5121ce52009-01-03 21:22:43 +000055
56#define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
57#define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
58
59#define TST1_LEAVE U1++; }
60#define TST2_LEAVE U2++; }
61
62#define ONE_ITERATION \
63 \
64 PTEST = PT1 >> 20; \
65 \
66 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
67 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
68 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
69 \
70 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
71 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
72 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
73 \
74 PTX = (PT1 >> 18) & 7; \
75 PT1 &= 0x1FFF; \
76 PT2 &= 0x1FFF; \
Gilles Peskine78462992019-06-07 16:38:28 +020077 CLK = (uint32_t) mbedtls_timing_hardclock(); \
Paul Bakker5121ce52009-01-03 21:22:43 +000078 \
79 i = 0; \
80 A = &WALK[PT1 ]; RES[i++] ^= *A; \
81 B = &WALK[PT2 ]; RES[i++] ^= *B; \
82 C = &WALK[PT1 ^ 1]; RES[i++] ^= *C; \
83 D = &WALK[PT2 ^ 4]; RES[i++] ^= *D; \
84 \
85 IN = (*A >> (1)) ^ (*A << (31)) ^ CLK; \
86 *A = (*B >> (2)) ^ (*B << (30)) ^ CLK; \
87 *B = IN ^ U1; \
88 *C = (*C >> (3)) ^ (*C << (29)) ^ CLK; \
89 *D = (*D >> (4)) ^ (*D << (28)) ^ CLK; \
90 \
91 A = &WALK[PT1 ^ 2]; RES[i++] ^= *A; \
92 B = &WALK[PT2 ^ 2]; RES[i++] ^= *B; \
93 C = &WALK[PT1 ^ 3]; RES[i++] ^= *C; \
94 D = &WALK[PT2 ^ 6]; RES[i++] ^= *D; \
95 \
96 if( PTEST & 1 ) SWAP( A, C ); \
97 \
98 IN = (*A >> (5)) ^ (*A << (27)) ^ CLK; \
99 *A = (*B >> (6)) ^ (*B << (26)) ^ CLK; \
Gilles Peskine78462992019-06-07 16:38:28 +0200100 *B = IN; CLK = (uint32_t) mbedtls_timing_hardclock(); \
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 *C = (*C >> (7)) ^ (*C << (25)) ^ CLK; \
102 *D = (*D >> (8)) ^ (*D << (24)) ^ CLK; \
103 \
104 A = &WALK[PT1 ^ 4]; \
105 B = &WALK[PT2 ^ 1]; \
106 \
107 PTEST = PT2 >> 1; \
108 \
109 PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]); \
110 PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8); \
111 PTY = (PT2 >> 10) & 7; \
112 \
113 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
114 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
115 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
116 \
117 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
118 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
119 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
120 \
121 C = &WALK[PT1 ^ 5]; \
122 D = &WALK[PT2 ^ 5]; \
123 \
124 RES[i++] ^= *A; \
125 RES[i++] ^= *B; \
126 RES[i++] ^= *C; \
127 RES[i++] ^= *D; \
128 \
129 IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK; \
130 *A = (*B >> (10)) ^ (*B << (22)) ^ CLK; \
131 *B = IN ^ U2; \
132 *C = (*C >> (11)) ^ (*C << (21)) ^ CLK; \
133 *D = (*D >> (12)) ^ (*D << (20)) ^ CLK; \
134 \
135 A = &WALK[PT1 ^ 6]; RES[i++] ^= *A; \
136 B = &WALK[PT2 ^ 3]; RES[i++] ^= *B; \
137 C = &WALK[PT1 ^ 7]; RES[i++] ^= *C; \
138 D = &WALK[PT2 ^ 7]; RES[i++] ^= *D; \
139 \
140 IN = (*A >> (13)) ^ (*A << (19)) ^ CLK; \
141 *A = (*B >> (14)) ^ (*B << (18)) ^ CLK; \
142 *B = IN; \
143 *C = (*C >> (15)) ^ (*C << (17)) ^ CLK; \
144 *D = (*D >> (16)) ^ (*D << (16)) ^ CLK; \
145 \
Paul Bakker66d5d072014-06-17 16:39:18 +0200146 PT1 = ( RES[( i - 8 ) ^ PTX] ^ \
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 WALK[PT1 ^ PTX ^ 7] ) & (~1); \
148 PT1 ^= (PT2 ^ 0x10) & 0x10; \
149 \
150 for( n++, i = 0; i < 16; i++ ) \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 hs->pool[n % MBEDTLS_HAVEGE_COLLECT_SIZE] ^= RES[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
153/*
154 * Entropy gathering function
155 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156static void havege_fill( mbedtls_havege_state *hs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000157{
Gilles Peskine78462992019-06-07 16:38:28 +0200158 size_t n = 0;
Gilles Peskinebc2adf92019-06-24 15:45:09 +0200159 size_t i;
Gilles Peskine78462992019-06-07 16:38:28 +0200160 uint32_t U1, U2, *A, *B, *C, *D;
161 uint32_t PT1, PT2, *WALK, RES[16];
162 uint32_t PTX, PTY, CLK, PTEST, IN;
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
164 WALK = hs->WALK;
165 PT1 = hs->PT1;
166 PT2 = hs->PT2;
167
168 PTX = U1 = 0;
169 PTY = U2 = 0;
170
Simon Butcher65b1fa62016-05-23 23:18:26 +0100171 (void)PTX;
172
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 memset( RES, 0, sizeof( RES ) );
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 while( n < MBEDTLS_HAVEGE_COLLECT_SIZE * 4 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 {
177 ONE_ITERATION
178 ONE_ITERATION
179 ONE_ITERATION
180 ONE_ITERATION
181 }
182
183 hs->PT1 = PT1;
184 hs->PT2 = PT2;
185
186 hs->offset[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 hs->offset[1] = MBEDTLS_HAVEGE_COLLECT_SIZE / 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000188}
189
190/*
191 * HAVEGE initialization
192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193void mbedtls_havege_init( mbedtls_havege_state *hs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000194{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 memset( hs, 0, sizeof( mbedtls_havege_state ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
197 havege_fill( hs );
198}
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200void mbedtls_havege_free( mbedtls_havege_state *hs )
Paul Bakkera317a982014-06-18 16:44:11 +0200201{
202 if( hs == NULL )
203 return;
204
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500205 mbedtls_platform_zeroize( hs, sizeof( mbedtls_havege_state ) );
Paul Bakkera317a982014-06-18 16:44:11 +0200206}
207
Paul Bakker5121ce52009-01-03 21:22:43 +0000208/*
209 * HAVEGE rand function
210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211int mbedtls_havege_random( void *p_rng, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000212{
Gilles Peskine78462992019-06-07 16:38:28 +0200213 uint32_t val;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000214 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_havege_state *hs = (mbedtls_havege_state *) p_rng;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000216 unsigned char *p = buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
Paul Bakkera3d195c2011-11-27 21:07:34 +0000218 while( len > 0 )
219 {
220 use_len = len;
Gilles Peskine78462992019-06-07 16:38:28 +0200221 if( use_len > sizeof( val ) )
222 use_len = sizeof( val );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 if( hs->offset[1] >= MBEDTLS_HAVEGE_COLLECT_SIZE )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000225 havege_fill( hs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000226
Paul Bakkera3d195c2011-11-27 21:07:34 +0000227 val = hs->pool[hs->offset[0]++];
228 val ^= hs->pool[hs->offset[1]++];
229
230 memcpy( p, &val, use_len );
Paul Bakker9af723c2014-05-01 13:03:14 +0200231
Paul Bakkera3d195c2011-11-27 21:07:34 +0000232 len -= use_len;
233 p += use_len;
234 }
235
236 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000237}
238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#endif /* MBEDTLS_HAVEGE_C */