OpenVAS Scanner 23.40.3
genrand.c File Reference

Unix SMB/CIFS implementation. Functions to create reasonable random numbers for crypto use. More...

#include "byteorder.h"
#include "md4.h"
#include "proto.h"
#include "smb.h"
#include <pwd.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
Include dependency graph for genrand.c:

Go to the source code of this file.

Macros

#define uint32   uint32_t
#define False   0
#define True   1
#define ZERO_STRUCT(x)

Functions

static void get_rand_reseed_data_ntlmssp (int *reseed_data)
static void do_filehash_ntlmssp (const char *fname, unsigned char *the_hash)
static int do_reseed_ntlmssp (bool use_fd, int fd)
void generate_random_buffer_ntlmssp (unsigned char *out, int len)

Variables

static unsigned char smb_arc4_state [258]
static uint32 counter
static bool done_reseed_ntlmssp = False
static void(* reseed_callback_ntlmssp )(int *newseed)

Detailed Description

Unix SMB/CIFS implementation. Functions to create reasonable random numbers for crypto use.

Random number generation.

Definition in file genrand.c.

Macro Definition Documentation

◆ False

#define False   0

Definition at line 44 of file genrand.c.

◆ True

#define True   1

Definition at line 45 of file genrand.c.

◆ uint32

◆ ZERO_STRUCT

#define ZERO_STRUCT ( x)
Value:
memset ((char *) &(x), 0, sizeof (x))

Definition at line 56 of file genrand.c.

Referenced by E_deshash_ntlmssp(), hmac_md5_init_limK_to_64(), and SMBOWFencrypt_ntlmssp().

Function Documentation

◆ do_filehash_ntlmssp()

void do_filehash_ntlmssp ( const char * fname,
unsigned char * the_hash )
static

Definition at line 84 of file genrand.c.

85{
86 unsigned char buf[1011]; /* deliberate weird size */
87 unsigned char tmp_md4[16];
88 int fd, n;
89
90 fd = open (fname, O_RDONLY, 0);
91 if (fd == -1)
92 return;
93
94 while ((n = read (fd, (char *) buf, sizeof (buf))) > 0)
95 {
96 mdfour_ntlmssp (tmp_md4, buf, n);
97 for (n = 0; n < 16; n++)
98 the_hash[n] ^= tmp_md4[n];
99 }
100 close (fd);
101}
void mdfour_ntlmssp(unsigned char *out, const unsigned char *in, int n)
Definition md4.c:165

References mdfour_ntlmssp().

Referenced by do_reseed_ntlmssp().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ do_reseed_ntlmssp()

int do_reseed_ntlmssp ( bool use_fd,
int fd )
static

Definition at line 116 of file genrand.c.

117{
118 unsigned char seed_inbuf[40];
119 uint32 v1, v2;
120 struct timeval tval;
121 pid_t mypid;
122 int reseed_data = 0;
123
124 if (use_fd)
125 {
126 if (fd != -1)
127 return fd;
128 fd = open ("/dev/urandom", O_RDONLY, 0);
129 if (fd >= 0)
130 return fd;
131 }
132
133 /* Add in some secret file contents */
134 memset (seed_inbuf, '\0', sizeof (seed_inbuf));
135 do_filehash_ntlmssp ("/etc/shadow", &seed_inbuf[0]);
136 /*
137 * Add the counter, time of day, and pid.
138 */
139
140 GetTimeOfDay_ntlmssp (&tval);
141 mypid = getpid ();
142 v1 = (counter++) + mypid + (uint32) tval.tv_sec;
143 v2 = (counter++) * mypid + (uint32) tval.tv_usec;
144
145 SIVAL (seed_inbuf, 32, v1 ^ IVAL (seed_inbuf, 32));
146 SIVAL (seed_inbuf, 36, v2 ^ IVAL (seed_inbuf, 36));
147
148 /*
149 * Add any user-given reseed data.
150 */
151
152 get_rand_reseed_data_ntlmssp (&reseed_data);
153 if (reseed_data)
154 {
155 size_t i;
156 for (i = 0; i < sizeof (seed_inbuf); i++)
157 seed_inbuf[i] ^= ((char *) (&reseed_data))[i % sizeof (reseed_data)];
158 }
159
160 smb_arc4_init_ntlmssp (smb_arc4_state, seed_inbuf, sizeof (seed_inbuf));
161
162 return -1;
163}
void smb_arc4_init_ntlmssp(unsigned char arc4_state_out[258], const unsigned char *key, size_t keylen)
Definition arc4.c:16
#define SIVAL(buf, pos, val)
Definition byteorder.h:117
#define IVAL(buf, pos)
Definition byteorder.h:108
static void get_rand_reseed_data_ntlmssp(int *reseed_data)
Definition genrand.c:66
static unsigned char smb_arc4_state[258]
Definition genrand.c:47
static void do_filehash_ntlmssp(const char *fname, unsigned char *the_hash)
Definition genrand.c:84
static uint32 counter
Definition genrand.c:48
#define uint32
Definition genrand.c:40
static struct timeval timeval(unsigned long val)
void GetTimeOfDay_ntlmssp(struct timeval *tval)
Definition time.c:91

References counter, do_filehash_ntlmssp(), get_rand_reseed_data_ntlmssp(), GetTimeOfDay_ntlmssp(), IVAL, SIVAL, smb_arc4_init_ntlmssp(), smb_arc4_state, timeval(), and uint32.

Referenced by generate_random_buffer_ntlmssp().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generate_random_buffer_ntlmssp()

void generate_random_buffer_ntlmssp ( unsigned char * out,
int len )

Definition at line 170 of file genrand.c.

171{
172 static int urand_fd = -1;
173 unsigned char md4_buf[64];
174 unsigned char tmp_buf[16];
175 unsigned char *p;
176
178 {
179 urand_fd = do_reseed_ntlmssp (True, urand_fd);
181 }
182
183 if (urand_fd != -1 && len > 0)
184 {
185 if (read (urand_fd, out, len) == len)
186 return; /* len bytes of random data read from urandom. */
187
188 /* Read of urand error, drop back to non urand method. */
189 close (urand_fd);
190 urand_fd = -1;
193 }
194
195 /*
196 * Generate random numbers in chunks of 64 bytes,
197 * then md4 them & copy to the output buffer.
198 * This way the raw state of the stream is never externally
199 * seen.
200 */
201
202 p = out;
203 while (len > 0)
204 {
205 int copy_len = len > 16 ? 16 : len;
206
207 bzero (md4_buf, sizeof (md4_buf));
208 smb_arc4_crypt_ntlmssp (smb_arc4_state, md4_buf, sizeof (md4_buf));
209 mdfour_ntlmssp (tmp_buf, md4_buf, sizeof (md4_buf));
210 memcpy (p, tmp_buf, copy_len);
211 p += copy_len;
212 len -= copy_len;
213 }
214}
void smb_arc4_crypt_ntlmssp(unsigned char arc4_state_inout[258], unsigned char *data, size_t len)
Definition arc4.c:47
#define False
Definition charcnv.c:63
#define True
Definition charcnv.c:64
static int do_reseed_ntlmssp(bool use_fd, int fd)
Definition genrand.c:116
static bool done_reseed_ntlmssp
Definition genrand.c:58
uint8_t len

References do_reseed_ntlmssp(), done_reseed_ntlmssp, False, len, mdfour_ntlmssp(), smb_arc4_crypt_ntlmssp(), smb_arc4_state, and True.

Referenced by LMv2_generate_response_ntlmssp(), ntlmssp_genauth_keyexchg(), ntlmssp_genauth_ntlm2(), and NTLMv2_generate_client_data_ntlmssp().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_rand_reseed_data_ntlmssp()

void get_rand_reseed_data_ntlmssp ( int * reseed_data)
static

Definition at line 66 of file genrand.c.

67{
69 {
70 reseed_callback_ntlmssp (reseed_data);
71 }
72 else
73 {
74 *reseed_data = 0;
75 }
76}
static void(* reseed_callback_ntlmssp)(int *newseed)
Definition genrand.c:59

References reseed_callback_ntlmssp.

Referenced by do_reseed_ntlmssp().

Here is the caller graph for this function:

Variable Documentation

◆ counter

uint32 counter
static

Definition at line 48 of file genrand.c.

Referenced by do_reseed_ntlmssp(), and ftp_log_in().

◆ done_reseed_ntlmssp

bool done_reseed_ntlmssp = False
static

Definition at line 58 of file genrand.c.

Referenced by generate_random_buffer_ntlmssp().

◆ reseed_callback_ntlmssp

void(* reseed_callback_ntlmssp) (int *newseed) ( int * newseed)
static

Definition at line 59 of file genrand.c.

Referenced by get_rand_reseed_data_ntlmssp().

◆ smb_arc4_state

unsigned char smb_arc4_state[258]
static

Definition at line 47 of file genrand.c.

Referenced by do_reseed_ntlmssp(), and generate_random_buffer_ntlmssp().