created generate_prime fn

it uses gmp 😎
This commit is contained in:
Václav Šmejkal 2024-02-23 10:24:38 +01:00
parent 2beb4401ee
commit df4f7aae7d
Signed by: ENGO150
GPG Key ID: 4A57E86482968843

View File

@ -16,4 +16,31 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <why2/chat/crypto.h>
#include <why2/chat/crypto.h>
#include <stdio.h>
#include <sys/random.h>
#include <gmp.h>
//LOCAL
void generate_prime(mpz_t x)
{
//RANDOM
gmp_randstate_t state;
gmp_randinit_default(state);
unsigned long random_buffer; //SEED
do
{
if (getrandom(&random_buffer, sizeof(unsigned long), GRND_NONBLOCK) == -1) why2_die("getrandom fn failed!");
//GENERATE RANDOM PRIME USING random_buffer SEED
gmp_randseed_ui(state, random_buffer);
mpz_urandomb(x, state, WHY2_CHAT_KEY_BITS);
mpz_nextprime(x, x);
} while (mpz_probab_prime_p(x, WHY2_CHAT_PRIME_ITERS) == 0); //CHECK FOR PRIME PROBABILITY
//DEALLOCATION
gmp_randclear(state);
}