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
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)
66 selected_meth = &hc_rand_unix_method;
68 selected_meth = &hc_rand_fortuna_method;
73 * Seed that random number generator. Secret material can securely be
74 * feed into the function, they will never be returned.
76 * @param indata seed data
77 * @param size length seed data
79 * @ingroup hcrypto_rand
83 RAND_seed(const void *indata, size_t size)
86 (*selected_meth->seed)(indata, size);
90 * Get a random block from the random generator, can be used for key material.
92 * @param outdata random data
93 * @param size length random data
95 * @return 1 on success, 0 on failure.
97 * @ingroup hcrypto_rand
100 RAND_bytes(void *outdata, size_t size)
103 return (*selected_meth->bytes)(outdata, size);
107 * Reset and free memory used by the random generator.
109 * @ingroup hcrypto_rand
115 const RAND_METHOD *meth = selected_meth;
116 ENGINE *engine = selected_engine;
118 selected_meth = NULL;
119 selected_engine = NULL;
124 ENGINE_finish(engine);
128 * Seed that random number generator. Secret material can securely be
129 * feed into the function, they will never be returned.
131 * @param indata the input data.
132 * @param size size of in data.
133 * @param entropi entropi in data.
136 * @ingroup hcrypto_rand
140 RAND_add(const void *indata, size_t size, double entropi)
143 (*selected_meth->add)(indata, size, entropi);
147 * Get a random block from the random generator, should NOT be used for key material.
149 * @param outdata random data
150 * @param size length random data
152 * @return 1 on success, 0 on failure.
154 * @ingroup hcrypto_rand
158 RAND_pseudo_bytes(void *outdata, size_t size)
161 return (*selected_meth->pseudorand)(outdata, size);
165 * Return status of the random generator
167 * @return 1 if the random generator can deliver random data.
169 * @ingroup hcrypto_rand
176 return (*selected_meth->status)();
180 * Set the default random method.
182 * @param meth set the new default method.
184 * @return 1 on success.
186 * @ingroup hcrypto_rand
190 RAND_set_rand_method(const RAND_METHOD *meth)
192 const RAND_METHOD *old = selected_meth;
193 selected_meth = meth;
196 if (selected_engine) {
197 ENGINE_finish(selected_engine);
198 selected_engine = NULL;
204 * Get the default random method.
206 * @ingroup hcrypto_rand
210 RAND_get_rand_method(void)
213 return selected_meth;
217 * Set the default random method from engine.
219 * @param engine use engine, if NULL is passed it, old method and engine is cleared.
221 * @return 1 on success, 0 on failure.
223 * @ingroup hcrypto_rand
227 RAND_set_rand_engine(ENGINE *engine)
229 const RAND_METHOD *meth, *old = selected_meth;
232 ENGINE_up_ref(engine);
233 meth = ENGINE_get_RAND(engine);
235 ENGINE_finish(engine);
246 ENGINE_finish(selected_engine);
248 selected_engine = engine;
249 selected_meth = meth;
254 #define RAND_FILE_SIZE 1024
257 * Load a a file and feed it into RAND_seed().
259 * @param filename name of file to read.
260 * @param size minimum size to read.
262 * @ingroup hcrypto_rand
266 RAND_load_file(const char *filename, size_t size)
268 unsigned char buf[128];
273 fd = open(filename, O_RDONLY | O_BINARY, 0600);
279 slen = read(fd, buf, sizeof(buf));
282 RAND_seed(buf, slen);
291 * Write of random numbers to a file to store for later initiation with RAND_load_file().
293 * @param filename name of file to write.
295 * @return 1 on success and non-one on failure.
296 * @ingroup hcrypto_rand
300 RAND_write_file(const char *filename)
302 unsigned char buf[128];
306 fd = open(filename, O_WRONLY | O_CREAT | O_BINARY, 0600);
312 while(len < RAND_FILE_SIZE) {
313 res = RAND_bytes(buf, sizeof(buf));
316 if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
329 * Return the default random state filename for a user to use for
330 * RAND_load_file(), and RAND_write_file().
332 * @param filename buffer to hold file name.
333 * @param size size of buffer filename.
335 * @return the buffer filename or NULL on failure.
337 * @ingroup hcrypto_rand
341 RAND_file_name(char *filename, size_t size)
343 const char *e = NULL;
347 e = getenv("RANDFILE");
355 * Here we really want to call getpwuid(getuid()) but this will
356 * cause recursive lookups if the nss library uses
357 * gssapi/krb5/hcrypto to authenticate to the ldap servers.
364 ret = snprintf(filename, size, "%s/.rnd", e);
366 ret = snprintf(filename, size, "%s", e);
368 if (ret <= 0 || ret >= size)