Skip to content

Commit

Permalink
adding some comments and removing logging from verifiers
Browse files Browse the repository at this point in the history
Signed-off-by: David Lawrence <[email protected]> (github: endophage)
  • Loading branch information
David Lawrence committed May 7, 2015
1 parent d5f0d60 commit c84345b
Showing 1 changed file with 6 additions and 26 deletions.
32 changes: 6 additions & 26 deletions signed/verifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ import (
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"log"
"reflect"

"github.com/flynn/go-tuf/Godeps/_workspace/src/github.com/agl/ed25519"
)

// The Verifier interface provides flexibility over exactly how a verifier
// is implemented vs having a simple function type.
type Verifier interface {
Verify(key []byte, msg []byte, sig []byte) error
}

// Verifiers serves as a map of all verifiers available on the system and
// can be injected into a verificationService. For testing and configuration
// purposes, it will not be used by default.
// Verifiers is used to map algorithm names to Verifier instances.
var Verifiers = map[string]Verifier{
"ed25519": Ed25519Verifier{},
//"rsa": RSAVerifier{},
Expand All @@ -26,28 +24,15 @@ var Verifiers = map[string]Verifier{
// RegisterVerifier provides a convenience function for init() functions
// to register additional verifiers or replace existing ones.
func RegisterVerifier(name string, v Verifier) {
curr, ok := Verifiers[name]
if ok {
typOld := reflect.TypeOf(curr)
typNew := reflect.TypeOf(v)
log.Printf(
"Replacing already loaded verifier %s:%s with %s:%s\n",
typOld.PkgPath(), typOld.Name(),
typNew.PkgPath(), typNew.Name(),
)
} else {
log.Println("Adding verifier for: ", name)
}
Verifiers[name] = v
}

// Ed25519Verifier is an implementation of a Verifier that verifys ed25519 signatures
type Ed25519Verifier struct{}

func (v Ed25519Verifier) Verify(key []byte, msg []byte, sig []byte) error {
log.Println("Verifying signature with Ed25519")
var sigBytes [ed25519.SignatureSize]byte
if len(sig) != len(sigBytes) {
log.Printf("Signature length is incorrect, must be %d, was %d.\n", ed25519.SignatureSize, len(sig))
return ErrInvalid
}
copy(sigBytes[:], sig)
Expand All @@ -56,34 +41,29 @@ func (v Ed25519Verifier) Verify(key []byte, msg []byte, sig []byte) error {
copy(keyBytes[:], key)

if !ed25519.Verify(&keyBytes, msg, &sigBytes) {
log.Println("Failed ed25519 verification")
return ErrInvalid
}
log.Println("verification succeeded.")
return nil
}

// RSAVerifier is an implementation of a Verifier that verifys RSA signatures.
// N.B. Currently not covered by unit tests, use at your own risk.
type RSAVerifier struct{}

func (v RSAVerifier) Verify(key []byte, msg []byte, sig []byte) error {
log.Printf("Verifying signature with RSA %d\n", len(sig)*8)
digest := sha256.Sum256(msg)
pub, err := x509.ParsePKIXPublicKey(key)
if err != nil {
log.Printf("Failed to parse public key: %s\n", err)
return ErrInvalid
}

rsaPub, ok := pub.(*rsa.PublicKey)
if !ok {
log.Println("Value returned from ParsePKIXPublicKey was not an RSA public key")
return ErrInvalid
}

if err = rsa.VerifyPKCS1v15(rsaPub, crypto.SHA256, digest[:], sig); err != nil {
log.Printf("Failed verification: %s\n", err)
return ErrInvalid
}
log.Println("verification succeeded.")
return nil
}

0 comments on commit c84345b

Please sign in to comment.