mod_entropy_add_entropy.c
2.39 KB
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
/**
* \file
*
* This adds the generated random bytes (sha1 hash of request) to the
* /dev/random
*
* \author Georg Hopp <georg@steffers.org
*
* \copyright
* Copyright © 2012 Georg Hopp
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <linux/random.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
int get_entropy_bits(const unsigned char *, size_t);
/**
* fill /dev/random with sha1 from current request
*
* \todo add error handling...
*/
void
add_entropy(const unsigned char * data, size_t ndata)
{
int i;
int fd;
int entropy = get_entropy_bits(data, ndata);
struct rand_pool_info * output;
output = (struct rand_pool_info *)malloc(
sizeof(struct rand_pool_info) + ndata);
output->entropy_count = entropy;
output->buf_size = ndata;
memcpy(output->buf, data, ndata);
fd = open("/dev/random", O_WRONLY|O_NONBLOCK);
if (ioctl(fd, RNDADDENTROPY, output) == -1) {
switch(errno) {
case EBADF:
fprintf(stderr,
"ioctl failed: no valid file descriptor %d\n",
fd);
break;
case EFAULT:
fprintf(stderr,
"ioctl failed: invalid argument: %p\n",
output);
break;
case EINVAL:
fprintf(stderr,
"ioctl failed: invalid request\n",
errno);
break;
case ENOTTY:
fprintf(stderr,
"ioctl failed: discriptor not associated to "
"character device\n",
errno);
break;
case EPERM:
fprintf(stderr,
"ioctl failed: invalid permissions\n",
errno);
break;
default:
fprintf(stderr,
"ioctl(RNDADDENTROPY) failed: %d\n",
errno);
break;
}
}
fflush(stderr);
free(output);
close(fd);
}
// vim: set ts=4 sw=4: