blob: 93450745b51e0f07cdcc3700185de87d42e8d5f2 [file] [log] [blame] [raw]
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é-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The HAVEGE RNG was designed by Andre Seznec in 2002.
24 *
25 * http://www.irisa.fr/caps/projects/hipsor/publi.php
26 *
27 * Contact: seznec(at)irisa_dot_fr - orocheco(at)irisa_dot_fr
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_HAVEGE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/havege.h"
39#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker23986e52011-04-24 08:57:21 +000041#include <string.h>
Paul Bakker23986e52011-04-24 08:57:21 +000042
Paul Bakkera317a982014-06-18 16:44:11 +020043/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakkera317a982014-06-18 16:44:11 +020045 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
46}
47
Paul Bakker5121ce52009-01-03 21:22:43 +000048/* ------------------------------------------------------------------------
49 * On average, one iteration accesses two 8-word blocks in the havege WALK
50 * table, and generates 16 words in the RES array.
51 *
52 * The data read in the WALK table is updated and permuted after each use.
53 * The result of the hardware clock counter read is used for this update.
54 *
55 * 25 conditional tests are present. The conditional tests are grouped in
56 * two nested groups of 12 conditional tests and 1 test that controls the
57 * permutation; on average, there should be 6 tests executed and 3 of them
58 * should be mispredicted.
59 * ------------------------------------------------------------------------
60 */
61
62#define SWAP(X,Y) { int *T = X; X = Y; Y = T; }
63
64#define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
65#define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
66
67#define TST1_LEAVE U1++; }
68#define TST2_LEAVE U2++; }
69
70#define ONE_ITERATION \
71 \
72 PTEST = PT1 >> 20; \
73 \
74 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
75 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
76 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
77 \
78 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
79 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
80 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
81 \
82 PTX = (PT1 >> 18) & 7; \
83 PT1 &= 0x1FFF; \
84 PT2 &= 0x1FFF; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 CLK = (int) mbedtls_timing_hardclock(); \
Paul Bakker5121ce52009-01-03 21:22:43 +000086 \
87 i = 0; \
88 A = &WALK[PT1 ]; RES[i++] ^= *A; \
89 B = &WALK[PT2 ]; RES[i++] ^= *B; \
90 C = &WALK[PT1 ^ 1]; RES[i++] ^= *C; \
91 D = &WALK[PT2 ^ 4]; RES[i++] ^= *D; \
92 \
93 IN = (*A >> (1)) ^ (*A << (31)) ^ CLK; \
94 *A = (*B >> (2)) ^ (*B << (30)) ^ CLK; \
95 *B = IN ^ U1; \
96 *C = (*C >> (3)) ^ (*C << (29)) ^ CLK; \
97 *D = (*D >> (4)) ^ (*D << (28)) ^ CLK; \
98 \
99 A = &WALK[PT1 ^ 2]; RES[i++] ^= *A; \
100 B = &WALK[PT2 ^ 2]; RES[i++] ^= *B; \
101 C = &WALK[PT1 ^ 3]; RES[i++] ^= *C; \
102 D = &WALK[PT2 ^ 6]; RES[i++] ^= *D; \
103 \
104 if( PTEST & 1 ) SWAP( A, C ); \
105 \
106 IN = (*A >> (5)) ^ (*A << (27)) ^ CLK; \
107 *A = (*B >> (6)) ^ (*B << (26)) ^ CLK; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 *B = IN; CLK = (int) mbedtls_timing_hardclock(); \
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 *C = (*C >> (7)) ^ (*C << (25)) ^ CLK; \
110 *D = (*D >> (8)) ^ (*D << (24)) ^ CLK; \
111 \
112 A = &WALK[PT1 ^ 4]; \
113 B = &WALK[PT2 ^ 1]; \
114 \
115 PTEST = PT2 >> 1; \
116 \
117 PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]); \
118 PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8); \
119 PTY = (PT2 >> 10) & 7; \
120 \
121 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
122 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
123 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
124 \
125 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
126 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
127 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
128 \
129 C = &WALK[PT1 ^ 5]; \
130 D = &WALK[PT2 ^ 5]; \
131 \
132 RES[i++] ^= *A; \
133 RES[i++] ^= *B; \
134 RES[i++] ^= *C; \
135 RES[i++] ^= *D; \
136 \
137 IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK; \
138 *A = (*B >> (10)) ^ (*B << (22)) ^ CLK; \
139 *B = IN ^ U2; \
140 *C = (*C >> (11)) ^ (*C << (21)) ^ CLK; \
141 *D = (*D >> (12)) ^ (*D << (20)) ^ CLK; \
142 \
143 A = &WALK[PT1 ^ 6]; RES[i++] ^= *A; \
144 B = &WALK[PT2 ^ 3]; RES[i++] ^= *B; \
145 C = &WALK[PT1 ^ 7]; RES[i++] ^= *C; \
146 D = &WALK[PT2 ^ 7]; RES[i++] ^= *D; \
147 \
148 IN = (*A >> (13)) ^ (*A << (19)) ^ CLK; \
149 *A = (*B >> (14)) ^ (*B << (18)) ^ CLK; \
150 *B = IN; \
151 *C = (*C >> (15)) ^ (*C << (17)) ^ CLK; \
152 *D = (*D >> (16)) ^ (*D << (16)) ^ CLK; \
153 \
Paul Bakker66d5d072014-06-17 16:39:18 +0200154 PT1 = ( RES[( i - 8 ) ^ PTX] ^ \
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 WALK[PT1 ^ PTX ^ 7] ) & (~1); \
156 PT1 ^= (PT2 ^ 0x10) & 0x10; \
157 \
158 for( n++, i = 0; i < 16; i++ ) \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 hs->pool[n % MBEDTLS_HAVEGE_COLLECT_SIZE] ^= RES[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
161/*
162 * Entropy gathering function
163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164static void havege_fill( mbedtls_havege_state *hs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000165{
166 int i, n = 0;
167 int U1, U2, *A, *B, *C, *D;
168 int PT1, PT2, *WALK, RES[16];
169 int PTX, PTY, CLK, PTEST, IN;
170
171 WALK = hs->WALK;
172 PT1 = hs->PT1;
173 PT2 = hs->PT2;
174
175 PTX = U1 = 0;
176 PTY = U2 = 0;
177
178 memset( RES, 0, sizeof( RES ) );
179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 while( n < MBEDTLS_HAVEGE_COLLECT_SIZE * 4 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 {
182 ONE_ITERATION
183 ONE_ITERATION
184 ONE_ITERATION
185 ONE_ITERATION
186 }
187
188 hs->PT1 = PT1;
189 hs->PT2 = PT2;
190
191 hs->offset[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 hs->offset[1] = MBEDTLS_HAVEGE_COLLECT_SIZE / 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000193}
194
195/*
196 * HAVEGE initialization
197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198void mbedtls_havege_init( mbedtls_havege_state *hs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000199{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 memset( hs, 0, sizeof( mbedtls_havege_state ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
202 havege_fill( hs );
203}
204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205void mbedtls_havege_free( mbedtls_havege_state *hs )
Paul Bakkera317a982014-06-18 16:44:11 +0200206{
207 if( hs == NULL )
208 return;
209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 mbedtls_zeroize( hs, sizeof( mbedtls_havege_state ) );
Paul Bakkera317a982014-06-18 16:44:11 +0200211}
212
Paul Bakker5121ce52009-01-03 21:22:43 +0000213/*
214 * HAVEGE rand function
215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216int mbedtls_havege_random( void *p_rng, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000217{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000218 int val;
219 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_havege_state *hs = (mbedtls_havege_state *) p_rng;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000221 unsigned char *p = buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
Paul Bakkera3d195c2011-11-27 21:07:34 +0000223 while( len > 0 )
224 {
225 use_len = len;
226 if( use_len > sizeof(int) )
227 use_len = sizeof(int);
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 if( hs->offset[1] >= MBEDTLS_HAVEGE_COLLECT_SIZE )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000230 havege_fill( hs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
Paul Bakkera3d195c2011-11-27 21:07:34 +0000232 val = hs->pool[hs->offset[0]++];
233 val ^= hs->pool[hs->offset[1]++];
234
235 memcpy( p, &val, use_len );
Paul Bakker9af723c2014-05-01 13:03:14 +0200236
Paul Bakkera3d195c2011-11-27 21:07:34 +0000237 len -= use_len;
238 p += use_len;
239 }
240
241 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000242}
243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244#endif /* MBEDTLS_HAVEGE_C */