diff --git a/rsa/bigint.c b/rsa/bigint.c index 4ecc0d6..25496a4 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -1,5 +1,9 @@ #include "rsa.h" +void my_memcpy(void *dst, void *src, size_t n) { + memcpy(dst, src, n); +} + void bigint_set_random_bytes(bigint_t n, size_t len) { int fd = open("/dev/urandom", O_RDONLY); read(fd, n.data, len * sizeof(uint32_t)); @@ -12,37 +16,33 @@ void bigint_set_msb_and_lsb_to_one(bigint_t n, size_t len) { } 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[i] = n.data[i] >> 1 | (n.data[i + 1] & 1) << 31; } 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[i] = n.data[i] << 1 | ((n.data[i - 1] & (1 << 31)) >> 31); } n.data[0] <<= 1; } void move_bigint_bitwise_left_shift(bigint_t n, bigint_t result) { - memcpy(result.data, n.data, n.len * sizeof(uint32_t)); - size_t size = sizeof(uint32_t) * 8 - 1; +// my_memcpy(result.data, n.data, n.len * sizeof(uint32_t)); 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[i] = n.data[i] << 1 | ((n.data[i - 1] & (1 << 31)) >> 31); } - result.data[0] <<= 1; + result.data[0] = n.data[0] << 1; } // 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) { + while (cursor < n.len << 5) { + n.data[cursor >> 32] = n.data[cursor >> 5] ^ (1 << (cursor % 32)); + if (((n.data[cursor >> 5] >> (cursor % 32)) & 1) == 0) { return; } cursor += 1; @@ -88,8 +88,8 @@ int is_not_zero(bigint_t n) { // TODO check opti void bigint_substraction(bigint_t a, bigint_t b, bigint_t borrow, bigint_t y) { - memcpy(borrow.data, b.data, b.len * sizeof(uint32_t)); - memcpy(y.data, b.data, b.len * sizeof(uint32_t)); + //my_memcpy(borrow.data, b.data, b.len * sizeof(uint32_t)); + my_memcpy(y.data, b.data, b.len * sizeof(uint32_t)); while (is_not_zero(y)) { for (size_t i = 0; i < a.len; i++) { borrow.data[i] = ~a.data[i] & y.data[i]; @@ -102,7 +102,7 @@ void bigint_substraction(bigint_t a, bigint_t b, bigint_t borrow, bigint_t y) { // TODO check opti void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result, bigint_t mod, bigint_t borrow_sub, bigint_t y_sub) { bigint_set_zeros(result); - memcpy(result.data, a.data, a.len * sizeof(uint32_t)); + my_memcpy(result.data, a.data, a.len * sizeof(uint32_t)); if (bigint_cmp(result, b) < 0) { return ; } @@ -145,7 +145,7 @@ bigint_t bigint_clone(bigint_t src) { 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)); + my_memcpy(dst.data, src.data, src.len * sizeof(uint32_t)); return dst; } @@ -178,11 +178,11 @@ void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result, bigint_t *b_tool int width = a.len * 32; bigint_set_zeros(result); bigint_set_zeros(b_tool[0]); - memcpy(b_tool[0].data, b.data, b.len * sizeof(uint32_t)); + my_memcpy(b_tool[0].data, b.data, b.len * sizeof(uint32_t)); for (int i = 1; i < 32; i++) { bigint_set_zeros(b_tool[i]); - memcpy(b_tool[i].data, b_tool[i - 1].data, b.len * sizeof(uint32_t)); + my_memcpy(b_tool[i].data, b_tool[i - 1].data, b.len * sizeof(uint32_t)); bigint_bitwise_left_shift(b_tool[i]); } @@ -201,10 +201,9 @@ void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, 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))) { + my_memcpy(result.data, a.data, a.len * sizeof(uint32_t)); + int cursor = (e.len << 5) - 1; + while (!(e.data[cursor >> 5] & 1 << (cursor % 32))) { cursor--; } cursor--; @@ -212,11 +211,11 @@ void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, custom_bigint_mul(result, result, custom, b_tool); custom_bigint_modulo(custom, n, custom2, mod, borrow_sub, y_sub); bigint_set_zeros(result); - memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); - if (e.data[cursor / 32] & 1 << (cursor % 32)) { + my_memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); + if (e.data[cursor >> 5] & 1 << (cursor % 32)) { custom_bigint_mul(result, a, custom, b_tool); custom_bigint_modulo(custom, n, custom2, mod, borrow_sub, y_sub); - memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); + my_memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); } cursor -= 1; } @@ -241,7 +240,9 @@ 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(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); + size_t my_size = len * 2; + + bigint_t n = bigint_zero(my_size); bigint_set_random_bytes(n, len); bigint_set_msb_and_lsb_to_one(n, len); @@ -257,19 +258,19 @@ bigint_t bigint_prime(size_t len) { s += 1; } - bigint_t x = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); - bigint_t y = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); - bigint_t custom = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); - bigint_t custom2 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); + bigint_t x = bigint_zero(my_size); + bigint_t y = bigint_zero(my_size); + bigint_t custom = bigint_zero(my_size); + bigint_t custom2 = bigint_zero(my_size); bigint_t *b_tool = (bigint_t *)protected_malloc(32 * sizeof(bigint_t)); for (int i = 0; i < 32; i++) { - b_tool[i] = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); + b_tool[i] = bigint_zero(my_size * 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(my_size); + bigint_t one = bigint_zero(my_size); two.data[0] = 2; one.data[0] = 1; bigint_t n_minus_two = bigint_clone(n); @@ -277,7 +278,7 @@ bigint_t bigint_prime(size_t len) { n_minus_two.data[0] -= 1; n_minus_one.data[0] -= 1; bigint_decrement(n_minus_two); - bigint_t a = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); + bigint_t a = bigint_zero(my_size); 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) {