| |
| |
| |
|
|
| package ecdh |
|
|
| import ( |
| "bytes" |
| "crypto/internal/fips140" |
| "crypto/internal/fips140/drbg" |
| "crypto/internal/fips140/nistec" |
| "crypto/internal/fips140deps/byteorder" |
| "errors" |
| "io" |
| "math/bits" |
| ) |
|
|
| |
| |
| |
|
|
| |
| |
|
|
| type PrivateKey struct { |
| pub PublicKey |
| d []byte |
| } |
|
|
| func (priv *PrivateKey) Bytes() []byte { |
| return priv.d |
| } |
|
|
| func (priv *PrivateKey) PublicKey() *PublicKey { |
| return &priv.pub |
| } |
|
|
| type PublicKey struct { |
| curve curveID |
| q []byte |
| } |
|
|
| func (pub *PublicKey) Bytes() []byte { |
| return pub.q |
| } |
|
|
| type curveID string |
|
|
| const ( |
| p224 curveID = "P-224" |
| p256 curveID = "P-256" |
| p384 curveID = "P-384" |
| p521 curveID = "P-521" |
| ) |
|
|
| type Curve[P Point[P]] struct { |
| curve curveID |
| newPoint func() P |
| N []byte |
| } |
|
|
| |
| type Point[P any] interface { |
| *nistec.P224Point | *nistec.P256Point | *nistec.P384Point | *nistec.P521Point |
| Bytes() []byte |
| BytesX() ([]byte, error) |
| SetBytes([]byte) (P, error) |
| ScalarMult(P, []byte) (P, error) |
| ScalarBaseMult([]byte) (P, error) |
| } |
|
|
| func P224() *Curve[*nistec.P224Point] { |
| return &Curve[*nistec.P224Point]{ |
| curve: p224, |
| newPoint: nistec.NewP224Point, |
| N: p224Order, |
| } |
| } |
|
|
| var p224Order = []byte{ |
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x16, 0xa2, |
| 0xe0, 0xb8, 0xf0, 0x3e, 0x13, 0xdd, 0x29, 0x45, |
| 0x5c, 0x5c, 0x2a, 0x3d, |
| } |
|
|
| func P256() *Curve[*nistec.P256Point] { |
| return &Curve[*nistec.P256Point]{ |
| curve: p256, |
| newPoint: nistec.NewP256Point, |
| N: p256Order, |
| } |
| } |
|
|
| var p256Order = []byte{ |
| 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, |
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 0xbc, 0xe6, 0xfa, 0xad, 0xa7, 0x17, 0x9e, 0x84, |
| 0xf3, 0xb9, 0xca, 0xc2, 0xfc, 0x63, 0x25, 0x51, |
| } |
|
|
| func P384() *Curve[*nistec.P384Point] { |
| return &Curve[*nistec.P384Point]{ |
| curve: p384, |
| newPoint: nistec.NewP384Point, |
| N: p384Order, |
| } |
| } |
|
|
| var p384Order = []byte{ |
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 0xc7, 0x63, 0x4d, 0x81, 0xf4, 0x37, 0x2d, 0xdf, |
| 0x58, 0x1a, 0x0d, 0xb2, 0x48, 0xb0, 0xa7, 0x7a, |
| 0xec, 0xec, 0x19, 0x6a, 0xcc, 0xc5, 0x29, 0x73, |
| } |
|
|
| func P521() *Curve[*nistec.P521Point] { |
| return &Curve[*nistec.P521Point]{ |
| curve: p521, |
| newPoint: nistec.NewP521Point, |
| N: p521Order, |
| } |
| } |
|
|
| var p521Order = []byte{0x01, 0xff, |
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfa, |
| 0x51, 0x86, 0x87, 0x83, 0xbf, 0x2f, 0x96, 0x6b, |
| 0x7f, 0xcc, 0x01, 0x48, 0xf7, 0x09, 0xa5, 0xd0, |
| 0x3b, 0xb5, 0xc9, 0xb8, 0x89, 0x9c, 0x47, 0xae, |
| 0xbb, 0x6f, 0xb7, 0x1e, 0x91, 0x38, 0x64, 0x09, |
| } |
|
|
| |
| func GenerateKey[P Point[P]](c *Curve[P], rand io.Reader) (*PrivateKey, error) { |
| fips140.RecordApproved() |
| |
| |
|
|
| for { |
| key := make([]byte, len(c.N)) |
| if err := drbg.ReadWithReader(rand, key); err != nil { |
| return nil, err |
| } |
| |
| |
| |
| key[1] ^= 0x42 |
|
|
| |
| |
| if c.curve == p521 && c.N[0]&0b1111_1110 == 0 { |
| key[0] &= 0b0000_0001 |
| } |
|
|
| privateKey, err := NewPrivateKey(c, key) |
| if err != nil { |
| continue |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| fips140.PCT("ECDH PCT", func() error { |
| p1, err := c.newPoint().ScalarBaseMult(privateKey.d) |
| if err != nil { |
| return err |
| } |
| if !bytes.Equal(p1.Bytes(), privateKey.pub.q) { |
| return errors.New("crypto/ecdh: public key does not match private key") |
| } |
| return nil |
| }) |
|
|
| return privateKey, nil |
| } |
| } |
|
|
| func NewPrivateKey[P Point[P]](c *Curve[P], key []byte) (*PrivateKey, error) { |
| |
| |
| |
| if len(key) != len(c.N) || isZero(key) || !isLess(key, c.N) { |
| return nil, errors.New("crypto/ecdh: invalid private key") |
| } |
|
|
| p, err := c.newPoint().ScalarBaseMult(key) |
| if err != nil { |
| |
| |
| panic("crypto/ecdh: internal error: nistec ScalarBaseMult failed for a fixed-size input") |
| } |
|
|
| publicKey := p.Bytes() |
| if len(publicKey) == 1 { |
| |
| |
| |
| panic("crypto/ecdh: internal error: public key is the identity element") |
| } |
|
|
| k := &PrivateKey{d: bytes.Clone(key), pub: PublicKey{curve: c.curve, q: publicKey}} |
| return k, nil |
| } |
|
|
| func NewPublicKey[P Point[P]](c *Curve[P], key []byte) (*PublicKey, error) { |
| |
| if len(key) == 0 || key[0] != 4 { |
| return nil, errors.New("crypto/ecdh: invalid public key") |
| } |
|
|
| |
| |
| |
| |
| if _, err := c.newPoint().SetBytes(key); err != nil { |
| return nil, err |
| } |
|
|
| return &PublicKey{curve: c.curve, q: bytes.Clone(key)}, nil |
| } |
|
|
| func ECDH[P Point[P]](c *Curve[P], k *PrivateKey, peer *PublicKey) ([]byte, error) { |
| fipsSelfTest() |
| fips140.RecordApproved() |
| return ecdh(c, k, peer) |
| } |
|
|
| func ecdh[P Point[P]](c *Curve[P], k *PrivateKey, peer *PublicKey) ([]byte, error) { |
| if c.curve != k.pub.curve { |
| return nil, errors.New("crypto/ecdh: mismatched curves") |
| } |
| if k.pub.curve != peer.curve { |
| return nil, errors.New("crypto/ecdh: mismatched curves") |
| } |
|
|
| |
| |
|
|
| |
| if len(k.pub.q) == 1 { |
| return nil, errors.New("crypto/ecdh: public key is the identity element") |
| } |
|
|
| |
| |
| p, err := c.newPoint().SetBytes(peer.q) |
| if err != nil { |
| return nil, err |
| } |
|
|
| |
| if _, err := p.ScalarMult(p, k.d); err != nil { |
| return nil, err |
| } |
|
|
| |
| |
| return p.BytesX() |
| } |
|
|
| |
| func isZero(x []byte) bool { |
| var acc byte |
| for _, b := range x { |
| acc |= b |
| } |
| return acc == 0 |
| } |
|
|
| |
| |
| func isLess(a, b []byte) bool { |
| if len(a) != len(b) { |
| panic("crypto/ecdh: internal error: mismatched isLess inputs") |
| } |
|
|
| |
| |
| |
| if len(a) > 72 { |
| panic("crypto/ecdh: internal error: isLess input too large") |
| } |
| bufA, bufB := make([]byte, 72), make([]byte, 72) |
| for i := range a { |
| bufA[i], bufB[i] = a[len(a)-i-1], b[len(b)-i-1] |
| } |
|
|
| |
| var borrow uint64 |
| for i := 0; i < len(bufA); i += 8 { |
| limbA, limbB := byteorder.LEUint64(bufA[i:]), byteorder.LEUint64(bufB[i:]) |
| _, borrow = bits.Sub64(limbA, limbB, borrow) |
| } |
|
|
| |
| return borrow == 1 |
| } |
|
|