2 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
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
51 * @page page_rand RAND - random number
53 * See the library functions here: @ref hcrypto_rand
56 static const RAND_METHOD *selected_meth = NULL;
57 static ENGINE *selected_engine = NULL;
62 if (selected_meth != NULL)
65 selected_meth = &hc_rand_w32crypto_method;
66 #elif defined(__APPLE__)
67 selected_meth = &hc_rand_unix_method;
69 selected_meth = &hc_rand_fortuna_method;
74 * Seed that random number generator. Secret material can securely be
75 * feed into the function, they will never be returned.
77 * @param indata seed data
78 * @param size length seed data
80 * @ingroup hcrypto_rand
84 RAND_seed(const void *indata, size_t size)
87 (*selected_meth->seed)(indata, size);
91 * Get a random block from the random generator, can be used for key material.
93 * @param outdata random data
94 * @param size length random data
96 * @return 1 on success, 0 on failure.
98 * @ingroup hcrypto_rand
101 RAND_bytes(void *outdata, size_t size)
106 return (*selected_meth->bytes)(outdata, size);
110 * Reset and free memory used by the random generator.
112 * @ingroup hcrypto_rand
118 const RAND_METHOD *meth = selected_meth;
119 ENGINE *engine = selected_engine;
121 selected_meth = NULL;
122 selected_engine = NULL;
127 ENGINE_finish(engine);
131 * Seed that random number generator. Secret material can securely be
132 * feed into the function, they will never be returned.
134 * @param indata the input data.
135 * @param size size of in data.
136 * @param entropi entropi in data.
139 * @ingroup hcrypto_rand
143 RAND_add(const void *indata, size_t size, double entropi)
146 (*selected_meth->add)(indata, size, entropi);
150 * Get a random block from the random generator, should NOT be used for key material.
152 * @param outdata random data
153 * @param size length random data
155 * @return 1 on success, 0 on failure.
157 * @ingroup hcrypto_rand
161 RAND_pseudo_bytes(void *outdata, size_t size)
164 return (*selected_meth->pseudorand)(outdata, size);
168 * Return status of the random generator
170 * @return 1 if the random generator can deliver random data.
172 * @ingroup hcrypto_rand
179 return (*selected_meth->status)();
183 * Set the default random method.
185 * @param meth set the new default method.
187 * @return 1 on success.
189 * @ingroup hcrypto_rand
193 RAND_set_rand_method(const RAND_METHOD *meth)
195 const RAND_METHOD *old = selected_meth;
196 selected_meth = meth;
199 if (selected_engine) {
200 ENGINE_finish(selected_engine);
201 selected_engine = NULL;
207 * Get the default random method.
209 * @return Returns a RAND_METHOD
211 * @ingroup hcrypto_rand
215 RAND_get_rand_method(void)
218 return selected_meth;
222 * Set the default random method from engine.
224 * @param engine use engine, if NULL is passed it, old method and engine is cleared.
226 * @return 1 on success, 0 on failure.
228 * @ingroup hcrypto_rand
232 RAND_set_rand_engine(ENGINE *engine)
234 const RAND_METHOD *meth, *old = selected_meth;
237 ENGINE_up_ref(engine);
238 meth = ENGINE_get_RAND(engine);
240 ENGINE_finish(engine);
251 ENGINE_finish(selected_engine);
253 selected_engine = engine;
254 selected_meth = meth;
259 #define RAND_FILE_SIZE 1024
262 * Load a a file and feed it into RAND_seed().
264 * @param filename name of file to read.
265 * @param size minimum size to read.
267 * @return Returns the number of seed bytes loaded (0 indicates failure)
269 * @ingroup hcrypto_rand
273 RAND_load_file(const char *filename, size_t size)
275 unsigned char buf[128];
280 fd = open(filename, O_RDONLY | O_BINARY, 0600);
286 slen = read(fd, buf, sizeof(buf));
289 RAND_seed(buf, slen);
298 * Write of random numbers to a file to store for later initiation with RAND_load_file().
300 * @param filename name of file to write.
302 * @return 1 on success and non-one on failure.
303 * @ingroup hcrypto_rand
307 RAND_write_file(const char *filename)
309 unsigned char buf[128];
313 fd = open(filename, O_WRONLY | O_CREAT | O_BINARY, 0600);
319 while(len < RAND_FILE_SIZE) {
320 res = RAND_bytes(buf, sizeof(buf));
323 if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
336 * Return the default random state filename for a user to use for
337 * RAND_load_file(), and RAND_write_file().
339 * @param filename buffer to hold file name.
340 * @param size size of buffer filename.
342 * @return the buffer filename or NULL on failure.
344 * @ingroup hcrypto_rand
348 RAND_file_name(char *filename, size_t size)
350 const char *e = NULL;
353 e = secure_getenv("RANDFILE");
355 e = secure_getenv("HOME");
361 * Here we really want to call getpwuid(getuid()) but this will
362 * cause recursive lookups if the nss library uses
363 * gssapi/krb5/hcrypto to authenticate to the ldap servers.
365 * So at least return the unix /dev/random if we have one
370 fd = _hc_unix_device_fd(O_RDONLY, &e);
377 char profile[MAX_PATH];
379 if (SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL,
380 SHGFP_TYPE_CURRENT, profile) == S_OK) {
381 ret = snprintf(filename, size, "%s\\.rnd", profile);
383 if (ret > 0 && ret < size)
394 ret = snprintf(filename, size, "%s/.rnd", e);
396 ret = snprintf(filename, size, "%s", e);
398 if (ret <= 0 || ret >= size)