Skip to content

Commit

Permalink
Added ConvertToED25519 to ED25519phSignerVerifier
Browse files Browse the repository at this point in the history
Signed-off-by: Riccardo Schirone <riccardo.schirone@trailofbits.com>
  • Loading branch information
ret2libc committed Feb 2, 2024
1 parent e1f9a17 commit 59c5484
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/signature/ed25519ph.go
Expand Up @@ -46,6 +46,16 @@ func LoadED25519phSigner(priv ed25519.PrivateKey) (*ED25519phSigner, error) {
}, nil
}

// ToED25519SignerVerifier creates a ED25519SignerVerifier from a ED25519phSignerVerifier
//
// Clients that use ED25519phSignerVerifier should use this method to get a
// SignerVerifier that uses the same ED25519 private key, but with the Pure
// Ed25519 algorithm. This might be necessary to interact with Fulcio, which
// only supports the Pure Ed25519 algorithm.
func (e ED25519phSignerVerifier) ToED25519SignerVerifier() (*ED25519SignerVerifier, error) {
return LoadED25519SignerVerifier(e.priv)
}

// SignMessage signs the provided message. If the message is provided,
// this method will compute the digest according to the hash function specified
// when the ED25519phSigner was created.
Expand Down
27 changes: 27 additions & 0 deletions pkg/signature/signerverifier_test.go
Expand Up @@ -17,6 +17,7 @@ package signature
import (
"bytes"
"crypto"
"crypto/ed25519"
"crypto/rsa"
"encoding/base64"
"testing"
Expand Down Expand Up @@ -53,3 +54,29 @@ func TestLoadRSAPSSSignerVerifier(t *testing.T) {
t.Fatalf("unexpected error verifying expected signature: %v", err)
}
}

func TestConvertED25519ph(t *testing.T) {
privateKey, err := cryptoutils.UnmarshalPEMToPrivateKey([]byte(ed25519Priv), cryptoutils.SkipPassword)
if err != nil {
t.Fatalf("unexpected error unmarshalling public key: %v", err)
}
edPriv, ok := privateKey.(ed25519.PrivateKey)
if !ok {
t.Fatalf("expected ed25519.PrivateKey")
}

sv, err := LoadED25519phSignerVerifier(edPriv)
if err != nil {
t.Fatalf("unexpected error creating signer/verifier: %v", err)
}

newSV, err := sv.ToED25519SignerVerifier()
if err != nil {
t.Fatalf("unexpected error converting to ed25519: %v", err)
}

message := []byte("sign me")
sig, _ := base64.StdEncoding.DecodeString("cnafwd8DKq2nQ564eN66ckYV8anVFGFi5vaYiQg2aal7ej/J0/OE0PPdKHLHe9wdzWRMFy5MpurRD/2cGXGLBQ==")
testingSigner(t, newSV, "ed25519", crypto.SHA256, message)
testingVerifier(t, newSV, "ed25519", crypto.SHA256, sig, message)
}

0 comments on commit 59c5484

Please sign in to comment.