CvStrongRng::CvStrongRng(CSRNG_TYPE type) { const int size = ( AES_SECURITY/sizeof(mr_small) ); String random; char seed[size]; #ifdef WIN32 if ( m_bEnableEntropy ) { CvEntropyServer( m_aEntropyServerUrl ).Generate( CvEntropyServer::StringToAlgorithm( m_aEntropyAlgorithm ), CvEntropyServer::enEncoding_Raw, size, random ); memcpy((void*)seed, (void*)random.data(), size); } #elif defined __linux__ FILE* fdRandom; if ( m_bEnableEntropy ) { CvEntropyServer( m_aEntropyServerUrl ).Generate( CvEntropyServer::StringToAlgorithm( m_aEntropyAlgorithm ), CvEntropyServer::enEncoding_Raw, size, random ); fdRandom = fmemopen( (void*)random.data(), size, "r" ); } else { fdRandom = fopen( "/dev/urandom", "r" ); } if ( fdRandom == NULL ) { mr_berror( _MIPP_ MR_ERR_DEV_RANDOM ); } for ( int i = 0; i < size; ++i ) { int c = fgetc( fdRandom ); seed[i] = c; if ( c == -1 ) { mr_berror( _MIPP_ MR_ERR_DEV_RANDOM ); } } if ( fdRandom != NULL ) { fclose( fdRandom ); } #endif time_t tod; time( &tod ); strong_init( &m_csprng, size, seed, tod ); }
//new interface CvStrongRng::CvStrongRng(CSRNG_MODE mode) : ISystemSource(new SystemCSPRNG()), IDongleSource(new CDongleSource()) { const int size = ( AES_SECURITY/sizeof(mr_small) ); char seed[size]; time_t tod; time( &tod ); strong_init( &m_csprng, size, seed, tod ); }
int main(int argc, char *argv[]) { mr_init_threading(); /************************************************* * Get the public parameters from the PKGs * **************************************************/ // Initialise a strong random number generator char raw[256]; FILE *fp; fp = fopen("/dev/urandom", "r"); fread(&raw, 1, 256, fp); fclose(fp); time_t seed; csprng rng; time(&seed); strong_init(&rng,strlen(raw),raw,(long)seed); // Make sure all random PFC elements rely on strong random number generator PFC pfc(AES_SECURITY, &rng); miracl *mip = get_mip(); int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1); mip->IOBASE=64; ThreadParams *myParams = new ThreadParams; G2 P; G2 Ppub; G1 Qpriv, Qid; G1 D; /************************************************* * Initialise client side socket for Scramble * **************************************************/ cout << "Socket is ready for Firefox requests. Listening:" << endl; int sockfd, newsockfd; socklen_t clilen; struct sockaddr_in serv_addr, cli_addr; // Initialise the socket descriptor. sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); // Bind socket to port serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(PORT_NB); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); // Listen to socket and accept incoming connections listen(sockfd,5); clilen = sizeof(cli_addr); *closeSocket = false; //mr_init_threading(); //TODO: make encryption multithreaded paramsInitialised = false; while(!(*closeSocket) ){ newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); if(paramsInitialised == false) { myParams->pfc = &pfc; bool extractionSucceeded = extractPrivateKey(myParams); if(extractionSucceeded == false) { cout << "Some DKGs were dishonest. Please select other DKGs." << endl; return 0; } P = myParams->P; D = myParams->D; Ppub = myParams->Ppub; paramsInitialised = true; } // Initialise new thread //pthread_t sniffer_thread; //pthread_attr_t attributes; ThreadParams *params = new ThreadParams; // params is a pointer to a threadParams struct //params = myParams; params->sockfd = newsockfd; params->pfc = &pfc; params->P = myParams->P; params->Ppub = myParams->Ppub; params->D = myParams->D; processXmlRequest(params); } //mr_end_threading(); sleep(5); return 0; }
// 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; }