sm4: change to use pure go for single block encryption/decryption

This commit is contained in:
Sun Yimin 2024-01-25 09:02:28 +08:00 committed by GitHub
parent 8e6f9c8fb4
commit 29b6da1d37
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import (
"crypto/cipher"
"github.com/emmansun/gmsm/internal/alias"
"github.com/emmansun/gmsm/internal/subtle"
)
// Assert that sm4CipherAsm implements the cbcEncAble and cbcDecAble interfaces.
@ -61,7 +62,21 @@ func (x *cbc) CryptBlocks(dst, src []byte) {
return
}
if x.enc == cbcEncrypt {
encryptBlocksChain(&x.b.enc[0], dst, src, &x.iv[0])
iv := x.iv
for len(src) > 0 {
// Write the xor to dst, then encrypt in place.
subtle.XORBytes(dst[:BlockSize], src[:BlockSize], iv)
x.b.Encrypt(dst[:BlockSize], dst[:BlockSize])
// Move to the next block with this block as the next iv.
iv = dst[:BlockSize]
src = src[BlockSize:]
dst = dst[BlockSize:]
}
// Save the iv for the next CryptBlocks call.
copy(x.iv, iv)
return
}

View File

@ -70,7 +70,7 @@ func (c *sm4CipherAsm) Encrypt(dst, src []byte) {
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
panic("sm4: invalid buffer overlap")
}
encryptBlockAsm(&c.enc[0], &dst[0], &src[0], INST_AES)
encryptBlockGo(c.enc, dst, src)
}
func (c *sm4CipherAsm) EncryptBlocks(dst, src []byte) {
@ -96,7 +96,7 @@ func (c *sm4CipherAsm) Decrypt(dst, src []byte) {
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
panic("sm4: invalid buffer overlap")
}
encryptBlockAsm(&c.dec[0], &dst[0], &src[0], INST_AES)
decryptBlockGo(c.dec, dst, src)
}
func (c *sm4CipherAsm) DecryptBlocks(dst, src []byte) {