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
50 * @page page_rand RAND - random number
52 * See the library functions here: @ref hcrypto_rand
55 const static RAND_METHOD
*selected_meth
= NULL
;
56 static ENGINE
*selected_engine
= NULL
;
61 if (selected_meth
!= NULL
)
64 selected_meth
= &hc_rand_w32crypto_method
;
65 #elif defined(__APPLE__)
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
)
105 return (*selected_meth
->bytes
)(outdata
, size
);
109 * Reset and free memory used by the random generator.
111 * @ingroup hcrypto_rand
117 const RAND_METHOD
*meth
= selected_meth
;
118 ENGINE
*engine
= selected_engine
;
120 selected_meth
= NULL
;
121 selected_engine
= NULL
;
126 ENGINE_finish(engine
);
130 * Seed that random number generator. Secret material can securely be
131 * feed into the function, they will never be returned.
133 * @param indata the input data.
134 * @param size size of in data.
135 * @param entropi entropi in data.
138 * @ingroup hcrypto_rand
142 RAND_add(const void *indata
, size_t size
, double entropi
)
145 (*selected_meth
->add
)(indata
, size
, entropi
);
149 * Get a random block from the random generator, should NOT be used for key material.
151 * @param outdata random data
152 * @param size length random data
154 * @return 1 on success, 0 on failure.
156 * @ingroup hcrypto_rand
160 RAND_pseudo_bytes(void *outdata
, size_t size
)
163 return (*selected_meth
->pseudorand
)(outdata
, size
);
167 * Return status of the random generator
169 * @return 1 if the random generator can deliver random data.
171 * @ingroup hcrypto_rand
178 return (*selected_meth
->status
)();
182 * Set the default random method.
184 * @param meth set the new default method.
186 * @return 1 on success.
188 * @ingroup hcrypto_rand
192 RAND_set_rand_method(const RAND_METHOD
*meth
)
194 const RAND_METHOD
*old
= selected_meth
;
195 selected_meth
= meth
;
198 if (selected_engine
) {
199 ENGINE_finish(selected_engine
);
200 selected_engine
= NULL
;
206 * Get the default random method.
208 * @ingroup hcrypto_rand
212 RAND_get_rand_method(void)
215 return selected_meth
;
219 * Set the default random method from engine.
221 * @param engine use engine, if NULL is passed it, old method and engine is cleared.
223 * @return 1 on success, 0 on failure.
225 * @ingroup hcrypto_rand
229 RAND_set_rand_engine(ENGINE
*engine
)
231 const RAND_METHOD
*meth
, *old
= selected_meth
;
234 ENGINE_up_ref(engine
);
235 meth
= ENGINE_get_RAND(engine
);
237 ENGINE_finish(engine
);
248 ENGINE_finish(selected_engine
);
250 selected_engine
= engine
;
251 selected_meth
= meth
;
256 #define RAND_FILE_SIZE 1024
259 * Load a a file and feed it into RAND_seed().
261 * @param filename name of file to read.
262 * @param size minimum size to read.
264 * @ingroup hcrypto_rand
268 RAND_load_file(const char *filename
, size_t size
)
270 unsigned char buf
[128];
275 fd
= open(filename
, O_RDONLY
| O_BINARY
, 0600);
281 slen
= read(fd
, buf
, sizeof(buf
));
284 RAND_seed(buf
, slen
);
293 * Write of random numbers to a file to store for later initiation with RAND_load_file().
295 * @param filename name of file to write.
297 * @return 1 on success and non-one on failure.
298 * @ingroup hcrypto_rand
302 RAND_write_file(const char *filename
)
304 unsigned char buf
[128];
308 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_BINARY
, 0600);
314 while(len
< RAND_FILE_SIZE
) {
315 res
= RAND_bytes(buf
, sizeof(buf
));
318 if (write(fd
, buf
, sizeof(buf
)) != sizeof(buf
)) {
331 * Return the default random state filename for a user to use for
332 * RAND_load_file(), and RAND_write_file().
334 * @param filename buffer to hold file name.
335 * @param size size of buffer filename.
337 * @return the buffer filename or NULL on failure.
339 * @ingroup hcrypto_rand
343 RAND_file_name(char *filename
, size_t size
)
349 e
= getenv("RANDFILE");
356 * Here we really want to call getpwuid(getuid()) but this will
357 * cause recursive lookups if the nss library uses
358 * gssapi/krb5/hcrypto to authenticate to the ldap servers.
360 * So at least return the unix /dev/random if we have one
366 fd
= _hc_unix_device_fd(O_RDONLY
, &e
);
375 ret
= snprintf(filename
, size
, "%s/.rnd", e
);
377 ret
= snprintf(filename
, size
, "%s", e
);
379 if (ret
<= 0 || ret
>= size
)