djbdnscurve6 53
djbdnscurve6
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 "dns.h"
5#include "open.h"
6#include "logmsg.h"
7#include "curve.h"
8
9static uint32 seed[32];
10static uint32 in[12];
11static uint32 out[8];
12static int outleft = 0;
13
14#define ROTATE(x,b) (((x) << (b)) | ((x) >> (32 - (b))))
15#define MUSH(i,b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROTATE(x,b));
16
17void surf(void)
18{
19 uint32 t[12]; uint32 x; uint32 sum = 0;
20 int r; int i; int loop;
21
22 for (i = 0; i < 12; ++i) t[i] = in[i] ^ seed[12 + i];
23 for (i = 0; i < 8; ++i) out[i] = seed[24 + i];
24 x = t[11];
25 for (loop = 0; loop < 2; ++loop) {
26 for (r = 0; r < 16; ++r) {
27 sum += 0x9e3779b9;
28 MUSH(0,5) MUSH(1,7) MUSH(2,9) MUSH(3,13)
29 MUSH(4,5) MUSH(5,7) MUSH(6,9) MUSH(7,13)
30 MUSH(8,5) MUSH(9,7) MUSH(10,9) MUSH(11,13)
31 }
32 for (i = 0; i < 8; ++i) out[i] ^= t[i + 4];
33 }
34}
35
36void dns_random_init(const char data[128])
37{
38 int i;
39 struct taia t;
40 char tpack[16];
41
42 for (i = 0; i < 32; ++i)
43 uint32_unpack((char *)data + 4 * i,seed + i);
44
45 taia_now(&t);
46 taia_pack(tpack,&t);
47 for (i = 0; i < 4; ++i)
48 uint32_unpack(tpack + 4 * i,in + 4 + i);
49
50 in[8] = getpid();
51 in[9] = getppid();
52 /* more space in 10 and 11, but this is probably enough */
53}
54
55unsigned int dns_random(unsigned int n)
56{
57 if (!n) return 0;
58
59 if (!outleft) {
60 if (!++in[0]) if (!++in[1]) if (!++in[2]) ++in[3];
61 surf();
62 outleft = 8;
63 }
64
65 return out[--outleft] % n;
66}
67
69
70/* All needed for cryptography random functions, taken from djbdns */
71
72void randombytes(uint8 *x,unsigned long long xlen)
73{
74 int i;
75
76 while (xlen > 0) {
77 if (xlen < 1048576) i = xlen;
78 else i = 1048576;
79
80 i = read(global_urandom_fd,x,i);
81 if (i < 1) { sleep(1); continue; }
82
83 x += i;
84 xlen -= i;
85 }
86}
87
89{
90 global_urandom_fd = open_read("/dev/urandom");
91 if (global_urandom_fd < 0) {
92 log_anon("opening /dev/urandom failed");
93 return 0;
94 }
95 randombytes((uint8 *) in,sizeof(in));
96 return 1;
97}
98
99unsigned int crypto_random(unsigned int n)
100{
101 if (!n) return 0;
102
103 if (!outleft) {
104 if (!++in[0]) if (!++in[1]) if (!++in[2]) ++in[3];
105 surf();
106 outleft = 8;
107 }
108
109 return out[--outleft] % n;
110}
char data[32767]
Definition: axfrdns.c:133
int crypto_random_init(void)
Definition: dns_random.c:88
void surf(void)
Definition: dns_random.c:17
unsigned int dns_random(unsigned int n)
Definition: dns_random.c:55
int global_urandom_fd
Definition: dns_random.c:68
unsigned int crypto_random(unsigned int n)
Definition: dns_random.c:99
void randombytes(uint8 *x, unsigned long long xlen)
Definition: dns_random.c:72
void dns_random_init(const char data[128])
Definition: dns_random.c:36
#define MUSH(i, b)
Definition: dns_random.c:15
struct tcpclient t[QUERY_MAXTCP]
struct line * x