From: Richard Henderson <richard.henderson@linaro.org>
To: qemu-devel@nongnu.org
Cc: berrange@redhat.com, qemu-ppc@nongnu.org, qemu-arm@nongnu.org,
qemu-riscv@nongnu.org
Subject: [PATCH v3 16/37] crypto: Add aesenc_MC
Date: Tue, 20 Jun 2023 13:07:37 +0200 [thread overview]
Message-ID: <20230620110758.787479-17-richard.henderson@linaro.org> (raw)
In-Reply-To: <20230620110758.787479-1-richard.henderson@linaro.org>
Add a primitive for MixColumns.
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
host/include/generic/host/crypto/aes-round.h | 2 +
include/crypto/aes-round.h | 18 ++++++
crypto/aes.c | 61 ++++++++++++++++++++
3 files changed, 81 insertions(+)
diff --git a/host/include/generic/host/crypto/aes-round.h b/host/include/generic/host/crypto/aes-round.h
index c9b9d732f0..1b82afc629 100644
--- a/host/include/generic/host/crypto/aes-round.h
+++ b/host/include/generic/host/crypto/aes-round.h
@@ -9,6 +9,8 @@
#define HAVE_AES_ACCEL false
#define ATTR_AES_ACCEL
+void aesenc_MC_accel(AESState *, const AESState *, bool)
+ QEMU_ERROR("unsupported accel");
void aesenc_SB_SR_AK_accel(AESState *, const AESState *,
const AESState *, bool)
QEMU_ERROR("unsupported accel");
diff --git a/include/crypto/aes-round.h b/include/crypto/aes-round.h
index f917339104..df252ac76d 100644
--- a/include/crypto/aes-round.h
+++ b/include/crypto/aes-round.h
@@ -20,6 +20,24 @@ typedef union {
#include "host/crypto/aes-round.h"
+/*
+ * Perform MixColumns.
+ */
+
+void aesenc_MC_gen(AESState *ret, const AESState *st);
+void aesenc_MC_genrev(AESState *ret, const AESState *st);
+
+static inline void aesenc_MC(AESState *r, const AESState *st, bool be)
+{
+ if (HAVE_AES_ACCEL) {
+ aesenc_MC_accel(r, st, be);
+ } else if (HOST_BIG_ENDIAN == be) {
+ aesenc_MC_gen(r, st);
+ } else {
+ aesenc_MC_genrev(r, st);
+ }
+}
+
/*
* Perform SubBytes + ShiftRows + AddRoundKey.
*/
diff --git a/crypto/aes.c b/crypto/aes.c
index 3d24781858..d1ef4c19c2 100644
--- a/crypto/aes.c
+++ b/crypto/aes.c
@@ -28,6 +28,8 @@
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "qemu/osdep.h"
+#include "qemu/bswap.h"
+#include "qemu/bitops.h"
#include "crypto/aes.h"
#include "crypto/aes-round.h"
@@ -1250,6 +1252,65 @@ static const u32 rcon[] = {
0x1B000000, 0x36000000, /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */
};
+/*
+ * Perform MixColumns.
+ */
+static inline void
+aesenc_MC_swap(AESState *r, const AESState *st, bool swap)
+{
+ int swap_b = swap * 0xf;
+ int swap_w = swap * 0x3;
+ bool be = HOST_BIG_ENDIAN ^ swap;
+ uint32_t t;
+
+ /* Note that AES_mc_rot is encoded for little-endian. */
+ t = ( AES_mc_rot[st->b[swap_b ^ 0x0]] ^
+ rol32(AES_mc_rot[st->b[swap_b ^ 0x1]], 8) ^
+ rol32(AES_mc_rot[st->b[swap_b ^ 0x2]], 16) ^
+ rol32(AES_mc_rot[st->b[swap_b ^ 0x3]], 24));
+ if (be) {
+ t = bswap32(t);
+ }
+ r->w[swap_w ^ 0] = t;
+
+ t = ( AES_mc_rot[st->b[swap_b ^ 0x4]] ^
+ rol32(AES_mc_rot[st->b[swap_b ^ 0x5]], 8) ^
+ rol32(AES_mc_rot[st->b[swap_b ^ 0x6]], 16) ^
+ rol32(AES_mc_rot[st->b[swap_b ^ 0x7]], 24));
+ if (be) {
+ t = bswap32(t);
+ }
+ r->w[swap_w ^ 1] = t;
+
+ t = ( AES_mc_rot[st->b[swap_b ^ 0x8]] ^
+ rol32(AES_mc_rot[st->b[swap_b ^ 0x9]], 8) ^
+ rol32(AES_mc_rot[st->b[swap_b ^ 0xA]], 16) ^
+ rol32(AES_mc_rot[st->b[swap_b ^ 0xB]], 24));
+ if (be) {
+ t = bswap32(t);
+ }
+ r->w[swap_w ^ 2] = t;
+
+ t = ( AES_mc_rot[st->b[swap_b ^ 0xC]] ^
+ rol32(AES_mc_rot[st->b[swap_b ^ 0xD]], 8) ^
+ rol32(AES_mc_rot[st->b[swap_b ^ 0xE]], 16) ^
+ rol32(AES_mc_rot[st->b[swap_b ^ 0xF]], 24));
+ if (be) {
+ t = bswap32(t);
+ }
+ r->w[swap_w ^ 3] = t;
+}
+
+void aesenc_MC_gen(AESState *r, const AESState *st)
+{
+ aesenc_MC_swap(r, st, false);
+}
+
+void aesenc_MC_genrev(AESState *r, const AESState *st)
+{
+ aesenc_MC_swap(r, st, true);
+}
+
/*
* Perform SubBytes + ShiftRows + AddRoundKey.
*/
--
2.34.1
next prev parent reply other threads:[~2023-06-20 11:16 UTC|newest]
Thread overview: 52+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-20 11:07 [PATCH v3 00/37] crypto: Provide aes-round.h and host accel Richard Henderson
2023-06-20 11:07 ` [PATCH v3 01/37] util: Add cpuinfo-ppc.c Richard Henderson
2023-06-20 11:07 ` [PATCH v3 02/37] tests/multiarch: Add test-aes Richard Henderson
2023-06-20 11:07 ` [PATCH v3 03/37] target/arm: Move aesmc and aesimc tables to crypto/aes.c Richard Henderson
2023-06-20 11:07 ` [PATCH v3 04/37] crypto/aes: Add constants for ShiftRows, InvShiftRows Richard Henderson
2023-06-20 11:07 ` [PATCH v3 05/37] crypto: Add aesenc_SB_SR_AK Richard Henderson
2023-06-21 16:38 ` Daniel P. Berrangé
2023-06-29 15:45 ` Max Chou
2023-06-29 16:02 ` Richard Henderson
2023-06-20 11:07 ` [PATCH v3 06/37] target/i386: Use aesenc_SB_SR_AK Richard Henderson
2023-06-20 11:07 ` [PATCH v3 07/37] target/arm: Demultiplex AESE and AESMC Richard Henderson
2023-06-20 11:07 ` [PATCH v3 08/37] target/arm: Use aesenc_SB_SR_AK Richard Henderson
2023-06-20 11:07 ` [PATCH v3 09/37] target/ppc: " Richard Henderson
2023-06-20 11:07 ` [PATCH v3 10/37] target/riscv: " Richard Henderson
2023-06-20 11:07 ` [PATCH v3 11/37] crypto: Add aesdec_ISB_ISR_AK Richard Henderson
2023-06-21 16:38 ` Daniel P. Berrangé
2023-06-20 11:07 ` [PATCH v3 12/37] target/i386: Use aesdec_ISB_ISR_AK Richard Henderson
2023-06-20 11:07 ` [PATCH v3 13/37] target/arm: " Richard Henderson
2023-06-20 11:07 ` [PATCH v3 14/37] target/ppc: " Richard Henderson
2023-06-20 11:07 ` [PATCH v3 15/37] target/riscv: " Richard Henderson
2023-06-20 11:07 ` Richard Henderson [this message]
2023-06-21 16:39 ` [PATCH v3 16/37] crypto: Add aesenc_MC Daniel P. Berrangé
2023-06-20 11:07 ` [PATCH v3 17/37] target/arm: Use aesenc_MC Richard Henderson
2023-06-20 11:07 ` [PATCH v3 18/37] crypto: Add aesdec_IMC Richard Henderson
2023-06-21 16:40 ` Daniel P. Berrangé
2023-06-20 11:07 ` [PATCH v3 19/37] target/i386: Use aesdec_IMC Richard Henderson
2023-06-20 11:07 ` [PATCH v3 20/37] target/arm: " Richard Henderson
2023-06-20 11:07 ` [PATCH v3 21/37] target/riscv: " Richard Henderson
2023-06-20 11:07 ` [PATCH v3 22/37] crypto: Add aesenc_SB_SR_MC_AK Richard Henderson
2023-06-21 16:40 ` Daniel P. Berrangé
2023-06-20 11:07 ` [PATCH v3 23/37] target/i386: Use aesenc_SB_SR_MC_AK Richard Henderson
2023-06-20 11:07 ` [PATCH v3 24/37] target/ppc: " Richard Henderson
2023-06-20 11:07 ` [PATCH v3 25/37] target/riscv: " Richard Henderson
2023-06-20 11:07 ` [PATCH v3 26/37] crypto: Add aesdec_ISB_ISR_IMC_AK Richard Henderson
2023-06-21 16:40 ` Daniel P. Berrangé
2023-06-20 11:07 ` [PATCH v3 27/37] target/i386: Use aesdec_ISB_ISR_IMC_AK Richard Henderson
2023-06-20 11:07 ` [PATCH v3 28/37] target/riscv: " Richard Henderson
2023-06-20 11:07 ` [PATCH v3 29/37] crypto: Add aesdec_ISB_ISR_AK_IMC Richard Henderson
2023-06-21 16:41 ` Daniel P. Berrangé
2023-06-20 11:07 ` [PATCH v3 30/37] target/ppc: Use aesdec_ISB_ISR_AK_IMC Richard Henderson
2023-06-20 11:07 ` [PATCH v3 31/37] crypto: Remove AES_shifts, AES_ishifts Richard Henderson
2023-06-21 16:41 ` Daniel P. Berrangé
2023-06-20 11:07 ` [PATCH v3 32/37] crypto: Implement aesdec_IMC with AES_imc_rot Richard Henderson
2023-06-21 16:42 ` Daniel P. Berrangé
2023-06-20 11:07 ` [PATCH v3 33/37] crypto: Remove AES_imc Richard Henderson
2023-06-21 16:42 ` Daniel P. Berrangé
2023-06-20 11:07 ` [PATCH v3 34/37] crypto: Unexport AES_*_rot, AES_TeN, AES_TdN Richard Henderson
2023-06-21 16:42 ` Daniel P. Berrangé
2023-06-20 11:07 ` [PATCH v3 35/37] host/include/i386: Implement aes-round.h Richard Henderson
2023-06-20 11:07 ` [PATCH v3 36/37] host/include/aarch64: " Richard Henderson
2023-06-20 11:07 ` [PATCH v3 37/37] host/include/ppc: " Richard Henderson
2023-06-22 4:52 ` [PATCH v3 00/37] crypto: Provide aes-round.h and host accel Richard Henderson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230620110758.787479-17-richard.henderson@linaro.org \
--to=richard.henderson@linaro.org \
--cc=berrange@redhat.com \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-ppc@nongnu.org \
--cc=qemu-riscv@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).