|
|
|
@ -1,14 +1,14 @@
|
|
|
|
|
#include "rsa.h"
|
|
|
|
|
|
|
|
|
|
void bigint_set_random_bytes(bigint_t n) {
|
|
|
|
|
void bigint_set_random_bytes(bigint_t n, size_t len) {
|
|
|
|
|
int fd = open("/dev/urandom", O_RDONLY);
|
|
|
|
|
read(fd, n.data, n.len * sizeof(uint32_t));
|
|
|
|
|
read(fd, n.data, len * sizeof(uint32_t));
|
|
|
|
|
close(fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bigint_set_msb_and_lsb_to_one(bigint_t n) {
|
|
|
|
|
void bigint_set_msb_and_lsb_to_one(bigint_t n, size_t len) {
|
|
|
|
|
n.data[0] |= 1;
|
|
|
|
|
n.data[n.len - 1] |= 1 << 31;
|
|
|
|
|
n.data[len - 1] |= 1 << 31;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bigint_bitwise_right_shift(bigint_t n) {
|
|
|
|
@ -51,61 +51,39 @@ void bigint_decrement(bigint_t n) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO refactor/clean assume same length ?
|
|
|
|
|
int bigint_cmp(bigint_t a, bigint_t b) {
|
|
|
|
|
uint32_t size = sizeof(uint32_t) * 8;
|
|
|
|
|
uint32_t acursor = size * a.len - 1;
|
|
|
|
|
uint32_t bcursor = size * b.len - 1;
|
|
|
|
|
while (acursor >= bcursor + size && a.data[acursor >> 5] == 0) {
|
|
|
|
|
acursor -= size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (acursor > bcursor) {
|
|
|
|
|
if (a.data[acursor >> 5] & (1 << acursor % size)) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
acursor -= 1;
|
|
|
|
|
}
|
|
|
|
|
while (bcursor >= acursor + size && b.data[bcursor / size] == 0) {
|
|
|
|
|
bcursor -= size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (bcursor > acursor) {
|
|
|
|
|
if (b.data[bcursor >> 5] & (1 << bcursor % size)) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
bcursor -= 1;
|
|
|
|
|
}
|
|
|
|
|
int cursor = acursor;
|
|
|
|
|
int64_t bigint_cmp(bigint_t a, bigint_t b) {
|
|
|
|
|
int cursor = a.len - 1;
|
|
|
|
|
while (cursor >= 0) {
|
|
|
|
|
if (a.data[cursor >> 5] > b.data[cursor >> 5]) {
|
|
|
|
|
if (a.data[cursor] > b.data[cursor]) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if (b.data[cursor >> 5] > a.data[cursor >> 5]) {
|
|
|
|
|
if (b.data[cursor] > a.data[cursor]) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
cursor -= size;
|
|
|
|
|
cursor -= 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO check opti
|
|
|
|
|
void bigint_substraction(bigint_t a, bigint_t bb) {
|
|
|
|
|
bigint_t b = bigint_clone(bb);
|
|
|
|
|
if (a.len > bb.len) {
|
|
|
|
|
bigint_destroy(b);
|
|
|
|
|
b = bigint_zero(a.len);
|
|
|
|
|
memcpy(b.data, bb.data, b.len * sizeof(uint32_t));
|
|
|
|
|
// TODO refactor/clean assume same length ?
|
|
|
|
|
int bigint_dif(bigint_t a, bigint_t b) {
|
|
|
|
|
int cursor = a.len - 1;
|
|
|
|
|
while (cursor >= 0) {
|
|
|
|
|
if (a.data[cursor] ^ b.data[cursor]) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if (a.len != b.len) {
|
|
|
|
|
printf("error: attempting to substract numbers of different length\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
cursor -= 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
// TODO check opti
|
|
|
|
|
void bigint_substraction(bigint_t a, bigint_t b) {
|
|
|
|
|
bigint_t borrow = bigint_clone(b);
|
|
|
|
|
bigint_t y = bigint_clone(b);
|
|
|
|
|
bigint_t zero = bigint_zero(a.len);
|
|
|
|
|
while (bigint_cmp(borrow, zero)) {
|
|
|
|
|
while (bigint_dif(borrow, zero)) {
|
|
|
|
|
for (size_t i = 0; i < a.len; i++) {
|
|
|
|
|
borrow.data[i] = ~a.data[i] & y.data[i];
|
|
|
|
|
a.data[i] = a.data[i] ^ y.data[i];
|
|
|
|
@ -113,7 +91,6 @@ void bigint_substraction(bigint_t a, bigint_t bb) {
|
|
|
|
|
bigint_destroy(y);
|
|
|
|
|
y = assignable_bigint_bitwise_left_shift(borrow);
|
|
|
|
|
}
|
|
|
|
|
bigint_destroy(b);
|
|
|
|
|
bigint_destroy(y);
|
|
|
|
|
bigint_destroy(borrow);
|
|
|
|
|
bigint_destroy(zero);
|
|
|
|
@ -129,21 +106,21 @@ void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result) {
|
|
|
|
|
mod = bigint_zero(a.len);
|
|
|
|
|
memcpy(mod.data, b.data, b.len * sizeof(uint32_t));
|
|
|
|
|
}
|
|
|
|
|
if (bigint_cmp(result, b) == -1) {
|
|
|
|
|
if (bigint_cmp(result, b) < 0) {
|
|
|
|
|
bigint_destroy(mod);
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
bigint_bitwise_left_shift(mod);
|
|
|
|
|
while (bigint_cmp(b, mod) == -1) {
|
|
|
|
|
while (bigint_cmp(result, mod) == 1) {
|
|
|
|
|
while (bigint_cmp(b, mod) < 0) {
|
|
|
|
|
while (bigint_cmp(result, mod) > 0) {
|
|
|
|
|
bigint_bitwise_left_shift(mod);
|
|
|
|
|
}
|
|
|
|
|
bigint_bitwise_right_shift(mod);
|
|
|
|
|
if (bigint_cmp(result, mod) == 1) {
|
|
|
|
|
if (bigint_cmp(result, mod) > 0) {
|
|
|
|
|
bigint_substraction(result, mod);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while (bigint_cmp(result, b) == 1) {
|
|
|
|
|
while (bigint_cmp(result, b) > 0) {
|
|
|
|
|
bigint_substraction(result, b);
|
|
|
|
|
}
|
|
|
|
|
bigint_destroy(mod);
|
|
|
|
@ -274,10 +251,11 @@ void bulk_destroy(bigint_t x, bigint_t y, bigint_t n, bigint_t d, bigint_t two,
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
bigint_t bigint_prime(size_t len) {
|
|
|
|
|
bigint_t n = bigint_zero(len);
|
|
|
|
|
//bigint_t n = bigint_zero(len);
|
|
|
|
|
bigint_t n = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2);
|
|
|
|
|
|
|
|
|
|
bigint_set_random_bytes(n);
|
|
|
|
|
bigint_set_msb_and_lsb_to_one(n);
|
|
|
|
|
bigint_set_random_bytes(n, len);
|
|
|
|
|
bigint_set_msb_and_lsb_to_one(n, len);
|
|
|
|
|
|
|
|
|
|
bigint_t d = bigint_clone(n);
|
|
|
|
|
d.data[0] -= 1;
|
|
|
|
@ -293,44 +271,48 @@ bigint_t bigint_prime(size_t len) {
|
|
|
|
|
bigint_t custom2 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2);
|
|
|
|
|
bigint_t custom3 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2);
|
|
|
|
|
|
|
|
|
|
bigint_t two = bigint_zero(len);
|
|
|
|
|
bigint_t two = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2);
|
|
|
|
|
bigint_t one = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2);
|
|
|
|
|
two.data[0] = 2;
|
|
|
|
|
bigint_t one = bigint_zero(len);
|
|
|
|
|
one.data[0] = 1;
|
|
|
|
|
bigint_t n_minus_two = bigint_clone(n);
|
|
|
|
|
bigint_t n_minus_one = bigint_clone(n);
|
|
|
|
|
n_minus_two.data[0] -= 1;
|
|
|
|
|
n_minus_one.data[0] -= 1;
|
|
|
|
|
bigint_decrement(n_minus_two);
|
|
|
|
|
bigint_t a = bigint_zero(len);
|
|
|
|
|
//bigint_t a = bigint_zero(len);
|
|
|
|
|
bigint_t a = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2);
|
|
|
|
|
for (uint32_t k = 0; k < 128; k++) {
|
|
|
|
|
bigint_set_zeros(a);
|
|
|
|
|
while (bigint_cmp(a, two) == -1 || bigint_cmp(a, n_minus_two) == 1) {
|
|
|
|
|
bigint_set_random_bytes(a);
|
|
|
|
|
while (bigint_cmp(a, two) < 0 || bigint_cmp(a, n_minus_two) > 0) {
|
|
|
|
|
bigint_set_random_bytes(a, len);
|
|
|
|
|
}
|
|
|
|
|
custom_bigint_pow_mod(a, d, n, x, custom, custom2, custom3);
|
|
|
|
|
for (uint32_t i = 0; i < s; i++) {
|
|
|
|
|
custom_bigint_pow_mod(x, two, n, y, custom, custom2, custom3);
|
|
|
|
|
if (bigint_cmp(y, one) == 0 && bigint_cmp(x, one) != 0 && bigint_cmp(x, n_minus_one) != 0) {
|
|
|
|
|
if (!bigint_dif(y, one) && bigint_dif(x, one) && bigint_dif(x, n_minus_one)) {
|
|
|
|
|
bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one);
|
|
|
|
|
bigint_destroy(custom);
|
|
|
|
|
bigint_destroy(custom2);
|
|
|
|
|
bigint_destroy(custom3);
|
|
|
|
|
bigint_destroy(a);
|
|
|
|
|
return bigint_prime(len);
|
|
|
|
|
}
|
|
|
|
|
bigint_destroy(x);
|
|
|
|
|
x = bigint_clone(y);
|
|
|
|
|
}
|
|
|
|
|
if (bigint_cmp(y, one) != 0) {
|
|
|
|
|
if (bigint_dif(y, one)) {
|
|
|
|
|
bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one);
|
|
|
|
|
bigint_destroy(custom);
|
|
|
|
|
bigint_destroy(custom2);
|
|
|
|
|
bigint_destroy(custom3);
|
|
|
|
|
bigint_destroy(a);
|
|
|
|
|
return bigint_prime(len);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
bulk_destroy(x, y, custom, d, two, one, n_minus_two, n_minus_one);
|
|
|
|
|
bigint_destroy(custom2);
|
|
|
|
|
bigint_destroy(custom3);
|
|
|
|
|
bigint_destroy(a);
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|