From patchwork Sun Jul 9 18:28:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 700853 Delivered-To: patch@linaro.org Received: by 2002:adf:fcc5:0:0:0:0:0 with SMTP id f5csp4827112wrs; Sun, 9 Jul 2023 11:31:09 -0700 (PDT) X-Google-Smtp-Source: APBJJlFMtR9082ZXHMbbdpH0ao90Pstu9cNQ4pYbAO0X9LvU3Q4+5EdjaKmIe1J9UbOJeVQdd5Be X-Received: by 2002:a05:620a:29c1:b0:765:ad54:d934 with SMTP id s1-20020a05620a29c100b00765ad54d934mr10939414qkp.2.1688927469073; Sun, 09 Jul 2023 11:31:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688927469; cv=none; d=google.com; s=arc-20160816; b=FjRtQnZruNaZkePXMWHROYKnO5B72WhSoOJwjMCJR05m9b0l1b3KDQN55y5pO3Pv6G YjUelO4M/26vkhYvQsrTSi01OxILQiRgA0frVX8Rgwje6AyaTk1TFx9mAS4I6UDNqq3F yFsJupxkqDNbbdoK+2IHziETd2F2R1E0ImpON3Xa1Ya0yMlZrD32WbwE2zdweEDE+euT F2mG7WmoS5jAmYuJiR3anOo/P+2wU/aomZ7wDFeU2vWKkywrbjGH5uhIkXFd6pBvLnWx fiL5GVHBO+ULcllDLA2pw4w7tdvUM/D81Lkd7NiEQwLsDNCsYbIG9LuDW9n28S/eP+t/ YFxQ== 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=1lNVmzIrZ20vaRM4z3bBKO9neuQ8i1reH4ftvygdvPI=; fh=5s19N4v2JlXpEkFgkc3sswK3fL8V2plB1o7R02nrQbs=; b=TqyXcdu6/5yM44UwoG8r1XGk3qAfhGr+OLZkTQu9RBpHds1v7mjQpW0Ogi2kMjh2TA qlFiMiw6q8aortjagB2RWX+OKalORYpYhqHHzTQnU4i82wotQ3DXhMJDBtl/Zwi5QVYi M5VwNBR5le6AzsmW5aKO3sJcZ0WvV9v+HzGAIwJHW9wEZXjKDpsZAy/lcYEkiU2EEo+n 9vzg8t0ZCmHDv3Pt0Ch+v0p6ajBYIhUn772A47WFEgxS0bTc230bG5DCKOx0GRrQp7nO H2eIdGleaOwbzzOUNRu6Yl42jvECxsPzFJkI7UQ3MKlsnp/38asYRr+i5h3vvS/RDUSe xEqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=hwKhR2u4; 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 h22-20020ae9ec16000000b00767162395fdsi4970107qkg.635.2023.07.09.11.31.08 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 09 Jul 2023 11:31:09 -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=hwKhR2u4; 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 1qIZAU-0008JI-L4; Sun, 09 Jul 2023 14:29: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 1qIZAO-0008FD-Cf for qemu-devel@nongnu.org; Sun, 09 Jul 2023 14:29:52 -0400 Received: from mail-wr1-x42a.google.com ([2a00:1450: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 1qIZAM-0000GJ-PB for qemu-devel@nongnu.org; Sun, 09 Jul 2023 14:29:52 -0400 Received: by mail-wr1-x42a.google.com with SMTP id ffacd0b85a97d-313fb7f0f80so3703647f8f.2 for ; Sun, 09 Jul 2023 11:29:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1688927389; x=1691519389; 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=1lNVmzIrZ20vaRM4z3bBKO9neuQ8i1reH4ftvygdvPI=; b=hwKhR2u4ZPO+LHJAJwZIEUHdQcXLyQZCxBfL5arSBucmL5FRyxbLxE8zZrU/xXuSpR 7Woiyhizdydk8HVElaquorPk7tAoOyERKEwIzaGXLP/HEXbzA2GoqbIECRIppG5AY1Lt BF5g+Z+gX/B1QNqHGBW4Z3RcDKOX2uni3BlzPxL0rKziybtjIjInruP1PGyq3GL3G+uA uYE4s0J+yx15y37a63NmbedeuiKYOXz8YJU7JDJXcBNegvjuf/tMqWfLp4JlZuxa1eM7 GWWVQi2uzvcQ7qAW1JFVUAIftPGGfQHv0eE9CygKystNLXVkVCOL8JoxR7zqeBcMxqX/ v7vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688927389; x=1691519389; 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=1lNVmzIrZ20vaRM4z3bBKO9neuQ8i1reH4ftvygdvPI=; b=dcJapRDLqhx06S/DWMPzUFnkkvkbp0JrL7W2SVVDMLYINW4GCoDUGjsYJuRC0G9Rl5 rFGqDtnMcZDiG7VVinHqg7QaqTDPiYXl5ZBgi8Uw7RRwovVcOPuBKlINeoUnWAK+laLU da1xXSgGzSQLWgJmP1aLEUMUXL7r6QaTDhGWDiv6iwwIBBfIfoupSAG50izNS+QZ/8my nGv5lY3iT0/YNAXR9G3quowQ73b31akAK8R0gIz8qsr4ZGlpSgVmLTvSxIC4RjlcMNRS QLTwFaWT6/LD5ACIJBKwz9oa+9AOzlmSVzEipp15AQBjUAsHObpJub5IJZMxnQLc4kZ4 27lw== X-Gm-Message-State: ABy/qLYv2aIA4BCD7CiLj36/xIUHj5ZJ7fDWjHRd7kTgQBu0SGVrlsvo AsLR3Y5ZhLOvkIIVyaA72A3/ioloFEaHjdKHAA6x0w== X-Received: by 2002:adf:eb09:0:b0:314:4914:3421 with SMTP id s9-20020adfeb09000000b0031449143421mr8296836wrn.12.1688927389094; Sun, 09 Jul 2023 11:29:49 -0700 (PDT) Received: from stoup.. ([148.252.133.210]) by smtp.gmail.com with ESMTPSA id q15-20020a7bce8f000000b003fbe791a0e8sm8317108wmj.0.2023.07.09.11.29.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Jul 2023 11:29:48 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: laurent@vivier.eu, =?utf-8?q?Daniel_P_=2E_Berrang=C3=A9?= Subject: [PULL 08/37] crypto: Add aesdec_IMC Date: Sun, 9 Jul 2023 19:28:27 +0100 Message-Id: <20230709182934.309468-16-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230709182934.309468-1-richard.henderson@linaro.org> References: <20230709182934.309468-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::42a; envelope-from=richard.henderson@linaro.org; helo=mail-wr1-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. 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 | 59 ++++++++++++++++++++ 3 files changed, 79 insertions(+) diff --git a/host/include/generic/host/crypto/aes-round.h b/host/include/generic/host/crypto/aes-round.h index 1b82afc629..335ec3f11e 100644 --- a/host/include/generic/host/crypto/aes-round.h +++ b/host/include/generic/host/crypto/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 7d2be40a67..7be2cc0d8e 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 + AddRoundKey. */ diff --git a/crypto/aes.c b/crypto/aes.c index ec300cda0c..6c05d731f4 100644 --- a/crypto/aes.c +++ b/crypto/aes.c @@ -1322,6 +1322,65 @@ void aesenc_SB_SR_AK_genrev(AESState *r, const AESState *s, const AESState *k) aesenc_SB_SR_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); +} + /* * Perform InvSubBytes + InvShiftRows + AddRoundKey. */