From patchwork Tue Oct 24 01:15:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 157189 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp1652006vqx; Mon, 23 Oct 2023 18:16:43 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGlQPGnbp5ILHxhHnXQhsK+XF0xJn/4ayVKqkUYbhfDvo8sX0cxMpK721FLColY8Y2Iql67 X-Received: by 2002:a17:902:c404:b0:1c3:ed30:ce0a with SMTP id k4-20020a170902c40400b001c3ed30ce0amr14155406plk.19.1698110203700; Mon, 23 Oct 2023 18:16:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698110203; cv=none; d=google.com; s=arc-20160816; b=KP4+Yay4qO7UDabZEqQaIwcbeGSQRz6DjeUXoFgCVb4H7vxnrKK3OPlj/mqqkPlN5a nhE/JIFoQkJkebLD/xc9rjS+0KO9PeHRnWyEwlnUwsDjCJ+3Z+pFrEOHtjmeFDvnHAl8 BWrMJpdGQjI5jvebYCtEqL1NDeIJl5sJr/MpAvyiy32gQCa9MEHWHzjeBsMtcMlhot/3 5uggGGeUX7u/WG2Uk9EfGzxV4/O3wWTJ/KvS6E9lbE3lL5+pMrSJXJqpKWs7mfPkzQpM VQEvQxn2Yv9pKrjsM021JaBENCV1nlAlT1Opfrnr/bLuvrP7oQ7KtQAq96CdXhnUL/UC 1FgQ== ARC-Message-Signature: i=1; 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=c2dsi8IkCNYKxe3AZB9HJJQh8mhKXIMRzK5AGbu/HdQ=; fh=hkRZg2rYNwmlPOOsQ5JJaWaUyzerXfJVacmHGPqxfLE=; b=VXMh7FlxbH/nN3zs8GUK4SeBtFj7nhf6VR3owxMl2RxJxU/3UC15q8ZQWosTayFO40 L/Z2RbxzIWGXDxDvgtajrOkKAgRS5Ux36H7GbDuOdz6scfc5GSHxqYBXGDTsnABe4B5D Qi4R5n87xDint8oC5wLr5sQM81Ie2WoVwHUMalTNkoqc1ImGtHNRezs2OEVn4463w/QM ui7HMeFtZ38Q5HuSBAP+Dz8coIVggssn0W6PUNJt2K+39/dQjKoUAIZqUHq7VNbPWB8n QDXDEksKkVaV8Nyza8BcB7yMa+hvAkta/FnhVSOpD/n0D3jEZve84srIoKPPXhs140na XT+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BjXY5kMq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id z22-20020a1709028f9600b001c62cfff799si7289284plo.176.2023.10.23.18.16.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 18:16:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BjXY5kMq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 04ACE805CDAB; Mon, 23 Oct 2023 18:16:41 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232099AbjJXBQY (ORCPT + 27 others); Mon, 23 Oct 2023 21:16:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231913AbjJXBQI (ORCPT ); Mon, 23 Oct 2023 21:16:08 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA3F510C7; Mon, 23 Oct 2023 18:16:05 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D6DADC433C8; Tue, 24 Oct 2023 01:16:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1698110165; bh=zqSHAjHVizHo8y4ADA2VLXqP7H3ZRtqJHGeDELzOKIY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BjXY5kMqELBqneL5YwbFpFGvccdAawmMaOKopnAFGEt7NcqBqYV11i1ldpb3gg4WA 3quTfkxUe1q6qAxQHI6+m8F0eLGe8nczuRsSlPVINqlf/UWzhoWswZu3PShI+gvlWB MHzD+uhsErWrRvdKb4+N4NMERfrwLMwJUSMHQJ3MW8LJDOFCWLQGvbG9uvXh0QEAIu LG6oSpyZYGFgkDh0IelZxnAahrRTancEYjtlcrQDFtEv6Ge+XL32Y0ornI94E8WrH2 v762b0kmHVL30E+oUsHhqBtVXWpwtbEPwyKzd7yPwsJ257GAnYYpqoWidvJoOzQUVl wXDwa4A+x47MQ== From: Jarkko Sakkinen To: linux-integrity@vger.kernel.org Cc: keyrings@vger.kernel.org, Jarkko Sakkinen , James Bottomley , William Roberts , Stefan Berger , David Howells , Jason Gunthorpe , Mimi Zohar , Peter Huewe , Mario Limonciello , Julien Gomes , Jerry Snitselaar , linux-kernel@vger.kernel.org (open list) Subject: [PATCH v3 5/6] tpm: Add tpm_buf_read_{u8,u16,u32} Date: Tue, 24 Oct 2023 04:15:23 +0300 Message-ID: <20231024011531.442587-6-jarkko@kernel.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231024011531.442587-1-jarkko@kernel.org> References: <20231024011531.442587-1-jarkko@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email 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 (fry.vger.email [0.0.0.0]); Mon, 23 Oct 2023 18:16:41 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780597605153263296 X-GMAIL-MSGID: 1780597605153263296 Add tpm_buf_read_u8(), tpm_buf_read_u16() and tpm_read_u32() for the sake of more convenient parsing of TPM responses. Signed-off-by: Jarkko Sakkinen --- drivers/char/tpm/tpm-buf.c | 69 ++++++++++++++++++++++++++++++++++++++ include/linux/tpm.h | 3 ++ 2 files changed, 72 insertions(+) diff --git a/drivers/char/tpm/tpm-buf.c b/drivers/char/tpm/tpm-buf.c index f1d92d7e758d..bcd3cbcd9dd9 100644 --- a/drivers/char/tpm/tpm-buf.c +++ b/drivers/char/tpm/tpm-buf.c @@ -124,3 +124,72 @@ void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value) tpm_buf_append(buf, (u8 *)&value2, 4); } EXPORT_SYMBOL_GPL(tpm_buf_append_u32); + +/** + * tpm_buf_read() - Read from a TPM buffer + * @buf: &tpm_buf instance + * @offset: offset within the buffer + * @count: the number of bytes to read + * @output: the output buffer + */ +static void tpm_buf_read(const struct tpm_buf *buf, off_t *offset, size_t count, void *output) +{ + if (*(offset + count) >= buf->length) { + WARN(1, "tpm_buf: overflow\n"); + return; + } + + memcpy(output, &buf->data[*offset], count); + *offset += count; +} + +/** + * tpm_buf_read_u8() - Read 8-bit word from a TPM buffer + * @buf: &tpm_buf instance + * @offset: offset within the buffer + * + * Return: next 8-bit word + */ +u8 tpm_buf_read_u8(const struct tpm_buf *buf, off_t *offset) +{ + u8 value; + + tpm_buf_read(buf, offset, sizeof(value), &value); + + return value; +} +EXPORT_SYMBOL_GPL(tpm_buf_read_u8); + +/** + * tpm_buf_read_u16() - Read 16-bit word from a TPM buffer + * @buf: &tpm_buf instance + * @offset: offset within the buffer + * + * Return: next 16-bit word + */ +u16 tpm_buf_read_u16(const struct tpm_buf *buf, off_t *offset) +{ + u16 value; + + tpm_buf_read(buf, offset, sizeof(value), &value); + + return be16_to_cpu(value); +} +EXPORT_SYMBOL_GPL(tpm_buf_read_u16); + +/** + * tpm_buf_read_u32() - Read 32-bit word from a TPM buffer + * @buf: &tpm_buf instance + * @offset: offset within the buffer + * + * Return: next 32-bit word + */ +u32 tpm_buf_read_u32(const struct tpm_buf *buf, off_t *offset) +{ + u32 value; + + tpm_buf_read(buf, offset, sizeof(value), &value); + + return be32_to_cpu(value); +} +EXPORT_SYMBOL_GPL(tpm_buf_read_u32); diff --git a/include/linux/tpm.h b/include/linux/tpm.h index 687b5173bdab..6590bd1f0a0e 100644 --- a/include/linux/tpm.h +++ b/include/linux/tpm.h @@ -337,6 +337,9 @@ void tpm_buf_append(struct tpm_buf *buf, const u8 *new_data, u16 new_length); void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value); void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value); void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value); +u8 tpm_buf_read_u8(const struct tpm_buf *buf, off_t *offset); +u16 tpm_buf_read_u16(const struct tpm_buf *buf, off_t *offset); +u32 tpm_buf_read_u32(const struct tpm_buf *buf, off_t *offset); /* * Check if TPM device is in the firmware upgrade mode.