/*******Sign/verify Code************/
int
main(void)
{
TSS_HCONTEXT hContext;
TSS_HHASH hHash;
TSS_HKEY hSigningKey, hSRK;
TSS HPOLICY hPolicy;
TSS_UUID srkUUID = TSS_UUID_SRK;
BYTE secret[] = TSS_WELL_KNOWN_SECRET;
UINT32 sigLen;
BYTE *sig;
BYTE hash[] =
{0x32, 0xd1, 0x0c, 0x7b, 0x8c, 0xf9, 0x65, 0x70, 0xca, 0x04,
0xce, 0x37, 0xf2, 0xa1, 0x9d, 0x84, 0x24, 0x0d, 0x3a, 0x89};
/* create context and connect */
Tspi_Context_Create(&hContext);
Tspi_Context_Connect(hContext, remote-pc);
/* create a signing key under the SRK */
Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_POLICY,
TSS_POLICY_USAGE, &hPolicy);
Tspi_Policy_SetSecret(hPolicy, TSS_SECRET_MODE_SHA1, 20, secret);
Tspi_Context_GetKeyByUUID(hContext, TSS_PS_TYPE_SYSTEM, srkUUID, &hSRK);
Tspi_Policy_AssignToObject(hPolicy,hSRK);
Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_RSAKEY,
TSS_KEY_TYPE_SIGNING |
TSS_KEY_SIZE_2048 |
TSS_KEY_AUTHORIZATION |
TSS_KEY_NOT_MIGRATABLE,
&hSigningKey);
Tspi_Policy_AssignToObject(hPolicy, hSigningKey);
Tspi_Key_CreateKey(hSigningKey, hSRK, 0);
Tspi_Key_LoadKey(hSigningKey, hSRK);
/* open valid hash object */
Tspi_Context_CreateObject(hContext,TSS_OBJECT_TYPE_HASH,TSS_HASH_SHA1,
&hHash);
/* set hash value and get valid signature */
Tspi_Hash_SetHashValue(hHash, sizeof(hash), hash);
Tspi_Hash_Sign(hHash, hSigningKey, &sigLen, &sig);
/* verify signature */
Tspi_Hash_VerifySignature(hHash, hSigningKey, sigLen, sig);
/* free sig memory, close signing key object and context */
Tspi Context FreeMemory(hContext sig);
Tspi_Context_CloseObject(hContext, hSigningKey);
/* close context */
Tspi_Context_Close(hContext);
// we forgot to unload the signing key, but the TSS did it for us
// when we closed the context
return 0;
}
|