mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 17 23:23:45 2019 +0000
Revision:
0:5b88d5760320
Child:
1:9db0e321a9f4
mbed-os5 only for TYBLE16

Who changed what in which revision?

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