2011-01-27 16:24:17 +01:00
|
|
|
/**
|
|
|
|
* \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion
|
2009-01-03 22:22:43 +01:00
|
|
|
*
|
2015-07-27 11:11:48 +02:00
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-01-03 22:22:43 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2009-01-03 22:22:43 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* The HAVEGE RNG was designed by Andre Seznec in 2002.
|
|
|
|
*
|
|
|
|
* http://www.irisa.fr/caps/projects/hipsor/publi.php
|
|
|
|
*
|
|
|
|
* Contact: seznec(at)irisa_dot_fr - orocheco(at)irisa_dot_fr
|
|
|
|
*/
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 12:39:06 +02:00
|
|
|
#else
|
2015-04-08 12:49:31 +02:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 12:39:06 +02:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_HAVEGE_C)
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/havege.h"
|
|
|
|
#include "mbedtls/timing.h"
|
2018-04-17 16:51:09 +02:00
|
|
|
#include "mbedtls/platform_util.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2019-06-17 15:12:51 +02:00
|
|
|
#include <limits.h>
|
2011-04-24 10:57:21 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2019-06-17 15:12:51 +02:00
|
|
|
/* If int isn't capable of storing 2^32 distinct values, the code of this
|
|
|
|
* module may cause a processor trap or a miscalculation. If int is more
|
|
|
|
* than 32 bits, the code may not calculate the intended values. */
|
|
|
|
#if INT_MIN + 1 != -0x7fffffff
|
|
|
|
#error "The HAVEGE module requires int to be exactly 32 bits, with INT_MIN = -2^31."
|
|
|
|
#endif
|
|
|
|
#if UINT_MAX != 0xffffffff
|
|
|
|
#error "The HAVEGE module requires unsigned to be exactly 32 bits."
|
|
|
|
#endif
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/* ------------------------------------------------------------------------
|
|
|
|
* On average, one iteration accesses two 8-word blocks in the havege WALK
|
|
|
|
* table, and generates 16 words in the RES array.
|
|
|
|
*
|
|
|
|
* The data read in the WALK table is updated and permuted after each use.
|
|
|
|
* The result of the hardware clock counter read is used for this update.
|
|
|
|
*
|
|
|
|
* 25 conditional tests are present. The conditional tests are grouped in
|
|
|
|
* two nested groups of 12 conditional tests and 1 test that controls the
|
|
|
|
* permutation; on average, there should be 6 tests executed and 3 of them
|
|
|
|
* should be mispredicted.
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2019-06-17 15:01:08 +02:00
|
|
|
#define SWAP(X,Y) { unsigned *T = (X); (X) = (Y); (Y) = T; }
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
#define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
|
|
|
|
#define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
|
|
|
|
|
|
|
|
#define TST1_LEAVE U1++; }
|
|
|
|
#define TST2_LEAVE U2++; }
|
|
|
|
|
|
|
|
#define ONE_ITERATION \
|
|
|
|
\
|
|
|
|
PTEST = PT1 >> 20; \
|
|
|
|
\
|
|
|
|
TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
|
|
|
|
TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
|
|
|
|
TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
|
|
|
|
\
|
|
|
|
TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
|
|
|
|
TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
|
|
|
|
TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
|
|
|
|
\
|
|
|
|
PTX = (PT1 >> 18) & 7; \
|
|
|
|
PT1 &= 0x1FFF; \
|
|
|
|
PT2 &= 0x1FFF; \
|
2019-06-17 15:01:08 +02:00
|
|
|
CLK = (unsigned) mbedtls_timing_hardclock(); \
|
2009-01-03 22:22:43 +01:00
|
|
|
\
|
|
|
|
i = 0; \
|
|
|
|
A = &WALK[PT1 ]; RES[i++] ^= *A; \
|
|
|
|
B = &WALK[PT2 ]; RES[i++] ^= *B; \
|
|
|
|
C = &WALK[PT1 ^ 1]; RES[i++] ^= *C; \
|
|
|
|
D = &WALK[PT2 ^ 4]; RES[i++] ^= *D; \
|
|
|
|
\
|
|
|
|
IN = (*A >> (1)) ^ (*A << (31)) ^ CLK; \
|
|
|
|
*A = (*B >> (2)) ^ (*B << (30)) ^ CLK; \
|
|
|
|
*B = IN ^ U1; \
|
|
|
|
*C = (*C >> (3)) ^ (*C << (29)) ^ CLK; \
|
|
|
|
*D = (*D >> (4)) ^ (*D << (28)) ^ CLK; \
|
|
|
|
\
|
|
|
|
A = &WALK[PT1 ^ 2]; RES[i++] ^= *A; \
|
|
|
|
B = &WALK[PT2 ^ 2]; RES[i++] ^= *B; \
|
|
|
|
C = &WALK[PT1 ^ 3]; RES[i++] ^= *C; \
|
|
|
|
D = &WALK[PT2 ^ 6]; RES[i++] ^= *D; \
|
|
|
|
\
|
|
|
|
if( PTEST & 1 ) SWAP( A, C ); \
|
|
|
|
\
|
|
|
|
IN = (*A >> (5)) ^ (*A << (27)) ^ CLK; \
|
|
|
|
*A = (*B >> (6)) ^ (*B << (26)) ^ CLK; \
|
2019-06-17 15:01:08 +02:00
|
|
|
*B = IN; CLK = (unsigned) mbedtls_timing_hardclock(); \
|
2009-01-03 22:22:43 +01:00
|
|
|
*C = (*C >> (7)) ^ (*C << (25)) ^ CLK; \
|
|
|
|
*D = (*D >> (8)) ^ (*D << (24)) ^ CLK; \
|
|
|
|
\
|
|
|
|
A = &WALK[PT1 ^ 4]; \
|
|
|
|
B = &WALK[PT2 ^ 1]; \
|
|
|
|
\
|
|
|
|
PTEST = PT2 >> 1; \
|
|
|
|
\
|
|
|
|
PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]); \
|
|
|
|
PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8); \
|
|
|
|
PTY = (PT2 >> 10) & 7; \
|
|
|
|
\
|
|
|
|
TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
|
|
|
|
TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
|
|
|
|
TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
|
|
|
|
\
|
|
|
|
TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
|
|
|
|
TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
|
|
|
|
TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
|
|
|
|
\
|
|
|
|
C = &WALK[PT1 ^ 5]; \
|
|
|
|
D = &WALK[PT2 ^ 5]; \
|
|
|
|
\
|
|
|
|
RES[i++] ^= *A; \
|
|
|
|
RES[i++] ^= *B; \
|
|
|
|
RES[i++] ^= *C; \
|
|
|
|
RES[i++] ^= *D; \
|
|
|
|
\
|
|
|
|
IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK; \
|
|
|
|
*A = (*B >> (10)) ^ (*B << (22)) ^ CLK; \
|
|
|
|
*B = IN ^ U2; \
|
|
|
|
*C = (*C >> (11)) ^ (*C << (21)) ^ CLK; \
|
|
|
|
*D = (*D >> (12)) ^ (*D << (20)) ^ CLK; \
|
|
|
|
\
|
|
|
|
A = &WALK[PT1 ^ 6]; RES[i++] ^= *A; \
|
|
|
|
B = &WALK[PT2 ^ 3]; RES[i++] ^= *B; \
|
|
|
|
C = &WALK[PT1 ^ 7]; RES[i++] ^= *C; \
|
|
|
|
D = &WALK[PT2 ^ 7]; RES[i++] ^= *D; \
|
|
|
|
\
|
|
|
|
IN = (*A >> (13)) ^ (*A << (19)) ^ CLK; \
|
|
|
|
*A = (*B >> (14)) ^ (*B << (18)) ^ CLK; \
|
|
|
|
*B = IN; \
|
|
|
|
*C = (*C >> (15)) ^ (*C << (17)) ^ CLK; \
|
|
|
|
*D = (*D >> (16)) ^ (*D << (16)) ^ CLK; \
|
|
|
|
\
|
2014-06-17 16:39:18 +02:00
|
|
|
PT1 = ( RES[( i - 8 ) ^ PTX] ^ \
|
2009-01-03 22:22:43 +01:00
|
|
|
WALK[PT1 ^ PTX ^ 7] ) & (~1); \
|
|
|
|
PT1 ^= (PT2 ^ 0x10) & 0x10; \
|
|
|
|
\
|
|
|
|
for( n++, i = 0; i < 16; i++ ) \
|
2019-06-17 15:01:08 +02:00
|
|
|
POOL[n % MBEDTLS_HAVEGE_COLLECT_SIZE] ^= RES[i];
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Entropy gathering function
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static void havege_fill( mbedtls_havege_state *hs )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2019-06-17 15:01:08 +02:00
|
|
|
unsigned i, n = 0;
|
|
|
|
unsigned U1, U2, *A, *B, *C, *D;
|
|
|
|
unsigned PT1, PT2, *WALK, *POOL, RES[16];
|
|
|
|
unsigned PTX, PTY, CLK, PTEST, IN;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2019-06-17 15:01:08 +02:00
|
|
|
WALK = (unsigned *) hs->WALK;
|
|
|
|
POOL = (unsigned *) hs->pool;
|
2009-01-03 22:22:43 +01:00
|
|
|
PT1 = hs->PT1;
|
|
|
|
PT2 = hs->PT2;
|
|
|
|
|
|
|
|
PTX = U1 = 0;
|
|
|
|
PTY = U2 = 0;
|
|
|
|
|
2016-05-24 00:18:26 +02:00
|
|
|
(void)PTX;
|
|
|
|
|
2019-10-02 14:47:01 +02:00
|
|
|
mbedtls_platform_memset( RES, 0, sizeof( RES ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
while( n < MBEDTLS_HAVEGE_COLLECT_SIZE * 4 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
ONE_ITERATION
|
|
|
|
ONE_ITERATION
|
|
|
|
ONE_ITERATION
|
|
|
|
ONE_ITERATION
|
|
|
|
}
|
|
|
|
|
|
|
|
hs->PT1 = PT1;
|
|
|
|
hs->PT2 = PT2;
|
|
|
|
|
|
|
|
hs->offset[0] = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
hs->offset[1] = MBEDTLS_HAVEGE_COLLECT_SIZE / 2;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* HAVEGE initialization
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_havege_init( mbedtls_havege_state *hs )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2019-10-02 14:47:01 +02:00
|
|
|
mbedtls_platform_memset( hs, 0, sizeof( mbedtls_havege_state ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
havege_fill( hs );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_havege_free( mbedtls_havege_state *hs )
|
2014-06-18 16:44:11 +02:00
|
|
|
{
|
|
|
|
if( hs == NULL )
|
|
|
|
return;
|
|
|
|
|
2018-04-17 16:51:09 +02:00
|
|
|
mbedtls_platform_zeroize( hs, sizeof( mbedtls_havege_state ) );
|
2014-06-18 16:44:11 +02:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* HAVEGE rand function
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_havege_random( void *p_rng, unsigned char *buf, size_t len )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2011-11-27 22:07:34 +01:00
|
|
|
int val;
|
|
|
|
size_t use_len;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_havege_state *hs = (mbedtls_havege_state *) p_rng;
|
2011-11-27 22:07:34 +01:00
|
|
|
unsigned char *p = buf;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-11-27 22:07:34 +01:00
|
|
|
while( len > 0 )
|
|
|
|
{
|
|
|
|
use_len = len;
|
|
|
|
if( use_len > sizeof(int) )
|
|
|
|
use_len = sizeof(int);
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( hs->offset[1] >= MBEDTLS_HAVEGE_COLLECT_SIZE )
|
2011-11-27 22:07:34 +01:00
|
|
|
havege_fill( hs );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-11-27 22:07:34 +01:00
|
|
|
val = hs->pool[hs->offset[0]++];
|
|
|
|
val ^= hs->pool[hs->offset[1]++];
|
|
|
|
|
2019-10-02 08:09:31 +02:00
|
|
|
mbedtls_platform_memcpy( p, &val, use_len );
|
2014-05-01 13:03:14 +02:00
|
|
|
|
2011-11-27 22:07:34 +01:00
|
|
|
len -= use_len;
|
|
|
|
p += use_len;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-11-27 22:07:34 +01:00
|
|
|
return( 0 );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_HAVEGE_C */
|