瀏覽代碼

Finished key generator

master
thajohns 2 年前
父節點
當前提交
985ecf5093
  1. 143
      keygen.c
  2. 20
      randsource.c
  3. 8
      randsource.h
  4. 4
      sgprime.h

143
keygen.c

@ -0,0 +1,143 @@
#include "randsource.h"
#include "sgprime.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#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 <bits> <output file> [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);
}

20
randsource.c

@ -0,0 +1,20 @@
// Change this file if your system uses a different randomness source.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
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;
}

8
randsource.h

@ -0,0 +1,8 @@
#ifndef RANDSOURCE_H
#define RANDSOURCE_H
int fill_random(void *buf, int size);
#endif

4
sgprime.h

@ -3,8 +3,8 @@
#include <gmp.h>
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);

載入中…
取消
儲存