From patchwork Mon Jul 3 10:04:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 698642 Delivered-To: patch@linaro.org Received: by 2002:adf:fcc5:0:0:0:0:0 with SMTP id f5csp1665062wrs; Mon, 3 Jul 2023 03:10:20 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ48+txOi38lqw0oslcDgsoJ7Gc9XwFOI7ft3WFo6K0r8x9e30vmb5GrolrMzsdtcUmRiBvH X-Received: by 2002:ac8:7dcc:0:b0:3f1:f14f:9fd2 with SMTP id c12-20020ac87dcc000000b003f1f14f9fd2mr12157900qte.48.1688379020683; Mon, 03 Jul 2023 03:10:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688379020; cv=none; d=google.com; s=arc-20160816; b=FBWiSKli7M0wCx4nnu1JqM6uQ0IXQO97qem83Im98xqOQsqtmFlTR/5zihY/lfYYaA ZT8K8T7AjGssEjhkIRkZj4/EKK9vBB3UF5+3UnTL3VnrgyGlYpWrsEmSPO3S5CfY5h75 zPS0sBsy1R1hylzgc15gYmuAuP583BgB80WChGAPKIHKVWIT9nZ5NVWvs4Q7R9IUxTYy vrhAouJF0ot+g0etH1bsp0CjtZwOW59Kpvj/dx34jNcb6Nq8P8HMfqqHRz7eIHmpRVwf Uvx607KSVh8+q+P4NKCqyUpTEWHS7kx2J7cKMHYT0ZJGjSXcWnE4rjo3bnsi8QQZt638 CfNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=OqJ8/74xslZqAPWuseMNlVx/dmpNm7/AQh+6yi6QQ/Q=; fh=yGBgCIAdw0xhdK58ttTTG5yX3hLvrsJsvO63zqD4x2E=; b=Vw0zkljlilfDD2Vhx+z+XqRox4kdOk3p7CTMRb5o6TFc04ZzhoGUKOUPLb3u/Q93ng OhkoiddjwalG4Px8y5MqZ657eAr2ooTNPouFxzDnDx0+6Iy4F7B3R6xSfibmapISKDaN ZpEuuxNi/UxKHU7GOjEWNwbw79j5vmlUFmUbD1jVFH5qtNwxOVOS/nLdDJDd6i1K9kkf 5K3sWmeZwJQtzxTfCYbM8ScSoFSrT74Q4dEGxG1XQDxoE8FtsmIfqU0pCh9rKWuLWmxP M99O8ofCI+Wr4jgA+N0IyAIVpSpQuT+lbd4G4izFpLTQUXv2j4RZ/8tReUdJHKbzkQoG N/9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=C8o2rooR; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id w16-20020ac87e90000000b00402a7f5850asi4121106qtj.108.2023.07.03.03.10.20 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 03 Jul 2023 03:10:20 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=C8o2rooR; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qGGRO-0002mO-Dg; Mon, 03 Jul 2023 06:05:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qGGRF-0002ap-EO for qemu-devel@nongnu.org; Mon, 03 Jul 2023 06:05:45 -0400 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qGGR5-0005js-Md for qemu-devel@nongnu.org; Mon, 03 Jul 2023 06:05:40 -0400 Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-3fbc5d5746cso44947485e9.2 for ; Mon, 03 Jul 2023 03:05:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1688378729; x=1690970729; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OqJ8/74xslZqAPWuseMNlVx/dmpNm7/AQh+6yi6QQ/Q=; b=C8o2rooRPqQYMAWpKdzFYgDoGcjTrndOODJvpVXvshPlKOSycu6pVsWJ3hUshe5zNB X3vRUPViH+aYGGNiLrKBvszBjwG4/dBDeoYDwPZ9+VR1+K3DvBeTQEVs2BgybxDlPyeA dW8MyjnctCnOyC4OCq7jhnp3a+O08BbuKy9ma0q/jSydm3TXihqq9o4eVNuCGpWpmRF1 L0joFKggtGGXGIWEhfj1/oMHz3YYIaoFEkYn7Z6JWxWyGVGcHOKIc4BmglTiGiYd3/NE zg2ConM7Q7BSUdaquv3U2oBtltdUof3YOZMqdfmCSJ7qflASPwydkMH5bsj4Wq5KQUEl 1fnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688378729; x=1690970729; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OqJ8/74xslZqAPWuseMNlVx/dmpNm7/AQh+6yi6QQ/Q=; b=Fn/ubFp6O6yZTicSwhSnS6ZZdEoYsGyN7xr24KstssvykGmbfGop031qKDm5JqkGMW AGvjbz0kd6JceVwpUscpz8VS7ak76R8hChVDms+tLuFKizs+9TBWkBgOnbWh1RyQ+Pjl b9oH4bqvzG04FXD8b7GpehAYo8j8/c8vSAx77AXx+WgN74jDgefeCZjrVB7EZbt4LENy sIGSyiC725ADR1da2z17+73e/LG7O+tbd8rWj3dRY44q03NtXWaooSvvfnzRKBKmYDEZ xs2haVUNfzcQS5w7D2PkVHxRNBbp0YNs5zj477qMvNGj7bYPtkPYGvpDZzcykapK6ARv porQ== X-Gm-Message-State: AC+VfDzsR51DToexGD9dR2Rv+8oLK8wZ/Y/MSXQVhXe/zznt6r0kPh0s 7C9P4b/7PMVN/SGTqycBnkqy0HtFKKOzpUzkS4DkIQ== X-Received: by 2002:a1c:6a07:0:b0:3fa:934c:8356 with SMTP id f7-20020a1c6a07000000b003fa934c8356mr9003317wmc.10.1688378729553; Mon, 03 Jul 2023 03:05:29 -0700 (PDT) Received: from stoup.home (91.232.79.188.dynamic.jazztel.es. [188.79.232.91]) by smtp.gmail.com with ESMTPSA id t5-20020a05600001c500b00313f7b077fesm18446562wrx.59.2023.07.03.03.05.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jul 2023 03:05:29 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org, qemu-riscv@nongnu.org, pbonzini@redhat.com, eduardo@habkost.net, alistair.francis@wdc.com, danielhb413@gmail.com, =?utf-8?q?Daniel_P_=2E_Berrang=C3=A9?= Subject: [PATCH v4 07/37] crypto: Add aesenc_MC Date: Mon, 3 Jul 2023 12:04:50 +0200 Message-Id: <20230703100520.68224-8-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230703100520.68224-1-richard.henderson@linaro.org> References: <20230703100520.68224-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::334; envelope-from=richard.henderson@linaro.org; helo=mail-wm1-x334.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Add a primitive for MixColumns. Acked-by: Daniel P. Berrangé Signed-off-by: Richard Henderson --- 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 dcf098b97b..7d2be40a67 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 90274c3706..ec300cda0c 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" @@ -1216,6 +1218,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. */