source: branches/samba-3.3.x/source/lib/genrand.c@ 206

Last change on this file since 206 was 206, checked in by Herwig Bauernfeind, 16 years ago

Import Samba 3.3 branch at 3.0.0 level (psmedley's port)

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