Skip to content

Commit

Permalink
Timing vulnerability in DSA signature generation (CVE-2018-0734).
Browse files Browse the repository at this point in the history
Avoid a timing attack that leaks information via a side channel that
triggers when a BN is resized.  Increasing the size of the BNs
prior to doing anything with them suppresses the attack.

Thanks due to Samuel Weiser for finding and locating this.

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from #7486)

(cherry picked from commit a9cfb8c)
  • Loading branch information
paulidale committed Oct 28, 2018
1 parent f1b12b8 commit 8abfe72
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions crypto/dsa/dsa_ossl.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <stdio.h>
#include "internal/cryptlib.h"
#include "internal/bn_int.h"
#include <openssl/bn.h>
#include <openssl/sha.h>
#include "dsa_locl.h"
Expand Down Expand Up @@ -180,9 +181,9 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
{
BN_CTX *ctx = NULL;
BIGNUM *k, *kinv = NULL, *r = *rp;
BIGNUM *l, *m;
BIGNUM *l;
int ret = 0;
int q_bits;
int q_bits, q_words;

if (!dsa->p || !dsa->q || !dsa->g) {
DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PARAMETERS);
Expand All @@ -191,8 +192,7 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,

k = BN_new();
l = BN_new();
m = BN_new();
if (k == NULL || l == NULL || m == NULL)
if (k == NULL || l == NULL)
goto err;

if (ctx_in == NULL) {
Expand All @@ -203,9 +203,9 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,

/* Preallocate space */
q_bits = BN_num_bits(dsa->q);
if (!BN_set_bit(k, q_bits)
|| !BN_set_bit(l, q_bits)
|| !BN_set_bit(m, q_bits))
q_words = bn_get_top(dsa->q);
if (!bn_wexpand(k, q_words + 2)
|| !bn_wexpand(l, q_words + 2))
goto err;

/* Get random k */
Expand Down Expand Up @@ -240,14 +240,17 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
* small timing information leakage. We then choose the sum that is
* one bit longer than the modulus.
*
* TODO: revisit the BN_copy aiming for a memory access agnostic
* conditional copy.
* There are some concerns about the efficacy of doing this. More
* specificly refer to the discussion starting with:
* https://github.com/openssl/openssl/pull/7486#discussion_r228323705
* The fix is to rework BN so these gymnastics aren't required.
*/
if (!BN_add(l, k, dsa->q)
|| !BN_add(m, l, dsa->q)
|| !BN_copy(k, BN_num_bits(l) > q_bits ? l : m))
|| !BN_add(k, l, dsa->q))
goto err;

BN_consttime_swap(BN_is_bit_set(l, q_bits), k, l, q_words + 2);

if ((dsa)->meth->bn_mod_exp != NULL) {
if (!dsa->meth->bn_mod_exp(dsa, r, dsa->g, k, dsa->p, ctx,
dsa->method_mont_p))
Expand All @@ -260,7 +263,7 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
if (!BN_mod(r, r, dsa->q, ctx))
goto err;

/* Compute part of 's = inv(k) (m + xr) mod q' */
/* Compute part of 's = inv(k) (m + xr) mod q' */
if ((kinv = dsa_mod_inverse_fermat(k, dsa->q, ctx)) == NULL)
goto err;

Expand All @@ -275,7 +278,6 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
BN_CTX_free(ctx);
BN_clear_free(k);
BN_clear_free(l);
BN_clear_free(m);
return ret;
}

Expand Down

2 comments on commit 8abfe72

@Fnaste
Copy link

@Fnaste Fnaste commented on 8abfe72 Oct 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just what is cherry picking? I'm curious.

@mattcaswell
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just what is cherry picking? I'm curious.

The process of copying a commit from one branch to another in git. In this case the original commit was on the "master" branch, and it was copied into the "OpenSSL_1_1_1-stable" branch. Search for "git cherry-pick"

Please sign in to comment.