Projects
home:rottame:vhosts-ng
openssl-1_1
openssl-CVE-2026-54872-CVE-2026-77696.patch
Log In
Username
Password
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File openssl-CVE-2026-54872-CVE-2026-77696.patch of Package openssl-1_1
From 9229a79e5038e17c0d8a09d967f83a6ed2d6c07c Mon Sep 17 00:00:00 2001 From: Igor Ustinov <igus@openssl.foundation> Date: Tue, 28 Jul 2026 22:09:29 +0200 Subject: [PATCH 1/2] ec: make ossl_ec_scalar_mul_ladder() scalar padding constant time Fixes CVE-2026-54872 Assisted-by: Claude:claude-opus-4-8 --- crypto/bn/bn_intern.c | 38 ++++++++++++++++++++++++++++++++++++++ crypto/ec/ec_mult.c | 38 ++++++++++++++++++++++++++++++++++++++ include/crypto/bn.h | 1 + 3 files changed, 77 insertions(+) Index: openssl-1.1.1w/crypto/bn/bn_intern.c =================================================================== --- openssl-1.1.1w.orig/crypto/bn/bn_intern.c +++ openssl-1.1.1w/crypto/bn/bn_intern.c @@ -9,6 +9,7 @@ #include "internal/cryptlib.h" #include "bn_local.h" +#include "internal/constant_time.h" /* * Determine the modified width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'. @@ -156,6 +157,43 @@ void bn_set_all_zero(BIGNUM *a) a->d[i] = 0; } +/* + * Zero-extend |a| so that it occupies exactly |words| words, flag it + * BN_FLG_FIXED_TOP and leave its numeric value unchanged. + * + * This is a companion to bn_correct_top(): where the latter minimises the top + * of a BIGNUM, this one pins the top to a caller-chosen, value-independent + * width. Constant-time code uses it to make the cost of subsequent word-wise + * operations (e.g. BN_uadd()/BN_add()) independent of the magnitude of a + * secret value. |words| must be greater than or equal to the current top. + * + * The routine is itself constant time with respect to the current a->top: it + * always sweeps a fixed |words| iterations and selects value-or-zero per word + * with an arithmetic mask, rather than looping over the (possibly secret) + * a->top..words range. Masking the high words with zero also launders any + * uninitialised padding, so it is safe for the memory sanitiser. + */ +int bn_set_top_fixed(BIGNUM *a, int words) +{ + size_t i, n = (size_t)words; + BN_ULONG mask; + + if (words < a->top) + return 0; + if (bn_wexpand(a, words) == NULL) { + BNerr(ERR_LIB_BN, ERR_R_BN_LIB); + return 0; + } + for (i = 0; i < n; i++) { + /* mask = all ones iff i < a->top, else all zeros */ + mask = value_barrier_bn((BN_ULONG)0 - ((i - a->top) >> (8 * sizeof(i) - 1))); + a->d[i] &= mask; + } + a->top = words; + a->flags |= BN_FLG_FIXED_TOP; + return 1; +} + int bn_copy_words(BN_ULONG *out, const BIGNUM *in, int size) { if (in->top > size) Index: openssl-1.1.1w/crypto/ec/ec_mult.c =================================================================== --- openssl-1.1.1w.orig/crypto/ec/ec_mult.c +++ openssl-1.1.1w/crypto/ec/ec_mult.c @@ -212,6 +212,17 @@ int ec_scalar_mul_ladder(const EC_GROUP goto err; } + /* + * Constant-timeness of this copy depends on the caller: BN_copy() moves + * scalar->top words unless |scalar| is flagged BN_FLG_CONSTTIME (in which + * case scalar->dmax words are moved). Secret scalars are therefore + * expected to arrive either BN_FLG_CONSTTIME or fixed-top, so that their + * top is a public, value-independent width and the copy length does not + * leak their magnitude. ECDSA satisfies this via + * ossl_bn_priv_rand_range_fixed_top(). The fixed-top pinning below makes + * the subsequent arithmetic constant time regardless, but cannot + * retroactively fix the copy length here. + */ if (!BN_copy(k, scalar)) { ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB); goto err; @@ -230,10 +241,36 @@ int ec_scalar_mul_ladder(const EC_GROUP } } + /* + * |k| may still carry a top that depends on the value of the secret + * scalar: callers pass either a fixed-top BIGNUM (e.g. the ECDSA nonce) + * or a minimal-top one (e.g. SM2), and BN_copy() above preserves that + * top. Pin |k| to a fixed number of words (matching the group + * cardinality) so that the additions below run in constant time, + * independently of the bit length of the scalar. Otherwise the work + * done by BN_add()/BN_uadd() depends on the operand tops and leaks the + * magnitude of the secret scalar. + */ + if (!bn_set_top_fixed(k, group_top)) { + ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB); + goto err; + } + if (!BN_add(lambda, k, cardinality)) { ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB); goto err; } + /* + * |lambda| = scalar + cardinality may or may not have produced a carry + * into an extra word depending on the secret scalar. Pin its top to one + * word above the group top so that the second addition, which consumes + * |lambda|, is likewise constant time and so that the BN_is_bit_set() + * below always inspects a defined word. + */ + if (!bn_set_top_fixed(lambda, group_top + 1)) { + ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB); + goto err; + } BN_set_flags(lambda, BN_FLG_CONSTTIME); if (!BN_add(k, lambda, cardinality)) { ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB); Index: openssl-1.1.1w/include/crypto/bn.h =================================================================== --- openssl-1.1.1w.orig/include/crypto/bn.h +++ openssl-1.1.1w/include/crypto/bn.h @@ -17,6 +17,7 @@ BIGNUM *bn_wexpand(BIGNUM *a, int words) BIGNUM *bn_expand2(BIGNUM *a, int words); void bn_correct_top(BIGNUM *a); +int bn_set_top_fixed(BIGNUM *a, int words); /* * Determine the modified width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'. Index: openssl-1.1.1w/crypto/sm2/sm2_sign.c =================================================================== --- openssl-1.1.1w.orig/crypto/sm2/sm2_sign.c +++ openssl-1.1.1w/crypto/sm2/sm2_sign.c @@ -12,6 +12,7 @@ #include "crypto/sm2.h" #include "crypto/sm2err.h" #include "crypto/ec.h" /* ec_group_do_inverse_ord() */ +#include "crypto/bn.h" /* fixed-top / Montgomery constant-time BN helpers */ #include "internal/numbers.h" #include <openssl/err.h> #include <openssl/evp.h> @@ -191,11 +192,16 @@ static ECDSA_SIG *sm2_sig_gen(const EC_K EC_POINT *kG = NULL; BN_CTX *ctx = NULL; BIGNUM *k = NULL; - BIGNUM *rk = NULL; BIGNUM *r = NULL; BIGNUM *s = NULL; BIGNUM *x1 = NULL; BIGNUM *tmp = NULL; + BN_MONT_CTX *mont = EC_GROUP_get_mont_data(group); + + if (mont == NULL) { + SM2err(SM2_F_SM2_SIG_GEN, ERR_R_EC_LIB); + goto done; + } kG = EC_POINT_new(group); ctx = BN_CTX_new(); @@ -206,7 +212,6 @@ static ECDSA_SIG *sm2_sig_gen(const EC_K BN_CTX_start(ctx); k = BN_CTX_get(ctx); - rk = BN_CTX_get(ctx); x1 = BN_CTX_get(ctx); tmp = BN_CTX_get(ctx); if (tmp == NULL) { @@ -231,6 +236,55 @@ static ECDSA_SIG *sm2_sig_gen(const EC_K SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR); goto done; } + /* + * Pin the nonce to a fixed, value-independent width and flag it + * BN_FLG_CONSTTIME, so its magnitude does not leak through operand + * lengths in the scalar copy inside the ladder or in the arithmetic + * below. BN_priv_rand_range_ex() is kept so the nonce value itself + * is unchanged; only its representation is pinned. + */ + BN_set_flags(k, BN_FLG_CONSTTIME); + if (!bn_set_top_fixed(k, bn_get_top(order))) { + SM2err(SM2_F_SM2_SIG_GEN, ERR_R_BN_LIB); + goto done; + } + /* + * Pin the nonce to a fixed, value-independent width and flag it + * BN_FLG_CONSTTIME, so its magnitude does not leak through operand + * lengths in the scalar copy inside the ladder or in the arithmetic + * below. BN_priv_rand_range_ex() is kept so the nonce value itself + * is unchanged; only its representation is pinned. + */ + BN_set_flags(k, BN_FLG_CONSTTIME); + if (!bn_set_top_fixed(k, bn_get_top(order))) { + SM2err(SM2_F_SM2_SIG_GEN, ERR_R_BN_LIB); + goto done; + } + + /* + * Pin the nonce to a fixed, value-independent width and flag it + * BN_FLG_CONSTTIME, so its magnitude does not leak through operand + * lengths in the scalar copy inside the ladder or in the arithmetic + * below. BN_priv_rand_range_ex() is kept so the nonce value itself + * is unchanged; only its representation is pinned. + */ + BN_set_flags(k, BN_FLG_CONSTTIME); + if (!bn_set_top_fixed(k, bn_get_top(order))) { + SM2err(SM2_F_SM2_SIG_GEN, ERR_R_BN_LIB); + goto done; + } + /* + * Pin the nonce to a fixed, value-independent width and flag it + * BN_FLG_CONSTTIME, so its magnitude does not leak through operand + * lengths in the scalar copy inside the ladder or in the arithmetic + * below. BN_priv_rand_range_ex() is kept so the nonce value itself + * is unchanged; only its representation is pinned. + */ + BN_set_flags(k, BN_FLG_CONSTTIME); + if (!bn_set_top_fixed(k, bn_get_top(order))) { + SM2err(SM2_F_SM2_SIG_GEN, ERR_R_BN_LIB); + goto done; + } if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx) || !EC_POINT_get_affine_coordinates(group, kG, x1, NULL, @@ -240,23 +294,48 @@ static ECDSA_SIG *sm2_sig_gen(const EC_K goto done; } - /* try again if r == 0 or r+k == n */ + /* try again if r == 0 or r + k == n */ if (BN_is_zero(r)) continue; - if (!BN_add(rk, r, k)) { - SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR); + /* + * Since 0 < r < n and 0 < k < n, r + k == n is the same as + * k == n - r. Both operands of the subtraction are public, so + * compute it in the open and then compare against the nonce with a + * fixed-width constant-time comparison. A BN_cmp() on r + k would + * branch on whether the sum carried into an extra word, which + * depends on the value of k. + */ + if (!BN_sub(tmp, order, r) + || !bn_set_top_fixed(tmp, bn_get_top(order))) { + SM2err(SM2_F_SM2_SIG_GEN, ERR_R_BN_LIB); goto done; } - if (BN_cmp(rk, order) == 0) + if (CRYPTO_memcmp(bn_get_words(k), bn_get_words(tmp), + bn_get_top(order) * sizeof(BN_ULONG)) == 0) continue; + /* + * s = ((1 + dA)^-1 * (k - r * dA)) mod order + * + * Computed with fixed-top / Montgomery constant-time primitives, so + * that the running time does not depend on the secret k or dA (the + * generic BN_mod_mul()/BN_sub() used previously reduce via BN_div(), + * whose timing is value dependent). This mirrors the ECDSA path. + * + * s holds (1 + dA)^-1 throughout; the (k - r * dA) term is built in + * tmp. bn_mul_mont_fixed_top() with one operand in the Montgomery + * domain yields the plain product, and the final + * BN_mod_mul_montgomery() returns the user-visible, normalised value. + */ if (!BN_add(s, dA, BN_value_one()) - || !ec_group_do_inverse_ord(group, s, s, ctx) - || !BN_mod_mul(tmp, dA, r, order, ctx) - || !BN_sub(tmp, k, tmp) - || !BN_mod_mul(s, s, tmp, order, ctx)) { + || !ec_group_do_inverse_ord(group, s, s, ctx) + || !bn_to_mont_fixed_top(tmp, r, mont, ctx) + || !bn_mul_mont_fixed_top(tmp, tmp, dA, mont, ctx) + || !bn_mod_sub_fixed_top(tmp, k, tmp, order) + || !bn_to_mont_fixed_top(tmp, tmp, mont, ctx) + || !BN_mod_mul_montgomery(s, tmp, s, mont, ctx)) { SM2err(SM2_F_SM2_SIG_GEN, ERR_R_BN_LIB); goto done; }
Locations
Projects
Search
Status Monitor
Help
Open Build Service
OBS Manuals
API Documentation
OBS Portal
Reporting a Bug
Contact
Mailing List
Forums
Chat (IRC)
Twitter
Open Build Service (OBS)
is an
openSUSE project
.