From patchwork Sun Jul 9 13:59:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 700840 Delivered-To: patch@linaro.org Received: by 2002:adf:fcc5:0:0:0:0:0 with SMTP id f5csp4742400wrs; Sun, 9 Jul 2023 07:05:03 -0700 (PDT) X-Google-Smtp-Source: APBJJlF8AwSRSetH0UXEuZBNiUwaNlkhs+yrnn++znTAx0zGesKeUenNJ4+stgvxGlGCpGsnMpQk X-Received: by 2002:ac8:580f:0:b0:403:3b39:45a0 with SMTP id g15-20020ac8580f000000b004033b3945a0mr13719412qtg.33.1688911503573; Sun, 09 Jul 2023 07:05:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688911503; cv=none; d=google.com; s=arc-20160816; b=VCM4YRatjH9qt1B1OmP+dypuFVGIJUxCWFCtVMii/jVF6SpVAlRTyNi31L8NT9Z1KH Lee4Z58zyNHXQmmhvmbqb4Q+GDMtA8b/TvY3DbwIuUn2nX0YyR2n9qjOSDoC9IMaL+Rt VQrrZWeXZW9MKUwHl5xlzhr6Nw8U+sITPcSIL9JT3TnMi+YfFEqsiw20OwPFrAl4k7ny 5Teor9nKmDuCl8+Pi63d1bh4jwIxOEEIYGOGze6XRrx5wUjY3/4WfIxyoTOU2j/p5c56 ilDKLWhvOf+HEtiJklEV/+kPvpWugd3WqvjcoE5G5n8EOd4OApGA1cmvXLwF4fjfwI0x s9sQ== 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=UNvrPJBGJx5dGrfGxip7sDDn/+xiNFreEIZcy5LuLWI=; fh=i1mkeM7bD/ppus3C3v+92ltBBCUUU0zBXrpbYjAjHqE=; b=hdLmk9QmtPdVfnvAXeNHPF1uD/dHdawgeKMaXsPpOSQzkb5Obnp5nrLJMcKW+p2Y3S MS9pLaijMuATPRu/zjk8o08keIkChbK04aWSqIT0KkDGXRF6iW2LqV27OEAe+4+KS2F/ N+lM4UuOgpZd9SudXChbWiWbUWKYrlDuL6r+RR9U4LA5nrw1iXlMFb35XdVVC21XW9vJ l72iLhReK/TxbasH/vvK0ih8oCR0eeSX30B2s7mpTUz6ioNWLAvXP5s8gFe9fs68kXQF cg0GkUU1CQMg6otL9eJJVDwmJRoRtDO9UcVZCp+FbP7VLFqX/wplxqDTHq45VJYMEPMd ANjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=zv48CWl8; 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 f2-20020ac87f02000000b0040358f2660csi4658193qtk.71.2023.07.09.07.05.03 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 09 Jul 2023 07:05:03 -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=zv48CWl8; 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 1qIUxe-0000Ur-CU; Sun, 09 Jul 2023 10:00:26 -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 1qIUxZ-0000Th-1w for qemu-devel@nongnu.org; Sun, 09 Jul 2023 10:00:23 -0400 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qIUxW-0000et-5X for qemu-devel@nongnu.org; Sun, 09 Jul 2023 10:00:20 -0400 Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-3142970df44so3380058f8f.3 for ; Sun, 09 Jul 2023 07:00:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1688911215; x=1691503215; 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=UNvrPJBGJx5dGrfGxip7sDDn/+xiNFreEIZcy5LuLWI=; b=zv48CWl8oDi9qBJaFDkRTHWP1ZYVinc4aKnbCxfAgjXd3YQGjqH7EiY988fU07K35/ V00uGoSHuqAn/bI9ykHlgIoEK1dIPWyL1CXp6qIfcMi7hsyo9LuZ8ycwm6XW2X28LU5k IHr+udPai1I0vE9kYENhlu6gSztIMh+QQpicbbjpZW8B7Hl6FTE+kbZaVfVFAAusTpvi dOHpw6t/dArcxcRDQaKOhgWpeKarXBbtH5o4JKE4vVKO4VroOK58OkE01Xa7D+XxOr+Q lVp0iG3u17lw7/mZBQNI6Aka0/c3qjhlA4bpm1zNYzFw1ghGoIGQ1DFBx7+/JECcLOqD dOxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688911215; x=1691503215; 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=UNvrPJBGJx5dGrfGxip7sDDn/+xiNFreEIZcy5LuLWI=; b=iOxvrO4LxZ8Y74raFBcWTbU0vJfPGO4UStcM+6sifvZ0aOBxVOA22z/SRI6+8YYpRB UMHOSpEfDOdYrrf8qsqkptxr0zKqg3aN8fbgw4DlJCgMgMYFVLCTxAA2E7DNDOZAlUsB PufleDC1EdSYKZyIbdpK7RRYuofuyb5/h5JKzpxcCL8Lvz8jBuyKe+aR4FlvLe/O/BII TlvXZrjA0WXfDAxg/kMlc/xOFwZA2l1jY8Wl10ZZ3DSSFllnRmSXeYko073/HhYpFInb awetK0VtJZ7p98dTfxlOeB7gQ9SgptIyllVdxcvfi0c7diXZV+reWB8I+pVVmV4dC2eJ N2xg== X-Gm-Message-State: ABy/qLYUFpVQ26Sy1q+GYdcQ/+l5kRdJjSpV/RppUDvZOlCHo+IrJQaZ t7ppPZNpo0KUzIrSkZQqiU6IV2X+Xi745zhm+/zX/g== X-Received: by 2002:adf:da4e:0:b0:314:315a:93b5 with SMTP id r14-20020adfda4e000000b00314315a93b5mr8091387wrl.54.1688911215446; Sun, 09 Jul 2023 07:00:15 -0700 (PDT) Received: from stoup.. ([148.252.133.210]) by smtp.gmail.com with ESMTPSA id v18-20020a5d6792000000b003063a92bbf5sm9239422wru.70.2023.07.09.07.00.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Jul 2023 07:00:15 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PULL 33/37] target/riscv: Use aesdec_ISB_ISR_IMC_AK Date: Sun, 9 Jul 2023 14:59:41 +0100 Message-Id: <20230709135945.250311-34-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230709135945.250311-1-richard.henderson@linaro.org> References: <20230709135945.250311-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::429; envelope-from=richard.henderson@linaro.org; helo=mail-wr1-x429.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 This implements the AES64DSM instruction. This was the last use of aes64_operation and its support macros, so remove them all. Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Richard Henderson --- target/riscv/crypto_helper.c | 101 ++++------------------------------- 1 file changed, 10 insertions(+), 91 deletions(-) diff --git a/target/riscv/crypto_helper.c b/target/riscv/crypto_helper.c index c036fe8632..99d85a6188 100644 --- a/target/riscv/crypto_helper.c +++ b/target/riscv/crypto_helper.c @@ -104,98 +104,8 @@ target_ulong HELPER(aes32dsi)(target_ulong rs1, target_ulong rs2, return aes32_operation(shamt, rs1, rs2, false, false); } -#define BY(X, I) ((X >> (8 * I)) & 0xFF) - -#define AES_SHIFROWS_LO(RS1, RS2) ( \ - (((RS1 >> 24) & 0xFF) << 56) | (((RS2 >> 48) & 0xFF) << 48) | \ - (((RS2 >> 8) & 0xFF) << 40) | (((RS1 >> 32) & 0xFF) << 32) | \ - (((RS2 >> 56) & 0xFF) << 24) | (((RS2 >> 16) & 0xFF) << 16) | \ - (((RS1 >> 40) & 0xFF) << 8) | (((RS1 >> 0) & 0xFF) << 0)) - -#define AES_INVSHIFROWS_LO(RS1, RS2) ( \ - (((RS2 >> 24) & 0xFF) << 56) | (((RS2 >> 48) & 0xFF) << 48) | \ - (((RS1 >> 8) & 0xFF) << 40) | (((RS1 >> 32) & 0xFF) << 32) | \ - (((RS1 >> 56) & 0xFF) << 24) | (((RS2 >> 16) & 0xFF) << 16) | \ - (((RS2 >> 40) & 0xFF) << 8) | (((RS1 >> 0) & 0xFF) << 0)) - -#define AES_MIXBYTE(COL, B0, B1, B2, B3) ( \ - BY(COL, B3) ^ BY(COL, B2) ^ AES_GFMUL(BY(COL, B1), 3) ^ \ - AES_GFMUL(BY(COL, B0), 2)) - -#define AES_MIXCOLUMN(COL) ( \ - AES_MIXBYTE(COL, 3, 0, 1, 2) << 24 | \ - AES_MIXBYTE(COL, 2, 3, 0, 1) << 16 | \ - AES_MIXBYTE(COL, 1, 2, 3, 0) << 8 | AES_MIXBYTE(COL, 0, 1, 2, 3) << 0) - -#define AES_INVMIXBYTE(COL, B0, B1, B2, B3) ( \ - AES_GFMUL(BY(COL, B3), 0x9) ^ AES_GFMUL(BY(COL, B2), 0xd) ^ \ - AES_GFMUL(BY(COL, B1), 0xb) ^ AES_GFMUL(BY(COL, B0), 0xe)) - -#define AES_INVMIXCOLUMN(COL) ( \ - AES_INVMIXBYTE(COL, 3, 0, 1, 2) << 24 | \ - AES_INVMIXBYTE(COL, 2, 3, 0, 1) << 16 | \ - AES_INVMIXBYTE(COL, 1, 2, 3, 0) << 8 | \ - AES_INVMIXBYTE(COL, 0, 1, 2, 3) << 0) - static const AESState aes_zero = { }; -static inline target_ulong aes64_operation(target_ulong rs1, target_ulong rs2, - bool enc, bool mix) -{ - uint64_t RS1 = rs1; - uint64_t RS2 = rs2; - uint64_t result; - uint64_t temp; - uint32_t col_0; - uint32_t col_1; - - if (enc) { - temp = AES_SHIFROWS_LO(RS1, RS2); - temp = (((uint64_t)AES_sbox[(temp >> 0) & 0xFF] << 0) | - ((uint64_t)AES_sbox[(temp >> 8) & 0xFF] << 8) | - ((uint64_t)AES_sbox[(temp >> 16) & 0xFF] << 16) | - ((uint64_t)AES_sbox[(temp >> 24) & 0xFF] << 24) | - ((uint64_t)AES_sbox[(temp >> 32) & 0xFF] << 32) | - ((uint64_t)AES_sbox[(temp >> 40) & 0xFF] << 40) | - ((uint64_t)AES_sbox[(temp >> 48) & 0xFF] << 48) | - ((uint64_t)AES_sbox[(temp >> 56) & 0xFF] << 56)); - if (mix) { - col_0 = temp & 0xFFFFFFFF; - col_1 = temp >> 32; - - col_0 = AES_MIXCOLUMN(col_0); - col_1 = AES_MIXCOLUMN(col_1); - - result = ((uint64_t)col_1 << 32) | col_0; - } else { - result = temp; - } - } else { - temp = AES_INVSHIFROWS_LO(RS1, RS2); - temp = (((uint64_t)AES_isbox[(temp >> 0) & 0xFF] << 0) | - ((uint64_t)AES_isbox[(temp >> 8) & 0xFF] << 8) | - ((uint64_t)AES_isbox[(temp >> 16) & 0xFF] << 16) | - ((uint64_t)AES_isbox[(temp >> 24) & 0xFF] << 24) | - ((uint64_t)AES_isbox[(temp >> 32) & 0xFF] << 32) | - ((uint64_t)AES_isbox[(temp >> 40) & 0xFF] << 40) | - ((uint64_t)AES_isbox[(temp >> 48) & 0xFF] << 48) | - ((uint64_t)AES_isbox[(temp >> 56) & 0xFF] << 56)); - if (mix) { - col_0 = temp & 0xFFFFFFFF; - col_1 = temp >> 32; - - col_0 = AES_INVMIXCOLUMN(col_0); - col_1 = AES_INVMIXCOLUMN(col_1); - - result = ((uint64_t)col_1 << 32) | col_0; - } else { - result = temp; - } - } - - return result; -} - target_ulong HELPER(aes64esm)(target_ulong rs1, target_ulong rs2) { AESState t; @@ -228,7 +138,16 @@ target_ulong HELPER(aes64ds)(target_ulong rs1, target_ulong rs2) target_ulong HELPER(aes64dsm)(target_ulong rs1, target_ulong rs2) { - return aes64_operation(rs1, rs2, false, true); + AESState t, z = { }; + + /* + * This instruction does not include a round key, + * so supply a zero to our primitive. + */ + t.d[HOST_BIG_ENDIAN] = rs1; + t.d[!HOST_BIG_ENDIAN] = rs2; + aesdec_ISB_ISR_IMC_AK(&t, &t, &z, false); + return t.d[HOST_BIG_ENDIAN]; } target_ulong HELPER(aes64ks2)(target_ulong rs1, target_ulong rs2)