Pull fix from branch_1_1, so proxied EAP replies work
[freeradius.git] / src / lib / isaac.c
1 /*
2 ------------------------------------------------------------------------------
3 http://burtleburtle.net/bob/rand/isaac.html
4 rand.c: By Bob Jenkins.  My random number generator, ISAAC.  Public Domain
5 MODIFIED:
6   960327: Creation (addition of randinit, really)
7   970719: use context, not global variables, for internal state
8   980324: make a portable version
9   010626: Note this is public domain
10 ------------------------------------------------------------------------------
11 */
12
13 #include <freeradius-devel/ident.h>
14 RCSID("$Id$")
15
16 #include <freeradius-devel/autoconf.h>
17 #include <freeradius-devel/missing.h>
18 #include <freeradius-devel/libradius.h>
19
20 #define RANDSIZL   (8)  /* I recommend 8 for crypto, 4 for simulations */
21 #define RANDSIZ    (1<<RANDSIZL)
22
23 #define ind(mm,x)  ((mm)[(x>>2)&(RANDSIZ-1)])
24 #define rngstep(mix,a,b,mm,m,m2,r,x) \
25 { \
26   x = *m;  \
27   a = ((a^(mix)) + *(m2++)) & 0xffffffff; \
28   *(m++) = y = (ind(mm,x) + a + b) & 0xffffffff; \
29   *(r++) = b = (ind(mm,y>>RANDSIZL) + x) & 0xffffffff; \
30 }
31
32 void lrad_isaac(lrad_randctx *ctx)
33 {
34    register uint32_t a,b,x,y,*m,*mm,*m2,*r,*mend;
35    mm=ctx->randmem; r=ctx->randrsl;
36    a = ctx->randa; b = (ctx->randb + (++ctx->randc)) & 0xffffffff;
37    for (m = mm, mend = m2 = m+(RANDSIZ/2); m<mend; )
38    {
39       rngstep( a<<13, a, b, mm, m, m2, r, x);
40       rngstep( a>>6 , a, b, mm, m, m2, r, x);
41       rngstep( a<<2 , a, b, mm, m, m2, r, x);
42       rngstep( a>>16, a, b, mm, m, m2, r, x);
43    }
44    for (m2 = mm; m2<mend; )
45    {
46       rngstep( a<<13, a, b, mm, m, m2, r, x);
47       rngstep( a>>6 , a, b, mm, m, m2, r, x);
48       rngstep( a<<2 , a, b, mm, m, m2, r, x);
49       rngstep( a>>16, a, b, mm, m, m2, r, x);
50    }
51    ctx->randb = b; ctx->randa = a;
52 }
53
54
55 #define mix(a,b,c,d,e,f,g,h) \
56 { \
57    a^=b<<11; d+=a; b+=c; \
58    b^=c>>2;  e+=b; c+=d; \
59    c^=d<<8;  f+=c; d+=e; \
60    d^=e>>16; g+=d; e+=f; \
61    e^=f<<10; h+=e; f+=g; \
62    f^=g>>4;  a+=f; g+=h; \
63    g^=h<<8;  b+=g; h+=a; \
64    h^=a>>9;  c+=h; a+=b; \
65 }
66
67 /* if (flag==1), then use the contents of randrsl[] to initialize mm[]. */
68 void lrad_randinit(lrad_randctx *ctx, int flag)
69 {
70   int i;
71   uint32_t a,b,c,d,e,f,g,h;
72   uint32_t *m,*r;
73   ctx->randa = ctx->randb = ctx->randc = 0;
74   m=ctx->randmem;
75   r=ctx->randrsl;
76   a=b=c=d=e=f=g=h=0x9e3779b9;  /* the golden ratio */
77
78   for (i=0; i<4; ++i) {        /* scramble it */
79     mix(a,b,c,d,e,f,g,h);
80   }
81
82   if (flag) {
83     /* initialize using the contents of r[] as the seed */
84     for (i=0; i<RANDSIZ; i+=8) {
85       a+=r[i  ]; b+=r[i+1]; c+=r[i+2]; d+=r[i+3];
86       e+=r[i+4]; f+=r[i+5]; g+=r[i+6]; h+=r[i+7];
87       mix(a,b,c,d,e,f,g,h);
88       m[i  ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
89       m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
90     }
91      /* do a second pass to make all of the seed affect all of m */
92     for (i=0; i<RANDSIZ; i+=8) {
93       a+=m[i  ]; b+=m[i+1]; c+=m[i+2]; d+=m[i+3];
94       e+=m[i+4]; f+=m[i+5]; g+=m[i+6]; h+=m[i+7];
95       mix(a,b,c,d,e,f,g,h);
96       m[i  ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
97       m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
98     }
99   } else {
100     for (i=0; i<RANDSIZ; i+=8) {
101       /* fill in mm[] with messy stuff */
102       mix(a,b,c,d,e,f,g,h);
103       m[i  ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
104       m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
105     }
106   }
107
108   lrad_isaac(ctx);       /* fill in the first set of results */
109   ctx->randcnt=RANDSIZ;  /* prepare to use the first set of results */
110 }
111
112
113 #ifdef TEST
114 /*
115  *  For testing.  Output should be the same as
116  *
117  *  http://burtleburtle.net/bob/rand/randvect.txt
118  */
119 int main()
120 {
121   uint32_t i,j;
122   lrad_randctx ctx;
123
124   ctx.randa = ctx.randb = ctx.randc = (uint32_t)0;
125
126   for (i=0; i<256; ++i) ctx.randrsl[i]=(uint32_t)0;
127   lrad_randinit(&ctx, 1);
128   for (i=0; i<2; ++i) {
129     lrad_isaac(&ctx);
130     for (j=0; j<256; ++j) {
131       printf("%.8lx",ctx.randrsl[j]);
132       if ((j&7)==7) printf("\n");
133     }
134   }
135 }
136 #endif