46 lines
1.2 KiB
C
46 lines
1.2 KiB
C
#include "rsa.h"
|
|
|
|
rsa_t rsa_init(size_t len, bigint_t *primes) {
|
|
rsa_t rsa;
|
|
|
|
printf("Generating two primes of length %d bits\n", RSA_BLOCK_SIZE / 2);
|
|
//printf("Generating p...\n");
|
|
rsa.p = bigint_prime(len / 2, primes);
|
|
printf("p = %lu\n", ((uint64_t)rsa.p.data[1] << 32) + (uint64_t)rsa.p.data[0]);
|
|
//printf("p = %u\n", rsa.p.data[0]);
|
|
//printf("Generating q...\n");
|
|
rsa.q = bigint_prime(len / 2, primes);
|
|
printf("q = %lu\n", ((uint64_t)rsa.q.data[1] << 32) + (uint64_t)rsa.q.data[0]);
|
|
//printf("q = %u\n", rsa.q.data[0]);
|
|
|
|
|
|
return rsa;
|
|
}
|
|
|
|
rsa_t rsa_generate_keys(size_t block_size) {
|
|
size_t len = block_size / sizeof(uint32_t) / 8;
|
|
bigint_t *primes = (bigint_t *)protected_malloc(3245 * sizeof(bigint_t));
|
|
for (int i = 0; i < 3245; i++) {
|
|
primes[i] = bigint_zero(len);
|
|
}
|
|
int fd = open("primes.0000", O_RDONLY);
|
|
char *buf = (char *)malloc(21290 * sizeof(char));
|
|
int ret = read(fd, buf, 21290);
|
|
char *tok = strtok(buf, "\n");
|
|
int i = 0;
|
|
while (tok) {
|
|
primes[i].data[0] = (uint32_t)atoi(tok);
|
|
tok = strtok(NULL, "\n");
|
|
i += 1;
|
|
}
|
|
primes[0].data[0] = 65537;
|
|
printf("ret %d\n", ret);
|
|
|
|
rsa_t rsa = rsa_init(len, primes);
|
|
bigint_destroy(rsa.p);
|
|
bigint_destroy(rsa.q);
|
|
|
|
return rsa;
|
|
}
|
|
|