smx9: rename CipherFactory to newCipher

This commit is contained in:
Sun Yimin 2023-02-15 17:30:33 +08:00 committed by GitHub
parent dd8b2f61dd
commit 21df52b623
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 25 deletions

View File

@ -46,11 +46,11 @@ func (opts *XOREncrypterOpts) Decrypt(key, ciphertext []byte) ([]byte, error) {
return key, nil
}
type CipherFactory func(key []byte) (cipher.Block, error)
type newCipher func(key []byte) (cipher.Block, error)
type baseBlockEncrypterOpts struct {
encryptType encryptType
cipherFactory CipherFactory
newCipher newCipher
cipherKeySize int
}
@ -68,18 +68,18 @@ type CBCEncrypterOpts struct {
padding padding.Padding
}
func NewCBCEncrypterOpts(padding padding.Padding, cipherFactory CipherFactory, keySize int) EncrypterOpts {
func NewCBCEncrypterOpts(padding padding.Padding, newCipher newCipher, keySize int) EncrypterOpts {
opts := new(CBCEncrypterOpts)
opts.encryptType = ENC_TYPE_CBC
opts.padding = padding
opts.cipherFactory = cipherFactory
opts.newCipher = newCipher
opts.cipherKeySize = keySize
return opts
}
// Encrypt encrypts the plaintext with the key, includes generated IV at the beginning of the ciphertext.
func (opts *CBCEncrypterOpts) Encrypt(rand io.Reader, key, plaintext []byte) ([]byte, error) {
block, err := opts.cipherFactory(key)
block, err := opts.newCipher(key)
if err != nil {
return nil, err
}
@ -96,7 +96,7 @@ func (opts *CBCEncrypterOpts) Encrypt(rand io.Reader, key, plaintext []byte) ([]
}
func (opts *CBCEncrypterOpts) Decrypt(key, ciphertext []byte) ([]byte, error) {
block, err := opts.cipherFactory(key)
block, err := opts.newCipher(key)
if err != nil {
return nil, err
}
@ -118,17 +118,17 @@ type ECBEncrypterOpts struct {
padding padding.Padding
}
func NewECBEncrypterOpts(padding padding.Padding, cipherFactory CipherFactory, keySize int) EncrypterOpts {
func NewECBEncrypterOpts(padding padding.Padding, newCipher newCipher, keySize int) EncrypterOpts {
opts := new(ECBEncrypterOpts)
opts.encryptType = ENC_TYPE_ECB
opts.padding = padding
opts.cipherFactory = cipherFactory
opts.newCipher = newCipher
opts.cipherKeySize = keySize
return opts
}
func (opts *ECBEncrypterOpts) Encrypt(rand io.Reader, key, plaintext []byte) ([]byte, error) {
block, err := opts.cipherFactory(key)
block, err := opts.newCipher(key)
if err != nil {
return nil, err
}
@ -140,7 +140,7 @@ func (opts *ECBEncrypterOpts) Encrypt(rand io.Reader, key, plaintext []byte) ([]
}
func (opts *ECBEncrypterOpts) Decrypt(key, ciphertext []byte) ([]byte, error) {
block, err := opts.cipherFactory(key)
block, err := opts.newCipher(key)
if err != nil {
return nil, err
}
@ -158,17 +158,17 @@ type CFBEncrypterOpts struct {
baseBlockEncrypterOpts
}
func NewCFBEncrypterOpts(cipherFactory CipherFactory, keySize int) EncrypterOpts {
func NewCFBEncrypterOpts(newCipher newCipher, keySize int) EncrypterOpts {
opts := new(CFBEncrypterOpts)
opts.encryptType = ENC_TYPE_CFB
opts.cipherFactory = cipherFactory
opts.newCipher = newCipher
opts.cipherKeySize = keySize
return opts
}
// Encrypt encrypts the plaintext with the key, includes generated IV at the beginning of the ciphertext.
func (opts *CFBEncrypterOpts) Encrypt(rand io.Reader, key, plaintext []byte) ([]byte, error) {
block, err := opts.cipherFactory(key)
block, err := opts.newCipher(key)
if err != nil {
return nil, err
}
@ -184,7 +184,7 @@ func (opts *CFBEncrypterOpts) Encrypt(rand io.Reader, key, plaintext []byte) ([]
}
func (opts *CFBEncrypterOpts) Decrypt(key, ciphertext []byte) ([]byte, error) {
block, err := opts.cipherFactory(key)
block, err := opts.newCipher(key)
if err != nil {
return nil, err
}
@ -205,17 +205,17 @@ type OFBEncrypterOpts struct {
baseBlockEncrypterOpts
}
func NewOFBEncrypterOpts(cipherFactory CipherFactory, keySize int) EncrypterOpts {
func NewOFBEncrypterOpts(newCipher newCipher, keySize int) EncrypterOpts {
opts := new(OFBEncrypterOpts)
opts.encryptType = ENC_TYPE_OFB
opts.cipherFactory = cipherFactory
opts.newCipher = newCipher
opts.cipherKeySize = keySize
return opts
}
// Encrypt encrypts the plaintext with the key, includes generated IV at the beginning of the ciphertext.
func (opts *OFBEncrypterOpts) Encrypt(rand io.Reader, key, plaintext []byte) ([]byte, error) {
block, err := opts.cipherFactory(key)
block, err := opts.newCipher(key)
if err != nil {
return nil, err
}
@ -231,7 +231,7 @@ func (opts *OFBEncrypterOpts) Encrypt(rand io.Reader, key, plaintext []byte) ([]
}
func (opts *OFBEncrypterOpts) Decrypt(key, ciphertext []byte) ([]byte, error) {
block, err := opts.cipherFactory(key)
block, err := opts.newCipher(key)
if err != nil {
return nil, err
}

View File

@ -1,13 +1,19 @@
package sm9
package sm9_test
import (
"bytes"
"crypto/aes"
"crypto/rand"
"encoding/hex"
"testing"
"github.com/emmansun/gmsm/padding"
"github.com/emmansun/gmsm/sm9"
)
func TestInvalidKeySize(t *testing.T) {
encOpts := []EncrypterOpts{
SM4ECBEncrypterOpts, SM4CBCEncrypterOpts, SM4CFBEncrypterOpts, SM4OFBEncrypterOpts,
encOpts := []sm9.EncrypterOpts{
sm9.SM4ECBEncrypterOpts, sm9.SM4CBCEncrypterOpts, sm9.SM4CFBEncrypterOpts, sm9.SM4OFBEncrypterOpts,
}
for _, opts := range encOpts {
_, err := opts.Encrypt(rand.Reader, []byte("123456789012345"), []byte("plaintext"))
@ -22,8 +28,8 @@ func TestInvalidKeySize(t *testing.T) {
}
func TestInvalidCiphertextSize(t *testing.T) {
encOpts := []EncrypterOpts{
SM4CBCEncrypterOpts, SM4CFBEncrypterOpts, SM4OFBEncrypterOpts,
encOpts := []sm9.EncrypterOpts{
sm9.SM4CBCEncrypterOpts, sm9.SM4CFBEncrypterOpts, sm9.SM4OFBEncrypterOpts,
}
for _, opts := range encOpts {
_, err := opts.Decrypt([]byte("1234567890123450"), []byte("ciphertext"))
@ -34,8 +40,8 @@ func TestInvalidCiphertextSize(t *testing.T) {
}
func TestEmptyCiphertext(t *testing.T) {
encOpts := []EncrypterOpts{
SM4ECBEncrypterOpts, DefaultEncrypterOpts,
encOpts := []sm9.EncrypterOpts{
sm9.SM4ECBEncrypterOpts, sm9.DefaultEncrypterOpts,
}
for _, opts := range encOpts {
_, err := opts.Decrypt([]byte("1234567890123450"), nil)
@ -44,3 +50,21 @@ func TestEmptyCiphertext(t *testing.T) {
}
}
}
func TestAESEncryption(t *testing.T) {
key, _ := hex.DecodeString("6368616e6765207468697320706173736368616e676520746869732070617373")
plaintext := []byte("Chinese IBE standard")
aescbc := sm9.NewCBCEncrypterOpts(padding.NewPKCS7Padding(aes.BlockSize), aes.NewCipher, 32)
ciphertext, err := aescbc.Encrypt(rand.Reader, key, plaintext)
if err != nil {
t.Fatal(err)
}
result, err := aescbc.Decrypt(key, ciphertext)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(plaintext, result) {
t.Fatalf("no same")
}
}