2 Unix SMB/CIFS implementation.
4 Functions to create reasonable random numbers for crypto use.
6 Copyright (C) Jeremy Allison 2001
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.
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.
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.
25 static unsigned char hash[258];
26 static uint32 counter;
28 static BOOL done_reseed = False;
29 static void (*reseed_callback)(int *newseed);
31 /****************************************************************
32 Copy any user given reseed data.
33 *****************************************************************/
35 void set_rand_reseed_callback(void (*fn)(int *))
38 set_need_random_reseed();
41 void set_need_random_reseed(void)
46 static void get_rand_reseed_data(int *reseed_data)
48 if (reseed_callback) {
49 reseed_callback(reseed_data);
55 /****************************************************************
57 *****************************************************************/
59 static void seed_random_stream(unsigned char *seedval, size_t seedlen)
64 for (ind = 0; ind < 256; ind++)
65 hash[ind] = (unsigned char)ind;
67 for( ind = 0; ind < 256; ind++) {
70 j += (hash[ind] + seedval[ind%seedlen]);
81 /****************************************************************
82 Get datasize bytes worth of random data.
83 *****************************************************************/
85 static void get_random_stream(unsigned char *data, size_t datasize)
87 unsigned char index_i = hash[256];
88 unsigned char index_j = hash[257];
91 for( ind = 0; ind < datasize; ind++) {
96 index_j += hash[index_i];
99 hash[index_i] = hash[index_j];
102 t = hash[index_i] + hash[index_j];
110 /****************************************************************
111 Get a 16 byte hash from the contents of a file.
112 Note that the hash is not initialised.
113 *****************************************************************/
115 static void do_filehash(const char *fname, unsigned char *the_hash)
117 unsigned char buf[1011]; /* deliberate weird size */
118 unsigned char tmp_md4[16];
121 fd = sys_open(fname,O_RDONLY,0);
125 while ((n = read(fd, (char *)buf, sizeof(buf))) > 0) {
126 mdfour(tmp_md4, buf, n);
128 the_hash[n] ^= tmp_md4[n];
133 /**************************************************************
134 Try and get a good random number seed. Try a number of
135 different factors. Firstly, try /dev/urandom - use if exists.
137 We use /dev/urandom as a read of /dev/random can block if
138 the entropy pool dries up. This leads clients to timeout
139 or be very slow on connect.
141 If we can't use /dev/urandom then seed the stream random generator
143 **************************************************************/
145 static int do_reseed(BOOL use_fd, int fd)
147 unsigned char seed_inbuf[40];
148 uint32 v1, v2; struct timeval tval; pid_t mypid;
156 fd = sys_open( "/dev/urandom", O_RDONLY,0);
161 /* Add in some secret file contents */
163 do_filehash("/etc/shadow", &seed_inbuf[0]);
164 do_filehash(lp_smb_passwd_file(), &seed_inbuf[16]);
167 * Add in the root encrypted password.
168 * On any system where security is taken
169 * seriously this will be secret.
172 pw = getpwnam_alloc("root");
173 if (pw && pw->pw_passwd) {
175 unsigned char md4_tmp[16];
176 mdfour(md4_tmp, (unsigned char *)pw->pw_passwd, strlen(pw->pw_passwd));
178 seed_inbuf[8+i] ^= md4_tmp[i];
183 * Add the counter, time of day, and pid.
187 mypid = sys_getpid();
188 v1 = (counter++) + mypid + tval.tv_sec;
189 v2 = (counter++) * mypid + tval.tv_usec;
191 SIVAL(seed_inbuf, 32, v1 ^ IVAL(seed_inbuf, 32));
192 SIVAL(seed_inbuf, 36, v2 ^ IVAL(seed_inbuf, 36));
195 * Add any user-given reseed data.
198 get_rand_reseed_data(&reseed_data);
201 for (i = 0; i < sizeof(seed_inbuf); i++)
202 seed_inbuf[i] ^= ((char *)(&reseed_data))[i % sizeof(reseed_data)];
205 seed_random_stream(seed_inbuf, sizeof(seed_inbuf));
210 /*******************************************************************
211 Interface to the (hopefully) good crypto random number generator.
212 ********************************************************************/
214 void generate_random_buffer( unsigned char *out, int len)
216 static int urand_fd = -1;
217 unsigned char md4_buf[64];
218 unsigned char tmp_buf[16];
222 urand_fd = do_reseed(True, urand_fd);
226 if (urand_fd != -1 && len > 0) {
228 if (read(urand_fd, out, len) == len)
229 return; /* len bytes of random data read from urandom. */
231 /* Read of urand error, drop back to non urand method. */
234 do_reseed(False, -1);
239 * Generate random numbers in chunks of 64 bytes,
240 * then md4 them & copy to the output buffer.
241 * This way the raw state of the stream is never externally
247 int copy_len = len > 16 ? 16 : len;
249 get_random_stream(md4_buf, sizeof(md4_buf));
250 mdfour(tmp_buf, md4_buf, sizeof(md4_buf));
251 memcpy(p, tmp_buf, copy_len);
257 /*******************************************************************
258 Use the random number generator to generate a random string.
259 ********************************************************************/
261 static char c_list[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_-#.,";
263 char *generate_random_str(size_t len)
265 static unsigned char retstr[256];
268 memset(retstr, '\0', sizeof(retstr));
270 if (len > sizeof(retstr)-1)
271 len = sizeof(retstr) -1;
272 generate_random_buffer( retstr, len);
273 for (i = 0; i < len; i++)
274 retstr[i] = c_list[ retstr[i] % (sizeof(c_list)-1) ];
278 return (char *)retstr;