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