From patchwork Fri Jun 9 02:23:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 690886 Delivered-To: patch@linaro.org Received: by 2002:a5d:4d8a:0:0:0:0:0 with SMTP id b10csp599147wru; Thu, 8 Jun 2023 19:32:02 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6j5l0FHbnrPYM+D/I9wgRf8eIT9uyfdCrGjL0qmDO1m/3AvGDyoEHuwlOOyKprgI8Boioy X-Received: by 2002:a05:620a:2b4b:b0:75b:23a1:3651 with SMTP id dp11-20020a05620a2b4b00b0075b23a13651mr7919949qkb.18.1686277922161; Thu, 08 Jun 2023 19:32:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686277922; cv=none; d=google.com; s=arc-20160816; b=AbmCeRNN/HrlOcIKIdokSdVXnsxsZ6aaR3ohHn3K/ZDJ17AqblhYQUuIIP3Gjax0MB omo5cwRwPzC5m7fVmuhcHAU2/OxGd+I6sxyP83zpv4wLNEyapsRWR1Ih5WUOOkEheLyO w45dPjbNwYv6K1eCpGwhJmwy5XgyP5w3RxnfTlvOmscfwHZiQiIs/CcZHeoD1WBLaSFo z2Pml4N4TYmjgjr/uKW2uZYsXJfXj9rgDMHr593poCpCkvNm5OWSEPEx/kvLOoXVWF5a S3OmtCt/kz542vb4FIzGyqCfNHHdlxfjOHs6M7I8TZw4UTFYBjVXUwel70j6OTvdn4CW Ej/A== 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=pS9H3nW1oUdrmNzz27iCH6iSm9j7i3Aiq4G8rnUmTu4=; b=gEtp7o3MD5DkcVMFWss7m3k9pmAWV4Zhg+VlAiiid+sGjUsRBklgBbspKdC1WZgOmu Fwo99YsLbBJIc8d4LM8EeOY8lImKBSVvBPLQTADTdHrKAAKYEJ5zsUAk+rZodRwvlw9o VTaEG9Ydxkcji30s07Q3ryzQJXDxpUdsiWbaId4ayV2aSqgERLCgcF32PL4dEVfoxPfh TmRcDxbYVFyZS0jHfL91P52Fmi2Dd+GCb6IwzdS+DORu6B6qx5knNUkVU82h2EHGlvOn P4OaGhjz1ZqEflRoSphf0xwPtG3h1PhyvCIg2EqDVqXyCHHIQhtFiSLhN8pwKKVDIp8a UDYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Y2RepjWe; 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 o1-20020a05620a130100b0075d139c8b93si1556643qkj.505.2023.06.08.19.32.02 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 08 Jun 2023 19:32:02 -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=Y2RepjWe; 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 1q7Rp8-00050i-Mi; Thu, 08 Jun 2023 22:25:58 -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 1q7Rnz-0002wT-A0 for qemu-devel@nongnu.org; Thu, 08 Jun 2023 22:24:51 -0400 Received: from mail-ot1-x331.google.com ([2607:f8b0:4864:20::331]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1q7RnZ-0005gE-8S for qemu-devel@nongnu.org; Thu, 08 Jun 2023 22:24:45 -0400 Received: by mail-ot1-x331.google.com with SMTP id 46e09a7af769-6af93a6166fso268741a34.3 for ; Thu, 08 Jun 2023 19:24:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686277459; x=1688869459; 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=pS9H3nW1oUdrmNzz27iCH6iSm9j7i3Aiq4G8rnUmTu4=; b=Y2RepjWegCsrpeqXzgnR+0DHfg1Vwts6vMSgpOaS5z3sRO83kA8RoJMtEPXOqtZwwk CiM9GswB2SlNmWk/ykQ0lcN5tXiKthcobDKzJYuCM8z/xAZRhwmCp7Sg5OG2KQK7tN4q njCMxA0/qnvZNQyKq2bwvreOUJd1JEJ6Fp26/btDwFUpq6Ng+Tv+huXlVA5v7sxQ0N0G 1+znq7Hj56DBZoO3A0gQf0eBqtpLlpi7mleaBOLKSyEozw+kanKnjXMjRlwRTT3ztE0n rxlFfidSPcIRDUsUH95Ht6MD8OEfDzjKkNW0D76dXOTxmnAK5tWoPb3EplaRgD78bQzH qu/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686277459; x=1688869459; 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=pS9H3nW1oUdrmNzz27iCH6iSm9j7i3Aiq4G8rnUmTu4=; b=PYcGGl2sHupPnPXTe9XF4AGNeaa1Poy2mjctu698DbosglONPrHvAGzO+hBUbbnFHm WtQgi1aJlE7oDRht3oz3Bnpc90/otfPI1uyviQ8TfQZcJsFWC0VdN/LaOMbLw2TeS/Q3 DLdx1ppO7vYBFjdGoUULnYNP1kpZoyEkedgRqGk3FJH0hevvX5/yjJ8hqoa1E2JTqcZe sqc1flOptsmRcEpVEt6rT08FBBkPJhepl8O/2psdZ5j7d8fJglvW8jmUK99dyTROy/rP +IjMJFozkGrm0eGEeFmwaQL7HtBWniNCKvJWpjOPKslJ5ptvlqc4rBw4Q0seentVaLwq AODA== X-Gm-Message-State: AC+VfDwfUZ4ChRTcYpq8LZ5S3i5D8OOFbe/qnUe2K120BOFtlUlgIBiR YBPszBdkwqsydSb7tpeLkhOFiS3WC+Qf9V/rpMs= X-Received: by 2002:a05:6830:1483:b0:6b1:5e8f:e508 with SMTP id s3-20020a056830148300b006b15e8fe508mr148822otq.33.1686277458977; Thu, 08 Jun 2023 19:24:18 -0700 (PDT) Received: from stoup.. ([2602:ae:1598:4c01:b071:df63:5761:f449]) by smtp.gmail.com with ESMTPSA id x25-20020a62fb19000000b0063afb08afeesm1686458pfm.67.2023.06.08.19.24.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 19:24:18 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: ardb@kernel.org, berrange@redhat.com, qemu-ppc@nongnu.org, qemu-arm@nongnu.org, qemu-riscv@nongnu.org, pbonzini@redhat.com Subject: [PATCH v2 17/38] crypto: Add aesenc_MC Date: Thu, 8 Jun 2023 19:23:40 -0700 Message-Id: <20230609022401.684157-18-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230609022401.684157-1-richard.henderson@linaro.org> References: <20230609022401.684157-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::331; envelope-from=richard.henderson@linaro.org; helo=mail-ot1-x331.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=unavailable 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. Signed-off-by: Richard Henderson --- host/include/generic/host/aes-round.h | 2 + include/crypto/aes-round.h | 18 ++++++++ crypto/aes.c | 59 +++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) diff --git a/host/include/generic/host/aes-round.h b/host/include/generic/host/aes-round.h index e8f6bb0b99..b00e9b50b1 100644 --- a/host/include/generic/host/aes-round.h +++ b/host/include/generic/host/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 56376cc83b..9f263ca726 100644 --- a/include/crypto/aes-round.h +++ b/include/crypto/aes-round.h @@ -20,6 +20,24 @@ typedef union { #include "host/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. */ diff --git a/crypto/aes.c b/crypto/aes.c index 767930223c..89de8e8db4 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" @@ -1293,6 +1295,63 @@ void aesenc_SB_SR_AK_genrev(AESState *r, const AESState *s, const AESState *k) aesenc_SB_SR_AK_swap(r, s, k, true); } +/* 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 InvSubBytes + InvShiftRows. */ static inline void aesdec_ISB_ISR_AK_swap(AESState *ret, const AESState *st,