1 
2 #include <stdlib.h>
3 #include <string.h>
4 #include <trousers/tss.h>
5 #include <trousers/trousers.h>
6 #include "spi_internal_types.h"
7 #include <spi_utils.h>
8 #include <obj.h>
9 #include "tsplog.h"
10 #include "daa_parameter.h"
11 
12 setenv("TCSD_FOREGROUND", "1", 1);
13 
14 // simulating Tspi_TPM_DAA_JoinInit (spi_daa.c)
Tspi_DAA_Join(TSS_HTPM hTPM,int stage,UINT32 inputSize0,BYTE * inputData0,UINT32 inputSize1,BYTE * inputData1,UINT32 * outputSize,BYTE ** outputData)15 TSS_RESULT Tspi_DAA_Join(TSS_HTPM hTPM, int stage, UINT32 inputSize0, BYTE *inputData0, UINT32 inputSize1, BYTE *inputData1, UINT32 *outputSize, BYTE **outputData) {
16 	TSS_RESULT result;
17 	TCS_CONTEXT_HANDLE tcsContext;
18 	TSS_HCONTEXT tspContext;
19 	TSS_HPOLICY hPolicy;
20 	TCPA_DIGEST digest;
21 	TPM_AUTH ownerAuth;
22 	UINT16 offset = 0;
23 	BYTE hashblob[1000];
24 
25 	printf("[%s:%d] obj_tpm_is_connected(hTPM)\n", __FILE__, __LINE__);
26 	if( (result = obj_tpm_is_connected(  hTPM, &tcsContext)) != TSS_SUCCESS) return result;
27 	printf("[%s:%d] obj_tpm_get_tsp_context(hTPM)\n", __FILE__, __LINE__);
28 	if( (result = obj_tpm_get_tsp_context( hTPM, &tspContext)) != TSS_SUCCESS) return result;
29 	printf("[%s:%d] obj_tpm_get_policy(hTPM)\n", __FILE__, __LINE__);
30 	if( (result = obj_tpm_get_policy( hTPM, &hPolicy)) != TSS_SUCCESS) return result;
31 
32 	printf("[%s:%d] Trspi_LoadBlob_UINT32(&offset, TPM_ORD_DAA_Join, hashblob)\n", __FILE__, __LINE__);
33 	Trspi_LoadBlob_UINT32(&offset, TPM_ORD_DAA_Join, hashblob); // hash TPM_COMMAND_CODE
34 	printf("[%s:%d] Trspi_Hash(TSS_HASH_SHA1, offset, hashblob, digest.digest)\n",__FILE__, __LINE__);
35 	Trspi_LoadBlob_BYTE(&offset, stage, hashblob); // hash stage
36 	printf("[%s:%d] Trspi_LoadBlob_UINT32(&offset, 0, hashblob)\n",__FILE__, __LINE__);
37 	//TODO old 4
38 	Trspi_LoadBlob_UINT32(&offset, inputSize0, hashblob); // hash inputSize0
39 	printf("[%s:%d] Trspi_LoadBlob_UINT32(&offset, 0, hashblob)\n",__FILE__, __LINE__);
40 	Trspi_LoadBlob( &offset, inputSize0, hashblob, inputData0); // hash inputData0
41 	//TODO old 1
42 	Trspi_LoadBlob_UINT32(&offset, inputSize1, hashblob); // hash inputSize1
43 	printf("[%s:%d] Trspi_LoadBlob_UINT32(&offset, 0, hashblob)\n",__FILE__, __LINE__);
44 	Trspi_LoadBlob( &offset, inputSize1, hashblob, inputData1); // hash inputData1
45 	Trspi_Hash(TSS_HASH_SHA1, offset, hashblob, digest.digest);
46 
47 	if ((result = secret_PerformAuth_OIAP(hTPM, TPM_ORD_DAA_Join,
48 	     hPolicy, &digest,
49 	     &ownerAuth)) != TSS_SUCCESS) return result;
50 	printf("[%s:%d] secret_PerformAuth_OIAP(hTPM, TPM_ORD_DAA_Join ret=%d\n",__FILE__, __LINE__, result);
51 	// out
52 
53     /* step of the following call:
54 	TCSP_DAAJoin 		tcsd_api/calltcsapi.c (define in spi_utils.h)
55 	TCSP_DAAJoin_TP 	tcsd_api/tcstp.c (define in	trctp.h)
56     */
57 
58 	printf("[%s:%d] TCSP_DAAJoin(%x,%x,%x,%x,%x,%x,%x)\n",__FILE__, __LINE__,
59 	       (int)hTPM, 0, inputSize0,(int)inputData0,inputSize1,(int)inputData1,(int)&ownerAuth);
60 	if ( (result =  TCSP_DaaJoin( tcsContext, hTPM, 0, inputSize0, inputData0, inputSize1, inputData1, &ownerAuth, outputSize, outputData)) != TSS_SUCCESS)
61 		return result;
62 
63 	offset = 0;
64 	Trspi_LoadBlob_UINT32(&offset, result, hashblob);
65 	Trspi_LoadBlob_UINT32(&offset, TPM_ORD_DAA_Join, hashblob);
66 	Trspi_LoadBlob_UINT32(&offset, *outputSize, hashblob);
67 	Trspi_LoadBlob(&offset, *outputSize, hashblob, *outputData);
68 	Trspi_Hash(TSS_HASH_SHA1, offset, hashblob, digest.digest);
69 	if( (result = obj_policy_validate_auth_oiap( hPolicy, &digest, &ownerAuth)))
70 	{
71 		printf("[%s:%d] obj_policy_validate_auth=%d\n",__FILE__, __LINE__, result);
72 	}
73 	return result;
74 }
75 
76 
main(int argc,char * argv[])77 int main(int argc, char *argv[])
78 {
79     TSS_HCONTEXT hContext;
80     TSS_RESULT result;
81     TSS_HTPM hTPM;
82     TSS_HPOLICY hPolicy;
83 
84     // Create Context
85     printf("Create Context\n");
86     result = Tspi_Context_Create( &hContext );
87     if ( result != TSS_SUCCESS )
88     {
89         fprintf( stderr, "Tspi_Context_Create %d\n", result );
90         exit( result );
91     }
92 
93     // Connect to Context
94     printf("\nConnect to the context\n");
95     result = Tspi_Context_Connect( hContext, NULL );
96     if ( result != TSS_SUCCESS ) goto out_close;
97 
98     if( (result = Tspi_Context_GetTpmObject( hContext, &hTPM)) != TSS_SUCCESS)
99         goto out_close;
100 
101     // Get the correct policy using the TPM ownership PASSWD
102     char *szTpmPasswd = "OWN_PWD";
103     if( (result = Tspi_GetPolicyObject( hTPM, TSS_POLICY_USAGE, &hPolicy)) != TSS_SUCCESS)
104         goto out_close;
105     //BUSS
106     if( (result = Tspi_Policy_SetSecret( hPolicy, TSS_SECRET_MODE_PLAIN, strlen( szTpmPasswd), szTpmPasswd)) != TSS_SUCCESS)
107         goto out_close;
108     printf("Tspi_Policy_SetSecret hPolicy received;%d\n", hPolicy);
109 
110      //BUSS
111     // in
112     //int modulus_length = DAA_PARAM_SIZE_MODULUS_GAMMA / 8;
113     UINT32 inputSize0 = sizeof(int);
114     UINT32 inputSize1 = 0;
115     UINT32 outputSize = 0;
116     int ia_length = 7;
117     BYTE *inputData0 = (BYTE *)(&ia_length);//= (BYTE *)malloc( inputSize0)
118     BYTE *inputData1 = NULL;
119     BYTE *outputData = NULL;
120 
121     if( (result = Tspi_DAA_Join(hTPM, 0, inputSize0, inputData0, inputSize1, inputData1, &outputSize, &outputData)) != TSS_SUCCESS) goto out_close;
122 
123     goto out;
124 out_close:
125 	printf( "Tspi Error:%d - %s\n", result, err_string( result) );
126 
127 out:
128 	printf("ouputSize=%d\n", outputSize);
129 	if( outputData != NULL) {
130 		int i;
131 		printf("outputData(hex  )=[\n");
132 		for( i=0; i<(int)outputSize; i++) printf("%x ", outputData[i]);
133 		printf("\n]");
134 		printf("outputData(ascii)=[\n");
135 		for( i=0; i<(int)outputSize; i++) printf("%c ", outputData[i]);
136 		printf("\n]");
137 	}
138     Tspi_Context_FreeMemory( hContext, NULL );
139     Tspi_Context_Close( hContext );
140     printf("[%s:%d] THE END\n",__FILE__, __LINE__);
141     return result;
142 }
143