This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathrand.c
161 lines (139 loc) · 3.54 KB
/
rand.c
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
/*
* Copyright (C) 2017 - This file is part of libecc project
*
* Authors:
* Ryad BENADJILA <[email protected]>
* Arnaud EBALARD <[email protected]>
* Jean-Pierre FLORI <[email protected]>
*
* Contributors:
* Nicolas VIVET <[email protected]>
* Karim KHALFALLAH <[email protected]>
*
* This software is licensed under a dual BSD and GPL v2 license.
* See LICENSE file at the root folder of the project.
*/
#include "rand.h"
/* Unix and compatible case (including macOS) */
#if defined(WITH_STDLIB) && (defined(__unix__) || defined(__APPLE__))
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "../words/words.h"
/*
* Copy file content to buffer. Return 0 on success, i.e. if the request
* size has been read and copied to buffer and -1 otherwise.
*/
ATTRIBUTE_WARN_UNUSED_RET static int fimport(unsigned char *buf, u16 buflen,
const char *path)
{
u16 rem = buflen, copied = 0;
ssize_t ret;
int fd;
if ((buf == NULL) || (path == NULL)) {
ret = -1;
goto err;
}
fd = open(path, O_RDONLY);
if (fd == -1) {
printf("Unable to open input file %s\n", path);
ret = -1;
goto err;
}
while (rem) {
ret = (int)read(fd, buf + copied, rem);
if (ret <= 0) {
break;
} else {
rem = (u16)(rem - ret);
copied = (u16)(copied + ret);
}
}
if (close(fd)) {
printf("Unable to close input file %s\n", path);
ret = -1;
goto err;
}
ret = (copied == buflen) ? 0 : -1;
err:
return (int)ret;
}
int get_random(unsigned char *buf, u16 len)
{
return fimport(buf, len, "/dev/urandom");
}
/* Windows case */
#elif defined(WITH_STDLIB) && defined(__WIN32__)
#include <windows.h>
#include <wincrypt.h>
int get_random(unsigned char *buf, u16 len)
{
int ret;
HCRYPTPROV hCryptProv = 0;
if (CryptAcquireContext(&hCryptProv, NULL, NULL,
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) == FALSE) {
ret = -1;
goto err;
}
if (CryptGenRandom(hCryptProv, len, buf) == FALSE) {
CryptReleaseContext(hCryptProv, 0);
ret = -1;
goto err;
}
CryptReleaseContext(hCryptProv, 0);
ret = 0;
err:
return ret;
}
/* No platform detected, the user must provide an implementation! */
#else
/* WARNING: when providing/implementing the get_random function, one must:
* - Use a proper entropy source with a TRNG (True Random Number Generator)
* basis and clean PRNG (Pseudo-Random Number Generator) post-processing
* when needed.
* - Use a non-leaking generator in contexts where attackers that have access
* to side channels are a plausible threat (a process in an OS sharing memory
* and caches with other possibly malicious processes, a microcontroller
* that can be observed using EM probes or power consumtion, ...).
*/
#error "rand.c: you have to implement get_random with a proper entropy source!"
#endif
/* Unsafe random source:
* Initial seeding is performed using good entropy, then
* a congruential linear system is used.
*/
static u64 seed = 0;
int get_unsafe_random(unsigned char *buf, u16 len)
{
int ret;
u64 a, b;
u16 i, j;
a = (u64)2862933555777941757;
b = (u64)3037000493;
if(seed == 0){
ret = get_random((u8*)&seed, sizeof(seed));
if(ret){
ret = -1;
goto err;
}
}
i = 0;
while(i < len){
/* Use a congruential linear generator */
seed = ((a * seed) + b);
for(j = 0; j < sizeof(seed); j++){
if((i + j) < len){
buf[i + j] = (u8)((seed >> (j * 8)) & 0xff);
}
}
i = (u16)(i + sizeof(seed));
}
ret = 0;
err:
return ret;
}