xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/tcs/tcsi_counter.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 
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 
16 #include "trousers/tss.h"
17 #include "trousers_types.h"
18 #include "tcs_tsp.h"
19 #include "tcs_utils.h"
20 #include "tcs_int_literals.h"
21 #include "capabilities.h"
22 #include "tcslog.h"
23 #include "tcsps.h"
24 #include "req_mgr.h"
25 
26 
27 TSS_RESULT
TCSP_ReadCounter_Internal(TCS_CONTEXT_HANDLE hContext,TSS_COUNTER_ID idCounter,TPM_COUNTER_VALUE * counterValue)28 TCSP_ReadCounter_Internal(TCS_CONTEXT_HANDLE hContext,
29 			  TSS_COUNTER_ID     idCounter,
30 			  TPM_COUNTER_VALUE* counterValue)
31 {
32 	TSS_RESULT result;
33 	UINT32 paramSize;
34 	UINT64 offset = 0;
35 	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
36 
37 	if ((result = ctx_verify_context(hContext)))
38 		return result;
39 
40 	if ((result = tpm_rqu_build(TPM_ORD_ReadCounter, &offset, txBlob, idCounter, NULL)))
41 		return result;
42 
43 	if ((result = req_mgr_submit_req(txBlob)))
44 		goto out;
45 
46 	if ((result = UnloadBlob_Header(txBlob, &paramSize))) {
47 		LogDebugFn("TPM_ReadCounter failed: rc=0x%x", result);
48 		goto out;
49 	}
50 
51 	if (!result) {
52 		result = tpm_rsp_parse(TPM_ORD_ReadCounter, txBlob, paramSize, NULL, counterValue,
53 				       NULL);
54 	}
55 
56 out:
57 	return result;
58 }
59 
60 TSS_RESULT
TCSP_CreateCounter_Internal(TCS_CONTEXT_HANDLE hContext,UINT32 LabelSize,BYTE * pLabel,TPM_ENCAUTH CounterAuth,TPM_AUTH * pOwnerAuth,TSS_COUNTER_ID * idCounter,TPM_COUNTER_VALUE * counterValue)61 TCSP_CreateCounter_Internal(TCS_CONTEXT_HANDLE hContext,
62 			    UINT32             LabelSize,
63 			    BYTE*              pLabel,
64 			    TPM_ENCAUTH        CounterAuth,
65 			    TPM_AUTH*          pOwnerAuth,
66 			    TSS_COUNTER_ID*    idCounter,
67 			    TPM_COUNTER_VALUE* counterValue)
68 {
69 	TSS_RESULT result;
70 	UINT32 paramSize;
71 	UINT64 offset = 0;
72 	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
73 
74 	if (LabelSize != 4) {
75 		LogDebugFn("BAD_PARAMETER: LabelSize != 4");
76 		return TCSERR(TSS_E_BAD_PARAMETER);
77 	}
78 
79 	if ((result = ctx_verify_context(hContext)))
80 		return result;
81 
82 	if ((result = auth_mgr_check(hContext, &pOwnerAuth->AuthHandle)))
83 		return result;
84 
85 	if ((result = tpm_rqu_build(TPM_ORD_CreateCounter, &offset, txBlob, CounterAuth.authdata,
86 				    LabelSize, pLabel, pOwnerAuth)))
87 		return result;
88 
89 	if ((result = req_mgr_submit_req(txBlob)))
90 		goto out;
91 
92 	if ((result = UnloadBlob_Header(txBlob, &paramSize))) {
93 		LogDebugFn("TPM_CreateCounter failed: rc=0x%x", result);
94 		goto out;
95 	}
96 
97 	if (!result) {
98 		result = tpm_rsp_parse(TPM_ORD_CreateCounter, txBlob, paramSize, idCounter,
99 				       counterValue, pOwnerAuth);
100 	}
101 
102 out:
103 	return result;
104 }
105 
106 TSS_RESULT
TCSP_IncrementCounter_Internal(TCS_CONTEXT_HANDLE hContext,TSS_COUNTER_ID idCounter,TPM_AUTH * pCounterAuth,TPM_COUNTER_VALUE * counterValue)107 TCSP_IncrementCounter_Internal(TCS_CONTEXT_HANDLE hContext,
108 			       TSS_COUNTER_ID     idCounter,
109 			       TPM_AUTH*          pCounterAuth,
110 			       TPM_COUNTER_VALUE* counterValue)
111 {
112 	TSS_RESULT result;
113 	UINT32 paramSize;
114 	UINT64 offset = 0;
115 	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
116 
117 	if ((result = ctx_verify_context(hContext)))
118 		return result;
119 
120 	if ((result = auth_mgr_check(hContext, &pCounterAuth->AuthHandle)))
121 		return result;
122 
123 	if ((result = tpm_rqu_build(TPM_ORD_IncrementCounter, &offset, txBlob, idCounter,
124 				    pCounterAuth)))
125 		return result;
126 
127 	if ((result = req_mgr_submit_req(txBlob)))
128 		goto out;
129 
130 	if ((result = UnloadBlob_Header(txBlob, &paramSize))) {
131 		LogDebugFn("UnloadBlob_Header failed: rc=0x%x", result);
132 		goto out;
133 	}
134 
135 	if (!result) {
136 		result = tpm_rsp_parse(TPM_ORD_IncrementCounter, txBlob, paramSize, NULL,
137 				       counterValue, pCounterAuth);
138 	}
139 out:
140 	return result;
141 }
142 
143 TSS_RESULT
TCSP_ReleaseCounter_Internal(TCS_CONTEXT_HANDLE hContext,TSS_COUNTER_ID idCounter,TPM_AUTH * pCounterAuth)144 TCSP_ReleaseCounter_Internal(TCS_CONTEXT_HANDLE hContext,
145 			     TSS_COUNTER_ID     idCounter,
146 			     TPM_AUTH*          pCounterAuth)
147 {
148 	TSS_RESULT result;
149 	UINT32 paramSize;
150 	UINT64 offset = 0;
151 	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
152 
153 	if ((result = ctx_verify_context(hContext)))
154 		return result;
155 
156 	if ((result = auth_mgr_check(hContext, &pCounterAuth->AuthHandle)))
157 		return result;
158 
159 	if ((result = tpm_rqu_build(TPM_ORD_ReleaseCounter, &offset, txBlob, idCounter,
160 				    pCounterAuth)))
161 		return result;
162 
163 	if ((result = req_mgr_submit_req(txBlob)))
164 		goto out;
165 
166 	if ((result = UnloadBlob_Header(txBlob, &paramSize))) {
167 		LogDebugFn("UnloadBlob_Header failed: rc=0x%x", result);
168 		goto out;
169 	}
170 
171 	if (!result) {
172 		result = tpm_rsp_parse(TPM_ORD_ReleaseCounter, txBlob, paramSize, pCounterAuth);
173 	}
174 out:
175 	return result;
176 }
177 
178 TSS_RESULT
TCSP_ReleaseCounterOwner_Internal(TCS_CONTEXT_HANDLE hContext,TSS_COUNTER_ID idCounter,TPM_AUTH * pOwnerAuth)179 TCSP_ReleaseCounterOwner_Internal(TCS_CONTEXT_HANDLE hContext,
180 				  TSS_COUNTER_ID     idCounter,
181 				  TPM_AUTH*          pOwnerAuth)
182 {
183 	TSS_RESULT result;
184 	UINT32 paramSize;
185 	UINT64 offset = 0;
186 	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
187 
188 	if ((result = ctx_verify_context(hContext)))
189 		return result;
190 
191 	if ((result = auth_mgr_check(hContext, &pOwnerAuth->AuthHandle)))
192 		return result;
193 
194 	if ((result = tpm_rqu_build(TPM_ORD_ReleaseCounterOwner, &offset, txBlob, idCounter,
195 				    pOwnerAuth)))
196 		return result;
197 
198 	if ((result = req_mgr_submit_req(txBlob)))
199 		goto out;
200 
201 	if ((result = UnloadBlob_Header(txBlob, &paramSize))) {
202 		LogDebugFn("UnloadBlob_Header failed: rc=0x%x", result);
203 		goto out;
204 	}
205 
206 	if (!result) {
207 		result = tpm_rsp_parse(TPM_ORD_ReleaseCounterOwner, txBlob, paramSize, pOwnerAuth);
208 	}
209 out:
210 	return result;
211 }
212 
213