forked from Novators/libsqrl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqrlEntropy.cpp
More file actions
290 lines (266 loc) · 9.44 KB
/
SqrlEntropy.cpp
File metadata and controls
290 lines (266 loc) · 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/** \file SqrlEntropy.cpp
*
* \author Adam Comley
*
* This file is part of libsqrl. It is released under the MIT license.
* For more details, see the LICENSE file included with this package.
**/
#include "sqrl_internal.h"
#include "sqrl.h"
#include "SqrlEntropy.h"
#if defined(WITH_THREADS)
#include <mutex>
#endif
#define SQRL_ENTROPY_REPEAT_FAST 9
#define SQRL_ENTROPY_REPEAT_SLOW 190
#define SQRL_ENTROPY_TARGET 512
void *libsqrl::SqrlEntropy::state = NULL;
int libsqrl::SqrlEntropy::estimated_entropy = 0;
int libsqrl::SqrlEntropy::entropy_target = SQRL_ENTROPY_TARGET;
bool libsqrl::SqrlEntropy::initialized = false;
bool libsqrl::SqrlEntropy::stopping = false;
int libsqrl::SqrlEntropy::sleeptime = SQRL_ENTROPY_REPEAT_FAST;
#if defined(WITH_THREADS)
std::mutex *libsqrl::SqrlEntropy::mutex = NULL;
std::thread *libsqrl::SqrlEntropy::thread = NULL;
#endif
#if defined(__MACH__)
#include "rdrand.h"
#include "SqrlEntropy_mac.h"
#elif defined(_WIN32)
#include "rdrand.h"
#include "SqrlEntropy_Win.h"
#elif defined(ARDUINO)
#include "SqrlEntropy_Arduino.h"
#else
#include "rdrand.h"
#include "SqrlEntropy_linux.h"
#endif
namespace libsqrl
{
struct sqrl_entropy_message
{
uint8_t *msg;
struct sqrl_fast_flux_entropy ffe;
};
void SqrlEntropy::update() {
#ifdef ARDUINO
RNG.loop();
SqrlEntropy::increment( 1 );
#else
if( !SqrlEntropy::state ) return;
struct sqrl_fast_flux_entropy ffe;
sqrl_store_fast_flux_entropy( &ffe );
SqrlEntropy::mutex->lock();
crypto_hash_sha512_update(
(crypto_hash_sha512_state*)SqrlEntropy::state,
(unsigned char*)&ffe,
sizeof( struct sqrl_fast_flux_entropy ) );
if( ++(SqrlEntropy::estimated_entropy) >= SqrlEntropy::entropy_target ) {
SqrlEntropy::sleeptime = SQRL_ENTROPY_REPEAT_SLOW;
}
SqrlEntropy::mutex->unlock();
#endif
}
void
SqrlEntropy::threadFunction() {
#ifndef ARDUINO
struct sqrl_entropy_pool *pool = (struct sqrl_entropy_pool*)SqrlEntropy::state;
crypto_hash_sha512_init( (crypto_hash_sha512_state*)SqrlEntropy::state );
SqrlEntropy::addBracket( NULL );
while( !SqrlEntropy::stopping ) {
SqrlEntropy::update();
sqrl_sleep( SqrlEntropy::sleeptime );
}
SqrlEntropy::mutex->lock();
SqrlEntropy::estimated_entropy = 0;
SqrlEntropy::initialized = false;
free( SqrlEntropy::state );
SqrlEntropy::state = NULL;
SqrlEntropy::mutex->unlock();
delete SqrlEntropy::mutex;
SqrlEntropy::mutex = NULL;
delete SqrlEntropy::thread;
SqrlEntropy::thread = NULL;
#endif
}
void SqrlEntropy::start() {
#ifdef ARDUINO
SqrlEntropy::initialized = true;
size_t len = Sqrl_Version( NULL, 0 );
char *buf = new char[len + 1];
Sqrl_Version( buf, len );
// TODO: EEPROM address?
RNG.begin( buf, 0 );
delete buf;
#else
if( SqrlEntropy::state ) return;
SqrlEntropy::initialized = true;
SqrlEntropy::stopping = false;
SqrlEntropy::estimated_entropy = 0;
SqrlEntropy::entropy_target = SQRL_ENTROPY_TARGET;
SqrlEntropy::sleeptime = SQRL_ENTROPY_REPEAT_FAST;
SqrlEntropy::state = calloc( 1, sizeof( crypto_hash_sha512_state ) );
SqrlEntropy::mutex = new std::mutex();
SqrlEntropy::thread = new std::thread( SqrlEntropy::threadFunction );
SqrlEntropy::thread->detach();
while( SqrlEntropy::estimated_entropy == 0 ) {
sqrl_sleep( 5 );
}
#endif
}
void SqrlEntropy::stop() {
#ifndef ARDUINO
if( !SqrlEntropy::state ) return;
SqrlEntropy::stopping = true;
while( SqrlEntropy::thread ) {
sqrl_sleep( 5 );
}
#endif
}
void SqrlEntropy::increment( int amount ) {
SqrlEntropy::estimated_entropy += amount;
#ifndef ARDUINO
if( SqrlEntropy::estimated_entropy >= SqrlEntropy::entropy_target ) {
SqrlEntropy::sleeptime = SQRL_ENTROPY_REPEAT_SLOW;
}
#endif
}
/**
* Collects additional entropy.
*
* Available entropy is increased by (1 + (\p msg_len / 64))
*
* @param msg A chunk of data to be added to the pool
* @param msg_len The length of \p msg (in bytes)
*/
void SqrlEntropy::add( uint8_t* msg, size_t msg_len ) {
#ifdef ARDUINO
RNG.stir( msg, msg_len );
SqrlEntropy::increment( 1 + ((int)msg_len / 64) );
#else
if( !SqrlEntropy::state ) SqrlEntropy::start();
struct sqrl_entropy_pool *pool = (struct sqrl_entropy_pool*)SqrlEntropy::state;
if( SqrlEntropy::initialized ) {
SqrlEntropy::mutex->lock();
if( SqrlEntropy::initialized ) {
struct sqrl_fast_flux_entropy ffe;
sqrl_store_fast_flux_entropy( &ffe );
uint8_t *buf = new uint8_t[msg_len + sizeof( struct sqrl_fast_flux_entropy )];
if( buf ) {
memcpy( buf, msg, msg_len );
memcpy( buf + msg_len, &ffe, sizeof( struct sqrl_fast_flux_entropy ) );
crypto_hash_sha512_update( (crypto_hash_sha512_state*)SqrlEntropy::state, (unsigned char*)buf, sizeof( buf ) );
SqrlEntropy::estimated_entropy += (1 + ((int)msg_len / 64));
if( SqrlEntropy::estimated_entropy >= SqrlEntropy::entropy_target ) {
SqrlEntropy::sleeptime = SQRL_ENTROPY_REPEAT_SLOW;
}
delete buf;
}
}
SqrlEntropy::mutex->unlock();
}
#endif
}
/**
* Gets a chunk of entropy, and resets the avaliable entropy counter. Blocks until \p desired_entropy is available.
*
* @param buf A buffer to receive the entropy. Must be at least 512 bits (64 bytes) long.
* @param desired_entropy The minimum amount of estimated entropy required.
* @return The actual amount of estimated entropy.
*/
int SqrlEntropy::get( uint8_t *buf, int desired_entropy, bool blocking ) {
if( !SqrlEntropy::initialized ) SqrlEntropy::start();
#ifndef ARDUINO
struct sqrl_entropy_pool *pool = (struct sqrl_entropy_pool*)SqrlEntropy::state;
#endif
int received_entropy = 0;
START:
if( !SqrlEntropy::initialized ) return 0;
SqrlEntropy::entropy_target = desired_entropy;
if( blocking ) {
while( SqrlEntropy::estimated_entropy < desired_entropy ) {
#ifdef ARDUINO
SqrlEntropy::update();
#else
sqrl_sleep( SQRL_ENTROPY_REPEAT_SLOW );
#endif
}
} else {
#ifdef ARDUINO
SqrlEntropy::update();
#else
if( SqrlEntropy::estimated_entropy < desired_entropy ) {
SqrlEntropy::entropy_target = desired_entropy;
SqrlEntropy::sleeptime = SQRL_ENTROPY_REPEAT_FAST;
}
#endif
return 0;
}
if( SqrlEntropy::initialized &&
SqrlEntropy::estimated_entropy >= desired_entropy ) {
#ifdef ARDUINO
RNG.rand( buf, 64 );
received_entropy = SqrlEntropy::estimated_entropy;
SqrlEntropy::estimated_entropy = 0;
#else
SqrlEntropy::mutex->lock();
if( SqrlEntropy::initialized &&
SqrlEntropy::estimated_entropy >= desired_entropy ) {
SqrlEntropy::addBracket( NULL );
crypto_hash_sha512_final( (crypto_hash_sha512_state*)SqrlEntropy::state, buf );
crypto_hash_sha512_init( (crypto_hash_sha512_state*)SqrlEntropy::state );
SqrlEntropy::addBracket( buf );
received_entropy = SqrlEntropy::estimated_entropy;
SqrlEntropy::estimated_entropy = 0;
} else {
SqrlEntropy::mutex->unlock();
goto START;
}
SqrlEntropy::mutex->unlock();
#endif
} else {
goto START;
}
SqrlEntropy::entropy_target = SQRL_ENTROPY_TARGET;
SqrlEntropy::sleeptime = SQRL_ENTROPY_REPEAT_FAST;
return received_entropy;
}
int SqrlEntropy::bytes( uint8_t* buf, int nBytes ) {
if( !buf || (nBytes <= 0) ) return 0;
int desired_entropy = (nBytes > 64) ? (8 * 64) : (8 * nBytes);
#ifdef ARDUINO
while( SqrlEntropy::estimated_entropy < desired_entropy ) {
SqrlEntropy::update();
}
RNG.rand( buf, nBytes );
SqrlEntropy::estimated_entropy = 0;
SqrlEntropy::update();
#else
uint8_t tmp[64];
sqrl_mlock( tmp, 64 );
SqrlEntropy::get( tmp, desired_entropy, true );
if( nBytes <= 64 ) {
memcpy( buf, tmp, nBytes );
} else {
crypto_stream_chacha20( (unsigned char*)buf, nBytes, tmp, (tmp + crypto_stream_chacha20_NONCEBYTES) );
}
sqrl_munlock( tmp, 64 );
#endif
return nBytes;
}
/**
* Gets the estimated amount of entropy available in the entropy collection pool.
*
* Estimated entropy is not an exact measurement. It is incremented when additional entropy is collected. We conservatively assume that each entropy collection contains AT LEAST on bit of real entropy.
*
* @return The estimated entropy (bits) available
*/
int SqrlEntropy::estimate() {
if( !SqrlEntropy::state ) SqrlEntropy::start();
if( SqrlEntropy::initialized ) {
return SqrlEntropy::estimated_entropy;
}
return 0;
}
}