forked from ANSSI-FR/libecc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrand.c
101 lines (88 loc) · 2.6 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
/*
* Copyright (C) 2017 - This file is part of libecc project
*
* Authors:
* Ryad BENADJILA <ryadbenadjila@gmail.com>
* Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
* Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
*
* Contributors:
* Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
* Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
*
* 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.
*/
static int fimport(unsigned char *buf, u16 buflen, const char *path)
{
u16 rem = buflen, copied = 0;
ssize_t ret;
int fd;
fd = open(path, O_RDONLY);
if (fd == -1) {
printf("Unable to open input file %s\n", path);
return -1;
}
while (rem) {
ret = (int)read(fd, buf + copied, rem);
if (ret <= 0) {
break;
} else {
rem -= (u16)ret;
copied += (u16)ret;
}
}
close(fd);
return (copied == buflen) ? 0 : -1;
}
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)
{
HCRYPTPROV hCryptProv = 0;
if (CryptAcquireContext(&hCryptProv, NULL, NULL,
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) == FALSE) {
return -1;
}
if (CryptGenRandom(hCryptProv, len, buf) == FALSE) {
CryptReleaseContext(hCryptProv, 0);
return -1;
}
CryptReleaseContext(hCryptProv, 0);
return 0;
}
/* 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