2 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
38 RCSID("$Id: rand.c 20126 2007-02-01 22:08:41Z lha $");
52 const static RAND_METHOD *selected_meth = NULL;
57 if (selected_meth != NULL)
60 if ((*hc_rand_unix_method.status)() == 1)
61 selected_meth = &hc_rand_unix_method;
63 selected_meth = &hc_rand_fortuna_method;
67 RAND_seed(const void *indata, size_t size)
70 (*selected_meth->seed)(indata, size);
74 RAND_bytes(void *outdata, size_t size)
77 return (*selected_meth->bytes)(outdata, size);
84 (*selected_meth->cleanup)();
88 RAND_add(const void *indata, size_t size, double entropi)
91 (*selected_meth->add)(indata, size, entropi);
95 RAND_pseudo_bytes(void *outdata, size_t size)
98 return (*selected_meth->pseudorand)(outdata, size);
105 return (*selected_meth->status)();
109 RAND_set_rand_method(const RAND_METHOD *meth)
111 selected_meth = meth;
116 RAND_get_rand_method(void)
118 return selected_meth;
122 RAND_set_rand_engine(ENGINE *engine)
127 #define RAND_FILE_SIZE 1024
130 RAND_load_file(const char *filename, size_t size)
132 unsigned char buf[128];
137 fd = open(filename, O_RDONLY | O_BINARY, 0600);
143 slen = read(fd, buf, sizeof(buf));
146 RAND_seed(buf, slen);
155 RAND_write_file(const char *filename)
157 unsigned char buf[128];
161 fd = open(filename, O_WRONLY | O_CREAT | O_BINARY, 0600);
166 while(len < RAND_FILE_SIZE) {
167 res = RAND_bytes(buf, sizeof(buf));
170 if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
183 RAND_file_name(char *filename, size_t size)
185 const char *e = NULL;
189 e = getenv("RANDFILE");
197 struct passwd *pw = getpwuid(getuid());
207 ret = snprintf(filename, size, "%s/.rnd", e);
209 ret = snprintf(filename, size, "%s", e);
211 if (ret <= 0 || ret >= size)