some changes for OSF1 support in smbwrapper (just preliminary changes,
[Samba/gbeck.git] / source / lib / genrand.c
blobbb1922e4f5c795bafda8c11e0f415d7534e6589b
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
5 Functions to create reasonable random numbers for crypto use.
7 Copyright (C) Jeremy Allison 1998
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "includes.h"
26 extern int DEBUGLEVEL;
27 static uint32 counter = 0;
29 /****************************************************************
30 get a 16 byte hash from the contents of a file
31 Note that the hash is not initialised.
32 *****************************************************************/
33 static void do_filehash(char *fname, unsigned char *hash)
35 unsigned char buf[1011]; /* deliberate weird size */
36 unsigned char tmp_md4[16];
37 int fd, n;
39 fd = open(fname,O_RDONLY);
40 if (fd == -1) return;
42 while ((n = read(fd, (char *)buf, sizeof(buf))) > 0) {
43 mdfour(tmp_md4, buf, n);
44 for (n=0;n<16;n++)
45 hash[n] ^= tmp_md4[n];
47 close(fd);
52 /****************************************************************
53 Try and get a seed by looking at the atimes of files in a given
54 directory. XOR them into the buf array.
55 *****************************************************************/
57 static void do_dirrand(char *name, unsigned char *buf, int buf_len)
59 void *dp = dos_opendir(name);
60 pstring fullname;
61 int len_left;
62 int fullname_len;
63 char *pos;
65 pstrcpy(fullname, name);
66 fullname_len = strlen(fullname);
68 if(fullname_len + 2 > sizeof(pstring))
69 return;
71 if(fullname[fullname_len] != '/') {
72 fullname[fullname_len] = '/';
73 fullname[fullname_len+1] = '\0';
74 fullname_len = strlen(fullname);
77 len_left = sizeof(pstring) - fullname_len - 1;
78 pos = &fullname[fullname_len];
80 if(dp != NULL) {
81 char *p;
83 while ((p = readdirname(dp))) {
84 SMB_STRUCT_STAT st;
86 if(strlen(p) <= len_left)
87 pstrcpy(pos, p);
89 if(dos_stat(fullname,&st) == 0) {
90 SIVAL(buf, ((counter * 4)%(buf_len-4)),
91 IVAL(buf,((counter * 4)%(buf_len-4))) ^ st.st_atime);
92 counter++;
93 DEBUG(10,("do_dirrand: value from file %s.\n", fullname));
96 closedir(dp);
100 /**************************************************************
101 Try and get a good random number seed. Try a number of
102 different factors. Firstly, try /dev/random and try and
103 read from this. If this fails iterate through /tmp and
104 /dev and XOR all the file timestamps. Next add in
105 a hash of the contents of /etc/shadow and the smb passwd
106 file and a combination of pid and time of day (yes I know this
107 sucks :-). Finally md4 the result.
109 The result goes in a 16 byte buffer passed from the caller
110 **************************************************************/
112 static uint32 do_reseed(unsigned char *md4_outbuf)
114 unsigned char md4_inbuf[40];
115 BOOL got_random = False;
116 uint32 v1, v2, ret;
117 int fd;
118 struct timeval tval;
119 pid_t mypid;
120 struct passwd *pw;
122 memset(md4_inbuf, '\0', sizeof(md4_inbuf));
124 fd = open( "/dev/random", O_RDONLY);
125 if(fd >= 0) {
127 * We can use /dev/random !
129 if(read(fd, md4_inbuf, 40) == 40) {
130 got_random = True;
131 DEBUG(10,("do_reseed: got 40 bytes from /dev/random.\n"));
133 close(fd);
136 if(!got_random) {
138 * /dev/random failed - try /tmp and /dev for timestamps.
140 do_dirrand("/tmp", md4_inbuf, sizeof(md4_inbuf));
141 do_dirrand("/dev", md4_inbuf, sizeof(md4_inbuf));
144 /* possibly add in some secret file contents */
145 do_filehash("/etc/shadow", &md4_inbuf[0]);
146 do_filehash(lp_smb_passwd_file(), &md4_inbuf[16]);
148 /* add in the root encrypted password. On any system where security is taken
149 seriously this will be secret */
150 pw = getpwnam("root");
151 if (pw && pw->pw_passwd) {
152 int i;
153 unsigned char md4_tmp[16];
154 mdfour(md4_tmp, (unsigned char *)pw->pw_passwd, strlen(pw->pw_passwd));
155 for (i=0;i<16;i++)
156 md4_inbuf[8+i] ^= md4_tmp[i];
160 * Finally add the counter, time of day, and pid.
162 GetTimeOfDay(&tval);
163 mypid = getpid();
164 v1 = (counter++) + mypid + tval.tv_sec;
165 v2 = (counter++) * mypid + tval.tv_usec;
167 SIVAL(md4_inbuf, 32, v1 ^ IVAL(md4_inbuf, 32));
168 SIVAL(md4_inbuf, 36, v2 ^ IVAL(md4_inbuf, 36));
170 mdfour(md4_outbuf, md4_inbuf, sizeof(md4_inbuf));
173 * Return a 32 bit int created from XORing the
174 * 16 bit return buffer.
177 ret = IVAL(md4_outbuf, 0);
178 ret ^= IVAL(md4_outbuf, 4);
179 ret ^= IVAL(md4_outbuf, 8);
180 return (ret ^ IVAL(md4_outbuf, 12));
183 /*******************************************************************
184 Interface to the (hopefully) good crypto random number generator.
185 ********************************************************************/
187 void generate_random_buffer( unsigned char *out, int len, BOOL re_seed)
189 static BOOL done_reseed = False;
190 static unsigned char md4_buf[16];
191 unsigned char tmp_buf[16];
192 unsigned char *p;
194 if(!done_reseed || re_seed) {
195 sys_srandom(do_reseed(md4_buf));
196 done_reseed = True;
200 * Generate random numbers in chunks of 64 bytes,
201 * then md4 them & copy to the output buffer.
202 * Added XOR with output from random, seeded
203 * by the original md4_buf. This is to stop the
204 * output from this function being the previous
205 * md4_buf md4'ed. The output from this function
206 * is often output onto the wire, and so it should
207 * not be possible to guess the next output from
208 * this function based on the previous output.
209 * XORing in the output from random(), seeded by
210 * the original md4 hash should stop this. JRA.
213 p = out;
214 while(len > 0) {
215 int i;
216 int copy_len = len > 16 ? 16 : len;
217 mdfour(tmp_buf, md4_buf, sizeof(md4_buf));
218 memcpy(md4_buf, tmp_buf, sizeof(md4_buf));
219 /* XOR in output from random(). */
220 for(i = 0; i < 4; i++)
221 SIVAL(tmp_buf, i*4, (IVAL(tmp_buf, i*4) ^ (uint32)sys_random()));
222 memcpy(p, tmp_buf, copy_len);
223 p += copy_len;
224 len -= copy_len;