| Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 1 | /** | 
|  | 2 | *  \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3 | * | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | *  Copyright The Mbed TLS Contributors | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | *  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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 18 | */ | 
|  | 19 | /* | 
|  | 20 | *  The HAVEGE RNG was designed by Andre Seznec in 2002. | 
|  | 21 | * | 
|  | 22 | *  http://www.irisa.fr/caps/projects/hipsor/publi.php | 
|  | 23 | * | 
|  | 24 | *  Contact: seznec(at)irisa_dot_fr - orocheco(at)irisa_dot_fr | 
|  | 25 | */ | 
|  | 26 |  | 
| Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 27 | #include "common.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #if defined(MBEDTLS_HAVEGE_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 30 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/havege.h" | 
|  | 32 | #include "mbedtls/timing.h" | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 33 | #include "mbedtls/platform_util.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 34 |  | 
| Gilles Peskine | 7846299 | 2019-06-07 16:38:28 +0200 | [diff] [blame] | 35 | #include <stdint.h> | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 36 | #include <string.h> | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 37 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 38 | /* ------------------------------------------------------------------------ | 
|  | 39 | * On average, one iteration accesses two 8-word blocks in the havege WALK | 
|  | 40 | * table, and generates 16 words in the RES array. | 
|  | 41 | * | 
|  | 42 | * The data read in the WALK table is updated and permuted after each use. | 
|  | 43 | * The result of the hardware clock counter read is used  for this update. | 
|  | 44 | * | 
|  | 45 | * 25 conditional tests are present.  The conditional tests are grouped in | 
|  | 46 | * two nested  groups of 12 conditional tests and 1 test that controls the | 
|  | 47 | * permutation; on average, there should be 6 tests executed and 3 of them | 
|  | 48 | * should be mispredicted. | 
|  | 49 | * ------------------------------------------------------------------------ | 
|  | 50 | */ | 
|  | 51 |  | 
| Gilles Peskine | 7846299 | 2019-06-07 16:38:28 +0200 | [diff] [blame] | 52 | #define SWAP(X,Y) { uint32_t *T = (X); (X) = (Y); (Y) = T; } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 53 |  | 
|  | 54 | #define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1; | 
|  | 55 | #define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1; | 
|  | 56 |  | 
|  | 57 | #define TST1_LEAVE U1++; } | 
|  | 58 | #define TST2_LEAVE U2++; } | 
|  | 59 |  | 
|  | 60 | #define ONE_ITERATION                                   \ | 
|  | 61 | \ | 
|  | 62 | PTEST = PT1 >> 20;                                  \ | 
|  | 63 | \ | 
|  | 64 | TST1_ENTER  TST1_ENTER  TST1_ENTER  TST1_ENTER      \ | 
|  | 65 | TST1_ENTER  TST1_ENTER  TST1_ENTER  TST1_ENTER      \ | 
|  | 66 | TST1_ENTER  TST1_ENTER  TST1_ENTER  TST1_ENTER      \ | 
|  | 67 | \ | 
|  | 68 | TST1_LEAVE  TST1_LEAVE  TST1_LEAVE  TST1_LEAVE      \ | 
|  | 69 | TST1_LEAVE  TST1_LEAVE  TST1_LEAVE  TST1_LEAVE      \ | 
|  | 70 | TST1_LEAVE  TST1_LEAVE  TST1_LEAVE  TST1_LEAVE      \ | 
|  | 71 | \ | 
|  | 72 | PTX = (PT1 >> 18) & 7;                              \ | 
|  | 73 | PT1 &= 0x1FFF;                                      \ | 
|  | 74 | PT2 &= 0x1FFF;                                      \ | 
| Gilles Peskine | 7846299 | 2019-06-07 16:38:28 +0200 | [diff] [blame] | 75 | CLK = (uint32_t) mbedtls_timing_hardclock();        \ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 76 | \ | 
|  | 77 | i = 0;                                              \ | 
|  | 78 | A = &WALK[PT1    ]; RES[i++] ^= *A;                 \ | 
|  | 79 | B = &WALK[PT2    ]; RES[i++] ^= *B;                 \ | 
|  | 80 | C = &WALK[PT1 ^ 1]; RES[i++] ^= *C;                 \ | 
|  | 81 | D = &WALK[PT2 ^ 4]; RES[i++] ^= *D;                 \ | 
|  | 82 | \ | 
|  | 83 | IN = (*A >> (1)) ^ (*A << (31)) ^ CLK;              \ | 
|  | 84 | *A = (*B >> (2)) ^ (*B << (30)) ^ CLK;              \ | 
|  | 85 | *B = IN ^ U1;                                       \ | 
|  | 86 | *C = (*C >> (3)) ^ (*C << (29)) ^ CLK;              \ | 
|  | 87 | *D = (*D >> (4)) ^ (*D << (28)) ^ CLK;              \ | 
|  | 88 | \ | 
|  | 89 | A = &WALK[PT1 ^ 2]; RES[i++] ^= *A;                 \ | 
|  | 90 | B = &WALK[PT2 ^ 2]; RES[i++] ^= *B;                 \ | 
|  | 91 | C = &WALK[PT1 ^ 3]; RES[i++] ^= *C;                 \ | 
|  | 92 | D = &WALK[PT2 ^ 6]; RES[i++] ^= *D;                 \ | 
|  | 93 | \ | 
|  | 94 | if( PTEST & 1 ) SWAP( A, C );                       \ | 
|  | 95 | \ | 
|  | 96 | IN = (*A >> (5)) ^ (*A << (27)) ^ CLK;              \ | 
|  | 97 | *A = (*B >> (6)) ^ (*B << (26)) ^ CLK;              \ | 
| Gilles Peskine | 7846299 | 2019-06-07 16:38:28 +0200 | [diff] [blame] | 98 | *B = IN; CLK = (uint32_t) mbedtls_timing_hardclock();       \ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 99 | *C = (*C >> (7)) ^ (*C << (25)) ^ CLK;              \ | 
|  | 100 | *D = (*D >> (8)) ^ (*D << (24)) ^ CLK;              \ | 
|  | 101 | \ | 
|  | 102 | A = &WALK[PT1 ^ 4];                                 \ | 
|  | 103 | B = &WALK[PT2 ^ 1];                                 \ | 
|  | 104 | \ | 
|  | 105 | PTEST = PT2 >> 1;                                   \ | 
|  | 106 | \ | 
|  | 107 | PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]);   \ | 
|  | 108 | PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8);  \ | 
|  | 109 | PTY = (PT2 >> 10) & 7;                              \ | 
|  | 110 | \ | 
|  | 111 | TST2_ENTER  TST2_ENTER  TST2_ENTER  TST2_ENTER      \ | 
|  | 112 | TST2_ENTER  TST2_ENTER  TST2_ENTER  TST2_ENTER      \ | 
|  | 113 | TST2_ENTER  TST2_ENTER  TST2_ENTER  TST2_ENTER      \ | 
|  | 114 | \ | 
|  | 115 | TST2_LEAVE  TST2_LEAVE  TST2_LEAVE  TST2_LEAVE      \ | 
|  | 116 | TST2_LEAVE  TST2_LEAVE  TST2_LEAVE  TST2_LEAVE      \ | 
|  | 117 | TST2_LEAVE  TST2_LEAVE  TST2_LEAVE  TST2_LEAVE      \ | 
|  | 118 | \ | 
|  | 119 | C = &WALK[PT1 ^ 5];                                 \ | 
|  | 120 | D = &WALK[PT2 ^ 5];                                 \ | 
|  | 121 | \ | 
|  | 122 | RES[i++] ^= *A;                                     \ | 
|  | 123 | RES[i++] ^= *B;                                     \ | 
|  | 124 | RES[i++] ^= *C;                                     \ | 
|  | 125 | RES[i++] ^= *D;                                     \ | 
|  | 126 | \ | 
|  | 127 | IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK;             \ | 
|  | 128 | *A = (*B >> (10)) ^ (*B << (22)) ^ CLK;             \ | 
|  | 129 | *B = IN ^ U2;                                       \ | 
|  | 130 | *C = (*C >> (11)) ^ (*C << (21)) ^ CLK;             \ | 
|  | 131 | *D = (*D >> (12)) ^ (*D << (20)) ^ CLK;             \ | 
|  | 132 | \ | 
|  | 133 | A = &WALK[PT1 ^ 6]; RES[i++] ^= *A;                 \ | 
|  | 134 | B = &WALK[PT2 ^ 3]; RES[i++] ^= *B;                 \ | 
|  | 135 | C = &WALK[PT1 ^ 7]; RES[i++] ^= *C;                 \ | 
|  | 136 | D = &WALK[PT2 ^ 7]; RES[i++] ^= *D;                 \ | 
|  | 137 | \ | 
|  | 138 | IN = (*A >> (13)) ^ (*A << (19)) ^ CLK;             \ | 
|  | 139 | *A = (*B >> (14)) ^ (*B << (18)) ^ CLK;             \ | 
|  | 140 | *B = IN;                                            \ | 
|  | 141 | *C = (*C >> (15)) ^ (*C << (17)) ^ CLK;             \ | 
|  | 142 | *D = (*D >> (16)) ^ (*D << (16)) ^ CLK;             \ | 
|  | 143 | \ | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 144 | PT1 = ( RES[( i - 8 ) ^ PTX] ^                      \ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 145 | WALK[PT1 ^ PTX ^ 7] ) & (~1);               \ | 
|  | 146 | PT1 ^= (PT2 ^ 0x10) & 0x10;                         \ | 
|  | 147 | \ | 
|  | 148 | for( n++, i = 0; i < 16; i++ )                      \ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 149 | hs->pool[n % MBEDTLS_HAVEGE_COLLECT_SIZE] ^= RES[i]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 150 |  | 
|  | 151 | /* | 
|  | 152 | * Entropy gathering function | 
|  | 153 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | static void havege_fill( mbedtls_havege_state *hs ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 155 | { | 
| Gilles Peskine | 7846299 | 2019-06-07 16:38:28 +0200 | [diff] [blame] | 156 | size_t n = 0; | 
| Gilles Peskine | bc2adf9 | 2019-06-24 15:45:09 +0200 | [diff] [blame] | 157 | size_t i; | 
| Gilles Peskine | 7846299 | 2019-06-07 16:38:28 +0200 | [diff] [blame] | 158 | uint32_t  U1,  U2, *A, *B, *C, *D; | 
|  | 159 | uint32_t PT1, PT2, *WALK, RES[16]; | 
|  | 160 | uint32_t PTX, PTY, CLK, PTEST, IN; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 161 |  | 
|  | 162 | WALK = hs->WALK; | 
|  | 163 | PT1  = hs->PT1; | 
|  | 164 | PT2  = hs->PT2; | 
|  | 165 |  | 
|  | 166 | PTX  = U1 = 0; | 
|  | 167 | PTY  = U2 = 0; | 
|  | 168 |  | 
| Simon Butcher | 65b1fa6 | 2016-05-23 23:18:26 +0100 | [diff] [blame] | 169 | (void)PTX; | 
|  | 170 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 171 | memset( RES, 0, sizeof( RES ) ); | 
|  | 172 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 173 | while( n < MBEDTLS_HAVEGE_COLLECT_SIZE * 4 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 174 | { | 
|  | 175 | ONE_ITERATION | 
|  | 176 | ONE_ITERATION | 
|  | 177 | ONE_ITERATION | 
|  | 178 | ONE_ITERATION | 
|  | 179 | } | 
|  | 180 |  | 
|  | 181 | hs->PT1 = PT1; | 
|  | 182 | hs->PT2 = PT2; | 
|  | 183 |  | 
|  | 184 | hs->offset[0] = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 185 | hs->offset[1] = MBEDTLS_HAVEGE_COLLECT_SIZE / 2; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 186 | } | 
|  | 187 |  | 
|  | 188 | /* | 
|  | 189 | * HAVEGE initialization | 
|  | 190 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 191 | void mbedtls_havege_init( mbedtls_havege_state *hs ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 192 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 193 | memset( hs, 0, sizeof( mbedtls_havege_state ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 194 |  | 
|  | 195 | havege_fill( hs ); | 
|  | 196 | } | 
|  | 197 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 198 | void mbedtls_havege_free( mbedtls_havege_state *hs ) | 
| Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 199 | { | 
|  | 200 | if( hs == NULL ) | 
|  | 201 | return; | 
|  | 202 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 203 | mbedtls_platform_zeroize( hs, sizeof( mbedtls_havege_state ) ); | 
| Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 204 | } | 
|  | 205 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 206 | /* | 
|  | 207 | * HAVEGE rand function | 
|  | 208 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 209 | int mbedtls_havege_random( void *p_rng, unsigned char *buf, size_t len ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 210 | { | 
| Gilles Peskine | 7846299 | 2019-06-07 16:38:28 +0200 | [diff] [blame] | 211 | uint32_t val; | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 212 | size_t use_len; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 213 | mbedtls_havege_state *hs = (mbedtls_havege_state *) p_rng; | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 214 | unsigned char *p = buf; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 215 |  | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 216 | while( len > 0 ) | 
|  | 217 | { | 
|  | 218 | use_len = len; | 
| Gilles Peskine | 7846299 | 2019-06-07 16:38:28 +0200 | [diff] [blame] | 219 | if( use_len > sizeof( val ) ) | 
|  | 220 | use_len = sizeof( val ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 221 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 222 | if( hs->offset[1] >= MBEDTLS_HAVEGE_COLLECT_SIZE ) | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 223 | havege_fill( hs ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 224 |  | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 225 | val  = hs->pool[hs->offset[0]++]; | 
|  | 226 | val ^= hs->pool[hs->offset[1]++]; | 
|  | 227 |  | 
|  | 228 | memcpy( p, &val, use_len ); | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 229 |  | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 230 | len -= use_len; | 
|  | 231 | p += use_len; | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | return( 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 235 | } | 
|  | 236 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 237 | #endif /* MBEDTLS_HAVEGE_C */ |