From ca18fb55f4397550d17d61a69a184a35448dfaab Mon Sep 17 00:00:00 2001 From: Sun Yimin Date: Tue, 6 Dec 2022 10:11:02 +0800 Subject: [PATCH] sm2: test decrypt error --- sm2/sm2_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/sm2/sm2_test.go b/sm2/sm2_test.go index a068644..c975da0 100644 --- a/sm2/sm2_test.go +++ b/sm2/sm2_test.go @@ -224,6 +224,30 @@ func TestCiphertextASN12Plain(t *testing.T) { } } +func TestEncryptWithInfinitePublicKey(t *testing.T) { + pub := new(ecdsa.PublicKey) + pub.Curve = P256() + pub.X = big.NewInt(0) + pub.Y = big.NewInt(0) + + _, err := Encrypt(rand.Reader, pub, []byte("sm2 encryption standard"), nil) + if err == nil { + t.Fatalf("should be failed") + } +} + +func TestEncryptEmptyPlaintext(t *testing.T) { + priv, _ := GenerateKey(rand.Reader) + ciphertext, err := Encrypt(rand.Reader, &priv.PublicKey, nil, nil) + if err != nil || ciphertext != nil { + t.Fatalf("nil plaintext should return nil") + } + ciphertext, err = Encrypt(rand.Reader, &priv.PublicKey, []byte{}, nil) + if err != nil || ciphertext != nil { + t.Fatalf("empty plaintext should return nil") + } +} + func TestEncryptDecrypt(t *testing.T) { priv, _ := GenerateKey(rand.Reader) priv2, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) @@ -293,6 +317,28 @@ func TestEncryptDecrypt(t *testing.T) { } } +func TestInvalidCiphertext(t *testing.T) { + priv, _ := GenerateKey(rand.Reader) + tests := []struct { + name string + ciphertext []byte + }{ + // TODO: Add test cases. + {errCiphertextTooShort.Error(), make([]byte, 65)}, + {ErrDecryption.Error(), append([]byte{0x04}, make([]byte, 96)...)}, + {ErrDecryption.Error(), append([]byte{0x04}, make([]byte, 97)...)}, + {ErrDecryption.Error(), append([]byte{0x02}, make([]byte, 65)...)}, + {ErrDecryption.Error(), append([]byte{0x30}, make([]byte, 97)...)}, + {ErrDecryption.Error(), make([]byte, 97)}, + } + for i, tt := range tests { + _, err := Decrypt(priv, tt.ciphertext) + if err.Error() != tt.name { + t.Fatalf("case %v, expected %v, got %v\n", i, tt.name, err.Error()) + } + } +} + func TestSignVerify(t *testing.T) { priv, _ := GenerateKey(rand.Reader) tests := []struct {