Tidy up some repetitive code.

Change-Id: Ib5b22a342b603854495d7013bd9376a4f1e12bd6
Reviewed-on: https://boringssl-review.googlesource.com/14869
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/rsa/rsa_impl.c b/crypto/rsa/rsa_impl.c
index e385e60..611212c 100644
--- a/crypto/rsa/rsa_impl.c
+++ b/crypto/rsa/rsa_impl.c
@@ -763,6 +763,13 @@
   return ret;
 }
 
+static int ensure_bignum(BIGNUM **out) {
+  if (*out == NULL) {
+    *out = BN_new();
+  }
+  return *out != NULL;
+}
+
 int rsa_default_multi_prime_keygen(RSA *rsa, int bits, int num_primes,
                                    BIGNUM *e_value, BN_GENCB *cb) {
   BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *tmp;
@@ -817,28 +824,14 @@
   }
 
   /* We need the RSA components non-NULL */
-  if (!rsa->n && ((rsa->n = BN_new()) == NULL)) {
-    goto err;
-  }
-  if (!rsa->d && ((rsa->d = BN_new()) == NULL)) {
-    goto err;
-  }
-  if (!rsa->e && ((rsa->e = BN_new()) == NULL)) {
-    goto err;
-  }
-  if (!rsa->p && ((rsa->p = BN_new()) == NULL)) {
-    goto err;
-  }
-  if (!rsa->q && ((rsa->q = BN_new()) == NULL)) {
-    goto err;
-  }
-  if (!rsa->dmp1 && ((rsa->dmp1 = BN_new()) == NULL)) {
-    goto err;
-  }
-  if (!rsa->dmq1 && ((rsa->dmq1 = BN_new()) == NULL)) {
-    goto err;
-  }
-  if (!rsa->iqmp && ((rsa->iqmp = BN_new()) == NULL)) {
+  if (!ensure_bignum(&rsa->n) ||
+      !ensure_bignum(&rsa->d) ||
+      !ensure_bignum(&rsa->e) ||
+      !ensure_bignum(&rsa->p) ||
+      !ensure_bignum(&rsa->q) ||
+      !ensure_bignum(&rsa->dmp1) ||
+      !ensure_bignum(&rsa->dmq1) ||
+      !ensure_bignum(&rsa->iqmp)) {
     goto err;
   }