blob: a0e29988c5e2594679aaa73f75fef402a5542b94 (
plain)
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
|
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <fcntl.h>
#ifndef S_SPLINT_S /* Including this here triggers a known bug in splint */
#include <unistd.h>
#endif
#define RNGDEV "/dev/urandom"
int libscrypt_salt_gen(uint8_t *salt, size_t len)
{
unsigned char buf[len];
size_t data_read = 0;
int urandom = open(RNGDEV, O_RDONLY);
if (urandom < 0)
{
return -1;
}
while (data_read < len) {
ssize_t result = read(urandom, buf + data_read, len - data_read);
if (result < 0)
{
if (errno == EINTR || errno == EAGAIN) {
continue;
}
else {
(void)close(urandom);
return -1;
}
}
data_read += result;
}
/* Failures on close() shouldn't occur with O_RDONLY */
(void)close(urandom);
memcpy(salt, buf, len);
return 0;
}
|