diff --git a/rsa/Makefile b/rsa/Makefile index 63baf89..ea760a3 100644 --- a/rsa/Makefile +++ b/rsa/Makefile @@ -12,6 +12,9 @@ all: $(NAME) $(NAME): gcc -Wall -Wextra -Werror $(SRC) -o $(NAME) +fast: + gcc -Wall -Wextra -Werror -o3 $(SRC) -o $(NAME) + fclean: rm -rf $(NAME) diff --git a/rsa/bigint.c b/rsa/bigint.c index a48ac09..370796a 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -164,6 +164,35 @@ bigint_t assignable_bigint_modulo(bigint_t a, bigint_t b) { return result; } +// 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) { + 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); +} + void bigint_add(bigint_t a, bigint_t b) { bigint_t result = bigint_zero(a.len); size_t size = sizeof(uint32_t) * 8; @@ -207,10 +236,31 @@ bigint_t assignable_bigint_mul(bigint_t a, bigint_t b) { return result; } +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); + 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 !!! -bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n) { - bigint_t result = bigint_clone(a); +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))) { @@ -218,26 +268,45 @@ bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n) { } cursor--; while (cursor >= 0) { - bigint_t tmp_result2 = assignable_bigint_mul(result, result); - bigint_destroy(result); - result = bigint_clone(tmp_result2); - bigint_destroy(tmp_result2); - tmp_result2 = assignable_bigint_modulo(result, n); - bigint_destroy(result); - result = bigint_clone(tmp_result2); - bigint_destroy(tmp_result2); + 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)) { - bigint_t tmp_result = assignable_bigint_mul(result, a); - bigint_destroy(result); - result = bigint_clone(tmp_result); - bigint_destroy(tmp_result); - tmp_result = assignable_bigint_modulo(result, n); - bigint_destroy(result); - result = bigint_clone(tmp_result); - bigint_destroy(tmp_result); + custom_bigint_mul(result, a, custom); + custom_bigint_modulo(custom, n, custom2); + memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); } cursor -= 1; } +} +// a^e mod n +// clean memory tricks !!! +bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n) { + bigint_t result = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); + memcpy(result.data, a.data, a.len * sizeof(uint32_t)); + 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); + 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; + } + bigint_destroy(custom); + bigint_destroy(custom2); return result; } @@ -257,39 +326,97 @@ bigint_t bigint_clone(bigint_t src) { return dst; } -bigint_t bigint_prime(size_t len) { - bigint_t n = bigint_new(len); +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); - printf("new\n"); - bigint_print(n); bigint_set_random_bytes(n); - printf("random bytes\n"); - bigint_print(n); bigint_set_msb_and_lsb_to_one(n); - printf("msb and lsb set to one\n"); - bigint_print(n); + // printf("msb and lsb set to one N IS: \n"); + // bigint_print(n); bigint_t d = bigint_clone(n); d.data[0] -= 1; uint32_t s = 0; - while (!d.data[0] & 1) { + while (!(d.data[0] & 1)) { bigint_bitwise_right_shift(d); s += 1; } + // printf("D IS:\n"); + // bigint_print(d); + + 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 n_minus_two = bigint_clone(d); + 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); + // printf("s is %d\n", s); for (uint32_t k = 0; k < 128; k++) { - bigint_t a = bigint_zero(len); + // printf("this is good %d\n", k); + //bigint_t a = bigint_zero(len); + bigint_set_zeros(a); while (bigint_cmp(a, two) == -1 || bigint_cmp(a, n_minus_two) == 1) { - printf("this is good %d\n", k); bigint_set_random_bytes(a); } - bigint_destroy(a); + //printf("A IS: \n"); + //bigint_print(a); + //printf("a %d\n", k); + //bigint_t x = custom_bigint_pow_mod(a, d, n, x, custom, custom2); + custom_bigint_pow_mod(a, d, n, x, custom, custom2); + //printf("b %d\n", k); + for (uint32_t i = 0; i < s; i++) { + //bigint_destroy(y); + custom_bigint_pow_mod(x, two, n, y, custom, custom2); + //y = assignable_bigint_pow_mod(x, two, n); + //printf("X IS: \n"); + //bigint_print(x); + //printf("Y IS: \n"); + //bigint_print(y); + 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(a); + // printf("failed first test\n"); + return bigint_prime(len); + } + bigint_destroy(x); + x = bigint_clone(y); + } + if (bigint_cmp(y, one) != 0) { + // printf("y is equal to %d and %d", y.data[1], y.data[0]); + // printf("failed second test\n"); + bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one); + bigint_destroy(a); + return bigint_prime(len); + } } + bigint_destroy(a); + // for (int i = 0; i < 10000; i++) { + // printf("CHU A MON PRIME!!!\n"); + // } return n; } diff --git a/rsa/rsa.c b/rsa/rsa.c index 66c254b..623b244 100644 --- a/rsa/rsa.c +++ b/rsa/rsa.c @@ -3,11 +3,15 @@ rsa_t rsa_init(size_t len) { rsa_t rsa; -// rsa.p = bigint_prime(len / 2); -// rsa.q = bigint_prime(len / 2); + printf("Generating two primes of length %d bits\n", RSA_BLOCK_SIZE / 2); + printf("Generating p...\n"); + rsa.p = bigint_prime(len / 2); + printf("p = %u\n", rsa.p.data[0]); + printf("Generating q...\n"); + rsa.q = bigint_prime(len / 2); + printf("q = %u\n", rsa.q.data[0]); + - rsa.p = bigint_zero(len / 2); - rsa.q = bigint_zero(len / 2); return rsa; } @@ -20,84 +24,21 @@ rsa_t rsa_generate_keys(size_t block_size) { a.data[0] = 1234567890; b.data[0] = 234567; - - printf("cmp a and b %d\n", bigint_cmp(a, b)); - - a.data[0] = 1234567890; -// b.data[0] = 1921572864; - - printf("cmp a and b %d\n", bigint_cmp(a, b)); - -/* - printf("cmp a and b %d\n", bigint_cmp(a, b)); - printf("cmp b and a %d\n", bigint_cmp(b, a)); - - bigint_bitwise_left_shift(b); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("b %ud\n", b.data[0]); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("b %ud\n", b.data[0]); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("b %ud\n", b.data[0]); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("b %ud\n", b.data[0]); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("b %ud\n", b.data[0]); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("b %ud\n", b.data[0]); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("b %ud\n", b.data[0]); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("b %ud\n", b.data[0]); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("b %ud\n", b.data[0]); - printf("cmp a and b %d\n", bigint_cmp(a, b)); -*/ bigint_t result = assignable_bigint_modulo(a, b); printf("result is %ud\n", result.data[0]); + bigint_destroy(a); a = bigint_clone(result); b.data[0] = 5764; printf("length\na: %lu e: %lu n: %lu\n", result.len, a.len, b.len); bigint_t result2 = assignable_bigint_pow_mod(result, a, b); + bigint_destroy(a); + bigint_destroy(b); + bigint_destroy(result); printf("bigpowmod is %u \n", result2.data[0]); -/* result.data[0] = 8; - a.data[0] = 4; - result2 = assignable_bigint_mul(result, a); - printf("result2 is %u \n", result2.data[0]); - result.data[0] = 84; - a.data[0] = 463; - result2 = assignable_bigint_mul(result, a); - printf("result2 is %u \n", result2.data[0]); - bigint_add(result, a); - - printf("result2 is %u \n", result.data[0]); -*/ + bigint_destroy(result2); return rsa; } diff --git a/rsa/rsa.h b/rsa/rsa.h index 02860e4..fc29fca 100644 --- a/rsa/rsa.h +++ b/rsa/rsa.h @@ -9,7 +9,7 @@ #include #include -#define RSA_BLOCK_SIZE 2048 +#define RSA_BLOCK_SIZE 64 typedef struct bigint_s { uint32_t *data; @@ -42,6 +42,7 @@ void bigint_add(bigint_t a, bigint_t b); bigint_t assignable_bigint_mul(bigint_t a, bigint_t b); bigint_t assignable_bigint_modulo(bigint_t a, bigint_t b); bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n); +void bigint_set_zeros(bigint_t n); void bigint_destroy(bigint_t n);