Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
8774 rgimad 1
/**
2
 *  \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion
3
 *
4
 *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5
 *  SPDX-License-Identifier: GPL-2.0
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  (at your option) any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License along
18
 *  with this program; if not, write to the Free Software Foundation, Inc.,
19
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
 *
21
 *  This file is part of mbed TLS (https://tls.mbed.org)
22
 */
23
/*
24
 *  The HAVEGE RNG was designed by Andre Seznec in 2002.
25
 *
26
 *  http://www.irisa.fr/caps/projects/hipsor/publi.php
27
 *
28
 *  Contact: seznec(at)irisa_dot_fr - orocheco(at)irisa_dot_fr
29
 */
30
 
31
#if !defined(MBEDTLS_CONFIG_FILE)
32
#include "mbedtls/config.h"
33
#else
34
#include MBEDTLS_CONFIG_FILE
35
#endif
36
 
37
#if defined(MBEDTLS_HAVEGE_C)
38
 
39
#include "mbedtls/havege.h"
40
#include "mbedtls/timing.h"
41
#include "mbedtls/platform_util.h"
42
 
43
#include 
44
#include 
45
 
46
/* If int isn't capable of storing 2^32 distinct values, the code of this
47
 * module may cause a processor trap or a miscalculation. If int is more
48
 * than 32 bits, the code may not calculate the intended values. */
49
#if INT_MIN + 1 != -0x7fffffff
50
#error "The HAVEGE module requires int to be exactly 32 bits, with INT_MIN = -2^31."
51
#endif
52
#if UINT_MAX != 0xffffffff
53
#error "The HAVEGE module requires unsigned to be exactly 32 bits."
54
#endif
55
 
56
/* ------------------------------------------------------------------------
57
 * On average, one iteration accesses two 8-word blocks in the havege WALK
58
 * table, and generates 16 words in the RES array.
59
 *
60
 * The data read in the WALK table is updated and permuted after each use.
61
 * The result of the hardware clock counter read is used  for this update.
62
 *
63
 * 25 conditional tests are present.  The conditional tests are grouped in
64
 * two nested  groups of 12 conditional tests and 1 test that controls the
65
 * permutation; on average, there should be 6 tests executed and 3 of them
66
 * should be mispredicted.
67
 * ------------------------------------------------------------------------
68
 */
69
 
70
#define SWAP(X,Y) { unsigned *T = (X); (X) = (Y); (Y) = T; }
71
 
72
#define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
73
#define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
74
 
75
#define TST1_LEAVE U1++; }
76
#define TST2_LEAVE U2++; }
77
 
78
#define ONE_ITERATION                                   \
79
                                                        \
80
    PTEST = PT1 >> 20;                                  \
81
                                                        \
82
    TST1_ENTER  TST1_ENTER  TST1_ENTER  TST1_ENTER      \
83
    TST1_ENTER  TST1_ENTER  TST1_ENTER  TST1_ENTER      \
84
    TST1_ENTER  TST1_ENTER  TST1_ENTER  TST1_ENTER      \
85
                                                        \
86
    TST1_LEAVE  TST1_LEAVE  TST1_LEAVE  TST1_LEAVE      \
87
    TST1_LEAVE  TST1_LEAVE  TST1_LEAVE  TST1_LEAVE      \
88
    TST1_LEAVE  TST1_LEAVE  TST1_LEAVE  TST1_LEAVE      \
89
                                                        \
90
    PTX = (PT1 >> 18) & 7;                              \
91
    PT1 &= 0x1FFF;                                      \
92
    PT2 &= 0x1FFF;                                      \
93
    CLK = (unsigned) mbedtls_timing_hardclock();        \
94
                                                        \
95
    i = 0;                                              \
96
    A = &WALK[PT1    ]; RES[i++] ^= *A;                 \
97
    B = &WALK[PT2    ]; RES[i++] ^= *B;                 \
98
    C = &WALK[PT1 ^ 1]; RES[i++] ^= *C;                 \
99
    D = &WALK[PT2 ^ 4]; RES[i++] ^= *D;                 \
100
                                                        \
101
    IN = (*A >> (1)) ^ (*A << (31)) ^ CLK;              \
102
    *A = (*B >> (2)) ^ (*B << (30)) ^ CLK;              \
103
    *B = IN ^ U1;                                       \
104
    *C = (*C >> (3)) ^ (*C << (29)) ^ CLK;              \
105
    *D = (*D >> (4)) ^ (*D << (28)) ^ CLK;              \
106
                                                        \
107
    A = &WALK[PT1 ^ 2]; RES[i++] ^= *A;                 \
108
    B = &WALK[PT2 ^ 2]; RES[i++] ^= *B;                 \
109
    C = &WALK[PT1 ^ 3]; RES[i++] ^= *C;                 \
110
    D = &WALK[PT2 ^ 6]; RES[i++] ^= *D;                 \
111
                                                        \
112
    if( PTEST & 1 ) SWAP( A, C );                       \
113
                                                        \
114
    IN = (*A >> (5)) ^ (*A << (27)) ^ CLK;              \
115
    *A = (*B >> (6)) ^ (*B << (26)) ^ CLK;              \
116
    *B = IN; CLK = (unsigned) mbedtls_timing_hardclock(); \
117
    *C = (*C >> (7)) ^ (*C << (25)) ^ CLK;              \
118
    *D = (*D >> (8)) ^ (*D << (24)) ^ CLK;              \
119
                                                        \
120
    A = &WALK[PT1 ^ 4];                                 \
121
    B = &WALK[PT2 ^ 1];                                 \
122
                                                        \
123
    PTEST = PT2 >> 1;                                   \
124
                                                        \
125
    PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]);   \
126
    PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8);  \
127
    PTY = (PT2 >> 10) & 7;                              \
128
                                                        \
129
    TST2_ENTER  TST2_ENTER  TST2_ENTER  TST2_ENTER      \
130
    TST2_ENTER  TST2_ENTER  TST2_ENTER  TST2_ENTER      \
131
    TST2_ENTER  TST2_ENTER  TST2_ENTER  TST2_ENTER      \
132
                                                        \
133
    TST2_LEAVE  TST2_LEAVE  TST2_LEAVE  TST2_LEAVE      \
134
    TST2_LEAVE  TST2_LEAVE  TST2_LEAVE  TST2_LEAVE      \
135
    TST2_LEAVE  TST2_LEAVE  TST2_LEAVE  TST2_LEAVE      \
136
                                                        \
137
    C = &WALK[PT1 ^ 5];                                 \
138
    D = &WALK[PT2 ^ 5];                                 \
139
                                                        \
140
    RES[i++] ^= *A;                                     \
141
    RES[i++] ^= *B;                                     \
142
    RES[i++] ^= *C;                                     \
143
    RES[i++] ^= *D;                                     \
144
                                                        \
145
    IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK;             \
146
    *A = (*B >> (10)) ^ (*B << (22)) ^ CLK;             \
147
    *B = IN ^ U2;                                       \
148
    *C = (*C >> (11)) ^ (*C << (21)) ^ CLK;             \
149
    *D = (*D >> (12)) ^ (*D << (20)) ^ CLK;             \
150
                                                        \
151
    A = &WALK[PT1 ^ 6]; RES[i++] ^= *A;                 \
152
    B = &WALK[PT2 ^ 3]; RES[i++] ^= *B;                 \
153
    C = &WALK[PT1 ^ 7]; RES[i++] ^= *C;                 \
154
    D = &WALK[PT2 ^ 7]; RES[i++] ^= *D;                 \
155
                                                        \
156
    IN = (*A >> (13)) ^ (*A << (19)) ^ CLK;             \
157
    *A = (*B >> (14)) ^ (*B << (18)) ^ CLK;             \
158
    *B = IN;                                            \
159
    *C = (*C >> (15)) ^ (*C << (17)) ^ CLK;             \
160
    *D = (*D >> (16)) ^ (*D << (16)) ^ CLK;             \
161
                                                        \
162
    PT1 = ( RES[( i - 8 ) ^ PTX] ^                      \
163
            WALK[PT1 ^ PTX ^ 7] ) & (~1);               \
164
    PT1 ^= (PT2 ^ 0x10) & 0x10;                         \
165
                                                        \
166
    for( n++, i = 0; i < 16; i++ )                      \
167
        POOL[n % MBEDTLS_HAVEGE_COLLECT_SIZE] ^= RES[i];
168
 
169
/*
170
 * Entropy gathering function
171
 */
172
static void havege_fill( mbedtls_havege_state *hs )
173
{
174
    unsigned i, n = 0;
175
    unsigned  U1,  U2, *A, *B, *C, *D;
176
    unsigned PT1, PT2, *WALK, *POOL, RES[16];
177
    unsigned PTX, PTY, CLK, PTEST, IN;
178
 
179
    WALK = (unsigned *) hs->WALK;
180
    POOL = (unsigned *) hs->pool;
181
    PT1  = hs->PT1;
182
    PT2  = hs->PT2;
183
 
184
    PTX  = U1 = 0;
185
    PTY  = U2 = 0;
186
 
187
    (void)PTX;
188
 
189
    memset( RES, 0, sizeof( RES ) );
190
 
191
    while( n < MBEDTLS_HAVEGE_COLLECT_SIZE * 4 )
192
    {
193
        ONE_ITERATION
194
        ONE_ITERATION
195
        ONE_ITERATION
196
        ONE_ITERATION
197
    }
198
 
199
    hs->PT1 = PT1;
200
    hs->PT2 = PT2;
201
 
202
    hs->offset[0] = 0;
203
    hs->offset[1] = MBEDTLS_HAVEGE_COLLECT_SIZE / 2;
204
}
205
 
206
/*
207
 * HAVEGE initialization
208
 */
209
void mbedtls_havege_init( mbedtls_havege_state *hs )
210
{
211
    memset( hs, 0, sizeof( mbedtls_havege_state ) );
212
 
213
    havege_fill( hs );
214
}
215
 
216
void mbedtls_havege_free( mbedtls_havege_state *hs )
217
{
218
    if( hs == NULL )
219
        return;
220
 
221
    mbedtls_platform_zeroize( hs, sizeof( mbedtls_havege_state ) );
222
}
223
 
224
/*
225
 * HAVEGE rand function
226
 */
227
int mbedtls_havege_random( void *p_rng, unsigned char *buf, size_t len )
228
{
229
    int val;
230
    size_t use_len;
231
    mbedtls_havege_state *hs = (mbedtls_havege_state *) p_rng;
232
    unsigned char *p = buf;
233
 
234
    while( len > 0 )
235
    {
236
        use_len = len;
237
        if( use_len > sizeof(int) )
238
            use_len = sizeof(int);
239
 
240
        if( hs->offset[1] >= MBEDTLS_HAVEGE_COLLECT_SIZE )
241
            havege_fill( hs );
242
 
243
        val  = hs->pool[hs->offset[0]++];
244
        val ^= hs->pool[hs->offset[1]++];
245
 
246
        memcpy( p, &val, use_len );
247
 
248
        len -= use_len;
249
        p += use_len;
250
    }
251
 
252
    return( 0 );
253
}
254
 
255
#endif /* MBEDTLS_HAVEGE_C */