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_t counter;
27 static unsigned char *reseed_data;
28 static size_t reseed_data_size;
30 /****************************************************************
31 Copy any user given reseed data.
32 *****************************************************************/
34 void set_rand_reseed_data(unsigned char *data, size_t len)
36 SAFE_FREE(reseed_data);
39 reseed_data = (unsigned char *)memdup(data, len);
41 reseed_data_size = len;
44 /****************************************************************
46 *****************************************************************/
48 static void seed_random_stream(unsigned char *seedval, size_t seedlen)
53 for (ind = 0; ind < 256; ind++)
54 hash[ind] = (unsigned char)ind;
56 for( ind = 0; ind < 256; ind++) {
59 j += (hash[ind] + seedval[ind%seedlen]);
70 /****************************************************************
71 Get datasize bytes worth of random data.
72 *****************************************************************/
74 static void get_random_stream(unsigned char *data, size_t datasize)
76 unsigned char index_i = hash[256];
77 unsigned char index_j = hash[257];
80 for( ind = 0; ind < datasize; ind++) {
85 index_j += hash[index_i];
88 hash[index_i] = hash[index_j];
91 t = hash[index_i] + hash[index_j];
99 /****************************************************************
100 Get a 16 byte hash from the contents of a file.
101 Note that the hash is not initialised.
102 *****************************************************************/
104 static void do_filehash(const char *fname, unsigned char *the_hash)
106 unsigned char buf[1011]; /* deliberate weird size */
107 unsigned char tmp_md4[16];
110 fd = sys_open(fname,O_RDONLY,0);
114 while ((n = read(fd, (char *)buf, sizeof(buf))) > 0) {
115 mdfour(tmp_md4, buf, n);
117 the_hash[n] ^= tmp_md4[n];
122 /**************************************************************
123 Try and get a good random number seed. Try a number of
124 different factors. Firstly, try /dev/urandom - use if exists.
126 We use /dev/urandom as a read of /dev/random can block if
127 the entropy pool dries up. This leads clients to timeout
128 or be very slow on connect.
130 If we can't use /dev/urandom then seed the stream random generator
132 **************************************************************/
134 static int do_reseed(BOOL use_fd, int fd)
136 unsigned char seed_inbuf[40];
137 uint32_t v1, v2; struct timeval tval; pid_t mypid;
143 fd = sys_open( "/dev/urandom", O_RDONLY,0);
148 /* Add in some secret file contents */
150 do_filehash("/etc/shadow", &seed_inbuf[0]);
151 do_filehash(lp_smb_passwd_file(), &seed_inbuf[16]);
154 * Add the counter, time of day, and pid.
159 v1 = (counter++) + mypid + tval.tv_sec;
160 v2 = (counter++) * mypid + tval.tv_usec;
162 SIVAL(seed_inbuf, 32, v1 ^ IVAL(seed_inbuf, 32));
163 SIVAL(seed_inbuf, 36, v2 ^ IVAL(seed_inbuf, 36));
166 * Add any user-given reseed data.
171 for (i = 0; i < sizeof(seed_inbuf); i++)
172 seed_inbuf[i] ^= reseed_data[i % reseed_data_size];
175 seed_random_stream(seed_inbuf, sizeof(seed_inbuf));
180 /*******************************************************************
181 Interface to the (hopefully) good crypto random number generator.
182 ********************************************************************/
184 void generate_random_buffer( unsigned char *out, int len, BOOL do_reseed_now)
186 static BOOL done_reseed = False;
187 static int urand_fd = -1;
188 unsigned char md4_buf[64];
189 unsigned char tmp_buf[16];
192 if(!done_reseed || do_reseed_now) {
193 urand_fd = do_reseed(True, urand_fd);
197 if (urand_fd != -1 && len > 0) {
199 if (read(urand_fd, out, len) == len)
200 return; /* len bytes of random data read from urandom. */
202 /* Read of urand error, drop back to non urand method. */
205 do_reseed(False, -1);
210 * Generate random numbers in chunks of 64 bytes,
211 * then md4 them & copy to the output buffer.
212 * This way the raw state of the stream is never externally
218 int copy_len = len > 16 ? 16 : len;
220 get_random_stream(md4_buf, sizeof(md4_buf));
221 mdfour(tmp_buf, md4_buf, sizeof(md4_buf));
222 memcpy(p, tmp_buf, copy_len);
230 very basic password quality checker
232 BOOL check_password_quality(const char *s)
234 int has_digit=0, has_capital=0, has_lower=0;
238 } else if (isupper(*s)) {
240 } else if (islower(*s)) {
246 return has_digit && has_lower && has_capital;
249 /*******************************************************************
250 Use the random number generator to generate a random string.
251 ********************************************************************/
253 static char c_list[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_-#.,";
255 char *generate_random_str(TALLOC_CTX *mem_ctx, size_t len)
259 char *retstr = talloc(mem_ctx, len + 1);
265 generate_random_buffer(retstr, len, False);
266 for (i = 0; i < len; i++)
267 retstr[i] = c_list[retstr[i] % (sizeof(c_list)-1) ];
271 /* we need to make sure the random string passes basic quality tests
272 or it might be rejected by windows as a password */
273 if (len >= 7 && !check_password_quality(retstr)) {