mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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