From patchwork Wed Aug 4 16:18:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Cameron X-Patchwork-Id: 12419421 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B0D2BC432BE for ; Wed, 4 Aug 2021 16:22:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8FE5960F94 for ; Wed, 4 Aug 2021 16:22:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234482AbhHDQWf (ORCPT ); Wed, 4 Aug 2021 12:22:35 -0400 Received: from frasgout.his.huawei.com ([185.176.79.56]:3578 "EHLO frasgout.his.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236749AbhHDQWf (ORCPT ); Wed, 4 Aug 2021 12:22:35 -0400 Received: from fraeml737-chm.china.huawei.com (unknown [172.18.147.207]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4GfxpR4JZWz6F80H; Thu, 5 Aug 2021 00:22:07 +0800 (CST) Received: from lhreml710-chm.china.huawei.com (10.201.108.61) by fraeml737-chm.china.huawei.com (10.206.15.218) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Wed, 4 Aug 2021 18:22:20 +0200 Received: from localhost.localdomain (10.123.41.22) by lhreml710-chm.china.huawei.com (10.201.108.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Wed, 4 Aug 2021 17:22:20 +0100 From: Jonathan Cameron To: , CC: , , Chris Browy , , Lorenzo Pieralisi , Bjorn Helgaas , Jeremy Kerr , Jonathan Cameron Subject: [RFC PATCH 1/4] lib/asn1_encoder: Add a function to encode many byte integer values. Date: Thu, 5 Aug 2021 00:18:36 +0800 Message-ID: <20210804161839.3492053-2-Jonathan.Cameron@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20210804161839.3492053-1-Jonathan.Cameron@huawei.com> References: <20210804161839.3492053-1-Jonathan.Cameron@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.123.41.22] X-ClientProxiedBy: lhreml754-chm.china.huawei.com (10.201.108.204) To lhreml710-chm.china.huawei.com (10.201.108.61) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-cxl@vger.kernel.org An example is the encoding of ECC signatures used by the ECDSA signature verification code. A user is the new SPDM support where raw signatures are returned by the responder. These can then be encoded so that we can pass them to signature_verify() Signed-off-by: Jonathan Cameron --- Needs considerably more testing with crafted examples to hit the corner cases. For now it's enough to show how this 'might' fit together. include/linux/asn1_encoder.h | 3 ++ lib/asn1_encoder.c | 54 ++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/include/linux/asn1_encoder.h b/include/linux/asn1_encoder.h index 08cd0c2ad34f..30c3ebacd46c 100644 --- a/include/linux/asn1_encoder.h +++ b/include/linux/asn1_encoder.h @@ -19,6 +19,9 @@ unsigned char * asn1_encode_tag(unsigned char *data, const unsigned char *end_data, u32 tag, const unsigned char *string, int len); unsigned char * +asn1_encode_integer_large_positive(unsigned char *data, const unsigned char *end_data, + u32 tag, const unsigned char *integer, int len); +unsigned char * asn1_encode_octet_string(unsigned char *data, const unsigned char *end_data, const unsigned char *string, u32 len); diff --git a/lib/asn1_encoder.c b/lib/asn1_encoder.c index 41e71aae3ef6..135c5cf2d77e 100644 --- a/lib/asn1_encoder.c +++ b/lib/asn1_encoder.c @@ -317,6 +317,60 @@ asn1_encode_tag(unsigned char *data, const unsigned char *end_data, } EXPORT_SYMBOL_GPL(asn1_encode_tag); +unsigned char * +asn1_encode_integer_large_positive(unsigned char *data, const unsigned char *end_data, + u32 tag, const unsigned char *integer, int len) +{ + int data_len = end_data - data; + unsigned char *d = &data[2]; + bool found = false; + int i; + + if (WARN(tag > 30, "ASN.1 tag can't be > 30")) + return ERR_PTR(-EINVAL); + + if (!integer && WARN(len > 127, + "BUG: recode tag is too big (>127)")) + return ERR_PTR(-EINVAL); + + if (IS_ERR(data)) + return data; + + if (data_len < 3) + return ERR_PTR(-EINVAL); + + + data[0] = _tagn(UNIV, PRIM, tag); + /* Leave space for length */ + data_len -= 2; + + for (i = 0; i < len; i++) { + int byte = integer[i]; + + if (!found && byte == 0) + continue; + + /* + * as per encode_integer + */ + if (!found && (byte & 0x80)) { + *d++ = 0; + data_len--; + } + found = true; + if (data_len == 0) + return ERR_PTR(-EINVAL); + + *d++ = byte; + data_len--; + } + + data[1] = d - data - 2; + + return d; +} +EXPORT_SYMBOL_GPL(asn1_encode_integer_large_positive); + /** * asn1_encode_octet_string() - encode an ASN.1 OCTET STRING * @data: pointer to encode at From patchwork Wed Aug 4 16:18:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Cameron X-Patchwork-Id: 12419423 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 41EEFC432BE for ; Wed, 4 Aug 2021 16:22:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 26D8961008 for ; Wed, 4 Aug 2021 16:22:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236893AbhHDQXG (ORCPT ); Wed, 4 Aug 2021 12:23:06 -0400 Received: from frasgout.his.huawei.com ([185.176.79.56]:3579 "EHLO frasgout.his.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236861AbhHDQXF (ORCPT ); Wed, 4 Aug 2021 12:23:05 -0400 Received: from fraeml745-chm.china.huawei.com (unknown [172.18.147.206]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4Gfxpz6prTz6GFPV; Thu, 5 Aug 2021 00:22:35 +0800 (CST) Received: from lhreml710-chm.china.huawei.com (10.201.108.61) by fraeml745-chm.china.huawei.com (10.206.15.226) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Wed, 4 Aug 2021 18:22:50 +0200 Received: from localhost.localdomain (10.123.41.22) by lhreml710-chm.china.huawei.com (10.201.108.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Wed, 4 Aug 2021 17:22:50 +0100 From: Jonathan Cameron To: , CC: , , Chris Browy , , Lorenzo Pieralisi , Bjorn Helgaas , Jeremy Kerr , Jonathan Cameron Subject: [RFC PATCH 2/4] spdm: Introduce a library for DMTF SPDM Date: Thu, 5 Aug 2021 00:18:37 +0800 Message-ID: <20210804161839.3492053-3-Jonathan.Cameron@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20210804161839.3492053-1-Jonathan.Cameron@huawei.com> References: <20210804161839.3492053-1-Jonathan.Cameron@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.123.41.22] X-ClientProxiedBy: lhreml754-chm.china.huawei.com (10.201.108.204) To lhreml710-chm.china.huawei.com (10.201.108.61) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-cxl@vger.kernel.org The Security Protocol and Data Model (SPDM) defines messages, data objects and sequences for performing message exchanges between devices over various transports and physical media. As the kernel supports several possible transports (mctp, PCI DOE) introduce a library than can in turn be used with all those transports. There are a large number of open questions around how we do this that need to be resolved. These include: * Key chain management - Current approach is to use a keychain provide as part of per transport initialization for the root certificates which are assumed to be loaded into that keychain, perhaps in an initrd script. - Each SPDM instance then has its own keychain to manage its certificates. It may make sense to drop this, but that looks like it will make a lot of the standard infrastructure harder to use. * ECC algorithms needing ASN1 encoded signatures. I'm struggling to find any specification that actual 'requires' that choice vs raw data, so my guess is that this is a question of existing usecases (x509 certs seem to use this form, but CHALLENGE_AUTH SPDM seems to use raw data). I'm not sure whether we are better off just encoding the signature in ASN1 as currently done in this series, or if it is worth a tweaking things in the crypto layers. * Lots of options in actual implementation to look at. Signed-off-by: Jonathan Cameron --- lib/Kconfig | 3 + lib/Makefile | 2 + lib/spdm.c | 1196 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 1201 insertions(+) diff --git a/lib/Kconfig b/lib/Kconfig index ac3b30697b2b..0aa2fef6a592 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -704,3 +704,6 @@ config PLDMFW config ASN1_ENCODER tristate + +config SPDM + tristate diff --git a/lib/Makefile b/lib/Makefile index 2cc359ec1fdd..566166d6936e 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -282,6 +282,8 @@ obj-$(CONFIG_PERCPU_TEST) += percpu_test.o obj-$(CONFIG_ASN1) += asn1_decoder.o obj-$(CONFIG_ASN1_ENCODER) += asn1_encoder.o +obj-$(CONFIG_SPDM) += spdm.o + obj-$(CONFIG_FONT_SUPPORT) += fonts/ hostprogs := gen_crc32table diff --git a/lib/spdm.c b/lib/spdm.c new file mode 100644 index 000000000000..3ce2341647f8 --- /dev/null +++ b/lib/spdm.c @@ -0,0 +1,1196 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * DMTF Security Protocol and Data Model + * + * Copyright (C) 2021 Huawei + * Jonathan Cameron + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +/* + * Todo + * - Secure channel setup. + * - Multiple slot support. + * - Measurement support (over secure channel or within CHALLENGE_AUTH. + * - Support more core algorithms (not CMA does not require them, but may use + * them if present. + * - Extended algorithm, support. + */ +/* + * Discussions points + * 1. Worth adding an SPDM layer around a transport layer? + * 2. Pad all SPDM request response to DWORD so we don't have to bounce for CMA /DOE. + * 3. Currently only implement one flow - so ignore whether we have certs cached. + * Could implement the alternative flows, but at cost of complexity. + * 4. Keyring management. How to ensure we can easily check root key against + * keys in appropriate keyring, but ensure we can't cross check keys + * from different devices. Current solution of one keyring per SPDM has issues + * around cleanup when an error occurs. + * 5. Several corners of the SPDM specification were not totally clear to me, so + * where unsure I verified options against openSPDM. + * Detailed stuff + * - SPDM spec doesn't define a header, but all requests and responses have same + * first 4 bytes. Either could define that as a header, or givem the better names + * to reflect how param1 and param2 are actually used. + */ + +static int spdm_append_buffer_a(struct spdm_state *spdm_state, void *data, + size_t data_size, bool reset) +{ + u8 *a_new; + + if (reset) { + kfree(spdm_state->a); + spdm_state->a = NULL; + spdm_state->a_length = 0; + } + + a_new = krealloc(spdm_state->a, spdm_state->a_length + data_size, GFP_KERNEL); + if (!a_new) + return -ENOMEM; + + spdm_state->a = a_new; + memcpy(spdm_state->a + spdm_state->a_length, data, data_size); + spdm_state->a_length += data_size; + + return 0; +} + +#define SPDM_REQ 0x80 +#define SPDM_GET_VERSION 0x04 + +struct spdm_get_version_req { + u8 version; + u8 code; + u8 param1; + u8 param2; +}; + +struct spdm_get_version_rsp { + u8 version; + u8 code; + u8 param1; + u8 param2; + + u8 reserved; + u8 version_number_entry_count; + __le16 version_number_entries[]; +}; + +#define SPDM_GET_CAPABILITIES 0x61 + +/* For this exchange the request and response messages have the same form. */ +struct spdm_get_capabilities_reqrsp { + u8 version; + u8 code; + u8 param1; + u8 param2; + + u8 reserved; + u8 ctexponent; + u16 reserved2; + +/* CACHE_CAP is only valid for response */ +#define SPDM_GET_CAP_FLAG_CACHE_CAP BIT(0) +#define SPDM_GET_CAP_FLAG_CERT_CAP BIT(1) +#define SPDM_GET_CAP_FLAG_CHAL_CAP BIT(2) + +/* MEAS_CAP values other than 0 only for response */ +#define SPDM_GET_CAP_FLAG_MEAS_CAP_MSK GENMASK(4, 3) +#define SPDM_GET_CAP_FLAG_MEAS_CAP_NO 0 +#define SPDM_GET_CAP_FLAG_MEAS_CAP_MEAS 1 +#define SPDM_GET_CAP_FLAG_MEAS_CAP_MEAS_SIG 2 + +/* MEAS_FRESH_CAP == 0 for request */ +#define SPDM_GET_CAP_FLAG_MEAS_FRESH_CAP BIT(5) +#define SPDM_GET_CAP_FLAG_ENCRYPT_CAP BIT(6) +#define SPDM_GET_CAP_FLAG_MAC_CAP BIT(7) +#define SPDM_GET_CAP_FLAG_MUT_AUTH_CAP BIT(8) +#define SPDM_GET_CAP_FLAG_KEY_EX_CAP BIT(9) + +#define SPDM_GET_CAP_FLAG_PSK_CAP_MSK GENMASK(11, 10) +#define SPDM_GET_CAP_FLAG_PSK_CAP_NO_PRESHARE 0 +#define SPDM_GET_CAP_FLAG_PSK_CAP_PRESHARE 1 + +#define SPDM_GET_CAP_FLAG_ENCAP_CAP BIT(12) +#define SPDM_GET_CAP_FLAG_HBEAT_CAP BIT(13) +#define SPDM_GET_CAP_FLAG_KEY_UPD_CAP BIT(14) +#define SPDM_GET_CAP_FLAG_HANDSHAKE_ITC_CAP BIT(15) +#define SPDM_GET_CAP_FLAG_PUB_KEY_ID_CAP BIT(16) + __le32 flags; +}; + +#define SPDM_NEGOTIATE_ALGS 0x63 + +struct spdm_negotiate_algs_req { + u8 version; + u8 code; + u8 param1; /* Numer of algorithm structure tables */ + u8 param2; + + __le16 length; /* <= 128 bytes */ + u8 measurement_specification; /* Only one bit set, BIT 0 == DMTF */ + u8 reserved; + __le32 base_asym_algo; + + /* Bit mask, entries form spdm_base_hash_algo */ + __le32 base_hash_algo; + + u8 reserved2[12]; + u8 ext_asm_count; + u8 ext_hash_count; + u8 reserved3[2]; + + /* + * Additional fields at end of this structure + * - ExtAsym 4 * ext_asm_count + * - ExtHash 4 * ext_hash_count + * - ReqAlgStruct size * param1 + */ +}; + +struct spdm_negotiate_algs_rsp { + u8 version; + u8 code; + u8 param1; /* Numer of algorithm structure tables */ + u8 param2; + + __le16 length; /* <= 128 bytes */ + u8 measurement_specification; /* Only one bit set, BIT 0 == DMTF */ + u8 reserved; + + /* Exactly one bit must be set if GET_MEASUREMENTS is supported */ + __le32 measurement_hash_algo; + /* At most one bit set to reflect negotiated alg */ + __le32 base_asym_sel; + __le32 base_hash_sel; + u8 reserved2[12]; + u8 ext_asym_sel_count; /* Either 0 or 1 */ + u8 ext_hash_sel_count; /* Either 0 or 1 */ + u8 reserved3[2]; + + /* + * Additional fields at end of this structure + * - ExtAsym 4 * ext_asm_count + * - ExtHash 4 * ext_hash_count + * - ReqAlgStruct size * param1 + */ +}; + +#define SPDM_REQ_ALG_STRUCT_TYPE_DHE 0x2 +#define SPDM_DHE_ALGO_FFDHE_2048 BIT(0) +#define SPDM_DHE_ALGO_FFDHE_3072 BIT(1) +#define SPDM_DHE_ALGO_FFDHE_4096 BIT(2) +#define SPDM_DHE_ALGO_SECP_256R1 BIT(3) +#define SPDM_DHE_ALGO_SECP_384R1 BIT(4) +#define SPDM_DHE_ALGO_SECP_521R1 BIT(5) + +#define SPDM_REQ_ALG_STRUCT_TYPE_AEAD_CIPHER_SUITE 0x3 +#define SPDM_AEAD_ALGO_AES_128_GCM BIT(0) +#define SPDM_AEAD_ALGO_AES_256_GCM BIT(1) +#define SPDM_AEAD_ALGO_CHACHA20_POLY1305 BIT(2) +#define SPDM_REQ_ALG_STRUCT_TYPE_REQ_BASE_ASYM_ALG 0x4 +/* As for base_asym_algo above */ + +#define SPDM_REQ_ALG_STRUCT_TYPE_KEY_SCHEDULE 0x5 +#define SPDM_KEY_SCHEDULE_SPDM BIT(0) + +struct spdm_req_alg_struct { + u8 alg_type; + u8 alg_count; /* 0x2K where K is number of alg_external entries */ + /* This field is sized based on alg_count[7:5] - currently always 2 */ + __le16 alg_supported; + __le32 alg_external[]; +}; + +#define SPDM_GET_DIGESTS 0x01 +struct spdm_digest_req { + u8 version; + u8 code; + u8 param1; /* Reserved */ + u8 param2; /* Reserved */ +}; + +struct spdm_cert_chain { + __le16 length; + u8 reserved[2]; + /* + * Additional fields: + * - Hash of the root + * - Certs: ASN.1 Der-encoded X.509 v3 - First cert signed by root or is the root. + */ +}; + +struct spdm_digest_rsp { + u8 version; + u8 code; + u8 param1; /* Reserved */ + u8 param2; /* Slot mask */ + /* Hash of spdm_cert_chain for each slot */ + u8 digests[]; +}; + +#define SPDM_GET_CERTIFICATE 0x02 +struct spdm_certificate_req { + u8 version; + u8 code; + u8 param1; /* Slot number 0..7 */ + u8 param2; /* Reserved */ + __le16 offset; + __le16 length; /* Note 0xFFFF and offset 0 is special value meaning whole chain */ +}; + +struct spdm_certificate_rsp { + u8 version; + u8 code; + u8 param1; /* Slot number 0..7 */ + u8 param2; /* Reserved */ + __le16 portion_length; + __le16 remainder_length; + u8 cert_chain[]; /* Portion Length Long */ +}; + + +#define SPDM_CHALLENGE 0x03 +struct spdm_challenge_req { + u8 version; + u8 code; + u8 param1; /* Slot number 0..7 */ + u8 param2; /* Measurement summary hash type */ + u8 nonce[32]; +}; + +struct spdm_challenge_rsp { + u8 version; + u8 code; + u8 param1; /* response attribute field, slot id from challenge, bit 7 is mutual auth */ + u8 param2; /* slot mask */ + /* Hash length cert chain */ + /* Nonce, 32 bytes */ + /* Measurement Summary Hash - if present */ + /* 2 byte opaque length */ + /* opaque data if length non 0 */ + /* Signature */ +}; + +static size_t spdm_challenge_rsp_signature_offset(struct spdm_state *spdm_state, + struct spdm_challenge_req *req, + struct spdm_challenge_rsp *rsp) +{ + u16 opaque_length; + size_t offset; + + offset = sizeof(*rsp); /* Header offset */ + offset += spdm_state->h; /* CertChain hash */ + offset += 32; /* Nonce */ + + /* Measurement summary hash */ + if (req->param2 && + (spdm_state->responder_caps & SPDM_GET_CAP_FLAG_MAC_CAP)) + offset += spdm_state->h; + /* + * This is almost certainly aligned, but that's not obvious from nearby code + * so play safe. + */ + opaque_length = get_unaligned_le16((u8 *)rsp + offset); + offset += sizeof(__le16); + offset += opaque_length; + + return offset; +} + +#define SPDM_ERROR 0x7f +enum spdm_error_code { + spdm_invalid_request = 0x01, + spdm_invalid_session = 0x02, + spdm_busy = 0x03, + spdm_unexpected_request = 0x04, + spdm_unspecified = 0x05, + spdm_decrypt_error = 0x06, + spdm_unsupported_request = 0x07, + spdm_request_in_flight = 0x08, + spdm_invalid_response_code = 0x09, + spdm_session_limit_exceeded = 0x0a, + spdm_major_version_missmatch = 0x41, + spdm_response_not_ready = 0x42, + spdm_request_resync = 0x43, + spdm_vendor_defined_error = 0xff, +}; + +struct spdm_error_rsp { + u8 version; + u8 code; + u8 param1; /* Error code */ + u8 param2; /* Error data */ + u8 extended_error_data[]; +}; +#define SPDM_ERROR_MIN_SIZE sizeof(struct spdm_error_rsp) + +static void spdm_err(struct device *dev, enum spdm_error_code error_code, + u8 error_data) +{ + switch (error_code) { + case spdm_invalid_request: + dev_err(dev, "Invalid Request\n"); + break; + case spdm_invalid_session: + dev_err(dev, "Invalid Session %#x\n", error_data); + break; + case spdm_busy: + dev_err(dev, "Busy\n"); + break; + case spdm_unexpected_request: + dev_err(dev, "Unexpected request\n"); + break; + case spdm_unspecified: + dev_err(dev, "Unspecified\n"); + break; + case spdm_decrypt_error: + dev_err(dev, "Decrypt Error\n"); + break; + case spdm_unsupported_request: + dev_err(dev, "Unsupported Request %#x\n", error_data); + break; + case spdm_request_in_flight: + dev_err(dev, "Request in flight\n"); + break; + case spdm_invalid_response_code: + dev_err(dev, "Invalid response code\n"); + break; + case spdm_session_limit_exceeded: + dev_err(dev, "Session limit exceeded\n"); + break; + case spdm_major_version_missmatch: + dev_err(dev, "Major version mismatch\n"); + break; + case spdm_response_not_ready: + dev_err(dev, "Response not ready\n"); + break; + case spdm_request_resync: + dev_err(dev, "Request resynchronization\n"); + break; + case spdm_vendor_defined_error: + dev_err(dev, "Vendor defined error\n"); + break; + } +} + +static int __spdm_exchange(struct spdm_state *spdm_state, struct spdm_exchange *ex, u8 version) +{ + int length; + int rc; + + if (ex->request_pl_sz < sizeof(*ex->request_pl) || + ex->response_pl_sz < sizeof(*ex->response_pl)) + return -EINVAL; + + ex->request_pl->version = version; + ex->request_pl->code = SPDM_REQ | ex->code; + + /* Will become an op pointer if we have a second transport */ + rc = spdm_state->transport_ex(spdm_state->transport_priv, ex); + if (rc < 0) + return rc; + + length = rc; + if (length < SPDM_ERROR_MIN_SIZE) + return -EIO; + + if (ex->response_pl->code == SPDM_ERROR) { + spdm_err(spdm_state->dev, ex->response_pl->param1, + ex->response_pl->param2); + return -EIO; + } + + if (ex->response_pl->code != ex->code) { + dev_err(spdm_state->dev, + "Invalid SPDM response received - does not match request code\n"); + return -EIO; + } + + return length; +} + +static int spdm1p0_exchange(struct spdm_state *spdm_state, struct spdm_exchange *ex) +{ + return __spdm_exchange(spdm_state, ex, 0x10); +} + +static int spdm1p1_exchange(struct spdm_state *spdm_state, struct spdm_exchange *ex) +{ + return __spdm_exchange(spdm_state, ex, 0x11); +} + +static int spdm_get_version_1p1(struct spdm_state *spdm_state) +{ + struct spdm_get_version_req req = {}; + struct spdm_get_version_rsp *rsp; + struct spdm_exchange spdm_ex; + ssize_t rc, rsp_sz, length; + int numversions = 2; + +retry: + rsp_sz = struct_size(rsp, version_number_entries, numversions); + rsp = kzalloc(rsp_sz, GFP_KERNEL); + + spdm_ex = (struct spdm_exchange) { + .request_pl = (struct spdm_header *)&req, + .request_pl_sz = sizeof(req), + .response_pl = (struct spdm_header *)rsp, + .response_pl_sz = rsp_sz, + .code = SPDM_GET_VERSION, + }; + rc = spdm1p0_exchange(spdm_state, &spdm_ex); + if (rc < 0) + goto err; + + length = rc; + + if (length < struct_size(rsp, version_number_entries, 1)) { + dev_err(spdm_state->dev, "SPDM must support at least one version\n"); + rc = -EIO; + goto err; + } + + /* If we didn't allocate enough space the first time, go around again */ + if (rsp->version_number_entry_count > numversions) { + numversions = rsp->version_number_entry_count; + kfree(rsp); + goto retry; + } + + /* + * Cache the request and response to use later to compute a message digest, + * as the algorithm to use is not yet known. + * + * Response may be padded, so we need to compute the length rather than relying + * on the length returned by the transport. + */ + length = struct_size(rsp, version_number_entries, rsp->version_number_entry_count); + rc = spdm_append_buffer_a(spdm_state, &req, sizeof(req), true); + if (rc) + goto err; + + rc = spdm_append_buffer_a(spdm_state, rsp, length, false); + +err: + kfree(rsp); + + return rc; +} + + +static int spdm_negotiate_caps(struct spdm_state *spdm_state, u32 req_caps, + u32 *rsp_caps) +{ + struct spdm_get_capabilities_reqrsp req = { + .ctexponent = 2, /* FIXME: Chose sensible value */ + .flags = cpu_to_le32(req_caps), + }; + struct spdm_get_capabilities_reqrsp rsp; + struct spdm_exchange spdm_ex = { + .request_pl = (struct spdm_header *)&req, + .request_pl_sz = sizeof(req), + .response_pl = (struct spdm_header *)&rsp, + .response_pl_sz = sizeof(rsp), + .code = SPDM_GET_CAPABILITIES, + }; + int rc, length; + + rc = spdm1p1_exchange(spdm_state, &spdm_ex); + if (rc < 0) + return rc; + length = rc; + + if (length < sizeof(rsp)) { + dev_err(spdm_state->dev, "NEGOTIATE_CAPS response short\n"); + return -EIO; + } + /* Cache capability as can affect data layout for other messages */ + spdm_state->responder_caps = le32_to_cpu(rsp.flags); + + if (rsp_caps) + *rsp_caps = spdm_state->responder_caps; + + rc = spdm_append_buffer_a(spdm_state, &req, sizeof(req), false); + if (rc) + return rc; + + return spdm_append_buffer_a(spdm_state, &rsp, sizeof(rsp), false); +} + +static int spdm_start_digest(struct spdm_state *spdm_state, + void *req, size_t req_sz, void *rsp, size_t rsp_sz) +{ + int rc; + + /* Build first part of challenge hash */ + switch (spdm_state->base_hash_alg) { + case spdm_base_hash_sha_384: + spdm_state->shash = crypto_alloc_shash("sha384", 0, CRYPTO_ALG_ASYNC); + break; + case spdm_base_hash_sha_256: + spdm_state->shash = crypto_alloc_shash("sha256", 0, CRYPTO_ALG_ASYNC); + break; + default: + /* Given device must support one of the above, lets stick to them for now */ + return -EINVAL; + } + + if (!spdm_state->shash) + return -ENOMEM; + + /* Used frequently to compute offsets, so cache H */ + spdm_state->h = crypto_shash_digestsize(spdm_state->shash); + + spdm_state->desc = kzalloc(struct_size(spdm_state->desc, __ctx, + crypto_shash_descsize(spdm_state->shash)), + GFP_KERNEL); + if (!spdm_state->desc) { + rc = -ENOMEM; + goto err_free_shash; + } + spdm_state->desc->tfm = spdm_state->shash; + + rc = crypto_shash_init(spdm_state->desc); + if (rc) + goto err_free_desc; + + rc = crypto_shash_update(spdm_state->desc, spdm_state->a, + spdm_state->a_length); + if (rc) + goto err_free_desc; + + rc = crypto_shash_update(spdm_state->desc, (u8 *)req, req_sz); + + if (rc) + goto err_free_desc; + + rc = crypto_shash_update(spdm_state->desc, (u8 *)rsp, rsp_sz); + if (rc) + goto err_free_desc; + + kfree(spdm_state->a); + spdm_state->a = NULL; + spdm_state->a_length = 0; + + return 0; + +err_free_desc: + kfree(spdm_state->desc); +err_free_shash: + crypto_free_shash(spdm_state->shash); + return rc; +} + +static int spdm_negotiate_algs(struct spdm_state *spdm_state) +{ + struct spdm_req_alg_struct *req_alg_struct; + /* + * Current support is focused on enabling PCI/CMA + * + * CMA requires that device must support + * One or more of: + * - RSASSA_3072 + * - ECDSA_ECC_NIST_P256 + * - ECDSA_ECC_NIST_P384 + * One or more of: + * - SHA_256 + * - SHA_384 + */ + struct spdm_negotiate_algs_req *req; + struct spdm_negotiate_algs_rsp *rsp; + struct spdm_exchange spdm_ex; + /* + * There are several variable length elements at the end of these structures. + * Allow for 0 Ext Asym, 0 Ext Hash, 4 ReqAlgStructs + */ + size_t req_sz = sizeof(*req) + 16; + /* + * Response length may be less than this if not all algorithm types supported, + * resulting in fewer AlgStructure fields than in the request. + */ + size_t rsp_sz = sizeof(*rsp) + 16; + size_t length; + int rc; + + req = kzalloc(req_sz, GFP_KERNEL); + if (!req) + return -ENOMEM; + + req->param1 = 4; + req->code = SPDM_NEGOTIATE_ALGS | SPDM_REQ; + req->length = cpu_to_le16(req_sz); + req->measurement_specification = BIT(0); + req->base_asym_algo = cpu_to_le16(BIT(spdm_asym_rsassa_3072) | + BIT(spdm_asym_ecdsa_ecc_nist_p256) | + BIT(spdm_asym_ecdsa_ecc_nist_p384)); + req->base_hash_algo = cpu_to_le16(BIT(spdm_base_hash_sha_256) | + BIT(spdm_base_hash_sha_384)); + + req->ext_asm_count = 0; + req->ext_hash_count = 0; + req_alg_struct = (struct spdm_req_alg_struct *)(req + 1); + /* + * For CMA only we don't need to support DHE, AEAD or Key Schedule - but the + * responder may reply with them anyway. + * TODO: Identify minimum set needed. + */ + req_alg_struct[0] = (struct spdm_req_alg_struct) { + .alg_type = SPDM_REQ_ALG_STRUCT_TYPE_DHE, + .alg_count = 0x20, + .alg_supported = cpu_to_le16(SPDM_DHE_ALGO_FFDHE_2048 | + SPDM_DHE_ALGO_FFDHE_3072 | + SPDM_DHE_ALGO_SECP_256R1 | + SPDM_DHE_ALGO_SECP_384R1) + }; + req_alg_struct[1] = (struct spdm_req_alg_struct) { + .alg_type = SPDM_REQ_ALG_STRUCT_TYPE_AEAD_CIPHER_SUITE, + .alg_count = 0x20, + .alg_supported = cpu_to_le16(SPDM_AEAD_ALGO_AES_256_GCM | + SPDM_AEAD_ALGO_CHACHA20_POLY1305) + }; + req_alg_struct[2] = (struct spdm_req_alg_struct) { + .alg_type = SPDM_REQ_ALG_STRUCT_TYPE_REQ_BASE_ASYM_ALG, + .alg_count = 0x20, + .alg_supported = cpu_to_le16(BIT(spdm_asym_rsassa_3072) | + BIT(spdm_asym_ecdsa_ecc_nist_p256) | + BIT(spdm_asym_ecdsa_ecc_nist_p384)), + }; + req_alg_struct[3] = (struct spdm_req_alg_struct) { + .alg_type = SPDM_REQ_ALG_STRUCT_TYPE_KEY_SCHEDULE, + .alg_count = 0x20, + .alg_supported = cpu_to_le16(SPDM_KEY_SCHEDULE_SPDM) + }; + + rsp = kzalloc(rsp_sz, GFP_KERNEL); + if (!rsp) { + rc = -ENOMEM; + goto err_free_req; + } + + spdm_ex = (struct spdm_exchange) { + .request_pl = (struct spdm_header *)req, + .request_pl_sz = req_sz, + .response_pl = (struct spdm_header *)rsp, + .response_pl_sz = rsp_sz, + .code = SPDM_NEGOTIATE_ALGS, + }; + + rc = spdm1p1_exchange(spdm_state, &spdm_ex); + if (rc < 0) + goto err_free_rsp; + length = rc; + + //TODO: Check this cannot return short. + if (length < rsp_sz) { + dev_err(spdm_state->dev, "Response too short\n"); + rc = -EIO; + goto err_free_rsp; + } + + spdm_state->measurement_hash_alg = __ffs(le16_to_cpu(rsp->measurement_hash_algo)); + spdm_state->base_asym_alg = __ffs(le16_to_cpu(rsp->base_asym_sel)); + spdm_state->base_hash_alg = __ffs(le16_to_cpu(rsp->base_hash_sel)); + + switch (spdm_state->base_asym_alg) { + case spdm_asym_rsassa_3072: + spdm_state->s = 384; + break; + case spdm_asym_ecdsa_ecc_nist_p256: + spdm_state->s = 64; + break; + case spdm_asym_ecdsa_ecc_nist_p384: + spdm_state->s = 96; + break; + default: + dev_err(spdm_state->dev, "Unknown async base algorithm\n"); + rc = -EINVAL; + goto err_free_rsp; + } + + rsp_sz = sizeof(*rsp) + rsp->param1 * sizeof(struct spdm_req_alg_struct); + rc = spdm_start_digest(spdm_state, req, req_sz, rsp, rsp_sz); + +err_free_rsp: + kfree(rsp); +err_free_req: + kfree(req); + + return rc; +} + +static int spdm_get_digests(struct spdm_state *spdm_state) +{ + struct spdm_digest_req req = {}; + struct spdm_digest_rsp *rsp; + struct spdm_exchange spdm_ex; + unsigned long slot_bm; + int rc; + /* Assume all slots may be present */ + size_t rsp_sz; + + rsp_sz = struct_size(rsp, digests, spdm_state->h * 8); + rsp = kzalloc(rsp_sz, GFP_KERNEL); + if (!rsp) + return -ENOMEM; + + spdm_ex = (struct spdm_exchange) { + .request_pl = (struct spdm_header *)&req, + .request_pl_sz = sizeof(req), + .response_pl = (struct spdm_header *)rsp, + .response_pl_sz = rsp_sz, + .code = SPDM_GET_DIGESTS, + }; + + rc = spdm1p1_exchange(spdm_state, &spdm_ex); + if (rc < 0) + return rc; + + rc = crypto_shash_update(spdm_state->desc, (u8 *)&req, sizeof(req)); + if (rc) + goto err_free_rsp; + + slot_bm = rsp->param2; + rsp_sz = struct_size(rsp, digests, + spdm_state->h * + bitmap_weight(&slot_bm, 8 * sizeof(rsp->param2))); + + rc = crypto_shash_update(spdm_state->desc, (u8 *)rsp, rsp_sz); + +err_free_rsp: + kfree(rsp); + + return rc; +} + +/* Used to give a unique name for per device keychains */ +static DEFINE_IDA(spdm_ida); + +static int spdm_get_certificate(struct spdm_state *spdm_state) +{ + //TODO: Test this. + /* This should ensure the limitation is rarely at the requester */ + u16 bufsize = 0x8000; + struct spdm_certificate_req req = { + .param1 = 0, /* Slot 0 */ + }; + struct spdm_certificate_rsp *rsp; + size_t rsp_sz; + struct spdm_exchange spdm_ex; + u16 remainder_length = bufsize; + u16 offset = 0; + char *keyring_name; + int keyring_id; + u8 *certs = NULL; + u16 certs_length = 0; + u16 next_cert; + int rc; + + rsp_sz = struct_size(rsp, cert_chain, bufsize); + rsp = kzalloc(rsp_sz, GFP_KERNEL); + if (!rsp) + return -ENOMEM; + + spdm_ex = (struct spdm_exchange) { + .request_pl = (struct spdm_header *)&req, + .request_pl_sz = sizeof(req), + .response_pl = (struct spdm_header *)rsp, + .response_pl_sz = rsp_sz, + .code = SPDM_GET_CERTIFICATE, + }; + + while (remainder_length > 0) { + u8 *newcerts; + + req.length = min(remainder_length, bufsize); + req.offset = offset; + + rc = spdm1p1_exchange(spdm_state, &spdm_ex); + if (rc < 0) + goto err_free_certs; + + /* + * Taking the hash of each message was established by looking at + * what openSPDM does, rather than it being clear from the SPDM + * specification. + */ + rc = crypto_shash_update(spdm_state->desc, (u8 *)&req, sizeof(req)); + if (rc) + goto err_free_certs; + + /* Care needed - each message might not be full length due to padding */ + rc = crypto_shash_update(spdm_state->desc, (u8 *)rsp, + sizeof(rsp) + rsp->portion_length); + if (rc) + goto err_free_certs; + + certs_length += rsp->portion_length; + newcerts = krealloc(certs, certs_length, GFP_KERNEL); + if (!newcerts) { + rc = -ENOMEM; + goto err_free_certs; + } + certs = newcerts; + memcpy(certs + offset, rsp->cert_chain, rsp->portion_length); + offset += rsp->portion_length; + remainder_length = rsp->remainder_length; + } + + keyring_id = ida_alloc(&spdm_ida, GFP_KERNEL); + if (keyring_id < 0) { + rc = keyring_id; + goto err_free_certs; + + } + + keyring_name = kasprintf(GFP_KERNEL, "_spdm%02d", keyring_id); + if (!keyring_name) { + rc = -ENOMEM; + goto err_free_ida; + } + + /* + * Create a spdm instance specific keyring to avoid mixing certs, + * Not a child of _cma keyring, because the search below should + * not find a self signed cert in here. + * + * Not sure how to release a keyring, so currently if this fails we leak. + * That might be fine but an ida could get reused. + */ + spdm_state->keyring = keyring_alloc(keyring_name, + KUIDT_INIT(0), KGIDT_INIT(0), + current_cred(), + (KEY_POS_ALL & ~KEY_POS_SETATTR) | + KEY_USR_VIEW | KEY_USR_READ, + KEY_ALLOC_NOT_IN_QUOTA | + KEY_ALLOC_SET_KEEP, + NULL, NULL); + kfree(keyring_name); + if (IS_ERR(spdm_state->keyring)) { + dev_err(spdm_state->dev, + "Failed to allocate per spdm keyring\n"); + rc = PTR_ERR(spdm_state->keyring); + goto err_free_ida; + } + + next_cert = sizeof(struct spdm_cert_chain) + spdm_state->h; + + /* + * Store the certificate chain on the per SPDM instance keyring. + * Allow for up to 3 bytes padding as transport sends multiples of 4 bytes. + */ + while (next_cert < offset) { + struct key *key; + key_ref_t key2; + + key2 = key_create_or_update(make_key_ref(spdm_state->keyring, 1), + "asymmetric", NULL, + certs + next_cert, offset - next_cert, + (KEY_POS_ALL & ~KEY_POS_SETATTR) | + KEY_USR_VIEW | KEY_USR_READ, + KEY_ALLOC_NOT_IN_QUOTA); + + if (IS_ERR(key2)) { + /* FIXME: Any additional cleanup to do here? */ + rc = PTR_ERR(key2); + goto err_free_ida; + } + + if (!spdm_state->leaf_key) { + /* First key in chain, so check against keys on _cma keyring */ + struct public_key_signature *sig = + key_ref_to_ptr(key2)->payload.data[asym_auth]; + + key = find_asymmetric_key(spdm_state->root_keyring, sig->auth_ids[0], + sig->auth_ids[1], false); + if (IS_ERR(key)) { + dev_err(spdm_state->dev, + "Unable to retrieve signing certificate from _cma keyring\n"); + rc = PTR_ERR(key); + goto err_free_ida; + } + + rc = verify_signature(key, sig); + if (rc) { + dev_err(spdm_state->dev, + "Unable to check SPDM cert against _cma keyring\n"); + goto err_free_ida; + } + + spdm_state->leaf_key = key_ref_to_ptr(key2); + } else { + /* Not the first key in chain, so check it against previous one */ + struct public_key_signature *sig = + key_ref_to_ptr(key2)->payload.data[asym_auth]; + + rc = verify_signature(spdm_state->leaf_key, sig); + if (rc) { + dev_err(spdm_state->dev, + "Unable to verify SPDM cert against previous cert in chain\n"); + goto err_free_ida; + } + spdm_state->leaf_key = key_ref_to_ptr(key2); + } + /* + * Horrible but need to pull this directly from the ASN1 stream as the cert + * chain is a concatentation of multiple cerificates. + */ + next_cert += get_unaligned_be16(certs + next_cert + 2) + 4; + } + + kfree(certs); + kfree(rsp); + + return 0; + +err_free_ida: + ida_free(&spdm_ida, keyring_id); +err_free_certs: + kfree(certs); + kfree(rsp); + + return rc; +} + + +static int spdm_verify_signature(struct spdm_state *spdm_state, u8 *sig_ptr, + u8 *digest, unsigned int digest_size) +{ + const struct asymmetric_key_ids *ids; + struct public_key_signature sig = {}; + /* Large enough for an ASN1 enocding of supported ECC signatures */ + unsigned char buffer2[128] = {}; + int rc; + + /* + * The ecdsa signatures are raw concatentation of the two values. + * In order to use verify_signature we need to reformat them into ASN1. + */ + switch (spdm_state->base_asym_alg) { + case spdm_asym_ecdsa_ecc_nist_p256: + case spdm_asym_ecdsa_ecc_nist_p384: + { + unsigned char buffer[128] = {}; + unsigned char *p = buffer; + unsigned char *p2; + + //TODO: test the ASN1 function rather more extensively. + /* First pack the two large integer values */ + p = asn1_encode_integer_large_positive(p, buffer + sizeof(buffer), + ASN1_INT, sig_ptr, + spdm_state->s / 2); + p = asn1_encode_integer_large_positive(p, buffer + sizeof(buffer), + ASN1_INT, + sig_ptr + spdm_state->s / 2, + spdm_state->s / 2); + + /* In turn pack those two large integer values into a sequence */ + p2 = asn1_encode_sequence(buffer2, buffer2 + sizeof(buffer2), + buffer, p - buffer); + + sig.s = buffer2; + sig.s_size = p2 - buffer2; + sig.encoding = "raw"; + break; + } + + case spdm_asym_rsassa_3072: + sig.s = sig_ptr; + sig.s_size = spdm_state->s; + sig.encoding = "pkcs1"; + break; + default: + dev_err(spdm_state->dev, + "Signature algorithm not yet supported\n"); + return -EINVAL; + } + sig.digest = digest; + sig.digest_size = digest_size; + ids = asymmetric_key_ids(spdm_state->leaf_key); + sig.auth_ids[0] = ids->id[0]; + sig.auth_ids[1] = ids->id[1]; + + switch (spdm_state->base_hash_alg) { + case spdm_base_hash_sha_384: + sig.hash_algo = "sha384"; + break; + case spdm_base_hash_sha_256: + sig.hash_algo = "sha256"; + break; + default: + return -EINVAL; + } + + rc = verify_signature(spdm_state->leaf_key, &sig); + if (rc) { + dev_err(spdm_state->dev, + "Failed to verify challenge_auth signature %d\n", rc); + return rc; + } + + return 0; +} + +static int spdm_challenge(struct spdm_state *spdm_state) +{ + struct spdm_challenge_req req = { + .param1 = 0, /* slot 0 for now */ + .param2 = 0, /* no measurement summary hash */ + }; + struct spdm_challenge_rsp *rsp; + struct spdm_exchange spdm_ex; + size_t sig_offset, rsp_max_size; + int length, rc; + u8 *digest; + + /* + * The response length is up to: + * 4 byte header + * H byte CertChainHash + * 32 byte nonce + * (H byte Measurement Summary Hash - not currently requested) + * 2 byte Opaque Length + * <= 1024 bytes Opaque Data + * S byte signature + */ + rsp_max_size = 4 + spdm_state->h + 32 + 2 + 1024 + spdm_state->s; + rsp = kzalloc(rsp_max_size, GFP_KERNEL); + if (!rsp) + return -ENOMEM; + + get_random_bytes(&req.nonce, sizeof(req.nonce)); + + spdm_ex = (struct spdm_exchange) { + .request_pl = (struct spdm_header *)&req, + .request_pl_sz = sizeof(req), + .response_pl = (struct spdm_header *)rsp, + .response_pl_sz = rsp_max_size, + .code = SPDM_CHALLENGE, + }; + + rc = spdm1p1_exchange(spdm_state, &spdm_ex); + if (rc < 0) + goto err_free_rsp; + length = rc; + + /* Last step of building the digest */ + rc = crypto_shash_update(spdm_state->desc, (u8 *)&req, sizeof(req)); + if (rc) + goto err_free_rsp; + + /* The hash is complete + signature received; verify against leaf key */ + sig_offset = spdm_challenge_rsp_signature_offset(spdm_state, &req, rsp); + if (sig_offset >= length) { + rc = -EIO; + goto err_free_rsp; + } + + rc = crypto_shash_update(spdm_state->desc, (u8 *)rsp, sig_offset); + if (rc) + goto err_free_rsp; + + digest = kmalloc(spdm_state->h, GFP_KERNEL); + if (!digest) { + rc = -ENOMEM; + goto err_free_rsp; + } + + crypto_shash_final(spdm_state->desc, digest); + + rc = spdm_verify_signature(spdm_state, (u8 *)rsp + sig_offset, digest, + spdm_state->h); + if (rc) { + dev_err(spdm_state->dev, "Failed to verify SPDM challenge auth signature\n"); + goto err_free_digest; + } + + kfree(spdm_state->desc); + + /* Clear to give a simple way to detect out of order */ + spdm_state->desc = NULL; + +err_free_digest: + kfree(digest); + +err_free_rsp: + kfree(rsp); + return rc; +} + +int spdm_authenticate(struct spdm_state *spdm_state) +{ + u32 req_caps, rsp_caps; + int rc; + + rc = spdm_get_version_1p1(spdm_state); + if (rc) + return rc; + + /* TODO: work out if a subset of these is fine for CMA */ + req_caps = SPDM_GET_CAP_FLAG_CERT_CAP | + SPDM_GET_CAP_FLAG_CHAL_CAP | + SPDM_GET_CAP_FLAG_ENCRYPT_CAP | + SPDM_GET_CAP_FLAG_MAC_CAP | + SPDM_GET_CAP_FLAG_MUT_AUTH_CAP | + SPDM_GET_CAP_FLAG_KEY_EX_CAP | + FIELD_PREP(SPDM_GET_CAP_FLAG_PSK_CAP_MSK, SPDM_GET_CAP_FLAG_PSK_CAP_PRESHARE) | + SPDM_GET_CAP_FLAG_ENCAP_CAP | + SPDM_GET_CAP_FLAG_HBEAT_CAP | + SPDM_GET_CAP_FLAG_KEY_UPD_CAP | + SPDM_GET_CAP_FLAG_HANDSHAKE_ITC_CAP; + + rc = spdm_negotiate_caps(spdm_state, req_caps, &rsp_caps); + if (rc) + return rc; + + rc = spdm_negotiate_algs(spdm_state); + if (rc) + return rc; + + /* At this point we know the hash so can start calculating it as we go */ + rc = spdm_get_digests(spdm_state); + if (rc) + goto err_free_hash; + + rc = spdm_get_certificate(spdm_state); + if (rc) + goto err_free_hash; + + rc = spdm_challenge(spdm_state); + if (rc) + goto err_free_hash; + + /* + * If we get to here, we have successfully verified the device is one we are happy + * with using. + */ + return 0; + +err_free_hash: + kfree(spdm_state->desc); + crypto_free_shash(spdm_state->shash); + + return rc; +} +EXPORT_SYMBOL_GPL(spdm_authenticate); + +MODULE_LICENSE("GPL v2"); From patchwork Wed Aug 4 16:18:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Cameron X-Patchwork-Id: 12419425 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A29E9C4320A for ; Wed, 4 Aug 2021 16:23:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 833D260F41 for ; Wed, 4 Aug 2021 16:23:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237349AbhHDQXg (ORCPT ); Wed, 4 Aug 2021 12:23:36 -0400 Received: from frasgout.his.huawei.com ([185.176.79.56]:3580 "EHLO frasgout.his.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234482AbhHDQXg (ORCPT ); Wed, 4 Aug 2021 12:23:36 -0400 Received: from fraeml744-chm.china.huawei.com (unknown [172.18.147.206]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4GfxqZ3XzTz6GFV7; Thu, 5 Aug 2021 00:23:06 +0800 (CST) Received: from lhreml710-chm.china.huawei.com (10.201.108.61) by fraeml744-chm.china.huawei.com (10.206.15.225) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Wed, 4 Aug 2021 18:23:21 +0200 Received: from localhost.localdomain (10.123.41.22) by lhreml710-chm.china.huawei.com (10.201.108.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Wed, 4 Aug 2021 17:23:20 +0100 From: Jonathan Cameron To: , CC: , , Chris Browy , , Lorenzo Pieralisi , Bjorn Helgaas , Jeremy Kerr , Jonathan Cameron Subject: [RFC PATCH 3/4] PCI/CMA: Initial support for Component Measurement and Authentication ECN Date: Thu, 5 Aug 2021 00:18:38 +0800 Message-ID: <20210804161839.3492053-4-Jonathan.Cameron@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20210804161839.3492053-1-Jonathan.Cameron@huawei.com> References: <20210804161839.3492053-1-Jonathan.Cameron@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.123.41.22] X-ClientProxiedBy: lhreml754-chm.china.huawei.com (10.201.108.204) To lhreml710-chm.china.huawei.com (10.201.108.61) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-cxl@vger.kernel.org This currently very much a PoC. Currently the SPDM library only provides a single function to allow a challenge / authentication of the PCI EP. SPDM exchanges must occur in one of a small set of valid squences over which the message digest used in authentication is built up. Placing that complexity in the SPDM library seems like a good way to enforce that logic, without having to do it for each transport. Signed-off-by: Jonathan Cameron Signed-off-by: Jonathan Cameron --- drivers/pci/Kconfig | 9 +++++++++ drivers/pci/Makefile | 1 + drivers/pci/doe.c | 2 -- include/linux/pci-doe.h | 2 ++ 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/drivers/pci/Kconfig b/drivers/pci/Kconfig index a30c59cf5e27..43e3b0d5e8cd 100644 --- a/drivers/pci/Kconfig +++ b/drivers/pci/Kconfig @@ -198,6 +198,15 @@ config PCI_DOE used by a number of different protocols. DOE is defined in the Data Object Exchange ECN to the PCIe r5.0 spec. +config PCI_CMA + tristate + select PCI_DOE + select ASN1_ENCODER + select SPDM + help + This enables library support for the PCI Component Measurement and + Authentication ECN. This uses DMTF SPDM 1.1 + choice prompt "PCI Express hierarchy optimization setting" default PCIE_BUS_DEFAULT diff --git a/drivers/pci/Makefile b/drivers/pci/Makefile index 1b61c1a1c232..3f6b3543d565 100644 --- a/drivers/pci/Makefile +++ b/drivers/pci/Makefile @@ -29,6 +29,7 @@ obj-$(CONFIG_PCI_PF_STUB) += pci-pf-stub.o obj-$(CONFIG_PCI_ECAM) += ecam.o obj-$(CONFIG_PCI_P2PDMA) += p2pdma.o obj-$(CONFIG_PCI_DOE) += doe.o +obj-$(CONFIG_PCI_CMA) += cma.o obj-$(CONFIG_XEN_PCIDEV_FRONTEND) += xen-pcifront.o # Endpoint library must be initialized before its users diff --git a/drivers/pci/doe.c b/drivers/pci/doe.c index 2d20f59e42c6..f6aaeed01010 100644 --- a/drivers/pci/doe.c +++ b/drivers/pci/doe.c @@ -20,8 +20,6 @@ /* Maximum number of DOE instances in the system */ #define PCI_DOE_MAX_CNT 65536 -#define PCI_DOE_PROTOCOL_DISCOVERY 0 - #define PCI_DOE_BUSY_MAX_RETRIES 16 #define PCI_DOE_POLL_INTERVAL (HZ / 128) diff --git a/include/linux/pci-doe.h b/include/linux/pci-doe.h index bdc5f15f14ab..1347c124ed70 100644 --- a/include/linux/pci-doe.h +++ b/include/linux/pci-doe.h @@ -19,6 +19,8 @@ struct pci_doe_prot { u8 type; }; +#define PCI_DOE_PROTOCOL_DISCOVERY 0 +#define PCI_DOE_PROTOCOL_CMA 1 struct workqueue_struct; enum pci_doe_state { From patchwork Wed Aug 4 16:18:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Cameron X-Patchwork-Id: 12419427 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A1F4EC432BE for ; Wed, 4 Aug 2021 16:23:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 804C260FC4 for ; Wed, 4 Aug 2021 16:23:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236406AbhHDQYG (ORCPT ); Wed, 4 Aug 2021 12:24:06 -0400 Received: from frasgout.his.huawei.com ([185.176.79.56]:3581 "EHLO frasgout.his.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234482AbhHDQYG (ORCPT ); Wed, 4 Aug 2021 12:24:06 -0400 Received: from fraeml743-chm.china.huawei.com (unknown [172.18.147.200]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4Gfxr86VKVz6GFTw; Thu, 5 Aug 2021 00:23:36 +0800 (CST) Received: from lhreml710-chm.china.huawei.com (10.201.108.61) by fraeml743-chm.china.huawei.com (10.206.15.224) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Wed, 4 Aug 2021 18:23:51 +0200 Received: from localhost.localdomain (10.123.41.22) by lhreml710-chm.china.huawei.com (10.201.108.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Wed, 4 Aug 2021 17:23:51 +0100 From: Jonathan Cameron To: , CC: , , Chris Browy , , Lorenzo Pieralisi , Bjorn Helgaas , Jeremy Kerr , Jonathan Cameron Subject: [RFC PATCH 4/4] cxl/pci: Add really basic CMA authentication support. Date: Thu, 5 Aug 2021 00:18:39 +0800 Message-ID: <20210804161839.3492053-5-Jonathan.Cameron@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20210804161839.3492053-1-Jonathan.Cameron@huawei.com> References: <20210804161839.3492053-1-Jonathan.Cameron@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.123.41.22] X-ClientProxiedBy: lhreml754-chm.china.huawei.com (10.201.108.204) To lhreml710-chm.china.huawei.com (10.201.108.61) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-cxl@vger.kernel.org This is just for purposes of poking the CMA / SPDM code. What exactly the model in the driver looks like is still to be worked out. Note the PROBE_FORCE_SYNCHRONOUS is a workaround to avoid warnings about trying to load an additional crypto module whilst doing an asychronous probe. Signed-off-by: Jonathan Cameron --- drivers/cxl/Kconfig | 1 + drivers/cxl/mem.h | 2 ++ drivers/cxl/pci.c | 13 ++++++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/drivers/cxl/Kconfig b/drivers/cxl/Kconfig index e2bca26eb879..c726cc9adddb 100644 --- a/drivers/cxl/Kconfig +++ b/drivers/cxl/Kconfig @@ -17,6 +17,7 @@ config CXL_MEM tristate "CXL.mem: Memory Devices" default CXL_BUS select PCI_DOE + select PCI_CMA help The CXL.mem protocol allows a device to act as a provider of "System RAM" and/or "Persistent Memory" that is fully coherent diff --git a/drivers/cxl/mem.h b/drivers/cxl/mem.h index f626aa7eb389..cb8c2dfda277 100644 --- a/drivers/cxl/mem.h +++ b/drivers/cxl/mem.h @@ -57,6 +57,7 @@ struct cxl_memdev { * @pdev: The PCI device associated with this CXL device. * @cxlmd: Logical memory device chardev / interface * @table_doe: Data exchange object mailbox used to read tables + * @cma_doe: Component measurement and authentication mailbox * @regs: Parsed register blocks * @payload_size: Size of space for payload * (CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register) @@ -73,6 +74,7 @@ struct cxl_mem { struct cxl_memdev *cxlmd; struct pci_doe *table_doe; + struct pci_doe *cma_doe; struct cxl_regs regs; size_t payload_size; diff --git a/drivers/cxl/pci.c b/drivers/cxl/pci.c index 471bddc9d167..e6ca84cc6fff 100644 --- a/drivers/cxl/pci.c +++ b/drivers/cxl/pci.c @@ -2,10 +2,12 @@ /* Copyright(c) 2020 Intel Corporation. All rights reserved. */ #include #include +#include #include #include #include #include +#include #include #include #include @@ -1723,6 +1725,7 @@ static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id) { struct cxl_memdev *cxlmd; struct cxl_mem *cxlm; + struct spdm_state spdm_state; int rc, irqs; rc = pcim_enable_device(pdev); @@ -1770,6 +1773,14 @@ static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id) cxlm->table_doe = pci_doe_find(pdev, PCI_DVSEC_VENDOR_ID_CXL, CXL_DOE_PROTOCOL_TABLE_ACCESS); + cxlm->cma_doe = pci_doe_find(pdev, PCI_VENDOR_ID_PCI_SIG, + PCI_DOE_PROTOCOL_CMA); + + pci_cma_init(cxlm->cma_doe, &spdm_state); + rc = pci_cma_authenticate(&spdm_state); + if (rc) + return rc; + rc = cxl_mem_setup_regs(cxlm); if (rc) return rc; @@ -1808,7 +1819,7 @@ static struct pci_driver cxl_mem_driver = { .id_table = cxl_mem_pci_tbl, .probe = cxl_mem_probe, .driver = { - .probe_type = PROBE_PREFER_ASYNCHRONOUS, + .probe_type = PROBE_FORCE_SYNCHRONOUS, }, };