Skip to content

Commit

Permalink
cipher: reduce duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
emmansun authored Dec 13, 2023
1 parent 0f3d767 commit 6bc061a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 43 deletions.
22 changes: 3 additions & 19 deletions cipher/bc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package cipher
import (
_cipher "crypto/cipher"

"github.com/emmansun/gmsm/internal/alias"
"github.com/emmansun/gmsm/internal/subtle"
)

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 5 additions & 4 deletions cipher/ecb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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:]
Expand Down
23 changes: 3 additions & 20 deletions cipher/ofbnlf.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ package cipher
import (
_cipher "crypto/cipher"
"errors"

"github.com/emmansun/gmsm/internal/alias"
)

type ofbnlf struct {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 6bc061a

Please sign in to comment.