mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-26 12:16:20 +08:00
smx9: rename CipherFactory to newCipher
This commit is contained in:
parent
dd8b2f61dd
commit
21df52b623
@ -46,11 +46,11 @@ func (opts *XOREncrypterOpts) Decrypt(key, ciphertext []byte) ([]byte, error) {
|
|||||||
return key, nil
|
return key, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type CipherFactory func(key []byte) (cipher.Block, error)
|
type newCipher func(key []byte) (cipher.Block, error)
|
||||||
|
|
||||||
type baseBlockEncrypterOpts struct {
|
type baseBlockEncrypterOpts struct {
|
||||||
encryptType encryptType
|
encryptType encryptType
|
||||||
cipherFactory CipherFactory
|
newCipher newCipher
|
||||||
cipherKeySize int
|
cipherKeySize int
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,18 +68,18 @@ type CBCEncrypterOpts struct {
|
|||||||
padding padding.Padding
|
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 := new(CBCEncrypterOpts)
|
||||||
opts.encryptType = ENC_TYPE_CBC
|
opts.encryptType = ENC_TYPE_CBC
|
||||||
opts.padding = padding
|
opts.padding = padding
|
||||||
opts.cipherFactory = cipherFactory
|
opts.newCipher = newCipher
|
||||||
opts.cipherKeySize = keySize
|
opts.cipherKeySize = keySize
|
||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encrypt encrypts the plaintext with the key, includes generated IV at the beginning of the ciphertext.
|
// 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) {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
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) {
|
func (opts *CBCEncrypterOpts) Decrypt(key, ciphertext []byte) ([]byte, error) {
|
||||||
block, err := opts.cipherFactory(key)
|
block, err := opts.newCipher(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -118,17 +118,17 @@ type ECBEncrypterOpts struct {
|
|||||||
padding padding.Padding
|
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 := new(ECBEncrypterOpts)
|
||||||
opts.encryptType = ENC_TYPE_ECB
|
opts.encryptType = ENC_TYPE_ECB
|
||||||
opts.padding = padding
|
opts.padding = padding
|
||||||
opts.cipherFactory = cipherFactory
|
opts.newCipher = newCipher
|
||||||
opts.cipherKeySize = keySize
|
opts.cipherKeySize = keySize
|
||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (opts *ECBEncrypterOpts) Encrypt(rand io.Reader, key, plaintext []byte) ([]byte, error) {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
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) {
|
func (opts *ECBEncrypterOpts) Decrypt(key, ciphertext []byte) ([]byte, error) {
|
||||||
block, err := opts.cipherFactory(key)
|
block, err := opts.newCipher(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -158,17 +158,17 @@ type CFBEncrypterOpts struct {
|
|||||||
baseBlockEncrypterOpts
|
baseBlockEncrypterOpts
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCFBEncrypterOpts(cipherFactory CipherFactory, keySize int) EncrypterOpts {
|
func NewCFBEncrypterOpts(newCipher newCipher, keySize int) EncrypterOpts {
|
||||||
opts := new(CFBEncrypterOpts)
|
opts := new(CFBEncrypterOpts)
|
||||||
opts.encryptType = ENC_TYPE_CFB
|
opts.encryptType = ENC_TYPE_CFB
|
||||||
opts.cipherFactory = cipherFactory
|
opts.newCipher = newCipher
|
||||||
opts.cipherKeySize = keySize
|
opts.cipherKeySize = keySize
|
||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encrypt encrypts the plaintext with the key, includes generated IV at the beginning of the ciphertext.
|
// 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) {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
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) {
|
func (opts *CFBEncrypterOpts) Decrypt(key, ciphertext []byte) ([]byte, error) {
|
||||||
block, err := opts.cipherFactory(key)
|
block, err := opts.newCipher(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -205,17 +205,17 @@ type OFBEncrypterOpts struct {
|
|||||||
baseBlockEncrypterOpts
|
baseBlockEncrypterOpts
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewOFBEncrypterOpts(cipherFactory CipherFactory, keySize int) EncrypterOpts {
|
func NewOFBEncrypterOpts(newCipher newCipher, keySize int) EncrypterOpts {
|
||||||
opts := new(OFBEncrypterOpts)
|
opts := new(OFBEncrypterOpts)
|
||||||
opts.encryptType = ENC_TYPE_OFB
|
opts.encryptType = ENC_TYPE_OFB
|
||||||
opts.cipherFactory = cipherFactory
|
opts.newCipher = newCipher
|
||||||
opts.cipherKeySize = keySize
|
opts.cipherKeySize = keySize
|
||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encrypt encrypts the plaintext with the key, includes generated IV at the beginning of the ciphertext.
|
// 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) {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
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) {
|
func (opts *OFBEncrypterOpts) Decrypt(key, ciphertext []byte) ([]byte, error) {
|
||||||
block, err := opts.cipherFactory(key)
|
block, err := opts.newCipher(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,19 @@
|
|||||||
package sm9
|
package sm9_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/aes"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"encoding/hex"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/emmansun/gmsm/padding"
|
||||||
|
"github.com/emmansun/gmsm/sm9"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestInvalidKeySize(t *testing.T) {
|
func TestInvalidKeySize(t *testing.T) {
|
||||||
encOpts := []EncrypterOpts{
|
encOpts := []sm9.EncrypterOpts{
|
||||||
SM4ECBEncrypterOpts, SM4CBCEncrypterOpts, SM4CFBEncrypterOpts, SM4OFBEncrypterOpts,
|
sm9.SM4ECBEncrypterOpts, sm9.SM4CBCEncrypterOpts, sm9.SM4CFBEncrypterOpts, sm9.SM4OFBEncrypterOpts,
|
||||||
}
|
}
|
||||||
for _, opts := range encOpts {
|
for _, opts := range encOpts {
|
||||||
_, err := opts.Encrypt(rand.Reader, []byte("123456789012345"), []byte("plaintext"))
|
_, err := opts.Encrypt(rand.Reader, []byte("123456789012345"), []byte("plaintext"))
|
||||||
@ -22,8 +28,8 @@ func TestInvalidKeySize(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestInvalidCiphertextSize(t *testing.T) {
|
func TestInvalidCiphertextSize(t *testing.T) {
|
||||||
encOpts := []EncrypterOpts{
|
encOpts := []sm9.EncrypterOpts{
|
||||||
SM4CBCEncrypterOpts, SM4CFBEncrypterOpts, SM4OFBEncrypterOpts,
|
sm9.SM4CBCEncrypterOpts, sm9.SM4CFBEncrypterOpts, sm9.SM4OFBEncrypterOpts,
|
||||||
}
|
}
|
||||||
for _, opts := range encOpts {
|
for _, opts := range encOpts {
|
||||||
_, err := opts.Decrypt([]byte("1234567890123450"), []byte("ciphertext"))
|
_, err := opts.Decrypt([]byte("1234567890123450"), []byte("ciphertext"))
|
||||||
@ -34,8 +40,8 @@ func TestInvalidCiphertextSize(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEmptyCiphertext(t *testing.T) {
|
func TestEmptyCiphertext(t *testing.T) {
|
||||||
encOpts := []EncrypterOpts{
|
encOpts := []sm9.EncrypterOpts{
|
||||||
SM4ECBEncrypterOpts, DefaultEncrypterOpts,
|
sm9.SM4ECBEncrypterOpts, sm9.DefaultEncrypterOpts,
|
||||||
}
|
}
|
||||||
for _, opts := range encOpts {
|
for _, opts := range encOpts {
|
||||||
_, err := opts.Decrypt([]byte("1234567890123450"), nil)
|
_, 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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user