target/arm: use x86 intrinsics to implement AES instructions
RFC PATCH 1/1 target/arm: use x86 intrinsics to implement AES instructions
ARM intrinsics for AES deviate from the x86 ones in the way they cover the different stages of each round, and so mapping one to the other is not entirely straight-forward. However, with a bit of care, we can still use the x86 ones to emulate the ARM ones, which makes them constant time (which is an important property in crypto) and substantially more efficient.
---
Suggestions welcome on how to make this more generic across targets and compilers etc.
target/arm/tcg/crypto_helper.c | 43 ++++++++++++++++++++ 1 file changed, 43 insertions(+)
target/arm/tcg/crypto_helper.c +43 -0
@@ -18,10 +18,32 @@#include "crypto/sm4.h" #include "vec_internal.h" +#ifdef __x86_64 +#pragma GCC target ("aes") +#include <cpuid.h> +#include <wmmintrin.h> + +static bool have_aes (void ) +{ +static int cpuid_have_aes =-1 ; + +if (cpuid_have_aes ==-1 ) { +unsigned int eax, ebx, ecx, edx; +int ret =__get_cpuid (0x1 , &eax, &ebx, &ecx, &edx); + + cpuid_have_aes = ret && (ecx & bit_AES); + } +return cpuid_have_aes >0 ; +} +#endif +union CRYPTO_STATE {uint8_t bytes[16 ];uint32_t words[4 ];uint64_t l[2 ]; +#ifdef __x86_64 +__m128i vec; +#endif };#if HOST_BIG_ENDIAN
@@ -54,6 +76,16 @@ static void do_crypto_aese(uint64_t *rd, uint64_t *rn,union CRYPTO_STATE st = { .l = { rn[0 ], rn[1 ] } };int i; +#ifdef __x86_64__ +if (have_aes ()) { +__m128i *d = (__m128i *)rd; + + *d = decrypt ?_mm_aesdeclast_si128 (rk.vec ^ st.vec, (__m128i ){}) + :_mm_aesenclast_si128 (rk.vec ^ st.vec, (__m128i ){}); +return ; + } +#endif +/* xor state vector with round key */ rk.l[0 ] ^= st.l[0 ]; rk.l[1 ] ^= st.l[1 ];
@@ -217,6 +249,17 @@ static void do_crypto_aesmc(uint64_t *rd, uint64_t *rm, bool decrypt)union CRYPTO_STATE st = { .l = { rm[0 ], rm[1 ] } };int i; +#ifdef __x86_64__ +if (have_aes ()) { +__m128i *d = (__m128i *)rd; + + *d = decrypt ?_mm_aesdec_si128 (_mm_aesenclast_si128 (st.vec, (__m128i ){}), + (__m128i ){}) + :_mm_aesenc_si128 (_mm_aesdeclast_si128 (st.vec, (__m128i ){}), + (__m128i ){}); +return ; + } +#endif for (i =0 ; i <16 ; i +=4 ) {CR_ST_WORD (st, i >>2 ) = mc[decrypt][CR_ST_BYTE(st, i)] ^
Signature · 1 line
2.39.2
Hi Ard,
Quoted 35 lines · Ard Biesheuvel
ARM intrinsics for AES deviate from the x86 ones in the way they cover the different stages of each round, and so mapping one to the other is not entirely straight-forward. However, with a bit of care, we can still use the x86 ones to emulate the ARM ones, which makes them constant time (which is an important property in crypto) and substantially more efficient.
Cc Peter Maydell <[email protected]>Cc Alex Bennée <[email protected]>Cc Richard Henderson <[email protected]>Cc Philippe Mathieu-Daudé <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>---
Suggestions welcome on how to make this more generic across targets and compilers etc.target/arm/tcg/crypto_helper.c | 43 ++++++++++++++++++++ 1 file changed, 43 insertions(+)target/arm/tcg/crypto_helper.c +8
-0@@ -18,10 +18,32 @@#include "crypto/sm4.h" #include "vec_internal.h" +#ifdef __x86_64 +#pragma GCC target ("aes") +#include <cpuid.h> +#include <wmmintrin.h> + +static bool have_aes (void ) +{ +static int cpuid_have_aes =-1 ;
target/arm/tcg/crypto_helper.c line 38
Quoted 10 lines
target/arm/tcg/crypto_helper.c +10
-0+ +if (cpuid_have_aes ==-1 ) { +unsigned int eax, ebx, ecx, edx; +int ret =__get_cpuid (0x1 , &eax, &ebx, &ecx, &edx); + + cpuid_have_aes = ret && (ecx & bit_AES); + } + return cpuid_have_aes > 0; +} +#endif
Per the recent cpuinfo API added in commit 6bc12fd042, I suppose we should add CPUINFO_AES to host/include/i386/host/cpuinfo.h.
Quoted 24 lines · Ard Biesheuvel
ARM intrinsics for AES deviate from the x86 ones in the way they cover the different stages of each round, and so mapping one to the other is not entirely straight-forward. However, with a bit of care, we can still use the x86 ones to emulate the ARM ones, which makes them constant time (which is an important property in crypto) and substantially more efficient.
Cc Peter Maydell <[email protected]>Cc Alex Bennée <[email protected]>Cc Richard Henderson <[email protected]>Cc Philippe Mathieu-Daudé <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>---
Suggestions welcome on how to make this more generic across targets and compilers etc.target/arm/tcg/crypto_helper.c | 43 ++++++++++++++++++++ 1 file changed, 43 insertions(+)target/arm/tcg/crypto_helper.c +0
-0
target/arm/tcg/crypto_helper.c line 22
Quoted 5 lines
target/arm/tcg/crypto_helper.c +2
-0@@ -18,10 +18,32 @@#include "crypto/sm4.h" #include "vec_internal.h" +#ifdef __x86_64 +#pragma GCC target ("aes")
This doesn't work with clang.
What does work is __attribute__((__target__("aes"))), which requires that you pull those little code blocks out to separate functions to be annotated. I believe they'll be inlined afterward, but not really relevant for your improvement.
Quoted 6 lines
target/arm/tcg/crypto_helper.c +6
-0+#include <cpuid.h> +#include <wmmintrin.h> + +static bool have_aes (void ) +{ +static int cpuid_have_aes =-1 ;
target/arm/tcg/crypto_helper.c line 38
Quoted 10 lines
target/arm/tcg/crypto_helper.c +10
-0+ +if (cpuid_have_aes ==-1 ) { +unsigned int eax, ebx, ecx, edx; +int ret =__get_cpuid (0x1 , &eax, &ebx, &ecx, &edx); + + cpuid_have_aes = ret && (ecx & bit_AES); + } + return cpuid_have_aes > 0; +} +#endif
See "host/include/i386/host/cpuinfo.h" and "util/cpuinfo-i386.c".
I'll have a closer look at the other AES uses to see what might be sharable.
r~
target/arm/tcg/crypto_helper.c static void do_crypto_aese(uint64_t *rd, uint64_t *rn, · line 84
Quoted 6 lines · Ard Biesheuvel
target/arm/tcg/crypto_helper.c +6
-0+#ifdef __x86_64__ +if (have_aes ()) { +__m128i *d = (__m128i *)rd; + + *d = decrypt ?_mm_aesdeclast_si128 (rk.vec ^ st.vec, (__m128i ){}) + :_mm_aesenclast_si128 (rk.vec ^ st.vec, (__m128i ){});
Do I correctly understand that the ARM xor is pre-shift
target/arm/tcg/crypto_helper.c static void do_crypto_aese(uint64_t *rd, uint64_t *rn, · line 91
Quoted 7 lines
target/arm/tcg/crypto_helper.c +4
-0+ return; + } +#endif +/* xor state vector with round key */ rk.l[0 ] ^= st.l[0 ]; rk.l[1 ] ^= st.l[1 ];
(like so)
whereas the x86 xor is post-shift
Quoted 9 lines
void
glue(helper_aesenclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s){ int i; Reg st = *v; Reg rk = *s; for (i = 0; i < 8 << SHIFT; i++) { d->B(i) = rk.B(i) ^ (AES_sbox[st.B(AES_shifts[i & 15] + (i & ~15))]); }
(like so, from target/i386/ops_sse.h)?
What might help: could we do the reverse -- emulate the x86 aesdeclast instruction with the aarch64 aesd instruction?
r~
Quoted 29 lines · Richard Henderson
target/arm/tcg/crypto_helper.c static void do_crypto_aese(uint64_t *rd, uint64_t *rn, · line 84
Quoted 6 lines · Ard Biesheuvel
target/arm/tcg/crypto_helper.c +6
-0+#ifdef __x86_64__ +if (have_aes ()) { +__m128i *d = (__m128i *)rd; + + *d = decrypt ?_mm_aesdeclast_si128 (rk.vec ^ st.vec, (__m128i ){}) + :_mm_aesenclast_si128 (rk.vec ^ st.vec, (__m128i ){});Do I correctly understand that the ARM xor is pre-shift
target/arm/tcg/crypto_helper.c static void do_crypto_aese(uint64_t *rd, uint64_t *rn, · line 91
Quoted 7 lines
target/arm/tcg/crypto_helper.c +4
-0+ return; + } +#endif +/* xor state vector with round key */ rk.l[0 ] ^= st.l[0 ]; rk.l[1 ] ^= st.l[1 ];(like so)
whereas the x86 xor is post-shift
Quoted 6 lines
void
glue(helper_aesenclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s){ int i; Reg st = *v; Reg rk = *s;
Quoted 6 lines
Quoted 3 lines
for (i = 0; i < 8 << SHIFT; i++) { d->B(i) = rk.B(i) ^ (AES_sbox[st.B(AES_shifts[i & 15] + (i & ~15))]); }(like so, from
target/i386/ops_sse.h)?
Indeed. Using the primitive operations defined in the AES paper, we basically have the following for n rounds of AES (for n in {10, 12, 14})
for (n-1 rounds) {
AddRoundKey
ShiftRows
SubBytes
MixColumns
}
AddRoundKey
ShiftRows
SubBytes
AddRoundKey
AddRoundKey is just XOR, but it is incorporated into the instructions that combine a couple of these steps.
So on x86, we have
aesenc:
ShiftRows
SubBytes
MixColumns
AddRoundKey
aesenclast:
ShiftRows
SubBytes
AddRoundKey
and on ARM we have
aese:
AddRoundKey
ShiftRows
SubBytes
aesmc:
MixColumns
Quoted 3 lines
What might help: could we do the reverse -- emulate the x86 aesdeclast instruction with the aarch64 aesd instruction?
Help in what sense? To emulate the x86 instructions on a ARM host?
But yes, aesenclast can be implement using aese in a similar way, i.e., by passing a {0} vector as the round key into the instruction, and performing the XOR explicitly using the real round key afterwards.
Quoted 80 lines · Ard Biesheuvel
Quoted 29 lines · Richard Henderson
target/arm/tcg/crypto_helper.c static void do_crypto_aese(uint64_t *rd, uint64_t *rn, · line 84
Quoted 6 lines · Ard Biesheuvel
target/arm/tcg/crypto_helper.c +6
-0+#ifdef __x86_64__ +if (have_aes ()) { +__m128i *d = (__m128i *)rd; + + *d = decrypt ?_mm_aesdeclast_si128 (rk.vec ^ st.vec, (__m128i ){}) + :_mm_aesenclast_si128 (rk.vec ^ st.vec, (__m128i ){});Do I correctly understand that the ARM xor is pre-shift
target/arm/tcg/crypto_helper.c static void do_crypto_aese(uint64_t *rd, uint64_t *rn, · line 91
Quoted 7 lines
target/arm/tcg/crypto_helper.c +4
-0+ return; + } +#endif +/* xor state vector with round key */ rk.l[0 ] ^= st.l[0 ]; rk.l[1 ] ^= st.l[1 ];(like so)
whereas the x86 xor is post-shift
Quoted 6 lines
void
glue(helper_aesenclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s){ int i; Reg st = *v; Reg rk = *s;Quoted 6 lines
Quoted 3 lines
for (i = 0; i < 8 << SHIFT; i++) { d->B(i) = rk.B(i) ^ (AES_sbox[st.B(AES_shifts[i & 15] + (i & ~15))]); }(like so, from
target/i386/ops_sse.h)?Indeed. Using the primitive operations defined in the AES paper, we basically have the following for n rounds of AES (for n in {10, 12, 14})
for (n-1 rounds) {
AddRoundKey
ShiftRows
SubBytes
MixColumns
}
AddRoundKey
ShiftRows
SubBytes
AddRoundKeyAddRoundKey is just XOR, but it is incorporated into the instructions that combine a couple of these steps.
So on x86, we have
aesenc:
ShiftRows
SubBytes
MixColumns
AddRoundKeyaesenclast:
ShiftRows
SubBytes
AddRoundKeyand on ARM we have
aese:
AddRoundKey
ShiftRows
SubBytesaesmc:
MixColumns
Quoted 5 lines
Quoted 3 lines
What might help: could we do the reverse -- emulate the x86 aesdeclast instruction with the aarch64 aesd instruction?
Help in what sense? To emulate the x86 instructions on a ARM host?
Well that too. I meant help me understand the two primitives.
Quoted 3 lines
But yes, aesenclast can be implement using aese in a similar way, i.e., by passing a {0} vector as the round key into the instruction, and performing the XOR explicitly using the real round key afterwards.
Excellent, thanks.
r~
Quoted 7 lines · Ard Biesheuvel
ARM intrinsics for AES deviate from the x86 ones in the way they cover the different stages of each round, and so mapping one to the other is not entirely straight-forward. However, with a bit of care, we can still use the x86 ones to emulate the ARM ones, which makes them constant time (which is an important property in crypto) and substantially more efficient.
Do you have examples of workloads and speedups obtained, by the way?
thanks
-- PMM
Quoted 10 lines · Peter Maydell
Quoted 7 lines · Ard Biesheuvel
ARM intrinsics for AES deviate from the x86 ones in the way they cover the different stages of each round, and so mapping one to the other is not entirely straight-forward. However, with a bit of care, we can still use the x86 ones to emulate the ARM ones, which makes them constant time (which is an important property in crypto) and substantially more efficient.
Quoted 3 lines
Do you have examples of workloads and speedups obtained, by the way?
I don't have any actual numbers to share, unfortunately.
I implemented this when i was experimenting with TPM based measured boot and disk encryption in the guest. I'd say that running an OS under emulation that uses disk encryption would be the most relevant use case here.
Accelerated AES is typically at least an order of magnitude faster than a table based C implementation, and does not stress the D-cache as much (the tables involved are not tiny).