blob: 1c2039e6ad9b5a10f4d1b6a252078da4a4c8cb77 [file] [log] [blame]
Peter Huewed5a36f62018-06-12 00:59:26 +02001/* SPDX-License-Identifier: BSD-2 */
Juergen Reppff821bd2017-12-11 15:21:42 +01002/*******************************************************************************
3 * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
4 * All rights reserved.
Juergen Reppff821bd2017-12-11 15:21:42 +01005 ******************************************************************************/
6
Philip Tricca910f17c2018-03-15 12:38:37 -07007#include "tss2_mu.h"
8#include "tss2_sys.h"
Philip Tricca8ffd3c42018-03-09 16:27:24 -08009#include "tss2_esys.h"
Juergen Repped6e6e22018-03-19 17:34:32 +010010
11#include "esys_types.h"
Juergen Reppff821bd2017-12-11 15:21:42 +010012#include "esys_iutil.h"
13#include "esys_mu.h"
Juergen Reppff821bd2017-12-11 15:21:42 +010014#define LOGMODULE esys
Philip Triccaa7c51ce2018-03-10 18:28:25 -080015#include "util/log.h"
Juergen Reppff821bd2017-12-11 15:21:42 +010016
Juergen Reppadd438d2018-04-09 10:00:19 +020017/** Store command parameters inside the ESYS_CONTEXT for use during _Finish */
Juergen Reppff821bd2017-12-11 15:21:42 +010018static void store_input_parameters (
19 ESYS_CONTEXT *esysContext,
20 TPMI_ECC_CURVE curveID)
21{
22 esysContext->in.ECC_Parameters.curveID = curveID;
23}
24
25/** One-Call function for TPM2_ECC_Parameters
26 *
27 * This function invokes the TPM2_ECC_Parameters command in a one-call
28 * variant. This means the function will block until the TPM response is
29 * available. All input parameters are const. The memory for non-simple output
30 * parameters is allocated by the function implementation.
31 *
32 * @param[in,out] esysContext The ESYS_CONTEXT.
Juergen Reppf7e5bd32018-04-26 11:11:32 +020033 * @param[in] shandle1 First session handle.
34 * @param[in] shandle2 Second session handle.
35 * @param[in] shandle3 Third session handle.
36 * @param[in] curveID Parameter set selector.
37 * @param[out] parameters ECC parameters for the selected curve.
38 * (callee-allocated)
Juergen Reppc3cf4b62018-08-22 15:27:59 +020039 * @retval TSS2_RC_SUCCESS if the function call was a success.
Juergen Reppf7e5bd32018-04-26 11:11:32 +020040 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
41 * pointers or required output handle references are NULL.
42 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
43 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
44 * internal operations or return parameters.
45 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
46 * operation already pending.
47 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
48 * at least contain the tag, response length, and response code.
49 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
Juergen Reppc3cf4b62018-08-22 15:27:59 +020050 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
51 did not verify.
Juergen Reppf7e5bd32018-04-26 11:11:32 +020052 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
53 * the 'decrypt' attribute bit set.
54 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
55 * the 'encrypt' attribute bit set.
56 * @retval TSS2_ESYS_RC_NO_DECRYPT_PARAM: if one of the sessions has the
57 * 'decrypt' attribute set and the command does not support encryption
58 * of the first command parameter.
59 * @retval TSS2_ESYS_RC_NO_ENCRYPT_PARAM: if one of the sessions has the
60 * 'encrypt' attribute set and the command does not support encryption
61 * of the first response parameter.
62 * @retval TSS2_RCs produced by lower layers of the software stack may be
63 * returned to the caller unaltered unless handled internally.
Juergen Reppff821bd2017-12-11 15:21:42 +010064 */
65TSS2_RC
66Esys_ECC_Parameters(
67 ESYS_CONTEXT *esysContext,
68 ESYS_TR shandle1,
69 ESYS_TR shandle2,
70 ESYS_TR shandle3,
71 TPMI_ECC_CURVE curveID,
72 TPMS_ALGORITHM_DETAIL_ECC **parameters)
73{
Juergen Repp3b7b41b2018-03-19 15:58:04 +010074 TSS2_RC r;
Juergen Reppff821bd2017-12-11 15:21:42 +010075
Juergen Reppadd438d2018-04-09 10:00:19 +020076 r = Esys_ECC_Parameters_Async(esysContext,
Juergen Reppff821bd2017-12-11 15:21:42 +010077 shandle1,
78 shandle2,
79 shandle3,
80 curveID);
81 return_if_error(r, "Error in async function");
82
Juergen Reppadd438d2018-04-09 10:00:19 +020083 /* Set the timeout to indefinite for now, since we want _Finish to block */
Juergen Reppff821bd2017-12-11 15:21:42 +010084 int32_t timeouttmp = esysContext->timeout;
85 esysContext->timeout = -1;
86 /*
87 * Now we call the finish function, until return code is not equal to
88 * from TSS2_BASE_RC_TRY_AGAIN.
89 * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
90 * have set the timeout to -1. This occurs for example if the TPM requests
91 * a retransmission of the command via TPM2_RC_YIELDED.
92 */
93 do {
Juergen Reppadd438d2018-04-09 10:00:19 +020094 r = Esys_ECC_Parameters_Finish(esysContext,
Juergen Reppff821bd2017-12-11 15:21:42 +010095 parameters);
96 /* This is just debug information about the reattempt to finish the
97 command */
98 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
99 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
100 " => resubmitting command", r);
101 } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
102
103 /* Restore the timeout value to the original value */
104 esysContext->timeout = timeouttmp;
105 return_if_error(r, "Esys Finish");
106
107 return TSS2_RC_SUCCESS;
108}
109
110/** Asynchronous function for TPM2_ECC_Parameters
111 *
112 * This function invokes the TPM2_ECC_Parameters command in a asynchronous
113 * variant. This means the function will return as soon as the command has been
114 * sent downwards the stack to the TPM. All input parameters are const.
Juergen Reppadd438d2018-04-09 10:00:19 +0200115 * In order to retrieve the TPM's response call Esys_ECC_Parameters_Finish.
Juergen Reppff821bd2017-12-11 15:21:42 +0100116 *
117 * @param[in,out] esysContext The ESYS_CONTEXT.
Juergen Reppf7e5bd32018-04-26 11:11:32 +0200118 * @param[in] shandle1 First session handle.
119 * @param[in] shandle2 Second session handle.
120 * @param[in] shandle3 Third session handle.
121 * @param[in] curveID Parameter set selector.
122 * @retval ESYS_RC_SUCCESS if the function call was a success.
123 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
124 * pointers or required output handle references are NULL.
125 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
126 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
127 * internal operations or return parameters.
128 * @retval TSS2_RCs produced by lower layers of the software stack may be
129 returned to the caller unaltered unless handled internally.
130 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
131 * the 'decrypt' attribute bit set.
132 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
133 * the 'encrypt' attribute bit set.
134 * @retval TSS2_ESYS_RC_NO_DECRYPT_PARAM: if one of the sessions has the
135 * 'decrypt' attribute set and the command does not support encryption
136 * of the first command parameter.
137 * @retval TSS2_ESYS_RC_NO_ENCRYPT_PARAM: if one of the sessions has the
138 * 'encrypt' attribute set and the command does not support encryption
139 * of the first response parameter.
Juergen Reppff821bd2017-12-11 15:21:42 +0100140 */
141TSS2_RC
Juergen Reppadd438d2018-04-09 10:00:19 +0200142Esys_ECC_Parameters_Async(
Juergen Reppff821bd2017-12-11 15:21:42 +0100143 ESYS_CONTEXT *esysContext,
144 ESYS_TR shandle1,
145 ESYS_TR shandle2,
146 ESYS_TR shandle3,
147 TPMI_ECC_CURVE curveID)
148{
Juergen Repp3b7b41b2018-03-19 15:58:04 +0100149 TSS2_RC r;
Andreas Fuchs15bbb672018-03-27 13:21:13 +0200150 LOG_TRACE("context=%p, curveID=%04"PRIx16"",
151 esysContext, curveID);
Andreas Fuchsc5a73eb2018-03-19 16:01:00 +0100152 TSS2L_SYS_AUTH_COMMAND auths;
Juergen Reppff821bd2017-12-11 15:21:42 +0100153
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100154 /* Check context, sequence correctness and set state to error for now */
Juergen Reppff821bd2017-12-11 15:21:42 +0100155 if (esysContext == NULL) {
156 LOG_ERROR("esyscontext is NULL.");
157 return TSS2_ESYS_RC_BAD_REFERENCE;
158 }
159 r = iesys_check_sequence_async(esysContext);
160 if (r != TSS2_RC_SUCCESS)
161 return r;
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100162 esysContext->state = _ESYS_STATE_INTERNALERROR;
Juergen Reppff821bd2017-12-11 15:21:42 +0100163
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100164 /* Check and store input parameters */
dantpmf6ef2472018-04-06 15:21:59 -0700165 r = check_session_feasibility(shandle1, shandle2, shandle3, 0);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100166 return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage");
Juergen Reppff821bd2017-12-11 15:21:42 +0100167 store_input_parameters(esysContext,
168 curveID);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100169
170 /* Initial invocation of SAPI to prepare the command buffer with parameters */
Juergen Reppff821bd2017-12-11 15:21:42 +0100171 r = Tss2_Sys_ECC_Parameters_Prepare(esysContext->sys,
172 curveID);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100173 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
174
175 /* Calculate the cpHash Values */
Juergen Reppff821bd2017-12-11 15:21:42 +0100176 r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100177 return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
Juergen Reppff821bd2017-12-11 15:21:42 +0100178 iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100179 iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
180 iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
Juergen Reppff821bd2017-12-11 15:21:42 +0100181
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100182 /* Generate the auth values and set them in the SAPI command buffer */
183 r = iesys_gen_auths(esysContext, NULL, NULL, NULL, &auths);
184 return_state_if_error(r, _ESYS_STATE_INIT, "Error in computation of auth values");
Juergen Reppff821bd2017-12-11 15:21:42 +0100185 esysContext->authsCount = auths.count;
186 r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100187 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
Juergen Reppff821bd2017-12-11 15:21:42 +0100188
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100189 /* Trigger execution and finish the async invocation */
Juergen Reppff821bd2017-12-11 15:21:42 +0100190 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100191 return_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Finish (Execute Async)");
Juergen Reppff821bd2017-12-11 15:21:42 +0100192
193 esysContext->state = _ESYS_STATE_SENT;
194
195 return r;
196}
197
198/** Asynchronous finish function for TPM2_ECC_Parameters
199 *
200 * This function returns the results of a TPM2_ECC_Parameters command
Juergen Reppadd438d2018-04-09 10:00:19 +0200201 * invoked via Esys_ECC_Parameters_Finish. All non-simple output parameters
Juergen Reppff821bd2017-12-11 15:21:42 +0100202 * are allocated by the function's implementation. NULL can be passed for every
203 * output parameter if the value is not required.
204 *
205 * @param[in,out] esysContext The ESYS_CONTEXT.
Juergen Reppf7e5bd32018-04-26 11:11:32 +0200206 * @param[out] parameters ECC parameters for the selected curve.
207 * (callee-allocated)
Juergen Reppff821bd2017-12-11 15:21:42 +0100208 * @retval TSS2_RC_SUCCESS on success
Juergen Reppf7e5bd32018-04-26 11:11:32 +0200209 * @retval ESYS_RC_SUCCESS if the function call was a success.
210 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
211 * pointers or required output handle references are NULL.
212 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
213 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
214 * internal operations or return parameters.
215 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
216 * operation already pending.
217 * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
218 * TPM response is received.
219 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
Juergen Reppc3cf4b62018-08-22 15:27:59 +0200220 * at least contain the tag, response length, and response code.
221 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
222 * not verify.
Juergen Reppf7e5bd32018-04-26 11:11:32 +0200223 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
224 * @retval TSS2_RCs produced by lower layers of the software stack may be
225 * returned to the caller unaltered unless handled internally.
Juergen Reppff821bd2017-12-11 15:21:42 +0100226 */
227TSS2_RC
Juergen Reppadd438d2018-04-09 10:00:19 +0200228Esys_ECC_Parameters_Finish(
Juergen Reppff821bd2017-12-11 15:21:42 +0100229 ESYS_CONTEXT *esysContext,
230 TPMS_ALGORITHM_DETAIL_ECC **parameters)
231{
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100232 TSS2_RC r;
Andreas Fuchs15bbb672018-03-27 13:21:13 +0200233 LOG_TRACE("context=%p, parameters=%p",
234 esysContext, parameters);
235
Juergen Reppff821bd2017-12-11 15:21:42 +0100236 if (esysContext == NULL) {
237 LOG_ERROR("esyscontext is NULL.");
238 return TSS2_ESYS_RC_BAD_REFERENCE;
239 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100240
241 /* Check for correct sequence and set sequence to irregular for now */
Juergen Reppff821bd2017-12-11 15:21:42 +0100242 if (esysContext->state != _ESYS_STATE_SENT) {
243 LOG_ERROR("Esys called in bad sequence.");
244 return TSS2_ESYS_RC_BAD_SEQUENCE;
245 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100246 esysContext->state = _ESYS_STATE_INTERNALERROR;
247
248 /* Allocate memory for response parameters */
Juergen Reppff821bd2017-12-11 15:21:42 +0100249 if (parameters != NULL) {
250 *parameters = calloc(sizeof(TPMS_ALGORITHM_DETAIL_ECC), 1);
251 if (*parameters == NULL) {
252 return_error(TSS2_ESYS_RC_MEMORY, "Out of memory");
253 }
254 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100255
256 /*Receive the TPM response and handle resubmissions if necessary. */
Juergen Reppff821bd2017-12-11 15:21:42 +0100257 r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
258 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
259 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100260 esysContext->state = _ESYS_STATE_SENT;
Juergen Reppff821bd2017-12-11 15:21:42 +0100261 goto error_cleanup;
262 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100263 /* This block handle the resubmission of TPM commands given a certain set of
264 * TPM response codes. */
Juergen Reppff821bd2017-12-11 15:21:42 +0100265 if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
266 LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
267 "resubmission: %" PRIx32, r);
Andreas Fuchs631d7e62018-03-19 10:54:56 +0100268 if (esysContext->submissionCount >= _ESYS_MAX_SUBMISSIONS) {
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100269 LOG_WARNING("Maximum number of (re)submissions has been reached.");
270 esysContext->state = _ESYS_STATE_INIT;
Juergen Reppff821bd2017-12-11 15:21:42 +0100271 goto error_cleanup;
272 }
273 esysContext->state = _ESYS_STATE_RESUBMISSION;
Juergen Reppadd438d2018-04-09 10:00:19 +0200274 r = Esys_ECC_Parameters_Async(esysContext,
Juergen Reppff821bd2017-12-11 15:21:42 +0100275 esysContext->session_type[0],
276 esysContext->session_type[1],
277 esysContext->session_type[2],
278 esysContext->in.ECC_Parameters.curveID);
279 if (r != TSS2_RC_SUCCESS) {
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100280 LOG_WARNING("Error attempting to resubmit");
281 /* We do not set esysContext->state here but inherit the most recent
282 * state of the _async function. */
Juergen Reppff821bd2017-12-11 15:21:42 +0100283 goto error_cleanup;
284 }
285 r = TSS2_ESYS_RC_TRY_AGAIN;
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100286 LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
Juergen Reppff821bd2017-12-11 15:21:42 +0100287 goto error_cleanup;
288 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100289 /* The following is the "regular error" handling. */
Juergen Repp033da112018-07-10 13:06:18 +0200290 if (iesys_tpm_error(r)) {
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100291 LOG_WARNING("Received TPM Error");
292 esysContext->state = _ESYS_STATE_INIT;
293 goto error_cleanup;
294 } else if (r != TSS2_RC_SUCCESS) {
295 LOG_ERROR("Received a non-TPM Error");
296 esysContext->state = _ESYS_STATE_INTERNALERROR;
Juergen Reppff821bd2017-12-11 15:21:42 +0100297 goto error_cleanup;
298 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100299
Juergen Reppff821bd2017-12-11 15:21:42 +0100300 /*
301 * Now the verification of the response (hmac check) and if necessary the
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100302 * parameter decryption have to be done.
Juergen Reppff821bd2017-12-11 15:21:42 +0100303 */
Juergen Repp109de7d2018-02-23 13:42:08 +0100304 r = iesys_check_response(esysContext);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100305 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Error: check response",
Juergen Reppff821bd2017-12-11 15:21:42 +0100306 error_cleanup);
Juergen Reppff821bd2017-12-11 15:21:42 +0100307 /*
308 * After the verification of the response we call the complete function
309 * to deliver the result.
310 */
311 r = Tss2_Sys_ECC_Parameters_Complete(esysContext->sys,
312 (parameters != NULL) ? *parameters : NULL);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100313 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Received error from SAPI"
dantpmf6ef2472018-04-06 15:21:59 -0700314 " unmarshaling" ,error_cleanup);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100315 esysContext->state = _ESYS_STATE_INIT;
Juergen Reppff821bd2017-12-11 15:21:42 +0100316
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100317 return TSS2_RC_SUCCESS;
Juergen Reppff821bd2017-12-11 15:21:42 +0100318
319error_cleanup:
320 if (parameters != NULL)
321 SAFE_FREE(*parameters);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100322
Juergen Reppff821bd2017-12-11 15:21:42 +0100323 return r;
Philip Tricca910f17c2018-03-15 12:38:37 -0700324}