mbed TLS upgraded to 2.6.0
Fork of mbedtls by
library/havege.c@0:cdf462088d13, 2017-01-05 (annotated)
- Committer:
- markrad
- Date:
- Thu Jan 05 00:18:44 2017 +0000
- Revision:
- 0:cdf462088d13
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
markrad | 0:cdf462088d13 | 1 | /** |
markrad | 0:cdf462088d13 | 2 | * \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion |
markrad | 0:cdf462088d13 | 3 | * |
markrad | 0:cdf462088d13 | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
markrad | 0:cdf462088d13 | 5 | * SPDX-License-Identifier: Apache-2.0 |
markrad | 0:cdf462088d13 | 6 | * |
markrad | 0:cdf462088d13 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
markrad | 0:cdf462088d13 | 8 | * not use this file except in compliance with the License. |
markrad | 0:cdf462088d13 | 9 | * You may obtain a copy of the License at |
markrad | 0:cdf462088d13 | 10 | * |
markrad | 0:cdf462088d13 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
markrad | 0:cdf462088d13 | 12 | * |
markrad | 0:cdf462088d13 | 13 | * Unless required by applicable law or agreed to in writing, software |
markrad | 0:cdf462088d13 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
markrad | 0:cdf462088d13 | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
markrad | 0:cdf462088d13 | 16 | * See the License for the specific language governing permissions and |
markrad | 0:cdf462088d13 | 17 | * limitations under the License. |
markrad | 0:cdf462088d13 | 18 | * |
markrad | 0:cdf462088d13 | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
markrad | 0:cdf462088d13 | 20 | */ |
markrad | 0:cdf462088d13 | 21 | /* |
markrad | 0:cdf462088d13 | 22 | * The HAVEGE RNG was designed by Andre Seznec in 2002. |
markrad | 0:cdf462088d13 | 23 | * |
markrad | 0:cdf462088d13 | 24 | * http://www.irisa.fr/caps/projects/hipsor/publi.php |
markrad | 0:cdf462088d13 | 25 | * |
markrad | 0:cdf462088d13 | 26 | * Contact: seznec(at)irisa_dot_fr - orocheco(at)irisa_dot_fr |
markrad | 0:cdf462088d13 | 27 | */ |
markrad | 0:cdf462088d13 | 28 | |
markrad | 0:cdf462088d13 | 29 | #if !defined(MBEDTLS_CONFIG_FILE) |
markrad | 0:cdf462088d13 | 30 | #include "mbedtls/config.h" |
markrad | 0:cdf462088d13 | 31 | #else |
markrad | 0:cdf462088d13 | 32 | #include MBEDTLS_CONFIG_FILE |
markrad | 0:cdf462088d13 | 33 | #endif |
markrad | 0:cdf462088d13 | 34 | |
markrad | 0:cdf462088d13 | 35 | #if defined(MBEDTLS_HAVEGE_C) |
markrad | 0:cdf462088d13 | 36 | |
markrad | 0:cdf462088d13 | 37 | #include "mbedtls/havege.h" |
markrad | 0:cdf462088d13 | 38 | #include "mbedtls/timing.h" |
markrad | 0:cdf462088d13 | 39 | |
markrad | 0:cdf462088d13 | 40 | #include <string.h> |
markrad | 0:cdf462088d13 | 41 | |
markrad | 0:cdf462088d13 | 42 | /* Implementation that should never be optimized out by the compiler */ |
markrad | 0:cdf462088d13 | 43 | static void mbedtls_zeroize( void *v, size_t n ) { |
markrad | 0:cdf462088d13 | 44 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
markrad | 0:cdf462088d13 | 45 | } |
markrad | 0:cdf462088d13 | 46 | |
markrad | 0:cdf462088d13 | 47 | /* ------------------------------------------------------------------------ |
markrad | 0:cdf462088d13 | 48 | * On average, one iteration accesses two 8-word blocks in the havege WALK |
markrad | 0:cdf462088d13 | 49 | * table, and generates 16 words in the RES array. |
markrad | 0:cdf462088d13 | 50 | * |
markrad | 0:cdf462088d13 | 51 | * The data read in the WALK table is updated and permuted after each use. |
markrad | 0:cdf462088d13 | 52 | * The result of the hardware clock counter read is used for this update. |
markrad | 0:cdf462088d13 | 53 | * |
markrad | 0:cdf462088d13 | 54 | * 25 conditional tests are present. The conditional tests are grouped in |
markrad | 0:cdf462088d13 | 55 | * two nested groups of 12 conditional tests and 1 test that controls the |
markrad | 0:cdf462088d13 | 56 | * permutation; on average, there should be 6 tests executed and 3 of them |
markrad | 0:cdf462088d13 | 57 | * should be mispredicted. |
markrad | 0:cdf462088d13 | 58 | * ------------------------------------------------------------------------ |
markrad | 0:cdf462088d13 | 59 | */ |
markrad | 0:cdf462088d13 | 60 | |
markrad | 0:cdf462088d13 | 61 | #define SWAP(X,Y) { int *T = X; X = Y; Y = T; } |
markrad | 0:cdf462088d13 | 62 | |
markrad | 0:cdf462088d13 | 63 | #define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1; |
markrad | 0:cdf462088d13 | 64 | #define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1; |
markrad | 0:cdf462088d13 | 65 | |
markrad | 0:cdf462088d13 | 66 | #define TST1_LEAVE U1++; } |
markrad | 0:cdf462088d13 | 67 | #define TST2_LEAVE U2++; } |
markrad | 0:cdf462088d13 | 68 | |
markrad | 0:cdf462088d13 | 69 | #define ONE_ITERATION \ |
markrad | 0:cdf462088d13 | 70 | \ |
markrad | 0:cdf462088d13 | 71 | PTEST = PT1 >> 20; \ |
markrad | 0:cdf462088d13 | 72 | \ |
markrad | 0:cdf462088d13 | 73 | TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \ |
markrad | 0:cdf462088d13 | 74 | TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \ |
markrad | 0:cdf462088d13 | 75 | TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \ |
markrad | 0:cdf462088d13 | 76 | \ |
markrad | 0:cdf462088d13 | 77 | TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \ |
markrad | 0:cdf462088d13 | 78 | TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \ |
markrad | 0:cdf462088d13 | 79 | TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \ |
markrad | 0:cdf462088d13 | 80 | \ |
markrad | 0:cdf462088d13 | 81 | PTX = (PT1 >> 18) & 7; \ |
markrad | 0:cdf462088d13 | 82 | PT1 &= 0x1FFF; \ |
markrad | 0:cdf462088d13 | 83 | PT2 &= 0x1FFF; \ |
markrad | 0:cdf462088d13 | 84 | CLK = (int) mbedtls_timing_hardclock(); \ |
markrad | 0:cdf462088d13 | 85 | \ |
markrad | 0:cdf462088d13 | 86 | i = 0; \ |
markrad | 0:cdf462088d13 | 87 | A = &WALK[PT1 ]; RES[i++] ^= *A; \ |
markrad | 0:cdf462088d13 | 88 | B = &WALK[PT2 ]; RES[i++] ^= *B; \ |
markrad | 0:cdf462088d13 | 89 | C = &WALK[PT1 ^ 1]; RES[i++] ^= *C; \ |
markrad | 0:cdf462088d13 | 90 | D = &WALK[PT2 ^ 4]; RES[i++] ^= *D; \ |
markrad | 0:cdf462088d13 | 91 | \ |
markrad | 0:cdf462088d13 | 92 | IN = (*A >> (1)) ^ (*A << (31)) ^ CLK; \ |
markrad | 0:cdf462088d13 | 93 | *A = (*B >> (2)) ^ (*B << (30)) ^ CLK; \ |
markrad | 0:cdf462088d13 | 94 | *B = IN ^ U1; \ |
markrad | 0:cdf462088d13 | 95 | *C = (*C >> (3)) ^ (*C << (29)) ^ CLK; \ |
markrad | 0:cdf462088d13 | 96 | *D = (*D >> (4)) ^ (*D << (28)) ^ CLK; \ |
markrad | 0:cdf462088d13 | 97 | \ |
markrad | 0:cdf462088d13 | 98 | A = &WALK[PT1 ^ 2]; RES[i++] ^= *A; \ |
markrad | 0:cdf462088d13 | 99 | B = &WALK[PT2 ^ 2]; RES[i++] ^= *B; \ |
markrad | 0:cdf462088d13 | 100 | C = &WALK[PT1 ^ 3]; RES[i++] ^= *C; \ |
markrad | 0:cdf462088d13 | 101 | D = &WALK[PT2 ^ 6]; RES[i++] ^= *D; \ |
markrad | 0:cdf462088d13 | 102 | \ |
markrad | 0:cdf462088d13 | 103 | if( PTEST & 1 ) SWAP( A, C ); \ |
markrad | 0:cdf462088d13 | 104 | \ |
markrad | 0:cdf462088d13 | 105 | IN = (*A >> (5)) ^ (*A << (27)) ^ CLK; \ |
markrad | 0:cdf462088d13 | 106 | *A = (*B >> (6)) ^ (*B << (26)) ^ CLK; \ |
markrad | 0:cdf462088d13 | 107 | *B = IN; CLK = (int) mbedtls_timing_hardclock(); \ |
markrad | 0:cdf462088d13 | 108 | *C = (*C >> (7)) ^ (*C << (25)) ^ CLK; \ |
markrad | 0:cdf462088d13 | 109 | *D = (*D >> (8)) ^ (*D << (24)) ^ CLK; \ |
markrad | 0:cdf462088d13 | 110 | \ |
markrad | 0:cdf462088d13 | 111 | A = &WALK[PT1 ^ 4]; \ |
markrad | 0:cdf462088d13 | 112 | B = &WALK[PT2 ^ 1]; \ |
markrad | 0:cdf462088d13 | 113 | \ |
markrad | 0:cdf462088d13 | 114 | PTEST = PT2 >> 1; \ |
markrad | 0:cdf462088d13 | 115 | \ |
markrad | 0:cdf462088d13 | 116 | PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]); \ |
markrad | 0:cdf462088d13 | 117 | PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8); \ |
markrad | 0:cdf462088d13 | 118 | PTY = (PT2 >> 10) & 7; \ |
markrad | 0:cdf462088d13 | 119 | \ |
markrad | 0:cdf462088d13 | 120 | TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \ |
markrad | 0:cdf462088d13 | 121 | TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \ |
markrad | 0:cdf462088d13 | 122 | TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \ |
markrad | 0:cdf462088d13 | 123 | \ |
markrad | 0:cdf462088d13 | 124 | TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \ |
markrad | 0:cdf462088d13 | 125 | TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \ |
markrad | 0:cdf462088d13 | 126 | TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \ |
markrad | 0:cdf462088d13 | 127 | \ |
markrad | 0:cdf462088d13 | 128 | C = &WALK[PT1 ^ 5]; \ |
markrad | 0:cdf462088d13 | 129 | D = &WALK[PT2 ^ 5]; \ |
markrad | 0:cdf462088d13 | 130 | \ |
markrad | 0:cdf462088d13 | 131 | RES[i++] ^= *A; \ |
markrad | 0:cdf462088d13 | 132 | RES[i++] ^= *B; \ |
markrad | 0:cdf462088d13 | 133 | RES[i++] ^= *C; \ |
markrad | 0:cdf462088d13 | 134 | RES[i++] ^= *D; \ |
markrad | 0:cdf462088d13 | 135 | \ |
markrad | 0:cdf462088d13 | 136 | IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK; \ |
markrad | 0:cdf462088d13 | 137 | *A = (*B >> (10)) ^ (*B << (22)) ^ CLK; \ |
markrad | 0:cdf462088d13 | 138 | *B = IN ^ U2; \ |
markrad | 0:cdf462088d13 | 139 | *C = (*C >> (11)) ^ (*C << (21)) ^ CLK; \ |
markrad | 0:cdf462088d13 | 140 | *D = (*D >> (12)) ^ (*D << (20)) ^ CLK; \ |
markrad | 0:cdf462088d13 | 141 | \ |
markrad | 0:cdf462088d13 | 142 | A = &WALK[PT1 ^ 6]; RES[i++] ^= *A; \ |
markrad | 0:cdf462088d13 | 143 | B = &WALK[PT2 ^ 3]; RES[i++] ^= *B; \ |
markrad | 0:cdf462088d13 | 144 | C = &WALK[PT1 ^ 7]; RES[i++] ^= *C; \ |
markrad | 0:cdf462088d13 | 145 | D = &WALK[PT2 ^ 7]; RES[i++] ^= *D; \ |
markrad | 0:cdf462088d13 | 146 | \ |
markrad | 0:cdf462088d13 | 147 | IN = (*A >> (13)) ^ (*A << (19)) ^ CLK; \ |
markrad | 0:cdf462088d13 | 148 | *A = (*B >> (14)) ^ (*B << (18)) ^ CLK; \ |
markrad | 0:cdf462088d13 | 149 | *B = IN; \ |
markrad | 0:cdf462088d13 | 150 | *C = (*C >> (15)) ^ (*C << (17)) ^ CLK; \ |
markrad | 0:cdf462088d13 | 151 | *D = (*D >> (16)) ^ (*D << (16)) ^ CLK; \ |
markrad | 0:cdf462088d13 | 152 | \ |
markrad | 0:cdf462088d13 | 153 | PT1 = ( RES[( i - 8 ) ^ PTX] ^ \ |
markrad | 0:cdf462088d13 | 154 | WALK[PT1 ^ PTX ^ 7] ) & (~1); \ |
markrad | 0:cdf462088d13 | 155 | PT1 ^= (PT2 ^ 0x10) & 0x10; \ |
markrad | 0:cdf462088d13 | 156 | \ |
markrad | 0:cdf462088d13 | 157 | for( n++, i = 0; i < 16; i++ ) \ |
markrad | 0:cdf462088d13 | 158 | hs->pool[n % MBEDTLS_HAVEGE_COLLECT_SIZE] ^= RES[i]; |
markrad | 0:cdf462088d13 | 159 | |
markrad | 0:cdf462088d13 | 160 | /* |
markrad | 0:cdf462088d13 | 161 | * Entropy gathering function |
markrad | 0:cdf462088d13 | 162 | */ |
markrad | 0:cdf462088d13 | 163 | static void havege_fill( mbedtls_havege_state *hs ) |
markrad | 0:cdf462088d13 | 164 | { |
markrad | 0:cdf462088d13 | 165 | int i, n = 0; |
markrad | 0:cdf462088d13 | 166 | int U1, U2, *A, *B, *C, *D; |
markrad | 0:cdf462088d13 | 167 | int PT1, PT2, *WALK, RES[16]; |
markrad | 0:cdf462088d13 | 168 | int PTX, PTY, CLK, PTEST, IN; |
markrad | 0:cdf462088d13 | 169 | |
markrad | 0:cdf462088d13 | 170 | WALK = hs->WALK; |
markrad | 0:cdf462088d13 | 171 | PT1 = hs->PT1; |
markrad | 0:cdf462088d13 | 172 | PT2 = hs->PT2; |
markrad | 0:cdf462088d13 | 173 | |
markrad | 0:cdf462088d13 | 174 | PTX = U1 = 0; |
markrad | 0:cdf462088d13 | 175 | PTY = U2 = 0; |
markrad | 0:cdf462088d13 | 176 | |
markrad | 0:cdf462088d13 | 177 | (void)PTX; |
markrad | 0:cdf462088d13 | 178 | |
markrad | 0:cdf462088d13 | 179 | memset( RES, 0, sizeof( RES ) ); |
markrad | 0:cdf462088d13 | 180 | |
markrad | 0:cdf462088d13 | 181 | while( n < MBEDTLS_HAVEGE_COLLECT_SIZE * 4 ) |
markrad | 0:cdf462088d13 | 182 | { |
markrad | 0:cdf462088d13 | 183 | ONE_ITERATION |
markrad | 0:cdf462088d13 | 184 | ONE_ITERATION |
markrad | 0:cdf462088d13 | 185 | ONE_ITERATION |
markrad | 0:cdf462088d13 | 186 | ONE_ITERATION |
markrad | 0:cdf462088d13 | 187 | } |
markrad | 0:cdf462088d13 | 188 | |
markrad | 0:cdf462088d13 | 189 | hs->PT1 = PT1; |
markrad | 0:cdf462088d13 | 190 | hs->PT2 = PT2; |
markrad | 0:cdf462088d13 | 191 | |
markrad | 0:cdf462088d13 | 192 | hs->offset[0] = 0; |
markrad | 0:cdf462088d13 | 193 | hs->offset[1] = MBEDTLS_HAVEGE_COLLECT_SIZE / 2; |
markrad | 0:cdf462088d13 | 194 | } |
markrad | 0:cdf462088d13 | 195 | |
markrad | 0:cdf462088d13 | 196 | /* |
markrad | 0:cdf462088d13 | 197 | * HAVEGE initialization |
markrad | 0:cdf462088d13 | 198 | */ |
markrad | 0:cdf462088d13 | 199 | void mbedtls_havege_init( mbedtls_havege_state *hs ) |
markrad | 0:cdf462088d13 | 200 | { |
markrad | 0:cdf462088d13 | 201 | memset( hs, 0, sizeof( mbedtls_havege_state ) ); |
markrad | 0:cdf462088d13 | 202 | |
markrad | 0:cdf462088d13 | 203 | havege_fill( hs ); |
markrad | 0:cdf462088d13 | 204 | } |
markrad | 0:cdf462088d13 | 205 | |
markrad | 0:cdf462088d13 | 206 | void mbedtls_havege_free( mbedtls_havege_state *hs ) |
markrad | 0:cdf462088d13 | 207 | { |
markrad | 0:cdf462088d13 | 208 | if( hs == NULL ) |
markrad | 0:cdf462088d13 | 209 | return; |
markrad | 0:cdf462088d13 | 210 | |
markrad | 0:cdf462088d13 | 211 | mbedtls_zeroize( hs, sizeof( mbedtls_havege_state ) ); |
markrad | 0:cdf462088d13 | 212 | } |
markrad | 0:cdf462088d13 | 213 | |
markrad | 0:cdf462088d13 | 214 | /* |
markrad | 0:cdf462088d13 | 215 | * HAVEGE rand function |
markrad | 0:cdf462088d13 | 216 | */ |
markrad | 0:cdf462088d13 | 217 | int mbedtls_havege_random( void *p_rng, unsigned char *buf, size_t len ) |
markrad | 0:cdf462088d13 | 218 | { |
markrad | 0:cdf462088d13 | 219 | int val; |
markrad | 0:cdf462088d13 | 220 | size_t use_len; |
markrad | 0:cdf462088d13 | 221 | mbedtls_havege_state *hs = (mbedtls_havege_state *) p_rng; |
markrad | 0:cdf462088d13 | 222 | unsigned char *p = buf; |
markrad | 0:cdf462088d13 | 223 | |
markrad | 0:cdf462088d13 | 224 | while( len > 0 ) |
markrad | 0:cdf462088d13 | 225 | { |
markrad | 0:cdf462088d13 | 226 | use_len = len; |
markrad | 0:cdf462088d13 | 227 | if( use_len > sizeof(int) ) |
markrad | 0:cdf462088d13 | 228 | use_len = sizeof(int); |
markrad | 0:cdf462088d13 | 229 | |
markrad | 0:cdf462088d13 | 230 | if( hs->offset[1] >= MBEDTLS_HAVEGE_COLLECT_SIZE ) |
markrad | 0:cdf462088d13 | 231 | havege_fill( hs ); |
markrad | 0:cdf462088d13 | 232 | |
markrad | 0:cdf462088d13 | 233 | val = hs->pool[hs->offset[0]++]; |
markrad | 0:cdf462088d13 | 234 | val ^= hs->pool[hs->offset[1]++]; |
markrad | 0:cdf462088d13 | 235 | |
markrad | 0:cdf462088d13 | 236 | memcpy( p, &val, use_len ); |
markrad | 0:cdf462088d13 | 237 | |
markrad | 0:cdf462088d13 | 238 | len -= use_len; |
markrad | 0:cdf462088d13 | 239 | p += use_len; |
markrad | 0:cdf462088d13 | 240 | } |
markrad | 0:cdf462088d13 | 241 | |
markrad | 0:cdf462088d13 | 242 | return( 0 ); |
markrad | 0:cdf462088d13 | 243 | } |
markrad | 0:cdf462088d13 | 244 | |
markrad | 0:cdf462088d13 | 245 | #endif /* MBEDTLS_HAVEGE_C */ |