OpenVAS Scanner  7.0.1~git
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)   memset ((char *) &(x), 0, sizeof (x))
 

Typedefs

typedef unsigned int bool
 

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 58 of file genrand.c.

◆ True

#define True   1

Definition at line 59 of file genrand.c.

◆ uint32

#define uint32   uint32_t

Definition at line 54 of file genrand.c.

◆ ZERO_STRUCT

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

Definition at line 70 of file genrand.c.

Typedef Documentation

◆ bool

typedef unsigned int bool

Definition at line 57 of file genrand.c.

Function Documentation

◆ do_filehash_ntlmssp()

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

Definition at line 98 of file genrand.c.

99 {
100  unsigned char buf[1011]; /* deliberate weird size */
101  unsigned char tmp_md4[16];
102  int fd, n;
103 
104  fd = open (fname, O_RDONLY, 0);
105  if (fd == -1)
106  return;
107 
108  while ((n = read (fd, (char *) buf, sizeof (buf))) > 0)
109  {
110  mdfour_ntlmssp (tmp_md4, buf, n);
111  for (n = 0; n < 16; n++)
112  the_hash[n] ^= tmp_md4[n];
113  }
114  close (fd);
115 }

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()

static int do_reseed_ntlmssp ( bool  use_fd,
int  fd 
)
static

Definition at line 130 of file genrand.c.

131 {
132  unsigned char seed_inbuf[40];
133  uint32 v1, v2;
134  struct timeval tval;
135  pid_t mypid;
136  int reseed_data = 0;
137 
138  if (use_fd)
139  {
140  if (fd != -1)
141  return fd;
142  fd = open ("/dev/urandom", O_RDONLY, 0);
143  if (fd >= 0)
144  return fd;
145  }
146 
147  /* Add in some secret file contents */
148  memset (seed_inbuf, '\0', sizeof (seed_inbuf));
149  do_filehash_ntlmssp ("/etc/shadow", &seed_inbuf[0]);
150  /*
151  * Add the counter, time of day, and pid.
152  */
153 
154  GetTimeOfDay_ntlmssp (&tval);
155  mypid = getpid ();
156  v1 = (counter++) + mypid + tval.tv_sec;
157  v2 = (counter++) * mypid + tval.tv_usec;
158 
159  SIVAL (seed_inbuf, 32, v1 ^ IVAL (seed_inbuf, 32));
160  SIVAL (seed_inbuf, 36, v2 ^ IVAL (seed_inbuf, 36));
161 
162  /*
163  * Add any user-given reseed data.
164  */
165 
166  get_rand_reseed_data_ntlmssp (&reseed_data);
167  if (reseed_data)
168  {
169  size_t i;
170  for (i = 0; i < sizeof (seed_inbuf); i++)
171  seed_inbuf[i] ^= ((char *) (&reseed_data))[i % sizeof (reseed_data)];
172  }
173 
174  smb_arc4_init_ntlmssp (smb_arc4_state, seed_inbuf, sizeof (seed_inbuf));
175 
176  return -1;
177 }

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 184 of file genrand.c.

185 {
186  static int urand_fd = -1;
187  unsigned char md4_buf[64];
188  unsigned char tmp_buf[16];
189  unsigned char *p;
190 
191  if (!done_reseed_ntlmssp)
192  {
193  urand_fd = do_reseed_ntlmssp (True, urand_fd);
195  }
196 
197  if (urand_fd != -1 && len > 0)
198  {
199  if (read (urand_fd, out, len) == len)
200  return; /* len bytes of random data read from urandom. */
201 
202  /* Read of urand error, drop back to non urand method. */
203  close (urand_fd);
204  urand_fd = -1;
205  do_reseed_ntlmssp (False, -1);
207  }
208 
209  /*
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
213  * seen.
214  */
215 
216  p = out;
217  while (len > 0)
218  {
219  int copy_len = len > 16 ? 16 : len;
220 
221  bzero (md4_buf, sizeof (md4_buf));
222  smb_arc4_crypt_ntlmssp (smb_arc4_state, md4_buf, sizeof (md4_buf));
223  mdfour_ntlmssp (tmp_buf, md4_buf, sizeof (md4_buf));
224  memcpy (p, tmp_buf, copy_len);
225  p += copy_len;
226  len -= copy_len;
227  }
228 }

References do_reseed_ntlmssp(), done_reseed_ntlmssp, False, 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()

static void get_rand_reseed_data_ntlmssp ( int *  reseed_data)
static

Definition at line 80 of file genrand.c.

81 {
83  {
84  reseed_callback_ntlmssp (reseed_data);
85  }
86  else
87  {
88  *reseed_data = 0;
89  }
90 }

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 62 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 72 of file genrand.c.

Referenced by generate_random_buffer_ntlmssp().

◆ reseed_callback_ntlmssp

void(* reseed_callback_ntlmssp) (int *newseed)
static

Definition at line 73 of file genrand.c.

Referenced by get_rand_reseed_data_ntlmssp().

◆ smb_arc4_state

unsigned char smb_arc4_state[258]
static

Definition at line 61 of file genrand.c.

Referenced by do_reseed_ntlmssp(), and generate_random_buffer_ntlmssp().

mdfour_ntlmssp
void mdfour_ntlmssp(unsigned char *out, const unsigned char *in, int n)
Definition: md4.c:174
do_reseed_ntlmssp
static int do_reseed_ntlmssp(bool use_fd, int fd)
Definition: genrand.c:130
True
#define True
Definition: genrand.c:59
timeval
struct timeval timeval(unsigned long val)
Definition: nasl_builtin_synscan.c:105
get_rand_reseed_data_ntlmssp
static void get_rand_reseed_data_ntlmssp(int *reseed_data)
Definition: genrand.c:80
SIVAL
#define SIVAL(buf, pos, val)
Definition: byteorder.h:130
smb_arc4_crypt_ntlmssp
void smb_arc4_crypt_ntlmssp(unsigned char arc4_state_inout[258], unsigned char *data, size_t len)
Definition: arc4.c:58
smb_arc4_state
static unsigned char smb_arc4_state[258]
Definition: genrand.c:61
done_reseed_ntlmssp
static bool done_reseed_ntlmssp
Definition: genrand.c:72
smb_arc4_init_ntlmssp
void smb_arc4_init_ntlmssp(unsigned char arc4_state_out[258], const unsigned char *key, size_t keylen)
Definition: arc4.c:27
counter
static uint32 counter
Definition: genrand.c:62
do_filehash_ntlmssp
static void do_filehash_ntlmssp(const char *fname, unsigned char *the_hash)
Definition: genrand.c:98
reseed_callback_ntlmssp
static void(* reseed_callback_ntlmssp)(int *newseed)
Definition: genrand.c:73
False
#define False
Definition: genrand.c:58
uint32
#define uint32
Definition: genrand.c:54
IVAL
#define IVAL(buf, pos)
Definition: byteorder.h:121
GetTimeOfDay_ntlmssp
void GetTimeOfDay_ntlmssp(struct timeval *tval)
Definition: time.c:102