xref: /reactos/dll/3rdparty/mbedtls/havege.c (revision 2b933529)
1 /**
2  *  \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion
3  *
4  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6  *
7  *  This file is provided under the Apache License 2.0, or the
8  *  GNU General Public License v2.0 or later.
9  *
10  *  **********
11  *  Apache License 2.0:
12  *
13  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
14  *  not use this file except in compliance with the License.
15  *  You may obtain a copy of the License at
16  *
17  *  http://www.apache.org/licenses/LICENSE-2.0
18  *
19  *  Unless required by applicable law or agreed to in writing, software
20  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the License for the specific language governing permissions and
23  *  limitations under the License.
24  *
25  *  **********
26  *
27  *  **********
28  *  GNU General Public License v2.0 or later:
29  *
30  *  This program is free software; you can redistribute it and/or modify
31  *  it under the terms of the GNU General Public License as published by
32  *  the Free Software Foundation; either version 2 of the License, or
33  *  (at your option) any later version.
34  *
35  *  This program is distributed in the hope that it will be useful,
36  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
37  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38  *  GNU General Public License for more details.
39  *
40  *  You should have received a copy of the GNU General Public License along
41  *  with this program; if not, write to the Free Software Foundation, Inc.,
42  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43  *
44  *  **********
45  *
46  *  This file is part of mbed TLS (https://tls.mbed.org)
47  */
48 /*
49  *  The HAVEGE RNG was designed by Andre Seznec in 2002.
50  *
51  *  http://www.irisa.fr/caps/projects/hipsor/publi.php
52  *
53  *  Contact: seznec(at)irisa_dot_fr - orocheco(at)irisa_dot_fr
54  */
55 
56 #if !defined(MBEDTLS_CONFIG_FILE)
57 #include "mbedtls/config.h"
58 #else
59 #include MBEDTLS_CONFIG_FILE
60 #endif
61 
62 #if defined(MBEDTLS_HAVEGE_C)
63 
64 #include "mbedtls/havege.h"
65 #include "mbedtls/timing.h"
66 
67 #include <limits.h>
68 #include <string.h>
69 
70 /* If int isn't capable of storing 2^32 distinct values, the code of this
71  * module may cause a processor trap or a miscalculation. If int is more
72  * than 32 bits, the code may not calculate the intended values. */
73 #if INT_MIN + 1 != -0x7fffffff
74 #error "The HAVEGE module requires int to be exactly 32 bits, with INT_MIN = -2^31."
75 #endif
76 #if UINT_MAX != 0xffffffff
77 #error "The HAVEGE module requires unsigned to be exactly 32 bits."
78 #endif
79 
80 /* Implementation that should never be optimized out by the compiler */
81 static void mbedtls_zeroize( void *v, size_t n ) {
82     volatile unsigned char *p = v; while( n-- ) *p++ = 0;
83 }
84 
85 /* ------------------------------------------------------------------------
86  * On average, one iteration accesses two 8-word blocks in the havege WALK
87  * table, and generates 16 words in the RES array.
88  *
89  * The data read in the WALK table is updated and permuted after each use.
90  * The result of the hardware clock counter read is used  for this update.
91  *
92  * 25 conditional tests are present.  The conditional tests are grouped in
93  * two nested  groups of 12 conditional tests and 1 test that controls the
94  * permutation; on average, there should be 6 tests executed and 3 of them
95  * should be mispredicted.
96  * ------------------------------------------------------------------------
97  */
98 
99 #define SWAP(X,Y) { unsigned *T = (X); (X) = (Y); (Y) = T; }
100 
101 #define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
102 #define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
103 
104 #define TST1_LEAVE U1++; }
105 #define TST2_LEAVE U2++; }
106 
107 #define ONE_ITERATION                                   \
108                                                         \
109     PTEST = PT1 >> 20;                                  \
110                                                         \
111     TST1_ENTER  TST1_ENTER  TST1_ENTER  TST1_ENTER      \
112     TST1_ENTER  TST1_ENTER  TST1_ENTER  TST1_ENTER      \
113     TST1_ENTER  TST1_ENTER  TST1_ENTER  TST1_ENTER      \
114                                                         \
115     TST1_LEAVE  TST1_LEAVE  TST1_LEAVE  TST1_LEAVE      \
116     TST1_LEAVE  TST1_LEAVE  TST1_LEAVE  TST1_LEAVE      \
117     TST1_LEAVE  TST1_LEAVE  TST1_LEAVE  TST1_LEAVE      \
118                                                         \
119     PTX = (PT1 >> 18) & 7;                              \
120     PT1 &= 0x1FFF;                                      \
121     PT2 &= 0x1FFF;                                      \
122     CLK = (unsigned) mbedtls_timing_hardclock();        \
123                                                         \
124     i = 0;                                              \
125     A = &WALK[PT1    ]; RES[i++] ^= *A;                 \
126     B = &WALK[PT2    ]; RES[i++] ^= *B;                 \
127     C = &WALK[PT1 ^ 1]; RES[i++] ^= *C;                 \
128     D = &WALK[PT2 ^ 4]; RES[i++] ^= *D;                 \
129                                                         \
130     IN = (*A >> (1)) ^ (*A << (31)) ^ CLK;              \
131     *A = (*B >> (2)) ^ (*B << (30)) ^ CLK;              \
132     *B = IN ^ U1;                                       \
133     *C = (*C >> (3)) ^ (*C << (29)) ^ CLK;              \
134     *D = (*D >> (4)) ^ (*D << (28)) ^ CLK;              \
135                                                         \
136     A = &WALK[PT1 ^ 2]; RES[i++] ^= *A;                 \
137     B = &WALK[PT2 ^ 2]; RES[i++] ^= *B;                 \
138     C = &WALK[PT1 ^ 3]; RES[i++] ^= *C;                 \
139     D = &WALK[PT2 ^ 6]; RES[i++] ^= *D;                 \
140                                                         \
141     if( PTEST & 1 ) SWAP( A, C );                       \
142                                                         \
143     IN = (*A >> (5)) ^ (*A << (27)) ^ CLK;              \
144     *A = (*B >> (6)) ^ (*B << (26)) ^ CLK;              \
145     *B = IN; CLK = (unsigned) mbedtls_timing_hardclock(); \
146     *C = (*C >> (7)) ^ (*C << (25)) ^ CLK;              \
147     *D = (*D >> (8)) ^ (*D << (24)) ^ CLK;              \
148                                                         \
149     A = &WALK[PT1 ^ 4];                                 \
150     B = &WALK[PT2 ^ 1];                                 \
151                                                         \
152     PTEST = PT2 >> 1;                                   \
153                                                         \
154     PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]);   \
155     PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8);  \
156     PTY = (PT2 >> 10) & 7;                              \
157                                                         \
158     TST2_ENTER  TST2_ENTER  TST2_ENTER  TST2_ENTER      \
159     TST2_ENTER  TST2_ENTER  TST2_ENTER  TST2_ENTER      \
160     TST2_ENTER  TST2_ENTER  TST2_ENTER  TST2_ENTER      \
161                                                         \
162     TST2_LEAVE  TST2_LEAVE  TST2_LEAVE  TST2_LEAVE      \
163     TST2_LEAVE  TST2_LEAVE  TST2_LEAVE  TST2_LEAVE      \
164     TST2_LEAVE  TST2_LEAVE  TST2_LEAVE  TST2_LEAVE      \
165                                                         \
166     C = &WALK[PT1 ^ 5];                                 \
167     D = &WALK[PT2 ^ 5];                                 \
168                                                         \
169     RES[i++] ^= *A;                                     \
170     RES[i++] ^= *B;                                     \
171     RES[i++] ^= *C;                                     \
172     RES[i++] ^= *D;                                     \
173                                                         \
174     IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK;             \
175     *A = (*B >> (10)) ^ (*B << (22)) ^ CLK;             \
176     *B = IN ^ U2;                                       \
177     *C = (*C >> (11)) ^ (*C << (21)) ^ CLK;             \
178     *D = (*D >> (12)) ^ (*D << (20)) ^ CLK;             \
179                                                         \
180     A = &WALK[PT1 ^ 6]; RES[i++] ^= *A;                 \
181     B = &WALK[PT2 ^ 3]; RES[i++] ^= *B;                 \
182     C = &WALK[PT1 ^ 7]; RES[i++] ^= *C;                 \
183     D = &WALK[PT2 ^ 7]; RES[i++] ^= *D;                 \
184                                                         \
185     IN = (*A >> (13)) ^ (*A << (19)) ^ CLK;             \
186     *A = (*B >> (14)) ^ (*B << (18)) ^ CLK;             \
187     *B = IN;                                            \
188     *C = (*C >> (15)) ^ (*C << (17)) ^ CLK;             \
189     *D = (*D >> (16)) ^ (*D << (16)) ^ CLK;             \
190                                                         \
191     PT1 = ( RES[( i - 8 ) ^ PTX] ^                      \
192             WALK[PT1 ^ PTX ^ 7] ) & (~1);               \
193     PT1 ^= (PT2 ^ 0x10) & 0x10;                         \
194                                                         \
195     for( n++, i = 0; i < 16; i++ )                      \
196         POOL[n % MBEDTLS_HAVEGE_COLLECT_SIZE] ^= RES[i];
197 
198 /*
199  * Entropy gathering function
200  */
201 static void havege_fill( mbedtls_havege_state *hs )
202 {
203     unsigned i, n = 0;
204     unsigned  U1,  U2, *A, *B, *C, *D;
205     unsigned PT1, PT2, *WALK, *POOL, RES[16];
206     unsigned PTX, PTY, CLK, PTEST, IN;
207 
208     WALK = (unsigned *) hs->WALK;
209     POOL = (unsigned *) hs->pool;
210     PT1  = hs->PT1;
211     PT2  = hs->PT2;
212 
213     PTX  = U1 = 0;
214     PTY  = U2 = 0;
215 
216     (void)PTX;
217 
218     memset( RES, 0, sizeof( RES ) );
219 
220     while( n < MBEDTLS_HAVEGE_COLLECT_SIZE * 4 )
221     {
222         ONE_ITERATION
223         ONE_ITERATION
224         ONE_ITERATION
225         ONE_ITERATION
226     }
227 
228     hs->PT1 = PT1;
229     hs->PT2 = PT2;
230 
231     hs->offset[0] = 0;
232     hs->offset[1] = MBEDTLS_HAVEGE_COLLECT_SIZE / 2;
233 }
234 
235 /*
236  * HAVEGE initialization
237  */
238 void mbedtls_havege_init( mbedtls_havege_state *hs )
239 {
240     memset( hs, 0, sizeof( mbedtls_havege_state ) );
241 
242     havege_fill( hs );
243 }
244 
245 void mbedtls_havege_free( mbedtls_havege_state *hs )
246 {
247     if( hs == NULL )
248         return;
249 
250     mbedtls_zeroize( hs, sizeof( mbedtls_havege_state ) );
251 }
252 
253 /*
254  * HAVEGE rand function
255  */
256 int mbedtls_havege_random( void *p_rng, unsigned char *buf, size_t len )
257 {
258     int val;
259     size_t use_len;
260     mbedtls_havege_state *hs = (mbedtls_havege_state *) p_rng;
261     unsigned char *p = buf;
262 
263     while( len > 0 )
264     {
265         use_len = len;
266         if( use_len > sizeof(int) )
267             use_len = sizeof(int);
268 
269         if( hs->offset[1] >= MBEDTLS_HAVEGE_COLLECT_SIZE )
270             havege_fill( hs );
271 
272         val  = hs->pool[hs->offset[0]++];
273         val ^= hs->pool[hs->offset[1]++];
274 
275         memcpy( p, &val, use_len );
276 
277         len -= use_len;
278         p += use_len;
279     }
280 
281     return( 0 );
282 }
283 
284 #endif /* MBEDTLS_HAVEGE_C */
285