| 1 |
// Copyright 2022 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package ecdsa |
| 6 |
|
| 7 |
import ( |
| 8 |
"crypto/elliptic" |
| 9 |
"crypto/internal/fips140only" |
| 10 |
"errors" |
| 11 |
"io" |
| 12 |
"math/big" |
| 13 |
"math/rand/v2" |
| 14 |
|
| 15 |
"golang.org/x/crypto/cryptobyte" |
| 16 |
"golang.org/x/crypto/cryptobyte/asn1" |
| 17 |
) |
| 18 |
|
| 19 |
// This file contains a math/big implementation of ECDSA that is only used for |
| 20 |
// deprecated custom curves. |
| 21 |
|
| 22 |
func generateLegacy(c elliptic.Curve, rand io.Reader) (*PrivateKey, error) { |
| 23 |
if fips140only.Enabled { |
| 24 |
return nil, errors.New("crypto/ecdsa: use of custom curves is not allowed in FIPS 140-only mode") |
| 25 |
} |
| 26 |
|
| 27 |
k, err := randFieldElement(c, rand) |
| 28 |
if err != nil { |
| 29 |
return nil, err |
| 30 |
} |
| 31 |
|
| 32 |
priv := new(PrivateKey) |
| 33 |
priv.PublicKey.Curve = c |
| 34 |
priv.D = k |
| 35 |
priv.PublicKey.X, priv.PublicKey.Y = c.ScalarBaseMult(k.Bytes()) |
| 36 |
return priv, nil |
| 37 |
} |
| 38 |
|
| 39 |
// hashToInt converts a hash value to an integer. Per FIPS 186-4, Section 6.4, |
| 40 |
// we use the left-most bits of the hash to match the bit-length of the order of |
| 41 |
// the curve. This also performs Step 5 of SEC 1, Version 2.0, Section 4.1.3. |
| 42 |
func hashToInt(hash []byte, c elliptic.Curve) *big.Int { |
| 43 |
orderBits := c.Params().N.BitLen() |
| 44 |
orderBytes := (orderBits + 7) / 8 |
| 45 |
if len(hash) > orderBytes { |
| 46 |
hash = hash[:orderBytes] |
| 47 |
} |
| 48 |
|
| 49 |
ret := new(big.Int).SetBytes(hash) |
| 50 |
excess := len(hash)*8 - orderBits |
| 51 |
if excess > 0 { |
| 52 |
ret.Rsh(ret, uint(excess)) |
| 53 |
} |
| 54 |
return ret |
| 55 |
} |
| 56 |
|
| 57 |
var errZeroParam = errors.New("zero parameter") |
| 58 |
|
| 59 |
// Sign signs a hash (which should be the result of hashing a larger message) |
| 60 |
// using the private key, priv. If the hash is longer than the bit-length of the |
| 61 |
// private key's curve order, the hash will be truncated to that length. It |
| 62 |
// returns the signature as a pair of integers. Most applications should use |
| 63 |
// [SignASN1] instead of dealing directly with r, s. |
| 64 |
func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err error) { |
| 65 |
sig, err := SignASN1(rand, priv, hash) |
| 66 |
if err != nil { |
| 67 |
return nil, nil, err |
| 68 |
} |
| 69 |
|
| 70 |
r, s = new(big.Int), new(big.Int) |
| 71 |
var inner cryptobyte.String |
| 72 |
input := cryptobyte.String(sig) |
| 73 |
if !input.ReadASN1(&inner, asn1.SEQUENCE) || |
| 74 |
!input.Empty() || |
| 75 |
!inner.ReadASN1Integer(r) || |
| 76 |
!inner.ReadASN1Integer(s) || |
| 77 |
!inner.Empty() { |
| 78 |
return nil, nil, errors.New("invalid ASN.1 from SignASN1") |
| 79 |
} |
| 80 |
return r, s, nil |
| 81 |
} |
| 82 |
|
| 83 |
func signLegacy(priv *PrivateKey, csprng io.Reader, hash []byte) (sig []byte, err error) { |
| 84 |
if fips140only.Enabled { |
| 85 |
return nil, errors.New("crypto/ecdsa: use of custom curves is not allowed in FIPS 140-only mode") |
| 86 |
} |
| 87 |
|
| 88 |
c := priv.Curve |
| 89 |
|
| 90 |
// A cheap version of hedged signatures, for the deprecated path. |
| 91 |
var seed [32]byte |
| 92 |
if _, err := io.ReadFull(csprng, seed[:]); err != nil { |
| 93 |
return nil, err |
| 94 |
} |
| 95 |
for i, b := range priv.D.Bytes() { |
| 96 |
seed[i%32] ^= b |
| 97 |
} |
| 98 |
for i, b := range hash { |
| 99 |
seed[i%32] ^= b |
| 100 |
} |
| 101 |
csprng = rand.NewChaCha8(seed) |
| 102 |
|
| 103 |
// SEC 1, Version 2.0, Section 4.1.3 |
| 104 |
N := c.Params().N |
| 105 |
if N.Sign() == 0 { |
| 106 |
return nil, errZeroParam |
| 107 |
} |
| 108 |
var k, kInv, r, s *big.Int |
| 109 |
for { |
| 110 |
for { |
| 111 |
k, err = randFieldElement(c, csprng) |
| 112 |
if err != nil { |
| 113 |
return nil, err |
| 114 |
} |
| 115 |
|
| 116 |
kInv = new(big.Int).ModInverse(k, N) |
| 117 |
|
| 118 |
r, _ = c.ScalarBaseMult(k.Bytes()) |
| 119 |
r.Mod(r, N) |
| 120 |
if r.Sign() != 0 { |
| 121 |
break |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
e := hashToInt(hash, c) |
| 126 |
s = new(big.Int).Mul(priv.D, r) |
| 127 |
s.Add(s, e) |
| 128 |
s.Mul(s, kInv) |
| 129 |
s.Mod(s, N) // N != 0 |
| 130 |
if s.Sign() != 0 { |
| 131 |
break |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
return encodeSignature(r.Bytes(), s.Bytes()) |
| 136 |
} |
| 137 |
|
| 138 |
// Verify verifies the signature in r, s of hash using the public key, pub. Its |
| 139 |
// return value records whether the signature is valid. Most applications should |
| 140 |
// use VerifyASN1 instead of dealing directly with r, s. |
| 141 |
// |
| 142 |
// The inputs are not considered confidential, and may leak through timing side |
| 143 |
// channels, or if an attacker has control of part of the inputs. |
| 144 |
func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool { |
| 145 |
if r.Sign() <= 0 || s.Sign() <= 0 { |
| 146 |
return false |
| 147 |
} |
| 148 |
sig, err := encodeSignature(r.Bytes(), s.Bytes()) |
| 149 |
if err != nil { |
| 150 |
return false |
| 151 |
} |
| 152 |
return VerifyASN1(pub, hash, sig) |
| 153 |
} |
| 154 |
|
| 155 |
func verifyLegacy(pub *PublicKey, hash []byte, sig []byte) bool { |
| 156 |
if fips140only.Enabled { |
| 157 |
panic("crypto/ecdsa: use of custom curves is not allowed in FIPS 140-only mode") |
| 158 |
} |
| 159 |
|
| 160 |
rBytes, sBytes, err := parseSignature(sig) |
| 161 |
if err != nil { |
| 162 |
return false |
| 163 |
} |
| 164 |
r, s := new(big.Int).SetBytes(rBytes), new(big.Int).SetBytes(sBytes) |
| 165 |
|
| 166 |
c := pub.Curve |
| 167 |
N := c.Params().N |
| 168 |
|
| 169 |
if r.Sign() <= 0 || s.Sign() <= 0 { |
| 170 |
return false |
| 171 |
} |
| 172 |
if r.Cmp(N) >= 0 || s.Cmp(N) >= 0 { |
| 173 |
return false |
| 174 |
} |
| 175 |
|
| 176 |
// SEC 1, Version 2.0, Section 4.1.4 |
| 177 |
e := hashToInt(hash, c) |
| 178 |
w := new(big.Int).ModInverse(s, N) |
| 179 |
|
| 180 |
u1 := e.Mul(e, w) |
| 181 |
u1.Mod(u1, N) |
| 182 |
u2 := w.Mul(r, w) |
| 183 |
u2.Mod(u2, N) |
| 184 |
|
| 185 |
x1, y1 := c.ScalarBaseMult(u1.Bytes()) |
| 186 |
x2, y2 := c.ScalarMult(pub.X, pub.Y, u2.Bytes()) |
| 187 |
x, y := c.Add(x1, y1, x2, y2) |
| 188 |
|
| 189 |
if x.Sign() == 0 && y.Sign() == 0 { |
| 190 |
return false |
| 191 |
} |
| 192 |
x.Mod(x, N) |
| 193 |
return x.Cmp(r) == 0 |
| 194 |
} |
| 195 |
|
| 196 |
var one = new(big.Int).SetInt64(1) |
| 197 |
|
| 198 |
// randFieldElement returns a random element of the order of the given |
| 199 |
// curve using the procedure given in FIPS 186-4, Appendix B.5.2. |
| 200 |
func randFieldElement(c elliptic.Curve, rand io.Reader) (k *big.Int, err error) { |
| 201 |
for { |
| 202 |
N := c.Params().N |
| 203 |
b := make([]byte, (N.BitLen()+7)/8) |
| 204 |
if _, err = io.ReadFull(rand, b); err != nil { |
| 205 |
return |
| 206 |
} |
| 207 |
if excess := len(b)*8 - N.BitLen(); excess > 0 { |
| 208 |
b[0] >>= excess |
| 209 |
} |
| 210 |
k = new(big.Int).SetBytes(b) |
| 211 |
if k.Sign() != 0 && k.Cmp(N) < 0 { |
| 212 |
return |
| 213 |
} |
| 214 |
} |
| 215 |
} |