-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrdrand.cpp
More file actions
43 lines (33 loc) · 1.09 KB
/
rdrand.cpp
File metadata and controls
43 lines (33 loc) · 1.09 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
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "api.h"
#include "immintrin.h"
/// \file rdrand.cpp
/// \brief This contains the routine to get randomness using rdrand_fill
namespace slh_dsa {
///
/// Call rdrand to fill the buffer with randomness
success_flag rdrand_fill( void* target, size_t bytes_to_fill) {
unsigned char* buffer = (unsigned char*)target;
unsigned long long temp;
// Subtle note: this will call _rdrand64_step one more time than necessary,
// and will completely ignore the last value returned. It does that so
// that the final value on the stack when we return will be that last
// value (that we ignored), which is uncorrelated to anything we put into
// target
for (;;) {
if (0 == _rdrand64_step( &temp )) {
// rdrand failed
return failure;
}
if (bytes_to_fill == 0) break;
size_t next_to_fill = bytes_to_fill;
if (next_to_fill > 8) next_to_fill = 8;
memcpy( buffer, &temp, next_to_fill );
buffer += next_to_fill;
bytes_to_fill -= next_to_fill;
}
return success;
}
} /* namespace shl_dsa */