void strong_bigrand(_MIPD_ csprng *rng,big w,big x) { int i, m; mr_small r; unsigned int ran; unsigned int ch; #ifdef MR_OS_THREADS miracl *mr_mip=get_mip(); #endif if (mr_mip->ERNUM) return; MR_IN(20) m = 0; zero(mr_mip->w1); do { m++; mr_mip->w1->len=m; for (r = 0, i = 0; i < sizeof(mr_small); i++) { ch=(unsigned char)strong_rng(rng); ran=ch; r = (r << 8) ^ ran; } if (mr_mip->base==0) mr_mip->w1->w[m-1]=r; else mr_mip->w1->w[m-1]=MR_REMAIN(r,mr_mip->base); } while (mr_compare(mr_mip->w1,w)<0); mr_lzero(mr_mip->w1); divide(_MIPP_ mr_mip->w1,w,w); copy(mr_mip->w1,x); MR_OUT }
/*! \brief Generate a random six digit one time password * * Generates a random six digit one time password * * @param seedValue random seed value * @param seedValueLength length of seedValue in bytes * @return OTP One Time Password */ AESGCM_EXPORT int generateOTP(char* seedValue, int seedValueLength) { int OTP=0; miracl *mip=mirsys(100,0); big bigOTP, modValue; bigOTP=mirvar(0); modValue=mirvar(1000000); /* Crypto string RNG */ csprng RNG; RNG = generateRNG(seedValue, seedValueLength); /* Generate randam 128 bit value */ int i; char val[16]; for (i=0; i<16; i++) val[i]=strong_rng(&RNG); /* Get the modulus */ bytes_to_big(16,val, bigOTP); divide(_MIPP_ bigOTP,modValue,modValue); OTP = size(bigOTP); return OTP; }
/*! \brief Generate value that is used for random seeding * * Generate value that is used for random seeding * * @param seedValue random seed value * @param seedValueLength length of seedValue in bytes */ AESGCM_EXPORT int generateSeedValue(char* seedValue, int seedValueLength) { int rtn=0; int i=0; /* Crypto string RNG */ csprng RNG; RNG = generateRNG(seedValue, seedValueLength); for (i=0; i<seedValueLength; i++) seedValue[i]=strong_rng(&RNG); return rtn; }
void strong_bigrand(_MIPD_ csprng *rng,big w,big x) { unsigned int ran; unsigned int ch; #ifndef MR_GENERIC_MT miracl *mr_mip=get_mip(); #endif if (mr_mip->ERNUM) return; MR_IN(139) zero(mr_mip->w1); do { if (mr_mip->ERNUM) break; ch=(unsigned char)strong_rng(rng); ran=ch; premult(_MIPP_ mr_mip->w1,256,mr_mip->w1); incr(_MIPP_ mr_mip->w1,(int)ran,mr_mip->w1); } while (compare(mr_mip->w1,w)<0); divide(_MIPP_ mr_mip->w1,w,w); copy(mr_mip->w1,x); MR_OUT }
// 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; }
/* * function encodeByteArrayToPoint : Encodes the given byte array into a point. * If the given byte array can not be encoded to a point, returns 0. * param dlog : Pointer to the native Dlog object. * param binaryString : The byte array to encode. * param k : k is the maximum length of a string to be converted to a Group Element of this group. * If a string exceeds the k length it cannot be converted. * return : The created point or 0 if the point cannot be created. */ JNIEXPORT jlong JNICALL Java_edu_biu_scapi_primitives_dlog_miracl_MiraclDlogECFp_encodeByteArrayToPoint (JNIEnv * env, jobject, jlong m, jbyteArray binaryString, jint k){ //Pseudo-code: /*If the length of binaryString exceeds k then throw IndexOutOfBoundsException. Let L be the length in bytes of p Choose a random byte array r of length L – k – 2 bytes Prepare a string newString of the following form: r || binaryString || binaryString.length (where || denotes concatenation) (i.e., the least significant byte of newString is the length of binaryString in bytes) Convert the result to a BigInteger (bIString) Compute the elliptic curve equation for this x and see if there exists a y such that (x,y) satisfies the equation. If yes, return (x,y) Else, go back to step 3 (choose a random r etc.) up to 80 times (This is an arbitrary hard-coded number). If did not find y such that (x,y) satisfies the equation after 80 trials then return null. */ /* convert the accepted parameters to MIRACL parameters*/ miracl* mip = (miracl*)m; jbyte* string = (jbyte*) env->GetByteArrayElements(binaryString, 0); int len = env->GetArrayLength(binaryString); if (len > k){ env ->ReleaseByteArrayElements(binaryString, string, 0); return 0; } big x, p; x = mirvar(mip, 0); p = mip->modulus; int l = logb2(mip, p)/8; char* randomArray = new char[l-k-2]; char* newString = new char[l - k - 1 + len]; memcpy(newString+l-k-2, string, len); newString[l - k - 2 + len] = (char) len; int counter = 0; bool success = 0; csprng rng; srand(time(0)); long seed; char raw = rand(); time((time_t*)&seed); strong_init(&rng,1,&raw,seed); do{ for (int i=0; i<l-k-2; i++){ randomArray[i] = strong_rng(&rng); } memcpy(newString, randomArray, l-k-2); bytes_to_big(mip, l - k - 1 + len, newString, x); //If the number is negative, make it positive. if(exsign(x)== -1){ absol(x, x); } //epoint_x returns true if the given x value leads to a valid point on the curve. //if failed, go back to choose a random r etc. success = epoint_x(mip, x); counter++; } while((!success) && (counter <= 80)); //we limit the amount of times we try to 80 which is an arbitrary number. epoint* point = 0; if (success){ point = epoint_init(mip); epoint_set(mip, x, x, 0, point); } char* temp = new char[l - k - 1 + len]; big_to_bytes(mip,l - k - 1 + len , x, temp, 1); //Delete the allocated memory. env ->ReleaseByteArrayElements(binaryString, string, 0); mirkill(x); delete(randomArray); delete(newString); //Return the created point. return (long) point; }