+diff --git a/crypto/bn/bn_prime.c b/crypto/bn/bn_prime.c\r
+index 1d25687..e933ead 100644\r
+--- a/crypto/bn/bn_prime.c\r
++++ b/crypto/bn/bn_prime.c\r
+@@ -131,7 +131,7 @@\r
+ static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,\r
+ const BIGNUM *a1_odd, int k, BN_CTX *ctx,\r
+ BN_MONT_CTX *mont);\r
+-static int probable_prime(BIGNUM *rnd, int bits);\r
++static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods);\r
+ static int probable_prime_dh(BIGNUM *rnd, int bits,\r
+ const BIGNUM *add, const BIGNUM *rem,\r
+ BN_CTX *ctx);\r
+@@ -166,9 +166,13 @@ int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,\r
+ BIGNUM *t;\r
+ int found = 0;\r
+ int i, j, c1 = 0;\r
+- BN_CTX *ctx;\r
++ BN_CTX *ctx = NULL;\r
++ prime_t *mods = NULL;\r
+ int checks = BN_prime_checks_for_size(bits);\r
+ \r
++ mods = OPENSSL_malloc(sizeof(*mods) * NUMPRIMES);\r
++ if (mods == NULL)\r
++ goto err;\r
+ ctx = BN_CTX_new();\r
+ if (ctx == NULL)\r
+ goto err;\r
+@@ -179,7 +183,7 @@ int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,\r
+ loop:\r
+ /* make a random number and set the top and bottom bits */\r
+ if (add == NULL) {\r
+- if (!probable_prime(ret, bits))\r
++ if (!probable_prime(ret, bits, mods))\r
+ goto err;\r
+ } else {\r
+ if (safe) {\r
+@@ -230,6 +234,7 @@ int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,\r
+ /* we have a prime :-) */\r
+ found = 1;\r
+ err:\r
++ OPENSSL_free(mods);\r
+ if (ctx != NULL) {\r
+ BN_CTX_end(ctx);\r
+ BN_CTX_free(ctx);\r
+@@ -375,10 +380,9 @@ static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,\r
+ return 1;\r
+ }\r
+ \r
+-static int probable_prime(BIGNUM *rnd, int bits)\r
++static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods)\r
+ {\r
+ int i;\r
+- prime_t mods[NUMPRIMES];\r
+ BN_ULONG delta, maxdelta;\r
+ \r
+ again:\r