-
Notifications
You must be signed in to change notification settings - Fork 3
/
csprng.cpp
91 lines (79 loc) · 2.06 KB
/
csprng.cpp
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
/*
CSPRNG - A CSPRNG and modulo a random number without bias.
Written in 2016-2018 Steve "Sc00bz" Thomas (steve at tobtu dot com)
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring
rights to this software to the public domain worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with this software.
If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#endif
//#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
#include "csprng.h"
/**
* Fills buffer with random using a CSPRNG.
*
* @param buffer - Buffer to receive the random data.
* @param size - Size of buffer.
* @return Zero on success, otherwise non-zero
*/
int getRandom(void *buffer, size_t size)
{
if (size > 0)
{
#ifdef _WIN32
static HCRYPTPROV hCryptProv = NULL;
const DWORD DWORD_MAX = (((DWORD) 1) << (8 * sizeof(DWORD) - 1)) - 1;
if (hCryptProv == NULL && !CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
{
hCryptProv = NULL;
return 1;
}
while (size > 0)
{
DWORD curSize = (DWORD) size;
if ((size_t) curSize != size || curSize < 0)
{
curSize = DWORD_MAX;
}
if (!CryptGenRandom(hCryptProv, curSize, (BYTE*) buffer))
{
return 1;
}
size -= (size_t) curSize;
buffer = ((uint8_t*) buffer) + curSize;
}
#else
int fin = open("/dev/urandom", O_RDONLY);
if (fin == -1)
{
return 1;
}
while (size > 0)
{
ssize_t curSize = (ssize_t) size;
if (size > (size_t) SSIZE_MAX)
{
curSize = SSIZE_MAX;
}
if (read(fin, buffer, curSize) != (ssize_t) curSize)
{
close(fin);
return 1;
}
size -= (size_t) curSize;
buffer = ((uint8_t*) buffer) + curSize;
}
close(fin);
#endif
}
return 0;
}