2025-07-07 15:42:11 -04:00
|
|
|
package varsig
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/binary"
|
2025-07-08 11:27:18 -04:00
|
|
|
"fmt"
|
2025-07-07 15:42:11 -04:00
|
|
|
)
|
|
|
|
|
|
2025-07-22 11:39:48 +02:00
|
|
|
// DiscriminatorEdDSA is the value specifying an EdDSA signature.
|
|
|
|
|
const DiscriminatorEdDSA = Discriminator(0xed)
|
2025-07-07 15:42:11 -04:00
|
|
|
|
2025-07-10 11:24:14 +02:00
|
|
|
// EdDSACurve are values that specify which Edwards curve is used when
|
|
|
|
|
// generating the signature.
|
2025-07-07 15:42:11 -04:00
|
|
|
type EdDSACurve uint64
|
|
|
|
|
|
2025-07-10 11:24:14 +02:00
|
|
|
// Constants describing the values for each specific Edwards curve that can
|
|
|
|
|
// be encoded into a Varsig.
|
2025-07-07 15:42:11 -04:00
|
|
|
const (
|
2025-07-10 11:24:14 +02:00
|
|
|
CurveEd25519 = EdDSACurve(0xed)
|
|
|
|
|
CurveEd448 = EdDSACurve(0x1203)
|
2025-07-07 15:42:11 -04:00
|
|
|
)
|
|
|
|
|
|
2025-07-08 18:38:23 +02:00
|
|
|
func decodeEdDSACurve(r BytesReader) (EdDSACurve, error) {
|
2025-07-08 11:27:18 -04:00
|
|
|
u, err := binary.ReadUvarint(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch curve := EdDSACurve(u); curve {
|
|
|
|
|
case CurveEd25519, CurveEd448:
|
|
|
|
|
return curve, nil
|
|
|
|
|
default:
|
|
|
|
|
return 0, fmt.Errorf("%w: %x", ErrUnknownEdDSACurve, u)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-08 18:38:23 +02:00
|
|
|
var _ Varsig = EdDSAVarsig{}
|
2025-07-07 15:42:11 -04:00
|
|
|
|
2025-07-08 07:34:57 -04:00
|
|
|
// EdDSAVarsig is a varsig that encodes the parameters required to describe
|
|
|
|
|
// an EdDSA signature.
|
2025-07-07 15:42:11 -04:00
|
|
|
type EdDSAVarsig struct {
|
2025-07-08 18:38:23 +02:00
|
|
|
varsig
|
2025-07-07 15:42:11 -04:00
|
|
|
|
|
|
|
|
curve EdDSACurve
|
2025-07-22 11:27:24 +02:00
|
|
|
hashAlg Hash
|
2025-07-07 15:42:11 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-08 07:34:57 -04:00
|
|
|
// NewEdDSAVarsig creates and validates an EdDSA varsig with the provided
|
|
|
|
|
// curve, hash algorithm and payload encoding.
|
2025-07-28 18:40:45 +02:00
|
|
|
func NewEdDSAVarsig(curve EdDSACurve, hashAlgorithm Hash, payloadEncoding PayloadEncoding) EdDSAVarsig {
|
|
|
|
|
return EdDSAVarsig{
|
2025-07-08 18:38:23 +02:00
|
|
|
varsig: varsig{
|
2025-07-28 18:40:45 +02:00
|
|
|
disc: DiscriminatorEdDSA,
|
2025-07-08 07:34:57 -04:00
|
|
|
payEnc: payloadEncoding,
|
2025-07-07 15:42:11 -04:00
|
|
|
},
|
|
|
|
|
curve: curve,
|
|
|
|
|
hashAlg: hashAlgorithm,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-08 07:34:57 -04:00
|
|
|
// Curve returns the Edwards curve used to generate the EdDSA signature.
|
2025-07-08 18:38:23 +02:00
|
|
|
func (v EdDSAVarsig) Curve() EdDSACurve {
|
2025-07-07 15:42:11 -04:00
|
|
|
return v.curve
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-22 11:27:24 +02:00
|
|
|
// Hash returns the value describing the hash algorithm used to hash
|
2025-07-10 11:24:14 +02:00
|
|
|
// the payload content before the signature is generated.
|
2025-07-22 11:27:24 +02:00
|
|
|
func (v EdDSAVarsig) Hash() Hash {
|
2025-07-07 15:42:11 -04:00
|
|
|
return v.hashAlg
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-08 07:34:57 -04:00
|
|
|
// Encode returns the encoded byte format of the EdDSAVarsig.
|
2025-07-07 15:42:11 -04:00
|
|
|
func (v EdDSAVarsig) Encode() []byte {
|
|
|
|
|
buf := v.encode()
|
|
|
|
|
|
2025-07-28 18:40:45 +02:00
|
|
|
buf = binary.AppendUvarint(buf, uint64(v.curve))
|
2025-07-07 15:42:11 -04:00
|
|
|
buf = binary.AppendUvarint(buf, uint64(v.hashAlg))
|
2025-07-24 13:53:03 +02:00
|
|
|
buf = append(buf, EncodePayloadEncoding(v.payEnc)...)
|
2025-07-07 15:42:11 -04:00
|
|
|
|
|
|
|
|
return buf
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 18:40:45 +02:00
|
|
|
func decodeEdDSA(r BytesReader) (Varsig, error) {
|
|
|
|
|
curve, err := decodeEdDSACurve(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2025-07-07 15:42:11 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-08 11:27:18 -04:00
|
|
|
hashAlg, err := DecodeHashAlgorithm(r)
|
2025-07-07 15:42:11 -04:00
|
|
|
if err != nil {
|
2025-07-08 11:27:18 -04:00
|
|
|
return nil, err
|
2025-07-07 15:42:11 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-28 18:40:45 +02:00
|
|
|
payEnc, err := DecodePayloadEncoding(r)
|
2025-07-08 18:38:23 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 18:40:45 +02:00
|
|
|
return NewEdDSAVarsig(curve, hashAlg, payEnc), nil
|
2025-07-07 15:42:11 -04:00
|
|
|
}
|