Simple AES chat program
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

143 lignes
2.6 KiB

#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);
}