BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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