Skip to content

Commit

Permalink
sm4: reduce slice checking internally
Browse files Browse the repository at this point in the history
  • Loading branch information
emmansun authored Mar 27, 2024
1 parent e4909be commit 34dd510
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 7 deletions.
4 changes: 4 additions & 0 deletions sm4/cipher_asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (c *sm4CipherAsm) Encrypt(dst, src []byte) {
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
panic("sm4: invalid buffer overlap")
}
c.encrypt(dst, src)
}

func (c *sm4CipherAsm) encrypt(dst, src []byte) {
if useAESNI4SingleBlock {
encryptBlockAsm(&c.enc[0], &dst[0], &src[0], INST_AES)
} else {
Expand Down
3 changes: 2 additions & 1 deletion sm4/ctr_cipher_asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ func (x *ctr) refill() {
copy(x.out, x.out[x.outUsed:])
x.out = x.out[:cap(x.out)]
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

// Generate complelte [x.b.batchBlocks] counters
Expand Down
4 changes: 2 additions & 2 deletions sm4/gcm_cipher_asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (g *gcm) Seal(dst, nonce, plaintext, data []byte) []byte {
var counter, tagMask [gcmBlockSize]byte
g.deriveCounter(&counter, nonce)

g.cipher.Encrypt(tagMask[:], counter[:])
g.cipher.encrypt(tagMask[:], counter[:])
gcmInc32(&counter)

g.counterCrypt(out, plaintext, &counter)
Expand Down Expand Up @@ -130,7 +130,7 @@ func (g *gcm) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) {
var counter, tagMask [gcmBlockSize]byte
g.deriveCounter(&counter, nonce)

g.cipher.Encrypt(tagMask[:], counter[:])
g.cipher.encrypt(tagMask[:], counter[:])
gcmInc32(&counter)

var expectedTag [gcmTagSize]byte
Expand Down
4 changes: 2 additions & 2 deletions sm4/sm4_gcm_asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (g *gcmAsm) Seal(dst, nonce, plaintext, data []byte) []byte {
gcmSm4Finish(&g.bytesProductTable, &tagMask, &counter, uint64(len(nonce)), uint64(0))
}

g.cipher.Encrypt(tagMask[:], counter[:])
g.cipher.encrypt(tagMask[:], counter[:])

var tagOut [gcmTagSize]byte
gcmSm4Data(&g.bytesProductTable, data, &tagOut)
Expand Down Expand Up @@ -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))
}

g.cipher.Encrypt(tagMask[:], counter[:])
g.cipher.encrypt(tagMask[:], counter[:])

var expectedTag [gcmTagSize]byte
gcmSm4Data(&g.bytesProductTable, data, &expectedTag)
Expand Down
4 changes: 2 additions & 2 deletions sm4/sm4ni_gcm_asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (g *gcmNI) Seal(dst, nonce, plaintext, data []byte) []byte {
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
gcmSm4Data(&g.bytesProductTable, data, &tagOut)
Expand Down Expand Up @@ -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))
}

g.cipher.Encrypt(tagMask[:], counter[:])
encryptBlockAsm(&g.cipher.enc[0], &tagMask[0], &counter[0], INST_SM4)

var expectedTag [gcmTagSize]byte
gcmSm4Data(&g.bytesProductTable, data, &expectedTag)
Expand Down

0 comments on commit 34dd510

Please sign in to comment.