No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / hcrypto / rand-egd.c
blobce3b21695e952e6eaf10838139f141aa33c98fcf
1 /*
2 * Copyright (c) 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
38 __RCSID("$Heimdal: rand-egd.c 21156 2007-06-18 22:00:59Z lha $"
39 "$NetBSD$");
41 #include <sys/types.h>
42 #ifdef HAVE_SYS_UN_H
43 #include <sys/un.h>
44 #endif
46 #include <stdio.h>
47 #include <stdlib.h>
48 #ifdef HAVE_UNISTD_H
49 #include <unistd.h>
50 #endif
51 #include <assert.h>
53 #include <rand.h>
54 #include <randi.h>
56 #include <roken.h>
58 static const char *egd_path = "/var/run/egd-pool";
60 #define MAX_EGD_DATA 255
62 static int
63 connect_egd(const char *path)
65 struct sockaddr_un addr;
66 int fd;
68 memset(&addr, 0, sizeof(addr));
70 if (strlen(path) > sizeof(addr.sun_path))
71 return -1;
73 addr.sun_family = AF_UNIX;
74 strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
76 fd = socket(AF_UNIX, SOCK_STREAM, 0);
77 if (fd < 0)
78 return -1;
80 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
81 close(fd);
82 return -1;
85 return fd;
88 static int
89 get_entropy(int fd, void *data, size_t len)
91 unsigned char msg[2];
93 assert(len <= MAX_EGD_DATA);
95 msg[0] = 0x02; /* read blocking data */
96 msg[1] = len; /* wanted length */
98 if (net_write(fd, msg, sizeof(msg)) != sizeof(msg))
99 return 0;
101 if (net_read(fd, data, len) != len)
102 return 0;
104 return 1;
107 static int
108 put_entropy(int fd, const void *data, size_t len)
110 unsigned char msg[4];
112 assert (len <= MAX_EGD_DATA);
114 msg[0] = 0x03; /* write data */
115 msg[1] = 0; /* dummy */
116 msg[2] = 0; /* entropy */
117 msg[3] = len; /* length */
119 if (net_write(fd, msg, sizeof(msg)) != sizeof(msg))
120 return 0;
121 if (net_write(fd, data, len) != len)
122 return 0;
124 return 1;
131 static void
132 egd_seed(const void *indata, int size)
134 size_t len;
135 int fd, ret = 1;
137 fd = connect_egd(egd_path);
138 if (fd < 0)
139 return;
141 while(size) {
142 len = size;
143 if (len > MAX_EGD_DATA)
144 len = MAX_EGD_DATA;
145 ret = put_entropy(fd, indata, len);
146 if (ret != 1)
147 break;
148 indata = ((unsigned char *)indata) + len;
149 size -= len;
151 close(fd);
154 static int
155 get_bytes(const char *path, unsigned char *outdata, int size)
157 size_t len;
158 int fd, ret = 1;
160 if (path == NULL)
161 path = egd_path;
163 fd = connect_egd(path);
164 if (fd < 0)
165 return 0;
167 while(size) {
168 len = size;
169 if (len > MAX_EGD_DATA)
170 len = MAX_EGD_DATA;
171 ret = get_entropy(fd, outdata, len);
172 if (ret != 1)
173 break;
174 outdata += len;
175 size -= len;
177 close(fd);
179 return ret;
182 static int
183 egd_bytes(unsigned char *outdata, int size)
185 return get_bytes(NULL, outdata, size);
188 static void
189 egd_cleanup(void)
193 static void
194 egd_add(const void *indata, int size, double entropi)
196 egd_seed(indata, size);
199 static int
200 egd_pseudorand(unsigned char *outdata, int size)
202 return get_bytes(NULL, outdata, size);
205 static int
206 egd_status(void)
208 int fd;
209 fd = connect_egd(egd_path);
210 if (fd < 0)
211 return 0;
212 close(fd);
213 return 1;
216 const RAND_METHOD hc_rand_egd_method = {
217 egd_seed,
218 egd_bytes,
219 egd_cleanup,
220 egd_add,
221 egd_pseudorand,
222 egd_status
225 const RAND_METHOD *
226 RAND_egd_method(void)
228 return &hc_rand_egd_method;
233 RAND_egd(const char *filename)
235 return RAND_egd_bytes(filename, 128);
239 RAND_egd_bytes(const char *filename, int size)
241 void *data;
242 int ret;
244 if (size <= 0)
245 return 0;
247 data = malloc(size);
248 if (data == NULL)
249 return 0;
251 ret = get_bytes(filename, data, size);
252 if (ret != 1) {
253 free(data);
254 return ret;
257 RAND_seed(data, size);
259 memset(data, 0, size);
260 free(data);
262 return 1;