Blob


1 #include "os.h"
2 #include <libsec.h>
4 typedef struct State{
5 QLock lock;
6 int seeded;
7 uvlong seed;
8 DES3state des3;
9 } State;
10 static State x917state;
12 static void
13 X917(uchar *rand, int nrand)
14 {
15 int i, m, n8;
16 uvlong I, x;
18 /* 1. Compute intermediate value I = Ek(time). */
19 I = nsec();
20 triple_block_cipher(x917state.des3.expanded, (uchar*)&I, 0); /* two-key EDE */
22 /* 2. x[i] = Ek(I^seed); seed = Ek(x[i]^I); */
23 m = (nrand+7)/8;
24 for(i=0; i<m; i++){
25 x = I ^ x917state.seed;
26 triple_block_cipher(x917state.des3.expanded, (uchar*)&x, 0);
27 n8 = (nrand>8) ? 8 : nrand;
28 memcpy(rand, (uchar*)&x, n8);
29 rand += 8;
30 nrand -= 8;
31 x ^= I;
32 triple_block_cipher(x917state.des3.expanded, (uchar*)&x, 0);
33 x917state.seed = x;
34 }
35 }
37 static void
38 X917init(void)
39 {
40 int n;
41 uchar mix[128];
42 uchar key3[3][8];
43 ulong *ulp;
45 ulp = (ulong*)key3;
46 for(n = 0; n < sizeof(key3)/sizeof(ulong); n++)
47 ulp[n] = truerand();
48 setupDES3state(&x917state.des3, key3, nil);
49 X917(mix, sizeof mix);
50 x917state.seeded = 1;
51 }
53 void
54 genrandom(uchar *p, int n)
55 {
56 qlock(&x917state.lock);
57 if(x917state.seeded == 0)
58 X917init();
59 X917(p, n);
60 qunlock(&x917state.lock);
61 }