Blame SOURCES/nettle-2.7.1-tmpalloc.patch

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