mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
features/mbedtls/mbed-crypto/src/havege.c@1:9db0e321a9f4, 2019-12-31 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue Dec 31 06:02:27 2019 +0000
- Revision:
- 1:9db0e321a9f4
- Parent:
- 0:5b88d5760320
updated based on mbed-os5.15.0
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:9db0e321a9f4 | 41 | #include <stdint.h> |
kenjiArai | 0:5b88d5760320 | 42 | #include <string.h> |
kenjiArai | 0:5b88d5760320 | 43 | |
kenjiArai | 0:5b88d5760320 | 44 | /* ------------------------------------------------------------------------ |
kenjiArai | 0:5b88d5760320 | 45 | * On average, one iteration accesses two 8-word blocks in the havege WALK |
kenjiArai | 0:5b88d5760320 | 46 | * table, and generates 16 words in the RES array. |
kenjiArai | 0:5b88d5760320 | 47 | * |
kenjiArai | 0:5b88d5760320 | 48 | * The data read in the WALK table is updated and permuted after each use. |
kenjiArai | 0:5b88d5760320 | 49 | * The result of the hardware clock counter read is used for this update. |
kenjiArai | 0:5b88d5760320 | 50 | * |
kenjiArai | 0:5b88d5760320 | 51 | * 25 conditional tests are present. The conditional tests are grouped in |
kenjiArai | 0:5b88d5760320 | 52 | * two nested groups of 12 conditional tests and 1 test that controls the |
kenjiArai | 0:5b88d5760320 | 53 | * permutation; on average, there should be 6 tests executed and 3 of them |
kenjiArai | 0:5b88d5760320 | 54 | * should be mispredicted. |
kenjiArai | 0:5b88d5760320 | 55 | * ------------------------------------------------------------------------ |
kenjiArai | 0:5b88d5760320 | 56 | */ |
kenjiArai | 0:5b88d5760320 | 57 | |
kenjiArai | 1:9db0e321a9f4 | 58 | #define SWAP(X,Y) { uint32_t *T = (X); (X) = (Y); (Y) = T; } |
kenjiArai | 0:5b88d5760320 | 59 | |
kenjiArai | 0:5b88d5760320 | 60 | #define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1; |
kenjiArai | 0:5b88d5760320 | 61 | #define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1; |
kenjiArai | 0:5b88d5760320 | 62 | |
kenjiArai | 0:5b88d5760320 | 63 | #define TST1_LEAVE U1++; } |
kenjiArai | 0:5b88d5760320 | 64 | #define TST2_LEAVE U2++; } |
kenjiArai | 0:5b88d5760320 | 65 | |
kenjiArai | 0:5b88d5760320 | 66 | #define ONE_ITERATION \ |
kenjiArai | 0:5b88d5760320 | 67 | \ |
kenjiArai | 0:5b88d5760320 | 68 | PTEST = PT1 >> 20; \ |
kenjiArai | 0:5b88d5760320 | 69 | \ |
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 | TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \ |
kenjiArai | 0:5b88d5760320 | 73 | \ |
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 | TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \ |
kenjiArai | 0:5b88d5760320 | 77 | \ |
kenjiArai | 0:5b88d5760320 | 78 | PTX = (PT1 >> 18) & 7; \ |
kenjiArai | 0:5b88d5760320 | 79 | PT1 &= 0x1FFF; \ |
kenjiArai | 0:5b88d5760320 | 80 | PT2 &= 0x1FFF; \ |
kenjiArai | 1:9db0e321a9f4 | 81 | CLK = (uint32_t) mbedtls_timing_hardclock(); \ |
kenjiArai | 0:5b88d5760320 | 82 | \ |
kenjiArai | 0:5b88d5760320 | 83 | i = 0; \ |
kenjiArai | 0:5b88d5760320 | 84 | A = &WALK[PT1 ]; RES[i++] ^= *A; \ |
kenjiArai | 0:5b88d5760320 | 85 | B = &WALK[PT2 ]; RES[i++] ^= *B; \ |
kenjiArai | 0:5b88d5760320 | 86 | C = &WALK[PT1 ^ 1]; RES[i++] ^= *C; \ |
kenjiArai | 0:5b88d5760320 | 87 | D = &WALK[PT2 ^ 4]; RES[i++] ^= *D; \ |
kenjiArai | 0:5b88d5760320 | 88 | \ |
kenjiArai | 0:5b88d5760320 | 89 | IN = (*A >> (1)) ^ (*A << (31)) ^ CLK; \ |
kenjiArai | 0:5b88d5760320 | 90 | *A = (*B >> (2)) ^ (*B << (30)) ^ CLK; \ |
kenjiArai | 0:5b88d5760320 | 91 | *B = IN ^ U1; \ |
kenjiArai | 0:5b88d5760320 | 92 | *C = (*C >> (3)) ^ (*C << (29)) ^ CLK; \ |
kenjiArai | 0:5b88d5760320 | 93 | *D = (*D >> (4)) ^ (*D << (28)) ^ CLK; \ |
kenjiArai | 0:5b88d5760320 | 94 | \ |
kenjiArai | 0:5b88d5760320 | 95 | A = &WALK[PT1 ^ 2]; RES[i++] ^= *A; \ |
kenjiArai | 0:5b88d5760320 | 96 | B = &WALK[PT2 ^ 2]; RES[i++] ^= *B; \ |
kenjiArai | 0:5b88d5760320 | 97 | C = &WALK[PT1 ^ 3]; RES[i++] ^= *C; \ |
kenjiArai | 0:5b88d5760320 | 98 | D = &WALK[PT2 ^ 6]; RES[i++] ^= *D; \ |
kenjiArai | 0:5b88d5760320 | 99 | \ |
kenjiArai | 0:5b88d5760320 | 100 | if( PTEST & 1 ) SWAP( A, C ); \ |
kenjiArai | 0:5b88d5760320 | 101 | \ |
kenjiArai | 0:5b88d5760320 | 102 | IN = (*A >> (5)) ^ (*A << (27)) ^ CLK; \ |
kenjiArai | 0:5b88d5760320 | 103 | *A = (*B >> (6)) ^ (*B << (26)) ^ CLK; \ |
kenjiArai | 1:9db0e321a9f4 | 104 | *B = IN; CLK = (uint32_t) mbedtls_timing_hardclock(); \ |
kenjiArai | 0:5b88d5760320 | 105 | *C = (*C >> (7)) ^ (*C << (25)) ^ CLK; \ |
kenjiArai | 0:5b88d5760320 | 106 | *D = (*D >> (8)) ^ (*D << (24)) ^ CLK; \ |
kenjiArai | 0:5b88d5760320 | 107 | \ |
kenjiArai | 0:5b88d5760320 | 108 | A = &WALK[PT1 ^ 4]; \ |
kenjiArai | 0:5b88d5760320 | 109 | B = &WALK[PT2 ^ 1]; \ |
kenjiArai | 0:5b88d5760320 | 110 | \ |
kenjiArai | 0:5b88d5760320 | 111 | PTEST = PT2 >> 1; \ |
kenjiArai | 0:5b88d5760320 | 112 | \ |
kenjiArai | 0:5b88d5760320 | 113 | PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]); \ |
kenjiArai | 0:5b88d5760320 | 114 | PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8); \ |
kenjiArai | 0:5b88d5760320 | 115 | PTY = (PT2 >> 10) & 7; \ |
kenjiArai | 0:5b88d5760320 | 116 | \ |
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 | TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \ |
kenjiArai | 0:5b88d5760320 | 120 | \ |
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 | TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \ |
kenjiArai | 0:5b88d5760320 | 124 | \ |
kenjiArai | 0:5b88d5760320 | 125 | C = &WALK[PT1 ^ 5]; \ |
kenjiArai | 0:5b88d5760320 | 126 | D = &WALK[PT2 ^ 5]; \ |
kenjiArai | 0:5b88d5760320 | 127 | \ |
kenjiArai | 0:5b88d5760320 | 128 | RES[i++] ^= *A; \ |
kenjiArai | 0:5b88d5760320 | 129 | RES[i++] ^= *B; \ |
kenjiArai | 0:5b88d5760320 | 130 | RES[i++] ^= *C; \ |
kenjiArai | 0:5b88d5760320 | 131 | RES[i++] ^= *D; \ |
kenjiArai | 0:5b88d5760320 | 132 | \ |
kenjiArai | 0:5b88d5760320 | 133 | IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK; \ |
kenjiArai | 0:5b88d5760320 | 134 | *A = (*B >> (10)) ^ (*B << (22)) ^ CLK; \ |
kenjiArai | 0:5b88d5760320 | 135 | *B = IN ^ U2; \ |
kenjiArai | 0:5b88d5760320 | 136 | *C = (*C >> (11)) ^ (*C << (21)) ^ CLK; \ |
kenjiArai | 0:5b88d5760320 | 137 | *D = (*D >> (12)) ^ (*D << (20)) ^ CLK; \ |
kenjiArai | 0:5b88d5760320 | 138 | \ |
kenjiArai | 0:5b88d5760320 | 139 | A = &WALK[PT1 ^ 6]; RES[i++] ^= *A; \ |
kenjiArai | 0:5b88d5760320 | 140 | B = &WALK[PT2 ^ 3]; RES[i++] ^= *B; \ |
kenjiArai | 0:5b88d5760320 | 141 | C = &WALK[PT1 ^ 7]; RES[i++] ^= *C; \ |
kenjiArai | 0:5b88d5760320 | 142 | D = &WALK[PT2 ^ 7]; RES[i++] ^= *D; \ |
kenjiArai | 0:5b88d5760320 | 143 | \ |
kenjiArai | 0:5b88d5760320 | 144 | IN = (*A >> (13)) ^ (*A << (19)) ^ CLK; \ |
kenjiArai | 0:5b88d5760320 | 145 | *A = (*B >> (14)) ^ (*B << (18)) ^ CLK; \ |
kenjiArai | 0:5b88d5760320 | 146 | *B = IN; \ |
kenjiArai | 0:5b88d5760320 | 147 | *C = (*C >> (15)) ^ (*C << (17)) ^ CLK; \ |
kenjiArai | 0:5b88d5760320 | 148 | *D = (*D >> (16)) ^ (*D << (16)) ^ CLK; \ |
kenjiArai | 0:5b88d5760320 | 149 | \ |
kenjiArai | 0:5b88d5760320 | 150 | PT1 = ( RES[( i - 8 ) ^ PTX] ^ \ |
kenjiArai | 0:5b88d5760320 | 151 | WALK[PT1 ^ PTX ^ 7] ) & (~1); \ |
kenjiArai | 0:5b88d5760320 | 152 | PT1 ^= (PT2 ^ 0x10) & 0x10; \ |
kenjiArai | 0:5b88d5760320 | 153 | \ |
kenjiArai | 0:5b88d5760320 | 154 | for( n++, i = 0; i < 16; i++ ) \ |
kenjiArai | 0:5b88d5760320 | 155 | hs->pool[n % MBEDTLS_HAVEGE_COLLECT_SIZE] ^= RES[i]; |
kenjiArai | 0:5b88d5760320 | 156 | |
kenjiArai | 0:5b88d5760320 | 157 | /* |
kenjiArai | 0:5b88d5760320 | 158 | * Entropy gathering function |
kenjiArai | 0:5b88d5760320 | 159 | */ |
kenjiArai | 0:5b88d5760320 | 160 | static void havege_fill( mbedtls_havege_state *hs ) |
kenjiArai | 0:5b88d5760320 | 161 | { |
kenjiArai | 1:9db0e321a9f4 | 162 | size_t n = 0; |
kenjiArai | 1:9db0e321a9f4 | 163 | size_t i; |
kenjiArai | 1:9db0e321a9f4 | 164 | uint32_t U1, U2, *A, *B, *C, *D; |
kenjiArai | 1:9db0e321a9f4 | 165 | uint32_t PT1, PT2, *WALK, RES[16]; |
kenjiArai | 1:9db0e321a9f4 | 166 | uint32_t PTX, PTY, CLK, PTEST, IN; |
kenjiArai | 0:5b88d5760320 | 167 | |
kenjiArai | 0:5b88d5760320 | 168 | WALK = hs->WALK; |
kenjiArai | 0:5b88d5760320 | 169 | PT1 = hs->PT1; |
kenjiArai | 0:5b88d5760320 | 170 | PT2 = hs->PT2; |
kenjiArai | 0:5b88d5760320 | 171 | |
kenjiArai | 0:5b88d5760320 | 172 | PTX = U1 = 0; |
kenjiArai | 0:5b88d5760320 | 173 | PTY = U2 = 0; |
kenjiArai | 0:5b88d5760320 | 174 | |
kenjiArai | 0:5b88d5760320 | 175 | (void)PTX; |
kenjiArai | 0:5b88d5760320 | 176 | |
kenjiArai | 0:5b88d5760320 | 177 | memset( RES, 0, sizeof( RES ) ); |
kenjiArai | 0:5b88d5760320 | 178 | |
kenjiArai | 0:5b88d5760320 | 179 | while( n < MBEDTLS_HAVEGE_COLLECT_SIZE * 4 ) |
kenjiArai | 0:5b88d5760320 | 180 | { |
kenjiArai | 0:5b88d5760320 | 181 | ONE_ITERATION |
kenjiArai | 0:5b88d5760320 | 182 | ONE_ITERATION |
kenjiArai | 0:5b88d5760320 | 183 | ONE_ITERATION |
kenjiArai | 0:5b88d5760320 | 184 | ONE_ITERATION |
kenjiArai | 0:5b88d5760320 | 185 | } |
kenjiArai | 0:5b88d5760320 | 186 | |
kenjiArai | 0:5b88d5760320 | 187 | hs->PT1 = PT1; |
kenjiArai | 0:5b88d5760320 | 188 | hs->PT2 = PT2; |
kenjiArai | 0:5b88d5760320 | 189 | |
kenjiArai | 0:5b88d5760320 | 190 | hs->offset[0] = 0; |
kenjiArai | 0:5b88d5760320 | 191 | hs->offset[1] = MBEDTLS_HAVEGE_COLLECT_SIZE / 2; |
kenjiArai | 0:5b88d5760320 | 192 | } |
kenjiArai | 0:5b88d5760320 | 193 | |
kenjiArai | 0:5b88d5760320 | 194 | /* |
kenjiArai | 0:5b88d5760320 | 195 | * HAVEGE initialization |
kenjiArai | 0:5b88d5760320 | 196 | */ |
kenjiArai | 0:5b88d5760320 | 197 | void mbedtls_havege_init( mbedtls_havege_state *hs ) |
kenjiArai | 0:5b88d5760320 | 198 | { |
kenjiArai | 0:5b88d5760320 | 199 | memset( hs, 0, sizeof( mbedtls_havege_state ) ); |
kenjiArai | 0:5b88d5760320 | 200 | |
kenjiArai | 0:5b88d5760320 | 201 | havege_fill( hs ); |
kenjiArai | 0:5b88d5760320 | 202 | } |
kenjiArai | 0:5b88d5760320 | 203 | |
kenjiArai | 0:5b88d5760320 | 204 | void mbedtls_havege_free( mbedtls_havege_state *hs ) |
kenjiArai | 0:5b88d5760320 | 205 | { |
kenjiArai | 0:5b88d5760320 | 206 | if( hs == NULL ) |
kenjiArai | 0:5b88d5760320 | 207 | return; |
kenjiArai | 0:5b88d5760320 | 208 | |
kenjiArai | 0:5b88d5760320 | 209 | mbedtls_platform_zeroize( hs, sizeof( mbedtls_havege_state ) ); |
kenjiArai | 0:5b88d5760320 | 210 | } |
kenjiArai | 0:5b88d5760320 | 211 | |
kenjiArai | 0:5b88d5760320 | 212 | /* |
kenjiArai | 0:5b88d5760320 | 213 | * HAVEGE rand function |
kenjiArai | 0:5b88d5760320 | 214 | */ |
kenjiArai | 0:5b88d5760320 | 215 | int mbedtls_havege_random( void *p_rng, unsigned char *buf, size_t len ) |
kenjiArai | 0:5b88d5760320 | 216 | { |
kenjiArai | 1:9db0e321a9f4 | 217 | uint32_t val; |
kenjiArai | 0:5b88d5760320 | 218 | size_t use_len; |
kenjiArai | 0:5b88d5760320 | 219 | mbedtls_havege_state *hs = (mbedtls_havege_state *) p_rng; |
kenjiArai | 0:5b88d5760320 | 220 | unsigned char *p = buf; |
kenjiArai | 0:5b88d5760320 | 221 | |
kenjiArai | 0:5b88d5760320 | 222 | while( len > 0 ) |
kenjiArai | 0:5b88d5760320 | 223 | { |
kenjiArai | 0:5b88d5760320 | 224 | use_len = len; |
kenjiArai | 1:9db0e321a9f4 | 225 | if( use_len > sizeof( val ) ) |
kenjiArai | 1:9db0e321a9f4 | 226 | use_len = sizeof( val ); |
kenjiArai | 0:5b88d5760320 | 227 | |
kenjiArai | 0:5b88d5760320 | 228 | if( hs->offset[1] >= MBEDTLS_HAVEGE_COLLECT_SIZE ) |
kenjiArai | 0:5b88d5760320 | 229 | havege_fill( hs ); |
kenjiArai | 0:5b88d5760320 | 230 | |
kenjiArai | 0:5b88d5760320 | 231 | val = hs->pool[hs->offset[0]++]; |
kenjiArai | 0:5b88d5760320 | 232 | val ^= hs->pool[hs->offset[1]++]; |
kenjiArai | 0:5b88d5760320 | 233 | |
kenjiArai | 0:5b88d5760320 | 234 | memcpy( p, &val, use_len ); |
kenjiArai | 0:5b88d5760320 | 235 | |
kenjiArai | 0:5b88d5760320 | 236 | len -= use_len; |
kenjiArai | 0:5b88d5760320 | 237 | p += use_len; |
kenjiArai | 0:5b88d5760320 | 238 | } |
kenjiArai | 0:5b88d5760320 | 239 | |
kenjiArai | 0:5b88d5760320 | 240 | return( 0 ); |
kenjiArai | 0:5b88d5760320 | 241 | } |
kenjiArai | 0:5b88d5760320 | 242 | |
kenjiArai | 0:5b88d5760320 | 243 | #endif /* MBEDTLS_HAVEGE_C */ |