r23456: Update Samba4 to current lorikeet-heimdal.
[sfrench/samba-autobuild/.git] / source / heimdal / lib / hcrypto / rand.c
1 /*
2  * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
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. 
16  *
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. 
20  *
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. 
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 RCSID("$Id: rand.c 20126 2007-02-01 22:08:41Z lha $");
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <rand.h>
43 #include <randi.h>
44
45 #include <roken.h>
46
47 #ifndef O_BINARY
48 #define O_BINARY 0
49 #endif
50
51
52 const static RAND_METHOD *selected_meth = NULL;
53
54 static void
55 init_method(void)
56 {
57     if (selected_meth != NULL)
58         return;
59
60     if ((*hc_rand_unix_method.status)() == 1)
61         selected_meth = &hc_rand_unix_method;
62     else
63         selected_meth = &hc_rand_fortuna_method;
64 }
65
66 void
67 RAND_seed(const void *indata, size_t size)
68 {
69     init_method();
70     (*selected_meth->seed)(indata, size);
71 }
72
73 int
74 RAND_bytes(void *outdata, size_t size)
75 {
76     init_method();
77     return (*selected_meth->bytes)(outdata, size);
78 }
79
80 void
81 RAND_cleanup(void)
82 {
83     init_method();
84     (*selected_meth->cleanup)();
85 }
86
87 void
88 RAND_add(const void *indata, size_t size, double entropi)
89 {
90     init_method();
91     (*selected_meth->add)(indata, size, entropi);
92 }
93
94 int
95 RAND_pseudo_bytes(void *outdata, size_t size)
96 {
97     init_method();
98     return (*selected_meth->pseudorand)(outdata, size);
99 }
100
101 int
102 RAND_status(void)
103 {
104     init_method();
105     return (*selected_meth->status)();
106 }
107
108 int
109 RAND_set_rand_method(const RAND_METHOD *meth)
110 {
111     selected_meth = meth;
112     return 1;
113 }
114
115 const RAND_METHOD *
116 RAND_get_rand_method(void)
117 {
118     return selected_meth;
119 }
120
121 int
122 RAND_set_rand_engine(ENGINE *engine)
123 {
124     return 1;
125 }
126
127 #define RAND_FILE_SIZE 1024
128
129 int
130 RAND_load_file(const char *filename, size_t size)
131 {
132     unsigned char buf[128];
133     size_t len;
134     ssize_t slen;
135     int fd;
136
137     fd = open(filename, O_RDONLY | O_BINARY, 0600);
138     if (fd < 0)
139         return 0;
140
141     len = 0;
142     while(len < size) {
143         slen = read(fd, buf, sizeof(buf));
144         if (slen <= 0)
145             break;
146         RAND_seed(buf, slen);
147         len += slen;
148     }
149     close(fd);
150
151     return len ? 1 : 0;
152 }
153
154 int
155 RAND_write_file(const char *filename)
156 {
157     unsigned char buf[128];
158     size_t len;
159     int res = 0, fd;
160
161     fd = open(filename, O_WRONLY | O_CREAT | O_BINARY, 0600);
162     if (fd < 0)
163         return 0;
164
165     len = 0;
166     while(len < RAND_FILE_SIZE) {
167         res = RAND_bytes(buf, sizeof(buf));
168         if (res != 1)
169             break;
170         if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
171             res = 0;
172             break;
173         }
174         len += sizeof(buf);
175     }
176
177     close(fd);
178
179     return res;
180 }
181
182 const char *
183 RAND_file_name(char *filename, size_t size)
184 {
185     const char *e = NULL;
186     int pathp = 0, ret;
187
188     if (!issuid()) {
189         e = getenv("RANDFILE");
190         if (e == NULL) {
191             e = getenv("HOME");
192             if (e)
193                 pathp = 1;
194         }
195     }
196     if (e == NULL) {
197         struct passwd *pw = getpwuid(getuid()); 
198         if (pw) {
199             e = pw->pw_dir;
200             pathp = 1;
201         }
202     }
203     if (e == NULL)
204         return NULL;
205
206     if (pathp)
207         ret = snprintf(filename, size, "%s/.rnd", e);
208     else
209         ret = snprintf(filename, size, "%s", e);
210
211     if (ret <= 0 || ret >= size)
212         return NULL;
213
214     return filename;
215 }