2 * \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 * The HAVEGE RNG was designed by Andre Seznec in 2002.
24 * http://www.irisa.fr/caps/projects/hipsor/publi.php
26 * Contact: seznec(at)irisa_dot_fr - orocheco(at)irisa_dot_fr
29 #if !defined(MBEDTLS_CONFIG_FILE)
30 #include "mbedtls/config.h"
32 #include MBEDTLS_CONFIG_FILE
35 #if defined(MBEDTLS_HAVEGE_C)
37 #include "mbedtls/havege.h"
38 #include "mbedtls/timing.h"
42 /* Implementation that should never be optimized out by the compiler */
43 static void mbedtls_zeroize( void *v
, size_t n
) {
44 volatile unsigned char *p
= v
; while( n
-- ) *p
++ = 0;
47 /* ------------------------------------------------------------------------
48 * On average, one iteration accesses two 8-word blocks in the havege WALK
49 * table, and generates 16 words in the RES array.
51 * The data read in the WALK table is updated and permuted after each use.
52 * The result of the hardware clock counter read is used for this update.
54 * 25 conditional tests are present. The conditional tests are grouped in
55 * two nested groups of 12 conditional tests and 1 test that controls the
56 * permutation; on average, there should be 6 tests executed and 3 of them
57 * should be mispredicted.
58 * ------------------------------------------------------------------------
61 #define SWAP(X,Y) { int *T = X; X = Y; Y = T; }
63 #define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
64 #define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
66 #define TST1_LEAVE U1++; }
67 #define TST2_LEAVE U2++; }
69 #define ONE_ITERATION \
73 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
74 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
75 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
77 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
78 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
79 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
81 PTX = (PT1 >> 18) & 7; \
84 CLK = (int) mbedtls_timing_hardclock(); \
87 A = &WALK[PT1 ]; RES[i++] ^= *A; \
88 B = &WALK[PT2 ]; RES[i++] ^= *B; \
89 C = &WALK[PT1 ^ 1]; RES[i++] ^= *C; \
90 D = &WALK[PT2 ^ 4]; RES[i++] ^= *D; \
92 IN = (*A >> (1)) ^ (*A << (31)) ^ CLK; \
93 *A = (*B >> (2)) ^ (*B << (30)) ^ CLK; \
95 *C = (*C >> (3)) ^ (*C << (29)) ^ CLK; \
96 *D = (*D >> (4)) ^ (*D << (28)) ^ CLK; \
98 A = &WALK[PT1 ^ 2]; RES[i++] ^= *A; \
99 B = &WALK[PT2 ^ 2]; RES[i++] ^= *B; \
100 C = &WALK[PT1 ^ 3]; RES[i++] ^= *C; \
101 D = &WALK[PT2 ^ 6]; RES[i++] ^= *D; \
103 if( PTEST & 1 ) SWAP( A, C ); \
105 IN = (*A >> (5)) ^ (*A << (27)) ^ CLK; \
106 *A = (*B >> (6)) ^ (*B << (26)) ^ CLK; \
107 *B = IN; CLK = (int) mbedtls_timing_hardclock(); \
108 *C = (*C >> (7)) ^ (*C << (25)) ^ CLK; \
109 *D = (*D >> (8)) ^ (*D << (24)) ^ CLK; \
111 A = &WALK[PT1 ^ 4]; \
112 B = &WALK[PT2 ^ 1]; \
116 PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]); \
117 PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8); \
118 PTY = (PT2 >> 10) & 7; \
120 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
121 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
122 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
124 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
125 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
126 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
128 C = &WALK[PT1 ^ 5]; \
129 D = &WALK[PT2 ^ 5]; \
136 IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK; \
137 *A = (*B >> (10)) ^ (*B << (22)) ^ CLK; \
139 *C = (*C >> (11)) ^ (*C << (21)) ^ CLK; \
140 *D = (*D >> (12)) ^ (*D << (20)) ^ CLK; \
142 A = &WALK[PT1 ^ 6]; RES[i++] ^= *A; \
143 B = &WALK[PT2 ^ 3]; RES[i++] ^= *B; \
144 C = &WALK[PT1 ^ 7]; RES[i++] ^= *C; \
145 D = &WALK[PT2 ^ 7]; RES[i++] ^= *D; \
147 IN = (*A >> (13)) ^ (*A << (19)) ^ CLK; \
148 *A = (*B >> (14)) ^ (*B << (18)) ^ CLK; \
150 *C = (*C >> (15)) ^ (*C << (17)) ^ CLK; \
151 *D = (*D >> (16)) ^ (*D << (16)) ^ CLK; \
153 PT1 = ( RES[( i - 8 ) ^ PTX] ^ \
154 WALK[PT1 ^ PTX ^ 7] ) & (~1); \
155 PT1 ^= (PT2 ^ 0x10) & 0x10; \
157 for( n++, i = 0; i < 16; i++ ) \
158 hs->pool[n % MBEDTLS_HAVEGE_COLLECT_SIZE] ^= RES[i];
161 * Entropy gathering function
163 static void havege_fill( mbedtls_havege_state
*hs
)
166 int U1
, U2
, *A
, *B
, *C
, *D
;
167 int PT1
, PT2
, *WALK
, RES
[16];
168 int PTX
, PTY
, CLK
, PTEST
, IN
;
179 memset( RES
, 0, sizeof( RES
) );
181 while( n
< MBEDTLS_HAVEGE_COLLECT_SIZE
* 4 )
193 hs
->offset
[1] = MBEDTLS_HAVEGE_COLLECT_SIZE
/ 2;
197 * HAVEGE initialization
199 void mbedtls_havege_init( mbedtls_havege_state
*hs
)
201 memset( hs
, 0, sizeof( mbedtls_havege_state
) );
206 void mbedtls_havege_free( mbedtls_havege_state
*hs
)
211 mbedtls_zeroize( hs
, sizeof( mbedtls_havege_state
) );
215 * HAVEGE rand function
217 int mbedtls_havege_random( void *p_rng
, unsigned char *buf
, size_t len
)
221 mbedtls_havege_state
*hs
= (mbedtls_havege_state
*) p_rng
;
222 unsigned char *p
= buf
;
227 if( use_len
> sizeof(int) )
228 use_len
= sizeof(int);
230 if( hs
->offset
[1] >= MBEDTLS_HAVEGE_COLLECT_SIZE
)
233 val
= hs
->pool
[hs
->offset
[0]++];
234 val
^= hs
->pool
[hs
->offset
[1]++];
236 memcpy( p
, &val
, use_len
);
245 #endif /* MBEDTLS_HAVEGE_C */