BOOL DH1080_Init() { unsigned char raw_buf[256], iniHash[33]; FILE *hRnd; hRnd = fopen("/dev/urandom", "rb"); // don't use /dev/random, it's a blocking device if(!hRnd) return FALSE; // #*#*#*#*#* RNG START #*#*#*#*#* if(fread(raw_buf, 1, sizeof(raw_buf), hRnd) < 128) /* At least 128 bytes of seeding */ { ZeroMemory(raw_buf, sizeof(raw_buf)); fclose(hRnd); return FALSE; } fclose(hRnd); sha_file(iniPath, (char *)iniHash); memXOR((char *)raw_buf+128, (char *)iniHash, 32); sha_file((char *)get_irssi_config(), (char *)iniHash); memXOR((char *)raw_buf+128, (char *)iniHash, 32); ZeroMemory(iniHash, sizeof(iniHash)); // first 128 byte in raw_buf: output from /dev/urandom // last 32 byte in raw_buf: SHA-256 digest from blow.ini and irssi.conf /* Seed and initialize ISAAC */ memcpy(csprng.randrsl, raw_buf, sizeof(raw_buf)); randinit(&csprng, TRUE); /* RNG END */ initb64(); mpz_init(b_prime1080); mpz_import(b_prime1080, DH1080_PRIME_BYTES, 1, 1, 0, 0, prime1080); return TRUE; }
// Input: priv_key = buffer of 200 bytes // pub_key = buffer of 200 bytes // Output: priv_key = Your private key // pub_key = Your public key int DH1080_gen(char *priv_key, char *pub_key) { unsigned char raw_buf[160], iniHash[33]; unsigned long seed; int len, iRet; big b_privkey, b_pubkey; csprng myRNG; FILE *hRnd; priv_key[0]='0'; priv_key[1]='\0'; pub_key[0]='0'; pub_key[1]='\0'; hRnd = fopen("/dev/urandom", "r"); // don't use /dev/random, it's a blocking device if(!hRnd) return 0; b_privkey=mirvar(0); b_pubkey=mirvar(0); // #*#*#*#*#* RNG START #*#*#*#*#* time((time_t *)&seed); seed ^= (long)hRnd << 16; if(fread(raw_buf, 1, sizeof(raw_buf), hRnd) < 32) { ZeroMemory(raw_buf, sizeof(raw_buf)); fclose(hRnd); mirkill(b_privkey); mirkill(b_pubkey); return 0; } fclose(hRnd); sha_file(iniPath, iniHash); memXOR(raw_buf+128, iniHash, 32); sha_file((unsigned char *)get_irssi_config(), iniHash); memXOR(raw_buf+128, iniHash, 32); ZeroMemory(iniHash, sizeof(iniHash)); // first 128 byte in raw_buf: output from /dev/urandom // last 32 byte in raw_buf: SHA-256 digest from blow.ini and irssi.conf seed *= (unsigned long)mip; strong_init(&myRNG, sizeof(raw_buf), raw_buf, (unsigned int)seed); strong_rng(&myRNG); strong_bigdig(&myRNG, 1080, 2, b_privkey); strong_kill(&myRNG); seed=0; // #*#*#*#*#* RNG END #*#*#*#*#* powltr(2, b_privkey, b_prime1080, b_pubkey); if(DH_verifyPubKey(b_pubkey)) { len=big_to_bytes(sizeof(raw_buf), b_privkey, raw_buf, FALSE); htob64(raw_buf, priv_key, len); len=big_to_bytes(sizeof(raw_buf), b_pubkey, raw_buf, FALSE); htob64(raw_buf, pub_key, len); iRet=1; } else iRet=0; ZeroMemory(raw_buf, sizeof(raw_buf)); mirkill(b_privkey); mirkill(b_pubkey); return iRet; }