From 4a3cbd75b57b2046721dd3f83b14c74f0644a13b Mon Sep 17 00:00:00 2001 From: gbrochar Date: Fri, 16 Feb 2024 17:33:46 +0100 Subject: [PATCH] fix: leaks --- rsa/bigint.c | 38 ++++++++++---------------------------- 1 file changed, 10 insertions(+), 28 deletions(-) diff --git a/rsa/bigint.c b/rsa/bigint.c index ab4d131..dda3504 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -82,35 +82,11 @@ int bigint_cmp(bigint_t a, bigint_t b) { return 0; } -// TODO check opti -bigint_t assignable_bigint_substraction(bigint_t a, bigint_t b) { - if (a.len != b.len) { - printf("error: attempting to substract numbers of different length\n"); - exit(1); - } - - bigint_t result = bigint_clone(a); - 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] = ~result.data[i] & y.data[i]; - result.data[i] = result.data[i] ^ y.data[i]; - } - bigint_destroy(y); - y = assignable_bigint_bitwise_left_shift(borrow); - } - bigint_destroy(y); - bigint_destroy(borrow); - bigint_destroy(zero); - return result; -} - // 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)); } @@ -130,6 +106,7 @@ 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); @@ -141,6 +118,7 @@ void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t 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)); } @@ -211,9 +189,7 @@ void bigint_add(bigint_t a, bigint_t b) { 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(a); - a = bigint_clone(result); + memcpy(a.data, result.data, a.len * sizeof(uint32_t)); bigint_destroy(result); } @@ -330,6 +306,8 @@ bigint_t bigint_prime(size_t len) { 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); } @@ -338,10 +316,14 @@ bigint_t bigint_prime(size_t len) { } 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; }