1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*******************************************************************************
3 * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
4 * All rights reserved.
5 ******************************************************************************/
6
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include "tss2_mu.h"
12 #include "tss2_sys.h"
13 #include "tss2_esys.h"
14
15 #include "esys_types.h"
16 #include "esys_iutil.h"
17 #include "esys_mu.h"
18 #define LOGMODULE esys
19 #include "util/log.h"
20 #include "util/aux_util.h"
21
22 /** One-Call function for TPM2_EncryptDecrypt
23 *
24 * This function invokes the TPM2_EncryptDecrypt command in a one-call
25 * variant. This means the function will block until the TPM response is
26 * available. All input parameters are const. The memory for non-simple output
27 * parameters is allocated by the function implementation.
28 *
29 * @param[in,out] esysContext The ESYS_CONTEXT.
30 * @param[in] keyHandle The symmetric key used for the operation.
31 * @param[in] shandle1 Session handle for authorization of keyHandle
32 * @param[in] shandle2 Second session handle.
33 * @param[in] shandle3 Third session handle.
34 * @param[in] decrypt If YES, then the operation is decryption; if NO, the
35 * operation is encryption.
36 * @param[in] mode Symmetric mode.
37 * @param[in] ivIn An initial value as required by the algorithm.
38 * @param[in] inData The data to be encrypted/decrypted.
39 * @param[out] outData Encrypted or decrypted output.
40 * (callee-allocated)
41 * @param[out] ivOut Chaining value to use for IV in next round.
42 * (callee-allocated)
43 * @retval TSS2_RC_SUCCESS if the function call was a success.
44 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
45 * pointers or required output handle references are NULL.
46 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
47 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
48 * internal operations or return parameters.
49 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
50 * operation already pending.
51 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
52 * at least contain the tag, response length, and response code.
53 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
54 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
55 did not verify.
56 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
57 * the 'decrypt' attribute bit set.
58 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
59 * the 'encrypt' attribute bit set.
60 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
61 * to the ESYS_CONTEXT or are of the wrong type or if required
62 * ESYS_TR objects are ESYS_TR_NONE.
63 * @retval TSS2_ESYS_RC_NO_DECRYPT_PARAM: if one of the sessions has the
64 * 'decrypt' attribute set and the command does not support encryption
65 * of the first command parameter.
66 * @retval TSS2_RCs produced by lower layers of the software stack may be
67 * returned to the caller unaltered unless handled internally.
68 */
69 TSS2_RC
Esys_EncryptDecrypt(ESYS_CONTEXT * esysContext,ESYS_TR keyHandle,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,TPMI_YES_NO decrypt,TPMI_ALG_SYM_MODE mode,const TPM2B_IV * ivIn,const TPM2B_MAX_BUFFER * inData,TPM2B_MAX_BUFFER ** outData,TPM2B_IV ** ivOut)70 Esys_EncryptDecrypt(
71 ESYS_CONTEXT *esysContext,
72 ESYS_TR keyHandle,
73 ESYS_TR shandle1,
74 ESYS_TR shandle2,
75 ESYS_TR shandle3,
76 TPMI_YES_NO decrypt,
77 TPMI_ALG_SYM_MODE mode,
78 const TPM2B_IV *ivIn,
79 const TPM2B_MAX_BUFFER *inData,
80 TPM2B_MAX_BUFFER **outData,
81 TPM2B_IV **ivOut)
82 {
83 TSS2_RC r;
84
85 r = Esys_EncryptDecrypt_Async(esysContext, keyHandle, shandle1, shandle2,
86 shandle3, decrypt, mode, ivIn, inData);
87 return_if_error(r, "Error in async function");
88
89 /* Set the timeout to indefinite for now, since we want _Finish to block */
90 int32_t timeouttmp = esysContext->timeout;
91 esysContext->timeout = -1;
92 /*
93 * Now we call the finish function, until return code is not equal to
94 * from TSS2_BASE_RC_TRY_AGAIN.
95 * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
96 * have set the timeout to -1. This occurs for example if the TPM requests
97 * a retransmission of the command via TPM2_RC_YIELDED.
98 */
99 do {
100 r = Esys_EncryptDecrypt_Finish(esysContext, outData, ivOut);
101 /* This is just debug information about the reattempt to finish the
102 command */
103 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
104 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
105 " => resubmitting command", r);
106 } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
107
108 /* Restore the timeout value to the original value */
109 esysContext->timeout = timeouttmp;
110 return_if_error(r, "Esys Finish");
111
112 return TSS2_RC_SUCCESS;
113 }
114
115 /** Asynchronous function for TPM2_EncryptDecrypt
116 *
117 * This function invokes the TPM2_EncryptDecrypt command in a asynchronous
118 * variant. This means the function will return as soon as the command has been
119 * sent downwards the stack to the TPM. All input parameters are const.
120 * In order to retrieve the TPM's response call Esys_EncryptDecrypt_Finish.
121 *
122 * @param[in,out] esysContext The ESYS_CONTEXT.
123 * @param[in] keyHandle The symmetric key used for the operation.
124 * @param[in] shandle1 Session handle for authorization of keyHandle
125 * @param[in] shandle2 Second session handle.
126 * @param[in] shandle3 Third session handle.
127 * @param[in] decrypt If YES, then the operation is decryption; if NO, the
128 * operation is encryption.
129 * @param[in] mode Symmetric mode.
130 * @param[in] ivIn An initial value as required by the algorithm.
131 * @param[in] inData The data to be encrypted/decrypted.
132 * @retval ESYS_RC_SUCCESS if the function call was a success.
133 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
134 * pointers or required output handle references are NULL.
135 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
136 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
137 * internal operations or return parameters.
138 * @retval TSS2_RCs produced by lower layers of the software stack may be
139 returned to the caller unaltered unless handled internally.
140 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
141 * the 'decrypt' attribute bit set.
142 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
143 * the 'encrypt' attribute bit set.
144 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
145 * to the ESYS_CONTEXT or are of the wrong type or if required
146 * ESYS_TR objects are ESYS_TR_NONE.
147 * @retval TSS2_ESYS_RC_NO_DECRYPT_PARAM: if one of the sessions has the
148 * 'decrypt' attribute set and the command does not support encryption
149 * of the first command parameter.
150 */
151 TSS2_RC
Esys_EncryptDecrypt_Async(ESYS_CONTEXT * esysContext,ESYS_TR keyHandle,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,TPMI_YES_NO decrypt,TPMI_ALG_SYM_MODE mode,const TPM2B_IV * ivIn,const TPM2B_MAX_BUFFER * inData)152 Esys_EncryptDecrypt_Async(
153 ESYS_CONTEXT *esysContext,
154 ESYS_TR keyHandle,
155 ESYS_TR shandle1,
156 ESYS_TR shandle2,
157 ESYS_TR shandle3,
158 TPMI_YES_NO decrypt,
159 TPMI_ALG_SYM_MODE mode,
160 const TPM2B_IV *ivIn,
161 const TPM2B_MAX_BUFFER *inData)
162 {
163 TSS2_RC r;
164 LOG_TRACE("context=%p, keyHandle=%"PRIx32 ", decrypt=%02"PRIx8","
165 "mode=%04"PRIx16", ivIn=%p, inData=%p",
166 esysContext, keyHandle, decrypt, mode, ivIn,
167 inData);
168 TSS2L_SYS_AUTH_COMMAND auths;
169 RSRC_NODE_T *keyHandleNode;
170
171 /* Check context, sequence correctness and set state to error for now */
172 if (esysContext == NULL) {
173 LOG_ERROR("esyscontext is NULL.");
174 return TSS2_ESYS_RC_BAD_REFERENCE;
175 }
176 r = iesys_check_sequence_async(esysContext);
177 if (r != TSS2_RC_SUCCESS)
178 return r;
179 esysContext->state = _ESYS_STATE_INTERNALERROR;
180
181 /* Check input parameters */
182 r = check_session_feasibility(shandle1, shandle2, shandle3, 1);
183 return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage");
184
185 /* Retrieve the metadata objects for provided handles */
186 r = esys_GetResourceObject(esysContext, keyHandle, &keyHandleNode);
187 return_state_if_error(r, _ESYS_STATE_INIT, "keyHandle unknown.");
188
189 /* Initial invocation of SAPI to prepare the command buffer with parameters */
190 r = Tss2_Sys_EncryptDecrypt_Prepare(esysContext->sys,
191 (keyHandleNode == NULL) ? TPM2_RH_NULL
192 : keyHandleNode->rsrc.handle, decrypt,
193 mode, ivIn, inData);
194 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
195
196 /* Calculate the cpHash Values */
197 r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
198 return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
199 if (keyHandleNode != NULL)
200 iesys_compute_session_value(esysContext->session_tab[0],
201 &keyHandleNode->rsrc.name, &keyHandleNode->auth);
202 else
203 iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
204
205 iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
206 iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
207
208 /* Generate the auth values and set them in the SAPI command buffer */
209 r = iesys_gen_auths(esysContext, keyHandleNode, NULL, NULL, &auths);
210 return_state_if_error(r, _ESYS_STATE_INIT,
211 "Error in computation of auth values");
212
213 esysContext->authsCount = auths.count;
214 if (auths.count > 0) {
215 r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
216 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
217 }
218
219 /* Trigger execution and finish the async invocation */
220 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
221 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
222 "Finish (Execute Async)");
223
224 esysContext->state = _ESYS_STATE_SENT;
225
226 return r;
227 }
228
229 /** Asynchronous finish function for TPM2_EncryptDecrypt
230 *
231 * This function returns the results of a TPM2_EncryptDecrypt command
232 * invoked via Esys_EncryptDecrypt_Finish. All non-simple output parameters
233 * are allocated by the function's implementation. NULL can be passed for every
234 * output parameter if the value is not required.
235 *
236 * @param[in,out] esysContext The ESYS_CONTEXT.
237 * @param[out] outData Encrypted or decrypted output.
238 * (callee-allocated)
239 * @param[out] ivOut Chaining value to use for IV in next round.
240 * (callee-allocated)
241 * @retval TSS2_RC_SUCCESS on success
242 * @retval ESYS_RC_SUCCESS if the function call was a success.
243 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
244 * pointers or required output handle references are NULL.
245 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
246 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
247 * internal operations or return parameters.
248 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
249 * operation already pending.
250 * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
251 * TPM response is received.
252 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
253 * at least contain the tag, response length, and response code.
254 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
255 * not verify.
256 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
257 * @retval TSS2_RCs produced by lower layers of the software stack may be
258 * returned to the caller unaltered unless handled internally.
259 */
260 TSS2_RC
Esys_EncryptDecrypt_Finish(ESYS_CONTEXT * esysContext,TPM2B_MAX_BUFFER ** outData,TPM2B_IV ** ivOut)261 Esys_EncryptDecrypt_Finish(
262 ESYS_CONTEXT *esysContext,
263 TPM2B_MAX_BUFFER **outData,
264 TPM2B_IV **ivOut)
265 {
266 TSS2_RC r;
267 LOG_TRACE("context=%p, outData=%p, ivOut=%p",
268 esysContext, outData, ivOut);
269
270 if (esysContext == NULL) {
271 LOG_ERROR("esyscontext is NULL.");
272 return TSS2_ESYS_RC_BAD_REFERENCE;
273 }
274
275 /* Check for correct sequence and set sequence to irregular for now */
276 if (esysContext->state != _ESYS_STATE_SENT &&
277 esysContext->state != _ESYS_STATE_RESUBMISSION) {
278 LOG_ERROR("Esys called in bad sequence.");
279 return TSS2_ESYS_RC_BAD_SEQUENCE;
280 }
281 esysContext->state = _ESYS_STATE_INTERNALERROR;
282
283 /* Allocate memory for response parameters */
284 if (outData != NULL) {
285 *outData = calloc(sizeof(TPM2B_MAX_BUFFER), 1);
286 if (*outData == NULL) {
287 return_error(TSS2_ESYS_RC_MEMORY, "Out of memory");
288 }
289 }
290 if (ivOut != NULL) {
291 *ivOut = calloc(sizeof(TPM2B_IV), 1);
292 if (*ivOut == NULL) {
293 goto_error(r, TSS2_ESYS_RC_MEMORY, "Out of memory", error_cleanup);
294 }
295 }
296
297 /*Receive the TPM response and handle resubmissions if necessary. */
298 r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
299 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
300 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
301 esysContext->state = _ESYS_STATE_SENT;
302 goto error_cleanup;
303 }
304 /* This block handle the resubmission of TPM commands given a certain set of
305 * TPM response codes. */
306 if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
307 LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
308 "resubmission: %" PRIx32, r);
309 if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
310 LOG_WARNING("Maximum number of (re)submissions has been reached.");
311 esysContext->state = _ESYS_STATE_INIT;
312 goto error_cleanup;
313 }
314 esysContext->state = _ESYS_STATE_RESUBMISSION;
315 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
316 if (r != TSS2_RC_SUCCESS) {
317 LOG_WARNING("Error attempting to resubmit");
318 /* We do not set esysContext->state here but inherit the most recent
319 * state of the _async function. */
320 goto error_cleanup;
321 }
322 r = TSS2_ESYS_RC_TRY_AGAIN;
323 LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
324 goto error_cleanup;
325 }
326 /* The following is the "regular error" handling. */
327 if (iesys_tpm_error(r)) {
328 LOG_WARNING("Received TPM Error");
329 esysContext->state = _ESYS_STATE_INIT;
330 goto error_cleanup;
331 } else if (r != TSS2_RC_SUCCESS) {
332 LOG_ERROR("Received a non-TPM Error");
333 esysContext->state = _ESYS_STATE_INTERNALERROR;
334 goto error_cleanup;
335 }
336
337 /*
338 * Now the verification of the response (hmac check) and if necessary the
339 * parameter decryption have to be done.
340 */
341 r = iesys_check_response(esysContext);
342 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Error: check response",
343 error_cleanup);
344
345 /*
346 * After the verification of the response we call the complete function
347 * to deliver the result.
348 */
349 r = Tss2_Sys_EncryptDecrypt_Complete(esysContext->sys,
350 (outData != NULL) ? *outData : NULL,
351 (ivOut != NULL) ? *ivOut : NULL);
352 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR,
353 "Received error from SAPI unmarshaling" ,
354 error_cleanup);
355
356 esysContext->state = _ESYS_STATE_INIT;
357
358 return TSS2_RC_SUCCESS;
359
360 error_cleanup:
361 if (outData != NULL)
362 SAFE_FREE(*outData);
363 if (ivOut != NULL)
364 SAFE_FREE(*ivOut);
365
366 return r;
367 }
368