No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / hcrypto / rand.c
bloba4cf2bf3d7a51cb2b115f9a6c3a6bce494c3cd84
1 /*
2 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
38 __RCSID("$Heimdal: rand.c 22199 2007-12-07 13:43:25Z lha $"
39 "$NetBSD$");
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <rand.h>
44 #include <randi.h>
46 #include <roken.h>
48 #ifndef O_BINARY
49 #define O_BINARY 0
50 #endif
53 const static RAND_METHOD *selected_meth = NULL;
55 static void
56 init_method(void)
58 if (selected_meth != NULL)
59 return;
60 selected_meth = &hc_rand_fortuna_method;
63 void
64 RAND_seed(const void *indata, size_t size)
66 init_method();
67 (*selected_meth->seed)(indata, size);
70 int
71 RAND_bytes(void *outdata, size_t size)
73 init_method();
74 return (*selected_meth->bytes)(outdata, size);
77 void
78 RAND_cleanup(void)
80 init_method();
81 (*selected_meth->cleanup)();
84 void
85 RAND_add(const void *indata, size_t size, double entropi)
87 init_method();
88 (*selected_meth->add)(indata, size, entropi);
91 int
92 RAND_pseudo_bytes(void *outdata, size_t size)
94 init_method();
95 return (*selected_meth->pseudorand)(outdata, size);
98 int
99 RAND_status(void)
101 init_method();
102 return (*selected_meth->status)();
106 RAND_set_rand_method(const RAND_METHOD *meth)
108 selected_meth = meth;
109 return 1;
112 const RAND_METHOD *
113 RAND_get_rand_method(void)
115 return selected_meth;
119 RAND_set_rand_engine(ENGINE *engine)
121 return 1;
124 #define RAND_FILE_SIZE 1024
127 RAND_load_file(const char *filename, size_t size)
129 unsigned char buf[128];
130 size_t len;
131 ssize_t slen;
132 int fd;
134 fd = open(filename, O_RDONLY | O_BINARY, 0600);
135 if (fd < 0)
136 return 0;
138 len = 0;
139 while(len < size) {
140 slen = read(fd, buf, sizeof(buf));
141 if (slen <= 0)
142 break;
143 RAND_seed(buf, slen);
144 len += slen;
146 close(fd);
148 return len ? 1 : 0;
152 RAND_write_file(const char *filename)
154 unsigned char buf[128];
155 size_t len;
156 int res = 0, fd;
158 fd = open(filename, O_WRONLY | O_CREAT | O_BINARY, 0600);
159 if (fd < 0)
160 return 0;
162 len = 0;
163 while(len < RAND_FILE_SIZE) {
164 res = RAND_bytes(buf, sizeof(buf));
165 if (res != 1)
166 break;
167 if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
168 res = 0;
169 break;
171 len += sizeof(buf);
174 close(fd);
176 return res;
179 const char *
180 RAND_file_name(char *filename, size_t size)
182 const char *e = NULL;
183 int pathp = 0, ret;
185 if (!issuid()) {
186 e = getenv("RANDFILE");
187 if (e == NULL) {
188 e = getenv("HOME");
189 if (e)
190 pathp = 1;
194 * Here we really want to call getpwuid(getuid()) but this will
195 * cause recursive lookups if the nss library uses
196 * gssapi/krb5/hcrypto to authenticate to the ldap servers.
199 if (e == NULL)
200 return NULL;
202 if (pathp)
203 ret = snprintf(filename, size, "%s/.rnd", e);
204 else
205 ret = snprintf(filename, size, "%s", e);
207 if (ret <= 0 || ret >= size)
208 return NULL;
210 return filename;