193 lines
4.2 KiB
C
193 lines
4.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;
|
|
}
|
|
|
|
int64_t euler(int64_t r0, int64_t r1) {
|
|
int64_t s0 = 1;
|
|
int64_t s1 = 0;
|
|
int64_t t0 = 0;
|
|
int64_t t1 = 1;
|
|
int64_t q0 = 0;
|
|
|
|
//printf(""
|
|
//printf("|% 20d|% 20ld|% 20ld|% 20ld|\n", 0, r0, s0, t0);
|
|
//printf("|% 20d|% 20ld|% 20ld|% 20ld|\n", 0, r1, s1, t1);
|
|
|
|
while (r1 != 0) {
|
|
q0 = r0 / r1;
|
|
int64_t tmp = r0 % r1;
|
|
r0 = r1;
|
|
r1 = tmp;
|
|
tmp = s0 - q0 * s1;
|
|
s0 = s1;
|
|
s1 = tmp;
|
|
tmp = t0 - q0 * t1;
|
|
t0 = t1;
|
|
t1 = tmp;
|
|
//printf("|% 20ld|% 20ld|% 20ld|% 20ld|\n", q0, r1, s1, t1);
|
|
}
|
|
return s0;
|
|
}
|
|
|
|
int64_t euler2(int64_t r0, int64_t r1) {
|
|
int64_t s0 = 1;
|
|
int64_t s1 = 0;
|
|
int64_t t0 = 0;
|
|
int64_t t1 = 1;
|
|
int64_t q0 = 0;
|
|
|
|
//printf(""
|
|
printf("|% 20d|% 20ld|% 20ld|% 20ld|\n", 0, r0, s0, t0);
|
|
printf("|% 20d|% 20ld|% 20ld|% 20ld|\n", 0, r1, s1, t1);
|
|
|
|
while (r1 != 0) {
|
|
q0 = r0 / r1;
|
|
int64_t tmp = r0 % r1;
|
|
r0 = r1;
|
|
r1 = tmp;
|
|
tmp = s0 - q0 * s1;
|
|
s0 = s1;
|
|
s1 = tmp;
|
|
tmp = t0 - q0 * t1;
|
|
t0 = t1;
|
|
t1 = tmp;
|
|
printf("|% 20ld|% 20ld|% 20ld|% 20ld|\n", q0, r1, s1, t1);
|
|
}
|
|
return t0;
|
|
}
|
|
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);
|
|
|
|
//int64_t p = 56843;//(uint64_t)generate_prime();
|
|
//int64_t q = 61861;//(uint64_t)generate_prime();
|
|
//int64_t p = 36671;
|
|
//int64_t q = 53939;
|
|
//int64_t p = 57313;
|
|
//int64_t q = 51329;
|
|
//int64_t p = 39901;
|
|
//int64_t q = 43391;
|
|
|
|
// int fd2 = open("/dev/urandom", O_RDONLY);
|
|
for (int try = 0; try < 1000; try++) {
|
|
|
|
if (try % 100 == 0)
|
|
printf("try: %d\n", try);
|
|
int64_t p = (uint64_t)generate_prime();
|
|
int64_t q = (uint64_t)generate_prime();
|
|
//p = 63761;
|
|
//q = 65003;
|
|
int64_t ln = (p - 1) * (q - 1);
|
|
int64_t e = 11317;
|
|
//e = 11;
|
|
|
|
while (ln % e == 0 || p == q) {
|
|
p = generate_prime();
|
|
q = generate_prime();
|
|
ln = (p - 1) * (q - 1);
|
|
}
|
|
|
|
if (q > p) {
|
|
uint64_t tmp = p;
|
|
p = q;
|
|
q = tmp;
|
|
}
|
|
int64_t n = p * q;
|
|
|
|
int64_t r0 = e;
|
|
int64_t r1 = ln;
|
|
int64_t s0 = 1;
|
|
int64_t s1 = 0;
|
|
int64_t t0 = 0;
|
|
int64_t t1 = 1;
|
|
int64_t q0 = 0;
|
|
|
|
while (r1 != 0) {
|
|
q0 = r0 / r1;
|
|
int64_t tmp = r0 % r1;
|
|
r0 = r1;
|
|
r1 = tmp;
|
|
tmp = s0 - q0 * s1;
|
|
s0 = s1;
|
|
s1 = tmp;
|
|
tmp = t0 - q0 * t1;
|
|
t0 = t1;
|
|
t1 = tmp;
|
|
}
|
|
|
|
int64_t d = euler(e, ln) + ln;
|
|
if (d > n) {
|
|
d -= ln;
|
|
}
|
|
|
|
/* printf("p: %ld\n", p);
|
|
printf("q: %ld\n", q);
|
|
printf("ln: %ld\n", ln);
|
|
printf("n: %ld\n", n);
|
|
printf("d: %ld\n", d);
|
|
printf("e: %ld\n", e);
|
|
printf("d * e %% ln = %ld\n", (d*e)%ln);*/
|
|
for (uint64_t m = 0; m < 16384; m++) {
|
|
//uint64_t m = get_random_bytes(fd2);
|
|
uint64_t c = pow_mod(m, e, n);
|
|
uint64_t m2 = pow_mod(c, d, n);
|
|
if (m != m2) {
|
|
printf("ERROR try: %d\nround: n/a\nmsg: %ld\ncypher: %ld\ndecrypted: %ld\nd: %ld\ne: %ld\np: %lu\nq: %lu\nn: %lu\n", try, m, c, m2, d, e, p, q, n);
|
|
break;
|
|
}
|
|
}
|
|
//int64_t m = 42;
|
|
/*
|
|
for (int64_t m = 41; m < 43; m++) {
|
|
int64_t c = pow_mod(m, e, n);
|
|
int64_t m2 = pow_mod(c, d, n);
|
|
if (d < 0) {
|
|
int64_t c2 = euler(c, n);
|
|
printf("c2: %ld\n", c2);
|
|
printf("c2 * c %% n = %ld\n", ((c2 + n)*c)%n);
|
|
printf("c2 * c %% n = %ld\n", ((c2)*c)%n);
|
|
m2 = pow_mod(c2 + n, -d, n);
|
|
}
|
|
printf("message: %ld\n", m);
|
|
printf("cypher: %ld\n", c);
|
|
printf("decrypted: %ld\n", m2);
|
|
}*/
|
|
}
|
|
return rsa;
|
|
}
|
|
|