fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
dns_random.c
Go to the documentation of this file.
1#include <unistd.h>
2#include "taia.h"
3#include "uint_t.h"
4#include "dnsresolv.h"
5
13static uint32 seed[32];
14static uint32 in[12];
15static uint32 out[8];
16static int outleft = 0;
17
18#define ROTATE(x,b) (((x) << (b)) | ((x) >> (32 - (b))))
19#define MUSH(i,b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROTATE(x,b));
20
21static void surf(void)
22{
23 uint32 t[12]; uint32 x; uint32 sum = 0;
24 int r; int i; int loop;
25
26 for (i = 0; i < 12; ++i) t[i] = in[i] ^ seed[12 + i];
27 for (i = 0; i < 8; ++i) out[i] = seed[24 + i];
28 x = t[11];
29 for (loop = 0; loop < 2; ++loop) {
30 for (r = 0; r < 16; ++r) {
31 sum += 0x9e3779b9;
32 MUSH(0,5) MUSH(1,7) MUSH(2,9) MUSH(3,13)
33 MUSH(4,5) MUSH(5,7) MUSH(6,9) MUSH(7,13)
34 MUSH(8,5) MUSH(9,7) MUSH(10,9) MUSH(11,13)
35 }
36 for (i = 0; i < 8; ++i) out[i] ^= t[i + 4];
37 }
38}
39
40void dns_random_init(const char data[128])
41{
42 int i;
43 struct taia t;
44 char tpack[16];
45
46 for (i = 0; i < 32; ++i)
47 uint32_unpack((char *)data + 4 * i,seed + i);
48
49 taia_now(&t);
50 taia_pack(tpack,&t);
51 for (i = 0; i < 4; ++i)
52 uint32_unpack(tpack + 4 * i,in + 4 + i);
53
54 in[8] = getpid();
55 in[9] = getppid();
56 /* more space in 10 and 11, but this is probably enough */
57}
58
59unsigned int dns_random(unsigned int n)
60{
61 if (!n) return 0;
62
63 if (!outleft) {
64 if (!++in[0]) if (!++in[1]) if (!++in[2]) ++in[3];
65 surf();
66 outleft = 8;
67 }
68
69 return out[--outleft] % n;
70}
unsigned int dns_random(unsigned int n)
Definition: dns_random.c:59
void dns_random_init(const char data[128])
Definition: dns_random.c:40
#define MUSH(i, b)
Definition: dns_random.c:19
additional types and pack routines
void uint32_unpack(char *, uint32 *)
uint32_t uint32
Definition: uint_t.h:40
void taia_pack(char *, struct taia *)
Definition: taia.c:62
int taia_now(struct taia *)
Definition: taia.c:48
Definition: taia.h:13