sm4: reduce slice checking internally

This commit is contained in:
Sun Yimin 2024-03-27 09:36:56 +08:00 committed by GitHub
parent e4909bed2d
commit 34dd5104ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 12 additions and 7 deletions

View File

@ -71,6 +71,10 @@ func (c *sm4CipherAsm) Encrypt(dst, src []byte) {
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) { if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
panic("sm4: invalid buffer overlap") panic("sm4: invalid buffer overlap")
} }
c.encrypt(dst, src)
}
func (c *sm4CipherAsm) encrypt(dst, src []byte) {
if useAESNI4SingleBlock { if useAESNI4SingleBlock {
encryptBlockAsm(&c.enc[0], &dst[0], &src[0], INST_AES) encryptBlockAsm(&c.enc[0], &dst[0], &src[0], INST_AES)
} else { } else {

View File

@ -66,7 +66,8 @@ func (x *ctr) refill() {
copy(x.out, x.out[x.outUsed:]) copy(x.out, x.out[x.outUsed:])
x.out = x.out[:cap(x.out)] x.out = x.out[:cap(x.out)]
for remain <= len(x.out)-x.b.blocksSize { for remain <= len(x.out)-x.b.blocksSize {
x.b.EncryptBlocks(x.out[remain:], x.ctr) encryptBlocksAsm(&x.b.enc[0], x.out[remain:], x.ctr, INST_AES)
remain += x.b.blocksSize remain += x.b.blocksSize
// Generate complelte [x.b.batchBlocks] counters // Generate complelte [x.b.batchBlocks] counters

View File

@ -93,7 +93,7 @@ func (g *gcm) Seal(dst, nonce, plaintext, data []byte) []byte {
var counter, tagMask [gcmBlockSize]byte var counter, tagMask [gcmBlockSize]byte
g.deriveCounter(&counter, nonce) g.deriveCounter(&counter, nonce)
g.cipher.Encrypt(tagMask[:], counter[:]) g.cipher.encrypt(tagMask[:], counter[:])
gcmInc32(&counter) gcmInc32(&counter)
g.counterCrypt(out, plaintext, &counter) g.counterCrypt(out, plaintext, &counter)
@ -130,7 +130,7 @@ func (g *gcm) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) {
var counter, tagMask [gcmBlockSize]byte var counter, tagMask [gcmBlockSize]byte
g.deriveCounter(&counter, nonce) g.deriveCounter(&counter, nonce)
g.cipher.Encrypt(tagMask[:], counter[:]) g.cipher.encrypt(tagMask[:], counter[:])
gcmInc32(&counter) gcmInc32(&counter)
var expectedTag [gcmTagSize]byte var expectedTag [gcmTagSize]byte

View File

@ -80,7 +80,7 @@ func (g *gcmAsm) Seal(dst, nonce, plaintext, data []byte) []byte {
gcmSm4Finish(&g.bytesProductTable, &tagMask, &counter, uint64(len(nonce)), uint64(0)) gcmSm4Finish(&g.bytesProductTable, &tagMask, &counter, uint64(len(nonce)), uint64(0))
} }
g.cipher.Encrypt(tagMask[:], counter[:]) g.cipher.encrypt(tagMask[:], counter[:])
var tagOut [gcmTagSize]byte var tagOut [gcmTagSize]byte
gcmSm4Data(&g.bytesProductTable, data, &tagOut) gcmSm4Data(&g.bytesProductTable, data, &tagOut)
@ -134,7 +134,7 @@ func (g *gcmAsm) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) {
gcmSm4Finish(&g.bytesProductTable, &tagMask, &counter, uint64(len(nonce)), uint64(0)) gcmSm4Finish(&g.bytesProductTable, &tagMask, &counter, uint64(len(nonce)), uint64(0))
} }
g.cipher.Encrypt(tagMask[:], counter[:]) g.cipher.encrypt(tagMask[:], counter[:])
var expectedTag [gcmTagSize]byte var expectedTag [gcmTagSize]byte
gcmSm4Data(&g.bytesProductTable, data, &expectedTag) gcmSm4Data(&g.bytesProductTable, data, &expectedTag)

View File

@ -73,7 +73,7 @@ func (g *gcmNI) Seal(dst, nonce, plaintext, data []byte) []byte {
gcmSm4Finish(&g.bytesProductTable, &tagMask, &counter, uint64(len(nonce)), uint64(0)) gcmSm4Finish(&g.bytesProductTable, &tagMask, &counter, uint64(len(nonce)), uint64(0))
} }
g.cipher.Encrypt(tagMask[:], counter[:]) encryptBlockAsm(&g.cipher.enc[0], &tagMask[0], &counter[0], INST_SM4)
var tagOut [gcmTagSize]byte var tagOut [gcmTagSize]byte
gcmSm4Data(&g.bytesProductTable, data, &tagOut) gcmSm4Data(&g.bytesProductTable, data, &tagOut)
@ -127,7 +127,7 @@ func (g *gcmNI) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) {
gcmSm4Finish(&g.bytesProductTable, &tagMask, &counter, uint64(len(nonce)), uint64(0)) gcmSm4Finish(&g.bytesProductTable, &tagMask, &counter, uint64(len(nonce)), uint64(0))
} }
g.cipher.Encrypt(tagMask[:], counter[:]) encryptBlockAsm(&g.cipher.enc[0], &tagMask[0], &counter[0], INST_SM4)
var expectedTag [gcmTagSize]byte var expectedTag [gcmTagSize]byte
gcmSm4Data(&g.bytesProductTable, data, &expectedTag) gcmSm4Data(&g.bytesProductTable, data, &expectedTag)