1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
package torchwood
import (
"crypto/ed25519"
"encoding/base64"
"errors"
"strconv"
"strings"
"golang.org/x/mod/sumdb/note"
)
const algEd25519 = 1
// NewVerifierFromSigner constructs a new c2sp.org/signed-note [note.Verifier]
// from an encoded Ed25519 signer key, the same input as [note.NewSigner].
func NewVerifierFromSigner(skey string) (note.Verifier, error) {
priv1, skey := chop(skey, "+")
priv2, skey := chop(skey, "+")
name, skey := chop(skey, "+")
hash16, key64 := chop(skey, "+")
hash, err1 := strconv.ParseUint(hash16, 16, 32)
key, err2 := base64.StdEncoding.DecodeString(key64)
if priv1 != "PRIVATE" || priv2 != "KEY" || len(hash16) != 8 || err1 != nil || err2 != nil || !isValidName(name) || len(key) == 0 {
return nil, errors.New("malformed verifier id")
}
alg, key := key[0], key[1:]
if alg != algEd25519 {
return nil, errors.New("unknown verifier algorithm")
}
if len(key) != 32 {
return nil, errors.New("malformed verifier id")
}
pub := ed25519.NewKeyFromSeed(key).Public().(ed25519.PublicKey)
if uint32(hash) != keyHash(name, append([]byte{algEd25519}, pub...)) {
return nil, errors.New("invalid verifier hash")
}
return &verifier{
name: name,
hash: uint32(hash),
verify: func(msg, sig []byte) bool {
return ed25519.Verify(pub, msg, sig)
},
}, nil
}
type verifier struct {
name string
hash uint32
verify func(msg, sig []byte) bool
}
func (v *verifier) Name() string { return v.name }
func (v *verifier) KeyHash() uint32 { return v.hash }
func (v *verifier) Verify(msg, sig []byte) bool { return v.verify(msg, sig) }
// chop chops s at the first instance of sep, if any,
// and returns the text before and after sep.
// If sep is not present, chop returns before is s and after is empty.
func chop(s, sep string) (before, after string) {
i := strings.Index(s, sep)
if i < 0 {
return s, ""
}
return s[:i], s[i+len(sep):]
}
|