From nobody Tue Mar 15 18:16:10 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id DCE7C1A24BC7; Tue, 15 Mar 2022 18:16:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4KJ1n64923z58mc; Tue, 15 Mar 2022 18:16:10 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1647368170; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=cxAOXLxFn0MUClSXbhyDvVX2u+GjfGrQf2ldHXHdpDo=; b=V5K9vW6BV5sFxOTqFv82N0roTlDJrhMSzWSTd2Cp9jLRutwiMQYMoH/DCQ5ncFiAKCufWl G6tNWn+WYsgSD5yaNgqSqPTbV9MhHvcCyLuQwc86R88JpsQtpRsBAo3KypXkVyb0ETk864 4g49h08As4+x6MrRKRPGIJ3lyjYxiZOul9gbwKOaF+SO/i4AnXawI6tsff8DXNR/sC5USg NV3aPHLUf8hdyBq10fWLGdRCYGS3eY5Be9sJktfvuHdCg9WYHpsyd/EX115M6/WAc/4uk0 MbtabWKZZVpacfcXKqzMOZ8y5ryDWPzyyeUEistvxGVrR+f2/NjOrCwAS0DuSg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 6E14D26B1A; Tue, 15 Mar 2022 18:16:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 22FIGAlZ075519; Tue, 15 Mar 2022 18:16:10 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 22FIGArh075518; Tue, 15 Mar 2022 18:16:10 GMT (envelope-from git) Date: Tue, 15 Mar 2022 18:16:10 GMT Message-Id: <202203151816.22FIGArh075518@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: 7895cf4ce4f7 - releng/12.3 - Fix a bug in BN_mod_sqrt() that can cause it to loop forever. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/releng/12.3 X-Git-Reftype: branch X-Git-Commit: 7895cf4ce4f7cb3bdf4f84388e97035cf256f97c Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1647368170; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=cxAOXLxFn0MUClSXbhyDvVX2u+GjfGrQf2ldHXHdpDo=; b=cAvrKIkcLcH/7tLu5exMsSuvNIavni3fNbDotKGqA/ahk1QC3QT6s3G0hFAbVWg8P3xHwv TxGiGxdxr07jM8NJAY4w35jSpzQ3pcQPFlyVc6ltXeqY1uHZsOKdnrnXSyyp0iMvwCCl9I MAzFgVW7eYzeMzT8Hy6p2ngwC76Vk6bqT5oOtPo8XJ+M4EPxe1ZvH597Z8x/sDCKISeh4T /haN7n5+6IddT3oR7WkvM7/9kWmgBbkrFlAkktJQdhp1XDfZVzX44R92iL+H5r8A2B/H7G m66G6dVoHIO4wpIHQmJsTbk4/I7bT2zOIpDMDcJURdce3IHJWJotTaDzF1uCEw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1647368170; a=rsa-sha256; cv=none; b=ij00dnu6iGDoz4P9deXF8nZU0rhU8sxUO9sv+B2nPamkJxqjpfUcOjKSiIQd6ISXvI/Ohx vGjt4Fi19fFs1iGI20spInm+NZVumtyozk1C1WBbAJ4bXyqzTvt59ZsYtJ7/OHpS5Ci3do 6f4qqQfcNFDAF0nAWNRVAIEx4f+FoyYgEuqCq7AlG+XR7UXEm/f+U9EBgAqulPLxXyuAFc 5rb/tMmK2Xr+4fMTtNG9D+/ExnwSKR+BCZXttwCtiggP5CjvZoVhNlJC8mTWk5TJQ9Qj/A MJ7l19AY0IVCMBbfTj0FAgktt0ACnm4ytQ03TeOu19KfD9B7/wia7s5qwHFX1w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch releng/12.3 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=7895cf4ce4f7cb3bdf4f84388e97035cf256f97c commit 7895cf4ce4f7cb3bdf4f84388e97035cf256f97c Author: Gordon Tetlow AuthorDate: 2022-03-15 16:48:59 +0000 Commit: Mark Johnston CommitDate: 2022-03-15 17:46:45 +0000 Fix a bug in BN_mod_sqrt() that can cause it to loop forever. Obtained from: OpenSSL Project Security: CVE-2022-0778 Security: FreeBSD-SA-22:03.openssl (cherry picked from commit fdc418f15e92732a3551832bcb625ba9b47242df) (cherry picked from commit c2a7d6e643bbc8801b1b83c9e64d57e726eeed98) Approved by: so --- crypto/openssl/crypto/bn/bn_sqrt.c | 30 ++++++++++++++++++------------ crypto/openssl/doc/man3/BN_add.pod | 15 +++++++++++++-- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/crypto/openssl/crypto/bn/bn_sqrt.c b/crypto/openssl/crypto/bn/bn_sqrt.c index 1723d5ded5a8..53b0f559855c 100644 --- a/crypto/openssl/crypto/bn/bn_sqrt.c +++ b/crypto/openssl/crypto/bn/bn_sqrt.c @@ -14,7 +14,8 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) /* * Returns 'ret' such that ret^2 == a (mod p), using the Tonelli/Shanks * algorithm (cf. Henri Cohen, "A Course in Algebraic Computational Number - * Theory", algorithm 1.5.1). 'p' must be prime! + * Theory", algorithm 1.5.1). 'p' must be prime, otherwise an error or + * an incorrect "result" will be returned. */ { BIGNUM *ret = in; @@ -301,18 +302,23 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) goto vrfy; } - /* find smallest i such that b^(2^i) = 1 */ - i = 1; - if (!BN_mod_sqr(t, b, p, ctx)) - goto end; - while (!BN_is_one(t)) { - i++; - if (i == e) { - BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE); - goto end; + /* Find the smallest i, 0 < i < e, such that b^(2^i) = 1. */ + for (i = 1; i < e; i++) { + if (i == 1) { + if (!BN_mod_sqr(t, b, p, ctx)) + goto end; + + } else { + if (!BN_mod_mul(t, t, t, p, ctx)) + goto end; } - if (!BN_mod_mul(t, t, t, p, ctx)) - goto end; + if (BN_is_one(t)) + break; + } + /* If not found, a is not a square or p is not prime. */ + if (i >= e) { + BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE); + goto end; } /* t := y^2^(e - i - 1) */ diff --git a/crypto/openssl/doc/man3/BN_add.pod b/crypto/openssl/doc/man3/BN_add.pod index dccd4790ede7..1f5e37a4d183 100644 --- a/crypto/openssl/doc/man3/BN_add.pod +++ b/crypto/openssl/doc/man3/BN_add.pod @@ -3,7 +3,7 @@ =head1 NAME BN_add, BN_sub, BN_mul, BN_sqr, BN_div, BN_mod, BN_nnmod, BN_mod_add, -BN_mod_sub, BN_mod_mul, BN_mod_sqr, BN_exp, BN_mod_exp, BN_gcd - +BN_mod_sub, BN_mod_mul, BN_mod_sqr, BN_mod_sqrt, BN_exp, BN_mod_exp, BN_gcd - arithmetic operations on BIGNUMs =head1 SYNOPSIS @@ -36,6 +36,8 @@ arithmetic operations on BIGNUMs int BN_mod_sqr(BIGNUM *r, BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); + BIGNUM *BN_mod_sqrt(BIGNUM *in, BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); + int BN_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BN_CTX *ctx); int BN_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p, @@ -87,6 +89,12 @@ L. BN_mod_sqr() takes the square of I modulo B and places the result in I. +BN_mod_sqrt() returns the modular square root of I such that +C. The modulus I

must be a +prime, otherwise an error or an incorrect "result" will be returned. +The result is stored into I which can be NULL. The result will be +newly allocated in that case. + BN_exp() raises I to the I

-th power and places the result in I (C). This function is faster than repeated applications of BN_mul(). @@ -108,7 +116,10 @@ the arguments. =head1 RETURN VALUES -For all functions, 1 is returned for success, 0 on error. The return +The BN_mod_sqrt() returns the result (possibly incorrect if I

is +not a prime), or NULL. + +For all remaining functions, 1 is returned for success, 0 on error. The return value should always be checked (e.g., C). The error codes can be obtained by L.