summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortb <>2023-04-05 11:30:12 +0000
committertb <>2023-04-05 11:30:12 +0000
commite8c1a52cfab8796d0aed6aead31cc8e12c4b09d3 (patch)
treea5d86bc2769c8a5d18b67654d35de99abf413540
parent2db0dca691f89b76656d40a20829357632754405 (diff)
downloadopenbsd-e8c1a52cfab8796d0aed6aead31cc8e12c4b09d3.tar.gz
openbsd-e8c1a52cfab8796d0aed6aead31cc8e12c4b09d3.tar.bz2
openbsd-e8c1a52cfab8796d0aed6aead31cc8e12c4b09d3.zip
Set up the RSA's _method_mod_n before the initial blinding
As observed by Bernd Edlinger, the main part of the RSA timing leak that was recently made public is that the initial blinding isn't done with Montgomery exponentiation but rather with plain exponentiation. Pull up the initialization of the cached Montgomery context to ensure we use Montgomery exponentiation. Do this for private_{de,en}crypt(). Interestingly, the latter was fixed in OpenSSL a while ago by Andy Polyakov as part of the "smooth CRT-RSA" addition. If this code was anything but completely insane this would never have been an issue in the first place. But it's libcrypto... ok jsing
-rw-r--r--src/lib/libcrypto/rsa/rsa_eay.c24
1 files changed, 13 insertions, 11 deletions
diff --git a/src/lib/libcrypto/rsa/rsa_eay.c b/src/lib/libcrypto/rsa/rsa_eay.c
index a19201727e..089b8782d5 100644
--- a/src/lib/libcrypto/rsa/rsa_eay.c
+++ b/src/lib/libcrypto/rsa/rsa_eay.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: rsa_eay.c,v 1.56 2022/12/26 07:18:52 jmc Exp $ */ 1/* $OpenBSD: rsa_eay.c,v 1.57 2023/04/05 11:30:12 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -403,6 +403,12 @@ RSA_eay_private_encrypt(int flen, const unsigned char *from, unsigned char *to,
403 goto err; 403 goto err;
404 } 404 }
405 405
406 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
407 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n,
408 CRYPTO_LOCK_RSA, rsa->n, ctx))
409 goto err;
410 }
411
406 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) { 412 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
407 blinding = rsa_get_blinding(rsa, &local_blinding, ctx); 413 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
408 if (blinding == NULL) { 414 if (blinding == NULL) {
@@ -431,11 +437,6 @@ RSA_eay_private_encrypt(int flen, const unsigned char *from, unsigned char *to,
431 BN_init(&d); 437 BN_init(&d);
432 BN_with_flags(&d, rsa->d, BN_FLG_CONSTTIME); 438 BN_with_flags(&d, rsa->d, BN_FLG_CONSTTIME);
433 439
434 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
435 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n,
436 CRYPTO_LOCK_RSA, rsa->n, ctx))
437 goto err;
438
439 if (!rsa->meth->bn_mod_exp(ret, f, &d, rsa->n, ctx, 440 if (!rsa->meth->bn_mod_exp(ret, f, &d, rsa->n, ctx,
440 rsa->_method_mod_n)) { 441 rsa->_method_mod_n)) {
441 goto err; 442 goto err;
@@ -521,6 +522,12 @@ RSA_eay_private_decrypt(int flen, const unsigned char *from, unsigned char *to,
521 goto err; 522 goto err;
522 } 523 }
523 524
525 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
526 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n,
527 CRYPTO_LOCK_RSA, rsa->n, ctx))
528 goto err;
529 }
530
524 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) { 531 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
525 blinding = rsa_get_blinding(rsa, &local_blinding, ctx); 532 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
526 if (blinding == NULL) { 533 if (blinding == NULL) {
@@ -550,11 +557,6 @@ RSA_eay_private_decrypt(int flen, const unsigned char *from, unsigned char *to,
550 BN_init(&d); 557 BN_init(&d);
551 BN_with_flags(&d, rsa->d, BN_FLG_CONSTTIME); 558 BN_with_flags(&d, rsa->d, BN_FLG_CONSTTIME);
552 559
553 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
554 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n,
555 CRYPTO_LOCK_RSA, rsa->n, ctx))
556 goto err;
557
558 if (!rsa->meth->bn_mod_exp(ret, f, &d, rsa->n, ctx, 560 if (!rsa->meth->bn_mod_exp(ret, f, &d, rsa->n, ctx,
559 rsa->_method_mod_n)) { 561 rsa->_method_mod_n)) {
560 goto err; 562 goto err;