Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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