Skip to content

Commit 106c804

Browse files
FiloSottilegopherbot
authored andcommitted
crypto/rsa: fix keys with p < q
Updates #70643 Change-Id: I4aee8373dbddf774564902b3957c6eba11d15fc9 Reviewed-on: https://go-review.googlesource.com/c/go/+/632955 Reviewed-by: Roland Shoemaker <[email protected]> Auto-Submit: Filippo Valsorda <[email protected]> Reviewed-by: Russ Cox <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent ab59a4f commit 106c804

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/crypto/internal/fips140/rsa/rsa.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,12 @@ func checkPrivateKey(priv *PrivateKey) error {
265265
}
266266

267267
// Check that qInv * q ≡ 1 mod p.
268-
one := q.Nat().Mul(priv.qInv, p)
269-
if one.IsOne() != 1 {
268+
qP, err := bigmod.NewNat().SetOverflowingBytes(q.Nat().Bytes(q), p)
269+
if err != nil {
270+
// q >= 2^⌈log2(p)⌉
271+
qP = bigmod.NewNat().Mod(q.Nat(), p)
272+
}
273+
if qP.Mul(priv.qInv, p).IsOne() != 1 {
270274
return errors.New("crypto/rsa: invalid CRT coefficient")
271275
}
272276

src/crypto/rsa/rsa_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bufio"
99
"bytes"
1010
"crypto"
11+
"crypto/internal/boring"
1112
"crypto/internal/cryptotest"
1213
"crypto/internal/fips140"
1314
"crypto/rand"
@@ -352,6 +353,30 @@ func testEverything(t *testing.T, priv *PrivateKey) {
352353
if err == nil {
353354
t.Errorf("DecryptPKCS1v15 accepted a long ciphertext")
354355
}
356+
357+
der, err := x509.MarshalPKCS8PrivateKey(priv)
358+
if err != nil {
359+
t.Errorf("MarshalPKCS8PrivateKey: %v", err)
360+
}
361+
key, err := x509.ParsePKCS8PrivateKey(der)
362+
if err != nil {
363+
t.Errorf("ParsePKCS8PrivateKey: %v", err)
364+
}
365+
if !key.(*PrivateKey).Equal(priv) {
366+
t.Errorf("private key mismatch")
367+
}
368+
369+
der, err = x509.MarshalPKIXPublicKey(&priv.PublicKey)
370+
if err != nil {
371+
t.Errorf("MarshalPKIXPublicKey: %v", err)
372+
}
373+
pub, err := x509.ParsePKIXPublicKey(der)
374+
if err != nil {
375+
t.Errorf("ParsePKIXPublicKey: %v", err)
376+
}
377+
if !pub.(*PublicKey).Equal(&priv.PublicKey) {
378+
t.Errorf("public key mismatch")
379+
}
355380
}
356381

357382
func TestKeyTooSmall(t *testing.T) {
@@ -1015,3 +1040,21 @@ var testEncryptOAEPData = []testEncryptOAEPStruct{
10151040
},
10161041
},
10171042
}
1043+
1044+
func TestPSmallerThanQ(t *testing.T) {
1045+
// This key has a 256-bit P and a 257-bit Q.
1046+
k := parseKey(testingKey(`-----BEGIN RSA TESTING KEY-----
1047+
MIIBOgIBAAJBAKj34GkxFhD90vcNLYLInFEX6Ppy1tPf9Cnzj4p4WGeKLs1Pt8Qu
1048+
KUpRKfFLfRYC9AIKjbJTWit+CqvjWYzvQwECAwEAAQJAIJLixBy2qpFoS4DSmoEm
1049+
o3qGy0t6z09AIJtH+5OeRV1be+N4cDYJKffGzDa88vQENZiRm0GRq6a+HPGQMd2k
1050+
TQIhAKMSvzIBnni7ot/OSie2TmJLY4SwTQAevXysE2RbFDYdAiEBCUEaRQnMnbp7
1051+
9mxDXDf6AU0cN/RPBjb9qSHDcWZHGzUCIG2Es59z8ugGrDY+pxLQnwfotadxd+Uy
1052+
v/Ow5T0q5gIJAiEAyS4RaI9YG8EWx/2w0T67ZUVAw8eOMB6BIUg0Xcu+3okCIBOs
1053+
/5OiPgoTdSy7bcF9IGpSE8ZgGKzgYQVZeN97YE00
1054+
-----END RSA TESTING KEY-----`))
1055+
t.Setenv("GODEBUG", "rsa1024min=0")
1056+
if boring.Enabled {
1057+
t.Skip("BoringCrypto mode returns the wrong error from SignPSS")
1058+
}
1059+
testEverything(t, k)
1060+
}

0 commit comments

Comments
 (0)