Skip to content

Commit

Permalink
test bad hash implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Shiwei Zhang <shizh@microsoft.com>
  • Loading branch information
shizhMSFT committed Aug 19, 2022
1 parent 9274c19 commit aa2f81c
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 2 deletions.
37 changes: 37 additions & 0 deletions algorithm_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package cose

import (
"crypto"
"crypto/sha256"
"hash"
"io"
"reflect"
"testing"
)
Expand Down Expand Up @@ -149,3 +153,36 @@ func TestAlgorithm_computeHash(t *testing.T) {
})
}
}

type badHash struct{}

func badHashNew() hash.Hash {
return &badHash{}
}

func (*badHash) Write(p []byte) (n int, err error) {
return 0, io.EOF
}

func (*badHash) Sum(b []byte) []byte {
return b
}

func (*badHash) Reset() {}

func (*badHash) Size() int {
return 0
}
func (*badHash) BlockSize() int {
return 0
}

func Test_computeHash(t *testing.T) {
crypto.RegisterHash(crypto.SHA256, badHashNew)
defer crypto.RegisterHash(crypto.SHA256, sha256.New)

_, err := computeHash(crypto.SHA256, nil)
if err != io.EOF {
t.Fatalf("computeHash() error = %v, wantErr %v", err, io.EOF)
}
}
44 changes: 42 additions & 2 deletions ecdsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"encoding/asn1"
"errors"
"io"
Expand Down Expand Up @@ -290,15 +291,32 @@ func Test_ecdsaBadCryptoSigner_BadSignature(t *testing.T) {
testSignFailure(t, AlgorithmES256, badSigner)
}

func Test_ecdsaKeySigner_SignHashFailure(t *testing.T) {
key := generateTestECDSAKey(t)
crypto.RegisterHash(crypto.SHA256, badHashNew)
defer crypto.RegisterHash(crypto.SHA256, sha256.New)
testSignFailure(t, AlgorithmES256, key)
}

func Test_ecdsaCryptoSigner_SignHashFailure(t *testing.T) {
wrappedKey := struct {
crypto.Signer
}{
Signer: generateTestECDSAKey(t),
}
crypto.RegisterHash(crypto.SHA256, badHashNew)
defer crypto.RegisterHash(crypto.SHA256, sha256.New)
testSignFailure(t, AlgorithmES256, wrappedKey)
}

func testSignFailure(t *testing.T, alg Algorithm, key crypto.Signer) {
signer, err := NewSigner(alg, key)
if err != nil {
t.Fatalf("NewSigner() error = %v", err)
}

content := []byte("hello world")
_, err = signer.Sign(rand.Reader, content)
if err == nil {
if _, err = signer.Sign(rand.Reader, content); err == nil {
t.Fatalf("Sign() error = nil, wantErr true")
}
}
Expand Down Expand Up @@ -421,3 +439,25 @@ func Test_ecdsaVerifier_Verify_InvalidSignature(t *testing.T) {
})
}
}

func Test_ecdsaVerifier_Verify_HashFailure(t *testing.T) {
// generate key
alg := AlgorithmES256
key := generateTestECDSAKey(t)

// generate a valid signature
content, sig := signTestData(t, alg, key)

// set up verifier
verifier, err := NewVerifier(alg, key.Public())
if err != nil {
t.Fatalf("NewVerifier() error = %v", err)
}

// verify with bad hash implementation
crypto.RegisterHash(crypto.SHA256, badHashNew)
defer crypto.RegisterHash(crypto.SHA256, sha256.New)
if err := verifier.Verify(content, sig); err == nil {
t.Fatalf("ecdsaVerifier.Verify() error = nil, wantErr true")
}
}
44 changes: 44 additions & 0 deletions rsa_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cose

import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"reflect"
"testing"
)
Expand Down Expand Up @@ -49,6 +51,26 @@ func Test_rsaSigner(t *testing.T) {
}
}

func Test_rsaSigner_SignHashFailure(t *testing.T) {
// generate key
alg := AlgorithmPS256
key := generateTestRSAKey(t)

// set up signer
signer, err := NewSigner(alg, key)
if err != nil {
t.Fatalf("NewSigner() error = %v", err)
}

// sign with bad hash implementation
crypto.RegisterHash(crypto.SHA256, badHashNew)
defer crypto.RegisterHash(crypto.SHA256, sha256.New)
content := []byte("hello world")
if _, err = signer.Sign(rand.Reader, content); err == nil {
t.Fatalf("Sign() error = nil, wantErr true")
}
}

func Test_rsaVerifier_Verify_Success(t *testing.T) {
// generate key
alg := AlgorithmPS256
Expand Down Expand Up @@ -167,3 +189,25 @@ func Test_rsaVerifier_Verify_InvalidSignature(t *testing.T) {
})
}
}

func Test_rsaVerifier_Verify_HashFailure(t *testing.T) {
// generate key
alg := AlgorithmPS256
key := generateTestRSAKey(t)

// generate a valid signature
content, sig := signTestData(t, alg, key)

// set up verifier
verifier, err := NewVerifier(alg, key.Public())
if err != nil {
t.Fatalf("NewVerifier() error = %v", err)
}

// verify with bad hash implementation
crypto.RegisterHash(crypto.SHA256, badHashNew)
defer crypto.RegisterHash(crypto.SHA256, sha256.New)
if err := verifier.Verify(content, sig); err == nil {
t.Fatalf("rsaVerifier.Verify() error = nil, wantErr true")
}
}

0 comments on commit aa2f81c

Please sign in to comment.