From patchwork Mon Sep 25 11:19:48 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roberto Sassu X-Patchwork-Id: 9969749 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 37D5C60365 for ; Mon, 25 Sep 2017 11:21:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2E40B288E0 for ; Mon, 25 Sep 2017 11:21:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2319728A48; Mon, 25 Sep 2017 11:21:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 88AEA288E0 for ; Mon, 25 Sep 2017 11:21:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933698AbdIYLV3 (ORCPT ); Mon, 25 Sep 2017 07:21:29 -0400 Received: from lhrrgout.huawei.com ([194.213.3.17]:36473 "EHLO lhrrgout.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933306AbdIYLV2 (ORCPT ); Mon, 25 Sep 2017 07:21:28 -0400 Received: from 172.18.7.190 (EHLO LHREML711-CAH.china.huawei.com) ([172.18.7.190]) by lhrrg01-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id DWE29609; Mon, 25 Sep 2017 11:21:25 +0000 (GMT) Received: from roberto-HP-EliteDesk-800-G2-DM-65W.huawei.com (10.204.65.254) by smtpsuk.huawei.com (10.201.108.34) with Microsoft SMTP Server (TLS) id 14.3.301.0; Mon, 25 Sep 2017 12:21:17 +0100 From: Roberto Sassu To: CC: , , , , Roberto Sassu Subject: [PATCH 1/3] tpm: move PCR read code to static function tpm2_pcr_read_common() Date: Mon, 25 Sep 2017 13:19:48 +0200 Message-ID: <20170925111950.21511-2-roberto.sassu@huawei.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170925111950.21511-1-roberto.sassu@huawei.com> References: <20170925111950.21511-1-roberto.sassu@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.204.65.254] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A090201.59C8E6B6.0127, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2013-06-18 04:22:30, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 740020f7595d9a84fa22e82d64a9ea44 Sender: linux-integrity-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP tpm2_pcr_read() copies the digest stored in a PCR to a buffer provided by the caller. However, it does not return the digest size, included in the output from the TPM. Retrieving it would be useful when a TPM algorithm is not known by the crypto subsystem, which the TPM driver currently depends upon. Most of tpm2_pcr_read() code is moved to the static function tpm2_pcr_read_common(), which writes the output of the PCR read to the tpm_buf structure passed as input. tpm2_pcr_read_common() will be called by tpm2_pcr_read(), and by the new function tpm2_init_active_bank_info(), which will store the identifier and the digest size of TPM algorithms in the tpm_chip structure. Signed-off-by: Roberto Sassu --- drivers/char/tpm/tpm2-cmd.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c index e1a41b7..0cad0f6 100644 --- a/drivers/char/tpm/tpm2-cmd.c +++ b/drivers/char/tpm/tpm2-cmd.c @@ -218,6 +218,26 @@ struct tpm2_pcr_read_out { u8 digest[]; } __packed; +static int tpm2_pcr_read_common(struct tpm_chip *chip, int pcr_idx, + enum tpm2_algorithms algo, struct tpm_buf *buf, + char *msg) +{ + u8 pcr_select[TPM2_PCR_SELECT_MIN] = {0}; + + if (pcr_idx >= TPM2_PLATFORM_PCR) + return -EINVAL; + + pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7); + + tpm_buf_append_u32(buf, 1); + tpm_buf_append_u16(buf, algo); + tpm_buf_append_u8(buf, TPM2_PCR_SELECT_MIN); + tpm_buf_append(buf, (const unsigned char *)pcr_select, + sizeof(pcr_select)); + + return tpm_transmit_cmd(chip, NULL, buf->data, PAGE_SIZE, 0, 0, msg); +} + /** * tpm2_pcr_read() - read a PCR value * @chip: TPM chip to use. @@ -231,24 +251,12 @@ int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf) int rc; struct tpm_buf buf; struct tpm2_pcr_read_out *out; - u8 pcr_select[TPM2_PCR_SELECT_MIN] = {0}; - - if (pcr_idx >= TPM2_PLATFORM_PCR) - return -EINVAL; rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_READ); if (rc) return rc; - pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7); - - tpm_buf_append_u32(&buf, 1); - tpm_buf_append_u16(&buf, TPM2_ALG_SHA1); - tpm_buf_append_u8(&buf, TPM2_PCR_SELECT_MIN); - tpm_buf_append(&buf, (const unsigned char *)pcr_select, - sizeof(pcr_select)); - - rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, + rc = tpm2_pcr_read_common(chip, pcr_idx, TPM2_ALG_SHA1, &buf, res_buf ? "attempting to read a pcr value" : NULL); if (rc == 0 && res_buf) { out = (struct tpm2_pcr_read_out *)&buf.data[TPM_HEADER_SIZE];