From patchwork Sun Jul 9 13:59:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 700833 Delivered-To: patch@linaro.org Received: by 2002:adf:fcc5:0:0:0:0:0 with SMTP id f5csp4741863wrs; Sun, 9 Jul 2023 07:03:58 -0700 (PDT) X-Google-Smtp-Source: APBJJlGuq/AAz/HsTtDHRuMnB66uaPaIAD6lN1S40/C4VEeADZbCG7faevcGSeqFfmZkrAnWLxqJ X-Received: by 2002:a0c:db8d:0:b0:634:7c34:6c6f with SMTP id m13-20020a0cdb8d000000b006347c346c6fmr9880692qvk.13.1688911438492; Sun, 09 Jul 2023 07:03:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688911438; cv=none; d=google.com; s=arc-20160816; b=klmWjhA/sdefuYtM/WBM1WNQskwPMeij1gyEyTIA4+nbEj+MsNxRWsZsvagWwyKCW5 M3IMfHhrg6cMYx/eoPQqob2B9oi5+T/19Z6TgiY8SyVauNCnMRwkqJJeTkt3X05HjQN4 t0DBWJMURX6AFoJcvTAK6ZIe74/Aacc7LJAP7sNcIDQh6Re3K2FL7r4u7N0bXfJuPJKX a0+IGI8KcPdU4T+Zkt7DCkS5XTB5dL53aMQiUYRLlprRAKWOpoZJFokv4Y3Fql9tejuv 1pdM2sB9vwVIGhKE7bW8xwUBBoGN4yvKV3u9C8X6BH6/fCf7PrGOYGlvX59PwJyX3rWm 8yfg== 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=NDeJ2wqT+fijtWONfLVnciqd4z0YwE/p9OnFcWQIM00=; b=ytS/IroIQr59511eueClTS/BR2z2tgO7rkxpBvMwVLshCN++t9vAS9efpyaA5cyOkW UKX5VahMsQA97Jfa51H/nVYYVmc3Pw6swY98tzyr9VV8wbMhc2MSwe295ElzBg5BKyId EleWK35P5o6whcq8IidKCmIV9rMjGjB/B4btiWrKKQ2HW6hQZHj/5DhtEwOdhAgPB1nV ZktDd3D5BUwLl8MNh3lhhU5sFOa1O5Zi09yJQlo7CQ0U7bcl0L3YvFwSzoy9UyEDIdJd i3V+fLcJ58N8U0jJsYvFXqrVr2EJsR7TN2ngeyT9kkYmFjDbuKwbSW8DNQSP0ek3Qm5F fNYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Chqmz1iD; 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 f8-20020a05620a280800b00767cd8d1659si708041qkp.548.2023.07.09.07.03.58 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 09 Jul 2023 07:03:58 -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=Chqmz1iD; 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 1qIUxW-0000T2-9X; Sun, 09 Jul 2023 10:00:18 -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 1qIUxA-0000KG-V6 for qemu-devel@nongnu.org; Sun, 09 Jul 2023 09:59:56 -0400 Received: from mail-wr1-x435.google.com ([2a00:1450:4864:20::435]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qIUx9-0000My-1Y for qemu-devel@nongnu.org; Sun, 09 Jul 2023 09:59:56 -0400 Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-3090d3e9c92so4314124f8f.2 for ; Sun, 09 Jul 2023 06:59:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1688911193; x=1691503193; 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=Chqmz1iDwCeT7ZJzh2IEpMbgA7A/F+hYIW30CVw96oIG/EsovgwrYLGJOf3l+4Ff6G oItmokdDqSg6ZlwMAlCJtxjXlrgVDXc9GbfNdyDbuoL3WMTp2agv+mbYr7EnM4qWwqCH Ojbz9NOo7F44b8ZvriJb7jBuas3gdBgvzL/AhGToWXHljcFWUUR4D9qHIB0LINMBAsfX VGPGXKx6WcAffLIcpPkLVMOQZ5me9t5JZ9V8LdixqDTJgcGFjtBXmrUBdkglQVtCpPCE uBPRymTacaS0IFSzsiHOPDFcJt1Jxa6Eo4tRx8Wt+92MN7G8kqSBNR/kVm2RtTigYOaE SHpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688911193; x=1691503193; 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=Y8aRSfEiaiOaqY8a28YP3Dd5/PF8GEjI3tuYW9xoOyoyrTR910vUskerNcvI0gPAtH SVpZF+c9n+d5vv/7SwWizPBIkdyqtTTcWC619rnSTq2KqkjkDqnx2uYiYBYk9RjwBKP+ 6htNzhlNhtOBlCHTEK3naaBPRCNKAn7OXciFHdfTQelSUCFEAz5oLfjugAAsyHjTqDP9 PcWVwUL1pDPaEsbngbit8wTgMsGCYYXHAfmlBoE6p5HEyTZ+9mOwoskZ7c5fIeaHELSV FnWj5XRvgTxRyzKSxMsOGoh30/C5d2BHAItVAUMpTscAliMCEvF5Bs+uPOjgGCj0ceCM YBog== X-Gm-Message-State: ABy/qLYRAYpXCh6ZA/fbtiqFKz6elcYAz+lJz3hMc85bVaF1bbrI7IYx KmhyBrYOulbT69n4vDvqaf9VSM3brveGpsKjRS+KxA== X-Received: by 2002:adf:e844:0:b0:314:3b1f:8ea2 with SMTP id d4-20020adfe844000000b003143b1f8ea2mr11057948wrn.6.1688911193462; Sun, 09 Jul 2023 06:59:53 -0700 (PDT) Received: from stoup.. ([148.252.133.210]) by smtp.gmail.com with ESMTPSA id v18-20020a5d6792000000b003063a92bbf5sm9239422wru.70.2023.07.09.06.59.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Jul 2023 06:59:53 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?utf-8?q?Daniel_P_=2E_Berrang=C3=A9?= Subject: [PULL 07/37] crypto: Add aesenc_MC Date: Sun, 9 Jul 2023 14:59:15 +0100 Message-Id: <20230709135945.250311-8-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::435; envelope-from=richard.henderson@linaro.org; helo=mail-wr1-x435.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. */