From 567e54b97c4d354cff9aa7f6841ae90b251fbe70 Mon Sep 17 00:00:00 2001 From: David Barksdale Date: Mon, 9 Oct 2017 14:50:30 -0500 Subject: [PATCH] Fix leaks in CRYPTO_paillier and test --- src/util/crypto_paillier.c | 2 ++ src/util/test_crypto_paillier.c | 35 +++++++++++++++++++++++---------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/util/crypto_paillier.c b/src/util/crypto_paillier.c index 3ed025a2a..530a2957f 100644 --- a/src/util/crypto_paillier.c +++ b/src/util/crypto_paillier.c @@ -370,9 +370,11 @@ GNUNET_CRYPTO_paillier_decrypt (const struct GNUNET_CRYPTO_PaillierPrivateKey *p /* mod = cmum1 / n (mod n) */ GNUNET_assert (0 != (mod = gcry_mpi_new (0))); gcry_mpi_div (mod, NULL, cmum1, n, 0); + gcry_mpi_release (cmum1); /* m = mod * mu mod n */ gcry_mpi_mulm (m, mod, mu, n); + gcry_mpi_release (mod); gcry_mpi_release (mu); gcry_mpi_release (n); } diff --git a/src/util/test_crypto_paillier.c b/src/util/test_crypto_paillier.c index 9950978c1..1e7e0b301 100644 --- a/src/util/test_crypto_paillier.c +++ b/src/util/test_crypto_paillier.c @@ -37,6 +37,7 @@ test_crypto () struct GNUNET_CRYPTO_PaillierCiphertext ciphertext; struct GNUNET_CRYPTO_PaillierPublicKey public_key; struct GNUNET_CRYPTO_PaillierPrivateKey private_key; + int ret = 0; GNUNET_CRYPTO_paillier_create (&public_key, &private_key); @@ -54,7 +55,6 @@ test_crypto () &public_key, &ciphertext, plaintext_result); - if (0 != gcry_mpi_cmp (plaintext, plaintext_result)) { @@ -65,9 +65,11 @@ test_crypto () plaintext); gcry_log_debugmpi ("\n", plaintext_result); - return 1; + ret = 1; } - return 0; + gcry_mpi_release (plaintext); + gcry_mpi_release (plaintext_result); + return ret; } @@ -84,6 +86,7 @@ test_hom_simple (unsigned int a, struct GNUNET_CRYPTO_PaillierCiphertext c_result; struct GNUNET_CRYPTO_PaillierPublicKey public_key; struct GNUNET_CRYPTO_PaillierPrivateKey private_key; + int ret = 0; GNUNET_CRYPTO_paillier_create (&public_key, &private_key); @@ -119,9 +122,13 @@ test_hom_simple (unsigned int a, "GNUNET_CRYPTO_paillier failed simple math!\n"); gcry_log_debugmpi ("got ", hom_result); gcry_log_debugmpi ("wanted ", result); - return 1; + ret = 1; } - return 0; + gcry_mpi_release (m1); + gcry_mpi_release (m2); + gcry_mpi_release (result); + gcry_mpi_release (hom_result); + return ret; } @@ -168,7 +175,8 @@ test_hom () fprintf (stderr, "GNUNET_CRYPTO_paillier_encrypt 1 failed, should return 1 allowed operation, got %d!\n", ret); - return 1; + ret = 1; + goto out; } if (2 != (ret = GNUNET_CRYPTO_paillier_encrypt (&public_key, m2, @@ -178,7 +186,8 @@ test_hom () fprintf (stderr, "GNUNET_CRYPTO_paillier_encrypt 2 failed, should return 2 allowed operation, got %d!\n", ret); - return 1; + ret = 1; + goto out; } if (0 != (ret = GNUNET_CRYPTO_paillier_hom_add (&public_key, @@ -189,7 +198,8 @@ test_hom () fprintf (stderr, "GNUNET_CRYPTO_paillier_hom_add failed, expected 0 remaining operations, got %d!\n", ret); - return 1; + ret = 1; + goto out; } GNUNET_CRYPTO_paillier_decrypt (&private_key, @@ -203,9 +213,14 @@ test_hom () "GNUNET_CRYPTO_paillier miscalculated with large numbers!\n"); gcry_log_debugmpi ("got", hom_result); gcry_log_debugmpi ("wanted", result); - return 1; + ret = 1; } - return 0; +out: + gcry_mpi_release (m1); + gcry_mpi_release (m2); + gcry_mpi_release (result); + gcry_mpi_release (hom_result); + return ret; } -- 2.25.1