TPM2 工作原理及操作 --- 数据封装(一)

本文详细介绍了TPM2.0(Trusted Platform Module)中密钥的创建、管理和使用,包括密钥属性、层次结构、主密钥与非主密钥的创建,以及数据封装、密封和解封的过程。此外,还讨论了使用对称和非对称密钥进行加密解密的操作。通过具体的TPM命令示例,阐述了如何在实践中确保数据的安全性。
摘要由CSDN通过智能技术生成

 

前言

以下部分内容来自我对tpm2-tools API文档、《A Practical Guide to TPM 2.0》的翻译和理解

关于TPM2基本原理、特性、密钥、层次、PCR等内容,TPM-JS这个项目有较详细的说明: 

TPM-JShttps://google.github.io/tpm-js/#pg_welcome想自学以及更深入了解的同学可以参考tpm2-tools doc :Home - tpm2-tools

TPM 的外部编程接口规范: https ://trustedcomputinggroup.org/tpm-library-specification/

TPM2.0实用指南 《A Practical Guide to TPM 2.0》:

​​​​​​https://link.springer.com/content/pdf/10.1007/978-1-4302-6584-9.pdf 

数据封装

1.密钥

1.1 对象属性

对象属性位图字段是TPM2B_PUBLIC属性内容:

  • 解密( TPMA_OBJECT_DECRYPT )。指定加密密钥。
  • 签名 ( TPMA_OBJECT_SIGN_ENCRYPT )。指定签名密钥。
  • 受限 ( TPMA_OBJECT_RESTRICTED )。将密钥限制为仅对内部TPM数据进行签名/加密。

层次结构可以被认为具有父密钥和子密钥。父密钥保护其子密钥,当子密钥存储在 TPM 之外时提供保密性和完整性。包装其他密钥的密钥也称为存储密钥。存储密钥是受限制的加密密钥。它们不能用于一般解密,这可能会泄露孩子的秘密。

层次结构顶部的最终父级是主密钥。孩子可以是存储钥匙,在这种情况下,他们也可以是父母。孩子也可以是非存储密钥,在这种情况下,它们是叶密钥:孩子但不是父母。

  • 密钥必须是加密密钥或签名密钥。
  • 受限密钥不能同时用于加密和签名。
  • 存储密钥是受限制的加密密钥。
  • 受限签名密钥用于 PCR 报告和密钥证书。

使用TPM2_CC_CreatePrimary该命令创建主密钥。

使用TPM2_CC_Create该命令创建非主密钥。该命令将父密钥的句柄和密钥的 TPM2B_PUBLIC 模板作为输入。

TPM2_CC_Create 创建但不将密钥加载到 TPM。该命令返回TPM2B_PRIVATE由父密钥包装(加密)的密钥材料。用于TPM2_CC_Load将密钥加载到TPM。

2.密封数据

使用tpm2_create 创建子对象时,可以通过参数-i,--sealing-input = FILESTDIN:FILE要密封的数据文件,可选。如果文件是 - ,从标准输入读取。密封数据时,仅允许使用 NULL 方案的TPM_ALG_KEYEDHASH算法。因此,无法指定-G 。

tpm2_unseal (1) - 返回加载的 TPM 对象中的数据 blob。数据 blob 以明文形式返回。使用tpm2_create工具在创建对象时密封数据。这种用于密封数据的对象必须是TPM_ALG_KEYEDHASH类型。

2.1 使用tpm2_create密封数据

具体TPM命令流程如下:

1. 为了创建一个对象,我们必须首先创建一个主秘钥作为它的父密钥。

tpm2_createprimary -c primary.ctx

2.将数据密封到 TPM

在密钥对象之外,TPM 允许将少量用户指定的数据密封到 TPM。

echo "my sealed data" > seal.dat

tpm2_create -C primary.ctx -i seal.dat -u sealobj.pub -r sealobj.priv -c seal.ctx

3.解封数据

tpm2_unseal -c seal.ctx

2.2 使用对称密钥命令tpm2_encryptdecrypt加密或解密数据

tpm2_encryptdecrypt [选项] [参数] - 使用指定的对称密钥对FILE的内容执行对称加密或解密。如果未指定FILE ,则默认为stdin

注:使用tpm2_encryptdecrypt会报错误:WARN: Using a weak IV, try specifying an IV,需要新建16字节iv文件。

具体TPM命令流程如下:

1.创建 AES 密钥

tpm2_createprimary -c primary.ctx

tpm2_create -C primary.ctx --key-algorithm=aes128 -u key.pub -r key.priv

tpm2_load -C primary.ctx -u key.pub -r key.priv -c key.ctx

2.加密和解密一些数据

echo "my secret" > secret.dat

echo "secret iv file." >iv.ctx

tpm2_encryptdecrypt -c key.ctx -o secret.enc secret.dat -t iv.ctx

tpm2_encryptdecrypt -d -c key.ctx -o secret.dec secret.enc -t iv.ctx

cat secret.dec

my secret

2.3 使用非对称密钥命令tpm2_rsaencrypt 、tpm2_rsadecrypt加密或解密数据

tpm2_rsaencrypt  [选项] [参数]- 使用 TPM 执行 RSA 加密操作,

tpm2_rsadecrypt [选项] [参数]- 使用 TPM 执行 RSA 解密操作。

具体TPM命令流程如下:

1、创建一个 RSA 密钥并加载它

tpm2_createprimary -c primary.ctx

tpm2_create -C primary.ctx -Grsa2048 -u key.pub -r key.priv

tpm2_load -C primary.ctx -u key.pub -r key.priv -c key.ctx

2.使用 RSA 加密

echo "my message" > msg.dat

tpm2_rsaencrypt -c key.ctx -o msg.enc msg.dat

3.使用 RSA 解密

tpm2_rsadecrypt -c key.ctx -o msg.ptext msg.enc

cat msg.ptext

my message

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
A Practical Guide to TPM 2.0: Using the Trusted Platform Module in the New Age of Security is a straight-forward primer for developers. It shows security and TPM concepts, demonstrating their use in real applications that the reader can try out. Simply put, this book is designed to empower and excite the programming community to go out and do cool things with the TPM. The approach is to ramp the reader up quickly and keep their interest.A Practical Guide to TPM 2.0: Using the Trusted Platform Module in the New Age of Security explains security concepts, describes the TPM 2.0 architecture, and provides code and pseudo-code examples in parallel, from very simple concepts and code to highly complex concepts and pseudo-code. The book includes instructions for the available execution environments and real code examples to get readers up and talking to the TPM quickly. The authors then help the users expand on that with pseudo-code descriptions of useful applications using the TPM. What you’ll learn TPM 2.0 architecture fundamentals, including changes from TPM 1.2 TPM 2.0 security concepts Essential application development techniques A deep dive into the features of TPM 2.0 A primer on the execution environments available for application development. Learn as you go! Who this book is for Application software developers, OS developers, device-driver developers, and embedded-device specialists, who will benefit from mastering TPM 2.0 capabilities and building their own applications quickly. This book will give them the tools they need to experiment with and understand the technology. Software architects who need to understand the security guarantees provided by TPMs Managers who fund the projects that use TPMs. Non-technical users who may want to know why TPMs are on their computers and how to make use of them.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大大大飞啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值