sm4: add test cases, cover more plaintext length

This commit is contained in:
Sun Yimin 2023-08-04 08:25:16 +08:00 committed by GitHub
parent 71afa44b91
commit 24637cf61d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 3 deletions

View File

@ -94,8 +94,8 @@ func TestECBBasic(t *testing.T) {
func TestECBRandom(t *testing.T) {
key := []byte("0123456789ABCDEF")
plaintext := make([]byte, 448)
ciphertext := make([]byte, 448)
plaintext := make([]byte, 464)
ciphertext := make([]byte, 464)
io.ReadFull(rand.Reader, plaintext)
c, err := sm4.NewCipher(key)
if err != nil {
@ -103,7 +103,7 @@ func TestECBRandom(t *testing.T) {
}
encrypter := cipher.NewECBEncrypter(c)
encrypter.CryptBlocks(ciphertext, plaintext)
result := make([]byte, 448)
result := make([]byte, 464)
decrypter := cipher.NewECBDecrypter(c)
decrypter.CryptBlocks(result, ciphertext)
if !bytes.Equal(result, plaintext) {

View File

@ -3,7 +3,9 @@ package cipher_test
import (
"bytes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"io"
"testing"
"github.com/emmansun/gmsm/sm4"
@ -391,3 +393,28 @@ func TestGCMCounterWrap(t *testing.T) {
}
}
}
func TestSM4GCMRandom(t *testing.T) {
key := []byte("0123456789ABCDEF")
nonce := []byte("0123456789AB")
plaintext := make([]byte, 464)
io.ReadFull(rand.Reader, plaintext)
c, err := sm4.NewCipher(key)
if err != nil {
t.Fatal(err)
}
aead, err := cipher.NewGCMWithNonceSize(c, len(nonce))
if err != nil {
t.Fatal(err)
}
got := aead.Seal(nil, nonce, plaintext, nil)
result, err := aead.Open(nil, nonce, got, nil)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(result, plaintext) {
t.Error("gcm seal/open 464 bytes fail")
}
}