Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

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