source: branches/samba-3.0/source/lib/genrand.c@ 125

Last change on this file since 125 was 1, checked in by Paul Smedley, 18 years ago

Initial code import

File size: 6.0 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 Functions to create reasonable random numbers for crypto use.
5
6 Copyright (C) Jeremy Allison 2001
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23#include "includes.h"
24
25static unsigned char smb_arc4_state[258];
26static uint32 counter;
27
28static BOOL done_reseed = False;
29static void (*reseed_callback)(int *newseed);
30
31
32/****************************************************************
33 Copy any user given reseed data.
34*****************************************************************/
35
36void set_rand_reseed_callback(void (*fn)(int *))
37{
38 reseed_callback = fn;
39 set_need_random_reseed();
40}
41
42void set_need_random_reseed(void)
43{
44 done_reseed = False;
45}
46
47static void get_rand_reseed_data(int *reseed_data)
48{
49 if (reseed_callback) {
50 reseed_callback(reseed_data);
51 } else {
52 *reseed_data = 0;
53 }
54}
55
56/****************************************************************
57 Get a 16 byte hash from the contents of a file.
58 Note that the hash is not initialised.
59*****************************************************************/
60
61static void do_filehash(const char *fname, unsigned char *the_hash)
62{
63 unsigned char buf[1011]; /* deliberate weird size */
64 unsigned char tmp_md4[16];
65 int fd, n;
66
67 fd = sys_open(fname,O_RDONLY,0);
68 if (fd == -1)
69 return;
70
71 while ((n = read(fd, (char *)buf, sizeof(buf))) > 0) {
72 mdfour(tmp_md4, buf, n);
73 for (n=0;n<16;n++)
74 the_hash[n] ^= tmp_md4[n];
75 }
76 close(fd);
77}
78
79/**************************************************************
80 Try and get a good random number seed. Try a number of
81 different factors. Firstly, try /dev/urandom - use if exists.
82
83 We use /dev/urandom as a read of /dev/random can block if
84 the entropy pool dries up. This leads clients to timeout
85 or be very slow on connect.
86
87 If we can't use /dev/urandom then seed the stream random generator
88 above...
89**************************************************************/
90
91static int do_reseed(BOOL use_fd, int fd)
92{
93 unsigned char seed_inbuf[40];
94 uint32 v1, v2; struct timeval tval; pid_t mypid;
95 struct passwd *pw;
96 int reseed_data = 0;
97
98 if (use_fd) {
99 if (fd != -1)
100 return fd;
101
102 fd = sys_open( "/dev/urandom", O_RDONLY,0);
103 if(fd >= 0)
104 return fd;
105 }
106
107 /* Add in some secret file contents */
108
109 do_filehash("/etc/shadow", &seed_inbuf[0]);
110 do_filehash(lp_smb_passwd_file(), &seed_inbuf[16]);
111
112 /*
113 * Add in the root encrypted password.
114 * On any system where security is taken
115 * seriously this will be secret.
116 */
117
118 pw = getpwnam_alloc(NULL, "root");
119 if (pw && pw->pw_passwd) {
120 size_t i;
121 unsigned char md4_tmp[16];
122 mdfour(md4_tmp, (unsigned char *)pw->pw_passwd, strlen(pw->pw_passwd));
123 for (i=0;i<16;i++)
124 seed_inbuf[8+i] ^= md4_tmp[i];
125 TALLOC_FREE(pw);
126 }
127
128 /*
129 * Add the counter, time of day, and pid.
130 */
131
132 GetTimeOfDay(&tval);
133 mypid = sys_getpid();
134 v1 = (counter++) + mypid + tval.tv_sec;
135 v2 = (counter++) * mypid + tval.tv_usec;
136
137 SIVAL(seed_inbuf, 32, v1 ^ IVAL(seed_inbuf, 32));
138 SIVAL(seed_inbuf, 36, v2 ^ IVAL(seed_inbuf, 36));
139
140 /*
141 * Add any user-given reseed data.
142 */
143
144 get_rand_reseed_data(&reseed_data);
145 if (reseed_data) {
146 size_t i;
147 for (i = 0; i < sizeof(seed_inbuf); i++)
148 seed_inbuf[i] ^= ((char *)(&reseed_data))[i % sizeof(reseed_data)];
149 }
150
151 smb_arc4_init(smb_arc4_state, seed_inbuf, sizeof(seed_inbuf));
152
153 return -1;
154}
155
156/*******************************************************************
157 Interface to the (hopefully) good crypto random number generator.
158********************************************************************/
159
160void generate_random_buffer( unsigned char *out, int len)
161{
162 static int urand_fd = -1;
163 unsigned char md4_buf[64];
164 unsigned char tmp_buf[16];
165 unsigned char *p;
166
167#ifndef __OS2__
168 if(!done_reseed) {
169 urand_fd = do_reseed(True, urand_fd);
170 done_reseed = True;
171 }
172
173 if (urand_fd != -1 && len > 0) {
174
175 if (read(urand_fd, out, len) == len)
176 return; /* len bytes of random data read from urandom. */
177
178 /* Read of urand error, drop back to non urand method. */
179 close(urand_fd);
180 urand_fd = -1;
181 do_reseed(False, -1);
182 done_reseed = True;
183 }
184#endif /* __OS2__ */
185 /*
186 * Generate random numbers in chunks of 64 bytes,
187 * then md4 them & copy to the output buffer.
188 * This way the raw state of the stream is never externally
189 * seen.
190 */
191
192 p = out;
193 while(len > 0) {
194 int copy_len = len > 16 ? 16 : len;
195
196#ifdef __OS2__
197 os2_randget(md4_buf, sizeof(md4_buf));
198#endif
199 smb_arc4_crypt(smb_arc4_state, md4_buf, sizeof(md4_buf));
200 mdfour(tmp_buf, md4_buf, sizeof(md4_buf));
201 memcpy(p, tmp_buf, copy_len);
202 p += copy_len;
203 len -= copy_len;
204 }
205}
206
207/*******************************************************************
208 Use the random number generator to generate a random string.
209********************************************************************/
210
211static char c_list[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_-#.,";
212
213char *generate_random_str(size_t len)
214{
215 static unsigned char retstr[256];
216 size_t i;
217
218 memset(retstr, '\0', sizeof(retstr));
219
220 if (len > sizeof(retstr)-1)
221 len = sizeof(retstr) -1;
222 generate_random_buffer( retstr, len);
223 for (i = 0; i < len; i++)
224 retstr[i] = c_list[ retstr[i] % (sizeof(c_list)-1) ];
225
226 retstr[i] = '\0';
227
228 return (char *)retstr;
229}
Note: See TracBrowser for help on using the repository browser.