2 * \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: GPL-2.0
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * This file is part of mbed TLS (https://tls.mbed.org)
24 * The HAVEGE RNG was designed by Andre Seznec in 2002.
26 * http://www.irisa.fr/caps/projects/hipsor/publi.php
28 * Contact: seznec(at)irisa_dot_fr - orocheco(at)irisa_dot_fr
31 #if !defined(MBEDTLS_CONFIG_FILE)
32 #include "mbedtls/config.h"
34 #include MBEDTLS_CONFIG_FILE
37 #if defined(MBEDTLS_HAVEGE_C)
39 #include "mbedtls/havege.h"
40 #include "mbedtls/timing.h"
44 /* Implementation that should never be optimized out by the compiler */
45 static void mbedtls_zeroize( void *v
, size_t n
) {
46 volatile unsigned char *p
= v
; while( n
-- ) *p
++ = 0;
49 /* ------------------------------------------------------------------------
50 * On average, one iteration accesses two 8-word blocks in the havege WALK
51 * table, and generates 16 words in the RES array.
53 * The data read in the WALK table is updated and permuted after each use.
54 * The result of the hardware clock counter read is used for this update.
56 * 25 conditional tests are present. The conditional tests are grouped in
57 * two nested groups of 12 conditional tests and 1 test that controls the
58 * permutation; on average, there should be 6 tests executed and 3 of them
59 * should be mispredicted.
60 * ------------------------------------------------------------------------
63 #define SWAP(X,Y) { int *T = X; X = Y; Y = T; }
65 #define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
66 #define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
68 #define TST1_LEAVE U1++; }
69 #define TST2_LEAVE U2++; }
71 #define ONE_ITERATION \
75 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
76 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
77 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
79 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
80 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
81 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
83 PTX = (PT1 >> 18) & 7; \
86 CLK = (int) mbedtls_timing_hardclock(); \
89 A = &WALK[PT1 ]; RES[i++] ^= *A; \
90 B = &WALK[PT2 ]; RES[i++] ^= *B; \
91 C = &WALK[PT1 ^ 1]; RES[i++] ^= *C; \
92 D = &WALK[PT2 ^ 4]; RES[i++] ^= *D; \
94 IN = (*A >> (1)) ^ (*A << (31)) ^ CLK; \
95 *A = (*B >> (2)) ^ (*B << (30)) ^ CLK; \
97 *C = (*C >> (3)) ^ (*C << (29)) ^ CLK; \
98 *D = (*D >> (4)) ^ (*D << (28)) ^ CLK; \
100 A = &WALK[PT1 ^ 2]; RES[i++] ^= *A; \
101 B = &WALK[PT2 ^ 2]; RES[i++] ^= *B; \
102 C = &WALK[PT1 ^ 3]; RES[i++] ^= *C; \
103 D = &WALK[PT2 ^ 6]; RES[i++] ^= *D; \
105 if( PTEST & 1 ) SWAP( A, C ); \
107 IN = (*A >> (5)) ^ (*A << (27)) ^ CLK; \
108 *A = (*B >> (6)) ^ (*B << (26)) ^ CLK; \
109 *B = IN; CLK = (int) mbedtls_timing_hardclock(); \
110 *C = (*C >> (7)) ^ (*C << (25)) ^ CLK; \
111 *D = (*D >> (8)) ^ (*D << (24)) ^ CLK; \
113 A = &WALK[PT1 ^ 4]; \
114 B = &WALK[PT2 ^ 1]; \
118 PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]); \
119 PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8); \
120 PTY = (PT2 >> 10) & 7; \
122 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
123 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
124 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
126 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
127 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
128 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
130 C = &WALK[PT1 ^ 5]; \
131 D = &WALK[PT2 ^ 5]; \
138 IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK; \
139 *A = (*B >> (10)) ^ (*B << (22)) ^ CLK; \
141 *C = (*C >> (11)) ^ (*C << (21)) ^ CLK; \
142 *D = (*D >> (12)) ^ (*D << (20)) ^ CLK; \
144 A = &WALK[PT1 ^ 6]; RES[i++] ^= *A; \
145 B = &WALK[PT2 ^ 3]; RES[i++] ^= *B; \
146 C = &WALK[PT1 ^ 7]; RES[i++] ^= *C; \
147 D = &WALK[PT2 ^ 7]; RES[i++] ^= *D; \
149 IN = (*A >> (13)) ^ (*A << (19)) ^ CLK; \
150 *A = (*B >> (14)) ^ (*B << (18)) ^ CLK; \
152 *C = (*C >> (15)) ^ (*C << (17)) ^ CLK; \
153 *D = (*D >> (16)) ^ (*D << (16)) ^ CLK; \
155 PT1 = ( RES[( i - 8 ) ^ PTX] ^ \
156 WALK[PT1 ^ PTX ^ 7] ) & (~1); \
157 PT1 ^= (PT2 ^ 0x10) & 0x10; \
159 for( n++, i = 0; i < 16; i++ ) \
160 hs->pool[n % MBEDTLS_HAVEGE_COLLECT_SIZE] ^= RES[i];
163 * Entropy gathering function
165 static void havege_fill( mbedtls_havege_state
*hs
)
168 int U1
, U2
, *A
, *B
, *C
, *D
;
169 int PT1
, PT2
, *WALK
, RES
[16];
170 int PTX
, PTY
, CLK
, PTEST
, IN
;
181 memset( RES
, 0, sizeof( RES
) );
183 while( n
< MBEDTLS_HAVEGE_COLLECT_SIZE
* 4 )
195 hs
->offset
[1] = MBEDTLS_HAVEGE_COLLECT_SIZE
/ 2;
199 * HAVEGE initialization
201 void mbedtls_havege_init( mbedtls_havege_state
*hs
)
203 memset( hs
, 0, sizeof( mbedtls_havege_state
) );
208 void mbedtls_havege_free( mbedtls_havege_state
*hs
)
213 mbedtls_zeroize( hs
, sizeof( mbedtls_havege_state
) );
217 * HAVEGE rand function
219 int mbedtls_havege_random( void *p_rng
, unsigned char *buf
, size_t len
)
223 mbedtls_havege_state
*hs
= (mbedtls_havege_state
*) p_rng
;
224 unsigned char *p
= buf
;
229 if( use_len
> sizeof(int) )
230 use_len
= sizeof(int);
232 if( hs
->offset
[1] >= MBEDTLS_HAVEGE_COLLECT_SIZE
)
235 val
= hs
->pool
[hs
->offset
[0]++];
236 val
^= hs
->pool
[hs
->offset
[1]++];
238 memcpy( p
, &val
, use_len
);
247 #endif /* MBEDTLS_HAVEGE_C */