linux tpm 测试完整记录,亲测有效。
没有tpm芯片,采用模拟器的方式来测试。
实验环境:内核版本 3.10.0-327
软件包准备:
内网,没有仓库,自己网上下载:
1. cmake-3.9.6-Linux-x86_64.tar.gz
解压后就已经有bin文件,可以直接使用bin下的cmake,
ls cmake-3.9.6-Linux-x86_64/
bin doc man share
2. 然后下载gmp,我使用的是gmp-6.1.2版本,
./configure && make && make install
在install的时候,它会提醒要求make check
+-------------------------------------------------------------+ | CAUTION: | | | | If you have not already run "make check", then we strongly | | recommend you do so. | | | | GMP has been carefully tested by its authors, but compilers | | are all too often released with serious bugs. GMP tends to | | explore interesting corners in compilers and has hit bugs | | on quite a few occasions. | | | +-------------------------------------------------------------+
我也按要求操作一把,没看到什么区别,但是同事在复制我的过程的时候,还是有些区别,建议大家按照安装步骤来。
3.安装tpm_emulator
因为并没有真实的芯片,所以还需要安装模拟器,如果有真实的芯片支持,则可以跳过安装模拟器这一步。
下载并解压之后,老规矩,先看readme,readme中针对不同的操作系统给了不同的步骤。
The compilation and installation of the TPM emulator package is based on
the CMake build environment (version 2.6 or better) and requires that the
GNU MP library (version 4.0 or better) is properly installed on your
system. A working MinGW compiler suite is further required on Windows
(see http://www.mingw.org/). To compile and install the package execute:
# tar -xvzf tpm_emulator-X.Y.tar.gz
# cd tpm_emulator-X.Y
# mkdir build
# cd build
# cmake ../ ----------这一步,由于我们第一步没有安装cmake,可以用绝对路径因为bin文件即可。本例中,我使用的是/home/caq/cmake-3.9.6-Linux-x86_64/bin/cmake -D CMAKE_BUILD_TYPE=Debug ../
# make
# make install
按步骤操作即可。
Usage Examples for TPM/J -------------------------------------------------------------------------- === Linux # modprobe tpmd_dev # tpmd -f -d
4.下一步,下载trousers-master.zip并安装trousers,这个就是传说中的tss协议栈 (tcg software stack) 。
解压后,参照readme文件,一步步操作:
不过,由于我们是使用模拟器操作,所以原本makefile中的lib需要替换成我们模拟器安装的lib。
先查找一下lib的路径:
# whereis libtddl.so libtddl: /usr/local/lib/libtddl.a /usr/local/lib/libtddl.so
然后,修改对应的makefile
To build trousers after you have the device driver installed: $ sh bootstrap.sh $ ./configure [--enable-debug] [--enable-gprof] [--enable-gcov] $ make # make install
这个步骤的README非常重要,包括64位的安装的不同也会提示。也包括安装完之后的运行方法和参数,不过-h参数也提供了方法。
另外,由于这个步骤默认的configure是将编译warning设置成了error,所以make的时候,经常会停止,建议在make之前,
将configure脚本中的CFLAGS 参数修改一下,去掉-Werror -Wno-unused-parameter 。不然会好多编译不过的。
然后运行 tcsd -e -f 可以看到如下进程运行,我为了了解运行机制,gdb了一把,这个就需要在configure的时候对debug使能。
# ps -ef |grep -i tcsd
root 156492 183333 0 16:20 pts/4 00:00:00 grep --color=auto -i tcsd
tss 184541 107431 0 Nov15 pts/11 00:00:00 tcsd -e -f
使能了debug的运行结果,如果没有使能debug,不方便调试:
tpm_testing.c:221: Debug: tpm_rsa_encrypt(RSA_ES_OAEP_SHA1) tpm_testing.c:225: Debug: tpm_rsa_decrypt(RSA_ES_OAEP_SHA1) tpm_testing.c:229: Debug: verify plain text tpm_testing.c:261: Info: Self-Test succeeded tpm_startup.c:43: Info: TPM_Startup(2) tpmd.c:412: Debug: waiting for connections...
5.下载安装tpm-tools
BUILDING tpm-tools -------- --------- $ sh ./bootstrap.sh $ ./configure $ make # make install By default the build will place everything in /usr/local. Issue ./configure --help to see how to install to a different location.
安装完之后,敲入tpm然后tab健,就可以看到新增了很多命令了:
tpm_changeownerauth tpm_nvinfo tpm_restrictpubek tpm_setactive tpm_setpresence tpmtool
tpm_clear tpm_nvread tpm_restrictsrk tpm_setclearable tpm_takeownership
tpm_createek tpm_nvrelease tpm_revokeek tpm_setenable tpm_unsealdata
tpm_getpubek tpm_nvwrite tpm_sealdata tpm_setoperatorauth tpm_version
tpm_nvdefine tpm_resetdalock tpm_selftest tpm_setownable tpmd
下面是最终的测试了:
# tpm_takeownership
Enter owner password:
Confirm password:
Enter SRK password:
Confirm password:
设置两个密码,要记住,后面要用到。
# ./tpm_version LOG_DEBUG TSPI rpc/tcstp/rpc.c:362 Sending TSP packet to host localhost. LOG_DEBUG TSPI rpc/tcstp/rpc.c:377 Connecting to 127.0.0.1 LOG_DEBUG TSPI rpc/tcstp/rpc_context.c:44 RPC_OpenContext_TP: Received TCS Context: 0xa0d40600 LOG_DEBUG TSPI rpc/tcstp/rpc_caps_tpm.c:40 RPC_GetTPMCapability_TP: TCS Context: 0xa0d40600 TPM 1.2 Version Info: Chip Version: 1.2.0.7 Spec Level: 2 Errata Revision: 1 TPM Vendor ID: ETHZ LOG_DEBUG TSPI rpc/tcstp/rpc_caps_tpm.c:40 RPC_GetTPMCapability_TP: TCS Context: 0xa0d40600 TPM Version: 01010000 LOG_DEBUG TSPI rpc/tcstp/rpc_caps_tpm.c:40 RPC_GetTPMCapability_TP: TCS Context: 0xa0d40600 Manufacturer Info: 4554485a LOG_DEBUG TSPI rpc/tcstp/rpc_context.c:60 RPC_CloseContext_TP: TCS Context: 0xa0d40600 LOG_RETERR TSPI tspi_context.c:113: 0x126
加密某个文件:
tpm_sealdata -i /home/caq/Path_test/caq.txt -p 4 -p 8 -p 9 -p 12 -p 14 -o /home/caq/Path_test/sealed_key
会要求输入srk的密码,就是刚才设置的第二个密码。
加密后的文件内容:
-----BEGIN TSS----- -----TSS KEY----- AQEAAAARAAAABAEAAAABAAMAAQAAAAwAAAgAAAAAAgAAAAAAAAAAAAABANxiK5q8 Nfx/spy+mnJgRpRAaEgBhvT3bTDdV9gNSdy6hryJGNKkIsQCSdam1Yo28DPHiB9i iout25PXLi9VaNLmlT7UPVMTReujGhSMWk5pkejpNd0He+CoTyov6CveHnk7YqXa Hp1ipsg98+IVLrhshPkbW4yq6/PEK07Za56Cj0gVDQ1S+3zkZY8wegPilXG79GQ8 j/yHaPar62jExVfGuoOtPsUINFAcgvWO14Syq30ZM0Dr4aTGcj4XjKqrRtuuiE7y qkQM37DM1x7/EYgg9/ITIpR3fVTP7hEuo+qPyfUy8rEINAoTCBiBN/53r84xdA+y t+Ic1tt+qSJi/t8AAAEAq+dKuhTIuoo75gKIkRfiEbpeUn3yK3sHJTkZKwJudMyp 1f1oYegR48lhatQhgRPC5nfKBG/7N/VoKb9KQMA/XuFM4iKVdfqckSnnq8s97Exh 7cK9DHPWDLCChk/Fa6xPR4AIcq6Qj4DTiESQeb66ZGGl/DSKzMLQwYFeg011UmI9 5r4sb3vdkCVIj2suuLQpbmc80O9rLsHmo7z68SNB8HgHFSR/LQeywoPa4Gq5xFsH /qDRSS5rayvd1+/W3n7bhlj1Byym6/T9Yjzgsycm1TlqWlY7uX9wiXP63y29IAzQ sTnK1eTWfU2KrmpZznmwoa9oxjGuzvMPdplyMV4O2Q== -----ENC KEY----- Symmetric Key: AES-256-CBC AQEAAAAAACwAAhBTkyUEalmn0CnsJOCtGAt3AocthliTJQRqWafQKewk4K0YC3cC hy2GWAAAAQBvqgwWrpyJ0pTdR4aNEvq+YfQ+58GyISxMLh/DziwijcwZ5VZf03Ps +vLq876xOSuo3SDbKJfHas7JHasxq5WnX8j50VqTubvHXQ6H2y3Frtp3suG6LQgT kI4DNTrkHXMbp8AJU5Euk4DGf7dXKeKbu/XBsKqoY0FP61WgrSMU6wCGI3nQ0esW ARdVBoZkWl4Gf7KHb+Jhkfyh8T+1vn63IZ31BvaJBjlQDTGqJofDvaBkR6ceKrAD h9G1EA0NUc9Grwv59L0onyZ7R6D7P2lgCheLsvyt2gFr0bxyMm78D1Fuywx5iZtx 7e7iToJSld4tq7LJep8Rf/f7Bo9DvJnL -----ENC DAT----- UxkNGTc11B7eIdPUx65IsA== -----END TSS-----
解密这个文件:
tpm_unsealdata -i /home/caq/Path_test/sealed_key -o hah
也要求输入密码,密码输入错误是拒绝解密的。
tpm_sealdata -i /home/caq/Path_test/sealed_key -o hah LOG_DEBUG TSPI rpc/tcstp/rpc.c:362 Sending TSP packet to host localhost. LOG_DEBUG TSPI rpc/tcstp/rpc.c:377 Connecting to 127.0.0.1 LOG_DEBUG TSPI rpc/tcstp/rpc_context.c:44 RPC_OpenContext_TP: Received TCS Context: 0xa0b0fe15 LOG_DEBUG TSPI rpc/tcstp/rpc_random.c:37 RPC_GetRandom_TP: TCS Context: 0xa0b0fe15 LOG_DEBUG TSPI rpc/tcstp/rpc_ps.c:318 RPC_LoadKeyByUUID_TP: TCS Context: 0xa0b0fe15 LOG_DEBUG TSPI rpc/tcstp/rpc_ps.c:339 RPC_LoadKeyByUUID_TP: TCS key handle: 0x40000000 LOG_DEBUG TSPI rpc/tcstp/rpc_ps.c:274 RPC_GetRegisteredKeyBlob_TP: TCS Context: 0xa0b0fe15 Enter SRK password: LOG_DEBUG TSPI rpc/tcstp/rpc_auth.c:70 RPC_OSAP_TP: TCS Context: 0xa0b0fe15 LOG_DEBUG TSPI rpc/tcstp/rpc_key.c:119 RPC_CreateWrapKey_TP: TCS Context: 0xa0b0fe15 LOG_DEBUG TSPI rpc/tcstp/rpc_auth.c:37 RPC_OIAP_TP: TCS Context: 0xa0b0fe15 LOG_DEBUG TSPI obj_policy.c:230 Got a secret: 36 F7 7C 80 32 12 AD 05 7D 36 63 03 76 D2 85 2E 24 72 D6 4D LOG_DEBUG TSPI rpc/tcstp/rpc_key.c:42 RPC_LoadKeyByBlob_TP: IN: TCS Context: 0xa0b0fe15 LOG_DEBUG TSPI rpc/tcstp/rpc_key.c:75 RPC_LoadKeyByBlob_TP: OUT: TCS key handle: 0x2233000d, TPM key slot: 0xb LOG_DEBUG TSPI rpc/tcstp/rpc_auth.c:70 RPC_OSAP_TP: TCS Context: 0xa0b0fe15 LOG_DEBUG TSPI rpc/tcstp/rpc_seal.c:46 common_Seal_TP: TCS Context: 0xa0b0fe15 LOG_DEBUG TSPI rpc/tcstp/rpc_context.c:60 RPC_CloseContext_TP: TCS Context: 0xa0b0fe15 LOG_RETERR TSPI tspi_context.c:113: 0x126
其他的一些使用方法,可以tpm_unsealdata -h来找到方法。解密之后,diff对比下加密前的文件,可以看到两者是一样的。
模拟器和芯片的区别,就是秘钥的存放位置。