version 1.1.1.1, 2012/02/21 22:14:23
|
version 1.1.1.3, 2023/09/27 11:11:38
|
Line 1
|
Line 1
|
/* |
/* |
* $Id$ |
|
* |
|
* libnet |
* libnet |
* libnet_prand.c - pseudo-random number generation |
* libnet_prand.c - pseudo-random number generation |
* |
* |
Line 29
|
Line 27
|
* SUCH DAMAGE. |
* SUCH DAMAGE. |
* |
* |
*/ |
*/ |
| |
#if (HAVE_CONFIG_H) | #include "common.h" |
#include "../include/config.h" | |
#endif | #ifdef _WIN32 |
#if (!(_WIN32) || (__CYGWIN__)) | #include <wincrypt.h> |
#include "../include/libnet.h" | #include <time.h> |
| |
#else |
#else |
#include "../include/win32/libnet.h" | #include <sys/time.h> /* gettimeofday() */ |
#endif |
#endif |
|
|
|
|
int |
int |
libnet_seed_prand(libnet_t *l) |
libnet_seed_prand(libnet_t *l) |
{ |
{ |
#if !(__WIN32__) | #ifndef WIN32 |
struct timeval seed; |
struct timeval seed; |
#endif | #endif |
|
|
if (l == NULL) |
if (l == NULL) |
{ | { |
return (-1); |
return (-1); |
} | } |
|
|
#if __WIN32__ | #ifdef WIN32 |
srand((unsigned)time(NULL)); |
srand((unsigned)time(NULL)); |
#else | #else |
if (gettimeofday(&seed, NULL) == -1) |
if (gettimeofday(&seed, NULL) == -1) |
{ |
{ |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
"%s(): cannot gettimeofday\n", __func__); | "%s(): cannot gettimeofday", __func__); |
return (-1); |
return (-1); |
} |
} |
|
|
Line 64 libnet_seed_prand(libnet_t *l)
|
Line 65 libnet_seed_prand(libnet_t *l)
|
* More entropy then just seeding with time(2). |
* More entropy then just seeding with time(2). |
*/ |
*/ |
srandom((unsigned)(seed.tv_sec ^ seed.tv_usec)); |
srandom((unsigned)(seed.tv_sec ^ seed.tv_usec)); |
#endif | #endif |
return (1); |
return (1); |
} |
} |
|
|
| /* FIXME this code makes no sense. On unix we use random(), which |
u_int32_t | * is intended to have no security, and under win32 we use cryptographically |
| * strong entropy source? If necessary, why aren't we using /dev/random |
| * on unix? What's going on here? |
| */ |
| uint32_t |
libnet_get_prand(int mod) |
libnet_get_prand(int mod) |
{ |
{ |
u_int32_t n; /* 0 to 4,294,967,295 */ | uint32_t n; /* 0 to 4,294,967,295 */ |
#ifndef _WIN32 | #ifndef WIN32 |
n = random(); |
n = random(); |
#else |
#else |
HCRYPTPROV hProv = 0; | HCRYPTPROV hProv = 0; |
CryptAcquireContext(&hProv, | |
0, 0, PROV_RSA_FULL, | CryptAcquireContext(&hProv, |
CRYPT_VERIFYCONTEXT); | 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); |
| |
CryptGenRandom(hProv, | CryptGenRandom(hProv, sizeof(n), (BYTE*)&n); |
sizeof(n), (BYTE*)&n); | CryptReleaseContext(hProv, 0); |
CryptReleaseContext(hProv, 0); | |
#endif |
#endif |
switch (mod) |
switch (mod) |
{ |
{ |
case LIBNET_PR2: |
case LIBNET_PR2: |
return (n % 0x2); /* 0 - 1 */ | return (n & 0x1); /* 0 - 1 */ |
case LIBNET_PR8: |
case LIBNET_PR8: |
return (n % 0xff); /* 0 - 255 */ | return (n & 0xff); /* 0 - 255 */ |
case LIBNET_PR16: |
case LIBNET_PR16: |
return (n % 0x7fff); /* 0 - 32767 */ | return (n & 0x7fff); /* 0 - 32767 */ |
case LIBNET_PRu16: |
case LIBNET_PRu16: |
return (n % 0xffff); /* 0 - 65535 */ | return (n & 0xffff); /* 0 - 65535 */ |
case LIBNET_PR32: |
case LIBNET_PR32: |
return (n % 0x7fffffff); /* 0 - 2147483647 */ | return (n & 0x7fffffff); /* 0 - 2147483647 */ |
case LIBNET_PRu32: |
case LIBNET_PRu32: |
return (n); /* 0 - 4294967295 */ |
return (n); /* 0 - 4294967295 */ |
} |
} |
return (0); /* NOTTREACHED */ |
return (0); /* NOTTREACHED */ |
} |
} |
|
|
/* EOF */ | /** |
| * Local Variables: |
| * indent-tabs-mode: nil |
| * c-file-style: "stroustrup" |
| * End: |
| */ |