xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/tspi/tsp_selftest.c (revision 2d5f7628c5531eb583b9313ac2fd1cf8582b4479)
1 
2 /*
3  * Licensed Materials - Property of IBM
4  *
5  * trousers - An open source TCG Software Stack
6  *
7  * (C) Copyright International Business Machines Corp. 2007
8  *
9  */
10 
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <inttypes.h>
15 
16 #include "trousers/tss.h"
17 #include "trousers/trousers.h"
18 #include "trousers_types.h"
19 #include "spi_utils.h"
20 #include "capabilities.h"
21 #include "tsplog.h"
22 #include "obj.h"
23 
24 
25 #ifdef TSS_BUILD_TRANSPORT
26 TSS_RESULT
Transport_SelfTestFull(TSS_HCONTEXT tspContext)27 Transport_SelfTestFull(TSS_HCONTEXT tspContext)
28 {
29 	TSS_RESULT result;
30 	TCS_HANDLE handlesLen = 0;
31 
32 	if ((result = obj_context_transport_init(tspContext)))
33 		return result;
34 
35 	LogDebugFn("Executing in a transport session");
36 
37 	return obj_context_transport_execute(tspContext, TPM_ORD_SelfTestFull, 0, NULL, NULL,
38 					     &handlesLen, NULL, NULL, NULL, NULL, NULL);
39 }
40 
41 TSS_RESULT
Transport_CertifySelfTest(TSS_HCONTEXT tspContext,TCS_KEY_HANDLE keyHandle,TCPA_NONCE antiReplay,TPM_AUTH * privAuth,UINT32 * sigSize,BYTE ** sig)42 Transport_CertifySelfTest(TSS_HCONTEXT tspContext,	/* in */
43 			  TCS_KEY_HANDLE keyHandle,	/* in */
44 			  TCPA_NONCE antiReplay,	/* in */
45 			  TPM_AUTH * privAuth,	/* in, out */
46 			  UINT32 * sigSize,	/* out */
47 			  BYTE ** sig)	/* out */
48 {
49 	TSS_RESULT result;
50 	UINT32 handlesLen, decLen = 0;
51 	BYTE *dec = NULL;
52 	UINT64 offset;
53 	TPM_DIGEST pubKeyHash;
54 	Trspi_HashCtx hashCtx;
55 	TCS_HANDLE *handles, handle;
56 
57 	if ((result = obj_context_transport_init(tspContext)))
58 		return result;
59 
60 	if ((result = obj_tcskey_get_pubkeyhash(keyHandle, pubKeyHash.digest)))
61 		return result;
62 
63 	result = Trspi_HashInit(&hashCtx, TSS_HASH_SHA1);
64 	result |= Trspi_Hash_DIGEST(&hashCtx, pubKeyHash.digest);
65 	if ((result |= Trspi_HashFinal(&hashCtx, pubKeyHash.digest)))
66 		return result;
67 
68 	handlesLen = 1;
69 	handle = keyHandle;
70 	handles = &handle;
71 
72 	LogDebugFn("Executing in a transport session");
73 
74 	if ((result = obj_context_transport_execute(tspContext, TPM_ORD_CertifySelfTest,
75 						    sizeof(TCPA_NONCE), antiReplay.nonce,
76 						    &pubKeyHash, &handlesLen, &handles, privAuth,
77 						    NULL, &decLen, &dec)))
78 		return result;
79 
80 	offset = 0;
81 	Trspi_UnloadBlob_UINT32(&offset, sigSize, dec);
82 
83 	if ((*sig = malloc(*sigSize)) == NULL) {
84 		free(dec);
85 		LogError("malloc of %u bytes failed", *sigSize);
86 		*sigSize = 0;
87 		return TSPERR(TSS_E_OUTOFMEMORY);
88 	}
89 	Trspi_UnloadBlob(&offset, *sigSize, dec, *sig);
90 
91 	free(dec);
92 
93 	return result;
94 }
95 
96 TSS_RESULT
Transport_GetTestResult(TSS_HCONTEXT tspContext,UINT32 * outDataSize,BYTE ** outData)97 Transport_GetTestResult(TSS_HCONTEXT tspContext,	/* in */
98 			UINT32 * outDataSize,	/* out */
99 			BYTE ** outData)	/* out */
100 {
101 	TSS_RESULT result;
102 	UINT32 decLen = 0;
103 	BYTE *dec = NULL;
104 	UINT64 offset;
105 	TCS_HANDLE handlesLen = 0;
106 
107 	if ((result = obj_context_transport_init(tspContext)))
108 		return result;
109 
110 	LogDebugFn("Executing in a transport session");
111 
112 	if ((result = obj_context_transport_execute(tspContext, TPM_ORD_GetTestResult, 0, NULL,
113 						    NULL, &handlesLen, NULL, NULL, NULL, &decLen,
114 						    &dec)))
115 		return result;
116 
117 	offset = 0;
118 	Trspi_UnloadBlob_UINT32(&offset, outDataSize, dec);
119 
120 	if ((*outData = malloc(*outDataSize)) == NULL) {
121 		free(dec);
122 		LogError("malloc of %u bytes failed", *outDataSize);
123 		*outDataSize = 0;
124 		return TSPERR(TSS_E_OUTOFMEMORY);
125 	}
126 	Trspi_UnloadBlob(&offset, *outDataSize, dec, *outData);
127 
128 	free(dec);
129 
130 	return result;
131 }
132 #endif
133