Blame SOURCES/nettle-2.7.1-tmpalloc.patch

c687f3
diff --git a/Makefile.in b/Makefile.in
c687f3
index 3b0e1cb..ebef2c4 100644
c687f3
--- a/Makefile.in
c687f3
+++ b/Makefile.in
c687f3
@@ -176,7 +176,7 @@ DISTFILES = $(SOURCES) $(HEADERS) getopt.h .bootstrap run-tests \
c687f3
 	cast128_sboxes.h desinfo.h desCode.h \
c687f3
 	nettle-internal.h nettle-write.h prime-list.h \
c687f3
 	gmp-glue.h ecc-internal.h \
c687f3
-	mini-gmp.h mini-gmp.c asm.m4 \
c687f3
+	mini-gmp.h mini-gmp.c asm.m4 bignum-internal.h \
c687f3
 	nettle.texinfo nettle.info nettle.html nettle.pdf sha-example.c
c687f3
 
c687f3
 # Rules building static libraries
c687f3
diff --git a/bignum-internal.h b/bignum-internal.h
c687f3
new file mode 100644
c687f3
index 0000000..26a7cdb
c687f3
--- /dev/null
c687f3
+++ b/bignum-internal.h
c687f3
@@ -0,0 +1,36 @@
c687f3
+/* bignum-internal.h
c687f3
+ *
c687f3
+ */
c687f3
+
c687f3
+/* nettle, low-level cryptographics library
c687f3
+ *
c687f3
+ * Copyright (C) 2013 Red Hat
c687f3
+ *  
c687f3
+ * The nettle library is free software; you can redistribute it and/or modify
c687f3
+ * it under the terms of the GNU Lesser General Public License as published by
c687f3
+ * the Free Software Foundation; either version 2.1 of the License, or (at your
c687f3
+ * option) any later version.
c687f3
+ * 
c687f3
+ * The nettle library is distributed in the hope that it will be useful, but
c687f3
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
c687f3
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
c687f3
+ * License for more details.
c687f3
+ * 
c687f3
+ * You should have received a copy of the GNU Lesser General Public License
c687f3
+ * along with the nettle library; see the file COPYING.LIB.  If not, write to
c687f3
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
c687f3
+ * MA 02111-1301, USA.
c687f3
+ */
c687f3
+
c687f3
+#ifndef BIGNUM_INTERNAL_H
c687f3
+# define BIGNUM_INTERNAL_H
c687f3
+
c687f3
+#include <gmp-glue.h>
c687f3
+
c687f3
+#define TMP_GMP_DECL(name, type) type *name; \
c687f3
+	unsigned tmp_##name##_size
c687f3
+#define TMP_GMP_ALLOC(name, size) \
c687f3
+	(name = gmp_alloc(&tmp_##name##_size, sizeof (*name) * (size)))
c687f3
+#define TMP_GMP_FREE(name) (gmp_free(name, tmp_##name##_size))
c687f3
+
c687f3
+#endif
c687f3
diff --git a/bignum-next-prime.c b/bignum-next-prime.c
c687f3
index 58a4df8..bc89399 100644
c687f3
--- a/bignum-next-prime.c
c687f3
+++ b/bignum-next-prime.c
c687f3
@@ -31,6 +31,7 @@
c687f3
 #include <stdlib.h>
c687f3
 
c687f3
 #include "bignum.h"
c687f3
+#include "bignum-internal.h"
c687f3
 
c687f3
 #include "nettle-internal.h"
c687f3
 
c687f3
@@ -77,9 +78,8 @@ nettle_next_prime(mpz_t p, mpz_t n, unsigned count, unsigned prime_limit,
c687f3
 		  void *progress_ctx, nettle_progress_func *progress)
c687f3
 {
c687f3
   mpz_t tmp;
c687f3
-  TMP_DECL(moduli, unsigned, NUMBER_OF_PRIMES);
c687f3
-  
c687f3
   unsigned difference;
c687f3
+  TMP_GMP_DECL(moduli, unsigned);
c687f3
 
c687f3
   if (prime_limit > NUMBER_OF_PRIMES)
c687f3
     prime_limit = NUMBER_OF_PRIMES;
c687f3
@@ -112,7 +112,8 @@ nettle_next_prime(mpz_t p, mpz_t n, unsigned count, unsigned prime_limit,
c687f3
      between the 5760 odd numbers in this interval that have no factor
c687f3
      in common with 15015.
c687f3
    */
c687f3
-  TMP_ALLOC(moduli, prime_limit);
c687f3
+  TMP_GMP_ALLOC(moduli, prime_limit);
c687f3
+
c687f3
   {
c687f3
     unsigned i;
c687f3
     for (i = 0; i < prime_limit; i++)
c687f3
@@ -159,4 +160,5 @@ nettle_next_prime(mpz_t p, mpz_t n, unsigned count, unsigned prime_limit,
c687f3
 #endif
c687f3
     }
c687f3
   mpz_clear(tmp);
c687f3
+  TMP_GMP_FREE(moduli);
c687f3
 }
c687f3
diff --git a/bignum-random.c b/bignum-random.c
c687f3
index f305f04..07ae1ba 100644
c687f3
--- a/bignum-random.c
c687f3
+++ b/bignum-random.c
c687f3
@@ -30,6 +30,7 @@
c687f3
 #include <stdlib.h>
c687f3
 
c687f3
 #include "bignum.h"
c687f3
+#include "bignum-internal.h"
c687f3
 #include "nettle-internal.h"
c687f3
 
c687f3
 void
c687f3
@@ -38,15 +39,17 @@ nettle_mpz_random_size(mpz_t x,
c687f3
 		       unsigned bits)
c687f3
 {
c687f3
   unsigned length = (bits + 7) / 8;
c687f3
-  TMP_DECL(data, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
c687f3
-  TMP_ALLOC(data, length);
c687f3
+  TMP_GMP_DECL(data, uint8_t);
c687f3
 
c687f3
-  random(ctx, length, data);
c687f3
+  TMP_GMP_ALLOC(data, length);
c687f3
 
c687f3
+  random(ctx, length, data);
c687f3
   nettle_mpz_set_str_256_u(x, length, data);
c687f3
 
c687f3
   if (bits % 8)
c687f3
     mpz_fdiv_r_2exp(x, x, bits);
c687f3
+  
c687f3
+  TMP_GMP_FREE(data);
c687f3
 }
c687f3
 
c687f3
 /* Returns a random number x, 0 <= x < n */
c687f3
diff --git a/gmp-glue.c b/gmp-glue.c
c687f3
index a2633a5..991e793 100644
c687f3
--- a/gmp-glue.c
c687f3
+++ b/gmp-glue.c
c687f3
@@ -239,3 +239,24 @@ gmp_free_limbs (mp_limb_t *p, mp_size_t n)
c687f3
 
c687f3
   free_func (p, (size_t) n * sizeof(mp_limb_t));
c687f3
 }
c687f3
+
c687f3
+void* gmp_alloc(unsigned* out_n, size_t n)
c687f3
+{
c687f3
+	void *(*alloc_func)(size_t);
c687f3
+	assert (n > 0);
c687f3
+
c687f3
+	mp_get_memory_functions(&alloc_func, NULL, NULL);
c687f3
+	
c687f3
+	*out_n = n;
c687f3
+	return alloc_func (n);
c687f3
+}
c687f3
+
c687f3
+void gmp_free(void* p, size_t n)
c687f3
+{
c687f3
+  void (*free_func)(void *, size_t);
c687f3
+  assert (n > 0);
c687f3
+  assert (p != 0);
c687f3
+  mp_get_memory_functions (NULL, NULL, &free_func);
c687f3
+
c687f3
+  free_func (p, (size_t) n);
c687f3
+}
c687f3
diff --git a/gmp-glue.h b/gmp-glue.h
c687f3
index 269667f..ff936a1 100644
c687f3
--- a/gmp-glue.h
c687f3
+++ b/gmp-glue.h
c687f3
@@ -65,6 +65,8 @@
c687f3
 #define mpn_set_base256 _nettle_mpn_set_base256
c687f3
 #define gmp_alloc_limbs _nettle_gmp_alloc_limbs
c687f3
 #define gmp_free_limbs _nettle_gmp_free_limbs
c687f3
+#define gmp_free _nettle_gmp_free
c687f3
+#define gmp_alloc _nettle_gmp_alloc
c687f3
 
c687f3
 /* Use only in-place operations, so we can fall back to addmul_1/submul_1 */
c687f3
 #ifdef mpn_cnd_add_n
c687f3
@@ -155,5 +157,7 @@ gmp_alloc_limbs (mp_size_t n);
c687f3
 void
c687f3
 gmp_free_limbs (mp_limb_t *p, mp_size_t n);
c687f3
 
c687f3
+void* gmp_alloc(unsigned* out_n, size_t n);
c687f3
+void gmp_free(void* p, size_t n);
c687f3
 
c687f3
 #endif /* NETTLE_GMP_GLUE_H_INCLUDED */
c687f3
diff --git a/pkcs1-decrypt.c b/pkcs1-decrypt.c
c687f3
index 754fd51..89b4dcf 100644
c687f3
--- a/pkcs1-decrypt.c
c687f3
+++ b/pkcs1-decrypt.c
c687f3
@@ -31,6 +31,7 @@
c687f3
 #include "pkcs1.h"
c687f3
 
c687f3
 #include "bignum.h"
c687f3
+#include "bignum-internal.h"
c687f3
 #include "nettle-internal.h"
c687f3
 
c687f3
 int
c687f3
@@ -38,35 +39,50 @@ pkcs1_decrypt (unsigned key_size,
c687f3
 	       const mpz_t m,
c687f3
 	       unsigned *length, uint8_t *message)
c687f3
 {
c687f3
-  TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
c687f3
+  TMP_GMP_DECL(em, uint8_t);
c687f3
   uint8_t *terminator;
c687f3
-  unsigned padding;
c687f3
-  unsigned message_length;
c687f3
-
c687f3
-  TMP_ALLOC(em, key_size);
c687f3
+  size_t padding;
c687f3
+  size_t message_length;
c687f3
+  int ret;
c687f3
+ 
c687f3
+  TMP_GMP_ALLOC(em, key_size);
c687f3
   nettle_mpz_get_str_256(key_size, em, m);
c687f3
-
c687f3
+ 
c687f3
   /* Check format */
c687f3
   if (em[0] || em[1] != 2)
c687f3
-    return 0;
c687f3
-
c687f3
+    {
c687f3
+     ret = 0;
c687f3
+     goto cleanup;
c687f3
+    }
c687f3
+ 
c687f3
   terminator = memchr(em + 2, 0, key_size - 2);
c687f3
-
c687f3
+ 
c687f3
   if (!terminator)
c687f3
-    return 0;
c687f3
+    {
c687f3
+     ret = 0;
c687f3
+     goto cleanup;
c687f3
+    }
c687f3
   
c687f3
   padding = terminator - (em + 2);
c687f3
   if (padding < 8)
c687f3
-    return 0;
c687f3
-
c687f3
+    {
c687f3
+     ret = 0;
c687f3
+     goto cleanup;
c687f3
+    }
c687f3
+ 
c687f3
   message_length = key_size - 3 - padding;
c687f3
-
c687f3
+ 
c687f3
   if (*length < message_length)
c687f3
-    return 0;
c687f3
+    {
c687f3
+     ret = 0;
c687f3
+     goto cleanup;
c687f3
+    }
c687f3
   
c687f3
   memcpy(message, terminator + 1, message_length);
c687f3
   *length = message_length;
c687f3
-
c687f3
-  return 1;
c687f3
+ 
c687f3
+  ret = 1;
c687f3
+cleanup:
c687f3
+  TMP_GMP_FREE(em);
c687f3
+  return ret;
c687f3
 }
c687f3
-	       
c687f3
diff --git a/pkcs1-encrypt.c b/pkcs1-encrypt.c
c687f3
index cde19bc..5246455 100644
c687f3
--- a/pkcs1-encrypt.c
c687f3
+++ b/pkcs1-encrypt.c
c687f3
@@ -34,6 +34,7 @@
c687f3
 #include "pkcs1.h"
c687f3
 
c687f3
 #include "bignum.h"
c687f3
+#include "bignum-internal.h"
c687f3
 #include "nettle-internal.h"
c687f3
 
c687f3
 int
c687f3
@@ -43,7 +44,7 @@ pkcs1_encrypt (unsigned key_size,
c687f3
 	       unsigned length, const uint8_t *message,
c687f3
 	       mpz_t m)
c687f3
 {
c687f3
-  TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
c687f3
+  TMP_GMP_DECL(em, uint8_t);
c687f3
   unsigned padding;
c687f3
   unsigned i;
c687f3
 
c687f3
@@ -63,7 +64,7 @@ pkcs1_encrypt (unsigned key_size,
c687f3
   padding = key_size - length - 3;
c687f3
   assert(padding >= 8);
c687f3
   
c687f3
-  TMP_ALLOC(em, key_size - 1);
c687f3
+  TMP_GMP_ALLOC(em, key_size - 1);
c687f3
   em[0] = 2;
c687f3
 
c687f3
   random(random_ctx, padding, em + 1);
c687f3
@@ -77,5 +78,7 @@ pkcs1_encrypt (unsigned key_size,
c687f3
   memcpy(em + padding + 2, message, length);
c687f3
 
c687f3
   nettle_mpz_set_str_256_u(m, key_size - 1, em);
c687f3
+  
c687f3
+  TMP_GMP_FREE(em);
c687f3
   return 1;
c687f3
 }
c687f3
diff --git a/pkcs1-rsa-digest.c b/pkcs1-rsa-digest.c
c687f3
index e4a6c52..3379b8f 100644
c687f3
--- a/pkcs1-rsa-digest.c
c687f3
+++ b/pkcs1-rsa-digest.c
c687f3
@@ -29,21 +29,27 @@
c687f3
 #include "pkcs1.h"
c687f3
 
c687f3
 #include "bignum.h"
c687f3
+#include "bignum-internal.h"
c687f3
 #include "nettle-internal.h"
c687f3
 
c687f3
 int
c687f3
 pkcs1_rsa_digest_encode(mpz_t m, unsigned key_size,
c687f3
 			unsigned di_length, const uint8_t *digest_info)
c687f3
 {
c687f3
-  TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
c687f3
-  TMP_ALLOC(em, key_size);
c687f3
+  TMP_GMP_DECL(em, uint8_t);
c687f3
+
c687f3
+  TMP_GMP_ALLOC(em, key_size);
c687f3
 
c687f3
   if (_pkcs1_signature_prefix(key_size, em,
c687f3
 			      di_length, digest_info, 0))
c687f3
     {
c687f3
       nettle_mpz_set_str_256_u(m, key_size, em);
c687f3
+      TMP_GMP_FREE(em);
c687f3
       return 1;
c687f3
     }
c687f3
   else
c687f3
-    return 0;
c687f3
+    {
c687f3
+      TMP_GMP_FREE(em);
c687f3
+      return 0;
c687f3
+    }
c687f3
 }
c687f3
diff --git a/pkcs1-rsa-md5.c b/pkcs1-rsa-md5.c
c687f3
index 00514fc..e5edaf3 100644
c687f3
--- a/pkcs1-rsa-md5.c
c687f3
+++ b/pkcs1-rsa-md5.c
c687f3
@@ -34,6 +34,7 @@
c687f3
 #include "rsa.h"
c687f3
 
c687f3
 #include "bignum.h"
c687f3
+#include "bignum-internal.h"
c687f3
 #include "pkcs1.h"
c687f3
 
c687f3
 #include "nettle-internal.h"
c687f3
@@ -65,8 +66,9 @@ int
c687f3
 pkcs1_rsa_md5_encode(mpz_t m, unsigned key_size, struct md5_ctx *hash)
c687f3
 {
c687f3
   uint8_t *p;
c687f3
-  TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
c687f3
-  TMP_ALLOC(em, key_size);
c687f3
+  TMP_GMP_DECL(em, uint8_t);
c687f3
+
c687f3
+  TMP_GMP_ALLOC(em, key_size);
c687f3
 
c687f3
   p = _pkcs1_signature_prefix(key_size, em,
c687f3
 			      sizeof(md5_prefix),
c687f3
@@ -76,18 +78,23 @@ pkcs1_rsa_md5_encode(mpz_t m, unsigned key_size, struct md5_ctx *hash)
c687f3
     {
c687f3
       md5_digest(hash, MD5_DIGEST_SIZE, p);
c687f3
       nettle_mpz_set_str_256_u(m, key_size, em);
c687f3
+      TMP_GMP_FREE(em);
c687f3
       return 1;
c687f3
     }
c687f3
   else
c687f3
-    return 0;
c687f3
+    {
c687f3
+      TMP_GMP_FREE(em);
c687f3
+      return 0;
c687f3
+    }
c687f3
 }
c687f3
 
c687f3
 int
c687f3
 pkcs1_rsa_md5_encode_digest(mpz_t m, unsigned key_size, const uint8_t *digest)
c687f3
 {
c687f3
   uint8_t *p;
c687f3
-  TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
c687f3
-  TMP_ALLOC(em, key_size);
c687f3
+  TMP_GMP_DECL(em, uint8_t);
c687f3
+
c687f3
+  TMP_GMP_ALLOC(em, key_size);
c687f3
 
c687f3
   p = _pkcs1_signature_prefix(key_size, em,
c687f3
 			      sizeof(md5_prefix),
c687f3
@@ -97,8 +104,12 @@ pkcs1_rsa_md5_encode_digest(mpz_t m, unsigned key_size, const uint8_t *digest)
c687f3
     {
c687f3
       memcpy(p, digest, MD5_DIGEST_SIZE);
c687f3
       nettle_mpz_set_str_256_u(m, key_size, em);
c687f3
+      TMP_GMP_FREE(em);
c687f3
       return 1;
c687f3
     }
c687f3
   else
c687f3
-    return 0;
c687f3
+    {
c687f3
+      TMP_GMP_FREE(em);
c687f3
+      return 0;
c687f3
+    }
c687f3
 }
c687f3
diff --git a/pkcs1-rsa-sha1.c b/pkcs1-rsa-sha1.c
c687f3
index 2951618..2a68121 100644
c687f3
--- a/pkcs1-rsa-sha1.c
c687f3
+++ b/pkcs1-rsa-sha1.c
c687f3
@@ -34,6 +34,7 @@
c687f3
 #include "rsa.h"
c687f3
 
c687f3
 #include "bignum.h"
c687f3
+#include "bignum-internal.h"
c687f3
 #include "pkcs1.h"
c687f3
 
c687f3
 #include "nettle-internal.h"
c687f3
@@ -65,8 +66,9 @@ int
c687f3
 pkcs1_rsa_sha1_encode(mpz_t m, unsigned key_size, struct sha1_ctx *hash)
c687f3
 {
c687f3
   uint8_t *p;
c687f3
-  TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
c687f3
-  TMP_ALLOC(em, key_size);
c687f3
+  TMP_GMP_DECL(em, uint8_t);
c687f3
+
c687f3
+  TMP_GMP_ALLOC(em, key_size);
c687f3
 
c687f3
   p = _pkcs1_signature_prefix(key_size, em,
c687f3
 			      sizeof(sha1_prefix),
c687f3
@@ -76,18 +78,23 @@ pkcs1_rsa_sha1_encode(mpz_t m, unsigned key_size, struct sha1_ctx *hash)
c687f3
     {
c687f3
       sha1_digest(hash, SHA1_DIGEST_SIZE, p);
c687f3
       nettle_mpz_set_str_256_u(m, key_size, em);
c687f3
+      TMP_GMP_FREE(em);
c687f3
       return 1;
c687f3
     }
c687f3
   else
c687f3
-    return 0;
c687f3
+    {
c687f3
+      TMP_GMP_FREE(em);
c687f3
+      return 0;
c687f3
+    }
c687f3
 }
c687f3
 
c687f3
 int
c687f3
 pkcs1_rsa_sha1_encode_digest(mpz_t m, unsigned key_size, const uint8_t *digest)
c687f3
 {
c687f3
   uint8_t *p;
c687f3
-  TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
c687f3
-  TMP_ALLOC(em, key_size);
c687f3
+  TMP_GMP_DECL(em, uint8_t);
c687f3
+
c687f3
+  TMP_GMP_ALLOC(em, key_size);
c687f3
 
c687f3
   p = _pkcs1_signature_prefix(key_size, em,
c687f3
 			      sizeof(sha1_prefix),
c687f3
@@ -97,8 +104,12 @@ pkcs1_rsa_sha1_encode_digest(mpz_t m, unsigned key_size, const uint8_t *digest)
c687f3
     {
c687f3
       memcpy(p, digest, SHA1_DIGEST_SIZE);
c687f3
       nettle_mpz_set_str_256_u(m, key_size, em);
c687f3
+      TMP_GMP_FREE(em);
c687f3
       return 1;
c687f3
     }
c687f3
   else
c687f3
-    return 0;
c687f3
+    {
c687f3
+      TMP_GMP_FREE(em);
c687f3
+      return 0;
c687f3
+    }
c687f3
 }
c687f3
diff --git a/pkcs1-rsa-sha256.c b/pkcs1-rsa-sha256.c
c687f3
index cb07375..3aaabe1 100644
c687f3
--- a/pkcs1-rsa-sha256.c
c687f3
+++ b/pkcs1-rsa-sha256.c
c687f3
@@ -34,6 +34,7 @@
c687f3
 #include "rsa.h"
c687f3
 
c687f3
 #include "bignum.h"
c687f3
+#include "bignum-internal.h"
c687f3
 #include "pkcs1.h"
c687f3
 
c687f3
 #include "nettle-internal.h"
c687f3
@@ -63,8 +64,9 @@ int
c687f3
 pkcs1_rsa_sha256_encode(mpz_t m, unsigned key_size, struct sha256_ctx *hash)
c687f3
 {
c687f3
   uint8_t *p;
c687f3
-  TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
c687f3
-  TMP_ALLOC(em, key_size);
c687f3
+  TMP_GMP_DECL(em, uint8_t);
c687f3
+
c687f3
+  TMP_GMP_ALLOC(em, key_size);
c687f3
 
c687f3
   p = _pkcs1_signature_prefix(key_size, em,
c687f3
 			      sizeof(sha256_prefix),
c687f3
@@ -74,18 +76,23 @@ pkcs1_rsa_sha256_encode(mpz_t m, unsigned key_size, struct sha256_ctx *hash)
c687f3
     {
c687f3
       sha256_digest(hash, SHA256_DIGEST_SIZE, p);
c687f3
       nettle_mpz_set_str_256_u(m, key_size, em);
c687f3
+      TMP_GMP_FREE(em);
c687f3
       return 1;
c687f3
     }
c687f3
   else
c687f3
-    return 0;	
c687f3
+    {
c687f3
+      TMP_GMP_FREE(em);
c687f3
+      return 0;
c687f3
+    }
c687f3
 }
c687f3
 
c687f3
 int
c687f3
 pkcs1_rsa_sha256_encode_digest(mpz_t m, unsigned key_size, const uint8_t *digest)
c687f3
 {
c687f3
   uint8_t *p;
c687f3
-  TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
c687f3
-  TMP_ALLOC(em, key_size);
c687f3
+  TMP_GMP_DECL(em, uint8_t);
c687f3
+
c687f3
+  TMP_GMP_ALLOC(em, key_size);
c687f3
 
c687f3
   p = _pkcs1_signature_prefix(key_size, em,
c687f3
 			      sizeof(sha256_prefix),
c687f3
@@ -95,8 +102,12 @@ pkcs1_rsa_sha256_encode_digest(mpz_t m, unsigned key_size, const uint8_t *digest
c687f3
     {
c687f3
       memcpy(p, digest, SHA256_DIGEST_SIZE);
c687f3
       nettle_mpz_set_str_256_u(m, key_size, em);
c687f3
+      TMP_GMP_FREE(em);
c687f3
       return 1;
c687f3
     }
c687f3
   else
c687f3
-    return 0;
c687f3
+    {
c687f3
+      TMP_GMP_FREE(em);
c687f3
+      return 0;
c687f3
+    }
c687f3
 }
c687f3
diff --git a/pkcs1-rsa-sha512.c b/pkcs1-rsa-sha512.c
c687f3
index 3afd790..bd3d277 100644
c687f3
--- a/pkcs1-rsa-sha512.c
c687f3
+++ b/pkcs1-rsa-sha512.c
c687f3
@@ -34,6 +34,7 @@
c687f3
 #include "rsa.h"
c687f3
 
c687f3
 #include "bignum.h"
c687f3
+#include "bignum-internal.h"
c687f3
 #include "pkcs1.h"
c687f3
 
c687f3
 #include "nettle-internal.h"
c687f3
@@ -63,8 +64,9 @@ int
c687f3
 pkcs1_rsa_sha512_encode(mpz_t m, unsigned key_size, struct sha512_ctx *hash)
c687f3
 {
c687f3
   uint8_t *p;
c687f3
-  TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
c687f3
-  TMP_ALLOC(em, key_size);
c687f3
+  TMP_GMP_DECL(em, uint8_t);
c687f3
+
c687f3
+  TMP_GMP_ALLOC(em, key_size);
c687f3
 
c687f3
   p = _pkcs1_signature_prefix(key_size, em,
c687f3
 			      sizeof(sha512_prefix),
c687f3
@@ -74,18 +76,23 @@ pkcs1_rsa_sha512_encode(mpz_t m, unsigned key_size, struct sha512_ctx *hash)
c687f3
     {
c687f3
       sha512_digest(hash, SHA512_DIGEST_SIZE, p);
c687f3
       nettle_mpz_set_str_256_u(m, key_size, em);
c687f3
+      TMP_GMP_FREE(em);
c687f3
       return 1;
c687f3
     }
c687f3
   else
c687f3
-    return 0;
c687f3
+    {
c687f3
+      TMP_GMP_FREE(em);
c687f3
+      return 0;
c687f3
+    }
c687f3
 }
c687f3
 
c687f3
 int
c687f3
 pkcs1_rsa_sha512_encode_digest(mpz_t m, unsigned key_size, const uint8_t *digest)
c687f3
 {
c687f3
   uint8_t *p;
c687f3
-  TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
c687f3
-  TMP_ALLOC(em, key_size);
c687f3
+  TMP_GMP_DECL(em, uint8_t);
c687f3
+
c687f3
+  TMP_GMP_ALLOC(em, key_size);
c687f3
 
c687f3
   p = _pkcs1_signature_prefix(key_size, em,
c687f3
 			      sizeof(sha512_prefix),
c687f3
@@ -95,8 +102,12 @@ pkcs1_rsa_sha512_encode_digest(mpz_t m, unsigned key_size, const uint8_t *digest
c687f3
     {
c687f3
       memcpy(p, digest, SHA512_DIGEST_SIZE);
c687f3
       nettle_mpz_set_str_256_u(m, key_size, em);
c687f3
+      TMP_GMP_FREE(em);
c687f3
       return 1;
c687f3
     }
c687f3
   else
c687f3
-    return 0;
c687f3
+    {
c687f3
+      TMP_GMP_FREE(em);
c687f3
+      return 0;
c687f3
+    }
c687f3
 }