我正在使用TrouSerS库。我想写一个应用程序,它将绑定AES密钥并将此有界密钥放入文件和第二个应用程序,它将从文件解密此密钥。
这是我的代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <tss/tss_error.h>
#include <tss/platform.h>
#include <tss/tss_defines.h>
#include <tss/tss_typedef.h>
#include <tss/tss_structs.h>
#include <tss/tspi.h>
#include <trousers/trousers.h>
#define DBG(message, tResult) printf("Line%d, %s) %s returned 0x%08x. %s.\n", __LINE__, __func__, message, tResult,(char *)Trspi_Error_String(tResult));
int main(int argc, char **argv){
TSS_HCONTEXT hContext=0;
TSS_HTPM hTPM=0;
TSS_RESULT result;
TSS_HKEY hSRK=0;
TSS_HPOLICY hSRKPolicy=0;
TSS_HPOLICY hTPMPolicy=0;
TSS_UUID SRK_UUID=TSS_UUID_SRK;
BYTE wks[20];
memset(wks,0,20);
result=Tspi_Context_Create(&hContext);
result=Tspi_Context_Connect(hContext, NULL);
result=Tspi_Context_GetTpmObject(hContext, &hTPM);
result=Tspi_Context_LoadKeyByUUID(hContext,TSS_PS_TYPE_SYSTEM, SRK_UUID, &hSRK);
result=Tspi_GetPolicyObject(hSRK, TSS_POLICY_USAGE, &hSRKPolicy);
result=Tspi_Policy_SetSecret(hSRKPolicy, TSS_SECRET_MODE_SHA1,20,wks);
UINT32 ulDataLength;
BYTE *rgbBoundData;
FILE *fin;
TSS_HKEY hBindingKey;
BYTE newPubKey[284];
fin=fopen("Bind.pub","r");
read(fileno(fin),newPubKey,284);
fclose(fin);
TSS_FLAG initFlags;
initFlags=TSS_KEY_TYPE_BIND|TSS_KEY_SIZE_2048|TSS_KEY_AUTHORIZATION|TSS_KEY_NOT_MIGRATABLE;
result=Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_RSAKEY, initFlags, &hBindingKey);
DBG("",result);
result=Tspi_SetAttribData(hBindingKey, TSS_TSPATTRIB_KEY_BLOB, TSS_TSPATTRIB_KEYBLOB_PUBLIC_KEY, 284, newPubKey);
DBG("",result);
BYTE encData[7];
TSS_HENCDATA hEncData;
fin=fopen("AES.key","rb");
read(fileno(fin),encData,7);
fclose(fin);
result=Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_ENCDATA, TSS_ENCDATA_BIND, &hEncData);
DBG("",result);
result=Tspi_Data_Bind(hEncData, hBindingKey, 7, encData);
DBG("",result);
result=Tspi_GetAttribData(hEncData, TSS_TSPATTRIB_ENCDATA_BLOB, TSS_TSPATTRIB_ENCDATABLOB_BLOB, &ulDataLength, &rgbBoundData);
DBG("",result);
FILE *fout;
fout=fopen("Bound.data","wb");
write(fileno(fout),rgbBoundData,ulDataLength);
fclose(fout);
Tspi_Context_Close(hSRKPolicy);
DBG("", result);
Tspi_Context_FreeMemory(hContext, NULL);
DBG("", result);
Tspi_Context_Close(hContext);
DBG("", result);
return 0;
}
它正在执行而没有任何错误。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <tss/tss_error.h>
#include <tss/platform.h>
#include <tss/tss_defines.h>
#include <tss/tss_typedef.h>
#include <tss/tss_structs.h>
#include <tss/tspi.h>
#include <trousers/trousers.h>
#define DBG(message, tResult) printf("Line%d, %s) %s returned 0x%08x. %s.\n", __LINE__, __func__, message, tResult,(char *)Trspi_Error_String(tResult));
int main(int argc, char **argv){
TSS_HCONTEXT hContext=0;
TSS_HTPM hTPM=0;
TSS_RESULT result;
TSS_HKEY hSRK=0;
TSS_HPOLICY hSRKPolicy=0;
TSS_HPOLICY hTPMPolicy=0;
TSS_UUID SRK_UUID=TSS_UUID_SRK;
BYTE wks[20];
memset(wks,0,20);
result=Tspi_Context_Create(&hContext);
result=Tspi_Context_Connect(hContext, NULL);
result=Tspi_Context_GetTpmObject(hContext, &hTPM);
result=Tspi_Context_LoadKeyByUUID(hContext,TSS_PS_TYPE_SYSTEM, SRK_UUID, &hSRK);
result=Tspi_GetPolicyObject(hSRK, TSS_POLICY_USAGE, &hSRKPolicy);
result=Tspi_Policy_SetSecret(hSRKPolicy, TSS_SECRET_MODE_SHA1,20,wks);
UINT32 encLen=256;
BYTE encryptedData[256], *pointer;
BYTE *rgbDataunBound;
UINT32 ulDataLength;
TSS_UUID MY_UUID={0,0,0,0,0,{0,0,0,2,11}};
FILE *fin;
FILE *fout;
TSS_HKEY hUnbindKey=0;
fin=fopen("Bound.data","rb");
read(fileno(fin),encryptedData,&ulDataLength);
fclose(fin);
TSS_HENCDATA hEncData;
result=Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_ENCDATA, TSS_ENCDATA_BIND, &hEncData);
DBG("",result);
result=Tspi_SetAttribData(hEncData, TSS_TSPATTRIB_ENCDATA_BLOB,TSS_TSPATTRIB_ENCDATABLOB_BLOB,256,encryptedData);
DBG("",result);
result=Tspi_Context_GetKeyByUUID(hContext, TSS_PS_TYPE_SYSTEM, MY_UUID, &hUnbindKey);
DBG("",result);
result=Tspi_Key_LoadKey(hUnbindKey,hSRK);
DBG("",result);
TSS_HPOLICY hUnbindPolicy;
result=Tspi_GetPolicyObject(hUnbindKey, TSS_POLICY_USAGE, &hUnbindPolicy);
DBG("",result);
result=Tspi_Policy_SetSecret(hUnbindPolicy,TSS_SECRET_MODE_PLAIN,3,"123");
DBG("",result);
result=Tspi_Data_Unbind(hEncData, hUnbindKey, &ulDataLength, &rgbDataunBound);
DBG("",result);
Tspi_Context_Close(hSRKPolicy);
DBG("", result);
Tspi_Context_FreeMemory(hContext, NULL);
DBG("", result);
Tspi_Context_Close(hContext);
DBG("", result);
return 0;
}
在这里,我得到“0x00000021。解密错误。”在Tspi_Data_Unbind函数之后。
你能帮助我吗?