From 985ecf50939beddbcc916def071d1f68b98e404c Mon Sep 17 00:00:00 2001 From: thajohns <0.442.26622.7tj@gmail.com> Date: Sat, 21 Dec 2019 01:29:46 -0500 Subject: [PATCH] Finished key generator --- keygen.c | 143 +++++++++++++++++++++++++++++++++++++++++++++++++++ randsource.c | 20 +++++++ randsource.h | 8 +++ sgprime.h | 4 +- 4 files changed, 173 insertions(+), 2 deletions(-) create mode 100644 keygen.c create mode 100644 randsource.c create mode 100644 randsource.h diff --git a/keygen.c b/keygen.c new file mode 100644 index 0000000..1dc7153 --- /dev/null +++ b/keygen.c @@ -0,0 +1,143 @@ + +#include "randsource.h" +#include "sgprime.h" + +#include +#include +#include + +#define RAND_SIZE 512 +#define CHUNK_SIZE 1 + +int main(int argc, char **argv) +{ + unsigned int threads; + unsigned int bits; + FILE *keyoutf; + + if (argc < 3 || argc > 4) + { + printf("Usage: %s [threads]\n", argv[0]); + return 0; + } + + if (argc == 3) + { + threads = 0; + } else + { + if (1 != sscanf(argv[3], "%u", &threads) || threads == 0) + { + printf("number of threads must be a positive integer\n"); + return 0; + } + + } + + if (1 != sscanf(argv[1], "%u", &bits) || bits == 0) + { + printf("number of bits must be a positive integer\n"); + return 0; + } + + keyoutf = fopen(argv[2], "w"); + if (keyoutf == NULL) + { + perror("could not open key out file: "); + return 1; + } + + printf("Key Generation Tool\n"); + + printf("generating a key with %u bits ...\n", bits); + + fflush(stdout); + + mpz_t l, p, seed, rc, g, t; + mpz_init(p); + mpz_init(l); + mpz_init(g); + mpz_init(t); + mpz_init(seed); + + mpz_set_ui(l, 0); + mpz_setbit(l, bits); + + unsigned long int offset, primorial; + void *difflist = make_difflist(l, &offset, &primorial); + + gmp_randstate_t rs; + gmp_randinit_default(rs); + + char *randbuff = malloc(RAND_SIZE); + if (fill_random(randbuff, RAND_SIZE)) + { + perror("unable to read randomness source: "); + return 2; + } + + mpz_import(seed, RAND_SIZE, 1, 1, 0, 0, randbuff); + gmp_randseed(rs, seed); + + free(randbuff); + + mpz_urandomb(p, rs, bits); + mpz_setbit(p, bits); + + printf("generating safe prime ... "); + fflush(stdout); + get_sg_prime(difflist, offset, primorial, p, threads, bits + 4, CHUNK_SIZE); + printf("done\n"); + fflush(stdout); + + destroy_difflist(difflist); + + mpz_mul_ui(l, p, 2); + + rc[0] = seed[0]; // Moving variable names -- not portable + mpz_set(rc, l); + + mpz_add_ui(l, l, 1); + + printf("finding a generator ... "); + fflush(stdout); + while (1) + { + mpz_urandomm(g, rs, rc); + mpz_add_ui(rc, rc, 1); + mpz_powm_ui(t, g, 2, l); + if (mpz_cmp_ui(g, 1) != 0) + { + mpz_powm(t, g, p, l); + if (mpz_cmp_ui(g, 1) != 0) + break; + } + } + printf("done\n"); + fflush(stdout); + + printf("creating a secret exponent ... "); + fflush(stdout); + mpz_urandomm(p, rs, l); + printf("done\n"); + fflush(stdout); + + printf("computing public point ... "); + fflush(stdout); + mpz_powm(rc, g, p, l); + printf("done\n"); + fflush(stdout); + + gmp_randclear(rs); + + gmp_fprintf(keyoutf, "m=%Zd\ng=%Zd\nx=%Zd\np=%Zd\n", l, g, p, rc); + + mpz_clear(seed); + mpz_clear(p); + mpz_clear(l); + mpz_clear(g); + mpz_clear(t); + + fclose(keyoutf); +} + diff --git a/randsource.c b/randsource.c new file mode 100644 index 0000000..c64a97c --- /dev/null +++ b/randsource.c @@ -0,0 +1,20 @@ +// Change this file if your system uses a different randomness source. + +#include +#include +#include + +int fill_random(void *buf, int size) +{ + FILE *random = fopen("/dev/urandom", "r"); + + if (!random) + { + return errno; + } + + fread(buf, size, 1, random); + fclose(random); + return 0; +} + diff --git a/randsource.h b/randsource.h new file mode 100644 index 0000000..63f7f5a --- /dev/null +++ b/randsource.h @@ -0,0 +1,8 @@ + +#ifndef RANDSOURCE_H +#define RANDSOURCE_H + +int fill_random(void *buf, int size); + +#endif + diff --git a/sgprime.h b/sgprime.h index d070759..a6c5f70 100644 --- a/sgprime.h +++ b/sgprime.h @@ -3,8 +3,8 @@ #include -void *make_difflist(mpz_t lower_bound, unsigned long int *offset); -mpz_t get_sg_prime(void *difflist, unsigned long int offset, unsigned long int primorial, mpz_t start_search, unsigned int threads, unsigned int reps, unsigned int chunksize) +void *make_difflist(mpz_t lower_bound, unsigned long int *offset, unsigned long int *primorial); +void get_sg_prime(void *difflist, unsigned long int offset, unsigned long int primorial, mpz_t start_search, unsigned int threads, unsigned int reps, unsigned int chunksize); void destroy_difflist(void *difflist);