This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
timbeight
Date:
Thu May 19 16:02:10 2016 +0000
Revision:
1:0ddbe2d3319c
Parent:
0:f7c60d3e7b8a
This is my first commit while in the class.

Who changed what in which revision?

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