Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers havege.c Source File

havege.c

00001 /**
00002  *  \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion
00003  *
00004  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
00005  *  SPDX-License-Identifier: Apache-2.0
00006  *
00007  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00008  *  not use this file except in compliance with the License.
00009  *  You may obtain a copy of the License at
00010  *
00011  *  http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  *  Unless required by applicable law or agreed to in writing, software
00014  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00015  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  *  See the License for the specific language governing permissions and
00017  *  limitations under the License.
00018  *
00019  *  This file is part of mbed TLS (https://tls.mbed.org)
00020  */
00021 /*
00022  *  The HAVEGE RNG was designed by Andre Seznec in 2002.
00023  *
00024  *  http://www.irisa.fr/caps/projects/hipsor/publi.php
00025  *
00026  *  Contact: seznec(at)irisa_dot_fr - orocheco(at)irisa_dot_fr
00027  */
00028 
00029 #if !defined(MBEDTLS_CONFIG_FILE)
00030 #include "mbedtls/config.h"
00031 #else
00032 #include MBEDTLS_CONFIG_FILE
00033 #endif
00034 
00035 #if defined(MBEDTLS_HAVEGE_C)
00036 
00037 #include "mbedtls/havege.h"
00038 #include "mbedtls/timing.h"
00039 #include "mbedtls/platform_util.h"
00040 
00041 #include <stdint.h>
00042 #include <string.h>
00043 
00044 /* ------------------------------------------------------------------------
00045  * On average, one iteration accesses two 8-word blocks in the havege WALK
00046  * table, and generates 16 words in the RES array.
00047  *
00048  * The data read in the WALK table is updated and permuted after each use.
00049  * The result of the hardware clock counter read is used  for this update.
00050  *
00051  * 25 conditional tests are present.  The conditional tests are grouped in
00052  * two nested  groups of 12 conditional tests and 1 test that controls the
00053  * permutation; on average, there should be 6 tests executed and 3 of them
00054  * should be mispredicted.
00055  * ------------------------------------------------------------------------
00056  */
00057 
00058 #define SWAP(X,Y) { uint32_t *T = (X); (X) = (Y); (Y) = T; }
00059 
00060 #define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
00061 #define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
00062 
00063 #define TST1_LEAVE U1++; }
00064 #define TST2_LEAVE U2++; }
00065 
00066 #define ONE_ITERATION                                   \
00067                                                         \
00068     PTEST = PT1 >> 20;                                  \
00069                                                         \
00070     TST1_ENTER  TST1_ENTER  TST1_ENTER  TST1_ENTER      \
00071     TST1_ENTER  TST1_ENTER  TST1_ENTER  TST1_ENTER      \
00072     TST1_ENTER  TST1_ENTER  TST1_ENTER  TST1_ENTER      \
00073                                                         \
00074     TST1_LEAVE  TST1_LEAVE  TST1_LEAVE  TST1_LEAVE      \
00075     TST1_LEAVE  TST1_LEAVE  TST1_LEAVE  TST1_LEAVE      \
00076     TST1_LEAVE  TST1_LEAVE  TST1_LEAVE  TST1_LEAVE      \
00077                                                         \
00078     PTX = (PT1 >> 18) & 7;                              \
00079     PT1 &= 0x1FFF;                                      \
00080     PT2 &= 0x1FFF;                                      \
00081     CLK = (uint32_t) mbedtls_timing_hardclock();        \
00082                                                         \
00083     i = 0;                                              \
00084     A = &WALK[PT1    ]; RES[i++] ^= *A;                 \
00085     B = &WALK[PT2    ]; RES[i++] ^= *B;                 \
00086     C = &WALK[PT1 ^ 1]; RES[i++] ^= *C;                 \
00087     D = &WALK[PT2 ^ 4]; RES[i++] ^= *D;                 \
00088                                                         \
00089     IN = (*A >> (1)) ^ (*A << (31)) ^ CLK;              \
00090     *A = (*B >> (2)) ^ (*B << (30)) ^ CLK;              \
00091     *B = IN ^ U1;                                       \
00092     *C = (*C >> (3)) ^ (*C << (29)) ^ CLK;              \
00093     *D = (*D >> (4)) ^ (*D << (28)) ^ CLK;              \
00094                                                         \
00095     A = &WALK[PT1 ^ 2]; RES[i++] ^= *A;                 \
00096     B = &WALK[PT2 ^ 2]; RES[i++] ^= *B;                 \
00097     C = &WALK[PT1 ^ 3]; RES[i++] ^= *C;                 \
00098     D = &WALK[PT2 ^ 6]; RES[i++] ^= *D;                 \
00099                                                         \
00100     if( PTEST & 1 ) SWAP( A, C );                       \
00101                                                         \
00102     IN = (*A >> (5)) ^ (*A << (27)) ^ CLK;              \
00103     *A = (*B >> (6)) ^ (*B << (26)) ^ CLK;              \
00104     *B = IN; CLK = (uint32_t) mbedtls_timing_hardclock();       \
00105     *C = (*C >> (7)) ^ (*C << (25)) ^ CLK;              \
00106     *D = (*D >> (8)) ^ (*D << (24)) ^ CLK;              \
00107                                                         \
00108     A = &WALK[PT1 ^ 4];                                 \
00109     B = &WALK[PT2 ^ 1];                                 \
00110                                                         \
00111     PTEST = PT2 >> 1;                                   \
00112                                                         \
00113     PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]);   \
00114     PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8);  \
00115     PTY = (PT2 >> 10) & 7;                              \
00116                                                         \
00117     TST2_ENTER  TST2_ENTER  TST2_ENTER  TST2_ENTER      \
00118     TST2_ENTER  TST2_ENTER  TST2_ENTER  TST2_ENTER      \
00119     TST2_ENTER  TST2_ENTER  TST2_ENTER  TST2_ENTER      \
00120                                                         \
00121     TST2_LEAVE  TST2_LEAVE  TST2_LEAVE  TST2_LEAVE      \
00122     TST2_LEAVE  TST2_LEAVE  TST2_LEAVE  TST2_LEAVE      \
00123     TST2_LEAVE  TST2_LEAVE  TST2_LEAVE  TST2_LEAVE      \
00124                                                         \
00125     C = &WALK[PT1 ^ 5];                                 \
00126     D = &WALK[PT2 ^ 5];                                 \
00127                                                         \
00128     RES[i++] ^= *A;                                     \
00129     RES[i++] ^= *B;                                     \
00130     RES[i++] ^= *C;                                     \
00131     RES[i++] ^= *D;                                     \
00132                                                         \
00133     IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK;             \
00134     *A = (*B >> (10)) ^ (*B << (22)) ^ CLK;             \
00135     *B = IN ^ U2;                                       \
00136     *C = (*C >> (11)) ^ (*C << (21)) ^ CLK;             \
00137     *D = (*D >> (12)) ^ (*D << (20)) ^ CLK;             \
00138                                                         \
00139     A = &WALK[PT1 ^ 6]; RES[i++] ^= *A;                 \
00140     B = &WALK[PT2 ^ 3]; RES[i++] ^= *B;                 \
00141     C = &WALK[PT1 ^ 7]; RES[i++] ^= *C;                 \
00142     D = &WALK[PT2 ^ 7]; RES[i++] ^= *D;                 \
00143                                                         \
00144     IN = (*A >> (13)) ^ (*A << (19)) ^ CLK;             \
00145     *A = (*B >> (14)) ^ (*B << (18)) ^ CLK;             \
00146     *B = IN;                                            \
00147     *C = (*C >> (15)) ^ (*C << (17)) ^ CLK;             \
00148     *D = (*D >> (16)) ^ (*D << (16)) ^ CLK;             \
00149                                                         \
00150     PT1 = ( RES[( i - 8 ) ^ PTX] ^                      \
00151             WALK[PT1 ^ PTX ^ 7] ) & (~1);               \
00152     PT1 ^= (PT2 ^ 0x10) & 0x10;                         \
00153                                                         \
00154     for( n++, i = 0; i < 16; i++ )                      \
00155         hs->pool[n % MBEDTLS_HAVEGE_COLLECT_SIZE] ^= RES[i];
00156 
00157 /*
00158  * Entropy gathering function
00159  */
00160 static void havege_fill( mbedtls_havege_state *hs )
00161 {
00162     size_t n = 0;
00163     size_t i;
00164     uint32_t  U1,  U2, *A, *B, *C, *D;
00165     uint32_t PT1, PT2, *WALK, RES[16];
00166     uint32_t PTX, PTY, CLK, PTEST, IN;
00167 
00168     WALK = hs->WALK;
00169     PT1  = hs->PT1;
00170     PT2  = hs->PT2;
00171 
00172     PTX  = U1 = 0;
00173     PTY  = U2 = 0;
00174 
00175     (void)PTX;
00176 
00177     memset( RES, 0, sizeof( RES ) );
00178 
00179     while( n < MBEDTLS_HAVEGE_COLLECT_SIZE * 4 )
00180     {
00181         ONE_ITERATION
00182         ONE_ITERATION
00183         ONE_ITERATION
00184         ONE_ITERATION
00185     }
00186 
00187     hs->PT1 = PT1;
00188     hs->PT2 = PT2;
00189 
00190     hs->offset[0] = 0;
00191     hs->offset[1] = MBEDTLS_HAVEGE_COLLECT_SIZE / 2;
00192 }
00193 
00194 /*
00195  * HAVEGE initialization
00196  */
00197 void mbedtls_havege_init( mbedtls_havege_state *hs )
00198 {
00199     memset( hs, 0, sizeof( mbedtls_havege_state ) );
00200 
00201     havege_fill( hs );
00202 }
00203 
00204 void mbedtls_havege_free( mbedtls_havege_state *hs )
00205 {
00206     if( hs == NULL )
00207         return;
00208 
00209     mbedtls_platform_zeroize( hs, sizeof( mbedtls_havege_state ) );
00210 }
00211 
00212 /*
00213  * HAVEGE rand function
00214  */
00215 int mbedtls_havege_random( void *p_rng, unsigned char *buf, size_t len )
00216 {
00217     uint32_t val;
00218     size_t use_len;
00219     mbedtls_havege_state *hs = (mbedtls_havege_state *) p_rng;
00220     unsigned char *p = buf;
00221 
00222     while( len > 0 )
00223     {
00224         use_len = len;
00225         if( use_len > sizeof( val ) )
00226             use_len = sizeof( val );
00227 
00228         if( hs->offset[1] >= MBEDTLS_HAVEGE_COLLECT_SIZE )
00229             havege_fill( hs );
00230 
00231         val  = hs->pool[hs->offset[0]++];
00232         val ^= hs->pool[hs->offset[1]++];
00233 
00234         memcpy( p, &val, use_len );
00235 
00236         len -= use_len;
00237         p += use_len;
00238     }
00239 
00240     return( 0 );
00241 }
00242 
00243 #endif /* MBEDTLS_HAVEGE_C */