Compare commits

..

No commits in common. "fa5c3d7f96383a42fa9c4b6331e41dbff980a306" and "585f9750dfbc26e790bb78e082adb8bfa2a0fb7f" have entirely different histories.

3 changed files with 62 additions and 46 deletions

View File

@ -25,5 +25,3 @@ fclean:
rm -rf $(NAME)
re: fclean all
.PHONY: all fast profile profile-fast fclean re

View File

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

View File

@ -27,12 +27,12 @@ void *protected_malloc(size_t size);
rsa_t rsa_generate_keys(size_t block_size);
void bigint_set_random_bytes(bigint_t n, size_t len);
void bigint_set_msb_and_lsb_to_one(bigint_t n, size_t len);
void bigint_set_random_bytes(bigint_t n);
void bigint_set_msb_and_lsb_to_one(bigint_t n);
void bigint_bitwise_left_shift(bigint_t n);
void bigint_bitwise_right_shift(bigint_t n);
void bigint_decrement(bigint_t n);
int64_t bigint_cmp(bigint_t a, bigint_t b);
int bigint_cmp(bigint_t a, bigint_t b);
bigint_t bigint_prime(size_t len);
void bigint_print(bigint_t n);
bigint_t bigint_new(size_t len);