make sure RSA blinding works when the PRNG is not properly seeded;
[openssl.git] / crypto / rsa / rsa_eay.c
index 24c77699fe6ad1603b9086cf37e5dac55381af92..6bc6ef391344a1c88f886a2e42ab9e0309f150d1 100644 (file)
@@ -211,6 +211,25 @@ err:
        return(r);
        }
 
+static int rsa_eay_blinding(RSA *rsa, BN_CTX *ctx)
+       {
+       int ret = 1;
+       CRYPTO_w_lock(CRYPTO_LOCK_RSA);
+       /* Check again inside the lock - the macro's check is racey */
+       if(rsa->blinding == NULL)
+               ret = RSA_blinding_on(rsa, ctx);
+       CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
+       return ret;
+       }
+
+#define BLINDING_HELPER(rsa, ctx, err_instr) \
+       do { \
+               if((!((rsa)->flags & RSA_FLAG_NO_BLINDING)) && \
+                   ((rsa)->blinding == NULL) && \
+                   !rsa_eay_blinding(rsa, ctx)) \
+                       err_instr \
+       } while(0)
+
 /* signing */
 static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
             unsigned char *to, RSA *rsa, int padding)
@@ -255,9 +274,9 @@ static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
                goto err;
                }
 
-       if ((rsa->flags & RSA_FLAG_BLINDING) && (rsa->blinding == NULL))
-               RSA_blinding_on(rsa,ctx);
-       if (rsa->flags & RSA_FLAG_BLINDING)
+       BLINDING_HELPER(rsa, ctx, goto err;);
+
+       if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
                if (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err;
 
        if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
@@ -274,7 +293,7 @@ static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
                                rsa->_method_mod_n)) goto err;
                }
 
-       if (rsa->flags & RSA_FLAG_BLINDING)
+       if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
                if (!BN_BLINDING_invert(&ret,rsa->blinding,ctx)) goto err;
 
        /* put in leading 0 bytes if the number is less than the
@@ -336,9 +355,9 @@ static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
                goto err;
                }
 
-       if ((rsa->flags & RSA_FLAG_BLINDING) && (rsa->blinding == NULL))
-               RSA_blinding_on(rsa,ctx);
-       if (rsa->flags & RSA_FLAG_BLINDING)
+       BLINDING_HELPER(rsa, ctx, goto err;);
+
+       if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
                if (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err;
 
        /* do the decrypt */
@@ -357,7 +376,7 @@ static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
                        goto err;
                }
 
-       if (rsa->flags & RSA_FLAG_BLINDING)
+       if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
                if (!BN_BLINDING_invert(&ret,rsa->blinding,ctx)) goto err;
 
        p=buf;