From patchwork Wed Dec 11 20:27:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 1207913 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47Y7pg39fBz9sR8 for ; Thu, 12 Dec 2019 07:30:19 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=linux.vnet.ibm.com Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 47Y7pg27BnzDqwN for ; Thu, 12 Dec 2019 07:30:19 +1100 (AEDT) X-Original-To: slof@lists.ozlabs.org Delivered-To: slof@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=linux.vnet.ibm.com (client-ip=148.163.156.1; helo=mx0a-001b2d01.pphosted.com; envelope-from=stefanb@linux.vnet.ibm.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=linux.vnet.ibm.com Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 47Y7lm2zVszDqZq for ; Thu, 12 Dec 2019 07:27:45 +1100 (AEDT) Received: from pps.filterd (m0098394.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id xBBKGjVg065888; Wed, 11 Dec 2019 15:27:42 -0500 Received: from ppma04dal.us.ibm.com (7a.29.35a9.ip4.static.sl-reverse.com [169.53.41.122]) by mx0a-001b2d01.pphosted.com with ESMTP id 2wsm2g0et8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 Dec 2019 15:27:42 -0500 Received: from pps.filterd (ppma04dal.us.ibm.com [127.0.0.1]) by ppma04dal.us.ibm.com (8.16.0.27/8.16.0.27) with SMTP id xBBKIIkA026632; Wed, 11 Dec 2019 20:27:41 GMT Received: from b01cxnp22033.gho.pok.ibm.com (b01cxnp22033.gho.pok.ibm.com [9.57.198.23]) by ppma04dal.us.ibm.com with ESMTP id 2wr3q723hs-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 Dec 2019 20:27:41 +0000 Received: from b01ledav003.gho.pok.ibm.com (b01ledav003.gho.pok.ibm.com [9.57.199.108]) by b01cxnp22033.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id xBBKRent51577308 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 11 Dec 2019 20:27:41 GMT Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D6F2FB205F; Wed, 11 Dec 2019 20:27:40 +0000 (GMT) Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D3942B2066; Wed, 11 Dec 2019 20:27:40 +0000 (GMT) Received: from newfield.pok.ibm.com (unknown [9.47.158.66]) by b01ledav003.gho.pok.ibm.com (Postfix) with ESMTP; Wed, 11 Dec 2019 20:27:40 +0000 (GMT) From: Stefan Berger To: slof@lists.ozlabs.org Date: Wed, 11 Dec 2019 15:27:14 -0500 Message-Id: <20191211202728.127996-20-stefanb@linux.vnet.ibm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191211202728.127996-1-stefanb@linux.vnet.ibm.com> References: <20191211202728.127996-1-stefanb@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.95, 18.0.572 definitions=2019-12-11_06:2019-12-11, 2019-12-11 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 mlxlogscore=999 clxscore=1015 bulkscore=0 mlxscore=0 impostorscore=0 adultscore=0 suspectscore=1 lowpriorityscore=0 priorityscore=1501 spamscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-1910280000 definitions=main-1912110168 Subject: [SLOF] [PATCH v4 19/33] tpm: Implement log related 32 bit endian conversion functions X-BeenThere: slof@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Patches for https://github.com/aik/SLOF" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin@koconnor.net MIME-Version: 1.0 Errors-To: slof-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "SLOF" Implement log related endian conversion functions for 32 bit numbers. This is necessary since TPM 1.2 logs are written in big endian format and TPM 2.0 logs in little endia format. Use the conversion function when we read or write 32 bit numbers to/from the 'pcpes' structure. Signed-off-by Stefan Berger --- lib/libtpm/tcgbios.c | 48 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/lib/libtpm/tcgbios.c b/lib/libtpm/tcgbios.c index 7554bd3..af29406 100644 --- a/lib/libtpm/tcgbios.c +++ b/lib/libtpm/tcgbios.c @@ -62,6 +62,37 @@ typedef uint8_t tpm_ppi_op; #define TPM_version spapr_get_tpm_version() +/* + * TPM 1.2 logs are written in big endian format and TPM 2 logs + * are written in little endian format. + */ +static inline uint32_t log32_to_cpu(uint32_t val) +{ + switch (TPM_version) { + case TPM_VERSION_1_2: + return be32_to_cpu(val); + case TPM_VERSION_2: + return le32_to_cpu(val); + } + return 0; +} + +static inline uint32_t cpu_to_log32(uint32_t val) +{ + switch (TPM_version) { + case TPM_VERSION_1_2: + return cpu_to_be32(val); + case TPM_VERSION_2: + return cpu_to_le32(val); + } + return 0; +} + +static inline bool tpm_log_is_be(void) +{ + return TPM_version == TPM_VERSION_1_2; +} + /******************************************************** Extensions for TCG-enabled BIOS *******************************************************/ @@ -256,7 +287,7 @@ static uint32_t tpm_log_event_long(struct pcpes *pcpes, return TCGBIOS_LOGOVERFLOW; } - pcpes->eventdatasize = event_length; + pcpes->eventdatasize = cpu_to_log32(event_length); memcpy(tpm_state.log_area_next_entry, pcpes, offset_of(struct pcpes, event)); @@ -271,7 +302,7 @@ static uint32_t tpm_log_event_long(struct pcpes *pcpes, bool tpm_log_event(struct pcpes *pcpes) { const char *event = NULL; - uint32_t event_length = pcpes->eventdatasize; + uint32_t event_length = log32_to_cpu(pcpes->eventdatasize); if (event_length) event = (void *)pcpes + offset_of(struct pcpes, event); @@ -411,13 +442,13 @@ static uint32_t hash_log_extend(struct pcpes *pcpes, { int ret; - if (pcpes->pcrindex >= 24) + if (log32_to_cpu(pcpes->pcrindex) >= 24) return TCGBIOS_INVALID_INPUT_PARA; if (hashdata) tpm_hash_all(hashdata, hashdata_length, pcpes->digest); if (extend) { - ret = tpm_extend(pcpes->digest, pcpes->pcrindex); + ret = tpm_extend(pcpes->digest, log32_to_cpu(pcpes->pcrindex)); if (ret) return TCGBIOS_COMMAND_ERROR; } @@ -448,8 +479,8 @@ static uint32_t tpm_add_measurement_to_log(uint32_t pcrindex, { struct pcpes pcpes; - pcpes.pcrindex = pcrindex; - pcpes.eventtype = eventtype; + pcpes.pcrindex = cpu_to_log32(pcrindex); + pcpes.eventtype = cpu_to_log32(eventtype); memset(&pcpes.digest, 0, sizeof(pcpes.digest)); return hash_log_extend(&pcpes, hashdata, hashdatalen, @@ -462,7 +493,7 @@ static uint32_t tpm_add_measurement_to_log(uint32_t pcrindex, uint32_t tpm_hash_log_extend_event(struct pcpes *pcpes) { const char *event = NULL; - uint32_t event_length = pcpes->eventdatasize; + uint32_t event_length = log32_to_cpu(pcpes->eventdatasize); if (!tpm_is_working()) return TCGBIOS_GENERAL_ERROR; @@ -471,7 +502,8 @@ uint32_t tpm_hash_log_extend_event(struct pcpes *pcpes) event = (void *)pcpes + offset_of(struct pcpes, event); return hash_log_extend(pcpes, - &pcpes->event, pcpes->eventdatasize, + &pcpes->event, + log32_to_cpu(pcpes->eventdatasize), event, event_length, true); }