Skip to content

Commit

Permalink
add digest test cases
Browse files Browse the repository at this point in the history
Signed-off-by: qmuntal <[email protected]>
  • Loading branch information
qmuntal committed Jul 18, 2023
1 parent bbfd793 commit 82a1648
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
20 changes: 19 additions & 1 deletion ecdsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func testSignVerify(t *testing.T, alg Algorithm, key crypto.Signer, isCryptoSign

// sign / verify round trip
// see also conformance_test.go for strict tests.
content := []byte("hello world")
content := []byte("hello world, مرحبا بالعالم")
sig, err := signer.Sign(rand.Reader, content)
if err != nil {
t.Fatalf("Sign() error = %v", err)
Expand All @@ -232,6 +232,24 @@ func testSignVerify(t *testing.T, alg Algorithm, key crypto.Signer, isCryptoSign
if err := verifier.Verify(content, sig); err != nil {
t.Fatalf("Verifier.Verify() error = %v", err)
}

// digested sign/verify round trip
dsigner, ok := signer.(DigestSigner)
if !ok {
t.Fatalf("signer is not a DigestSigner")
}
digest := sha256.Sum256(content)
dsig, err := dsigner.SignDigest(rand.Reader, digest[:])
if err != nil {
t.Fatalf("SignDigest() error = %v", err)
}
dverifier, ok := verifier.(DigestVerifier)
if !ok {
t.Fatalf("verifier is not a DigestVerifier")
}
if err := dverifier.VerifyDigest(digest[:], dsig); err != nil {
t.Fatalf("VerifyDigest() error = %v", err)
}
}

type ecdsaBadCryptoSigner struct {
Expand Down
9 changes: 9 additions & 0 deletions ed25519_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ func Test_ed25519Signer(t *testing.T) {
if err := verifier.Verify(content, sig); err != nil {
t.Fatalf("Verifier.Verify() error = %v", err)
}

_, ok := signer.(DigestSigner)
if ok {
t.Fatalf("signer shouldn't be a DigestSigner")
}
_, ok = verifier.(DigestVerifier)
if ok {
t.Fatalf("verifier shouldn't be a DigestVerifier")
}
}

func Test_ed25519Verifier_Verify_Success(t *testing.T) {
Expand Down
20 changes: 19 additions & 1 deletion rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Test_rsaSigner(t *testing.T) {

// sign / verify round trip
// see also conformance_test.go for strict tests.
content := []byte("hello world")
content := []byte("hello world, مرحبا بالعالم")
sig, err := signer.Sign(rand.Reader, content)
if err != nil {
t.Fatalf("Sign() error = %v", err)
Expand All @@ -49,6 +49,24 @@ func Test_rsaSigner(t *testing.T) {
if err := verifier.Verify(content, sig); err != nil {
t.Fatalf("Verifier.Verify() error = %v", err)
}

// digested sign/verify round trip
dsigner, ok := signer.(DigestSigner)
if !ok {
t.Fatalf("signer is not a DigestSigner")
}
digest := sha256.Sum256(content)
dsig, err := dsigner.SignDigest(rand.Reader, digest[:])
if err != nil {
t.Fatalf("SignDigest() error = %v", err)
}
dverifier, ok := verifier.(DigestVerifier)
if !ok {
t.Fatalf("verifier is not a DigestVerifier")
}
if err := dverifier.VerifyDigest(digest[:], dsig); err != nil {
t.Fatalf("VerifyDigest() error = %v", err)
}
}

func Test_rsaSigner_SignHashFailure(t *testing.T) {
Expand Down

0 comments on commit 82a1648

Please sign in to comment.