From patchwork Tue Jun 20 11:07:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 694489 Delivered-To: patch@linaro.org Received: by 2002:adf:e885:0:0:0:0:0 with SMTP id d5csp247437wrm; Tue, 20 Jun 2023 04:17:14 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7m4hYl08ew4smS10vHpYcsfVuFgtCznRCsGqYyB9d2oUTgoiJ7DtwbT20/gIDOuh7rU9lZ X-Received: by 2002:ad4:5d62:0:b0:62f:f23e:14fe with SMTP id fn2-20020ad45d62000000b0062ff23e14femr16883527qvb.20.1687259833886; Tue, 20 Jun 2023 04:17:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687259833; cv=none; d=google.com; s=arc-20160816; b=YHTlc89QZb7bvDAkchX/qglKkpf6d/nGlS62Qc4xeqWnbj+WU1oxSo23b8wMFwa1rJ eTFD7V4Gb+wGOjc8df6oYU0fQGkyiNIDSyUqPxzA+5N5J3SNvxA6DxRtdw8So8MmVxP/ cLUgpD9ZyKDehkDilbFBfzI2UNrkFSRMAL4M1OmBTD4TrXy+mFNapuvBq1qiBj3rL1iZ 54zvN7PR4TY5rfMkfoKVeHNqOm9PuIwKTO6tU8pJSmhEQSgX6S2mJWKm+SFVRSArUC0G JSBATSMa5dXSVYMIeFAYnncrFsjZUleUapINWW4V7fl5cTfkGICj7T1d4tHne2T0NXzj dmIA== 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=RF9gkbqYy40O49MNRxkszLXC6NRN9VopxJ/DiScwWiQ=; b=0TU9r6U3jG5S4OMiuD90qsz7/zZfF45/lJQuif2X29EzPNzlfQp/sc4d7AHKk8Eo2+ MImoOzRKGdZvaOQ3gihx77am7Z/lStljbzXnQWeLegZiZmsUq1Dt4+B3dPCsUkPTmMOR /Vr9hx4ZMfvmNDcdPO2P2nCsBz/Rkx5AcJFK6KpWfKSqc7U99iBUmnjc+aZOEOiW0Z6h opc78DI0zL6AGVE6OBV+IEv1ccm5Lr/Kf9MyernKXGNzSsrW0VaHanq5PDjG211M+Jmm YRVpNH78tTsq59GmCxpChFu4Ax2GltWJ6TYt6zmhHUOZfluyqyX0ypeMdqVXwDZoF7cg gzXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=fBJADhAM; 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 c21-20020ae9e215000000b00763b5beda2csi244646qkc.417.2023.06.20.04.17.13 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 20 Jun 2023 04:17:13 -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=fBJADhAM; 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 1qBZDj-00066n-9N; Tue, 20 Jun 2023 07:08:23 -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 1qBZDh-00064O-Fl for qemu-devel@nongnu.org; Tue, 20 Jun 2023 07:08:21 -0400 Received: from mail-ej1-x62a.google.com ([2a00:1450:4864:20::62a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qBZDe-0004Gw-GK for qemu-devel@nongnu.org; Tue, 20 Jun 2023 07:08:21 -0400 Received: by mail-ej1-x62a.google.com with SMTP id a640c23a62f3a-988a2715b8cso399563666b.0 for ; Tue, 20 Jun 2023 04:08:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1687259297; x=1689851297; 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=RF9gkbqYy40O49MNRxkszLXC6NRN9VopxJ/DiScwWiQ=; b=fBJADhAMiP4QNUtrnVCH9NPEqIkNx70zUzrL8agrNqowYCE1t+VnCOy/RoB1vJy5MC GKNxChM9un0ng7T6++w9/91t2G6s0sQaT/TkjFPGkWClTgCrEoOs4xg7OkTBTQMXpXCG xB5C0EAWBmXzsdIzTcVYVmm+iC4WIxMXP9HH0EvIp1b9EK6CsfsZC/rTAYUV57ri+LxT c7t5AlBW269ic3jylj8g76k2c8FKY29ur6q3B/PWMfS6XCgjhE6t0vqyG2Yz6oVxYSxK alJvKlFHcSdy4SnJXIB/YbcRagbAcC+tDih90Ax07C2pxwgsEDqbRZVE8iJmQNdoHRCy vLHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687259297; x=1689851297; 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=RF9gkbqYy40O49MNRxkszLXC6NRN9VopxJ/DiScwWiQ=; b=O/YhLFk+wZYSTL7K0gYpIToYj0PcH6K63uQnpNn7OG/vOV2frbyJQ7In/AXbZEHKMc YyOv7tFxhkvN+JHlkCrVBSadD8PmVUmemFUoy6+ZoMU/9g7oko5Tiy/GQ3f2gGztMemO NGX4oyePVF1IMoXsFgWrJ853X9+JFCDa/YD1jm4pqiJr+/dKwwBxpJnYke9j1hUwQRV6 Y9f/0SguBO8I564y2QOnH6ERptImbZYE74lsQhp8LSnKnfxnqvhT+4loziJRj3zo/+Zy gnwWul29UPY08w/L/hoSXcbOl1C5qsWlqYdf0mFHoRvV4hPAXiI3o69dL/4J0eR+3LZL Eejw== X-Gm-Message-State: AC+VfDx0JAoBg2zeu5G6uhVePgXx6jTMkMJ98oPCImCggS6o281+G3+X M8DcZLG1YWpVPlz82AcR90RY0uNVLiS738yMJH2S8pwm X-Received: by 2002:a17:907:7290:b0:988:e6dc:bfae with SMTP id dt16-20020a170907729000b00988e6dcbfaemr3300776ejc.24.1687259297234; Tue, 20 Jun 2023 04:08:17 -0700 (PDT) Received: from stoup.lan ([176.176.183.29]) by smtp.gmail.com with ESMTPSA id x17-20020a170906711100b009884f015a44sm1170687ejj.49.2023.06.20.04.08.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jun 2023 04:08:17 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: berrange@redhat.com, qemu-ppc@nongnu.org, qemu-arm@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH v3 11/37] crypto: Add aesdec_ISB_ISR_AK Date: Tue, 20 Jun 2023 13:07:32 +0200 Message-Id: <20230620110758.787479-12-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230620110758.787479-1-richard.henderson@linaro.org> References: <20230620110758.787479-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::62a; envelope-from=richard.henderson@linaro.org; helo=mail-ej1-x62a.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 InvSubBytes + InvShiftRows + AddRoundKey. Signed-off-by: Richard Henderson Reviewed-by: Daniel P. Berrangé --- host/include/generic/host/crypto/aes-round.h | 4 ++ include/crypto/aes-round.h | 21 +++++++++ crypto/aes.c | 45 ++++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/host/include/generic/host/crypto/aes-round.h b/host/include/generic/host/crypto/aes-round.h index c5d8066179..c9b9d732f0 100644 --- a/host/include/generic/host/crypto/aes-round.h +++ b/host/include/generic/host/crypto/aes-round.h @@ -13,4 +13,8 @@ void aesenc_SB_SR_AK_accel(AESState *, const AESState *, const AESState *, bool) QEMU_ERROR("unsupported accel"); +void aesdec_ISB_ISR_AK_accel(AESState *, const AESState *, + const AESState *, bool) + QEMU_ERROR("unsupported accel"); + #endif /* GENERIC_HOST_CRYPTO_AES_ROUND_H */ diff --git a/include/crypto/aes-round.h b/include/crypto/aes-round.h index d675d2468f..f917339104 100644 --- a/include/crypto/aes-round.h +++ b/include/crypto/aes-round.h @@ -41,4 +41,25 @@ static inline void aesenc_SB_SR_AK(AESState *r, const AESState *st, } } +/* + * Perform InvSubBytes + InvShiftRows + AddRoundKey. + */ + +void aesdec_ISB_ISR_AK_gen(AESState *ret, const AESState *st, + const AESState *rk); +void aesdec_ISB_ISR_AK_genrev(AESState *ret, const AESState *st, + const AESState *rk); + +static inline void aesdec_ISB_ISR_AK(AESState *r, const AESState *st, + const AESState *rk, bool be) +{ + if (HAVE_AES_ACCEL) { + aesdec_ISB_ISR_AK_accel(r, st, rk, be); + } else if (HOST_BIG_ENDIAN == be) { + aesdec_ISB_ISR_AK_gen(r, st, rk); + } else { + aesdec_ISB_ISR_AK_genrev(r, st, rk); + } +} + #endif /* CRYPTO_AES_ROUND_H */ diff --git a/crypto/aes.c b/crypto/aes.c index c00f10bad1..3d24781858 100644 --- a/crypto/aes.c +++ b/crypto/aes.c @@ -1295,6 +1295,51 @@ void aesenc_SB_SR_AK_genrev(AESState *r, const AESState *s, const AESState *k) aesenc_SB_SR_AK_swap(r, s, k, true); } +/* + * Perform InvSubBytes + InvShiftRows + AddRoundKey. + */ +static inline void +aesdec_ISB_ISR_AK_swap(AESState *ret, const AESState *st, + const AESState *rk, bool swap) +{ + const int swap_b = swap ? 15 : 0; + AESState t; + + t.b[swap_b ^ 0x0] = AES_isbox[st->b[swap_b ^ AES_ISH_0]]; + t.b[swap_b ^ 0x1] = AES_isbox[st->b[swap_b ^ AES_ISH_1]]; + t.b[swap_b ^ 0x2] = AES_isbox[st->b[swap_b ^ AES_ISH_2]]; + t.b[swap_b ^ 0x3] = AES_isbox[st->b[swap_b ^ AES_ISH_3]]; + t.b[swap_b ^ 0x4] = AES_isbox[st->b[swap_b ^ AES_ISH_4]]; + t.b[swap_b ^ 0x5] = AES_isbox[st->b[swap_b ^ AES_ISH_5]]; + t.b[swap_b ^ 0x6] = AES_isbox[st->b[swap_b ^ AES_ISH_6]]; + t.b[swap_b ^ 0x7] = AES_isbox[st->b[swap_b ^ AES_ISH_7]]; + t.b[swap_b ^ 0x8] = AES_isbox[st->b[swap_b ^ AES_ISH_8]]; + t.b[swap_b ^ 0x9] = AES_isbox[st->b[swap_b ^ AES_ISH_9]]; + t.b[swap_b ^ 0xa] = AES_isbox[st->b[swap_b ^ AES_ISH_A]]; + t.b[swap_b ^ 0xb] = AES_isbox[st->b[swap_b ^ AES_ISH_B]]; + t.b[swap_b ^ 0xc] = AES_isbox[st->b[swap_b ^ AES_ISH_C]]; + t.b[swap_b ^ 0xd] = AES_isbox[st->b[swap_b ^ AES_ISH_D]]; + t.b[swap_b ^ 0xe] = AES_isbox[st->b[swap_b ^ AES_ISH_E]]; + t.b[swap_b ^ 0xf] = AES_isbox[st->b[swap_b ^ AES_ISH_F]]; + + /* + * Perform the AddRoundKey with generic vectors. + * This may be expanded to either host integer or host vector code. + * The key and output endianness match, so no bswap required. + */ + ret->v = t.v ^ rk->v; +} + +void aesdec_ISB_ISR_AK_gen(AESState *r, const AESState *s, const AESState *k) +{ + aesdec_ISB_ISR_AK_swap(r, s, k, false); +} + +void aesdec_ISB_ISR_AK_genrev(AESState *r, const AESState *s, const AESState *k) +{ + aesdec_ISB_ISR_AK_swap(r, s, k, true); +} + /** * Expand the cipher key into the encryption key schedule. */