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
;
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 v1
, v2
; struct timeval tval
; pid_t mypid
;
144 fd
= sys_open( "/dev/urandom", O_RDONLY
,0);
149 /* Add in some secret file contents */
151 do_filehash("/etc/shadow", &seed_inbuf
[0]);
152 do_filehash(lp_smb_passwd_file(), &seed_inbuf
[16]);
155 * Add in the root encrypted password.
156 * On any system where security is taken
157 * seriously this will be secret.
160 pw
= getpwnam_alloc("root");
161 if (pw
&& pw
->pw_passwd
) {
163 unsigned char md4_tmp
[16];
164 mdfour(md4_tmp
, (unsigned char *)pw
->pw_passwd
, strlen(pw
->pw_passwd
));
166 seed_inbuf
[8+i
] ^= md4_tmp
[i
];
171 * Add the counter, time of day, and pid.
175 mypid
= sys_getpid();
176 v1
= (counter
++) + mypid
+ tval
.tv_sec
;
177 v2
= (counter
++) * mypid
+ tval
.tv_usec
;
179 SIVAL(seed_inbuf
, 32, v1
^ IVAL(seed_inbuf
, 32));
180 SIVAL(seed_inbuf
, 36, v2
^ IVAL(seed_inbuf
, 36));
183 * Add any user-given reseed data.
188 for (i
= 0; i
< sizeof(seed_inbuf
); i
++)
189 seed_inbuf
[i
] ^= reseed_data
[i
% reseed_data_size
];
192 seed_random_stream(seed_inbuf
, sizeof(seed_inbuf
));
197 /*******************************************************************
198 Interface to the (hopefully) good crypto random number generator.
199 ********************************************************************/
201 void generate_random_buffer( unsigned char *out
, int len
, BOOL do_reseed_now
)
203 static BOOL done_reseed
= False
;
204 static int urand_fd
= -1;
205 unsigned char md4_buf
[64];
206 unsigned char tmp_buf
[16];
209 if(!done_reseed
|| do_reseed_now
) {
210 urand_fd
= do_reseed(True
, urand_fd
);
214 if (urand_fd
!= -1 && len
> 0) {
216 if (read(urand_fd
, out
, len
) == len
)
217 return; /* len bytes of random data read from urandom. */
219 /* Read of urand error, drop back to non urand method. */
222 do_reseed(False
, -1);
227 * Generate random numbers in chunks of 64 bytes,
228 * then md4 them & copy to the output buffer.
229 * This way the raw state of the stream is never externally
235 int copy_len
= len
> 16 ? 16 : len
;
237 get_random_stream(md4_buf
, sizeof(md4_buf
));
238 mdfour(tmp_buf
, md4_buf
, sizeof(md4_buf
));
239 memcpy(p
, tmp_buf
, copy_len
);
245 /*******************************************************************
246 Use the random number generator to generate a random string.
247 ********************************************************************/
249 static char c_list
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_-#.,";
251 char *generate_random_str(size_t len
)
253 static unsigned char retstr
[256];
256 memset(retstr
, '\0', sizeof(retstr
));
258 if (len
> sizeof(retstr
)-1)
259 len
= sizeof(retstr
) -1;
260 generate_random_buffer( retstr
, len
, False
);
261 for (i
= 0; i
< len
; i
++)
262 retstr
[i
] = c_list
[ retstr
[i
] % (sizeof(c_list
)-1) ];
266 return (char *)retstr
;