From patchwork Tue Oct 3 15:52:26 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Andr=C3=A9_Draszik?= X-Patchwork-Id: 820938 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 3y63vN6zW1z9s8J for ; Wed, 4 Oct 2017 03:12:00 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id B9410C21F75; Tue, 3 Oct 2017 16:08:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=RCVD_IN_MSPIKE_H2 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id B30DAC21F85; Tue, 3 Oct 2017 16:07:37 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 00F2CC21D63; Tue, 3 Oct 2017 15:52:32 +0000 (UTC) Received: from mail-wr0-f196.google.com (mail-wr0-f196.google.com [209.85.128.196]) by lists.denx.de (Postfix) with ESMTPS id 9EB2BC21D09 for ; Tue, 3 Oct 2017 15:52:32 +0000 (UTC) Received: by mail-wr0-f196.google.com with SMTP id v38so3167163wrc.4 for ; Tue, 03 Oct 2017 08:52:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=S3uKQOrrGPRJbo27zjUVleqQZzyJYCsTNFCCOFxNNtI=; b=n/bCqiShp6LmS4QMwPev1YLJHrjOAECVw5Viob9ETy10WIUB9WQ0A7RciPEOnFD35O TP06PtT+KnsrjNzC3N6bGCDWpLjmiNI0+f4bUrznAuP+h3ZElsASMmuXuHGs2X63wbde YPmhmzNO/p1S7C3bVb/v1qge3KlviLTqSwACuxOEPkpT6182hTbSptgbgw4fvTO10K3T 5o7Ll9RMYmngfDWucaQ3VqZAF8sKIcKpRq5YIHAasiSVG+OOGeQO4et8Zn+kVRXzIRvP 1N225xnHMq91taf7PQ+lOnQ/II0UxD0wjTCJStvm3cNZ8KChNLc0Xl1XJN5V422uY8a+ tPiA== X-Gm-Message-State: AMCzsaXhyankuhXxl57iM6bSdSDQSWL1fSYv8UPTg+tE6ghcUYRdzaTN QBTBg5acwEDMUpdLQiHWRIoGogNl X-Google-Smtp-Source: AOwi7QC69PnmFm41HiD48EViHWkXKcg8N2GTEd7BJXEpsaENU0kLPc0r7CUuDlIplP8aDUF/LwM9xQ== X-Received: by 10.223.143.56 with SMTP id p53mr1225032wrb.179.1507045952067; Tue, 03 Oct 2017 08:52:32 -0700 (PDT) Received: from tfsielt31850.tycofs.com ([77.107.218.170]) by smtp.gmail.com with ESMTPSA id p9sm1676893wra.43.2017.10.03.08.52.30 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 03 Oct 2017 08:52:31 -0700 (PDT) From: =?utf-8?q?Andr=C3=A9_Draszik?= To: u-boot@lists.denx.de Date: Tue, 3 Oct 2017 16:52:26 +0100 Message-Id: <20171003155228.9702-3-git@andred.net> X-Mailer: git-send-email 2.14.2 In-Reply-To: <20171003155228.9702-1-git@andred.net> References: <20171003155228.9702-1-git@andred.net> MIME-Version: 1.0 X-Mailman-Approved-At: Tue, 03 Oct 2017 16:07:33 +0000 Subject: [U-Boot] [PATCH 3/5] tpm: add tpm_get_random() X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" From: André Draszik Signed-off-by: André Draszik --- include/tpm.h | 12 ++++++++++++ lib/tpm.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/include/tpm.h b/include/tpm.h index f88388f353..2a7528dd48 100644 --- a/include/tpm.h +++ b/include/tpm.h @@ -651,4 +651,16 @@ uint32_t tpm_flush_specific(uint32_t key_handle, uint32_t resource_type); uint32_t tpm_find_key_sha1(const uint8_t auth[20], const uint8_t pubkey_digest[20], uint32_t *handle); #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ + +/** + * Read random bytes from the TPM RNG. The implementation deals with the fact + * that the TPM may legally return fewer bytes than requested by retrying + * until @p count bytes have been received. + * + * @param data output buffer for the random bytes + * @param count size of output buffer + * @return return code of the operation + */ +uint32_t tpm_get_random(void *data, uint32_t count); + #endif /* __TPM_H */ diff --git a/lib/tpm.c b/lib/tpm.c index 5659fa5e18..42a6591f81 100644 --- a/lib/tpm.c +++ b/lib/tpm.c @@ -1049,3 +1049,46 @@ uint32_t tpm_find_key_sha1(const uint8_t auth[20], const uint8_t #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ #endif /* CONFIG_TPM_AUTH_SESSIONS */ + +uint32_t tpm_get_random(void *data, uint32_t count) +{ + const uint8_t command[14] = { + 0x0, 0xc1, /* TPM_TAG */ + 0x0, 0x0, 0x0, 0xe, /* parameter size */ + 0x0, 0x0, 0x0, 0x46, /* TPM_COMMAND_CODE */ + }; + const size_t length_offset = 10; + const size_t data_size_offset = 10; + const size_t data_offset = 14; + uint8_t buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE]; + size_t response_length = sizeof(response); + uint32_t data_size; + uint8_t *out = data; + + while (count > 0) { + uint32_t this_bytes = min(count, + sizeof (response) - data_offset); + uint32_t err; + + if (pack_byte_string(buf, sizeof(buf), "sd", + 0, command, sizeof(command), + length_offset, this_bytes)) + return TPM_LIB_ERROR; + err = tpm_sendrecv_command(buf, response, &response_length); + if (err) + return err; + if (unpack_byte_string(response, response_length, "d", + data_size_offset, &data_size)) + return TPM_LIB_ERROR; + if (data_size > count) + return TPM_LIB_ERROR; + if (unpack_byte_string(response, response_length, "s", + data_offset, out, data_size)) + return TPM_LIB_ERROR; + + count -= data_size; + out += data_size; + } + + return 0; +}