PR:make sure RSA blinding works when the PRNG is not properly seeded;
authorBodo Möller <bodo@openssl.org>
Thu, 20 Mar 2003 17:24:54 +0000 (17:24 +0000)
committerBodo Möller <bodo@openssl.org>
Thu, 20 Mar 2003 17:24:54 +0000 (17:24 +0000)
enable it automatically only for the built-in engine

CHANGES
crypto/rsa/rsa.h
crypto/rsa/rsa_eay.c
crypto/rsa/rsa_lib.c

diff --git a/CHANGES b/CHANGES
index ab440fc46dc6d9aa4653123d26b2e6a8f26a3b4f..f234afa9e550790cf17b8a631b8c244da18bb4d3 100644 (file)
--- a/CHANGES
+++ b/CHANGES
      in ssl3_get_client_key_exchange (ssl/s3_srvr.c).
      [Bodo Moeller]
 
-  *) Turn on RSA blinding by default, to avoid a timing attack. Applications
-     that don't want it can call RSA_blinding_off(). They would be ill-advised
-     to do so in most cases. The automatic enabling can also be turned off
-     by defining OPENSSL_FORCE_NO_RSA_BLINDING at compile-time.
+  *) Turn on RSA blinding by default in the default implementation
+     to avoid a timing attack. Applications that don't want it can call
+     RSA_blinding_off() or use the new flag RSA_FLAG_NO_BLINDING.
+     They would be ill-advised to do so in most cases.
      [Ben Laurie, Steve Henson, Geoff Thorpe]
 
+  *) Change RSA blinding code so that it works when the PRNG is not
+     seeded (in this case, the secret RSA exponent is abused as
+     an unpredictable seed -- if it is not unpredictable, there
+     is no point in blinding anyway).
+     [Bodo Moeller]
+
  Changes between 0.9.6h and 0.9.6i  [19 Feb 2003]
 
   *) In ssl3_get_record (ssl/s3_pkt.c), minimize information leaked
index 76aed3cb38522ea378aea8b7ac24d1ab7bcaf0af..6844ee9d2897c92ec2a97d585081db6c6dd071cb 100644 (file)
@@ -152,6 +152,11 @@ struct rsa_st
 #define RSA_FLAG_CACHE_PUBLIC          0x02
 #define RSA_FLAG_CACHE_PRIVATE         0x04
 #define RSA_FLAG_BLINDING              0x08
+#define RSA_FLAG_NO_BLINDING           0x80 /* new with 0.9.6j and 0.9.7b; the built-in
+                                              * RSA implementation now uses blinding by
+                                              * default (ignoring RSA_FLAG_BLINDING),
+                                              * but other engines might not need it
+                                              */
 #define RSA_FLAG_THREAD_SAFE           0x10
 /* This flag means the private key operations will be handled by rsa_mod_exp
  * and that they do not depend on the private key components being present:
@@ -164,6 +169,8 @@ struct rsa_st
  */
 #define RSA_FLAG_SIGN_VER              0x40
 
+#define RSA_FLAG_NO_BLINDING           0x80
+
 #define RSA_PKCS1_PADDING      1
 #define RSA_SSLV23_PADDING     2
 #define RSA_NO_PADDING         3
index 444111fc394e2a914a5dd6fd8915c77b867af6b5..e5f23fd684fe71c05096f90eaf7ce3b9771f03a1 100644 (file)
@@ -203,9 +203,9 @@ static int rsa_eay_blinding(RSA *rsa, BN_CTX *ctx)
 
 #define BLINDING_HELPER(rsa, ctx, err_instr) \
        do { \
-               if(((rsa)->flags & RSA_FLAG_BLINDING) && \
-                               ((rsa)->blinding == NULL) && \
-                               !rsa_eay_blinding(rsa, ctx)) \
+               if((!((rsa)->flags & RSA_FLAG_NO_BLINDING)) && \
+                   ((rsa)->blinding == NULL) && \
+                   !rsa_eay_blinding(rsa, ctx)) \
                        err_instr \
        } while(0)
 
@@ -255,7 +255,7 @@ static int RSA_eay_private_encrypt(int flen, unsigned char *from,
 
        BLINDING_HELPER(rsa, ctx, goto err;);
 
-       if (rsa->flags & RSA_FLAG_BLINDING)
+       if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
                if (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err;
 
        if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
@@ -270,7 +270,7 @@ static int RSA_eay_private_encrypt(int flen, unsigned char *from,
                if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL)) 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
@@ -334,7 +334,7 @@ static int RSA_eay_private_decrypt(int flen, unsigned char *from,
 
        BLINDING_HELPER(rsa, ctx, goto err;);
 
-       if (rsa->flags & RSA_FLAG_BLINDING)
+       if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
                if (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err;
 
        /* do the decrypt */
@@ -351,7 +351,7 @@ static int RSA_eay_private_decrypt(int flen, 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;
index 0aeefae7db9d34fc75def0b339dccccfeae38762..d6854591f61fd5e57bbe89d91e4860820232f367 100644 (file)
@@ -73,10 +73,6 @@ RSA *RSA_new(void)
        {
        RSA *r=RSA_new_method(NULL);
 
-#ifndef OPENSSL_NO_FORCE_RSA_BLINDING
-       r->flags|=RSA_FLAG_BLINDING;
-#endif
-
        return r;
        }
 
@@ -257,7 +253,8 @@ void RSA_blinding_off(RSA *rsa)
                BN_BLINDING_free(rsa->blinding);
                rsa->blinding=NULL;
                }
-       rsa->flags&= ~RSA_FLAG_BLINDING;
+       rsa->flags &= ~RSA_FLAG_BLINDING;
+       rsa->flags |= RSA_FLAG_NO_BLINDING;
        }
 
 int RSA_blinding_on(RSA *rsa, BN_CTX *p_ctx)
@@ -278,13 +275,23 @@ int RSA_blinding_on(RSA *rsa, BN_CTX *p_ctx)
 
        BN_CTX_start(ctx);
        A = BN_CTX_get(ctx);
-       if (!BN_rand_range(A,rsa->n)) goto err;
+       if ((RAND_status() == 0) && rsa->d != NULL && rsa->d->d != NULL)
+               {
+               /* if PRNG is not properly seeded, resort to secret exponent as unpredictable seed */
+               RAND_add(rsa->d->d, rsa->d->dmax * sizeof rsa->d->d[0], 0);
+               if (!BN_pseudo_rand_range(A,rsa->n)) goto err;
+               }
+       else
+               {
+               if (!BN_rand_range(A,rsa->n)) goto err;
+               }
        if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err;
 
        if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n))
            goto err;
        rsa->blinding=BN_BLINDING_new(A,Ai,rsa->n);
-       rsa->flags|=RSA_FLAG_BLINDING;
+       rsa->flags |= RSA_FLAG_BLINDING;
+       rsa->flags &= ~RSA_FLAG_NO_BLINDING;
        BN_free(Ai);
        ret=1;
 err: