Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp2756498imj; Mon, 11 Feb 2019 08:02:18 -0800 (PST) X-Google-Smtp-Source: AHgI3IYWgDNd0Z02qXxEDhr24HbQTnouH2V0KxCZZbv6BmpVzsdJ00a5zG+zqyHGaqTab4R6ERL4 X-Received: by 2002:a63:cf56:: with SMTP id b22mr34268905pgj.376.1549900937730; Mon, 11 Feb 2019 08:02:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549900937; cv=none; d=google.com; s=arc-20160816; b=VlokrKpIFIdfp1bH2KSW5T7cZS+Lw8drmUTj0l/Jcmhub+/SPkWWM76K7Xquxi0EPK Y79ZHRfOpoZnKtNoqnjIC9pnl+pLcM+Cm6uXFDhInV3/n1nHAY1nRMbrpeL+wbi9SAYd npMY6jCfysIIcVXgi/ShzxXDXz12wFpHi+VReB1ZXddAKQZFQuYnVfo9Dd6mTEOCbKVx 9Wk7yH5CFL4uFVKYy10VIQR75oGS06pDNmbSHpcOGxt7GrUeZjp0n6XuHdggnE0KqU2X 3KlWNn9XDyf6iP4WwkOwpbweskAJZBPXNEaCt4DqlnjQSqfALRQI+U2ustVqtVmxzNY1 HJ8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=AI0vxFWLuRgnzWT6ylhQhD0AdrQinrJeHY3w7Uhh0g8=; b=SQaGoSzTpGzUM3CJbjndnqBbsrgXCyC0HWK/wh2Chwn1FhNBWlVDpJOsrob+iH/xV9 FzxACX79s58c97JBkv3bnEEp03mCyrzjFpF1uCnRAgxC4FAB83o3XE8C5a3d2N1mFpPG jJDDDYmoXEYzlE3Pv3s3fKxxxPXPvpoZCEZS9bHevAmEoZlnjvMaTXiHXBxB8icIIPws 5/cDyJyobs84PWkSTqg/6Eeu6Z9McCwIZS9Efx4lvqNb7VAuCuyfmMflCify1rmBkvhc mM1aA5lmgqmcWTqeK4GY00utprbOSimpiLwxXkCLv/a2sps38keWWLdlO+T4P+8RADCj Ac2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@semihalf-com.20150623.gappssmtp.com header.s=20150623 header.b=v2ksebaY; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p1si5101669pgg.306.2019.02.11.08.01.58; Mon, 11 Feb 2019 08:02:17 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@semihalf-com.20150623.gappssmtp.com header.s=20150623 header.b=v2ksebaY; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731517AbfBKQBD (ORCPT + 99 others); Mon, 11 Feb 2019 11:01:03 -0500 Received: from mail-lj1-f196.google.com ([209.85.208.196]:39386 "EHLO mail-lj1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730303AbfBKOb2 (ORCPT ); Mon, 11 Feb 2019 09:31:28 -0500 Received: by mail-lj1-f196.google.com with SMTP id v12-v6so4212197ljc.6 for ; Mon, 11 Feb 2019 06:31:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=semihalf-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=AI0vxFWLuRgnzWT6ylhQhD0AdrQinrJeHY3w7Uhh0g8=; b=v2ksebaY4Ez01MrEKe6v7Ktauk/pBeXlPkS/WEEjGiEuuyVqjEhrsebEPUp/cy3/EB e7gMkTC9Df0iGfKUWHq7S4Br3Q/hWaRaMCWaC7kruVyrPzL6o+RvDsogOk1/4HFS/hsn YOtZgNvjDi1Lq0eKe9UwTEKbuoPB9V6EpQpBmAzhni7kOGifauuOTtGipb95GlP3eKD9 VL0d+KYCWP2FioU+0HTpRmu+7ao4H/JAwtxSdYtocJ9XrRyOH1UtuQOYa8uC5QUepFVj 5gZvG6dvlKhsJWpuul7dGLENK8xSy2JjGe0F+xoVwA0cVR/taP56iUxwwrcfIUvwFtnM DvWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=AI0vxFWLuRgnzWT6ylhQhD0AdrQinrJeHY3w7Uhh0g8=; b=pIcK1+pJpBsGRO6Gu48fBWRqMy4pEOuL4QpF0r/Ll22D4QKmfQw+bWTfgnSrP32KsG ftHBrUcRHvsHV1J4jxfefctcFAmgwcLEdeiP652tflN75w+2FUvde+9okjhPYs4vsuVe Ue/Mq+yX7/DwfQXtkx2waFnJTSLv2M8Z5NO8wgVkgBmkq40hHdpZIca7nsxdmOh3Po6L pzUFciyDsBh/Aagk3pctlt5Qvi1pvdhBO4pSoxLlroFJttAP9I4Bb25LOYpd4Jn0cXdE mencc6Q8FErRRqFyMXgKRzrZZA+4Vsv6blI2SgE6dqQtGPWpygVFMeYhw85ss1ZXq0IW +mvQ== X-Gm-Message-State: AHQUAuZfPaHIP/vzwi1yj95AkaDAn9diDk70cgAlEvYBtsVS3ePvDWRt p6yERH9mx2T4xKhtzy7UQsyc+w== X-Received: by 2002:a2e:874b:: with SMTP id q11-v6mr1980029ljj.97.1549895485732; Mon, 11 Feb 2019 06:31:25 -0800 (PST) Received: from bsz-kubuntu.semihalf.local (31-172-191-173.noc.fibertech.net.pl. [31.172.191.173]) by smtp.gmail.com with ESMTPSA id z6sm2322373lfa.87.2019.02.11.06.31.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 11 Feb 2019 06:31:25 -0800 (PST) From: bsz@semihalf.com To: linux-efi@vger.kernel.org, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, peterhuewe@gmx.de, jarkko.sakkinen@linux.intel.com, ard.biesheuvel@linaro.org Cc: tweek@google.com, mingo@kernel.org, hdegoede@redhat.com, leif.lindholm@linaro.org, mw@semihalf.com, Bartosz Szczepanek Subject: [PATCH 1/5] tpm: Copy calc_tpm2_event_size() to TPM library Date: Mon, 11 Feb 2019 15:30:48 +0100 Message-Id: <20190211143052.3128-2-bsz@semihalf.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190211143052.3128-1-bsz@semihalf.com> References: <20190211143052.3128-1-bsz@semihalf.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bartosz Szczepanek Function to calculate event size in TPM 2.0 log will also be needed in EFI stub. Separate it to library to make it accessible out of TPM character driver. It will be removed from tpm2.c in subsequent commit. Signed-off-by: Bartosz Szczepanek --- lib/tpm.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 lib/tpm.c diff --git a/lib/tpm.c b/lib/tpm.c new file mode 100644 index 000000000000..aaeeafe52426 --- /dev/null +++ b/lib/tpm.c @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2016 IBM Corporation + * + * Parts of this file based on earlier work by: + * Nayna Jain + * Petr Vandrovec + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + */ +#include +#include +#include +#include + +/* + * calc_tpm2_event_size() - calculate the event size, where event + * is an entry in the TPM 2.0 event log. The event is of type Crypto + * Agile Log Entry Format as defined in TCG EFI Protocol Specification + * Family "2.0". + + * @event: event whose size is to be calculated. + * @event_header: the first event in the event log. + * + * Returns size of the event. If it is an invalid event, returns 0. + */ +int calc_tpm2_event_size(struct tcg_pcr_event2 *event, + struct tcg_pcr_event *event_header) +{ + struct tcg_efi_specid_event *efispecid; + struct tcg_event_field *event_field; + void *marker; + void *marker_start; + u32 halg_size; + size_t size; + u16 halg; + int i; + int j; + + marker = event; + marker_start = marker; + marker = marker + sizeof(event->pcr_idx) + sizeof(event->event_type) + + sizeof(event->count); + + efispecid = (struct tcg_efi_specid_event *)event_header->event; + + /* Check if event is malformed. */ + if (event->count > efispecid->num_algs) + return 0; + + for (i = 0; i < event->count; i++) { + halg_size = sizeof(event->digests[i].alg_id); + memcpy(&halg, marker, halg_size); + marker = marker + halg_size; + for (j = 0; j < efispecid->num_algs; j++) { + if (halg == efispecid->digest_sizes[j].alg_id) { + marker += + efispecid->digest_sizes[j].digest_size; + break; + } + } + /* Algorithm without known length. Such event is unparseable. */ + if (j == efispecid->num_algs) + return 0; + } + + event_field = (struct tcg_event_field *)marker; + marker = marker + sizeof(event_field->event_size) + + event_field->event_size; + size = marker - marker_start; + + if ((event->event_type == 0) && (event_field->event_size == 0)) + return 0; + + return size; +} +EXPORT_SYMBOL(calc_tpm2_event_size); -- 2.14.4