1 /* $NetBSD: rand-egd.c,v 1.1.1.2 2014/04/24 12:45:30 pettai Exp $ */
4 * Copyright (c) 2007 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/types.h>
53 #include <krb5/roken.h>
55 static const char *egd_path
= "/var/run/egd-pool";
57 #define MAX_EGD_DATA 255
60 connect_egd(const char *path
)
62 struct sockaddr_un addr
;
65 memset(&addr
, 0, sizeof(addr
));
67 if (strlen(path
) > sizeof(addr
.sun_path
))
70 addr
.sun_family
= AF_UNIX
;
71 strlcpy(addr
.sun_path
, path
, sizeof(addr
.sun_path
));
73 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
79 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) != 0) {
88 get_entropy(int fd
, void *data
, size_t len
)
92 assert(len
<= MAX_EGD_DATA
);
94 msg
[0] = 0x02; /* read blocking data */
95 msg
[1] = len
; /* wanted length */
97 if (net_write(fd
, msg
, sizeof(msg
)) != sizeof(msg
))
100 if (net_read(fd
, data
, len
) != len
)
107 put_entropy(int fd
, const void *data
, size_t len
)
109 unsigned char msg
[4];
111 assert (len
<= MAX_EGD_DATA
);
113 msg
[0] = 0x03; /* write data */
114 msg
[1] = 0; /* dummy */
115 msg
[2] = 0; /* entropy */
116 msg
[3] = len
; /* length */
118 if (net_write(fd
, msg
, sizeof(msg
)) != sizeof(msg
))
120 if (net_write(fd
, data
, len
) != len
)
131 egd_seed(const void *indata
, int size
)
136 fd
= connect_egd(egd_path
);
142 if (len
> MAX_EGD_DATA
)
144 ret
= put_entropy(fd
, indata
, len
);
147 indata
= ((unsigned char *)indata
) + len
;
154 get_bytes(const char *path
, unsigned char *outdata
, int size
)
162 fd
= connect_egd(path
);
168 if (len
> MAX_EGD_DATA
)
170 ret
= get_entropy(fd
, outdata
, len
);
182 egd_bytes(unsigned char *outdata
, int size
)
184 return get_bytes(NULL
, outdata
, size
);
193 egd_add(const void *indata
, int size
, double entropi
)
195 egd_seed(indata
, size
);
199 egd_pseudorand(unsigned char *outdata
, int size
)
201 return get_bytes(NULL
, outdata
, size
);
208 fd
= connect_egd(egd_path
);
215 const RAND_METHOD hc_rand_egd_method
= {
225 RAND_egd_method(void)
227 return &hc_rand_egd_method
;
232 RAND_egd(const char *filename
)
234 return RAND_egd_bytes(filename
, 128);
238 RAND_egd_bytes(const char *filename
, int size
)
250 ret
= get_bytes(filename
, data
, size
);
256 RAND_seed(data
, size
);
258 memset(data
, 0, size
);