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