From patchwork Fri Jun 9 02:23:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 690867 Delivered-To: patch@linaro.org Received: by 2002:a5d:4d8a:0:0:0:0:0 with SMTP id b10csp598498wru; Thu, 8 Jun 2023 19:29:08 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ71qQ7FRCeazMt1PPbXYptKfrHk7di9uNUZNvi0Pv3rno3YU53p5rXcaHf0McAJZmnzgf9p X-Received: by 2002:a05:620a:2087:b0:75d:4e60:6146 with SMTP id e7-20020a05620a208700b0075d4e606146mr6847602qka.44.1686277748146; Thu, 08 Jun 2023 19:29:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686277748; cv=none; d=google.com; s=arc-20160816; b=jywej7nYkbsArbFnhRAV+BXNANUcOXAsGfY8GnqMFODjVnWkZw9crzXDA47GCXW7te +YJalVKq3F4QsSzvB7l/9GlhL3K64zWxoagxJB15xzDGOoh6qacJDzwcmq/jmyi65v4Z dOgaZMlOCNwgIdPtU02yPN5mr0nv/wlsRpqVR3Axezj6Px577gv0Gcact3rmXtq/PssI +SztccYhv86PwqGnKcWHszTx9eAY5YU/qak3lju1g3YBRGJ8dFkm2wvzjTXVBZ4oDfB/ EOyEJ14spztKtxsH0yLd4fdb4UgmY1L8GyPbMDu3yOEBHyzev5biYbJ5IkU2c9ChzIwe nlIQ== 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=xYxE0S+wgPkv66YYfd/jCMfWWCABG8XxYK8ruJluaP8=; b=RJ/88yraBDSNqnTGrlJyRYH1ybrHmYShuP2RpKhuNcDoqQnQZyjcfwkCzmnmKtMywr vYJae1/mL0+91RY0PID01wCQoJwzb6LIQdEqLqvrW1u4cFm1uEcoBS10ywipiEP/HsFX qq1Zs7zxrZi0ZJwH70Cn6WsBImb3rb3w+3DuyZxYluDyg3fg7/J+WBk6MNFKhWbqy1AR 8fFiccVS0rH1F/QaVaGDx+1SsiB/PdAEPW3uRiE4BP4D1PVXQHTiH1acjpMcn9DUJJgQ qBQ1/jyxtKL1uNs3g2rPAMvfxHZTrScGjNO4QHqXMrLbCREmL/bPDtfmMQMYuUYLcK2Z o2bA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=giaTpN5T; 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 l13-20020ae9f00d000000b0075d4ac5a478si1518134qkg.283.2023.06.08.19.29.07 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 08 Jun 2023 19:29:08 -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=giaTpN5T; 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 1q7Rp6-0004jD-TG; Thu, 08 Jun 2023 22:25:56 -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-0002wU-9w for qemu-devel@nongnu.org; Thu, 08 Jun 2023 22:24:50 -0400 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1q7RnZ-0005h8-SS for qemu-devel@nongnu.org; Thu, 08 Jun 2023 22:24:45 -0400 Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-654f8b56807so1293733b3a.1 for ; Thu, 08 Jun 2023 19:24:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686277460; x=1688869460; 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=xYxE0S+wgPkv66YYfd/jCMfWWCABG8XxYK8ruJluaP8=; b=giaTpN5T3y2SU0V4NKu9QJVob/S3L2ivzaOksdml6CeBC5CUQuPx9Zn6lTURbP9lem tYsZSBO3+iHBsPKVYbEuYEVhgsomUwFr8YO/3/wm1jsao7mH/ZwyyA8JdnVtUV8cG5Hn Txu+j6MAQJlCgtloMq0Mg7XxnNYt/Af7uIaqZzsLhPRCmwF4xO22DSJVqnDT3f3c1sZu f5DXLCxrN6fDDJUqy+9dANNqhgxVOdVBH/a5XUFo4MHjVHxRpptlNDmDCZA+wsJzNk7t y2snoUfXB3vj76lXuWmvihdwtE35a7shgtc9VfBa6BRGdr8RoXKuA2ctLFvnf+vdhBvA EbIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686277460; x=1688869460; 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=xYxE0S+wgPkv66YYfd/jCMfWWCABG8XxYK8ruJluaP8=; b=gKHhiLqzusqlZxQ0wBqLfw/pSj32gOGe/i+3IYpGfJ4rs8mTeWxlSty0XWKJkZcLL+ 6Yf8ycseR6khzBdnhxHF9eQq1u5f8L5wjKG6GHldbBNnHGb/DQfHXPFwd1/as5jKDdD8 7ROquoTGtFk0EmyF4lQBitryOzziw+Oikc29MezzsvYbwrMMJp4LgJZ7m5xBQu6X5VKP f38sHe6VsuRlzr/s5e+ESN7bMO1QIEpsIriiSm9qHF3QLqS1a5NILmSBSlMUQ/4dMt3d aK0lzWC0y7Wy1oHixu8aJEPrqq6MMsN6QP1kwiZoztFM6gH02zp+NmTFUcTUqEYtJkvo mjrA== X-Gm-Message-State: AC+VfDyG0tb7ZsFl3DKFBgSvcVbvOz93pCraF1wv+LdK+/R+794SEfrm jfLoWcahqNQI81jhe7Ith+CWCyEbP/1r9dwh9Hk= X-Received: by 2002:a05:6a00:1a4e:b0:643:8496:e41c with SMTP id h14-20020a056a001a4e00b006438496e41cmr7565pfv.20.1686277460694; Thu, 08 Jun 2023 19:24:20 -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.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 19:24:20 -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 19/38] crypto: Add aesdec_IMC Date: Thu, 8 Jun 2023 19:23:42 -0700 Message-Id: <20230609022401.684157-20-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::42a; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42a.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 InvMixColumns. Signed-off-by: Richard Henderson --- host/include/generic/host/aes-round.h | 2 + include/crypto/aes-round.h | 18 +++++++++ crypto/aes.c | 57 +++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/host/include/generic/host/aes-round.h b/host/include/generic/host/aes-round.h index b00e9b50b1..34068afe40 100644 --- a/host/include/generic/host/aes-round.h +++ b/host/include/generic/host/aes-round.h @@ -15,6 +15,8 @@ void aesenc_SB_SR_AK_accel(AESState *, const AESState *, const AESState *, bool) QEMU_ERROR("unsupported accel"); +void aesdec_IMC_accel(AESState *, const AESState *, bool) + QEMU_ERROR("unsupported accel"); void aesdec_ISB_ISR_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 9f263ca726..b80d4de664 100644 --- a/include/crypto/aes-round.h +++ b/include/crypto/aes-round.h @@ -59,6 +59,24 @@ static inline void aesenc_SB_SR_AK(AESState *r, const AESState *st, } } +/* + * Perform InvMixColumns. + */ + +void aesdec_IMC_gen(AESState *ret, const AESState *st); +void aesdec_IMC_genrev(AESState *ret, const AESState *st); + +static inline void aesdec_IMC(AESState *r, const AESState *st, bool be) +{ + if (HAVE_AES_ACCEL) { + aesdec_IMC_accel(r, st, be); + } else if (HOST_BIG_ENDIAN == be) { + aesdec_IMC_gen(r, st); + } else { + aesdec_IMC_genrev(r, st); + } +} + /* * Perform InvSubBytes + InvShiftRows. */ diff --git a/crypto/aes.c b/crypto/aes.c index 89de8e8db4..bfd41e3fb9 100644 --- a/crypto/aes.c +++ b/crypto/aes.c @@ -1395,6 +1395,63 @@ void aesdec_ISB_ISR_AK_genrev(AESState *r, const AESState *s, const AESState *k) aesdec_ISB_ISR_AK_swap(r, s, k, true); } +/* Perform InvMixColumns. */ +static inline void +aesdec_IMC_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_imc is encoded for big-endian. */ + t = (AES_imc[st->b[swap_b ^ 0x0]][0] ^ + AES_imc[st->b[swap_b ^ 0x1]][1] ^ + AES_imc[st->b[swap_b ^ 0x2]][2] ^ + AES_imc[st->b[swap_b ^ 0x3]][3]); + if (!be) { + t = bswap32(t); + } + r->w[swap_w ^ 0] = t; + + t = (AES_imc[st->b[swap_b ^ 0x4]][0] ^ + AES_imc[st->b[swap_b ^ 0x5]][1] ^ + AES_imc[st->b[swap_b ^ 0x6]][2] ^ + AES_imc[st->b[swap_b ^ 0x7]][3]); + if (!be) { + t = bswap32(t); + } + r->w[swap_w ^ 1] = t; + + t = (AES_imc[st->b[swap_b ^ 0x8]][0] ^ + AES_imc[st->b[swap_b ^ 0x9]][1] ^ + AES_imc[st->b[swap_b ^ 0xA]][2] ^ + AES_imc[st->b[swap_b ^ 0xB]][3]); + if (!be) { + t = bswap32(t); + } + r->w[swap_w ^ 2] = t; + + t = (AES_imc[st->b[swap_b ^ 0xC]][0] ^ + AES_imc[st->b[swap_b ^ 0xD]][1] ^ + AES_imc[st->b[swap_b ^ 0xE]][2] ^ + AES_imc[st->b[swap_b ^ 0xF]][3]); + if (!be) { + t = bswap32(t); + } + r->w[swap_w ^ 3] = t; +} + +void aesdec_IMC_gen(AESState *r, const AESState *st) +{ + aesdec_IMC_swap(r, st, false); +} + +void aesdec_IMC_genrev(AESState *r, const AESState *st) +{ + aesdec_IMC_swap(r, st, true); +} + /** * Expand the cipher key into the encryption key schedule. */