3
|
1 /* ---- PRNG Stuff ---- */ |
|
2 struct yarrow_prng { |
|
3 int cipher, hash; |
|
4 unsigned char pool[MAXBLOCKSIZE]; |
|
5 symmetric_CTR ctr; |
|
6 }; |
|
7 |
|
8 struct rc4_prng { |
|
9 int x, y; |
|
10 unsigned char buf[256]; |
|
11 }; |
|
12 |
143
|
13 struct fortuna_prng { |
|
14 hash_state pool[FORTUNA_POOLS]; /* the pools */ |
|
15 |
|
16 symmetric_key skey; |
|
17 |
|
18 unsigned char K[32], /* the current key */ |
|
19 IV[16]; /* IV for CTR mode */ |
|
20 |
|
21 unsigned long pool_idx, /* current pool we will add to */ |
|
22 pool0_len, /* length of 0'th pool */ |
|
23 wd; |
|
24 |
|
25 ulong64 reset_cnt; /* number of times we have reset */ |
|
26 }; |
|
27 |
|
28 struct sober128_prng { |
|
29 ulong32 R[17], /* Working storage for the shift register */ |
|
30 initR[17], /* saved register contents */ |
|
31 konst, /* key dependent constant */ |
|
32 sbuf; /* partial word encryption buffer */ |
|
33 |
|
34 int nbuf, /* number of part-word stream bits buffered */ |
|
35 flag, /* first add_entropy call or not? */ |
|
36 set; /* did we call add_entropy to set key? */ |
|
37 |
|
38 }; |
|
39 |
3
|
40 typedef union Prng_state { |
143
|
41 #ifdef YARROW |
3
|
42 struct yarrow_prng yarrow; |
143
|
43 #endif |
|
44 #ifdef RC4 |
3
|
45 struct rc4_prng rc4; |
143
|
46 #endif |
|
47 #ifdef FORTUNA |
|
48 struct fortuna_prng fortuna; |
|
49 #endif |
|
50 #ifdef SOBER128 |
|
51 struct sober128_prng sober128; |
|
52 #endif |
3
|
53 } prng_state; |
|
54 |
|
55 extern struct _prng_descriptor { |
|
56 char *name; |
143
|
57 int export_size; /* size in bytes of exported state */ |
3
|
58 int (*start)(prng_state *); |
|
59 int (*add_entropy)(const unsigned char *, unsigned long, prng_state *); |
|
60 int (*ready)(prng_state *); |
143
|
61 unsigned long (*read)(unsigned char *, unsigned long, prng_state *); |
|
62 int (*done)(prng_state *); |
|
63 int (*pexport)(unsigned char *, unsigned long *, prng_state *); |
|
64 int (*pimport)(const unsigned char *, unsigned long, prng_state *); |
|
65 int (*test)(void); |
3
|
66 } prng_descriptor[]; |
|
67 |
|
68 #ifdef YARROW |
143
|
69 int yarrow_start(prng_state *prng); |
|
70 int yarrow_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng); |
|
71 int yarrow_ready(prng_state *prng); |
|
72 unsigned long yarrow_read(unsigned char *buf, unsigned long len, prng_state *prng); |
|
73 int yarrow_done(prng_state *prng); |
|
74 int yarrow_export(unsigned char *out, unsigned long *outlen, prng_state *prng); |
|
75 int yarrow_import(const unsigned char *in, unsigned long inlen, prng_state *prng); |
|
76 int yarrow_test(void); |
|
77 extern const struct _prng_descriptor yarrow_desc; |
|
78 #endif |
|
79 |
|
80 #ifdef FORTUNA |
|
81 int fortuna_start(prng_state *prng); |
|
82 int fortuna_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng); |
|
83 int fortuna_ready(prng_state *prng); |
|
84 unsigned long fortuna_read(unsigned char *buf, unsigned long len, prng_state *prng); |
|
85 int fortuna_done(prng_state *prng); |
|
86 int fortuna_export(unsigned char *out, unsigned long *outlen, prng_state *prng); |
|
87 int fortuna_import(const unsigned char *in, unsigned long inlen, prng_state *prng); |
|
88 int fortuna_test(void); |
|
89 extern const struct _prng_descriptor fortuna_desc; |
3
|
90 #endif |
|
91 |
|
92 #ifdef RC4 |
143
|
93 int rc4_start(prng_state *prng); |
|
94 int rc4_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng); |
|
95 int rc4_ready(prng_state *prng); |
|
96 unsigned long rc4_read(unsigned char *buf, unsigned long len, prng_state *prng); |
|
97 int rc4_done(prng_state *prng); |
|
98 int rc4_export(unsigned char *out, unsigned long *outlen, prng_state *prng); |
|
99 int rc4_import(const unsigned char *in, unsigned long inlen, prng_state *prng); |
|
100 int rc4_test(void); |
|
101 extern const struct _prng_descriptor rc4_desc; |
3
|
102 #endif |
|
103 |
|
104 #ifdef SPRNG |
143
|
105 int sprng_start(prng_state *prng); |
|
106 int sprng_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng); |
|
107 int sprng_ready(prng_state *prng); |
|
108 unsigned long sprng_read(unsigned char *buf, unsigned long len, prng_state *prng); |
|
109 int sprng_done(prng_state *prng); |
|
110 int sprng_export(unsigned char *out, unsigned long *outlen, prng_state *prng); |
|
111 int sprng_import(const unsigned char *in, unsigned long inlen, prng_state *prng); |
|
112 int sprng_test(void); |
|
113 extern const struct _prng_descriptor sprng_desc; |
3
|
114 #endif |
|
115 |
143
|
116 #ifdef SOBER128 |
|
117 int sober128_start(prng_state *prng); |
|
118 int sober128_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng); |
|
119 int sober128_ready(prng_state *prng); |
|
120 unsigned long sober128_read(unsigned char *buf, unsigned long len, prng_state *prng); |
|
121 int sober128_done(prng_state *prng); |
|
122 int sober128_export(unsigned char *out, unsigned long *outlen, prng_state *prng); |
|
123 int sober128_import(const unsigned char *in, unsigned long inlen, prng_state *prng); |
|
124 int sober128_test(void); |
|
125 extern const struct _prng_descriptor sober128_desc; |
|
126 #endif |
3
|
127 |
143
|
128 int find_prng(const char *name); |
|
129 int register_prng(const struct _prng_descriptor *prng); |
|
130 int unregister_prng(const struct _prng_descriptor *prng); |
|
131 int prng_is_valid(int idx); |
3
|
132 |
|
133 /* Slow RNG you **might** be able to use to seed a PRNG with. Be careful as this |
|
134 * might not work on all platforms as planned |
|
135 */ |
143
|
136 unsigned long rng_get_bytes(unsigned char *buf, |
3
|
137 unsigned long len, |
|
138 void (*callback)(void)); |
|
139 |
143
|
140 int rng_make_prng(int bits, int wprng, prng_state *prng, void (*callback)(void)); |
3
|
141 |