From babd5da61fbaa7a1b3a5413c3c8947d71fa1001d Mon Sep 17 00:00:00 2001 From: Michael Munday Date: Thu, 21 Apr 2016 08:00:07 -0400 Subject: [PATCH] crypto/aes: use asm for BenchmarkExpand on amd64 This reverses the change to this benchmark made in 9b6bf20. Change-Id: I79ab88286c3028d3be561957140375bbc413e7ab Reviewed-on: https://go-review.googlesource.com/22340 Run-TryBot: Michael Munday TryBot-Result: Gobot Gobot Reviewed-by: Minux Ma --- src/crypto/aes/aes_test.go | 2 +- src/crypto/aes/cipher_amd64.go | 17 +++++++++++++++++ src/crypto/aes/cipher_generic.go | 6 ++++++ src/crypto/aes/cipher_s390x.go | 6 ++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/crypto/aes/aes_test.go b/src/crypto/aes/aes_test.go index 3cc390d4e2..28144968fc 100644 --- a/src/crypto/aes/aes_test.go +++ b/src/crypto/aes/aes_test.go @@ -380,6 +380,6 @@ func BenchmarkExpand(b *testing.B) { c := &aesCipher{make([]uint32, n), make([]uint32, n)} b.ResetTimer() for i := 0; i < b.N; i++ { - expandKeyGo(tt.key, c.enc, c.dec) + expandKey(tt.key, c.enc, c.dec) } } diff --git a/src/crypto/aes/cipher_amd64.go b/src/crypto/aes/cipher_amd64.go index 3b600c36f3..b33c8ff251 100644 --- a/src/crypto/aes/cipher_amd64.go +++ b/src/crypto/aes/cipher_amd64.go @@ -64,3 +64,20 @@ func (c *aesCipherAsm) Decrypt(dst, src []byte) { } decryptBlockAsm(len(c.dec)/4-1, &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 useAsm { + rounds := 10 // rounds needed for AES128 + switch len(key) { + case 192 / 8: + rounds = 12 + case 256 / 8: + rounds = 14 + } + expandKeyAsm(rounds, &key[0], &enc[0], &dec[0]) + } else { + expandKeyGo(key, enc, dec) + } +} diff --git a/src/crypto/aes/cipher_generic.go b/src/crypto/aes/cipher_generic.go index fc2c4c52cf..f8070346e3 100644 --- a/src/crypto/aes/cipher_generic.go +++ b/src/crypto/aes/cipher_generic.go @@ -18,3 +18,9 @@ import ( 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) +} diff --git a/src/crypto/aes/cipher_s390x.go b/src/crypto/aes/cipher_s390x.go index dfb95d7d5d..bec5933013 100644 --- a/src/crypto/aes/cipher_s390x.go +++ b/src/crypto/aes/cipher_s390x.go @@ -82,3 +82,9 @@ func (c *aesCipherAsm) Decrypt(dst, src []byte) { // The decrypt function code is equal to the function code + 128. cryptBlocks(c.function+128, &c.key[0], &dst[0], &src[0], BlockSize) } + +// expandKey is used by BenchmarkExpand. cipher message (KM) does not need key +// expansion so there is no assembly equivalent. +func expandKey(key []byte, enc, dec []uint32) { + expandKeyGo(key, enc, dec) +}