nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nitsshukla
Date:
Fri Nov 04 12:06:04 2016 +0000
Revision:
7:3a65ef12ba31
Parent:
1:55a6170b404f
kghj;

Who changed what in which revision?

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