cipher: use new functions

This commit is contained in:
Sun Yimin 2025-02-26 11:46:48 +08:00 committed by GitHub
parent a98b806453
commit 0bb54adc1e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 8 additions and 10 deletions

View File

@ -3,6 +3,7 @@
package cipher package cipher
import ( import (
"bytes"
_cipher "crypto/cipher" _cipher "crypto/cipher"
"github.com/emmansun/gmsm/internal/subtle" "github.com/emmansun/gmsm/internal/subtle"
@ -15,13 +16,11 @@ type bc struct {
} }
func newBC(b _cipher.Block, iv []byte) *bc { func newBC(b _cipher.Block, iv []byte) *bc {
c := &bc{ return &bc{
b: b, b: b,
blockSize: b.BlockSize(), blockSize: b.BlockSize(),
iv: make([]byte, b.BlockSize()), iv: bytes.Clone(iv),
} }
copy(c.iv, iv)
return c
} }
type bcEncrypter bc type bcEncrypter bc

View File

@ -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 // so overwrites dst in the event of a tag mismatch. That
// behavior is mimicked here in order to be consistent across // behavior is mimicked here in order to be consistent across
// platforms. // platforms.
for i := range out { clear(out)
out[i] = 0
}
return nil, errOpen return nil, errOpen
} }
return ret, nil return ret, nil

View File

@ -4,6 +4,7 @@
package cipher package cipher
import ( import (
"bytes"
_cipher "crypto/cipher" _cipher "crypto/cipher"
"errors" "errors"
) )
@ -28,8 +29,8 @@ func newOFBNLF(cipherFunc CipherCreator, key, iv []byte) (*ofbnlf, error) {
if len(iv) != c.blockSize { if len(iv) != c.blockSize {
return nil, errors.New("cipher: IV length must equal block size") return nil, errors.New("cipher: IV length must equal block size")
} }
c.iv = make([]byte, c.blockSize) c.iv = bytes.Clone(iv)
copy(c.iv, iv)
return c, nil return c, nil
} }

View File

@ -8,7 +8,7 @@ func mul2(tweak *[blockSize]byte, isGB bool) {
func doubleTweaks(tweak *[blockSize]byte, tweaks []byte, isGB bool) { func doubleTweaks(tweak *[blockSize]byte, tweaks []byte, isGB bool) {
count := len(tweaks) >> 4 count := len(tweaks) >> 4
for i := 0; i < count; i++ { for i := range count {
copy(tweaks[blockSize*i:], tweak[:]) copy(tweaks[blockSize*i:], tweak[:])
mul2(tweak, isGB) mul2(tweak, isGB)
} }