Skip to content

Commit 9ea1700

Browse files
ardbiesheuvelalistair23
authored andcommitted
target/riscv: Use existing lookup tables for MixColumns
The AES MixColumns and InvMixColumns operations are relatively expensive 4x4 matrix multiplications in GF(2^8), which is why C implementations usually rely on precomputed lookup tables rather than performing the calculations on demand. Given that we already carry those tables in QEMU, we can just grab the right value in the implementation of the RISC-V AES32 instructions. Note that the tables in question are permuted according to the respective Sbox, so we can omit the Sbox lookup as well in this case. Cc: Richard Henderson <[email protected]> Cc: Philippe Mathieu-Daudé <[email protected]> Cc: Zewen Ye <[email protected]> Cc: Weiwei Li <[email protected]> Cc: Junqiang Wang <[email protected]> Signed-off-by: Ard Biesheuvel <[email protected]> Reviewed-by: Richard Henderson <[email protected]> Message-ID: <[email protected]> Signed-off-by: Alistair Francis <[email protected]>
1 parent 4cc9f28 commit 9ea1700

File tree

3 files changed

+13
-32
lines changed

3 files changed

+13
-32
lines changed

crypto/aes.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ AES_Td3[x] = Si[x].[09, 0d, 0b, 0e];
272272
AES_Td4[x] = Si[x].[01, 01, 01, 01];
273273
*/
274274

275-
static const uint32_t AES_Te0[256] = {
275+
const uint32_t AES_Te0[256] = {
276276
0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU,
277277
0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U,
278278
0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU,
@@ -607,7 +607,7 @@ static const uint32_t AES_Te4[256] = {
607607
0xb0b0b0b0U, 0x54545454U, 0xbbbbbbbbU, 0x16161616U,
608608
};
609609

610-
static const uint32_t AES_Td0[256] = {
610+
const uint32_t AES_Td0[256] = {
611611
0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U,
612612
0x3bab6bcbU, 0x1f9d45f1U, 0xacfa58abU, 0x4be30393U,
613613
0x2030fa55U, 0xad766df6U, 0x88cc7691U, 0xf5024c25U,

include/crypto/aes.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,11 @@ void AES_decrypt(const unsigned char *in, unsigned char *out,
3030
extern const uint8_t AES_sbox[256];
3131
extern const uint8_t AES_isbox[256];
3232

33+
/*
34+
AES_Te0[x] = S [x].[02, 01, 01, 03];
35+
AES_Td0[x] = Si[x].[0e, 09, 0d, 0b];
36+
*/
37+
38+
extern const uint32_t AES_Te0[256], AES_Td0[256];
39+
3340
#endif

target/riscv/crypto_helper.c

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,53 +25,27 @@
2525
#include "crypto/aes-round.h"
2626
#include "crypto/sm4.h"
2727

28-
#define AES_XTIME(a) \
29-
((a << 1) ^ ((a & 0x80) ? 0x1b : 0))
30-
31-
#define AES_GFMUL(a, b) (( \
32-
(((b) & 0x1) ? (a) : 0) ^ \
33-
(((b) & 0x2) ? AES_XTIME(a) : 0) ^ \
34-
(((b) & 0x4) ? AES_XTIME(AES_XTIME(a)) : 0) ^ \
35-
(((b) & 0x8) ? AES_XTIME(AES_XTIME(AES_XTIME(a))) : 0)) & 0xFF)
36-
37-
static inline uint32_t aes_mixcolumn_byte(uint8_t x, bool fwd)
38-
{
39-
uint32_t u;
40-
41-
if (fwd) {
42-
u = (AES_GFMUL(x, 3) << 24) | (x << 16) | (x << 8) |
43-
(AES_GFMUL(x, 2) << 0);
44-
} else {
45-
u = (AES_GFMUL(x, 0xb) << 24) | (AES_GFMUL(x, 0xd) << 16) |
46-
(AES_GFMUL(x, 0x9) << 8) | (AES_GFMUL(x, 0xe) << 0);
47-
}
48-
return u;
49-
}
50-
5128
#define sext32_xlen(x) (target_ulong)(int32_t)(x)
5229

5330
static inline target_ulong aes32_operation(target_ulong shamt,
5431
target_ulong rs1, target_ulong rs2,
5532
bool enc, bool mix)
5633
{
5734
uint8_t si = rs2 >> shamt;
58-
uint8_t so;
5935
uint32_t mixed;
6036
target_ulong res;
6137

6238
if (enc) {
63-
so = AES_sbox[si];
6439
if (mix) {
65-
mixed = aes_mixcolumn_byte(so, true);
40+
mixed = be32_to_cpu(AES_Te0[si]);
6641
} else {
67-
mixed = so;
42+
mixed = AES_sbox[si];
6843
}
6944
} else {
70-
so = AES_isbox[si];
7145
if (mix) {
72-
mixed = aes_mixcolumn_byte(so, false);
46+
mixed = be32_to_cpu(AES_Td0[si]);
7347
} else {
74-
mixed = so;
48+
mixed = AES_isbox[si];
7549
}
7650
}
7751
mixed = rol32(mixed, shamt);

0 commit comments

Comments
 (0)