s4:heimdal: import lorikeet-heimdal-201003262338 (commit f4e0dc17709829235f057e0e100d...
[Samba/gebeck_regimport.git] / source4 / heimdal / lib / hcrypto / rand.c
blobd360ffcab4c02d8e1e902bb4ecbf1a7b74255935
1 /*
2 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
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
10 * are met:
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
33 * SUCH DAMAGE.
36 #include <config.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <rand.h>
41 #include <randi.h>
43 #include <roken.h>
45 #ifndef O_BINARY
46 #define O_BINARY 0
47 #endif
49 /**
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;
58 static void
59 init_method(void)
61 if (selected_meth != NULL)
62 return;
63 #if defined(_WIN32)
64 selected_meth = &hc_rand_w32crypto_method;
65 #elif defined(__APPLE__)
66 selected_meth = &hc_rand_unix_method;
67 #else
68 selected_meth = &hc_rand_fortuna_method;
69 #endif
72 /**
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
82 void
83 RAND_seed(const void *indata, size_t size)
85 init_method();
86 (*selected_meth->seed)(indata, size);
89 /**
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
99 int
100 RAND_bytes(void *outdata, size_t size)
102 if (size == 0)
103 return 1;
104 init_method();
105 return (*selected_meth->bytes)(outdata, size);
109 * Reset and free memory used by the random generator.
111 * @ingroup hcrypto_rand
114 void
115 RAND_cleanup(void)
117 const RAND_METHOD *meth = selected_meth;
118 ENGINE *engine = selected_engine;
120 selected_meth = NULL;
121 selected_engine = NULL;
123 if (meth)
124 (*meth->cleanup)();
125 if (engine)
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
141 void
142 RAND_add(const void *indata, size_t size, double entropi)
144 init_method();
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)
162 init_method();
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
175 RAND_status(void)
177 init_method();
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;
196 if (old)
197 (*old->cleanup)();
198 if (selected_engine) {
199 ENGINE_finish(selected_engine);
200 selected_engine = NULL;
202 return 1;
206 * Get the default random method.
208 * @ingroup hcrypto_rand
211 const RAND_METHOD *
212 RAND_get_rand_method(void)
214 init_method();
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;
233 if (engine) {
234 ENGINE_up_ref(engine);
235 meth = ENGINE_get_RAND(engine);
236 if (meth == NULL) {
237 ENGINE_finish(engine);
238 return 0;
240 } else {
241 meth = NULL;
244 if (old)
245 (*old->cleanup)();
247 if (selected_engine)
248 ENGINE_finish(selected_engine);
250 selected_engine = engine;
251 selected_meth = meth;
253 return 1;
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];
271 size_t len;
272 ssize_t slen;
273 int fd;
275 fd = open(filename, O_RDONLY | O_BINARY, 0600);
276 if (fd < 0)
277 return 0;
278 rk_cloexec(fd);
279 len = 0;
280 while(len < size) {
281 slen = read(fd, buf, sizeof(buf));
282 if (slen <= 0)
283 break;
284 RAND_seed(buf, slen);
285 len += slen;
287 close(fd);
289 return len ? 1 : 0;
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];
305 size_t len;
306 int res = 0, fd;
308 fd = open(filename, O_WRONLY | O_CREAT | O_BINARY, 0600);
309 if (fd < 0)
310 return 0;
311 rk_cloexec(fd);
313 len = 0;
314 while(len < RAND_FILE_SIZE) {
315 res = RAND_bytes(buf, sizeof(buf));
316 if (res != 1)
317 break;
318 if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
319 res = 0;
320 break;
322 len += sizeof(buf);
325 close(fd);
327 return res;
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
342 const char *
343 RAND_file_name(char *filename, size_t size)
345 char *e = NULL;
346 int pathp = 0, ret;
348 if (!issuid()) {
349 e = getenv("RANDFILE");
350 if (e == NULL)
351 e = getenv("HOME");
352 if (e)
353 pathp = 1;
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
362 #ifndef _WIN32
363 if (e == NULL) {
364 int fd;
366 fd = _hc_unix_device_fd(O_RDONLY, &e);
367 if (fd >= 0)
368 close(fd);
370 #endif
371 if (e == NULL)
372 return NULL;
374 if (pathp)
375 ret = snprintf(filename, size, "%s/.rnd", e);
376 else
377 ret = snprintf(filename, size, "%s", e);
379 if (ret <= 0 || ret >= size)
380 return NULL;
382 return filename;