diff options
author | Tomas Mraz <tomas@openssl.org> | 2023-04-11 16:24:44 +0200 |
---|---|---|
committer | Tomas Mraz <tomas@openssl.org> | 2023-04-13 15:23:05 +0200 |
commit | efbff4de3e259cee71a4e1bbd86b30ebd86bbdae (patch) | |
tree | e46415bf08e2a004afe63d691f097d33aea5e71a | |
parent | e35a21334172997c40928933e1f44e3ba4d2317c (diff) |
Fix the LCM computation in the RSA multiprime key check
Fixes #20693
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tom Cosgrove <tom.cosgrove@arm.com>
(Merged from https://github.com/openssl/openssl/pull/20708)
-rw-r--r-- | crypto/rsa/rsa_chk.c | 16 | ||||
-rw-r--r-- | test/recipes/15-test_mp_rsa.t | 5 | ||||
-rw-r--r-- | test/recipes/15-test_mp_rsa_data/rsamplcm.pem | 55 |
3 files changed, 69 insertions, 7 deletions
diff --git a/crypto/rsa/rsa_chk.c b/crypto/rsa/rsa_chk.c index f2fc89285b..73ac607da9 100644 --- a/crypto/rsa/rsa_chk.c +++ b/crypto/rsa/rsa_chk.c @@ -124,13 +124,17 @@ static int rsa_validate_keypair_multiprime(const RSA *key, BN_GENCB *cb) ret = -1; goto err; } + if (!BN_div(m, NULL, l, m, ctx)) { /* remainder is 0 */ + ret = -1; + goto err; + } for (idx = 0; idx < ex_primes; idx++) { pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx); if (!BN_sub(k, pinfo->r, BN_value_one())) { ret = -1; goto err; } - if (!BN_mul(l, l, k, ctx)) { + if (!BN_mul(l, m, k, ctx)) { ret = -1; goto err; } @@ -138,12 +142,12 @@ static int rsa_validate_keypair_multiprime(const RSA *key, BN_GENCB *cb) ret = -1; goto err; } + if (!BN_div(m, NULL, l, m, ctx)) { /* remainder is 0 */ + ret = -1; + goto err; + } } - if (!BN_div(k, NULL, l, m, ctx)) { /* remainder is 0 */ - ret = -1; - goto err; - } - if (!BN_mod_mul(i, key->d, key->e, k, ctx)) { + if (!BN_mod_mul(i, key->d, key->e, m, ctx)) { ret = -1; goto err; } diff --git a/test/recipes/15-test_mp_rsa.t b/test/recipes/15-test_mp_rsa.t index 2ab4e56f93..ffaf36cd52 100644 --- a/test/recipes/15-test_mp_rsa.t +++ b/test/recipes/15-test_mp_rsa.t @@ -35,10 +35,13 @@ my @test_param = ( }, ); -plan tests => 1 + scalar(@test_param) * 5 * 2; +plan tests => 2 + scalar(@test_param) * 5 * 2; ok(run(test(["rsa_mp_test"])), "running rsa multi prime test"); +ok(run(app(['openssl', 'pkey', '-noout', '-check', '-in', + data_file('rsamplcm.pem')])), "checking lcm in key check"); + my $cleartext = data_file("plain_text"); # genrsa diff --git a/test/recipes/15-test_mp_rsa_data/rsamplcm.pem b/test/recipes/15-test_mp_rsa_data/rsamplcm.pem new file mode 100644 index 0000000000..3196ea063a --- /dev/null +++ b/test/recipes/15-test_mp_rsa_data/rsamplcm.pem @@ -0,0 +1,55 @@ +-----BEGIN PRIVATE KEY----- +MIIJ3gIBADANBgkqhkiG9w0BAQEFAASCCcgwggnEAgEBAoICAQChMJM44ZMm2OQI +eKTnSEFib0cmahfRV8Byh5fKbcliL8aHRx9IJskg72JRiFfwnsWCYfsrrNzSAY4B +phPE4gZzdN45HTc32rvSzxImNBiIvhA3duzMmFwWBO9RBrNNZQZI3HJ/P3S0iNfV +ZvJz75ClJaQOtXrF9yluBaoFW7spRfMQNXqvYqZXXYMfx8KiQ62FWPEmQOAYrArn +PgB58LJKPAP2d2TwhEKGYjfi45zWAb4GUhCVYqXev/zQyW+oyvhcxlSSrtfmyFpU +7q7crI8ZLz7DLelG2jx5Z83+IiOahIRtENma2Ct+2e0dnH+vrq6eVRiKaHnmBwrR +oR9Oot13kSUhHsYekWDqZDkeAUNxCnoV9Op/I/vsON6fBXKa2N/sICRDihJGLdaN +/Ust2z+Jrn1BBAVEaJVZeePiPJSb6JtVW66erhfkxhyLx17mTNF5v1VRK+M8ZdVt +WUxNCisSCVi3aEVQ5hpcVmOgynR3f1rEDO4WuoOoq/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