Binding & Unbinding with TrouSerS library

0

I'm using TrouSerS library. I want to write one app which will bind AES key and put this bounded key into file and second app which will decrypt this key from file.

Here is my codes:

#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;
}

It's executing without any errors.

#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;
}

And here I'm getting "0x00000021. Decryption error." after Tspi_Data_Unbind function.

Could you help me, please?

c++
c
rsa
tpm
trusted-computing
asked on Stack Overflow Apr 12, 2017 by Adam S

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0