1、代码地址, GitHub - microsoft/ms-tpm-20-ref: Reference implementation of the TCG Trusted Platform Module 2.0 specification.Reference implementation of the TCG Trusted Platform Module 2.0 specification. - GitHub - microsoft/ms-tpm-20-ref: Reference implementation of the TCG Trusted Platform Module 2.0 specification.https://github.com/microsoft/ms-tpm-20-ref
2、解压后编译
(1) cd到TPMCmd目录下面, cd xxxx/ms-tpm-20-ref/ms-tpm-20-ref-main/TPMCmd
(2) ./bootstrap
./bootstrap 一般做了什么呢 ? :
./bootstrap 是一个常见的在 Unix 或 Linux 系统中用于初始化项目的脚本。具体做了什么取决于脚本的内容,但一般来说,它可能会做以下几件事:
1. 检查系统环境:检查是否安装了必要的软件和库,如果没有,可能会提示用户安装。
2. 配置项目:设置项目的环境变量,生成配置文件等。
3. 构建项目:编译源代码,生成可执行文件。
4. 安装依赖:如果项目有依赖其他库或者项目,./bootstrap 可能会自动下载并安装这些依赖。
这只是一般情况,具体的 ./bootstrap 脚本可能会根据项目的需要做更多的事情。如果你想了解一个具体的 ./bootstrap 脚本做了什么,你可以打开它看看脚本的内容。
(3)./configure
如果需要去掉warn当error ,打开Makefile文件 ,找到 EXTRA_CFLAGS 去掉 -Werror
如果需要添加其它main函数:
1、首先把TPMCmds.c中的main函数改个名比如main333
2、把 其它main函数拷贝到 TPMCmds.c中 或者 CryptRsa.c中 ,比如:
int
main(
int argc,
char* argv[]
)
{
int i;
int irc;
printf("C:begin genRsaKey\n");
UINT32 e = 0;
TPMT_PUBLIC publicArea;
TPMT_SENSITIVE sensitive;
RAND_STATE rand;
printf("C:keyLengthInBitssxxxx \n");
unsigned char c[] = { 0xBC, 0xEF, 0x25, 0xFD, 0xD9, 0xB2, 0xDF, 0x0C, 0xEE, 0x50, 0x1C, 0x5D, 0xBD, 0xB4, 0x6B, 0x66, 0x1F, 0xBB, 0xB6, 0xD8, 0x3F, 0x4B, 0xF3, 0x2B, 0xD7, 0x06, 0x65, 0x9A, 0xE0, 0x96, 0xBD, 0xA5, 0xC7, 0xBC, 0x17, 0xBF, 0x2E, 0x9B, 0x2D, 0x8E, 0x29, 0xF8, 0x99, 0x13, 0x77, 0xAC, 0xD0, 0x4B };
memset(rand.drbg.seed.bytes, 0,48);
memcpy(rand.drbg.seed.bytes, c, 48);
rand.drbg.magic = DRBG_MAGIC;
rand.drbg.reseedCounter = 0x0000000000000064;
publicArea.parameters.rsaDetail.exponent = 0;
publicArea.parameters.rsaDetail.keyBits = 2048;
publicArea.type = 1;
publicArea.nameAlg = 11;
publicArea.objectAttributes = 0x000300b2;
publicArea.authPolicy.t.size = 32;
TPMS_RSA_PARMS rsaParms = publicArea.parameters.rsaDetail;
rsaParms.keyBits = 0x800;
rsaParms.exponent = 0;
rsaParms.symmetric.algorithm = 0x06;
TPMU_SYM_KEY_BITS keyBits = rsaParms.symmetric.keyBits;
keyBits.aes = 0x80;
keyBits.camellia = 0x80;
keyBits.sym = 0x80;
keyBits.xor = 0x80;
TPMU_SYM_MODE mode = rsaParms.symmetric.mode;
mode.aes = 0x43;
mode.camellia = 0x43;
mode.sym = 0x43;
rsaParms.scheme.scheme = 0x10;
rsaParms.scheme.details.rsapss.hashAlg = 0;
sensitive.sensitiveType = 1;
sensitive.authValue.t.size = 32;
sensitive.seedValue.t.size = 0;
sensitive.sensitive.rsa.t.size = 0;
TPM_RC ret = CryptRsaGenerateKey(
&publicArea,
&sensitive,
&rand
);
printf("C:CryptRsaGenerateKey = %d \n", ret);
return ret;
}
(4)make
make 完成后会生成 xxx.a文件:
Platform/src/libplatform.a
tpm/src/libtpm.a
Platform/src/libplatform.a
(5)
make完成后能看到 生成libtpm.a 的指令:
ar cru tpm/src/libtpm.a tpm/src/X509/tpm_src_libtpm_a-TpmASN1.o tpm/src/X509/tpm_src_libtpm_a-X509_ECC.o tpm/src/X509/tpm_src_libtpm_a-X509_RSA.o tpm/src/X509/tpm_src_libtpm_a-X509_spt.o tpm/src/command/Asymmetric/tpm_src_libtpm_a-ECC_Decrypt.o tpm/src/command/Asymmetric/tpm_src_libtpm_a-ECC_Encrypt.o tpm/src/command/Asymmetric/tpm_src_libtpm_a-ECC_Parameters.o tpm/src/command/Asymmetric/tpm_src_libtpm_a-ECDH_KeyGen.o tpm/src/command/Asymmetric/tpm_src_libtpm_a-ECDH_ZGen.o tpm/src/command/Asymmetric/tpm_src_libtpm_a-EC_Ephemeral.o tpm/src/command/Asymmetric/tpm_src_libtpm_a-RSA_Decrypt.o tpm/src/command/Asymmetric/tpm_src_libtpm_a-RSA_Encrypt.o tpm/src/command/Asymmetric/tpm_src_libtpm_a-ZGen_2Phase.o tpm/src/command/AttachedComponent/tpm_src_libtpm_a-AC_GetCapability.o tpm/src/command/AttachedComponent/tpm_src_libtpm_a-AC_Send.o tpm/src/command/AttachedComponent/tpm_src_libtpm_a-AC_spt.o tpm/src/command/AttachedComponent/tpm_src_libtpm_a-Policy_AC_SendSelect.o tpm/src/command/Attestation/tpm_src_libtpm_a-Attest_spt.o tpm/src/command/Attestation/tpm_src_libtpm_a-Certify.o tpm/src/command/Attestation/tpm_src_libtpm_a-CertifyCreation.o tpm/src/command/Attestation/tpm_src_libtpm_a-CertifyX509.o tpm/src/command/Attestation/tpm_src_libtpm_a-GetCommandAuditDigest.o tpm/src/command/Attestation/tpm_src_libtpm_a-GetSessionAuditDigest.o tpm/src/command/Attestation/tpm_src_libtpm_a-GetTime.o tpm/src/command/Attestation/tpm_src_libtpm_a-Quote.o tpm/src/command/Capability/tpm_src_libtpm_a-GetCapability.o tpm/src/command/Capability/tpm_src_libtpm_a-TestParms.o tpm/src/command/ClockTimer/tpm_src_libtpm_a-ACT_SetTimeout.o tpm/src/command/ClockTimer/tpm_src_libtpm_a-ACT_spt.o tpm/src/command/ClockTimer/tpm_src_libtpm_a-ClockRateAdjust.o tpm/src/command/ClockTimer/tpm_src_libtpm_a-ClockSet.o tpm/src/command/ClockTimer/tpm_src_libtpm_a-ReadClock.o tpm/src/command/CommandAudit/tpm_src_libtpm_a-SetCommandCodeAuditStatus.o tpm/src/command/Context/tpm_src_libtpm_a-ContextLoad.o tpm/src/command/Context/tpm_src_libtpm_a-ContextSave.o tpm/src/command/Context/tpm_src_libtpm_a-Context_spt.o tpm/src/command/Context/tpm_src_libtpm_a-EvictControl.o tpm/src/command/Context/tpm_src_libtpm_a-FlushContext.o tpm/src/command/DA/tpm_src_libtpm_a-DictionaryAttackLockReset.o tpm/src/command/DA/tpm_src_libtpm_a-DictionaryAttackParameters.o tpm/src/command/Duplication/tpm_src_libtpm_a-Duplicate.o tpm/src/command/Duplication/tpm_src_libtpm_a-Import.o tpm/src/command/Duplication/tpm_src_libtpm_a-Rewrap.o tpm/src/command/EA/tpm_src_libtpm_a-PolicyAuthValue.o tpm/src/command/EA/tpm_src_libtpm_a-PolicyAuthorize.o tpm/src/command/EA/tpm_src_libtpm_a-PolicyAuthorizeNV.o tpm/src/command/EA/tpm_src_libtpm_a-PolicyCommandCode.o tpm/src/command/EA/tpm_src_libtpm_a-PolicyCounterTimer.o tpm/src/command/EA/tpm_src_libtpm_a-PolicyCpHash.o tpm/src/command/EA/tpm_src_libtpm_a-PolicyDuplicationSelect.o tpm/src/command/EA/tpm_src_libtpm_a-PolicyGetDigest.o tpm/src/command/EA/tpm_src_libtpm_a-PolicyLocality.o tpm/src/command/EA/tpm_src_libtpm_a-PolicyNV.o tpm/src/command/EA/tpm_src_libtpm_a-PolicyNameHash.o tpm/src/command/EA/tpm_src_libtpm_a-PolicyNvWritten.o tpm/src/command/EA/tpm_src_libtpm_a-PolicyOR.o tpm/src/command/EA/tpm_src_libtpm_a-PolicyPCR.o tpm/src/command/EA/tpm_src_libtpm_a-PolicyPassword.o tpm/src/command/EA/tpm_src_libtpm_a-PolicyPhysicalPresence.o tpm/src/command/EA/tpm_src_libtpm_a-PolicySecret.o tpm/src/command/EA/tpm_src_libtpm_a-PolicySigned.o tpm/src/command/EA/tpm_src_libtpm_a-PolicyTemplate.o tpm/src/command/EA/tpm_src_libtpm_a-PolicyTicket.o tpm/src/command/EA/tpm_src_libtpm_a-Policy_spt.o tpm/src/command/Ecdaa/tpm_src_libtpm_a-Commit.o tpm/src/command/FieldUpgrade/tpm_src_libtpm_a-FieldUpgradeData.o tpm/src/command/FieldUpgrade/tpm_src_libtpm_a-FieldUpgradeStart.o tpm/src/command/FieldUpgrade/tpm_src_libtpm_a-FirmwareRead.o tpm/src/command/HashHMAC/tpm_src_libtpm_a-EventSequenceComplete.o tpm/src/command/HashHMAC/tpm_src_libtpm_a-HMAC_Start.o tpm/src/command/HashHMAC/tpm_src_libtpm_a-HashSequenceStart.o tpm/src/command/HashHMAC/tpm_src_libtpm_a-MAC_Start.o tpm/src/command/HashHMAC/tpm_src_libtpm_a-SequenceComplete.o tpm/src/command/HashHMAC/tpm_src_libtpm_a-SequenceUpdate.o tpm/src/command/Hierarchy/tpm_src_libtpm_a-ChangeEPS.o tpm/src/command/Hierarchy/tpm_src_libtpm_a-ChangePPS.o tpm/src/command/Hierarchy/tpm_src_libtpm_a-Clear.o tpm/src/command/Hierarchy/tpm_src_libtpm_a-ClearControl.o tpm/src/command/Hierarchy/tpm_src_libtpm_a-CreatePrimary.o tpm/src/command/Hierarchy/tpm_src_libtpm_a-HierarchyChangeAuth.o tpm/src/command/Hierarchy/tpm_src_libtpm_a-HierarchyControl.o tpm/src/command/Hierarchy/tpm_src_libtpm_a-SetPrimaryPolicy.o tpm/src/command/Misc/tpm_src_libtpm_a-PP_Commands.o tpm/src/command/Misc/tpm_src_libtpm_a-SetAlgorithmSet.o tpm/src/command/NVStorage/tpm_src_libtpm_a-NV_Certify.o tpm/src/command/NVStorage/tpm_src_libtpm_a-NV_ChangeAuth.o tpm/src/command/NVStorage/tpm_src_libtpm_a-NV_DefineSpace.o tpm/src/command/NVStorage/tpm_src_libtpm_a-NV_Extend.o tpm/src/command/NVStorage/tpm_src_libtpm_a-NV_GlobalWriteLock.o tpm/src/command/NVStorage/tpm_src_libtpm_a-NV_Increment.o tpm/src/command/NVStorage/tpm_src_libtpm_a-NV_Read.o tpm/src/command/NVStorage/tpm_src_libtpm_a-NV_ReadLock.o tpm/src/command/NVStorage/tpm_src_libtpm_a-NV_ReadPublic.o tpm/src/command/NVStorage/tpm_src_libtpm_a-NV_SetBits.o tpm/src/command/NVStorage/tpm_src_libtpm_a-NV_UndefineSpace.o tpm/src/command/NVStorage/tpm_src_libtpm_a-NV_UndefineSpaceSpecial.o tpm/src/command/NVStorage/tpm_src_libtpm_a-NV_Write.o tpm/src/command/NVStorage/tpm_src_libtpm_a-NV_WriteLock.o tpm/src/command/NVStorage/tpm_src_libtpm_a-NV_spt.o tpm/src/command/Object/tpm_src_libtpm_a-ActivateCredential.o tpm/src/command/Object/tpm_src_libtpm_a-Create.o tpm/src/command/Object/tpm_src_libtpm_a-CreateLoaded.o tpm/src/command/Object/tpm_src_libtpm_a-Load.o tpm/src/command/Object/tpm_src_libtpm_a-LoadExternal.o tpm/src/command/Object/tpm_src_libtpm_a-MakeCredential.o tpm/src/command/Object/tpm_src_libtpm_a-ObjectChangeAuth.o tpm/src/command/Object/tpm_src_libtpm_a-Object_spt.o tpm/src/command/Object/tpm_src_libtpm_a-ReadPublic.o tpm/src/command/Object/tpm_src_libtpm_a-Unseal.o tpm/src/command/PCR/tpm_src_libtpm_a-PCR_Allocate.o tpm/src/command/PCR/tpm_src_libtpm_a-PCR_Event.o tpm/src/command/PCR/tpm_src_libtpm_a-PCR_Extend.o tpm/src/command/PCR/tpm_src_libtpm_a-PCR_Read.o tpm/src/command/PCR/tpm_src_libtpm_a-PCR_Reset.o tpm/src/command/PCR/tpm_src_libtpm_a-PCR_SetAuthPolicy.o tpm/src/command/PCR/tpm_src_libtpm_a-PCR_SetAuthValue.o tpm/src/command/Random/tpm_src_libtpm_a-GetRandom.o tpm/src/command/Random/tpm_src_libtpm_a-StirRandom.o tpm/src/command/Session/tpm_src_libtpm_a-PolicyRestart.o tpm/src/command/Session/tpm_src_libtpm_a-StartAuthSession.o tpm/src/command/Signature/tpm_src_libtpm_a-Sign.o tpm/src/command/Signature/tpm_src_libtpm_a-VerifySignature.o tpm/src/command/Startup/tpm_src_libtpm_a-Shutdown.o tpm/src/command/Startup/tpm_src_libtpm_a-Startup.o tpm/src/command/Symmetric/tpm_src_libtpm_a-EncryptDecrypt.o tpm/src/command/Symmetric/tpm_src_libtpm_a-EncryptDecrypt2.o tpm/src/command/Symmetric/tpm_src_libtpm_a-EncryptDecrypt_spt.o tpm/src/command/Symmetric/tpm_src_libtpm_a-HMAC.o tpm/src/command/Symmetric/tpm_src_libtpm_a-Hash.o tpm/src/command/Symmetric/tpm_src_libtpm_a-MAC.o tpm/src/command/Testing/tpm_src_libtpm_a-GetTestResult.o tpm/src/command/Testing/tpm_src_libtpm_a-IncrementalSelfTest.o tpm/src/command/Testing/tpm_src_libtpm_a-SelfTest.o tpm/src/command/Vendor/tpm_src_libtpm_a-Vendor_TCG_Test.o tpm/src/crypt/tpm_src_libtpm_a-AlgorithmTests.o tpm/src/crypt/tpm_src_libtpm_a-BnConvert.o tpm/src/crypt/tpm_src_libtpm_a-BnMath.o tpm/src/crypt/tpm_src_libtpm_a-BnMemory.o tpm/src/crypt/tpm_src_libtpm_a-CryptCmac.o tpm/src/crypt/tpm_src_libtpm_a-CryptDes.o tpm/src/crypt/tpm_src_libtpm_a-CryptEccCrypt.o tpm/src/crypt/tpm_src_libtpm_a-CryptEccData.o tpm/src/crypt/tpm_src_libtpm_a-CryptEccKeyExchange.o tpm/src/crypt/tpm_src_libtpm_a-CryptEccMain.o tpm/src/crypt/tpm_src_libtpm_a-CryptEccSignature.o tpm/src/crypt/tpm_src_libtpm_a-CryptHash.o tpm/src/crypt/tpm_src_libtpm_a-CryptPrime.o tpm/src/crypt/tpm_src_libtpm_a-CryptPrimeSieve.o tpm/src/crypt/tpm_src_libtpm_a-CryptRand.o tpm/src/crypt/tpm_src_libtpm_a-CryptRsa.o tpm/src/crypt/tpm_src_libtpm_a-CryptSelfTest.o tpm/src/crypt/tpm_src_libtpm_a-CryptSmac.o tpm/src/crypt/tpm_src_libtpm_a-CryptSym.o tpm/src/crypt/tpm_src_libtpm_a-CryptUtil.o tpm/src/crypt/tpm_src_libtpm_a-PrimeData.o tpm/src/crypt/tpm_src_libtpm_a-RsaKeyCache.o tpm/src/crypt/tpm_src_libtpm_a-Ticket.o tpm/src/crypt/ltc/tpm_src_libtpm_a-TpmToLtcDesSupport.o tpm/src/crypt/ltc/tpm_src_libtpm_a-TpmToLtcMath.o tpm/src/crypt/ltc/tpm_src_libtpm_a-TpmToLtcSupport.o tpm/src/crypt/ossl/tpm_src_libtpm_a-TpmToOsslDesSupport.o tpm/src/crypt/ossl/tpm_src_libtpm_a-TpmToOsslMath.o tpm/src/crypt/ossl/tpm_src_libtpm_a-TpmToOsslSupport.o tpm/src/crypt/wolf/tpm_src_libtpm_a-TpmToWolfDesSupport.o tpm/src/crypt/wolf/tpm_src_libtpm_a-TpmToWolfMath.o tpm/src/crypt/wolf/tpm_src_libtpm_a-TpmToWolfSupport.o tpm/src/events/tpm_src_libtpm_a-_TPM_Hash_Data.o tpm/src/events/tpm_src_libtpm_a-_TPM_Hash_End.o tpm/src/events/tpm_src_libtpm_a-_TPM_Hash_Start.o tpm/src/events/tpm_src_libtpm_a-_TPM_Init.o tpm/src/main/tpm_src_libtpm_a-CommandDispatcher.o tpm/src/main/tpm_src_libtpm_a-ExecCommand.o tpm/src/main/tpm_src_libtpm_a-SessionProcess.o tpm/src/subsystem/tpm_src_libtpm_a-CommandAudit.o tpm/src/subsystem/tpm_src_libtpm_a-DA.o tpm/src/subsystem/tpm_src_libtpm_a-Hierarchy.o tpm/src/subsystem/tpm_src_libtpm_a-NvDynamic.o tpm/src/subsystem/tpm_src_libtpm_a-NvReserved.o tpm/src/subsystem/tpm_src_libtpm_a-Object.o tpm/src/subsystem/tpm_src_libtpm_a-PCR.o tpm/src/subsystem/tpm_src_libtpm_a-PP.o tpm/src/subsystem/tpm_src_libtpm_a-Session.o tpm/src/subsystem/tpm_src_libtpm_a-Time.o tpm/src/support/tpm_src_libtpm_a-AlgorithmCap.o tpm/src/support/tpm_src_libtpm_a-Bits.o tpm/src/support/tpm_src_libtpm_a-CommandCodeAttributes.o tpm/src/support/tpm_src_libtpm_a-Entity.o tpm/src/support/tpm_src_libtpm_a-Global.o tpm/src/support/tpm_src_libtpm_a-Handle.o tpm/src/support/tpm_src_libtpm_a-IoBuffers.o tpm/src/support/tpm_src_libtpm_a-Locality.o tpm/src/support/tpm_src_libtpm_a-Manufacture.o tpm/src/support/tpm_src_libtpm_a-Marshal.o tpm/src/support/tpm_src_libtpm_a-MathOnByteBuffers.o tpm/src/support/tpm_src_libtpm_a-Memory.o tpm/src/support/tpm_src_libtpm_a-Power.o tpm/src/support/tpm_src_libtpm_a-PropertyCap.o tpm/src/support/tpm_src_libtpm_a-Response.o tpm/src/support/tpm_src_libtpm_a-ResponseCodeProcessing.o tpm/src/support/tpm_src_libtpm_a-TableDrivenMarshal.o tpm/src/support/tpm_src_libtpm_a-TableMarshalData.o tpm/src/support/tpm_src_libtpm_a-TpmFail.o tpm/src/support/tpm_src_libtpm_a-TpmSizeChecks.o
5.1 如果需要把tpm模块编译成 可执行文件则:
ar指令前面改成: gcc -fPIC -L Platform/src/ -L tpm/src/ -o tpm/src/libtpmExeMS
后面添加: -lcrypto -lplatform -ltpm (注意不能在前面加,否则会报符号找不到)
5.2 如果需要编译成 动态库.so文件则:
ar指令前面改成:gcc -fPIC -L Platform/src/ -L tpm/src/ -shared -o tpm/src/libtpm2.so
后面添加: -lcrypto -lplatform -ltpm (注意不能在前面加,否则会报符号找不到)
5.3 make后会把Simulator模块编译成可执行文件
gcc -std=gnu11 -Wall -Wformat-security -fstack-protector-all -fPIC -Wno-error=empty-body -Wno-error=parentheses -Wno-error=pointer-to-int-cast -Wno-error=missing-braces -Wno-error=unused-result -I ./Platform/include -I ./Platform/include/prototypes -I ./tpm/include -I ./tpm/include/prototypes -I ./Simulator/include -I ./Simulator/include/prototypes -pthread -g -O2 -o Simulator/src/tpm2-simulator Simulator/src/Simulator_src_tpm2_simulator-TPMCmdp.o Simulator/src/Simulator_src_tpm2_simulator-TPMCmds.o Simulator/src/Simulator_src_tpm2_simulator-TcpServer.o Platform/src/libplatform.a tpm/src/libtpm.a Platform/src/libplatform.a -lcrypto
其中,
输出为:Simulator/src/tpm2-simulator
输入为;
Simulator 目录下的3个.o文件
3个静态库.a和openssl库crypto