blob: 7623bc0676d2cf4e4537d8bd17fa53503a479d63 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_HAVEGE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/havege.h"
38#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker23986e52011-04-24 08:57:21 +000040#include <string.h>
Paul Bakker23986e52011-04-24 08:57:21 +000041
Paul Bakkera317a982014-06-18 16:44:11 +020042/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakkera317a982014-06-18 16:44:11 +020044 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
45}
46
Paul Bakker5121ce52009-01-03 21:22:43 +000047/* ------------------------------------------------------------------------
48 * On average, one iteration accesses two 8-word blocks in the havege WALK
49 * table, and generates 16 words in the RES array.
50 *
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.
53 *
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 * ------------------------------------------------------------------------
59 */
60
61#define SWAP(X,Y) { int *T = X; X = Y; Y = T; }
62
63#define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
64#define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
65
66#define TST1_LEAVE U1++; }
67#define TST2_LEAVE U2++; }
68
69#define ONE_ITERATION \
70 \
71 PTEST = PT1 >> 20; \
72 \
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 \
76 \
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 \
80 \
81 PTX = (PT1 >> 18) & 7; \
82 PT1 &= 0x1FFF; \
83 PT2 &= 0x1FFF; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 CLK = (int) mbedtls_timing_hardclock(); \
Paul Bakker5121ce52009-01-03 21:22:43 +000085 \
86 i = 0; \
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; \
91 \
92 IN = (*A >> (1)) ^ (*A << (31)) ^ CLK; \
93 *A = (*B >> (2)) ^ (*B << (30)) ^ CLK; \
94 *B = IN ^ U1; \
95 *C = (*C >> (3)) ^ (*C << (29)) ^ CLK; \
96 *D = (*D >> (4)) ^ (*D << (28)) ^ CLK; \
97 \
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; \
102 \
103 if( PTEST & 1 ) SWAP( A, C ); \
104 \
105 IN = (*A >> (5)) ^ (*A << (27)) ^ CLK; \
106 *A = (*B >> (6)) ^ (*B << (26)) ^ CLK; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 *B = IN; CLK = (int) mbedtls_timing_hardclock(); \
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 *C = (*C >> (7)) ^ (*C << (25)) ^ CLK; \
109 *D = (*D >> (8)) ^ (*D << (24)) ^ CLK; \
110 \
111 A = &WALK[PT1 ^ 4]; \
112 B = &WALK[PT2 ^ 1]; \
113 \
114 PTEST = PT2 >> 1; \
115 \
116 PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]); \
117 PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8); \
118 PTY = (PT2 >> 10) & 7; \
119 \
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 \
123 \
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 \
127 \
128 C = &WALK[PT1 ^ 5]; \
129 D = &WALK[PT2 ^ 5]; \
130 \
131 RES[i++] ^= *A; \
132 RES[i++] ^= *B; \
133 RES[i++] ^= *C; \
134 RES[i++] ^= *D; \
135 \
136 IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK; \
137 *A = (*B >> (10)) ^ (*B << (22)) ^ CLK; \
138 *B = IN ^ U2; \
139 *C = (*C >> (11)) ^ (*C << (21)) ^ CLK; \
140 *D = (*D >> (12)) ^ (*D << (20)) ^ CLK; \
141 \
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; \
146 \
147 IN = (*A >> (13)) ^ (*A << (19)) ^ CLK; \
148 *A = (*B >> (14)) ^ (*B << (18)) ^ CLK; \
149 *B = IN; \
150 *C = (*C >> (15)) ^ (*C << (17)) ^ CLK; \
151 *D = (*D >> (16)) ^ (*D << (16)) ^ CLK; \
152 \
Paul Bakker66d5d072014-06-17 16:39:18 +0200153 PT1 = ( RES[( i - 8 ) ^ PTX] ^ \
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 WALK[PT1 ^ PTX ^ 7] ) & (~1); \
155 PT1 ^= (PT2 ^ 0x10) & 0x10; \
156 \
157 for( n++, i = 0; i < 16; i++ ) \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 hs->pool[n % MBEDTLS_HAVEGE_COLLECT_SIZE] ^= RES[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
160/*
161 * Entropy gathering function
162 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163static void havege_fill( mbedtls_havege_state *hs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000164{
165 int i, n = 0;
166 int U1, U2, *A, *B, *C, *D;
167 int PT1, PT2, *WALK, RES[16];
168 int PTX, PTY, CLK, PTEST, IN;
169
170 WALK = hs->WALK;
171 PT1 = hs->PT1;
172 PT2 = hs->PT2;
173
174 PTX = U1 = 0;
175 PTY = U2 = 0;
176
177 memset( RES, 0, sizeof( RES ) );
178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 while( n < MBEDTLS_HAVEGE_COLLECT_SIZE * 4 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 {
181 ONE_ITERATION
182 ONE_ITERATION
183 ONE_ITERATION
184 ONE_ITERATION
185 }
186
187 hs->PT1 = PT1;
188 hs->PT2 = PT2;
189
190 hs->offset[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 hs->offset[1] = MBEDTLS_HAVEGE_COLLECT_SIZE / 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000192}
193
194/*
195 * HAVEGE initialization
196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197void mbedtls_havege_init( mbedtls_havege_state *hs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000198{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 memset( hs, 0, sizeof( mbedtls_havege_state ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
201 havege_fill( hs );
202}
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204void mbedtls_havege_free( mbedtls_havege_state *hs )
Paul Bakkera317a982014-06-18 16:44:11 +0200205{
206 if( hs == NULL )
207 return;
208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_zeroize( hs, sizeof( mbedtls_havege_state ) );
Paul Bakkera317a982014-06-18 16:44:11 +0200210}
211
Paul Bakker5121ce52009-01-03 21:22:43 +0000212/*
213 * HAVEGE rand function
214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215int mbedtls_havege_random( void *p_rng, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000216{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000217 int val;
218 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 mbedtls_havege_state *hs = (mbedtls_havege_state *) p_rng;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000220 unsigned char *p = buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
Paul Bakkera3d195c2011-11-27 21:07:34 +0000222 while( len > 0 )
223 {
224 use_len = len;
225 if( use_len > sizeof(int) )
226 use_len = sizeof(int);
Paul Bakker5121ce52009-01-03 21:22:43 +0000227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 if( hs->offset[1] >= MBEDTLS_HAVEGE_COLLECT_SIZE )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000229 havege_fill( hs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
Paul Bakkera3d195c2011-11-27 21:07:34 +0000231 val = hs->pool[hs->offset[0]++];
232 val ^= hs->pool[hs->offset[1]++];
233
234 memcpy( p, &val, use_len );
Paul Bakker9af723c2014-05-01 13:03:14 +0200235
Paul Bakkera3d195c2011-11-27 21:07:34 +0000236 len -= use_len;
237 p += use_len;
238 }
239
240 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000241}
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#endif /* MBEDTLS_HAVEGE_C */