Skip to content
刘群 edited this page Apr 19, 2017 · 9 revisions

Build from source

准备工作

目前 Ubuntu/Debian 和 Fedora 等比较新的 Linux 发行版本都已经加入了 TPM 2.0 软件栈开发工具.

这里以 Ubuntu 17.04 系统为例子, 介绍如何基于英特尔开源的 TPM 2.0 软件栈进行第三方应用的开发, 运行以下两条命令:

sudo apt-get update
sudo apt-get install libsapi-dev libsapi-utils

备注: Ubuntu 16.04 版本中自带的是比较旧的 0.98 版本 libtss2-dev, 软件包名字不同是因为开发者变更了 deb 包名, Ubuntu 16.04 版本相应的安装命令是sudo apt-get install libtss2-dev libtss2-utils

安装英特尔提供的开源 TPM 2.0 软件栈. 其中:

  • 软件包 libsapi-utils 包括可执行文件 /usr/sbin/resourcemgr 即英特尔 TPM 资源管理器
  • 软件包 libsapi-dev 包括 C 头文件和动态链接库, 将被分别安装到 /usr/include/sapi /usr/include/tcti 和 /usr/lib/{CPU架构}-linux-gnu 目录中去

安装扩展的 TPM 2.0 管理工具

运行命令:

sudo apt-get install tpm2-tools

安装因特尔提供的 TPM 2.0 客户端工具样例程序, 其中包括 tpm2_nvlist, tpm2_nvdefine, tpm2_nvrelease 等实用小命令.

以下三种方式可以查看每个命令的具体使用说明, 此处以 tpm2_nvlist 为例:

安装微软 Simulator.exe 或 IBM software TPM simulator

参照 https://github.com/01org/TPM2.0-TSS 的 README 自述文档, 安装 Windows 或 Linux 版本的模拟器

Simulator.exe 是一个 Win32 控制台应用程序, 可以在 Windows 命令提示符下启动, 也可以鼠标直接双击运行

创建 C/C++ 应用程序的步骤

确定头文件和动态库的安装位置

编译C/C++应用程序前需要确定头文件和动态链接库的安装位置, 大多数 Linux 发行版默认安装pkg-config命令, 用于查询主机已安装的开发库.

$ pkg-config --cflags sapi
-I/usr/include/sapi

$ pkg-config  --libs sapi
-lsapi

$ pkg-config --cflags tcti-socket
-I/usr/include/tcti -I/usr/local/include/sapi

$ pkg-config --libs tcti-socket
-ltcti-socket -lsapi

以上命令分别查询编译不同 C/C++ 应用程序时 gcc 所需要指定的头文件的安装位置和动态链接库的选项.

创建临时文件 myapp.c , 然后试试用如下命令编译和运行:

$ gcc -I/usr/include/sapi myapp.c -lsapi
$ ./a.out
Default sysContext size: 4160

以下是 myapp.c 的内容:

// File: myapp.c

#include <stdio.h>
#include <tpm20.h>

void main() {
    printf("Default sysContext size: %d\n", Tss2_Sys_GetContextSize(0));
}

测试步骤

  1. 在 Windows 下双击 Simulator.exe 启动 TPM 模拟器

  2. 在 Linux 终端窗口下启动 TPM 资源管理器:

    /usr/sbin/resourcemgr -sim -tpmhost 192.168.x.x

    选项-sim表示让资源管理器连接到 TPM 模拟器

    把选项-tpmhost 192.168.x.x中的 IP 地址改为实际运行 TPM 模拟器的主机的 IP 地址.

  3. 运行客户端应用程序, 例如运行 libsapi-utils 自带的两个 Demo 程序 /usr/bin/tpmclient/usr/bin/tpmtest