Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 1 | Backport patch to fix CVE-2018-0734. Remove a section which only remove a |
| 2 | space. It can't be applied because the context is different. |
| 3 | |
| 4 | CVE: CVE-2018-0734 |
| 5 | Upstream-Status: Backport |
| 6 | |
| 7 | Signed-off-by: Kai Kang <kai.kang@windriver.com> |
| 8 | |
| 9 | From 8abfe72e8c1de1b95f50aa0d9134803b4d00070f Mon Sep 17 00:00:00 2001 |
| 10 | From: Pauli <paul.dale@oracle.com> |
| 11 | Date: Wed, 24 Oct 2018 07:42:46 +1000 |
| 12 | Subject: [PATCH] Timing vulnerability in DSA signature generation |
| 13 | (CVE-2018-0734). |
| 14 | |
| 15 | Avoid a timing attack that leaks information via a side channel that |
| 16 | triggers when a BN is resized. Increasing the size of the BNs |
| 17 | prior to doing anything with them suppresses the attack. |
| 18 | |
| 19 | Thanks due to Samuel Weiser for finding and locating this. |
| 20 | |
| 21 | Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de> |
| 22 | (Merged from https://github.com/openssl/openssl/pull/7486) |
| 23 | |
| 24 | (cherry picked from commit a9cfb8c2aa7254a4aa6a1716909e3f8cb78049b6) |
| 25 | --- |
| 26 | crypto/dsa/dsa_ossl.c | 28 +++++++++++++++------------- |
| 27 | 1 file changed, 15 insertions(+), 13 deletions(-) |
| 28 | |
| 29 | diff --git a/crypto/dsa/dsa_ossl.c b/crypto/dsa/dsa_ossl.c |
| 30 | index ca20811200..2dd2d7489a 100644 |
| 31 | --- a/crypto/dsa/dsa_ossl.c |
| 32 | +++ b/crypto/dsa/dsa_ossl.c |
| 33 | @@ -9,6 +9,7 @@ |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | #include "internal/cryptlib.h" |
| 37 | +#include "internal/bn_int.h" |
| 38 | #include <openssl/bn.h> |
| 39 | #include <openssl/sha.h> |
| 40 | #include "dsa_locl.h" |
| 41 | @@ -180,9 +181,9 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, |
| 42 | { |
| 43 | BN_CTX *ctx = NULL; |
| 44 | BIGNUM *k, *kinv = NULL, *r = *rp; |
| 45 | - BIGNUM *l, *m; |
| 46 | + BIGNUM *l; |
| 47 | int ret = 0; |
| 48 | - int q_bits; |
| 49 | + int q_bits, q_words; |
| 50 | |
| 51 | if (!dsa->p || !dsa->q || !dsa->g) { |
| 52 | DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PARAMETERS); |
| 53 | @@ -191,8 +192,7 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, |
| 54 | |
| 55 | k = BN_new(); |
| 56 | l = BN_new(); |
| 57 | - m = BN_new(); |
| 58 | - if (k == NULL || l == NULL || m == NULL) |
| 59 | + if (k == NULL || l == NULL) |
| 60 | goto err; |
| 61 | |
| 62 | if (ctx_in == NULL) { |
| 63 | @@ -203,9 +203,9 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, |
| 64 | |
| 65 | /* Preallocate space */ |
| 66 | q_bits = BN_num_bits(dsa->q); |
| 67 | - if (!BN_set_bit(k, q_bits) |
| 68 | - || !BN_set_bit(l, q_bits) |
| 69 | - || !BN_set_bit(m, q_bits)) |
| 70 | + q_words = bn_get_top(dsa->q); |
| 71 | + if (!bn_wexpand(k, q_words + 2) |
| 72 | + || !bn_wexpand(l, q_words + 2)) |
| 73 | goto err; |
| 74 | |
| 75 | /* Get random k */ |
| 76 | @@ -240,14 +240,17 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, |
| 77 | * small timing information leakage. We then choose the sum that is |
| 78 | * one bit longer than the modulus. |
| 79 | * |
| 80 | - * TODO: revisit the BN_copy aiming for a memory access agnostic |
| 81 | - * conditional copy. |
| 82 | + * There are some concerns about the efficacy of doing this. More |
| 83 | + * specificly refer to the discussion starting with: |
| 84 | + * https://github.com/openssl/openssl/pull/7486#discussion_r228323705 |
| 85 | + * The fix is to rework BN so these gymnastics aren't required. |
| 86 | */ |
| 87 | if (!BN_add(l, k, dsa->q) |
| 88 | - || !BN_add(m, l, dsa->q) |
| 89 | - || !BN_copy(k, BN_num_bits(l) > q_bits ? l : m)) |
| 90 | + || !BN_add(k, l, dsa->q)) |
| 91 | goto err; |
| 92 | |
| 93 | + BN_consttime_swap(BN_is_bit_set(l, q_bits), k, l, q_words + 2); |
| 94 | + |
| 95 | if ((dsa)->meth->bn_mod_exp != NULL) { |
| 96 | if (!dsa->meth->bn_mod_exp(dsa, r, dsa->g, k, dsa->p, ctx, |
| 97 | dsa->method_mont_p)) |
| 98 | @@ -275,7 +278,6 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, |
| 99 | BN_CTX_free(ctx); |
| 100 | BN_clear_free(k); |
| 101 | BN_clear_free(l); |
| 102 | - BN_clear_free(m); |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | -- |
| 107 | 2.17.0 |
| 108 | |