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 3 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, see <http://www.gnu.org/licenses/>.
24 static struct arcfour_state smb_arc4_state;
25 static uint32 counter;
27 static bool done_reseed = False;
28 static void (*reseed_callback)(int *newseed);
30 /****************************************************************
31 Copy any user given reseed data.
32 *****************************************************************/
34 void set_rand_reseed_callback(void (*fn)(int *))
37 set_need_random_reseed();
40 void set_need_random_reseed(void)
45 static void get_rand_reseed_data(int *reseed_data)
47 if (reseed_callback) {
48 reseed_callback(reseed_data);
54 /****************************************************************
55 Get a 16 byte hash from the contents of a file.
56 Note that the hash is not initialised.
57 *****************************************************************/
59 static void do_filehash(const char *fname, unsigned char *the_hash)
61 unsigned char buf[1011]; /* deliberate weird size */
62 unsigned char tmp_md4[16];
65 fd = sys_open(fname,O_RDONLY,0);
69 while ((n = read(fd, (char *)buf, sizeof(buf))) > 0) {
70 mdfour(tmp_md4, buf, n);
72 the_hash[n] ^= tmp_md4[n];
77 /**************************************************************
78 Try and get a good random number seed. Try a number of
79 different factors. Firstly, try /dev/urandom - use if exists.
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.
85 If we can't use /dev/urandom then seed the stream random generator
87 **************************************************************/
89 static int do_reseed(bool use_fd, int fd)
91 unsigned char seed_inbuf[40];
92 DATA_BLOB seed_blob = { seed_inbuf, 40 };
93 uint32 v1, v2; struct timeval tval; pid_t mypid;
101 fd = sys_open( "/dev/urandom", O_RDONLY,0);
106 /* Add in some secret file contents */
108 do_filehash("/etc/shadow", &seed_inbuf[0]);
109 do_filehash(lp_smb_passwd_file(), &seed_inbuf[16]);
112 * Add in the root encrypted password.
113 * On any system where security is taken
114 * seriously this will be secret.
117 pw = getpwnam_alloc(NULL, "root");
118 if (pw && pw->pw_passwd) {
120 unsigned char md4_tmp[16];
121 mdfour(md4_tmp, (unsigned char *)pw->pw_passwd, strlen(pw->pw_passwd));
123 seed_inbuf[8+i] ^= md4_tmp[i];
128 * Add the counter, time of day, and pid.
132 mypid = sys_getpid();
133 v1 = (counter++) + mypid + tval.tv_sec;
134 v2 = (counter++) * mypid + tval.tv_usec;
136 SIVAL(seed_inbuf, 32, v1 ^ IVAL(seed_inbuf, 32));
137 SIVAL(seed_inbuf, 36, v2 ^ IVAL(seed_inbuf, 36));
140 * Add any user-given reseed data.
143 get_rand_reseed_data(&reseed_data);
146 for (i = 0; i < sizeof(seed_inbuf); i++)
147 seed_inbuf[i] ^= ((char *)(&reseed_data))[i % sizeof(reseed_data)];
150 arcfour_init(&smb_arc4_state, &seed_blob);
155 /*******************************************************************
156 Interface to the (hopefully) good crypto random number generator.
157 ********************************************************************/
159 void generate_random_buffer( unsigned char *out, int len)
161 static int urand_fd = -1;
162 unsigned char md4_buf[64];
163 unsigned char tmp_buf[16];
167 urand_fd = do_reseed(True, urand_fd);
171 if (urand_fd != -1 && len > 0) {
173 if (read(urand_fd, out, len) == len)
174 return; /* len bytes of random data read from urandom. */
176 /* Read of urand error, drop back to non urand method. */
179 do_reseed(False, -1);
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
192 int copy_len = len > 16 ? 16 : len;
194 arcfour_crypt_sbox(&smb_arc4_state, md4_buf, sizeof(md4_buf));
195 mdfour(tmp_buf, md4_buf, sizeof(md4_buf));
196 memcpy(p, tmp_buf, copy_len);
202 /*******************************************************************
203 Use the random number generator to generate a random string.
204 ********************************************************************/
206 static char c_list[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_-#.,";
208 char *generate_random_str(TALLOC_CTX *mem_ctx, size_t len)
210 unsigned char *retstr = talloc_zero_array(mem_ctx, unsigned char, len);
213 generate_random_buffer( retstr, len);
214 for (i = 0; i < len; i++)
215 retstr[i] = c_list[ retstr[i] % (sizeof(c_list)-1) ];
219 return (char *)retstr;