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 23464 2008-07-27 12:15:21Z lha $");
52 * @page page_rand RAND - random number
54 * See the library functions here: @ref hcrypto_rand
57 const static RAND_METHOD *selected_meth = NULL;
58 static ENGINE *selected_engine = NULL;
63 if (selected_meth != NULL)
65 selected_meth = &hc_rand_fortuna_method;
69 * Seed that random number generator. Secret material can securely be
70 * feed into the function, they will never be returned.
72 * @param indata seed data
73 * @param size length seed data
75 * @ingroup hcrypto_rand
79 RAND_seed(const void *indata, size_t size)
82 (*selected_meth->seed)(indata, size);
86 * Get a random block from the random generator, can be used for key material.
88 * @param outdata random data
89 * @param size length random data
91 * @return 1 on success, 0 on failure.
93 * @ingroup hcrypto_rand
96 RAND_bytes(void *outdata, size_t size)
99 return (*selected_meth->bytes)(outdata, size);
103 * Reset and free memory used by the random generator.
105 * @ingroup hcrypto_rand
111 const RAND_METHOD *meth = selected_meth;
112 ENGINE *engine = selected_engine;
114 selected_meth = NULL;
115 selected_engine = NULL;
120 ENGINE_finish(engine);
124 * Seed that random number generator. Secret material can securely be
125 * feed into the function, they will never be returned.
127 * @param indata the input data.
128 * @param size size of in data.
129 * @param entropi entropi in data.
132 * @ingroup hcrypto_rand
136 RAND_add(const void *indata, size_t size, double entropi)
139 (*selected_meth->add)(indata, size, entropi);
143 * Get a random block from the random generator, should NOT be used for key material.
145 * @param outdata random data
146 * @param size length random data
148 * @return 1 on success, 0 on failure.
150 * @ingroup hcrypto_rand
154 RAND_pseudo_bytes(void *outdata, size_t size)
157 return (*selected_meth->pseudorand)(outdata, size);
161 * Return status of the random generator
163 * @return 1 if the random generator can deliver random data.
165 * @ingroup hcrypto_rand
172 return (*selected_meth->status)();
176 * Set the default random method.
178 * @param meth set the new default method.
180 * @return 1 on success.
182 * @ingroup hcrypto_rand
186 RAND_set_rand_method(const RAND_METHOD *meth)
188 const RAND_METHOD *old = selected_meth;
189 selected_meth = meth;
192 if (selected_engine) {
193 ENGINE_finish(selected_engine);
194 selected_engine = NULL;
200 * Get the default random method.
202 * @ingroup hcrypto_rand
206 RAND_get_rand_method(void)
209 return selected_meth;
213 * Set the default random method from engine.
215 * @param engine use engine, if NULL is passed it, old method and engine is cleared.
217 * @return 1 on success, 0 on failure.
219 * @ingroup hcrypto_rand
223 RAND_set_rand_engine(ENGINE *engine)
225 const RAND_METHOD *meth, *old = selected_meth;
228 ENGINE_up_ref(engine);
229 meth = ENGINE_get_RAND(engine);
231 ENGINE_finish(engine);
242 ENGINE_finish(selected_engine);
244 selected_engine = engine;
245 selected_meth = meth;
250 #define RAND_FILE_SIZE 1024
253 * Load a a file and feed it into RAND_seed().
255 * @param filename name of file to read.
256 * @param size minimum size to read.
258 * @ingroup hcrypto_rand
262 RAND_load_file(const char *filename, size_t size)
264 unsigned char buf[128];
269 fd = open(filename, O_RDONLY | O_BINARY, 0600);
275 slen = read(fd, buf, sizeof(buf));
278 RAND_seed(buf, slen);
287 * Write of random numbers to a file to store for later initiation with RAND_load_file().
289 * @param filename name of file to write.
291 * @return 1 on success and non-one on failure.
292 * @ingroup hcrypto_rand
296 RAND_write_file(const char *filename)
298 unsigned char buf[128];
302 fd = open(filename, O_WRONLY | O_CREAT | O_BINARY, 0600);
308 while(len < RAND_FILE_SIZE) {
309 res = RAND_bytes(buf, sizeof(buf));
312 if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
325 * Return the default random state filename for a user to use for
326 * RAND_load_file(), and RAND_write_file().
328 * @param filename buffer to hold file name.
329 * @param size size of buffer filename.
331 * @return the buffer filename or NULL on failure.
333 * @ingroup hcrypto_rand
337 RAND_file_name(char *filename, size_t size)
339 const char *e = NULL;
343 e = getenv("RANDFILE");
351 * Here we really want to call getpwuid(getuid()) but this will
352 * cause recursive lookups if the nss library uses
353 * gssapi/krb5/hcrypto to authenticate to the ldap servers.
360 ret = snprintf(filename, size, "%s/.rnd", e);
362 ret = snprintf(filename, size, "%s", e);
364 if (ret <= 0 || ret >= size)