Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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