aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Levitte <levitte@openssl.org>2003-04-02 11:21:31 +0000
committerRichard Levitte <levitte@openssl.org>2003-04-02 11:21:31 +0000
commit5949e8e1dd0777dacd34ba999d66f67fbeea085e (patch)
tree1587975c8c7be218cca0af135491c2fc0b602a87
parentc4157adf1e9e2752e3931631198c2610e69e4501 (diff)
downloadopenssl-5949e8e1dd0777dacd34ba999d66f67fbeea085e.tar.gz
Recent changes from 0.9.6-stable.
-rw-r--r--CHANGES9
-rw-r--r--crypto/bn/bn.h2
-rw-r--r--crypto/rsa/rsa.h1
-rw-r--r--crypto/rsa/rsa_eay.c114
-rw-r--r--crypto/rsa/rsa_err.c3
-rw-r--r--crypto/rsa/rsa_lib.c9
-rw-r--r--util/mkerr.pl2
7 files changed, 127 insertions, 13 deletions
diff --git a/CHANGES b/CHANGES
index f234afa9e5..df556aae46 100644
--- a/CHANGES
+++ b/CHANGES
@@ -14,12 +14,17 @@
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]
+ [Ben Laurie, Steve Henson, Geoff Thorpe, Bodo Moeller]
*) 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).
+ is no point in blinding anyway). Make RSA blinding thread-safe
+ by remembering the creator's thread ID in rsa->blinding and
+ having all other threads use local one-time blinding factors
+ (this requires more computation than sharing rsa->blinding, but
+ avoids excessive locking; and if an RSA object is not shared
+ between threads, blinding will still be very fast).
[Bodo Moeller]
Changes between 0.9.6h and 0.9.6i [19 Feb 2003]
diff --git a/crypto/bn/bn.h b/crypto/bn/bn.h
index c0d6b1612d..74b7347523 100644
--- a/crypto/bn/bn.h
+++ b/crypto/bn/bn.h
@@ -259,6 +259,8 @@ typedef struct bn_blinding_st
BIGNUM *A;
BIGNUM *Ai;
BIGNUM *mod; /* just a reference */
+ unsigned long thread_id; /* added in OpenSSL 0.9.6j and 0.9.7b;
+ * used only by crypto/rsa/rsa_eay.c, rsa_lib.c */
} BN_BLINDING;
/* Used for montgomery multiplication */
diff --git a/crypto/rsa/rsa.h b/crypto/rsa/rsa.h
index 7cd6d95db9..47981457ec 100644
--- a/crypto/rsa/rsa.h
+++ b/crypto/rsa/rsa.h
@@ -348,6 +348,7 @@ void ERR_load_RSA_strings(void);
#define RSA_R_DMP1_NOT_CONGRUENT_TO_D 124
#define RSA_R_DMQ1_NOT_CONGRUENT_TO_D 125
#define RSA_R_D_E_NOT_CONGRUENT_TO_1 123
+#define RSA_R_INTERNAL_ERROR 133
#define RSA_R_INVALID_MESSAGE_LENGTH 131
#define RSA_R_IQMP_NOT_INVERSE_OF_Q 126
#define RSA_R_KEY_SIZE_TOO_SMALL 120
diff --git a/crypto/rsa/rsa_eay.c b/crypto/rsa/rsa_eay.c
index 4b44fb92d6..dbabd07c9f 100644
--- a/crypto/rsa/rsa_eay.c
+++ b/crypto/rsa/rsa_eay.c
@@ -212,6 +212,40 @@ static int rsa_eay_blinding(RSA *rsa, BN_CTX *ctx)
err_instr \
} while(0)
+static BN_BLINDING *setup_blinding(RSA *rsa, BN_CTX *ctx)
+ {
+ BIGNUM *A, *Ai;
+ BN_BLINDING *ret = NULL;
+
+ /* added in OpenSSL 0.9.6j and 0.9.7b */
+
+ /* NB: similar code appears in RSA_blinding_on (rsa_lib.c);
+ * this should be placed in a new function of its own, but for reasons
+ * of binary compatibility can't */
+
+ BN_CTX_start(ctx);
+ A = BN_CTX_get(ctx);
+ 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;
+ ret = BN_BLINDING_new(A,Ai,rsa->n);
+ BN_free(Ai);
+err:
+ BN_CTX_end(ctx);
+ return ret;
+ }
+
/* signing */
static int RSA_eay_private_encrypt(int flen, unsigned char *from,
unsigned char *to, RSA *rsa, int padding)
@@ -221,6 +255,8 @@ static int RSA_eay_private_encrypt(int flen, unsigned char *from,
int i,j,k,num=0,r= -1;
unsigned char *buf=NULL;
BN_CTX *ctx=NULL;
+ int local_blinding = 0;
+ BN_BLINDING *blinding = NULL;
meth = ENGINE_get_RSA(rsa->engine);
BN_init(&f);
@@ -259,9 +295,38 @@ static int RSA_eay_private_encrypt(int flen, unsigned char *from,
}
BLINDING_HELPER(rsa, ctx, goto err;);
-
+ blinding = rsa->blinding;
+
+ /* Now unless blinding is disabled, 'blinding' is non-NULL.
+ * But the BN_BLINDING object may be owned by some other thread
+ * (we don't want to keep it constant and we don't want to use
+ * lots of locking to avoid race conditions, so only a single
+ * thread can use it; other threads have to use local blinding
+ * factors) */
if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
- if (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err;
+ {
+ if (blinding == NULL)
+ {
+ RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, RSA_R_INTERNAL_ERROR);
+ goto err;
+ }
+ }
+
+ if (blinding != NULL)
+ {
+ if (blinding->thread_id != CRYPTO_thread_id())
+ {
+ /* we need a local one-time blinding factor */
+
+ blinding = setup_blinding(rsa, ctx);
+ if (blinding == NULL)
+ goto err;
+ local_blinding = 1;
+ }
+ }
+
+ if (blinding)
+ if (!BN_BLINDING_convert(&f, blinding, ctx)) goto err;
if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
((rsa->p != NULL) &&
@@ -275,8 +340,8 @@ static int RSA_eay_private_encrypt(int flen, unsigned char *from,
if (!meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL)) goto err;
}
- if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
- if (!BN_BLINDING_invert(&ret,rsa->blinding,ctx)) goto err;
+ if (blinding)
+ if (!BN_BLINDING_invert(&ret, blinding, ctx)) goto err;
/* put in leading 0 bytes if the number is less than the
* length of the modulus */
@@ -290,6 +355,8 @@ err:
if (ctx != NULL) BN_CTX_free(ctx);
BN_clear_free(&ret);
BN_clear_free(&f);
+ if (local_blinding)
+ BN_BLINDING_free(blinding);
if (buf != NULL)
{
OPENSSL_cleanse(buf,num);
@@ -307,6 +374,8 @@ static int RSA_eay_private_decrypt(int flen, unsigned char *from,
unsigned char *p;
unsigned char *buf=NULL;
BN_CTX *ctx=NULL;
+ int local_blinding = 0;
+ BN_BLINDING *blinding = NULL;
meth = ENGINE_get_RSA(rsa->engine);
BN_init(&f);
@@ -340,9 +409,38 @@ static int RSA_eay_private_decrypt(int flen, unsigned char *from,
}
BLINDING_HELPER(rsa, ctx, goto err;);
-
+ blinding = rsa->blinding;
+
+ /* Now unless blinding is disabled, 'blinding' is non-NULL.
+ * But the BN_BLINDING object may be owned by some other thread
+ * (we don't want to keep it constant and we don't want to use
+ * lots of locking to avoid race conditions, so only a single
+ * thread can use it; other threads have to use local blinding
+ * factors) */
if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
- if (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err;
+ {
+ if (blinding == NULL)
+ {
+ RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, RSA_R_INTERNAL_ERROR);
+ goto err;
+ }
+ }
+
+ if (blinding != NULL)
+ {
+ if (blinding->thread_id != CRYPTO_thread_id())
+ {
+ /* we need a local one-time blinding factor */
+
+ blinding = setup_blinding(rsa, ctx);
+ if (blinding == NULL)
+ goto err;
+ local_blinding = 1;
+ }
+ }
+
+ if (blinding)
+ if (!BN_BLINDING_convert(&f, blinding, ctx)) goto err;
/* do the decrypt */
if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
@@ -358,8 +456,8 @@ static int RSA_eay_private_decrypt(int flen, unsigned char *from,
goto err;
}
- if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
- if (!BN_BLINDING_invert(&ret,rsa->blinding,ctx)) goto err;
+ if (blinding)
+ if (!BN_BLINDING_invert(&ret, blinding, ctx)) goto err;
p=buf;
j=BN_bn2bin(&ret,p); /* j is only used with no-padding mode */
diff --git a/crypto/rsa/rsa_err.c b/crypto/rsa/rsa_err.c
index bff7cf5d12..be10200de1 100644
--- a/crypto/rsa/rsa_err.c
+++ b/crypto/rsa/rsa_err.c
@@ -1,6 +1,6 @@
/* crypto/rsa/rsa_err.c */
/* ====================================================================
- * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
+ * Copyright (c) 1999-2003 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -113,6 +113,7 @@ static ERR_STRING_DATA RSA_str_reasons[]=
{RSA_R_DMP1_NOT_CONGRUENT_TO_D ,"dmp1 not congruent to d"},
{RSA_R_DMQ1_NOT_CONGRUENT_TO_D ,"dmq1 not congruent to d"},
{RSA_R_D_E_NOT_CONGRUENT_TO_1 ,"d e not congruent to 1"},
+{RSA_R_INTERNAL_ERROR ,"internal error"},
{RSA_R_INVALID_MESSAGE_LENGTH ,"invalid message length"},
{RSA_R_IQMP_NOT_INVERSE_OF_Q ,"iqmp not inverse of q"},
{RSA_R_KEY_SIZE_TOO_SMALL ,"key size too small"},
diff --git a/crypto/rsa/rsa_lib.c b/crypto/rsa/rsa_lib.c
index d157f75bb4..0b7a4f7b3d 100644
--- a/crypto/rsa/rsa_lib.c
+++ b/crypto/rsa/rsa_lib.c
@@ -326,6 +326,10 @@ int RSA_blinding_on(RSA *rsa, BN_CTX *p_ctx)
if (rsa->blinding != NULL)
BN_BLINDING_free(rsa->blinding);
+ /* NB: similar code appears in setup_blinding (rsa_eay.c);
+ * this should be placed in a new function of its own, but for reasons
+ * of binary compatibility can't */
+
BN_CTX_start(ctx);
A = BN_CTX_get(ctx);
if ((RAND_status() == 0) && rsa->d != NULL && rsa->d->d != NULL)
@@ -343,7 +347,10 @@ int RSA_blinding_on(RSA *rsa, BN_CTX *p_ctx)
if (!ENGINE_get_RSA(rsa->engine)->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);
+ if ((rsa->blinding=BN_BLINDING_new(A,Ai,rsa->n)) == NULL) goto err;
+ /* to make things thread-safe without excessive locking,
+ * rsa->blinding will be used just by the current thread: */
+ rsa->blinding->thread_id = CRYPTO_thread_id();
rsa->flags |= RSA_FLAG_BLINDING;
rsa->flags &= ~RSA_FLAG_NO_BLINDING;
BN_free(Ai);
diff --git a/util/mkerr.pl b/util/mkerr.pl
index 449aa57bba..4eca98689f 100644
--- a/util/mkerr.pl
+++ b/util/mkerr.pl
@@ -320,7 +320,7 @@ EOF
print OUT <<"EOF";
/* $cfile */
/* ====================================================================
- * Copyright (c) 1999-2002 The OpenSSL Project. All rights reserved.
+ * Copyright (c) 1999-2003 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions