/* * Generate a random number with the ISAAC CSPRNG of * given size * * out output array of with size >= given size * size the size of the random number to generate in bytes */ void isaac_rand(unsigned char *out, const int size) { int i; if(initRand !=1){ isaac_init(); initRand = 1; } for(i = 0; ((size)/32) > i; i++){ if(count < 1){ isaac(); count = 256; } count--; memcpy(out+4*i, randrsl+count, 4); } if(size % 32 > 0){ if(count < 1){ isaac(); count = 256; } count--; memcpy(out+4*i, randrsl+count, (size-1)/8+1); } }
uint32_t randomIsaac::getNext( void ) { this->randomPoolCount--; if ( this->randomPoolCount < 0 ) { isaac(); this->randomPoolCount = poolSize - 1; } return this->randomPool[this->randomPoolCount]; }
/* * Initialize the isaac CSPRNG with a seed */ void isaac_init(void) { srand ( time(NULL) ); randrsl[0] = rand(); //not secure, but good for now randrsl[1] = rand(); randrsl[2] = rand(); randrsl[3] = rand(); randrsl[4] = rand(); randrsl[5] = rand(); randinit(1); isaac(); count = 256; }
/* if (flag==TRUE), then use the contents of randrsl[] to initialize mm[]. */ void randinit(randctx *ctx, word flag) { word i; ub4 a,b,c,d,e,f,g,h; ub4 *m,*r; ctx->randa = ctx->randb = ctx->randc = 0; m=ctx->randmem; r=ctx->randrsl; a=b=c=d=e=f=g=h=0x9e3779b9; /* the golden ratio */ for (i=0; i<4; ++i) /* scramble it */ { mix(a,b,c,d,e,f,g,h); } if (flag) { /* initialize using the contents of r[] as the seed */ for (i=0; i<RANDSIZ; i+=8) { a+=r[i ]; b+=r[i+1]; c+=r[i+2]; d+=r[i+3]; e+=r[i+4]; f+=r[i+5]; g+=r[i+6]; h+=r[i+7]; mix(a,b,c,d,e,f,g,h); m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d; m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h; } /* do a second pass to make all of the seed affect all of m */ for (i=0; i<RANDSIZ; i+=8) { a+=m[i ]; b+=m[i+1]; c+=m[i+2]; d+=m[i+3]; e+=m[i+4]; f+=m[i+5]; g+=m[i+6]; h+=m[i+7]; mix(a,b,c,d,e,f,g,h); m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d; m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h; } } else { /* fill in mm[] with messy stuff */ for (i=0; i<RANDSIZ; i+=8) { mix(a,b,c,d,e,f,g,h); m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d; m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h; } } isaac(ctx); /* fill in the first set of results */ ctx->randcnt=RANDSIZ; /* prepare to use the first set of results */ }
int main() { ub4 i,j; randctx ctx; ctx.randa=ctx.randb=ctx.randc=(ub4)0; for (i=0; i<256; ++i) ctx.randrsl[i]=(ub4)0; randinit(&ctx, TRUE); for (i=0; i<2; ++i) { isaac(&ctx); for (j=0; j<256; ++j) { printf("%.8lx",ctx.randrsl[j]); if ((j&7)==7) printf("\n"); } } }
void QTIsaac<ALPHA,T>::randinit(randctx* ctx, bool bUseSeed) { T a,b,c,d,e,f,g,h; a = b = c = d = e = f = g = h = GOLDEN_RATIO; T* m = (ctx->randmem); T* r = (ctx->randrsl); if(!bUseSeed) { ctx->randa = 0; ctx->randb = 0; ctx->randc = 0; } // scramble it for(int i=0; i < 4; ++i) { shuffle(a,b,c,d,e,f,g,h); } if(bUseSeed) { // initialize using the contents of r[] as the seed for(i=0; i < N; i+=8) { a+=r[i ]; b+=r[i+1]; c+=r[i+2]; d+=r[i+3]; e+=r[i+4]; f+=r[i+5]; g+=r[i+6]; h+=r[i+7]; shuffle(a,b,c,d,e,f,g,h); m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d; m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h; } //do a second pass to make all of the seed affect all of m for(i=0; i < N; i += 8) { a+=m[i ]; b+=m[i+1]; c+=m[i+2]; d+=m[i+3]; e+=m[i+4]; f+=m[i+5]; g+=m[i+6]; h+=m[i+7]; shuffle(a,b,c,d,e,f,g,h); m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d; m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h; } } else { // fill in mm[] with messy stuff shuffle(a,b,c,d,e,f,g,h); m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d; m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h; } isaac(ctx); // fill in the first set of results ctx->randcnt = N; // prepare to use the first set of results }
inline T QTIsaac<ALPHA,T>::rand(void) { return(!m_rc.randcnt-- ? (isaac(&m_rc), m_rc.randcnt=(N-1), m_rc.randrsl[m_rc.randcnt]) : m_rc.randrsl[m_rc.randcnt]); }