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
54 * @page page_rand RAND - random number
56 * See the library functions here: @ref hcrypto_rand
59 const static RAND_METHOD *selected_meth = NULL;
60 static ENGINE *selected_engine = NULL;
65 if (selected_meth != NULL)
68 selected_meth = &hc_rand_w32crypto_method;
69 #elif defined(__APPLE__)
70 selected_meth = &hc_rand_unix_method;
72 selected_meth = &hc_rand_fortuna_method;
77 * Seed that random number generator. Secret material can securely be
78 * feed into the function, they will never be returned.
80 * @param indata seed data
81 * @param size length seed data
83 * @ingroup hcrypto_rand
87 RAND_seed(const void *indata, size_t size)
90 (*selected_meth->seed)(indata, size);
94 * Get a random block from the random generator, can be used for key material.
96 * @param outdata random data
97 * @param size length random data
99 * @return 1 on success, 0 on failure.
101 * @ingroup hcrypto_rand
104 RAND_bytes(void *outdata, size_t size)
109 return (*selected_meth->bytes)(outdata, size);
113 * Reset and free memory used by the random generator.
115 * @ingroup hcrypto_rand
121 const RAND_METHOD *meth = selected_meth;
122 ENGINE *engine = selected_engine;
124 selected_meth = NULL;
125 selected_engine = NULL;
130 ENGINE_finish(engine);
134 * Seed that random number generator. Secret material can securely be
135 * feed into the function, they will never be returned.
137 * @param indata the input data.
138 * @param size size of in data.
139 * @param entropi entropi in data.
142 * @ingroup hcrypto_rand
146 RAND_add(const void *indata, size_t size, double entropi)
149 (*selected_meth->add)(indata, size, entropi);
153 * Get a random block from the random generator, should NOT be used for key material.
155 * @param outdata random data
156 * @param size length random data
158 * @return 1 on success, 0 on failure.
160 * @ingroup hcrypto_rand
164 RAND_pseudo_bytes(void *outdata, size_t size)
167 return (*selected_meth->pseudorand)(outdata, size);
171 * Return status of the random generator
173 * @return 1 if the random generator can deliver random data.
175 * @ingroup hcrypto_rand
182 return (*selected_meth->status)();
186 * Set the default random method.
188 * @param meth set the new default method.
190 * @return 1 on success.
192 * @ingroup hcrypto_rand
196 RAND_set_rand_method(const RAND_METHOD *meth)
198 const RAND_METHOD *old = selected_meth;
199 selected_meth = meth;
202 if (selected_engine) {
203 ENGINE_finish(selected_engine);
204 selected_engine = NULL;
210 * Get the default random method.
212 * @ingroup hcrypto_rand
216 RAND_get_rand_method(void)
219 return selected_meth;
223 * Set the default random method from engine.
225 * @param engine use engine, if NULL is passed it, old method and engine is cleared.
227 * @return 1 on success, 0 on failure.
229 * @ingroup hcrypto_rand
233 RAND_set_rand_engine(ENGINE *engine)
235 const RAND_METHOD *meth, *old = selected_meth;
238 ENGINE_up_ref(engine);
239 meth = ENGINE_get_RAND(engine);
241 ENGINE_finish(engine);
252 ENGINE_finish(selected_engine);
254 selected_engine = engine;
255 selected_meth = meth;
260 #define RAND_FILE_SIZE 1024
263 * Load a a file and feed it into RAND_seed().
265 * @param filename name of file to read.
266 * @param size minimum size to read.
268 * @ingroup hcrypto_rand
272 RAND_load_file(const char *filename, size_t size)
274 unsigned char buf[128];
279 fd = open(filename, O_RDONLY | O_BINARY, 0600);
285 slen = read(fd, buf, sizeof(buf));
288 RAND_seed(buf, slen);
297 * Write of random numbers to a file to store for later initiation with RAND_load_file().
299 * @param filename name of file to write.
301 * @return 1 on success and non-one on failure.
302 * @ingroup hcrypto_rand
306 RAND_write_file(const char *filename)
308 unsigned char buf[128];
312 fd = open(filename, O_WRONLY | O_CREAT | O_BINARY, 0600);
318 while(len < RAND_FILE_SIZE) {
319 res = RAND_bytes(buf, sizeof(buf));
322 if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
335 * Return the default random state filename for a user to use for
336 * RAND_load_file(), and RAND_write_file().
338 * @param filename buffer to hold file name.
339 * @param size size of buffer filename.
341 * @return the buffer filename or NULL on failure.
343 * @ingroup hcrypto_rand
347 RAND_file_name(char *filename, size_t size)
349 const char *e = NULL;
353 e = getenv("RANDFILE");
362 * Here we really want to call getpwuid(getuid()) but this will
363 * cause recursive lookups if the nss library uses
364 * gssapi/krb5/hcrypto to authenticate to the ldap servers.
366 * So at least return the unix /dev/random if we have one
371 fd = _hc_unix_device_fd(O_RDONLY, &e);
378 char profile[MAX_PATH];
380 if (SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL,
381 SHGFP_TYPE_CURRENT, profile) == S_OK) {
382 ret = snprintf(filename, size, "%s\\.rnd", profile);
384 if (ret > 0 && ret < size)
395 ret = snprintf(filename, size, "%s/.rnd", e);
397 ret = snprintf(filename, size, "%s", e);
399 if (ret <= 0 || ret >= size)