Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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