From f20f8b8b0a4236c7438c830261a5860cbd9efe80 Mon Sep 17 00:00:00 2001 From: Adam Langley Date: Mon, 25 Mar 2013 19:08:29 -0400 Subject: [PATCH] crypto/rsa: don't correct private exponent unless needed. At some point in the past, I believe the GCD algorithm was setting d to be negative. The RSA code has been correcting that ever since but, now, it appears to have changed and the correction isn't needed. Having d be too large is harmless, it's just a little odd and I happened to notice. R=golang-dev, rsc CC=golang-dev https://golang.org/cl/7948044 --- src/pkg/crypto/rsa/rsa.go | 4 +++- src/pkg/crypto/rsa/rsa_test.go | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/pkg/crypto/rsa/rsa.go b/src/pkg/crypto/rsa/rsa.go index 35a5f7c3c6a..f56fb37ee54 100644 --- a/src/pkg/crypto/rsa/rsa.go +++ b/src/pkg/crypto/rsa/rsa.go @@ -203,7 +203,9 @@ NextSetOfPrimes: g.GCD(priv.D, y, e, totient) if g.Cmp(bigOne) == 0 { - priv.D.Add(priv.D, totient) + if priv.D.Sign() < 0 { + priv.D.Add(priv.D, totient) + } priv.Primes = primes priv.N = n diff --git a/src/pkg/crypto/rsa/rsa_test.go b/src/pkg/crypto/rsa/rsa_test.go index f08cfe73c4c..ffd96e62f64 100644 --- a/src/pkg/crypto/rsa/rsa_test.go +++ b/src/pkg/crypto/rsa/rsa_test.go @@ -93,6 +93,9 @@ func testKeyBasics(t *testing.T, priv *PrivateKey) { if err := priv.Validate(); err != nil { t.Errorf("Validate() failed: %s", err) } + if priv.D.Cmp(priv.N) > 0 { + t.Errorf("private exponent too large") + } pub := &priv.PublicKey m := big.NewInt(42)