by Davide Guerri

RSA signatures with TPM2.0 and OpenSSL

原始文章链接: https://dguerriblog.wordpress.com/2016/03/03/tpm2-0-and-openssl-on-linux-2/
(需要科学上网)
--
全文如下:

Foreword

Welcome stranger and thanks for stoppingby. This is my first blog post (ever) and presentlyI am not even sure whether I want to maintain a blog… so who knows? It could also be the last.

What I am going to show applies to any Trusted Platform Module (TPM) implementing TPM2.0 specs. However,I wrotethis article afterspending two days trying to use the Minnowboard MAX firmware TPM (fTPM) forsomething useful in real life… I hope I can save you some time and a lot of troubles [1].

The problem

As it turns outtpm2-tools(the only TPM2.0 userland tools available on Linux that I am aware of) uses an output format forcryptographic operations like signatures, public keys export, hashing, etc which is not compatible with OpenSSL.

This is very annoying as you can’t use directly a TPM for useful stuff if the other partyis not able to load those TPM data structure (e.g. using a tpm2-tools).

After spending quite a bit of time on theTPM2.0 specs(a reading that I would recommend to anyone with a lot of time and masochistic personality) I came up with some procedures to convert RSA public keys and signatures.

In this article I am going to generate a RSA key that we canuse to identify a particular device using a TPM thatimplements TPM2.0 specification.The easiest way to achievethat is using an AIK.

But, let’s start from the beginning…

Generating an Endorsement Key (EK)

Before generating a new AIK, we need to generate an EK. As I am using a newly initialised TPM, I have no password configured, so I can just issue the following command:

~# tpm2_getpubek -H 0x81010000 -g 0x01 -f ek.pub

That will generate a new RSA (hex code 0x01) key, store it in the NVRAM of the TPM with handle 0x8101000 and export the public portion in a file named ek.pub.

Unfortunatelywe can’t use this key directly for what we need to do, so let’s:

Generatean Attestation Identity Key (AIK)

Similarly to what we have done to generate the EK, we can generate a AIK:

~# tpm2_getpubak -E 0x81010000 -k 0x81010010 -f ak.pub -n ak.name

RSA is the default algorithm.TheAIK is defined in the endorsement hierarchy so it needs to be generated using a EK (0x81010000 in this case). This new key isstored in the device NVRAM with handle0x81010010. The public bit is exported in ak.pub.

  • ak.name containsthe cryptographically secure name of the key. We are not going to need it for now.

  • ak.pub isaTPMT_PUBLIC structure which, among other things, containsthe RSA modulus. As we generated a 2048 bits key (default), the modulus is exactly 256 bytes.

It is important to note that ak.pub doesn’t contain the RSA exponent (actually that field is present but it is set to 0). For RSA, TPM2.0 assumes that the exponent is always 2^16+1, or 65537 (for a good reason).

All that being said, we can convert the key to a DER and/or PEM format.

The DER key is then defined as <header> <modulus> <mid-header> <exponent>; we can use the following commands to compute all these elements:

1. Extract the modulus (removing TPMT_PUBLIC header and padding)

~# dd if=ak.pub of=modulus.bin bs=1 skip=102 count=256

2. Define the fixed header used by OpenSSL to identify a RSA key

~# echo 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA' | openssl base64 -a -d > header.bin

3. Mid-header is always 0x02 0x03i.e.the exponent is a 3 bytes (0x03) integer (0x02)

~# echo -en '\x02\x03' > mid-header.bin

4. Exponent is always 65537 (2^16+1)as we have already seen

~# echo -ne '\x01\x00\x01' > exponent.bin

5. Compose the DER key

~# cat header.bin modulus.bin mid-header.bin exponent.bin > key.der

6. If needed you can easily convert the DER encoded key to PEM

~# openssl pkey -inform der -outform pem -pubin -in key.der -out key.pem

If you want to see how themodulus and the exponent look like, just run:

~# openssl rsa -in key.pem -pubin -noout -text

Modulus (2048 bit):
00:c7:2d:bd:f1:88:30:01:64:6a:0c:ae:61:52:23:
[stuff...]
87:a9
Exponent:
65537 (0x10001)

Ok. It seems legit, doesn’t it?

Signing a document

InTPM1.2 an AIK cannot be used to sign objects that are external to the TPM.TPM2.0 extends thisconcept: to sign an object with a primary key, we have to prove the TPM that object has been generated by the TPM itself. In order to do so, TPM2.0 uses tickets.

The following command computes the sha256hash ofa txt file and generate a TPM2.0 ticket (0x00B tellstpm2_hash to use SHA256.):

~# tpm2_hash -H e -g 0x00B -I message.txt -o hash.bin -t ticket.bin

Let’s signthe hash using ticket.bin as the authorisation token and the AIK with persistent handle0x81010010:

~# tpm2_sign -k 0x81010010 -g 0x000B -m message.txt -s sign.bin -t ticket.bin

sign.bin contains the signature, wrapped in aTPMU_SIGNATURE structure.

In order toget something we can use with OpenSSL, let’s extract the relevant bits (i.e.the raw signature):

~# dd if=sign.bin of=sign.raw bs=1 skip=6 count=256

Verifying a TPM2.0RSA signature

This is easy because we have already got a RSA public key that can be used by OpenSSL and a raw signature:

~# openssl dgst -verify key.pem -keyform pem -sha256 -signature sign.raw message.txt

If you get:

Verified OK

congratulations, it worked!

Conclusion

This is just an example of what we can do with a TPM. In one of the next articles (if any :P) I will explain how to decrypt a message encrypted with the a pubic key generated by the TPM.

Infineon_TPM_2.0.jpg

作者简介Davide Guerri https://archive.fosdem.org/2017/schedule/speaker/davide_guerri/

【转载】RSA signatures with TPM2.0 and OpenSSL相关推荐

  1. IBM TPM2.0 模拟器

    本文更新于2018-08-11 IBM TPM2.0 模拟器(链接)最近1年内发布了好几个版本,其中编号为532的老版本仅支持OpenSSL 1.0.2, 不支持 OpenSSL 1.1 以上版本(目 ...

  2. Ubuntu 安装 TPM-2.0 TSS 软件栈

    本文更新于2018-08-11 手动编译 TPM2.0-TSS sudo apt-get install -y git-coregit clone --branch=1.x --depth=1 htt ...

  3. 前方预警!Windows Server 将默认需符合 TPM2.0,服务器商需在来年 1 月 1 日前适配相应规则...

    [CSDN编者按]随着Windows Server宣布最新调整,服务器制造商需在2021年1月1日前适配相应规则. 作者 | Simon Sharwood 译者 | 孙薇,责编 | 夕颜 头图 | C ...

  4. 30分钟从工作电脑入侵公司内网!Win11:更新强制要求有TPM2.0,知道为啥了吧?...

    博雯 发自 凹非寺 量子位 报道 | 公众号 QbitAI 工作电脑被偷的30分钟后,公司内网就进人了. 不仅拥有活动目录上的基本特权,还能在内部文件中来去自如! 可我那保护重重的Windows防火墙 ...

  5. 华硕主板怎么开启tpm2.0

    tpm2.0是我们电脑主板上自带的功能,win11系统的安装就规定需要这个tpm2.0,如果自己的电脑主板没有tpm2.0,那就根据自己的主板使用不同的方法开启tpm2.0,下面一起来看一下华硕主板怎 ...

  6. 升级win11-需要开启主板的tpm2.0

    背景: 无法通过微软官方的Win11升级验证!那可能是你的TPM模块功能没有开启! 这里只针对华硕系的主板,面向intel平台和AMD平台,如果还是没有找到,有可能主板太老了! 方法如下: [图片]华 ...

  7. Honor笔记本 (2018款intel版本)win11升级教程(TPM2.0)

    目录 0. 引言 1. Step 1 准备阶段 2. Step 2 工具环节 3. Step 3 安装win 11 参考资料 0. 引言 注意:本人笔记本电脑是2018年买的荣耀刚出的magicboo ...

  8. 计算机tpm1.2怎么启动,tpm2.0开启的方法

    tpm2.0是我们电脑主板上自带的功能,而win11系统的安装就需要这个tpm2.0,我们可以在安装win11前使用healthcheck进行检测,如果发现没有tpm2.0,那就根据自己的主板,使用不 ...

  9. 安装win11电脑必须支持TPM2.0和必须支持安全启动的解决方法

    安装win11电脑必须支持TPM2.0和必须支持安全启动的解决方法 一.开启TPM设置 二.开启安全启动设置 三.更改硬盘模式(需硬盘支持) 安装 Win11 的基本要求,在win11最低要求是提示, ...

最新文章

  1. Pythonic版冒泡排序和快速排序(附:直接插入排序)
  2. 35 岁前程序员要规划好的四件事(转载)
  3. python 语义网络_专家鉴定这是2019最顶级的python框架,没有之一
  4. 管理共享文件夹和使用FTP服务器
  5. Window.navigator
  6. android 后台执行js,android - 当应用程序在后台运行时,Android WebView消耗大量电能...
  7. 指针结构体函数-事实上能够这样具体理解
  8. 面试之 Redis汇总
  9. Spoonwep破解wep加密无线路由密码
  10. django 1.8 官方文档翻译: 2-1-1 模型语法(初稿)
  11. hadoop-KMS密钥管理服务配置使用
  12. 用VC++建立Service服务应用程序
  13. 为什么我不建议你用去 “ ! = null “ 做判空?
  14. T-SQL 基础简介
  15. Nginx实现静态资源服务器
  16. 跟刘欣学习造spring
  17. 英语3500词(六)relationships主题(2022.1.18)
  18. 条码扫描枪有什么接口?应该怎么选择扫描枪的接口
  19. 苹果手机使用技巧篇:教你完美使用好苹果手机的4个方法
  20. python面向对象书籍_python面向对象法实现图书管理系统

热门文章

  1. Linux安装与基本操作命令与JDK的安装,Mysql的安装,Tomcat的安装
  2. 力扣——合并两个有序数组
  3. 基本结构标签(HTML)
  4. java 传绝对路径无效_又传噩耗!知名主持人小济南因突发肺栓塞不幸去世,享年36岁...
  5. Qt窗口部件——对话框QDialog
  6. 分布式Redis深度历险-复制
  7. computed、watch和methods特性比较
  8. linux oracle 10g dataguard 实施详细记录
  9. Redis实现微博后台业务逻辑系列(八)
  10. Linux教程:10条秘诀确保Linux桌面安全性