331 lines
8.6 KiB
C
331 lines
8.6 KiB
C
#include "rsa.h"
|
|
|
|
void bigint_set_random_bytes(bigint_t n) {
|
|
int fd = open("/dev/urandom", O_RDONLY);
|
|
read(fd, n.data, n.len * sizeof(uint32_t));
|
|
close(fd);
|
|
}
|
|
|
|
void bigint_set_msb_and_lsb_to_one(bigint_t n) {
|
|
n.data[0] |= 1;
|
|
n.data[n.len - 1] |= 1 << 31;
|
|
}
|
|
|
|
void bigint_bitwise_right_shift(bigint_t n) {
|
|
size_t size = sizeof(uint32_t) * 8 - 1;
|
|
for (size_t i = 0; i < n.len - 1; i++) {
|
|
n.data[i] = n.data[i] >> 1 | (n.data[i + 1] & 1) << size;
|
|
}
|
|
n.data[n.len - 1] >>= 1;
|
|
}
|
|
|
|
void bigint_bitwise_left_shift(bigint_t n) {
|
|
size_t size = sizeof(uint32_t) * 8 - 1;
|
|
for (int i = n.len - 1; i > 0; i--) {
|
|
n.data[i] = n.data[i] << 1 | ((n.data[i - 1] & (1 << size)) >> size);
|
|
}
|
|
n.data[0] <<= 1;
|
|
}
|
|
|
|
bigint_t assignable_bigint_bitwise_left_shift(bigint_t n) {
|
|
bigint_t result = bigint_clone(n);
|
|
size_t size = sizeof(uint32_t) * 8 - 1;
|
|
for (int i = result.len - 1; i > 0; i--) {
|
|
result.data[i] = result.data[i] << 1 | ((result.data[i - 1] & (1 << size)) >> size);
|
|
}
|
|
result.data[0] <<= 1;
|
|
return result;
|
|
}
|
|
|
|
// Will underflow
|
|
void bigint_decrement(bigint_t n) {
|
|
size_t cursor = 0;
|
|
size_t size = sizeof(uint32_t) * 8;
|
|
while (cursor < size * n.len) {
|
|
n.data[cursor / size] = n.data[cursor / size] ^ (1 << (cursor % size));
|
|
if (((n.data[cursor / size] >> (cursor % size)) & 1) == 0) {
|
|
return;
|
|
}
|
|
cursor += 1;
|
|
}
|
|
}
|
|
|
|
// 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) {
|
|
if (a.data[acursor / size] & (1 << acursor % size)) {
|
|
return 1;
|
|
}
|
|
acursor -= 1;
|
|
}
|
|
while (bcursor > acursor) {
|
|
if (b.data[bcursor / size] & (1 << bcursor % size)) {
|
|
return -1;
|
|
}
|
|
bcursor -= 1;
|
|
}
|
|
int cursor = acursor;
|
|
while (cursor >= 0) {
|
|
uint32_t abit = a.data[cursor / size] & (1 << (cursor % size));
|
|
uint32_t bbit = b.data[cursor / size] & (1 << (cursor % size));
|
|
if (abit > bbit) {
|
|
return 1;
|
|
}
|
|
if (bbit > abit) {
|
|
return -1;
|
|
}
|
|
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));
|
|
}
|
|
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_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];
|
|
}
|
|
bigint_destroy(y);
|
|
y = assignable_bigint_bitwise_left_shift(borrow);
|
|
}
|
|
bigint_destroy(b);
|
|
bigint_destroy(y);
|
|
bigint_destroy(borrow);
|
|
bigint_destroy(zero);
|
|
}
|
|
|
|
// TODO check opti
|
|
void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result) {
|
|
bigint_set_zeros(result);
|
|
memcpy(result.data, a.data, a.len * sizeof(uint32_t));
|
|
bigint_t mod = bigint_clone(b);
|
|
if (a.len > b.len) {
|
|
bigint_destroy(mod);
|
|
mod = bigint_zero(a.len);
|
|
memcpy(mod.data, b.data, b.len * sizeof(uint32_t));
|
|
}
|
|
if (bigint_cmp(result, b) == -1) {
|
|
bigint_destroy(mod);
|
|
return ;
|
|
}
|
|
bigint_bitwise_left_shift(mod);
|
|
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) == 1) {
|
|
bigint_substraction(result, mod);
|
|
}
|
|
}
|
|
while (bigint_cmp(result, b) == 1) {
|
|
bigint_substraction(result, b);
|
|
}
|
|
bigint_destroy(mod);
|
|
}
|
|
|
|
bigint_t bigint_new(size_t len) {
|
|
bigint_t bigint;
|
|
|
|
bigint.len = len;
|
|
bigint.data = (uint32_t *)protected_malloc(len * sizeof(uint32_t));
|
|
|
|
return bigint;
|
|
}
|
|
|
|
bigint_t bigint_zero(size_t len) {
|
|
bigint_t bigint;
|
|
|
|
bigint = bigint_new(len);
|
|
for (size_t i = 0; i < len; i++) {
|
|
bigint.data[i] = 0;
|
|
}
|
|
|
|
return bigint;
|
|
}
|
|
|
|
bigint_t bigint_clone(bigint_t src) {
|
|
bigint_t dst;
|
|
|
|
dst.len = src.len;
|
|
dst.data = (uint32_t *)protected_malloc(src.len * sizeof(uint32_t));
|
|
memcpy(dst.data, src.data, src.len * sizeof(uint32_t));
|
|
|
|
return dst;
|
|
}
|
|
|
|
void bigint_destroy(bigint_t n) {
|
|
free(n.data);
|
|
n.data = NULL;
|
|
}
|
|
|
|
void bigint_add(bigint_t a, bigint_t b) {
|
|
bigint_t result = bigint_zero(a.len);
|
|
size_t size = sizeof(uint32_t) * 8;
|
|
size_t width = a.len * size;
|
|
uint32_t carriage = 0;
|
|
|
|
for (size_t cursor = 0; cursor < width; cursor++) {
|
|
uint32_t a_bit = a.data[cursor / size] >> (cursor % size) & 1;
|
|
uint32_t b_bit = b.data[cursor / size] >> (cursor % size) & 1;
|
|
result.data[cursor / size] |= (a_bit ^ b_bit ^ carriage) << (cursor % size);
|
|
carriage = (a_bit & b_bit) | ((a_bit ^ b_bit) & carriage);
|
|
}
|
|
memcpy(a.data, result.data, a.len * sizeof(uint32_t));
|
|
bigint_destroy(result);
|
|
}
|
|
|
|
void bigint_set_zeros(bigint_t n) {
|
|
for (size_t i = 0; i < n.len; i++) {
|
|
n.data[i] = 0;
|
|
}
|
|
}
|
|
|
|
void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result) {
|
|
//bigint_t b_tool = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4);
|
|
bigint_t b_tool = bigint_zero(a.len + b.len);
|
|
size_t size = sizeof(uint32_t) * 8;
|
|
size_t width = a.len * size;
|
|
bigint_set_zeros(result);
|
|
for (size_t cursor = 0; cursor < width; cursor++) {
|
|
if (a.data[cursor / 32] >> (cursor % 32) & 1) {
|
|
bigint_set_zeros(b_tool);
|
|
memcpy(b_tool.data, b.data, b.len * sizeof(uint32_t));
|
|
for (size_t i = 0; i < cursor; i++) {
|
|
bigint_bitwise_left_shift(b_tool);
|
|
}
|
|
bigint_add(result, b_tool);
|
|
}
|
|
}
|
|
bigint_destroy(b_tool);
|
|
}
|
|
|
|
// a^e mod n
|
|
// clean memory tricks !!!
|
|
void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, bigint_t custom, bigint_t custom2) {
|
|
bigint_set_zeros(result);
|
|
bigint_set_zeros(custom);
|
|
bigint_set_zeros(custom2);
|
|
memcpy(result.data, a.data, a.len * sizeof(uint32_t));
|
|
size_t size = sizeof(uint32_t) * 8;
|
|
int cursor = e.len * size - 1;
|
|
while (!(e.data[cursor / 32] & 1 << (cursor % 32))) {
|
|
cursor--;
|
|
}
|
|
cursor--;
|
|
while (cursor >= 0) {
|
|
custom_bigint_mul(result, result, custom);
|
|
custom_bigint_modulo(custom, n, custom2);
|
|
bigint_set_zeros(result);
|
|
memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t));
|
|
if (e.data[cursor / 32] & 1 << (cursor % 32)) {
|
|
custom_bigint_mul(result, a, custom);
|
|
custom_bigint_modulo(custom, n, custom2);
|
|
memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t));
|
|
}
|
|
cursor -= 1;
|
|
}
|
|
}
|
|
|
|
void bigint_print(bigint_t n) {
|
|
for (int i = n.len - 1; i >= 0; i--) {
|
|
printf("bigint %ud\n", n.data[i]);
|
|
}
|
|
}
|
|
|
|
|
|
void bulk_destroy(bigint_t x, bigint_t y, bigint_t n, bigint_t d, bigint_t two, bigint_t one, bigint_t n_minus_two, bigint_t n_minus_one) {
|
|
bigint_destroy(x);
|
|
bigint_destroy(y);
|
|
bigint_destroy(n);
|
|
bigint_destroy(d);
|
|
bigint_destroy(two);
|
|
bigint_destroy(one);
|
|
bigint_destroy(n_minus_two);
|
|
bigint_destroy(n_minus_one);
|
|
}
|
|
/*
|
|
bigint_t bigint_random_range(bigint_t low, bigint_t high) {
|
|
|
|
}
|
|
*/
|
|
bigint_t bigint_prime(size_t len) {
|
|
bigint_t n = bigint_zero(len);
|
|
|
|
bigint_set_random_bytes(n);
|
|
bigint_set_msb_and_lsb_to_one(n);
|
|
|
|
bigint_t d = bigint_clone(n);
|
|
d.data[0] -= 1;
|
|
uint32_t s = 0;
|
|
while (!(d.data[0] & 1)) {
|
|
bigint_bitwise_right_shift(d);
|
|
s += 1;
|
|
}
|
|
|
|
bigint_t x = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4);
|
|
bigint_t y = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4);
|
|
bigint_t custom = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4);
|
|
bigint_t custom2 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4);
|
|
|
|
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);
|
|
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);
|
|
}
|
|
custom_bigint_pow_mod(a, d, n, x, custom, custom2);
|
|
for (uint32_t i = 0; i < s; i++) {
|
|
custom_bigint_pow_mod(x, two, n, y, custom, custom2);
|
|
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(a);
|
|
return bigint_prime(len);
|
|
}
|
|
bigint_destroy(x);
|
|
x = bigint_clone(y);
|
|
}
|
|
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(a);
|
|
return bigint_prime(len);
|
|
}
|
|
}
|
|
bulk_destroy(x, y, custom, d, two, one, n_minus_two, n_minus_one);
|
|
bigint_destroy(custom2);
|
|
bigint_destroy(a);
|
|
return n;
|
|
}
|
|
|