47a6b94c |
1 | /* |
2 | * uxgen.c: Unix implementation of get_heavy_noise() from cmdgen.c. |
3 | */ |
4 | |
5 | #include <stdio.h> |
6 | #include <fcntl.h> |
7 | #include <unistd.h> |
8 | |
9 | #include "putty.h" |
10 | |
11 | char *get_random_data(int len) |
12 | { |
13 | char *buf = snewn(len, char); |
14 | int fd; |
15 | int ngot, ret; |
16 | |
17 | fd = open("/dev/random", O_RDONLY); |
18 | if (fd < 0) { |
19 | sfree(buf); |
20 | perror("puttygen: unable to open /dev/random"); |
21 | return NULL; |
22 | } |
23 | |
24 | ngot = 0; |
25 | while (ngot < len) { |
26 | ret = read(fd, buf+ngot, len-ngot); |
27 | if (ret < 0) { |
28 | close(fd); |
29 | perror("puttygen: unable to read /dev/random"); |
30 | return NULL; |
31 | } |
32 | ngot += ret; |
33 | } |
34 | |
3b2ffe3b |
35 | close(fd); |
36 | |
47a6b94c |
37 | return buf; |
38 | } |