1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
3
* LibTomCrypt is a library that provides various cryptographic
4
* algorithms in a highly modular and flexible manner.
6
* The library is free for all purposes without any express
9
* Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
15
portable way to get secure random bits to feed a PRNG (Tom St Denis)
19
/* on *NIX read /dev/random */
20
static unsigned long rng_nix(unsigned char *buf, unsigned long len,
21
void (*callback)(void))
28
#ifdef TRY_URANDOM_FIRST
29
f = fopen("/dev/urandom", "rb");
31
#endif /* TRY_URANDOM_FIRST */
32
f = fopen("/dev/random", "rb");
38
/* disable buffering */
39
if (setvbuf(f, NULL, _IONBF, 0) != 0) {
44
x = (unsigned long)fread(buf, 1, (size_t)len, f);
47
#endif /* LTC_NO_FILE */
50
#endif /* DEVRANDOM */
52
/* on ANSI C platforms with 100 < CLOCKS_PER_SEC < 10000 */
53
#if defined(CLOCKS_PER_SEC)
57
static unsigned long rng_ansic(unsigned char *buf, unsigned long len,
58
void (*callback)(void))
61
int l, acc, bits, a, b;
63
if (XCLOCKS_PER_SEC < 100 || XCLOCKS_PER_SEC > 10000) {
71
if (callback != NULL) callback();
74
t1 = XCLOCK(); while (t1 == XCLOCK()) a ^= 1;
75
t1 = XCLOCK(); while (t1 == XCLOCK()) b ^= 1;
83
acc = bits = a = b = 0;
89
/* Try the Microsoft CSP */
91
#define _WIN32_WINNT 0x0400
95
static unsigned long rng_win32(unsigned char *buf, unsigned long len,
96
void (*callback)(void))
99
if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
100
(CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) &&
101
!CryptAcquireContext (&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
102
CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET))
105
if (CryptGenRandom(hProv, len, buf) == TRUE) {
106
CryptReleaseContext(hProv, 0);
109
CryptReleaseContext(hProv, 0);
118
@param out Destination
119
@param outlen Length desired (octets)
120
@param callback Pointer to void function to act as "callback" when RNG is slow. This can be NULL
121
@return Number of octets read
123
unsigned long rng_get_bytes(unsigned char *out, unsigned long outlen,
124
void (*callback)(void))
128
LTC_ARGCHK(out != NULL);
130
#if defined(DEVRANDOM)
131
x = rng_nix(out, outlen, callback); if (x != 0) { return x; }
134
x = rng_win32(out, outlen, callback); if (x != 0) { return x; }
137
x = rng_ansic(out, outlen, callback); if (x != 0) { return x; }
142
/* $Source: /cvs/libtom/libtomcrypt/src/prngs/rng_get_bytes.c,v $ */
143
/* $Revision: 1.3 $ */
144
/* $Date: 2005/05/05 14:35:59 $ */