Windows: RAND_file_name() should look up profile path
[heimdal.git] / lib / hcrypto / rand.c
blobd5c1f687b910d2848eda53d5dfd9b781f34acad4
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 #ifdef _WIN32
50 #include<shlobj.h>
51 #endif
53 /**
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;
62 static void
63 init_method(void)
65 if (selected_meth != NULL)
66 return;
67 #if defined(_WIN32)
68 selected_meth = &hc_rand_w32crypto_method;
69 #elif defined(__APPLE__)
70 selected_meth = &hc_rand_unix_method;
71 #else
72 selected_meth = &hc_rand_fortuna_method;
73 #endif
76 /**
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
86 void
87 RAND_seed(const void *indata, size_t size)
89 init_method();
90 (*selected_meth->seed)(indata, size);
93 /**
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)
106 if (size == 0)
107 return 1;
108 init_method();
109 return (*selected_meth->bytes)(outdata, size);
113 * Reset and free memory used by the random generator.
115 * @ingroup hcrypto_rand
118 void
119 RAND_cleanup(void)
121 const RAND_METHOD *meth = selected_meth;
122 ENGINE *engine = selected_engine;
124 selected_meth = NULL;
125 selected_engine = NULL;
127 if (meth)
128 (*meth->cleanup)();
129 if (engine)
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
145 void
146 RAND_add(const void *indata, size_t size, double entropi)
148 init_method();
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)
166 init_method();
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
179 RAND_status(void)
181 init_method();
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;
200 if (old)
201 (*old->cleanup)();
202 if (selected_engine) {
203 ENGINE_finish(selected_engine);
204 selected_engine = NULL;
206 return 1;
210 * Get the default random method.
212 * @ingroup hcrypto_rand
215 const RAND_METHOD *
216 RAND_get_rand_method(void)
218 init_method();
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;
237 if (engine) {
238 ENGINE_up_ref(engine);
239 meth = ENGINE_get_RAND(engine);
240 if (meth == NULL) {
241 ENGINE_finish(engine);
242 return 0;
244 } else {
245 meth = NULL;
248 if (old)
249 (*old->cleanup)();
251 if (selected_engine)
252 ENGINE_finish(selected_engine);
254 selected_engine = engine;
255 selected_meth = meth;
257 return 1;
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];
275 size_t len;
276 ssize_t slen;
277 int fd;
279 fd = open(filename, O_RDONLY | O_BINARY, 0600);
280 if (fd < 0)
281 return 0;
282 rk_cloexec(fd);
283 len = 0;
284 while(len < size) {
285 slen = read(fd, buf, sizeof(buf));
286 if (slen <= 0)
287 break;
288 RAND_seed(buf, slen);
289 len += slen;
291 close(fd);
293 return len ? 1 : 0;
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];
309 size_t len;
310 int res = 0, fd;
312 fd = open(filename, O_WRONLY | O_CREAT | O_BINARY, 0600);
313 if (fd < 0)
314 return 0;
315 rk_cloexec(fd);
317 len = 0;
318 while(len < RAND_FILE_SIZE) {
319 res = RAND_bytes(buf, sizeof(buf));
320 if (res != 1)
321 break;
322 if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
323 res = 0;
324 break;
326 len += sizeof(buf);
329 close(fd);
331 return res;
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
346 const char *
347 RAND_file_name(char *filename, size_t size)
349 const char *e = NULL;
350 int pathp = 0, ret;
352 if (!issuid()) {
353 e = getenv("RANDFILE");
354 if (e == NULL)
355 e = getenv("HOME");
356 if (e)
357 pathp = 1;
360 #ifndef _WIN32
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
368 if (e == NULL) {
369 int fd;
371 fd = _hc_unix_device_fd(O_RDONLY, &e);
372 if (fd >= 0)
373 close(fd);
375 #else /* Win32 */
377 if (e == NULL) {
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)
385 return filename;
389 #endif
391 if (e == NULL)
392 return NULL;
394 if (pathp)
395 ret = snprintf(filename, size, "%s/.rnd", e);
396 else
397 ret = snprintf(filename, size, "%s", e);
399 if (ret <= 0 || ret >= size)
400 return NULL;
402 return filename;