Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp2549175rdh; Sun, 29 Oct 2023 23:37:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEVX8AseHcsFRa/q2VhEjgqZAdf54CGDv7ZAglwbqd791PEHTohK3veTMiyXyAg8CFUUS66 X-Received: by 2002:a17:902:db11:b0:1cc:5a65:c535 with SMTP id m17-20020a170902db1100b001cc5a65c535mr139417plx.6.1698647859067; Sun, 29 Oct 2023 23:37:39 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698647859; cv=pass; d=google.com; s=arc-20160816; b=muN15sHTHO1rlPZ5rQQPoPPah/dpI1NEV92IRutPcRL6T3MZkzA4fU0E1v40j0XH92 6SVb1ZMRkFNhcPpVFjDW9M78vjEKObhHJVo7cY/kMaIBRmSG7lgkkNX5G/e5BvrMJWgL O/8HcX+HYkoYq2DIiMLvgtramF5w7yAmEWC8zMLjAQpwaQeWPwH9drMof77eiv833tUm 1/EUFTRA2BivmxXSn9ghCX3b9hgzJQGk6UhNVURvPXIAylnbSR/cclpNfB8VGs/LT9Iw oO/AwT3dZcyRG5kcP98FXjb8jIPFM9tEprdFXD80Wc77YE93bPHOpdyyJlPGXj+A2sBU 69fw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=UnM+8+c3f8zskns2iqqpD+1d8gxKI6fKSE+Y0j/KWpY=; fh=GrBYTfaCmKSBoJ0OQBiloTDpWB/NcGHvBdTqN4MlWVs=; b=SWdUCj2PhKHUK6RWvMLZqexRwxuhzoqHI/xCvwlOLjqxOs0PsqoOHOkixpsIB/M03K Gclvy7aLVBKonRNPXMhR/B/wO07ei8nSsDqEMl88wh7zJ+BYi9mXivTVsAgGfur3TteA M6qGm80k2CBGAD5vlaN60+suGRkbEuKb8geXGIcscau+PAlAsmYqkxzVbUgWms/2Z/kT oshwge6EYsrj2/slgNe735AKf+DXacDy7AmccaH5ZC6Inp/USTXrRsL98VEMDYLVNKsG jiYTsSwU0YMSvZYdZ5RrmzYiU4u9kWr7FzgncCfYcCgrghW49inu/O4LKioLa+dfAC/U MSZw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=Snd1NsVs; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id k5-20020a170902f28500b001b7ea20dbf2si4154647plc.224.2023.10.29.23.37.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Oct 2023 23:37:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=Snd1NsVs; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 29D1A80BE2FC; Sun, 29 Oct 2023 23:37:38 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231720AbjJ3Ghe (ORCPT + 99 others); Mon, 30 Oct 2023 02:37:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56954 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231719AbjJ3Gh2 (ORCPT ); Mon, 30 Oct 2023 02:37:28 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2089.outbound.protection.outlook.com [40.107.237.89]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3BA08AC; Sun, 29 Oct 2023 23:37:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gLlCLmAx5OFTrixDy4229j3UPnb46AlF52zJNc/4L6ufwYJnsmNCRhJfwJ+v9oohPCaUXB02cK9CcarF5/gnLNBx4UIJVoquxB8kft6ZtxlDirXFkTrc7n2xcsZirSJhC/EhvkzhdXMFckZd+GghgUsgJlFbaP8TSiSnYbCOXDmzBXCmP1/rX4oNlnLfZsL7nHrgQvn7v9KXgXqlSqbLLxnR0r15M7Vsb1DENJzT7Pu68JeVj/H1P36MVCOe9ltZa5+SDoG0Yh6PD+uaHvIhkbTTdLx9dCbV9vL8qs+DOvJkqkPH7HJoXydnPSsqkCdQRd5OydlGQit4b0UuYgYH4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=UnM+8+c3f8zskns2iqqpD+1d8gxKI6fKSE+Y0j/KWpY=; b=PRDSSMgmhMYIl1pNx0pfupimx97+cb3J5Onpw8IbhfXa7TjCdYIVjgQ7hhA/ANoDs5sZY1vENAsFfAQ6Bw9Y1f2/FWF9jBDSyf1sawzcfu2ypw1ONueCSHVg/oLO+i34QK3XC3+74cfqL8LcwUO/Emm4mKuGqZ7Q1T9AKfWRrlGc332eks4r15BGSIKnm6NRK68M3Prt5iTZE9t0/TpMirEzZC/OUCAhH/OU+c0ZkjgVstXuuhd4X9gtGU843sKupqamy7kgmfn8li0X8h8LYu3sL3K5jnIMxi0QyK2EGw3kxDjZ4wFazJPc0PUcgP/fT0tREw6EPC6XpAcJ6JYMlQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=UnM+8+c3f8zskns2iqqpD+1d8gxKI6fKSE+Y0j/KWpY=; b=Snd1NsVsrz5QKIcwVWpyuKJHr2eHN3EDNMnhngx7CMvCz0Y6RHT9ad5ivrLFPp3bXGzCYD+Ka2fTPXEuluFMeJYK2s81RTgpEJ8vr6hbJm68qGmjykwZrLQQazFg+U/XaLJ+lo+vEN0avSAaY7162aYpaWxDGMk/Bc9B5ugYHwg= Received: from MN2PR20CA0060.namprd20.prod.outlook.com (2603:10b6:208:235::29) by PH8PR12MB7135.namprd12.prod.outlook.com (2603:10b6:510:22c::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6933.26; Mon, 30 Oct 2023 06:37:21 +0000 Received: from BL6PEPF0001AB73.namprd02.prod.outlook.com (2603:10b6:208:235:cafe::21) by MN2PR20CA0060.outlook.office365.com (2603:10b6:208:235::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6933.27 via Frontend Transport; Mon, 30 Oct 2023 06:37:21 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by BL6PEPF0001AB73.mail.protection.outlook.com (10.167.242.166) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6933.22 via Frontend Transport; Mon, 30 Oct 2023 06:37:21 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.32; Mon, 30 Oct 2023 01:37:16 -0500 From: Nikunj A Dadhania To: , , , CC: , , , , , , , , Subject: [PATCH v5 01/14] virt: sev-guest: Use AES GCM crypto library Date: Mon, 30 Oct 2023 12:06:39 +0530 Message-ID: <20231030063652.68675-2-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231030063652.68675-1-nikunj@amd.com> References: <20231030063652.68675-1-nikunj@amd.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL6PEPF0001AB73:EE_|PH8PR12MB7135:EE_ X-MS-Office365-Filtering-Correlation-Id: 875d04f7-de63-4e25-568d-08dbd912ab6e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: tU3NaMQ+OLnJ5qQty5HI4/JtyrRjCow45+IvRGBzKvI5XvptKbXwexQnQv2fL24LZljNSuIlICLQWVyXwmgDouMQlFE5q9zpsPkW8UOGib2hYaEdQ25sy0AUYe/UlG28OlirsMbhi3SKvsnHh6pqgXg8elt+wLxlp7lhaUzzjdVE20nOxMGKrzZwOPGQ6CRaE63V7vkuP2cm+UhQkdUfymryaLcBgUUaMATngc5Se18g9sNousUUhO9AoU2zqPc0OEJTwvFvJZ0KBvMxsxyikVkjhznTHAYALSl3G3oA8Y+vMitysQaNvhtwlHmOMWMq4v8ZLRU+nkJVQ7Omz/3U3d4fBZkgGaHlGq1IBW5I9JEDl7YAKUSaKDdJjIUZcdFeHF4v70ji+KGC7LNGL8GJfanN0izvbmRGtbcjUBFs6UisJcRZ6jhwAULuJTtG3ud4qkkyJDELC19NfUSJER0HixmJZlaPMehh+RjpKXVRVByXyR/5N79/UWNBdTwOTVrsCvSwIpMXKvs/kB3XYK//hZqkiFxsMpRrbWnVWRewDfLX9yE/CSc9YwI1E410QsGf04MQEeohrcQ4E6gfN8+yv9lHdb8gqkdLW7CGdbKg+BiFnM3NsTDU7qC+YoPdU+4ojUNp5fhIRc58P+TdGEqU6nxlswHSJ98ldrxPSx5i0kalNYdryjgIUmbL+PeDgv8piKc1LVv+tpAbDcD1GlNtOrZZcqHjgS5AhQKVUBNNO/eaTUYR5xuBod1PHVDlH85huJtjEjqlLf2BtARqp1L4GA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(39860400002)(346002)(136003)(396003)(376002)(230922051799003)(82310400011)(451199024)(64100799003)(186009)(1800799009)(40470700004)(36840700001)(46966006)(40460700003)(36756003)(26005)(16526019)(1076003)(81166007)(83380400001)(426003)(336012)(5660300002)(356005)(7416002)(6666004)(7696005)(2616005)(82740400003)(8676002)(8936002)(47076005)(54906003)(316002)(41300700001)(36860700001)(30864003)(40480700001)(478600001)(110136005)(70586007)(70206006)(4326008)(2906002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Oct 2023 06:37:21.1362 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 875d04f7-de63-4e25-568d-08dbd912ab6e X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BL6PEPF0001AB73.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB7135 X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Sun, 29 Oct 2023 23:37:38 -0700 (PDT) The sev-guest driver encryption code uses Crypto API for SNP guest messaging to interact with AMD Security processor. For enabling SecureTSC, SEV-SNP guests need to send a TSC_INFO request guest message before the smpboot phase starts. Details from the TSC_INFO response will be used to program the VMSA before the secondary CPUs are brought up. The Crypto API is not available this early in the boot phase. In preparation of moving the encryption code out of sev-guest driver to support SecureTSC and make reviewing the diff easier, start using AES GCM library implementation instead of Crypto API. CC: Ard Biesheuvel Signed-off-by: Nikunj A Dadhania Reviewed-by: Tom Lendacky --- drivers/virt/coco/sev-guest/Kconfig | 4 +- drivers/virt/coco/sev-guest/sev-guest.c | 163 ++++++------------------ drivers/virt/coco/sev-guest/sev-guest.h | 3 + 3 files changed, 44 insertions(+), 126 deletions(-) diff --git a/drivers/virt/coco/sev-guest/Kconfig b/drivers/virt/coco/sev-guest/Kconfig index da2d7ca531f0..bcc760bfb468 100644 --- a/drivers/virt/coco/sev-guest/Kconfig +++ b/drivers/virt/coco/sev-guest/Kconfig @@ -2,9 +2,7 @@ config SEV_GUEST tristate "AMD SEV Guest driver" default m depends on AMD_MEM_ENCRYPT - select CRYPTO - select CRYPTO_AEAD2 - select CRYPTO_GCM + select CRYPTO_LIB_AESGCM help SEV-SNP firmware provides the guest a mechanism to communicate with the PSP without risk from a malicious hypervisor who wishes to read, diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c index 97dbe715e96a..68044c436866 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -16,8 +16,7 @@ #include #include #include -#include -#include +#include #include #include #include @@ -28,24 +27,16 @@ #include "sev-guest.h" #define DEVICE_NAME "sev-guest" -#define AAD_LEN 48 -#define MSG_HDR_VER 1 #define SNP_REQ_MAX_RETRY_DURATION (60*HZ) #define SNP_REQ_RETRY_DELAY (2*HZ) -struct snp_guest_crypto { - struct crypto_aead *tfm; - u8 *iv, *authtag; - int iv_len, a_len; -}; - struct snp_guest_dev { struct device *dev; struct miscdevice misc; void *certs_data; - struct snp_guest_crypto *crypto; + struct aesgcm_ctx *ctx; /* request and response are in unencrypted memory */ struct snp_guest_msg *request, *response; @@ -152,132 +143,59 @@ static inline struct snp_guest_dev *to_snp_dev(struct file *file) return container_of(dev, struct snp_guest_dev, misc); } -static struct snp_guest_crypto *init_crypto(struct snp_guest_dev *snp_dev, u8 *key, size_t keylen) +static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen) { - struct snp_guest_crypto *crypto; + struct aesgcm_ctx *ctx; - crypto = kzalloc(sizeof(*crypto), GFP_KERNEL_ACCOUNT); - if (!crypto) + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT); + if (!ctx) return NULL; - crypto->tfm = crypto_alloc_aead("gcm(aes)", 0, 0); - if (IS_ERR(crypto->tfm)) - goto e_free; - - if (crypto_aead_setkey(crypto->tfm, key, keylen)) - goto e_free_crypto; - - crypto->iv_len = crypto_aead_ivsize(crypto->tfm); - crypto->iv = kmalloc(crypto->iv_len, GFP_KERNEL_ACCOUNT); - if (!crypto->iv) - goto e_free_crypto; - - if (crypto_aead_authsize(crypto->tfm) > MAX_AUTHTAG_LEN) { - if (crypto_aead_setauthsize(crypto->tfm, MAX_AUTHTAG_LEN)) { - dev_err(snp_dev->dev, "failed to set authsize to %d\n", MAX_AUTHTAG_LEN); - goto e_free_iv; - } + if (aesgcm_expandkey(ctx, key, keylen, AUTHTAG_LEN)) { + pr_err("SNP: crypto init failed\n"); + kfree(ctx); + return NULL; } - crypto->a_len = crypto_aead_authsize(crypto->tfm); - crypto->authtag = kmalloc(crypto->a_len, GFP_KERNEL_ACCOUNT); - if (!crypto->authtag) - goto e_free_iv; - - return crypto; - -e_free_iv: - kfree(crypto->iv); -e_free_crypto: - crypto_free_aead(crypto->tfm); -e_free: - kfree(crypto); - - return NULL; -} - -static void deinit_crypto(struct snp_guest_crypto *crypto) -{ - crypto_free_aead(crypto->tfm); - kfree(crypto->iv); - kfree(crypto->authtag); - kfree(crypto); -} - -static int enc_dec_message(struct snp_guest_crypto *crypto, struct snp_guest_msg *msg, - u8 *src_buf, u8 *dst_buf, size_t len, bool enc) -{ - struct snp_guest_msg_hdr *hdr = &msg->hdr; - struct scatterlist src[3], dst[3]; - DECLARE_CRYPTO_WAIT(wait); - struct aead_request *req; - int ret; - - req = aead_request_alloc(crypto->tfm, GFP_KERNEL); - if (!req) - return -ENOMEM; - - /* - * AEAD memory operations: - * +------ AAD -------+------- DATA -----+---- AUTHTAG----+ - * | msg header | plaintext | hdr->authtag | - * | bytes 30h - 5Fh | or | | - * | | cipher | | - * +------------------+------------------+----------------+ - */ - sg_init_table(src, 3); - sg_set_buf(&src[0], &hdr->algo, AAD_LEN); - sg_set_buf(&src[1], src_buf, hdr->msg_sz); - sg_set_buf(&src[2], hdr->authtag, crypto->a_len); - - sg_init_table(dst, 3); - sg_set_buf(&dst[0], &hdr->algo, AAD_LEN); - sg_set_buf(&dst[1], dst_buf, hdr->msg_sz); - sg_set_buf(&dst[2], hdr->authtag, crypto->a_len); - - aead_request_set_ad(req, AAD_LEN); - aead_request_set_tfm(req, crypto->tfm); - aead_request_set_callback(req, 0, crypto_req_done, &wait); - - aead_request_set_crypt(req, src, dst, len, crypto->iv); - ret = crypto_wait_req(enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req), &wait); - - aead_request_free(req); - return ret; + return ctx; } -static int __enc_payload(struct snp_guest_dev *snp_dev, struct snp_guest_msg *msg, +static int __enc_payload(struct aesgcm_ctx *ctx, struct snp_guest_msg *msg, void *plaintext, size_t len) { - struct snp_guest_crypto *crypto = snp_dev->crypto; struct snp_guest_msg_hdr *hdr = &msg->hdr; + u8 iv[GCM_AES_IV_SIZE] = {}; - memset(crypto->iv, 0, crypto->iv_len); - memcpy(crypto->iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno)); + if (WARN_ON((hdr->msg_sz + ctx->authsize) > sizeof(msg->payload))) + return -EBADMSG; - return enc_dec_message(crypto, msg, plaintext, msg->payload, len, true); + memcpy(iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno)); + aesgcm_encrypt(ctx, msg->payload, plaintext, len, &hdr->algo, AAD_LEN, + iv, hdr->authtag); + return 0; } -static int dec_payload(struct snp_guest_dev *snp_dev, struct snp_guest_msg *msg, +static int dec_payload(struct aesgcm_ctx *ctx, struct snp_guest_msg *msg, void *plaintext, size_t len) { - struct snp_guest_crypto *crypto = snp_dev->crypto; struct snp_guest_msg_hdr *hdr = &msg->hdr; + u8 iv[GCM_AES_IV_SIZE] = {}; - /* Build IV with response buffer sequence number */ - memset(crypto->iv, 0, crypto->iv_len); - memcpy(crypto->iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno)); - - return enc_dec_message(crypto, msg, msg->payload, plaintext, len, false); + memcpy(iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno)); + if (aesgcm_decrypt(ctx, plaintext, msg->payload, len, &hdr->algo, + AAD_LEN, iv, hdr->authtag)) + return 0; + else + return -EBADMSG; } static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, u32 sz) { - struct snp_guest_crypto *crypto = snp_dev->crypto; struct snp_guest_msg *resp = &snp_dev->secret_response; struct snp_guest_msg *req = &snp_dev->secret_request; struct snp_guest_msg_hdr *req_hdr = &req->hdr; struct snp_guest_msg_hdr *resp_hdr = &resp->hdr; + struct aesgcm_ctx *ctx = snp_dev->ctx; dev_dbg(snp_dev->dev, "response [seqno %lld type %d version %d sz %d]\n", resp_hdr->msg_seqno, resp_hdr->msg_type, resp_hdr->msg_version, resp_hdr->msg_sz); @@ -298,11 +216,11 @@ static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, * If the message size is greater than our buffer length then return * an error. */ - if (unlikely((resp_hdr->msg_sz + crypto->a_len) > sz)) + if (unlikely((resp_hdr->msg_sz + ctx->authsize) > sz)) return -EBADMSG; /* Decrypt the payload */ - return dec_payload(snp_dev, resp, payload, resp_hdr->msg_sz + crypto->a_len); + return dec_payload(ctx, resp, payload, resp_hdr->msg_sz); } static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8 type, @@ -329,7 +247,7 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8 dev_dbg(snp_dev->dev, "request [seqno %lld type %d version %d sz %d]\n", hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz); - return __enc_payload(snp_dev, req, payload, sz); + return __enc_payload(snp_dev->ctx, req, payload, sz); } static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, @@ -472,7 +390,6 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg) { - struct snp_guest_crypto *crypto = snp_dev->crypto; struct snp_report_resp *resp; struct snp_report_req req; int rc, resp_len; @@ -490,7 +407,7 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_io * response payload. Make sure that it has enough space to cover the * authtag. */ - resp_len = sizeof(resp->data) + crypto->a_len; + resp_len = sizeof(resp->data) + snp_dev->ctx->authsize; resp = kzalloc(resp_len, GFP_KERNEL_ACCOUNT); if (!resp) return -ENOMEM; @@ -511,7 +428,6 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_io static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg) { - struct snp_guest_crypto *crypto = snp_dev->crypto; struct snp_derived_key_resp resp = {0}; struct snp_derived_key_req req; int rc, resp_len; @@ -528,7 +444,7 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_reque * response payload. Make sure that it has enough space to cover the * authtag. */ - resp_len = sizeof(resp.data) + crypto->a_len; + resp_len = sizeof(resp.data) + snp_dev->ctx->authsize; if (sizeof(buf) < resp_len) return -ENOMEM; @@ -552,7 +468,6 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_reque static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg) { - struct snp_guest_crypto *crypto = snp_dev->crypto; struct snp_ext_report_req req; struct snp_report_resp *resp; int ret, npages = 0, resp_len; @@ -590,7 +505,7 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques * response payload. Make sure that it has enough space to cover the * authtag. */ - resp_len = sizeof(resp->data) + crypto->a_len; + resp_len = sizeof(resp->data) + snp_dev->ctx->authsize; resp = kzalloc(resp_len, GFP_KERNEL_ACCOUNT); if (!resp) return -ENOMEM; @@ -802,8 +717,8 @@ static int __init sev_guest_probe(struct platform_device *pdev) goto e_free_response; ret = -EIO; - snp_dev->crypto = init_crypto(snp_dev, snp_dev->vmpck, VMPCK_KEY_LEN); - if (!snp_dev->crypto) + snp_dev->ctx = snp_init_crypto(snp_dev->vmpck, VMPCK_KEY_LEN); + if (!snp_dev->ctx) goto e_free_cert_data; misc = &snp_dev->misc; @@ -818,11 +733,13 @@ static int __init sev_guest_probe(struct platform_device *pdev) ret = misc_register(misc); if (ret) - goto e_free_cert_data; + goto e_free_ctx; dev_info(dev, "Initialized SEV guest driver (using vmpck_id %d)\n", vmpck_id); return 0; +e_free_ctx: + kfree(snp_dev->ctx); e_free_cert_data: free_shared_pages(snp_dev->certs_data, SEV_FW_BLOB_MAX_SIZE); e_free_response: @@ -841,7 +758,7 @@ static int __exit sev_guest_remove(struct platform_device *pdev) free_shared_pages(snp_dev->certs_data, SEV_FW_BLOB_MAX_SIZE); free_shared_pages(snp_dev->response, sizeof(struct snp_guest_msg)); free_shared_pages(snp_dev->request, sizeof(struct snp_guest_msg)); - deinit_crypto(snp_dev->crypto); + kfree(snp_dev->ctx); misc_deregister(&snp_dev->misc); return 0; diff --git a/drivers/virt/coco/sev-guest/sev-guest.h b/drivers/virt/coco/sev-guest/sev-guest.h index 21bda26fdb95..ceb798a404d6 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.h +++ b/drivers/virt/coco/sev-guest/sev-guest.h @@ -13,6 +13,9 @@ #include #define MAX_AUTHTAG_LEN 32 +#define AUTHTAG_LEN 16 +#define AAD_LEN 48 +#define MSG_HDR_VER 1 /* See SNP spec SNP_GUEST_REQUEST section for the structure */ enum msg_type { -- 2.34.1