-/* $OpenBSD: rsa_blinding.c,v 1.2 2023/08/09 09:26:43 tb Exp $ */
+/* $OpenBSD: rsa_blinding.c,v 1.3 2023/08/09 12:09:06 tb Exp $ */
/* ====================================================================
* Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
*
* [including the GNU Public Licence.]
*/
+#include <pthread.h>
#include <stdio.h>
#include <openssl/opensslconf.h>
BIGNUM *Ai;
BIGNUM *e;
BIGNUM *mod;
- CRYPTO_THREADID tid;
+ pthread_t tid;
int counter;
BN_MONT_CTX *m_ctx;
int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
/* Update on first use. */
ret->counter = BN_BLINDING_COUNTER - 1;
- CRYPTO_THREADID_current(&ret->tid);
+ ret->tid = pthread_self();
if (bn_mod_exp != NULL)
ret->bn_mod_exp = bn_mod_exp;
return BN_mod_mul(n, n, inv, b->mod, ctx);
}
-CRYPTO_THREADID *
-BN_BLINDING_thread_id(BN_BLINDING *b)
+int
+BN_BLINDING_is_local(BN_BLINDING *b)
{
- return &b->tid;
+ return pthread_equal(pthread_self(), b->tid) != 0;
}
static BIGNUM *
RSAerror(ERR_R_BN_LIB);
goto err;
}
- CRYPTO_THREADID_current(BN_BLINDING_thread_id(ret));
err:
BN_CTX_end(ctx);
-/* $OpenBSD: rsa_eay.c,v 1.64 2023/08/09 09:32:23 tb Exp $ */
+/* $OpenBSD: rsa_eay.c,v 1.65 2023/08/09 12:09:06 tb Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
{
BN_BLINDING *ret;
int got_write_lock = 0;
- CRYPTO_THREADID cur;
CRYPTO_r_lock(CRYPTO_LOCK_RSA);
rsa->blinding = RSA_setup_blinding(rsa, ctx);
}
- ret = rsa->blinding;
- if (ret == NULL)
+ if ((ret = rsa->blinding) == NULL)
goto err;
- CRYPTO_THREADID_current(&cur);
- if (!CRYPTO_THREADID_cmp(&cur, BN_BLINDING_thread_id(ret))) {
- /* rsa->blinding is ours! */
- *local = 1;
- } else {
- /* resort to rsa->mt_blinding instead */
- /*
- * Instruct rsa_blinding_convert(), rsa_blinding_invert()
- * that the BN_BLINDING is shared, meaning that accesses
- * require locks, and that the blinding factor must be
- * stored outside the BN_BLINDING
- */
- *local = 0;
-
+ /*
+ * We need a shared blinding. Accesses require locks and a copy of the
+ * blinding factor needs to be retained on use.
+ */
+ if ((*local = BN_BLINDING_is_local(ret)) == 0) {
if (rsa->mt_blinding == NULL) {
if (!got_write_lock) {
CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
ret = rsa->mt_blinding;
}
-err:
+ err:
if (got_write_lock)
CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
else
CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
+
return ret;
}
-/* $OpenBSD: rsa_local.h,v 1.5 2023/08/09 09:23:03 tb Exp $ */
+/* $OpenBSD: rsa_local.h,v 1.6 2023/08/09 12:09:06 tb Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
void BN_BLINDING_free(BN_BLINDING *b);
int BN_BLINDING_convert(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *);
int BN_BLINDING_invert(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *);
-CRYPTO_THREADID *BN_BLINDING_thread_id(BN_BLINDING *);
+int BN_BLINDING_is_local(BN_BLINDING *b);
BN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *ctx);
__END_HIDDEN_DECLS