diff --git a/cipher/bc.go b/cipher/bc.go index bc512aa..60d4a27 100644 --- a/cipher/bc.go +++ b/cipher/bc.go @@ -3,6 +3,7 @@ package cipher import ( + "bytes" _cipher "crypto/cipher" "github.com/emmansun/gmsm/internal/subtle" @@ -15,13 +16,11 @@ type bc struct { } func newBC(b _cipher.Block, iv []byte) *bc { - c := &bc{ + return &bc{ b: b, blockSize: b.BlockSize(), - iv: make([]byte, b.BlockSize()), + iv: bytes.Clone(iv), } - copy(c.iv, iv) - return c } type bcEncrypter bc diff --git a/cipher/ccm.go b/cipher/ccm.go index bfcfa57..3fe4b80 100644 --- a/cipher/ccm.go +++ b/cipher/ccm.go @@ -239,9 +239,7 @@ func (c *ccm) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) { // so overwrites dst in the event of a tag mismatch. That // behavior is mimicked here in order to be consistent across // platforms. - for i := range out { - out[i] = 0 - } + clear(out) return nil, errOpen } return ret, nil diff --git a/cipher/ofbnlf.go b/cipher/ofbnlf.go index 674f638..4f736eb 100644 --- a/cipher/ofbnlf.go +++ b/cipher/ofbnlf.go @@ -4,6 +4,7 @@ package cipher import ( + "bytes" _cipher "crypto/cipher" "errors" ) @@ -28,8 +29,8 @@ func newOFBNLF(cipherFunc CipherCreator, key, iv []byte) (*ofbnlf, error) { if len(iv) != c.blockSize { return nil, errors.New("cipher: IV length must equal block size") } - c.iv = make([]byte, c.blockSize) - copy(c.iv, iv) + c.iv = bytes.Clone(iv) + return c, nil } diff --git a/cipher/xts_generic.go b/cipher/xts_generic.go index c091c65..a82e5f8 100644 --- a/cipher/xts_generic.go +++ b/cipher/xts_generic.go @@ -8,7 +8,7 @@ func mul2(tweak *[blockSize]byte, isGB bool) { func doubleTweaks(tweak *[blockSize]byte, tweaks []byte, isGB bool) { count := len(tweaks) >> 4 - for i := 0; i < count; i++ { + for i := range count { copy(tweaks[blockSize*i:], tweak[:]) mul2(tweak, isGB) }