]> Cypherpunks repositories - gostls13.git/commitdiff
crypto/rsa, crypto/ecdsa: fail earlier on zero parameters
authorBrad Fitzpatrick <bradfitz@golang.org>
Tue, 5 Apr 2016 20:40:40 +0000 (20:40 +0000)
committerAndrew Gerrand <adg@golang.org>
Fri, 8 Apr 2016 05:21:07 +0000 (05:21 +0000)
Change-Id: Ia6ed49d5ef3a256a55e6d4eaa1b4d9f0fc447013
Reviewed-on: https://go-review.googlesource.com/21560
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-on: https://go-review.googlesource.com/21695
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/crypto/ecdsa/ecdsa.go
src/crypto/rsa/rsa.go

index 0731f2b670345dc0336f231cafaf454647dc46dc..e54488c9cf6b699d4d8d5716101d278218400b06 100644 (file)
@@ -23,6 +23,7 @@ import (
        "crypto/elliptic"
        "crypto/sha512"
        "encoding/asn1"
+       "errors"
        "io"
        "math/big"
 )
@@ -140,6 +141,8 @@ func fermatInverse(k, N *big.Int) *big.Int {
        return new(big.Int).Exp(k, nMinus2, N)
 }
 
+var errZeroParam = errors.New("zero parameter")
+
 // Sign signs an arbitrary length hash (which should be the result of hashing a
 // larger message) using the private key, priv. It returns the signature as a
 // pair of integers. The security of the private key depends on the entropy of
@@ -180,7 +183,9 @@ func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err err
        // See [NSA] 3.4.1
        c := priv.PublicKey.Curve
        N := c.Params().N
-
+       if N.Sign() == 0 {
+               return nil, nil, errZeroParam
+       }
        var k, kInv *big.Int
        for {
                for {
@@ -193,7 +198,7 @@ func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err err
                        if in, ok := priv.Curve.(invertible); ok {
                                kInv = in.Inverse(k)
                        } else {
-                               kInv = fermatInverse(k, N)
+                               kInv = fermatInverse(k, N) // N != 0
                        }
 
                        r, _ = priv.Curve.ScalarBaseMult(k.Bytes())
@@ -207,7 +212,7 @@ func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err err
                s = new(big.Int).Mul(priv.D, r)
                s.Add(s, e)
                s.Mul(s, kInv)
-               s.Mod(s, N)
+               s.Mod(s, N) // N != 0
                if s.Sign() != 0 {
                        break
                }
index ee022b803ae21fffd6d67ed2922a883c6d56dcbf..0f487fe1524b2137cefd05a6ba90e1fee99e4ec6 100644 (file)
@@ -465,6 +465,9 @@ func decrypt(random io.Reader, priv *PrivateKey, c *big.Int) (m *big.Int, err er
                err = ErrDecryption
                return
        }
+       if priv.N.Sign() == 0 {
+               return nil, ErrDecryption
+       }
 
        var ir *big.Int
        if random != nil {
@@ -490,7 +493,7 @@ func decrypt(random io.Reader, priv *PrivateKey, c *big.Int) (m *big.Int, err er
                        }
                }
                bigE := big.NewInt(int64(priv.E))
-               rpowe := new(big.Int).Exp(r, bigE, priv.N)
+               rpowe := new(big.Int).Exp(r, bigE, priv.N) // N != 0
                cCopy := new(big.Int).Set(c)
                cCopy.Mul(cCopy, rpowe)
                cCopy.Mod(cCopy, priv.N)