| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| package ecdsa |
|
|
| import ( |
| "crypto" |
| "crypto/ecdh" |
| "crypto/elliptic" |
| "crypto/internal/boring" |
| "crypto/internal/boring/bbig" |
| "crypto/internal/fips140/ecdsa" |
| "crypto/internal/fips140/nistec" |
| "crypto/internal/fips140cache" |
| "crypto/internal/fips140hash" |
| "crypto/internal/fips140only" |
| "crypto/internal/rand" |
| "crypto/sha512" |
| "crypto/subtle" |
| "errors" |
| "io" |
| "math/big" |
|
|
| "golang.org/x/crypto/cryptobyte" |
| "golang.org/x/crypto/cryptobyte/asn1" |
| ) |
|
|
| |
| type PublicKey struct { |
| elliptic.Curve |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| X, Y *big.Int |
| } |
|
|
| |
| |
|
|
| |
| |
| |
| func (pub *PublicKey) ECDH() (*ecdh.PublicKey, error) { |
| c := curveToECDH(pub.Curve) |
| if c == nil { |
| return nil, errors.New("ecdsa: unsupported curve by crypto/ecdh") |
| } |
| k, err := pub.Bytes() |
| if err != nil { |
| return nil, err |
| } |
| return c.NewPublicKey(k) |
| } |
|
|
| |
| |
| |
| |
| |
| func (pub *PublicKey) Equal(x crypto.PublicKey) bool { |
| xx, ok := x.(*PublicKey) |
| if !ok { |
| return false |
| } |
| return bigIntEqual(pub.X, xx.X) && bigIntEqual(pub.Y, xx.Y) && |
| |
| |
| |
| |
| pub.Curve == xx.Curve |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func ParseUncompressedPublicKey(curve elliptic.Curve, data []byte) (*PublicKey, error) { |
| if len(data) < 1 || data[0] != 4 { |
| return nil, errors.New("ecdsa: invalid uncompressed public key") |
| } |
| switch curve { |
| case elliptic.P224(): |
| return parseUncompressedPublicKey(ecdsa.P224(), curve, data) |
| case elliptic.P256(): |
| return parseUncompressedPublicKey(ecdsa.P256(), curve, data) |
| case elliptic.P384(): |
| return parseUncompressedPublicKey(ecdsa.P384(), curve, data) |
| case elliptic.P521(): |
| return parseUncompressedPublicKey(ecdsa.P521(), curve, data) |
| default: |
| return nil, errors.New("ecdsa: curve not supported by ParseUncompressedPublicKey") |
| } |
| } |
|
|
| func parseUncompressedPublicKey[P ecdsa.Point[P]](c *ecdsa.Curve[P], curve elliptic.Curve, data []byte) (*PublicKey, error) { |
| k, err := ecdsa.NewPublicKey(c, data) |
| if err != nil { |
| return nil, err |
| } |
| return publicKeyFromFIPS(curve, k) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func (pub *PublicKey) Bytes() ([]byte, error) { |
| switch pub.Curve { |
| case elliptic.P224(): |
| return publicKeyBytes(ecdsa.P224(), pub) |
| case elliptic.P256(): |
| return publicKeyBytes(ecdsa.P256(), pub) |
| case elliptic.P384(): |
| return publicKeyBytes(ecdsa.P384(), pub) |
| case elliptic.P521(): |
| return publicKeyBytes(ecdsa.P521(), pub) |
| default: |
| return nil, errors.New("ecdsa: curve not supported by PublicKey.Bytes") |
| } |
| } |
|
|
| func publicKeyBytes[P ecdsa.Point[P]](c *ecdsa.Curve[P], pub *PublicKey) ([]byte, error) { |
| k, err := publicKeyToFIPS(c, pub) |
| if err != nil { |
| return nil, err |
| } |
| return k.Bytes(), nil |
| } |
|
|
| |
| type PrivateKey struct { |
| PublicKey |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| D *big.Int |
| } |
|
|
| |
| |
| |
| func (priv *PrivateKey) ECDH() (*ecdh.PrivateKey, error) { |
| c := curveToECDH(priv.Curve) |
| if c == nil { |
| return nil, errors.New("ecdsa: unsupported curve by crypto/ecdh") |
| } |
| k, err := priv.Bytes() |
| if err != nil { |
| return nil, err |
| } |
| return c.NewPrivateKey(k) |
| } |
|
|
| func curveToECDH(c elliptic.Curve) ecdh.Curve { |
| switch c { |
| case elliptic.P256(): |
| return ecdh.P256() |
| case elliptic.P384(): |
| return ecdh.P384() |
| case elliptic.P521(): |
| return ecdh.P521() |
| default: |
| return nil |
| } |
| } |
|
|
| |
| func (priv *PrivateKey) Public() crypto.PublicKey { |
| return &priv.PublicKey |
| } |
|
|
| |
| |
| |
| func (priv *PrivateKey) Equal(x crypto.PrivateKey) bool { |
| xx, ok := x.(*PrivateKey) |
| if !ok { |
| return false |
| } |
| return priv.PublicKey.Equal(&xx.PublicKey) && bigIntEqual(priv.D, xx.D) |
| } |
|
|
| |
| |
| func bigIntEqual(a, b *big.Int) bool { |
| return subtle.ConstantTimeCompare(a.Bytes(), b.Bytes()) == 1 |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func ParseRawPrivateKey(curve elliptic.Curve, data []byte) (*PrivateKey, error) { |
| switch curve { |
| case elliptic.P224(): |
| return parseRawPrivateKey(ecdsa.P224(), nistec.NewP224Point, curve, data) |
| case elliptic.P256(): |
| return parseRawPrivateKey(ecdsa.P256(), nistec.NewP256Point, curve, data) |
| case elliptic.P384(): |
| return parseRawPrivateKey(ecdsa.P384(), nistec.NewP384Point, curve, data) |
| case elliptic.P521(): |
| return parseRawPrivateKey(ecdsa.P521(), nistec.NewP521Point, curve, data) |
| default: |
| return nil, errors.New("ecdsa: curve not supported by ParseRawPrivateKey") |
| } |
| } |
|
|
| func parseRawPrivateKey[P ecdsa.Point[P]](c *ecdsa.Curve[P], newPoint func() P, curve elliptic.Curve, data []byte) (*PrivateKey, error) { |
| q, err := newPoint().ScalarBaseMult(data) |
| if err != nil { |
| return nil, err |
| } |
| k, err := ecdsa.NewPrivateKey(c, data, q.Bytes()) |
| if err != nil { |
| return nil, err |
| } |
| return privateKeyFromFIPS(curve, k) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func (priv *PrivateKey) Bytes() ([]byte, error) { |
| switch priv.Curve { |
| case elliptic.P224(): |
| return privateKeyBytes(ecdsa.P224(), priv) |
| case elliptic.P256(): |
| return privateKeyBytes(ecdsa.P256(), priv) |
| case elliptic.P384(): |
| return privateKeyBytes(ecdsa.P384(), priv) |
| case elliptic.P521(): |
| return privateKeyBytes(ecdsa.P521(), priv) |
| default: |
| return nil, errors.New("ecdsa: curve not supported by PrivateKey.Bytes") |
| } |
| } |
|
|
| func privateKeyBytes[P ecdsa.Point[P]](c *ecdsa.Curve[P], priv *PrivateKey) ([]byte, error) { |
| k, err := privateKeyToFIPS(c, priv) |
| if err != nil { |
| return nil, err |
| } |
| return k.Bytes(), nil |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func (priv *PrivateKey) Sign(random io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) { |
| if random == nil { |
| return signRFC6979(priv, digest, opts) |
| } |
| random = rand.CustomReader(random) |
| return SignASN1(random, priv, digest) |
| } |
|
|
| |
| |
| |
| |
| |
| func GenerateKey(c elliptic.Curve, r io.Reader) (*PrivateKey, error) { |
| if boring.Enabled && rand.IsDefaultReader(r) { |
| x, y, d, err := boring.GenerateKeyECDSA(c.Params().Name) |
| if err != nil { |
| return nil, err |
| } |
| return &PrivateKey{PublicKey: PublicKey{Curve: c, X: bbig.Dec(x), Y: bbig.Dec(y)}, D: bbig.Dec(d)}, nil |
| } |
| boring.UnreachableExceptTests() |
|
|
| r = rand.CustomReader(r) |
|
|
| switch c.Params() { |
| case elliptic.P224().Params(): |
| return generateFIPS(c, ecdsa.P224(), r) |
| case elliptic.P256().Params(): |
| return generateFIPS(c, ecdsa.P256(), r) |
| case elliptic.P384().Params(): |
| return generateFIPS(c, ecdsa.P384(), r) |
| case elliptic.P521().Params(): |
| return generateFIPS(c, ecdsa.P521(), r) |
| default: |
| return generateLegacy(c, r) |
| } |
| } |
|
|
| func generateFIPS[P ecdsa.Point[P]](curve elliptic.Curve, c *ecdsa.Curve[P], rand io.Reader) (*PrivateKey, error) { |
| if fips140only.Enforced() && !fips140only.ApprovedRandomReader(rand) { |
| return nil, errors.New("crypto/ecdsa: only crypto/rand.Reader is allowed in FIPS 140-only mode") |
| } |
| privateKey, err := ecdsa.GenerateKey(c, rand) |
| if err != nil { |
| return nil, err |
| } |
| return privateKeyFromFIPS(curve, privateKey) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func SignASN1(r io.Reader, priv *PrivateKey, hash []byte) ([]byte, error) { |
| if boring.Enabled && rand.IsDefaultReader(r) { |
| b, err := boringPrivateKey(priv) |
| if err != nil { |
| return nil, err |
| } |
| return boring.SignMarshalECDSA(b, hash) |
| } |
| boring.UnreachableExceptTests() |
|
|
| r = rand.CustomReader(r) |
|
|
| switch priv.Curve.Params() { |
| case elliptic.P224().Params(): |
| return signFIPS(ecdsa.P224(), priv, r, hash) |
| case elliptic.P256().Params(): |
| return signFIPS(ecdsa.P256(), priv, r, hash) |
| case elliptic.P384().Params(): |
| return signFIPS(ecdsa.P384(), priv, r, hash) |
| case elliptic.P521().Params(): |
| return signFIPS(ecdsa.P521(), priv, r, hash) |
| default: |
| return signLegacy(priv, r, hash) |
| } |
| } |
|
|
| func signFIPS[P ecdsa.Point[P]](c *ecdsa.Curve[P], priv *PrivateKey, rand io.Reader, hash []byte) ([]byte, error) { |
| if fips140only.Enforced() && !fips140only.ApprovedRandomReader(rand) { |
| return nil, errors.New("crypto/ecdsa: only crypto/rand.Reader is allowed in FIPS 140-only mode") |
| } |
| k, err := privateKeyToFIPS(c, priv) |
| if err != nil { |
| return nil, err |
| } |
| |
| |
| |
| sig, err := ecdsa.Sign(c, sha512.New, k, rand, hash) |
| if err != nil { |
| return nil, err |
| } |
| return encodeSignature(sig.R, sig.S) |
| } |
|
|
| func signRFC6979(priv *PrivateKey, hash []byte, opts crypto.SignerOpts) ([]byte, error) { |
| if opts == nil { |
| return nil, errors.New("ecdsa: Sign called with nil opts") |
| } |
| h := opts.HashFunc() |
| if h.Size() != len(hash) { |
| return nil, errors.New("ecdsa: hash length does not match hash function") |
| } |
| switch priv.Curve.Params() { |
| case elliptic.P224().Params(): |
| return signFIPSDeterministic(ecdsa.P224(), h, priv, hash) |
| case elliptic.P256().Params(): |
| return signFIPSDeterministic(ecdsa.P256(), h, priv, hash) |
| case elliptic.P384().Params(): |
| return signFIPSDeterministic(ecdsa.P384(), h, priv, hash) |
| case elliptic.P521().Params(): |
| return signFIPSDeterministic(ecdsa.P521(), h, priv, hash) |
| default: |
| return nil, errors.New("ecdsa: curve not supported by deterministic signatures") |
| } |
| } |
|
|
| func signFIPSDeterministic[P ecdsa.Point[P]](c *ecdsa.Curve[P], hashFunc crypto.Hash, priv *PrivateKey, hash []byte) ([]byte, error) { |
| k, err := privateKeyToFIPS(c, priv) |
| if err != nil { |
| return nil, err |
| } |
| h := fips140hash.UnwrapNew(hashFunc.New) |
| if fips140only.Enforced() && !fips140only.ApprovedHash(h()) { |
| return nil, errors.New("crypto/ecdsa: use of hash functions other than SHA-2 or SHA-3 is not allowed in FIPS 140-only mode") |
| } |
| sig, err := ecdsa.SignDeterministic(c, h, k, hash) |
| if err != nil { |
| return nil, err |
| } |
| return encodeSignature(sig.R, sig.S) |
| } |
|
|
| func encodeSignature(r, s []byte) ([]byte, error) { |
| var b cryptobyte.Builder |
| b.AddASN1(asn1.SEQUENCE, func(b *cryptobyte.Builder) { |
| addASN1IntBytes(b, r) |
| addASN1IntBytes(b, s) |
| }) |
| return b.Bytes() |
| } |
|
|
| |
| |
| func addASN1IntBytes(b *cryptobyte.Builder, bytes []byte) { |
| for len(bytes) > 0 && bytes[0] == 0 { |
| bytes = bytes[1:] |
| } |
| if len(bytes) == 0 { |
| b.SetError(errors.New("invalid integer")) |
| return |
| } |
| b.AddASN1(asn1.INTEGER, func(c *cryptobyte.Builder) { |
| if bytes[0]&0x80 != 0 { |
| c.AddUint8(0) |
| } |
| c.AddBytes(bytes) |
| }) |
| } |
|
|
| |
| |
| |
| |
| |
| func VerifyASN1(pub *PublicKey, hash, sig []byte) bool { |
| if boring.Enabled { |
| key, err := boringPublicKey(pub) |
| if err != nil { |
| return false |
| } |
| return boring.VerifyECDSA(key, hash, sig) |
| } |
| boring.UnreachableExceptTests() |
|
|
| switch pub.Curve.Params() { |
| case elliptic.P224().Params(): |
| return verifyFIPS(ecdsa.P224(), pub, hash, sig) |
| case elliptic.P256().Params(): |
| return verifyFIPS(ecdsa.P256(), pub, hash, sig) |
| case elliptic.P384().Params(): |
| return verifyFIPS(ecdsa.P384(), pub, hash, sig) |
| case elliptic.P521().Params(): |
| return verifyFIPS(ecdsa.P521(), pub, hash, sig) |
| default: |
| return verifyLegacy(pub, hash, sig) |
| } |
| } |
|
|
| func verifyFIPS[P ecdsa.Point[P]](c *ecdsa.Curve[P], pub *PublicKey, hash, sig []byte) bool { |
| r, s, err := parseSignature(sig) |
| if err != nil { |
| return false |
| } |
| k, err := publicKeyToFIPS(c, pub) |
| if err != nil { |
| return false |
| } |
| if err := ecdsa.Verify(c, k, hash, &ecdsa.Signature{R: r, S: s}); err != nil { |
| return false |
| } |
| return true |
| } |
|
|
| func parseSignature(sig []byte) (r, s []byte, err error) { |
| var inner cryptobyte.String |
| input := cryptobyte.String(sig) |
| if !input.ReadASN1(&inner, asn1.SEQUENCE) || |
| !input.Empty() || |
| !inner.ReadASN1Integer(&r) || |
| !inner.ReadASN1Integer(&s) || |
| !inner.Empty() { |
| return nil, nil, errors.New("invalid ASN.1") |
| } |
| return r, s, nil |
| } |
|
|
| func publicKeyFromFIPS(curve elliptic.Curve, pub *ecdsa.PublicKey) (*PublicKey, error) { |
| x, y, err := pointToAffine(curve, pub.Bytes()) |
| if err != nil { |
| return nil, err |
| } |
| return &PublicKey{Curve: curve, X: x, Y: y}, nil |
| } |
|
|
| func privateKeyFromFIPS(curve elliptic.Curve, priv *ecdsa.PrivateKey) (*PrivateKey, error) { |
| pub, err := publicKeyFromFIPS(curve, priv.PublicKey()) |
| if err != nil { |
| return nil, err |
| } |
| return &PrivateKey{PublicKey: *pub, D: new(big.Int).SetBytes(priv.Bytes())}, nil |
| } |
|
|
| func publicKeyToFIPS[P ecdsa.Point[P]](c *ecdsa.Curve[P], pub *PublicKey) (*ecdsa.PublicKey, error) { |
| Q, err := pointFromAffine(pub.Curve, pub.X, pub.Y) |
| if err != nil { |
| return nil, err |
| } |
| return ecdsa.NewPublicKey(c, Q) |
| } |
|
|
| var privateKeyCache fips140cache.Cache[PrivateKey, ecdsa.PrivateKey] |
|
|
| func privateKeyToFIPS[P ecdsa.Point[P]](c *ecdsa.Curve[P], priv *PrivateKey) (*ecdsa.PrivateKey, error) { |
| Q, err := pointFromAffine(priv.Curve, priv.X, priv.Y) |
| if err != nil { |
| return nil, err |
| } |
|
|
| |
| if priv.D.BitLen() > priv.Curve.Params().N.BitLen() { |
| return nil, errors.New("ecdsa: private key scalar too large") |
| } |
| if priv.D.Sign() <= 0 { |
| return nil, errors.New("ecdsa: private key scalar is zero or negative") |
| } |
|
|
| size := (priv.Curve.Params().N.BitLen() + 7) / 8 |
| const maxScalarSize = 66 |
| if size > maxScalarSize { |
| return nil, errors.New("ecdsa: internal error: curve size too large") |
| } |
| D := priv.D.FillBytes(make([]byte, size, maxScalarSize)) |
|
|
| return privateKeyCache.Get(priv, func() (*ecdsa.PrivateKey, error) { |
| return ecdsa.NewPrivateKey(c, D, Q) |
| }, func(k *ecdsa.PrivateKey) bool { |
| return subtle.ConstantTimeCompare(k.PublicKey().Bytes(), Q) == 1 && |
| subtle.ConstantTimeCompare(k.Bytes(), D) == 1 |
| }) |
| } |
|
|
| |
| func pointFromAffine(curve elliptic.Curve, x, y *big.Int) ([]byte, error) { |
| bitSize := curve.Params().BitSize |
| |
| if x.Sign() < 0 || y.Sign() < 0 { |
| return nil, errors.New("negative coordinate") |
| } |
| if x.BitLen() > bitSize || y.BitLen() > bitSize { |
| return nil, errors.New("overflowing coordinate") |
| } |
| |
| byteLen := (bitSize + 7) / 8 |
| buf := make([]byte, 1+2*byteLen) |
| buf[0] = 4 |
| x.FillBytes(buf[1 : 1+byteLen]) |
| y.FillBytes(buf[1+byteLen : 1+2*byteLen]) |
| return buf, nil |
| } |
|
|
| |
| func pointToAffine(curve elliptic.Curve, p []byte) (x, y *big.Int, err error) { |
| if len(p) == 1 && p[0] == 0 { |
| |
| return nil, nil, errors.New("ecdsa: public key point is the infinity") |
| } |
| byteLen := (curve.Params().BitSize + 7) / 8 |
| x = new(big.Int).SetBytes(p[1 : 1+byteLen]) |
| y = new(big.Int).SetBytes(p[1+byteLen:]) |
| return x, y, nil |
| } |
|
|