ProtonMail / go-crypto

Fork of go/x/crypto, providing an up-to-date OpenPGP implementation
https://pkg.go.dev/github.com/ProtonMail/go-crypto
BSD 3-Clause "New" or "Revised" License
334 stars 101 forks source link

Add interfaces for ECDH, ECDSA, and EdDSA #111

Closed wussler closed 2 years ago

wussler commented 2 years ago

Add the following generic interfaces, to allow adding X448 and Ed448.

type Curve interface {
    GetCurveType() CurveType
    GetCurveName() string
}

type ECDSACurve interface {
    Curve
    MarshalPoint(x, y *big.Int) []byte
    UnmarshalPoint([]byte) (x, y *big.Int)
    MarshalIntegerSecret(d *big.Int) []byte
    UnmarshalIntegerSecret(d []byte) *big.Int
    GenerateECDSA(rand io.Reader) (x, y, secret *big.Int, err error)
    Sign(rand io.Reader, x, y, d *big.Int, hash []byte) (r, s *big.Int, err error)
    Verify(x, y *big.Int, hash []byte, r, s *big.Int) bool
    Validate(x, y *big.Int, secret []byte) error
}

type EdDSACurve interface {
    Curve
    MarshalPoint(x []byte) []byte
    UnmarshalPoint([]byte) (x []byte)
    MarshalByteSecret(d []byte) []byte
    UnmarshalByteSecret(d []byte) []byte
    GenerateEdDSA(rand io.Reader) (pub, priv []byte, err error)
    Sign(publicKey, privateKey, message []byte) (r, s []byte, err error)
    Verify(publicKey, message, r, s []byte) bool
    Validate(publicKey, privateKey []byte) (err error)
}

type ECDHCurve interface {
    Curve
    MarshalPoint(x, y *big.Int) []byte
    UnmarshalPoint([]byte) (x, y *big.Int)
    MarshalByteSecret(d []byte) []byte
    UnmarshalByteSecret(d []byte) []byte
    GetBuildKeyAttempts() int
    GenerateECDH(rand io.Reader) (x, y *big.Int, secret []byte, err error)
    Encaps(rand io.Reader, x, y *big.Int) (ephemeral, sharedSecret []byte, err error)
    Decaps(ephemeral, secret []byte) (sharedSecret []byte, err error)
    Validate(x, y *big.Int, secret []byte) error
}