mirror of
https://github.com/emmansun/gmsm.git
synced 2025-05-12 12:06:18 +08:00
sm4: add test cases, cover more plaintext length
This commit is contained in:
parent
71afa44b91
commit
24637cf61d
@ -94,8 +94,8 @@ func TestECBBasic(t *testing.T) {
|
|||||||
|
|
||||||
func TestECBRandom(t *testing.T) {
|
func TestECBRandom(t *testing.T) {
|
||||||
key := []byte("0123456789ABCDEF")
|
key := []byte("0123456789ABCDEF")
|
||||||
plaintext := make([]byte, 448)
|
plaintext := make([]byte, 464)
|
||||||
ciphertext := make([]byte, 448)
|
ciphertext := make([]byte, 464)
|
||||||
io.ReadFull(rand.Reader, plaintext)
|
io.ReadFull(rand.Reader, plaintext)
|
||||||
c, err := sm4.NewCipher(key)
|
c, err := sm4.NewCipher(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -103,7 +103,7 @@ func TestECBRandom(t *testing.T) {
|
|||||||
}
|
}
|
||||||
encrypter := cipher.NewECBEncrypter(c)
|
encrypter := cipher.NewECBEncrypter(c)
|
||||||
encrypter.CryptBlocks(ciphertext, plaintext)
|
encrypter.CryptBlocks(ciphertext, plaintext)
|
||||||
result := make([]byte, 448)
|
result := make([]byte, 464)
|
||||||
decrypter := cipher.NewECBDecrypter(c)
|
decrypter := cipher.NewECBDecrypter(c)
|
||||||
decrypter.CryptBlocks(result, ciphertext)
|
decrypter.CryptBlocks(result, ciphertext)
|
||||||
if !bytes.Equal(result, plaintext) {
|
if !bytes.Equal(result, plaintext) {
|
||||||
|
@ -3,7 +3,9 @@ package cipher_test
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"io"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/emmansun/gmsm/sm4"
|
"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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user