MAGIC - align with golang aes

This commit is contained in:
Emman 2021-03-23 10:05:18 +08:00
parent 78fd94a6aa
commit a5f2479d38
3 changed files with 20 additions and 4 deletions

View File

@ -8,6 +8,9 @@ import (
"golang.org/x/sys/cpu"
)
var supportsAES = cpu.X86.HasAES
var supportsGFMUL = cpu.X86.HasPCLMULQDQ
//go:noescape
func encryptBlocksAsm(xk *uint32, dst, src *byte)
@ -21,9 +24,6 @@ type sm4CipherAsm struct {
sm4Cipher
}
var supportsAES = cpu.X86.HasAES
var supportsGFMUL = cpu.X86.HasPCLMULQDQ
func newCipher(key []byte) (cipher.Block, error) {
if !supportsAES {
return newCipherGeneric(key)
@ -65,3 +65,13 @@ func (c *sm4CipherAsm) Decrypt(dst, src []byte) {
}
encryptBlockAsm(&c.dec[0], &dst[0], &src[0])
}
// expandKey is used by BenchmarkExpand to ensure that the asm implementation
// of key expansion is used for the benchmark when it is available.
func expandKey(key []byte, enc, dec []uint32) {
if supportsAES {
expandKeyAsm(&key[0], &ck[0], &enc[0], &dec[0])
} else {
expandKeyGo(key, enc, dec)
}
}

View File

@ -12,3 +12,9 @@ import "crypto/cipher"
func newCipher(key []byte) (cipher.Block, error) {
return newCipherGeneric(key)
}
// expandKey is used by BenchmarkExpand and should
// call an assembly implementation if one is available.
func expandKey(key []byte, enc, dec []uint32) {
expandKeyGo(key, enc, dec)
}

View File

@ -89,6 +89,6 @@ func BenchmarkExpand(b *testing.B) {
c := &sm4Cipher{make([]uint32, rounds), make([]uint32, rounds)}
b.ResetTimer()
for i := 0; i < b.N; i++ {
expandKeyGo(tt.key, c.enc, c.dec)
expandKey(tt.key, c.enc, c.dec)
}
}