ccryptographydiffie-hellman

C Library for atleast 256 bit Random number


I'm implementing Diffie-Hellman in C and want to use a 256 bit random number as a private key. I'm not sure if it is possible with the gmp library. So any other powerful library which can produce my requirement ?


Solution

  • Reading the gmp library manual for random number functions, we can see a provision to produce an output according to our requested size of bits.

    Modifying '14' in mpz_urandomb(rand_Num,r_state,14) acc to our req number of bits gives a rand number of that many bits(14->256). But even in for loop they produce the same set of random numbers.

    So, modify the 'i' limit (condition) in loop to rand() function, which depends upon srand() function, which in turn takes present system time as a seed value and ultimately produce different random numbers in rand(). So different iterations are produced every time and so the last number in the last iteration (with good randomness) ultimately sits in rand_Num variable.

     #include<time.h>
    

    // rest of the code as in manual//

     srand(time(0));
    
        for (i=0;i<rand();i++)
    
        {mpz_urandomb(rand_Num,r_state,256);} // for 256 bits random number
    
    
    
    
    enter code here