mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-26 12:16:20 +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 (
|
import (
|
||||||
_cipher "crypto/cipher"
|
_cipher "crypto/cipher"
|
||||||
|
|
||||||
"github.com/emmansun/gmsm/internal/alias"
|
|
||||||
"github.com/emmansun/gmsm/internal/subtle"
|
"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) BlockSize() int { return x.blockSize }
|
||||||
|
|
||||||
func (x *bcEncrypter) CryptBlocks(dst, src []byte) {
|
func (x *bcEncrypter) CryptBlocks(dst, src []byte) {
|
||||||
if len(src)%x.blockSize != 0 {
|
validate(x.blockSize, dst, src)
|
||||||
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")
|
|
||||||
}
|
|
||||||
|
|
||||||
iv := x.iv
|
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) BlockSize() int { return x.blockSize }
|
||||||
|
|
||||||
func (x *bcDecrypter) CryptBlocks(dst, src []byte) {
|
func (x *bcDecrypter) CryptBlocks(dst, src []byte) {
|
||||||
if len(src)%x.blockSize != 0 {
|
validate(x.blockSize, dst, src)
|
||||||
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")
|
|
||||||
}
|
|
||||||
if len(src) == 0 {
|
if len(src) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -21,8 +21,8 @@ func newECB(b goCipher.Block) *ecb {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *ecb) validate(dst, src []byte) {
|
func validate(size int, dst, src []byte) {
|
||||||
if len(src)%x.blockSize != 0 {
|
if len(src)%size != 0 {
|
||||||
panic("cipher: input not full blocks")
|
panic("cipher: input not full blocks")
|
||||||
}
|
}
|
||||||
if len(dst) < len(src) {
|
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) BlockSize() int { return x.blockSize }
|
||||||
|
|
||||||
func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
|
func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
|
||||||
(*ecb)(x).validate(dst, src)
|
validate(x.blockSize, dst, src)
|
||||||
|
|
||||||
for len(src) > 0 {
|
for len(src) > 0 {
|
||||||
x.b.Encrypt(dst[:x.blockSize], src[:x.blockSize])
|
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) BlockSize() int { return x.blockSize }
|
||||||
|
|
||||||
func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
|
func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
|
||||||
(*ecb)(x).validate(dst, src)
|
validate(x.blockSize, dst, src)
|
||||||
|
|
||||||
if len(src) == 0 {
|
if len(src) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for len(src) > 0 {
|
for len(src) > 0 {
|
||||||
x.b.Decrypt(dst[:x.blockSize], src[:x.blockSize])
|
x.b.Decrypt(dst[:x.blockSize], src[:x.blockSize])
|
||||||
src = src[x.blockSize:]
|
src = src[x.blockSize:]
|
||||||
|
@ -6,8 +6,6 @@ package cipher
|
|||||||
import (
|
import (
|
||||||
_cipher "crypto/cipher"
|
_cipher "crypto/cipher"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/emmansun/gmsm/internal/alias"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ofbnlf struct {
|
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) BlockSize() int { return x.blockSize }
|
||||||
|
|
||||||
func (x *ofbnlfEncrypter) CryptBlocks(dst, src []byte) {
|
func (x *ofbnlfEncrypter) CryptBlocks(dst, src []byte) {
|
||||||
if len(src)%x.blockSize != 0 {
|
validate(x.blockSize, dst, src)
|
||||||
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")
|
|
||||||
}
|
|
||||||
|
|
||||||
iv := x.iv
|
iv := x.iv
|
||||||
k := make([]byte, x.blockSize)
|
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) BlockSize() int { return x.blockSize }
|
||||||
|
|
||||||
func (x *ofbnlfDecrypter) CryptBlocks(dst, src []byte) {
|
func (x *ofbnlfDecrypter) CryptBlocks(dst, src []byte) {
|
||||||
if len(src)%x.blockSize != 0 {
|
validate(x.blockSize, dst, src)
|
||||||
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")
|
|
||||||
}
|
|
||||||
if len(src) == 0 {
|
if len(src) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user