mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-27 20:56:18 +08:00
cipher: reduce duplicate code
This commit is contained in:
parent
0f3d76705b
commit
6bc061a549
22
cipher/bc.go
22
cipher/bc.go
@ -5,7 +5,6 @@ package cipher
|
||||
import (
|
||||
_cipher "crypto/cipher"
|
||||
|
||||
"github.com/emmansun/gmsm/internal/alias"
|
||||
"github.com/emmansun/gmsm/internal/subtle"
|
||||
)
|
||||
|
||||
@ -51,15 +50,7 @@ func NewBCEncrypter(b _cipher.Block, iv []byte) _cipher.BlockMode {
|
||||
func (x *bcEncrypter) BlockSize() int { return x.blockSize }
|
||||
|
||||
func (x *bcEncrypter) CryptBlocks(dst, src []byte) {
|
||||
if len(src)%x.blockSize != 0 {
|
||||
panic("cipher: input not full blocks")
|
||||
}
|
||||
if len(dst) < len(src) {
|
||||
panic("cipher: output smaller than input")
|
||||
}
|
||||
if alias.InexactOverlap(dst[:len(src)], src) {
|
||||
panic("cipher: invalid buffer overlap")
|
||||
}
|
||||
validate(x.blockSize, dst, src)
|
||||
|
||||
iv := x.iv
|
||||
|
||||
@ -110,15 +101,8 @@ func NewBCDecrypter(b _cipher.Block, iv []byte) _cipher.BlockMode {
|
||||
func (x *bcDecrypter) BlockSize() int { return x.blockSize }
|
||||
|
||||
func (x *bcDecrypter) CryptBlocks(dst, src []byte) {
|
||||
if len(src)%x.blockSize != 0 {
|
||||
panic("cipher: input not full blocks")
|
||||
}
|
||||
if len(dst) < len(src) {
|
||||
panic("cipher: output smaller than input")
|
||||
}
|
||||
if alias.InexactOverlap(dst[:len(src)], src) {
|
||||
panic("cipher: invalid buffer overlap")
|
||||
}
|
||||
validate(x.blockSize, dst, src)
|
||||
|
||||
if len(src) == 0 {
|
||||
return
|
||||
}
|
||||
|
@ -21,8 +21,8 @@ func newECB(b goCipher.Block) *ecb {
|
||||
}
|
||||
}
|
||||
|
||||
func (x *ecb) validate(dst, src []byte) {
|
||||
if len(src)%x.blockSize != 0 {
|
||||
func validate(size int, dst, src []byte) {
|
||||
if len(src)%size != 0 {
|
||||
panic("cipher: input not full blocks")
|
||||
}
|
||||
if len(dst) < len(src) {
|
||||
@ -55,7 +55,7 @@ func NewECBEncrypter(b goCipher.Block) goCipher.BlockMode {
|
||||
func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
|
||||
|
||||
func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
|
||||
(*ecb)(x).validate(dst, src)
|
||||
validate(x.blockSize, dst, src)
|
||||
|
||||
for len(src) > 0 {
|
||||
x.b.Encrypt(dst[:x.blockSize], src[:x.blockSize])
|
||||
@ -86,11 +86,12 @@ func NewECBDecrypter(b goCipher.Block) goCipher.BlockMode {
|
||||
func (x *ecbDecrypter) BlockSize() int { return x.blockSize }
|
||||
|
||||
func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
|
||||
(*ecb)(x).validate(dst, src)
|
||||
validate(x.blockSize, dst, src)
|
||||
|
||||
if len(src) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for len(src) > 0 {
|
||||
x.b.Decrypt(dst[:x.blockSize], src[:x.blockSize])
|
||||
src = src[x.blockSize:]
|
||||
|
@ -6,8 +6,6 @@ package cipher
|
||||
import (
|
||||
_cipher "crypto/cipher"
|
||||
"errors"
|
||||
|
||||
"github.com/emmansun/gmsm/internal/alias"
|
||||
)
|
||||
|
||||
type ofbnlf struct {
|
||||
@ -51,15 +49,7 @@ func NewOFBNLFEncrypter(cipherFunc CipherCreator, key, iv []byte) (_cipher.Block
|
||||
func (x *ofbnlfEncrypter) BlockSize() int { return x.blockSize }
|
||||
|
||||
func (x *ofbnlfEncrypter) CryptBlocks(dst, src []byte) {
|
||||
if len(src)%x.blockSize != 0 {
|
||||
panic("cipher: input not full blocks")
|
||||
}
|
||||
if len(dst) < len(src) {
|
||||
panic("cipher: output smaller than input")
|
||||
}
|
||||
if alias.InexactOverlap(dst[:len(src)], src) {
|
||||
panic("cipher: invalid buffer overlap")
|
||||
}
|
||||
validate(x.blockSize, dst, src)
|
||||
|
||||
iv := x.iv
|
||||
k := make([]byte, x.blockSize)
|
||||
@ -104,15 +94,8 @@ func NewOFBNLFDecrypter(cipherFunc CipherCreator, key, iv []byte) (_cipher.Block
|
||||
func (x *ofbnlfDecrypter) BlockSize() int { return x.blockSize }
|
||||
|
||||
func (x *ofbnlfDecrypter) CryptBlocks(dst, src []byte) {
|
||||
if len(src)%x.blockSize != 0 {
|
||||
panic("cipher: input not full blocks")
|
||||
}
|
||||
if len(dst) < len(src) {
|
||||
panic("cipher: output smaller than input")
|
||||
}
|
||||
if alias.InexactOverlap(dst[:len(src)], src) {
|
||||
panic("cipher: invalid buffer overlap")
|
||||
}
|
||||
validate(x.blockSize, dst, src)
|
||||
|
||||
if len(src) == 0 {
|
||||
return
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user