262 lines
5.9 KiB
C
262 lines
5.9 KiB
C
#include "rsa.h"
|
|
|
|
void set_random_bits(int *n, int size) {
|
|
int fd = open("/dev/urandom", O_RDONLY);
|
|
read(fd, n, size);
|
|
}
|
|
|
|
void set_msb_and_lsb_to_one(int *n, int size) {
|
|
n[0] |= 1;
|
|
n[size / sizeof(int) - 1] |= 1 << 31;
|
|
}
|
|
|
|
/*
|
|
int *random_bits(int n) {
|
|
int fd = open("/dev/urandom", O_RDONLY);
|
|
ft_log(INFO, "fd");
|
|
ft_log(INFO, ft_itoa(fd));
|
|
int *random_bits = (int *)malloc(n >> 3);
|
|
if (!random_bits) {
|
|
ft_log(ERROR, "allocation failed on random_bits");
|
|
exit(1);
|
|
}
|
|
ft_log(INFO, "before read");
|
|
read(fd, random_bits, n >> 3, 0);
|
|
ft_log(INFO, "after read");
|
|
// set n bits to random values
|
|
// for (int i = 0; i < n >> 5; i++) {
|
|
// random_bits[i] = rand();
|
|
// }
|
|
// set LSB and MSB to 1
|
|
random_bits[0] |= 1;
|
|
printf("last %ud\n", random_bits[(n / sizeof(int) >> 3) - 1]);
|
|
random_bits[(n / sizeof(int) >> 3) - 1] |= 0x80000000;
|
|
printf("last %ud\n", random_bits[(n / sizeof(int) >> 3) - 1]);
|
|
return random_bits;
|
|
}
|
|
*/
|
|
|
|
/*
|
|
void bitshift_array(int *a, int len) {
|
|
for (int n = 0; n < len - 1; n++) {
|
|
a[n] = a[n] >> 1 | (a[n + 1] & 1) << 31;
|
|
}
|
|
a[len - 1] >>= 1;
|
|
}
|
|
*/
|
|
|
|
void array_bitwise_right_shift(int *a, int len) {
|
|
int size = sizeof(int) * 8 - 1;
|
|
for (int n = 0; n < len - 1; n++) {
|
|
a[n] = a[n] >> 1 | (a[n + 1] & 1) << size;
|
|
}
|
|
a[len - 1] >>= 1;
|
|
}
|
|
|
|
void array_bitwise_left_shift(int *a, int len) {
|
|
int size = sizeof(int) * 8 - 1;
|
|
for (int n = len - 1; n > 0; n--) {
|
|
a[n] = a[n] << 1 | ((a[n - 1] & (1 << size)) >> size);
|
|
}
|
|
a[0] <<= 1;
|
|
}
|
|
|
|
// Will underflow
|
|
void array_decrement(int *a, int len) {
|
|
int cursor = 0;
|
|
int size = sizeof(int) * 8;
|
|
while (cursor < size * len) {
|
|
a[cursor / size] = a[cursor / size] ^ (1 << (cursor % size));
|
|
if ((a[cursor / size] >> (cursor % size)) & 1 == 0) {
|
|
return;
|
|
}
|
|
cursor += 1;
|
|
}
|
|
}
|
|
|
|
/*
|
|
int *generate_a(int *n) {
|
|
int *a = (int *)malloc(RSA_SIZE_BYTES >> 1);
|
|
memcpy(a, n, RSA_SIZE_BYTES >> 1);
|
|
a[0] -= 1;
|
|
int cursor = 0;
|
|
// a = n - 2
|
|
while (1) {
|
|
a[cursor / 32] = a[cursor / 32] ^ (1 << (cursor % 32));
|
|
if ((a[cursor / 32] >> (cursor % 32)) & 1 == 0)
|
|
break;
|
|
cursor++;
|
|
}
|
|
}
|
|
*/
|
|
|
|
int large_cmp(t_bigint *a, t_bitint *b) {
|
|
int size = sizeof(int) * 8;
|
|
int alen = a.size / sizeof(int);
|
|
int blen = b.size / sizeof(int);
|
|
int acursor = size * alen - 1;
|
|
int bcursor = size * alen - 1;
|
|
while (acursor > bcursor) {
|
|
if (a[acursor / size] & (1 << acursor % size)) {
|
|
return 1;
|
|
}
|
|
acursor -= 1;
|
|
}
|
|
while (bcursor > acursor) {
|
|
if (b[bcursor / size] & (1 << bcursor % size)) {
|
|
return -1;
|
|
}
|
|
bcursor -= 1;
|
|
}
|
|
int cursor = acursor;
|
|
while (cursor >= 0) {
|
|
int abit = a[cursor / size] & (1 << cursor % size);
|
|
int bbit = b[cursor / size] & (1 << cursor % size);
|
|
if (abit > bbit) {
|
|
return 1;
|
|
}
|
|
if (bbit > abit) {
|
|
return -1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
t_bigint bigint_clone(t_bigint src) {
|
|
t_bitint dst;
|
|
bigint.size = src.size;
|
|
bigint.data = (int *)protected_malloc(bigint.size, "bigint.data in bigint_clone");
|
|
memcpy(bigint.data, src.data, size);
|
|
}
|
|
|
|
int *bigint_sub(t_bigint *a, t_bigint *b) {
|
|
int *tmp = (int *)protected_malloc(size)
|
|
}
|
|
|
|
int *bigint_mod(t_bigint *a, t_bigint *b) {
|
|
int size = sizeof(int) * 8;
|
|
int alen = a.size / sizeof(int);
|
|
int blen = b.size / sizeof(int);
|
|
int acursor = size * alen - 1;
|
|
int bcursor = size * blen - 1;
|
|
t_bigint bclone = bigint_clone(b);
|
|
|
|
int cmp = large_cmp(a, b);
|
|
if (cmp == 0) {
|
|
|
|
}
|
|
while (!(a[acursor / size] & (1 << acursor % size))) {
|
|
acursor -= 1;
|
|
}
|
|
while (!(b[bcursor / size] & (1 << bcursor % size))) {
|
|
bcursor -= 1;
|
|
}
|
|
int bpow = 0;
|
|
if (acursor < bcursor) {
|
|
return a.data;
|
|
}
|
|
while (large_cmp(a, bclone) == 1) {
|
|
while (large_cmp(a, b) == 1) {
|
|
array_bitwise_left_shift(b, blen);
|
|
bcursor += 1;
|
|
bpow += 1;
|
|
}
|
|
array_bitwise_right_shift(b, blen);
|
|
bcursor -= 1;
|
|
bpow -= 1;
|
|
a.data = bigint_sub(a, b);
|
|
}
|
|
}
|
|
|
|
void generate_prime(int *n, int size) {
|
|
set_random_bits(n, size);
|
|
set_msb_and_lsb_to_one(n, size);
|
|
|
|
// n - 1 = 2^s*d
|
|
int *d = (int *)protected_malloc(size, "d in generate_prime");
|
|
d[0] -= 1;
|
|
int s = 0;
|
|
while (!(d[0] & 1)) {
|
|
s += 1;
|
|
array_bitwise_right_shift(d, size / sizeof(int));
|
|
}
|
|
|
|
|
|
}
|
|
|
|
void generate_prime(int *n) {
|
|
ft_log(INFO, "Generating random RSA_SIZE / 2 bits number");
|
|
int *prime = random_bits(RSA_SIZE >> 1);
|
|
ft_log(INFO, "After random_bits");
|
|
printf("sizeof int : %d\n", sizeof(int));
|
|
for (int i = ((RSA_SIZE_BYTES / sizeof(int)) >> 1) - 1; i > -1; i--) {
|
|
printf("%ud\n", prime[i]);
|
|
}
|
|
int *d;
|
|
int s = 0;
|
|
ft_log(INFO, "Creating n - 1");
|
|
d = (int *)malloc(RSA_SIZE_BYTES >> 1);
|
|
memcpy(d, prime, RSA_SIZE_BYTES >> 1);
|
|
// d = n - 1 (n's LSB is guarrenteed to be 1)
|
|
d[0] -= 1;
|
|
for (int i = (RSA_SIZE_BYTES / sizeof(int) >> 1) - 1; i > -1; i--) {
|
|
printf("%ud\n", d[i]);
|
|
}
|
|
ft_log(INFO, "Factoriizing n - 1 as 2^s*d");
|
|
while (!(d[0] & 1)) {
|
|
s += 1;
|
|
bitshift_array(d, RSA_SIZE_BYTES / sizeof(int) >> 1);
|
|
}
|
|
for (int k = 0; k < 128; k++) {
|
|
// int *a = generate_a(prime);
|
|
}
|
|
}
|
|
|
|
int *ft_phi(int *p, int *q) {
|
|
return (int *)malloc(sizeof(int));
|
|
}
|
|
|
|
t_rsa rsa_allocate(int block_size) {
|
|
t_rsa rsa;
|
|
|
|
rsa.p.size = block_size;
|
|
rsa.q.size = block_size;
|
|
rsa.n.size = 2 * block_size;
|
|
rsa.e.size = block_size;
|
|
|
|
rsa.p.data = (int *)protected_malloc(rsa.p.size, "rsa.p.size");
|
|
rsa.q.data = (int *)protected_malloc(rsa.q.size, "rsa.q.size");
|
|
rsa.n.data = (int *)protected_malloc(rsa.n.size, "rsa.n.size");
|
|
rsa.e.data = (int *)protected_malloc(rsa.e.size, "rsa.e.size");
|
|
for (int i = 0; i < block_size / sizeof(int)) {
|
|
rsa.p.data[i] = 0;
|
|
rsa.q.data[i] = 0;
|
|
rsa.e.data[i] = 0;
|
|
}
|
|
for (int i = 0; i < 2 * block_size / sizeof(int)) {
|
|
rsa.n.data[i] = 0;
|
|
}
|
|
}
|
|
|
|
t_rsa rsa_new(int block_size) {
|
|
t_rsa rsa;
|
|
|
|
// Convert block_size to bytes
|
|
block_size /= 8;
|
|
|
|
rsa = rsa_allocate(block_size);
|
|
generate_prime(rsa.p.data, block_size / 2);
|
|
generate_prime(rsa.q.data, block_size / 2);
|
|
return rsa;
|
|
}
|
|
|
|
void generate_keys(int *p, int *q, int *e) {
|
|
ft_log(INFO, "Generating primes...");
|
|
p = 0;
|
|
q = 0;
|
|
e = 0;
|
|
generate_prime(p);
|
|
generate_prime(q);
|
|
int *phi = ft_phi(p, q);
|
|
}
|