From patchwork Mon Jul 3 10:04:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 698636 Delivered-To: patch@linaro.org Received: by 2002:adf:fcc5:0:0:0:0:0 with SMTP id f5csp1664748wrs; Mon, 3 Jul 2023 03:09:30 -0700 (PDT) X-Google-Smtp-Source: APBJJlGIlvK/EkyaQRodBGN60PUdMcJyQzt194hXebfRMJMKTYyppNswrAO2i13xY1FwadpztO9N X-Received: by 2002:a0c:f882:0:b0:631:f1fa:8c04 with SMTP id u2-20020a0cf882000000b00631f1fa8c04mr8842468qvn.38.1688378970221; Mon, 03 Jul 2023 03:09:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688378970; cv=none; d=google.com; s=arc-20160816; b=HEXzHveWPP2H8o+8QL9l8+30Cq6Rvm3gbHqlKqj83ZpvHW6jBPv7M/xxX1Ss9VBoFD 5kPfPgx+0Pxb42bbhRWoJmVaS2lY0t9EsSY17fAUKhQTo2FkIgBdReKE0Fx2KsYOMAdA yOR96AfXrUxineZCPW0pTx/DV0Kg5e/SIaXKS/utez6AzmImaPrJpk1LI5a/xXCGsaeS hR4rBxNYjH6BNaJFN2xi7e0LuDkt1C8IxrQclWgG12ld8KmK4WX0yyir+YlHCeGo6CS1 4TkeiVR9vhsWkmFTbCClb2+twn5IvPQ3Yqdj8gpe9Muo3MRmHsBu+iqISm3XWLavGcds OeSQ== 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=IwEenOSIExv4ocs0aF0VGqlBA7MmMe37v6A4s1oO9f0=; fh=yGBgCIAdw0xhdK58ttTTG5yX3hLvrsJsvO63zqD4x2E=; b=vhYdPuHFuldX0spX1F3eARsSGEgAGjGHkX4xnUnym12GSWx8hWXf7ltF8ojxWsqrQN Oq+n4xkcL57ibgrdDUep74MWxXCGkEQW+Sw+APsCpo2v64dA73yE/bkkeLnUJ1lzxYFE RhM22FSqbjjivgJoYVfIAX6dv1ayP6D5nrd0pEG1vGYXzVqo/LYLLQkj4MQd6Jn5r/TW sYetXb8hkzJOLQB4bLd7xMFmqJLslRx4OzL//pD9XPQhinqEBPt8yWQ9WKxWbKobbFxL yK7bCRQlXkdxc5sFuu30Op+cXe37s71zTy6bcQXs5cj12O8oYyaYcSzsjuyUZXyf+5X7 RBug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=U5K+qWzw; 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 q2-20020ad45ca2000000b00635e51f9287si10125645qvh.527.2023.07.03.03.09.30 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 03 Jul 2023 03:09:30 -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=U5K+qWzw; 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 1qGGRg-000384-Q8; Mon, 03 Jul 2023 06:06:12 -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 1qGGRX-0002si-Ai for qemu-devel@nongnu.org; Mon, 03 Jul 2023 06:06:03 -0400 Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qGGRH-0005iJ-A0 for qemu-devel@nongnu.org; Mon, 03 Jul 2023 06:06:02 -0400 Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-3fbc656873eso49084675e9.1 for ; Mon, 03 Jul 2023 03:05:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1688378727; x=1690970727; 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=IwEenOSIExv4ocs0aF0VGqlBA7MmMe37v6A4s1oO9f0=; b=U5K+qWzwE9QEjvBXzat6AyxD/Jthw9/912VSkv0TjiyaQtUykG8TkwNKTsnQ6EhPgu 0SwN4AeTdFREfuy9Y6zIRwm4/Flg8+FCB11pje6dsHUYCFv1H846TOAOOt+xDUyohMW/ D3qEKGSE0b6M4oxax6XZrZz++na3aF+o0zsevNdfR55ngk9H81KKihph+aveZysftAuI O/8yseRyKAspId1WnGLX+YsEQrhraWDwM7FTjJhW4Y1noCosFD2HsHkAw+ec+vFIjyvQ 4xl6sLv2KHx6VBUfSyXw5v/vLM63BlGcW01vJxpvWHXL47ceVedIkptXlA1357Z7lI/J YvfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688378727; x=1690970727; 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=IwEenOSIExv4ocs0aF0VGqlBA7MmMe37v6A4s1oO9f0=; b=VnIodPdYKlimjMjipz42zJh/uCN+Yl6oJQGNidArzDJPr3jNyhwFmzgSuhMi8B1h5p //gbGJ95ioJY7xSOW9WigdAC38KFKpegGcbbc3HUG+ZoH6KINjd8W1bmeppwQMIQ+u4r K49iaPhQP8vPcZkZT29mEnTVGdfv/5EZbWyQkNqHfABNp4jjDO24xe2dipgTYJFXi/0a 3dbq9U18JKpj92aVKR0Yg4rfYPx11CA4BHAgd9l76VAGEgt/eWIAL+16w6+40wStoQlr vVSC5QnvZo+1yspUeYe7SrOjgwji4GH9ywsHP1WEM1zFxJg5vVKr9KWB6Xp5q4GfdieT egNg== X-Gm-Message-State: AC+VfDw4d0DkfZeWhLx/Zpk9Kv5uRQS629d25TQckI76x/iZWBDTmm2l mM8xKDUUrzSqxaLN3mQ9xQ2cZ7PGqCiOsmQWhYz3uA== X-Received: by 2002:a7b:c853:0:b0:3f5:fff8:d4f3 with SMTP id c19-20020a7bc853000000b003f5fff8d4f3mr12918229wml.7.1688378727649; Mon, 03 Jul 2023 03:05:27 -0700 (PDT) Received: from stoup.home (91.232.79.188.dynamic.jazztel.es. [188.79.232.91]) by smtp.gmail.com with ESMTPSA id t5-20020a05600001c500b00313f7b077fesm18446562wrx.59.2023.07.03.03.05.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jul 2023 03:05:27 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org, qemu-riscv@nongnu.org, pbonzini@redhat.com, eduardo@habkost.net, alistair.francis@wdc.com, danielhb413@gmail.com, =?utf-8?q?Daniel_P_=2E_Berrang=C3=A9?= Subject: [PATCH v4 05/37] crypto: Add aesenc_SB_SR_AK Date: Mon, 3 Jul 2023 12:04:48 +0200 Message-Id: <20230703100520.68224-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230703100520.68224-1-richard.henderson@linaro.org> References: <20230703100520.68224-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::32f; envelope-from=richard.henderson@linaro.org; helo=mail-wm1-x32f.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 Start adding infrastructure for accelerating guest AES. Begin with a SubBytes + ShiftRows + AddRoundKey primitive. Acked-by: Daniel P. Berrangé Signed-off-by: Richard Henderson --- MAINTAINERS | 1 + host/include/generic/host/crypto/aes-round.h | 16 +++++++ include/crypto/aes-round.h | 44 +++++++++++++++++++ crypto/aes.c | 46 ++++++++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 host/include/generic/host/crypto/aes-round.h create mode 100644 include/crypto/aes-round.h diff --git a/MAINTAINERS b/MAINTAINERS index 4feea49a6e..f5e199bb7f 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3216,6 +3216,7 @@ M: Daniel P. Berrange S: Maintained F: crypto/ F: include/crypto/ +F: host/include/*/host/crypto/ F: qapi/crypto.json F: tests/unit/test-crypto-* F: tests/bench/benchmark-crypto-* diff --git a/host/include/generic/host/crypto/aes-round.h b/host/include/generic/host/crypto/aes-round.h new file mode 100644 index 0000000000..c5d8066179 --- /dev/null +++ b/host/include/generic/host/crypto/aes-round.h @@ -0,0 +1,16 @@ +/* + * No host specific aes acceleration. + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef GENERIC_HOST_CRYPTO_AES_ROUND_H +#define GENERIC_HOST_CRYPTO_AES_ROUND_H + +#define HAVE_AES_ACCEL false +#define ATTR_AES_ACCEL + +void aesenc_SB_SR_AK_accel(AESState *, const AESState *, + const AESState *, bool) + QEMU_ERROR("unsupported accel"); + +#endif /* GENERIC_HOST_CRYPTO_AES_ROUND_H */ diff --git a/include/crypto/aes-round.h b/include/crypto/aes-round.h new file mode 100644 index 0000000000..b85db1a30e --- /dev/null +++ b/include/crypto/aes-round.h @@ -0,0 +1,44 @@ +/* + * AES round fragments, generic version + * SPDX-License-Identifier: GPL-2.0-or-later + * + * Copyright (C) 2023 Linaro, Ltd. + */ + +#ifndef CRYPTO_AES_ROUND_H +#define CRYPTO_AES_ROUND_H + +/* Hosts with acceleration will usually need a 16-byte vector type. */ +typedef uint8_t AESStateVec __attribute__((vector_size(16))); + +typedef union { + uint8_t b[16]; + uint32_t w[4]; + uint64_t d[2]; + AESStateVec v; +} AESState; + +#include "host/crypto/aes-round.h" + +/* + * Perform SubBytes + ShiftRows + AddRoundKey. + */ + +void aesenc_SB_SR_AK_gen(AESState *ret, const AESState *st, + const AESState *rk); +void aesenc_SB_SR_AK_genrev(AESState *ret, const AESState *st, + const AESState *rk); + +static inline void aesenc_SB_SR_AK(AESState *r, const AESState *st, + const AESState *rk, bool be) +{ + if (HAVE_AES_ACCEL) { + aesenc_SB_SR_AK_accel(r, st, rk, be); + } else if (HOST_BIG_ENDIAN == be) { + aesenc_SB_SR_AK_gen(r, st, rk); + } else { + aesenc_SB_SR_AK_genrev(r, st, rk); + } +} + +#endif /* CRYPTO_AES_ROUND_H */ diff --git a/crypto/aes.c b/crypto/aes.c index e65c97e0c1..408d92b81f 100644 --- a/crypto/aes.c +++ b/crypto/aes.c @@ -29,6 +29,7 @@ */ #include "qemu/osdep.h" #include "crypto/aes.h" +#include "crypto/aes-round.h" typedef uint32_t u32; typedef uint8_t u8; @@ -1215,6 +1216,51 @@ static const u32 rcon[] = { 0x1B000000, 0x36000000, /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */ }; +/* + * Perform SubBytes + ShiftRows + AddRoundKey. + */ +static inline void +aesenc_SB_SR_AK_swap(AESState *ret, const AESState *st, + const AESState *rk, bool swap) +{ + const int swap_b = swap ? 15 : 0; + AESState t; + + t.b[swap_b ^ 0x0] = AES_sbox[st->b[swap_b ^ AES_SH(0x0)]]; + t.b[swap_b ^ 0x1] = AES_sbox[st->b[swap_b ^ AES_SH(0x1)]]; + t.b[swap_b ^ 0x2] = AES_sbox[st->b[swap_b ^ AES_SH(0x2)]]; + t.b[swap_b ^ 0x3] = AES_sbox[st->b[swap_b ^ AES_SH(0x3)]]; + t.b[swap_b ^ 0x4] = AES_sbox[st->b[swap_b ^ AES_SH(0x4)]]; + t.b[swap_b ^ 0x5] = AES_sbox[st->b[swap_b ^ AES_SH(0x5)]]; + t.b[swap_b ^ 0x6] = AES_sbox[st->b[swap_b ^ AES_SH(0x6)]]; + t.b[swap_b ^ 0x7] = AES_sbox[st->b[swap_b ^ AES_SH(0x7)]]; + t.b[swap_b ^ 0x8] = AES_sbox[st->b[swap_b ^ AES_SH(0x8)]]; + t.b[swap_b ^ 0x9] = AES_sbox[st->b[swap_b ^ AES_SH(0x9)]]; + t.b[swap_b ^ 0xa] = AES_sbox[st->b[swap_b ^ AES_SH(0xA)]]; + t.b[swap_b ^ 0xb] = AES_sbox[st->b[swap_b ^ AES_SH(0xB)]]; + t.b[swap_b ^ 0xc] = AES_sbox[st->b[swap_b ^ AES_SH(0xC)]]; + t.b[swap_b ^ 0xd] = AES_sbox[st->b[swap_b ^ AES_SH(0xD)]]; + t.b[swap_b ^ 0xe] = AES_sbox[st->b[swap_b ^ AES_SH(0xE)]]; + t.b[swap_b ^ 0xf] = AES_sbox[st->b[swap_b ^ AES_SH(0xF)]]; + + /* + * Perform the AddRoundKey with generic vectors. + * This may be expanded to either host integer or host vector code. + * The key and output endianness match, so no bswap required. + */ + ret->v = t.v ^ rk->v; +} + +void aesenc_SB_SR_AK_gen(AESState *r, const AESState *s, const AESState *k) +{ + aesenc_SB_SR_AK_swap(r, s, k, false); +} + +void aesenc_SB_SR_AK_genrev(AESState *r, const AESState *s, const AESState *k) +{ + aesenc_SB_SR_AK_swap(r, s, k, true); +} + /** * Expand the cipher key into the encryption key schedule. */