Skip to contentSkip to conversation

target/riscv: Use accelerated helper for AES64KS1I

PATCH Applied Ard Biesheuvel 4 participants 4 messages Aug 31, 2023 – Sep 1, 2023 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 target/riscv: Use accelerated helper for AES64KS1I

Use the accelerated SubBytes/ShiftRows/AddRoundKey AES helper to implement the first half of the key schedule derivation. This does not actually involve shifting rows, so clone the same value into all four columns of the AES vector to counter that operation.

Cc Richard Henderson <[email protected]>
Cc Philippe Mathieu-Daudé <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Alistair Francis <[email protected]>

---
v2: assign round constant to elements 0 and 1 only

 target/riscv/crypto_helper.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)
target/riscv/crypto_helper.c +5 -12
index 4d65945429c6dcc4..bb084e00efe52d1b 100644
@@ -148,24 +148,17 @@ target_ulong HELPER(aes64ks1i)(target_ulong rs1, target_ulong rnum)
 
     uint8_t enc_rnum = rnum;
     uint32_t temp = (RS1 >> 32) & 0xFFFFFFFF;
-    uint8_t rcon_ = 0;
-    target_ulong result;
+    AESState t, rc = {};
 
     if (enc_rnum != 0xA) {
         temp = ror32(temp, 8); /* Rotate right by 8 */
-        rcon_ = round_consts[enc_rnum];
+        rc.w[0] = rc.w[1] = round_consts[enc_rnum];
     }
 
-    temp = ((uint32_t)AES_sbox[(temp >> 24) & 0xFF] << 24) |
-           ((uint32_t)AES_sbox[(temp >> 16) & 0xFF] << 16) |
-           ((uint32_t)AES_sbox[(temp >> 8) & 0xFF] << 8) |
-           ((uint32_t)AES_sbox[(temp >> 0) & 0xFF] << 0);
+    t.w[0] = t.w[1] = t.w[2] = t.w[3] = temp;
+    aesenc_SB_SR_AK(&t, &t, &rc, false);
 
-    temp ^= rcon_;
-
-    result = ((uint64_t)temp << 32) | temp;
-
-    return result;
+    return t.d[0];
 }
 
 target_ulong HELPER(aes64im)(target_ulong rs1)
Signature · 1 line
2.39.2
Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines · Ard Biesheuvel

Use the accelerated SubBytes/ShiftRows/AddRoundKey AES helper to implement the first half of the key schedule derivation. This does not actually involve shifting rows, so clone the same value into all four columns of the AES vector to counter that operation.

Cc Richard Henderson<[email protected]>
Cc Philippe Mathieu-Daudé<[email protected]>
Cc Palmer Dabbelt<[email protected]>
Cc Alistair Francis<[email protected]>
Quoted 5 lines

---
v2: assign round constant to elements 0 and 1 only

  target/riscv/crypto_helper.c | 17 +++++------------
  1 file changed, 5 insertions(+), 12 deletions(-)

r~

Reviewed-by on v2 1/1
#2 lore raw
In reply to Ard Biesheuvel
Quoted 10 lines · Ard Biesheuvel

Use the accelerated SubBytes/ShiftRows/AddRoundKey AES helper to implement the first half of the key schedule derivation. This does not actually involve shifting rows, so clone the same value into all four columns of the AES vector to counter that operation.

Cc Richard Henderson <[email protected]>
Cc Philippe Mathieu-Daudé <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Alistair Francis <[email protected]>
Quoted 5 lines

---
v2: assign round constant to elements 0 and 1 only

  target/riscv/crypto_helper.c | 17 +++++------------
  1 file changed, 5 insertions(+), 12 deletions(-)
#3 lore raw
In reply to Ard Biesheuvel
Quoted 11 lines · Ard Biesheuvel

Use the accelerated SubBytes/ShiftRows/AddRoundKey AES helper to implement the first half of the key schedule derivation. This does not actually involve shifting rows, so clone the same value into all four columns of the AES vector to counter that operation.

Cc Richard Henderson <[email protected]>
Cc Philippe Mathieu-Daudé <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Alistair Francis <[email protected]>

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 44 lines

---
v2: assign round constant to elements 0 and 1 only

 target/riscv/crypto_helper.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)
target/riscv/crypto_helper.c +5 -13
index 4d65945429c6dcc4..bb084e00efe52d1b 100644
@@ -148,24 +148,17 @@ target_ulong HELPER(aes64ks1i)(target_ulong rs1, target_ulong rnum)
 
     uint8_t enc_rnum = rnum;
     uint32_t temp = (RS1 >> 32) & 0xFFFFFFFF;
-    uint8_t rcon_ = 0;
-    target_ulong result;
+    AESState t, rc = {};
 
     if (enc_rnum != 0xA) {
         temp = ror32(temp, 8); /* Rotate right by 8 */
-        rcon_ = round_consts[enc_rnum];
+        rc.w[0] = rc.w[1] = round_consts[enc_rnum];
     }
 
-    temp = ((uint32_t)AES_sbox[(temp >> 24) & 0xFF] << 24) |
-           ((uint32_t)AES_sbox[(temp >> 16) & 0xFF] << 16) |
-           ((uint32_t)AES_sbox[(temp >> 8) & 0xFF] << 8) |
-           ((uint32_t)AES_sbox[(temp >> 0) & 0xFF] << 0);
+    t.w[0] = t.w[1] = t.w[2] = t.w[3] = temp;
+    aesenc_SB_SR_AK(&t, &t, &rc, false);
 
-    temp ^= rcon_;
-
-    result = ((uint64_t)temp << 32) | temp;
-
-    return result;
+    return t.d[0];
 }
 
 target_ulong HELPER(aes64im)(target_ulong rs1)
--

2.39.2