From df4f7aae7dbce50fc9d761dc8701e6897870af91 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Fri, 23 Feb 2024 10:24:38 +0100 Subject: [PATCH] created generate_prime fn MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit it uses gmp 😎 --- src/chat/crypto.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/chat/crypto.c b/src/chat/crypto.c index 65033fc..742266d 100644 --- a/src/chat/crypto.c +++ b/src/chat/crypto.c @@ -16,4 +16,31 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#include \ No newline at end of file +#include + +#include +#include + +#include + +//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); +} \ No newline at end of file