fix(varsig): make generic and name decode everywhere

This commit is contained in:
Steve Moyer
2025-07-07 08:55:43 -04:00
parent 9c83def9e0
commit f9f39d363f
3 changed files with 27 additions and 27 deletions

View File

@@ -13,23 +13,23 @@ const (
Version1 Version = 1
)
// ParseFunc is a function that parses the varsig representing a specific
// DecodeFunc is a function that parses the varsig representing a specific
// signing algorithm.
type ParseFunc func(*bytes.Reader, Version, SignAlgorithm) (Varsig, error)
type DecodeFunc func(*bytes.Reader, Version, SignAlgorithm) (Varsig, error)
// Registry contains a mapping between known signing algorithms, and
// functions that can parse varsigs for that signing algorithm.
type Registry map[SignAlgorithm]ParseFunc
type Registry map[SignAlgorithm]DecodeFunc
// DefaultRegistry provides a Registry containing the mappings for the
// signing algorithms which have an implementation within this library.
func DefaultRegistry() Registry {
return map[SignAlgorithm]ParseFunc{
SignAlgorithmRSA: parseRSA,
SignAlgorithmEd25519: notYetImplementedVarsigParser,
SignAlgorithmECDSAP256: notYetImplementedVarsigParser,
SignAlgorithmECDSASecp256k1: notYetImplementedVarsigParser,
SignAlgorithmECDSAP521: notYetImplementedVarsigParser,
return map[SignAlgorithm]DecodeFunc{
SignAlgorithmRSA: decodeRSA,
SignAlgorithmEd25519: notYetImplementedVarsigDecoder,
SignAlgorithmECDSAP256: notYetImplementedVarsigDecoder,
SignAlgorithmECDSASecp256k1: notYetImplementedVarsigDecoder,
SignAlgorithmECDSAP521: notYetImplementedVarsigDecoder,
}
}
@@ -40,8 +40,8 @@ func NewRegistry() Registry {
// Register allows new mappings between a signing algorithm and its parsing
// function to the Registry.
func (rs Registry) Register(alg SignAlgorithm, parseFn ParseFunc) {
rs[alg] = parseFn
func (rs Registry) Register(alg SignAlgorithm, decodeFunc DecodeFunc) {
rs[alg] = decodeFunc
}
// Decode converts the provided data into one of the registered Varsig
@@ -62,20 +62,20 @@ func (rs Registry) DecodeStream(r *bytes.Reader) (Varsig, error) {
return nil, fmt.Errorf("%w: expected %d, got %d", ErrBadPrefix, Prefix, pre)
}
vers, signAlg, err := rs.parseVersAndSignAlg(r)
vers, signAlg, err := rs.decodeVersAndSignAlg(r)
if err != nil {
return nil, err
}
parseFn, ok := rs[SignAlgorithm(signAlg)]
decodeFunc, ok := rs[SignAlgorithm(signAlg)]
if !ok {
return nil, fmt.Errorf("%w: %x", ErrUnknownSignAlgorithm, signAlg)
}
return parseFn(r, vers, signAlg)
return decodeFunc(r, vers, signAlg)
}
func (rs Registry) parseVersAndSignAlg(r *bytes.Reader) (Version, SignAlgorithm, error) {
func (rs Registry) decodeVersAndSignAlg(r *bytes.Reader) (Version, SignAlgorithm, error) {
vers, err := binary.ReadUvarint(r)
if err != nil {
return Version(vers), 0, err
@@ -94,6 +94,6 @@ func (rs Registry) parseVersAndSignAlg(r *bytes.Reader) (Version, SignAlgorithm,
return Version(vers), SignAlgorithm(signAlg), err
}
func notYetImplementedVarsigParser(_ *bytes.Reader, vers Version, signAlg SignAlgorithm) (Varsig, error) {
func notYetImplementedVarsigDecoder(_ *bytes.Reader, vers Version, signAlg SignAlgorithm) (Varsig, error) {
return nil, fmt.Errorf("%w: Version: %d, SignAlgorithm: %x", ErrNotYetImplemented, vers, signAlg)
}

View File

@@ -52,13 +52,13 @@ func testRegistry(t *testing.T) varsig.Registry {
t.Helper()
reg := varsig.NewRegistry()
reg.Register(testSignAlgorithm0, testParseFunc(t))
reg.Register(testSignAlgorithm1, testParseFunc(t))
reg.Register(testSignAlgorithm0, testDecodeFunc(t))
reg.Register(testSignAlgorithm1, testDecodeFunc(t))
return reg
}
func testParseFunc(t *testing.T) varsig.ParseFunc {
func testDecodeFunc(t *testing.T) varsig.DecodeFunc {
t.Helper()
return func(r *bytes.Reader, vers varsig.Version, signAlg varsig.SignAlgorithm) (varsig.Varsig, error) {

View File

@@ -55,7 +55,7 @@ func DecodeStream(r *bytes.Reader) (Varsig, error) {
return DefaultRegistry().DecodeStream(r)
}
type varsig struct {
type varsig[T Varsig] struct {
vers Version
signAlg SignAlgorithm
payEnc PayloadEncoding
@@ -63,30 +63,30 @@ type varsig struct {
}
// Version returns the varsig's version field.
func (v *varsig) Version() Version {
func (v varsig[_]) Version() Version {
return v.vers
}
// SignatureAlgorithm returns the algorithm used to produce corresponding
// signature.
func (v *varsig) SignatureAlgorithm() SignAlgorithm {
func (v varsig[_]) SignatureAlgorithm() SignAlgorithm {
return v.signAlg
}
// PayloadEncoding returns the codec that was used to encode the signed
// data.
func (v *varsig) PayloadEncoding() PayloadEncoding {
func (v varsig[_]) PayloadEncoding() PayloadEncoding {
return v.payEnc
}
// Signature returns the cryptographic signature of the signed data. This
// value is never present in a varsig >= v1 and must either be a valid
// signature with the correct length or empty in varsig < v1.
func (v *varsig) Signature() []byte {
func (v varsig[_]) Signature() []byte {
return v.sig
}
func (v *varsig) encode() []byte {
func (v *varsig[_]) encode() []byte {
var buf []byte
buf = binary.AppendUvarint(buf, Prefix)
@@ -100,7 +100,7 @@ func (v *varsig) encode() []byte {
return buf
}
func (v *varsig) decodePayEncAndSig(r *bytes.Reader, varsig Varsig, expectedLength uint64) (Varsig, error) {
func (v *varsig[T]) decodePayEncAndSig(r *bytes.Reader, varsig *T, expectedLength uint64) (*T, error) {
payEnc, err := DecodePayloadEncoding(r, v.Version())
if err != nil {
return nil, err
@@ -118,7 +118,7 @@ func (v *varsig) decodePayEncAndSig(r *bytes.Reader, varsig Varsig, expectedLeng
return v.validateSig(varsig, expectedLength)
}
func (v *varsig) validateSig(varsig Varsig, expectedLength uint64) (Varsig, error) {
func (v *varsig[T]) validateSig(varsig *T, expectedLength uint64) (*T, error) {
if v.Version() == Version0 && len(v.sig) == 0 {
return varsig, ErrMissingSignature
}