1*758e9fbaSOystein Eftevaag /* SPDX-License-Identifier: BSD-2-Clause */
2*758e9fbaSOystein Eftevaag /*******************************************************************************
3*758e9fbaSOystein Eftevaag * Copyright 2018-2019, Fraunhofer SIT sponsored by Infineon Technologies AG
4*758e9fbaSOystein Eftevaag * All rights reserved.
5*758e9fbaSOystein Eftevaag ******************************************************************************/
6*758e9fbaSOystein Eftevaag
7*758e9fbaSOystein Eftevaag #ifdef HAVE_CONFIG_H
8*758e9fbaSOystein Eftevaag #include <config.h>
9*758e9fbaSOystein Eftevaag #endif
10*758e9fbaSOystein Eftevaag
11*758e9fbaSOystein Eftevaag #include <stdlib.h>
12*758e9fbaSOystein Eftevaag #include <errno.h>
13*758e9fbaSOystein Eftevaag #include <unistd.h>
14*758e9fbaSOystein Eftevaag #include <errno.h>
15*758e9fbaSOystein Eftevaag #include <string.h>
16*758e9fbaSOystein Eftevaag
17*758e9fbaSOystein Eftevaag #include "tss2_fapi.h"
18*758e9fbaSOystein Eftevaag #include "fapi_int.h"
19*758e9fbaSOystein Eftevaag #include "fapi_util.h"
20*758e9fbaSOystein Eftevaag #include "tss2_esys.h"
21*758e9fbaSOystein Eftevaag #include "ifapi_json_serialize.h"
22*758e9fbaSOystein Eftevaag #include "fapi_policy.h"
23*758e9fbaSOystein Eftevaag #define LOGMODULE fapi
24*758e9fbaSOystein Eftevaag #include "util/log.h"
25*758e9fbaSOystein Eftevaag #include "util/aux_util.h"
26*758e9fbaSOystein Eftevaag #include "fapi_crypto.h"
27*758e9fbaSOystein Eftevaag
28*758e9fbaSOystein Eftevaag #define IV_SIZE 16
29*758e9fbaSOystein Eftevaag
30*758e9fbaSOystein Eftevaag /** One-Call function for Fapi_Encrypt
31*758e9fbaSOystein Eftevaag *
32*758e9fbaSOystein Eftevaag * Encrypt the provided data for the target key using the TPM encryption
33*758e9fbaSOystein Eftevaag * schemes as specified in the crypto profile.
34*758e9fbaSOystein Eftevaag * This function does not use the TPM; i.e. works in non-TPM mode.
35*758e9fbaSOystein Eftevaag *
36*758e9fbaSOystein Eftevaag * @param[in,out] context The FAPI_CONTEXT
37*758e9fbaSOystein Eftevaag * @param[in] keyPath THe path to the encryption key
38*758e9fbaSOystein Eftevaag * @param[in] plainText The plaintext data to encrypt
39*758e9fbaSOystein Eftevaag * @param[in] plainTextSize The size of the plainText in bytes
40*758e9fbaSOystein Eftevaag * @param[out] cipherText The encoded cipher text.
41*758e9fbaSOystein Eftevaag * @param[out] cipherTextSize The size of the encoded cipher text.
42*758e9fbaSOystein Eftevaag *
43*758e9fbaSOystein Eftevaag * @retval TSS2_RC_SUCCESS: if the function call was a success.
44*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context, keyPath, plainText, or
45*758e9fbaSOystein Eftevaag * cipherText is NULL.
46*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
47*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_KEY_NOT_FOUND: if keyPath does not map to a FAPI key.
48*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_KEY: if the key at keyPath is unsuitable for
49*758e9fbaSOystein Eftevaag * encryption.
50*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_VALUE: if plainTextSize is 0.
51*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
52*758e9fbaSOystein Eftevaag * operation already pending.
53*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
54*758e9fbaSOystein Eftevaag * internal operations or return parameters.
55*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_NO_TPM if FAPI was initialized in no-TPM-mode via its
56*758e9fbaSOystein Eftevaag * config file.
57*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_IO_ERROR if an error occurred while accessing the
58*758e9fbaSOystein Eftevaag * object store.
59*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_NOT_IMPLEMENTED if the encryption algorithm is not available.
60*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_TRY_AGAIN if an I/O operation is not finished yet and
61*758e9fbaSOystein Eftevaag * this function needs to be called again.
62*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_PATH_NOT_FOUND if a FAPI object path was not found
63*758e9fbaSOystein Eftevaag * during authorization.
64*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_GENERAL_FAILURE if an internal error occurred.
65*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_AUTHORIZATION_UNKNOWN if a required authorization callback
66*758e9fbaSOystein Eftevaag * is not set.
67*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_AUTHORIZATION_FAILED if the authorization attempt fails.
68*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_POLICY_UNKNOWN if policy search for a certain policy digest
69*758e9fbaSOystein Eftevaag * was not successful.
70*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_* possible error codes of ESAPI.
71*758e9fbaSOystein Eftevaag */
72*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_Encrypt(FAPI_CONTEXT * context,char const * keyPath,uint8_t const * plainText,size_t plainTextSize,uint8_t ** cipherText,size_t * cipherTextSize)73*758e9fbaSOystein Eftevaag Fapi_Encrypt(
74*758e9fbaSOystein Eftevaag FAPI_CONTEXT *context,
75*758e9fbaSOystein Eftevaag char const *keyPath,
76*758e9fbaSOystein Eftevaag uint8_t const *plainText,
77*758e9fbaSOystein Eftevaag size_t plainTextSize,
78*758e9fbaSOystein Eftevaag uint8_t **cipherText,
79*758e9fbaSOystein Eftevaag size_t *cipherTextSize)
80*758e9fbaSOystein Eftevaag {
81*758e9fbaSOystein Eftevaag LOG_TRACE("called for context:%p", context);
82*758e9fbaSOystein Eftevaag
83*758e9fbaSOystein Eftevaag TSS2_RC r, r2;
84*758e9fbaSOystein Eftevaag
85*758e9fbaSOystein Eftevaag /* Check for NULL parameters */
86*758e9fbaSOystein Eftevaag check_not_null(context);
87*758e9fbaSOystein Eftevaag check_not_null(keyPath);
88*758e9fbaSOystein Eftevaag check_not_null(plainText);
89*758e9fbaSOystein Eftevaag check_not_null(cipherText);
90*758e9fbaSOystein Eftevaag
91*758e9fbaSOystein Eftevaag /* Check whether TCTI and ESYS are initialized */
92*758e9fbaSOystein Eftevaag return_if_null(context->esys, "Command can't be executed in none TPM mode.",
93*758e9fbaSOystein Eftevaag TSS2_FAPI_RC_NO_TPM);
94*758e9fbaSOystein Eftevaag
95*758e9fbaSOystein Eftevaag /* If the async state automata of FAPI shall be tested, then we must not set
96*758e9fbaSOystein Eftevaag the timeouts of ESYS to blocking mode.
97*758e9fbaSOystein Eftevaag During testing, the mssim tcti will ensure multiple re-invocations.
98*758e9fbaSOystein Eftevaag Usually however the synchronous invocations of FAPI shall instruct ESYS
99*758e9fbaSOystein Eftevaag to block until a result is available. */
100*758e9fbaSOystein Eftevaag #ifndef TEST_FAPI_ASYNC
101*758e9fbaSOystein Eftevaag r = Esys_SetTimeout(context->esys, TSS2_TCTI_TIMEOUT_BLOCK);
102*758e9fbaSOystein Eftevaag return_if_error_reset_state(r, "Set Timeout to blocking");
103*758e9fbaSOystein Eftevaag #endif /* TEST_FAPI_ASYNC */
104*758e9fbaSOystein Eftevaag
105*758e9fbaSOystein Eftevaag r = Fapi_Encrypt_Async(context, keyPath, plainText, plainTextSize);
106*758e9fbaSOystein Eftevaag return_if_error_reset_state(r, "Data_Encrypt");
107*758e9fbaSOystein Eftevaag
108*758e9fbaSOystein Eftevaag do {
109*758e9fbaSOystein Eftevaag /* We wait for file I/O to be ready if the FAPI state automata
110*758e9fbaSOystein Eftevaag are in a file I/O state. */
111*758e9fbaSOystein Eftevaag r = ifapi_io_poll(&context->io);
112*758e9fbaSOystein Eftevaag return_if_error(r, "Something went wrong with IO polling");
113*758e9fbaSOystein Eftevaag
114*758e9fbaSOystein Eftevaag /* Repeatedly call the finish function, until FAPI has transitioned
115*758e9fbaSOystein Eftevaag through all execution stages / states of this invocation. */
116*758e9fbaSOystein Eftevaag r = Fapi_Encrypt_Finish(context, cipherText, cipherTextSize);
117*758e9fbaSOystein Eftevaag } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
118*758e9fbaSOystein Eftevaag
119*758e9fbaSOystein Eftevaag /* Reset the ESYS timeout to non-blocking, immediate response. */
120*758e9fbaSOystein Eftevaag r2 = Esys_SetTimeout(context->esys, 0);
121*758e9fbaSOystein Eftevaag return_if_error(r2, "Set Timeout to non-blocking");
122*758e9fbaSOystein Eftevaag
123*758e9fbaSOystein Eftevaag return_if_error_reset_state(r, "Data_Encrypt");
124*758e9fbaSOystein Eftevaag
125*758e9fbaSOystein Eftevaag LOG_TRACE("finished");
126*758e9fbaSOystein Eftevaag return TSS2_RC_SUCCESS;
127*758e9fbaSOystein Eftevaag }
128*758e9fbaSOystein Eftevaag
129*758e9fbaSOystein Eftevaag /** Asynchronous function for Fapi_Encrypt
130*758e9fbaSOystein Eftevaag *
131*758e9fbaSOystein Eftevaag * Encrypt the provided data for the target key using the TPM encryption
132*758e9fbaSOystein Eftevaag * schemes as specified in the crypto profile.
133*758e9fbaSOystein Eftevaag * This function does not use the TPM; i.e. works in non-TPM mode.
134*758e9fbaSOystein Eftevaag *
135*758e9fbaSOystein Eftevaag * Call Fapi_Encrypt_Finish to finish the execution of this command.
136*758e9fbaSOystein Eftevaag *
137*758e9fbaSOystein Eftevaag * @param[in,out] context The FAPI_CONTEXT
138*758e9fbaSOystein Eftevaag * @param[in] keyPath The path to the encryption key
139*758e9fbaSOystein Eftevaag * @param[in] plainText The plainText data to encrypt
140*758e9fbaSOystein Eftevaag * @param[in] plainTextSize The size of the plainText in bytes
141*758e9fbaSOystein Eftevaag *
142*758e9fbaSOystein Eftevaag * @retval TSS2_RC_SUCCESS: if the function call was a success.
143*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context, keyPath or plainText is
144*758e9fbaSOystein Eftevaag * NULL.
145*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
146*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_KEY_NOT_FOUND: if keyPath does not map to a FAPI key.
147*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_KEY: if the key at keyPath is unsuitable for
148*758e9fbaSOystein Eftevaag * encryption.
149*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_VALUE: if plainTextSize is 0.
150*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
151*758e9fbaSOystein Eftevaag * operation already pending.
152*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
153*758e9fbaSOystein Eftevaag * internal operations or return parameters.
154*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_NO_TPM if FAPI was initialized in no-TPM-mode via its
155*758e9fbaSOystein Eftevaag * config file.
156*758e9fbaSOystein Eftevaag */
157*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_Encrypt_Async(FAPI_CONTEXT * context,char const * keyPath,uint8_t const * plainText,size_t plainTextSize)158*758e9fbaSOystein Eftevaag Fapi_Encrypt_Async(
159*758e9fbaSOystein Eftevaag FAPI_CONTEXT *context,
160*758e9fbaSOystein Eftevaag char const *keyPath,
161*758e9fbaSOystein Eftevaag uint8_t const *plainText,
162*758e9fbaSOystein Eftevaag size_t plainTextSize)
163*758e9fbaSOystein Eftevaag {
164*758e9fbaSOystein Eftevaag LOG_TRACE("called for context:%p", context);
165*758e9fbaSOystein Eftevaag LOG_TRACE("keyPath: %s", keyPath);
166*758e9fbaSOystein Eftevaag if (plainText) {
167*758e9fbaSOystein Eftevaag LOGBLOB_TRACE(plainText, plainTextSize, "plainText");
168*758e9fbaSOystein Eftevaag } else {
169*758e9fbaSOystein Eftevaag LOG_TRACE("plainText: (null) plainTextSize: %zi", plainTextSize);
170*758e9fbaSOystein Eftevaag }
171*758e9fbaSOystein Eftevaag
172*758e9fbaSOystein Eftevaag TSS2_RC r;
173*758e9fbaSOystein Eftevaag
174*758e9fbaSOystein Eftevaag /* Check for NULL parameters */
175*758e9fbaSOystein Eftevaag check_not_null(context);
176*758e9fbaSOystein Eftevaag check_not_null(keyPath);
177*758e9fbaSOystein Eftevaag check_not_null(plainText);
178*758e9fbaSOystein Eftevaag
179*758e9fbaSOystein Eftevaag /* Helpful alias pointers */
180*758e9fbaSOystein Eftevaag IFAPI_Data_EncryptDecrypt * command = &(context->cmd.Data_EncryptDecrypt);
181*758e9fbaSOystein Eftevaag
182*758e9fbaSOystein Eftevaag r = ifapi_session_init(context);
183*758e9fbaSOystein Eftevaag return_if_error(r, "Initialize Encrypt");
184*758e9fbaSOystein Eftevaag
185*758e9fbaSOystein Eftevaag /* Copy parameters to context for use during _Finish. */
186*758e9fbaSOystein Eftevaag uint8_t *inData = malloc(plainTextSize);
187*758e9fbaSOystein Eftevaag goto_if_null(inData, "Out of memory", r, error_cleanup);
188*758e9fbaSOystein Eftevaag memcpy(inData, plainText, plainTextSize);
189*758e9fbaSOystein Eftevaag command->in_data = inData;
190*758e9fbaSOystein Eftevaag
191*758e9fbaSOystein Eftevaag strdup_check(command->keyPath, keyPath, r, error_cleanup);
192*758e9fbaSOystein Eftevaag
193*758e9fbaSOystein Eftevaag command->in_dataSize = plainTextSize;
194*758e9fbaSOystein Eftevaag command->key_handle = ESYS_TR_NONE;
195*758e9fbaSOystein Eftevaag
196*758e9fbaSOystein Eftevaag /* Initialize the context state for this operation. */
197*758e9fbaSOystein Eftevaag context->state = DATA_ENCRYPT_WAIT_FOR_PROFILE;
198*758e9fbaSOystein Eftevaag LOG_TRACE("finished");
199*758e9fbaSOystein Eftevaag return TSS2_RC_SUCCESS;
200*758e9fbaSOystein Eftevaag
201*758e9fbaSOystein Eftevaag error_cleanup:
202*758e9fbaSOystein Eftevaag SAFE_FREE(inData);
203*758e9fbaSOystein Eftevaag SAFE_FREE(command->keyPath);
204*758e9fbaSOystein Eftevaag return r;
205*758e9fbaSOystein Eftevaag }
206*758e9fbaSOystein Eftevaag
207*758e9fbaSOystein Eftevaag /** Asynchronous finish function for Fapi_Encrypt
208*758e9fbaSOystein Eftevaag *
209*758e9fbaSOystein Eftevaag * This function should be called after a previous Fapi_Encrypt_Async.
210*758e9fbaSOystein Eftevaag *
211*758e9fbaSOystein Eftevaag * @param[in,out] context The FAPI_CONTEXT
212*758e9fbaSOystein Eftevaag * @param[out] cipherText The encoded ciphertext
213*758e9fbaSOystein Eftevaag * @param[out] cipherTextSize The size of the encoded cipher text.
214*758e9fbaSOystein Eftevaag *
215*758e9fbaSOystein Eftevaag * @retval TSS2_RC_SUCCESS: if the function call was a success.
216*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context or ciphertext is NULL.
217*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
218*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
219*758e9fbaSOystein Eftevaag * operation already pending.
220*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
221*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
222*758e9fbaSOystein Eftevaag * internal operations or return parameters.
223*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_TRY_AGAIN: if the asynchronous operation is not yet
224*758e9fbaSOystein Eftevaag * complete. Call this function again later.
225*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_NOT_IMPLEMENTED if the encryption algorithm is not available.
226*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
227*758e9fbaSOystein Eftevaag * the function.
228*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_PATH_NOT_FOUND if a FAPI object path was not found
229*758e9fbaSOystein Eftevaag * during authorization.
230*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_KEY_NOT_FOUND if a key was not found.
231*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_GENERAL_FAILURE if an internal error occurred.
232*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_AUTHORIZATION_UNKNOWN if a required authorization callback
233*758e9fbaSOystein Eftevaag * is not set.
234*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_AUTHORIZATION_FAILED if the authorization attempt fails.
235*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_POLICY_UNKNOWN if policy search for a certain policy digest
236*758e9fbaSOystein Eftevaag * was not successful.
237*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_* possible error codes of ESAPI.
238*758e9fbaSOystein Eftevaag */
239*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_Encrypt_Finish(FAPI_CONTEXT * context,uint8_t ** cipherText,size_t * cipherTextSize)240*758e9fbaSOystein Eftevaag Fapi_Encrypt_Finish(
241*758e9fbaSOystein Eftevaag FAPI_CONTEXT *context,
242*758e9fbaSOystein Eftevaag uint8_t **cipherText,
243*758e9fbaSOystein Eftevaag size_t *cipherTextSize)
244*758e9fbaSOystein Eftevaag {
245*758e9fbaSOystein Eftevaag LOG_TRACE("called for context:%p", context);
246*758e9fbaSOystein Eftevaag
247*758e9fbaSOystein Eftevaag TSS2_RC r;
248*758e9fbaSOystein Eftevaag
249*758e9fbaSOystein Eftevaag /* Check for NULL parameters */
250*758e9fbaSOystein Eftevaag check_not_null(context);
251*758e9fbaSOystein Eftevaag check_not_null(cipherText);
252*758e9fbaSOystein Eftevaag
253*758e9fbaSOystein Eftevaag /* Helpful alias pointers */
254*758e9fbaSOystein Eftevaag IFAPI_Data_EncryptDecrypt * command = &context->cmd.Data_EncryptDecrypt;
255*758e9fbaSOystein Eftevaag IFAPI_OBJECT *encKeyObject;
256*758e9fbaSOystein Eftevaag TPM2B_PUBLIC_KEY_RSA *tpmCipherText = NULL;
257*758e9fbaSOystein Eftevaag
258*758e9fbaSOystein Eftevaag switch (context->state) {
259*758e9fbaSOystein Eftevaag statecase(context->state, DATA_ENCRYPT_WAIT_FOR_PROFILE);
260*758e9fbaSOystein Eftevaag /* Retrieve the profile for the provided key in order to get the
261*758e9fbaSOystein Eftevaag encryption scheme below. */
262*758e9fbaSOystein Eftevaag r = ifapi_profiles_get(&context->profiles, command->keyPath,
263*758e9fbaSOystein Eftevaag &command->profile);
264*758e9fbaSOystein Eftevaag return_try_again(r);
265*758e9fbaSOystein Eftevaag goto_if_error_reset_state(r, " FAPI create session", error_cleanup);
266*758e9fbaSOystein Eftevaag
267*758e9fbaSOystein Eftevaag /* Initialize a session used for authorization and parameter encryption. */
268*758e9fbaSOystein Eftevaag r = ifapi_get_sessions_async(context,
269*758e9fbaSOystein Eftevaag IFAPI_SESSION_GENEK | IFAPI_SESSION1,
270*758e9fbaSOystein Eftevaag TPMA_SESSION_ENCRYPT | TPMA_SESSION_DECRYPT, 0);
271*758e9fbaSOystein Eftevaag goto_if_error_reset_state(r, "Create sessions", error_cleanup);
272*758e9fbaSOystein Eftevaag
273*758e9fbaSOystein Eftevaag fallthrough;
274*758e9fbaSOystein Eftevaag
275*758e9fbaSOystein Eftevaag statecase(context->state, DATA_ENCRYPT_WAIT_FOR_SESSION);
276*758e9fbaSOystein Eftevaag r = ifapi_get_sessions_finish(context, &context->profiles.default_profile,
277*758e9fbaSOystein Eftevaag context->profiles.default_profile.nameAlg);
278*758e9fbaSOystein Eftevaag return_try_again(r);
279*758e9fbaSOystein Eftevaag goto_if_error(r, "Get session.", error_cleanup);
280*758e9fbaSOystein Eftevaag
281*758e9fbaSOystein Eftevaag /* Load the reference key by loading all of its parents starting from the SRK. */
282*758e9fbaSOystein Eftevaag r = ifapi_load_keys_async(context, command->keyPath);
283*758e9fbaSOystein Eftevaag goto_if_error(r, "Load keys.", error_cleanup);
284*758e9fbaSOystein Eftevaag
285*758e9fbaSOystein Eftevaag fallthrough;
286*758e9fbaSOystein Eftevaag
287*758e9fbaSOystein Eftevaag statecase(context->state, DATA_ENCRYPT_WAIT_FOR_KEY);
288*758e9fbaSOystein Eftevaag r = ifapi_load_keys_finish(context, IFAPI_FLUSH_PARENT,
289*758e9fbaSOystein Eftevaag &command->key_handle,
290*758e9fbaSOystein Eftevaag &command->key_object);
291*758e9fbaSOystein Eftevaag return_try_again(r);
292*758e9fbaSOystein Eftevaag goto_if_error_reset_state(r, " Load key.", error_cleanup);
293*758e9fbaSOystein Eftevaag
294*758e9fbaSOystein Eftevaag encKeyObject = command->key_object;
295*758e9fbaSOystein Eftevaag
296*758e9fbaSOystein Eftevaag if (encKeyObject->misc.key.public.publicArea.type == TPM2_ALG_RSA) {
297*758e9fbaSOystein Eftevaag TPM2B_DATA null_data = { .size = 0, .buffer = {} };
298*758e9fbaSOystein Eftevaag TPM2B_PUBLIC_KEY_RSA *rsa_message = (TPM2B_PUBLIC_KEY_RSA *)&context->aux_data;
299*758e9fbaSOystein Eftevaag rsa_message->size = context->cmd.Data_EncryptDecrypt.in_dataSize;
300*758e9fbaSOystein Eftevaag size_t key_size =
301*758e9fbaSOystein Eftevaag encKeyObject->misc.key.public.publicArea.parameters.rsaDetail.keyBits / 8;
302*758e9fbaSOystein Eftevaag if (rsa_message->size <= key_size) {
303*758e9fbaSOystein Eftevaag memcpy(&rsa_message->buffer[0], context->cmd.Data_EncryptDecrypt.in_data,
304*758e9fbaSOystein Eftevaag context->cmd.Data_EncryptDecrypt.in_dataSize);
305*758e9fbaSOystein Eftevaag
306*758e9fbaSOystein Eftevaag /* Received plain text will be encrypted */
307*758e9fbaSOystein Eftevaag r = Esys_TRSess_SetAttributes(context->esys, context->session1,
308*758e9fbaSOystein Eftevaag TPMA_SESSION_CONTINUESESSION | TPMA_SESSION_DECRYPT,
309*758e9fbaSOystein Eftevaag 0xff);
310*758e9fbaSOystein Eftevaag goto_if_error_reset_state(r, "Set session attributes.", error_cleanup);
311*758e9fbaSOystein Eftevaag
312*758e9fbaSOystein Eftevaag r = Esys_RSA_Encrypt_Async(context->esys,
313*758e9fbaSOystein Eftevaag context->cmd.Data_EncryptDecrypt.key_handle,
314*758e9fbaSOystein Eftevaag context->session1, ESYS_TR_NONE, ESYS_TR_NONE,
315*758e9fbaSOystein Eftevaag rsa_message,
316*758e9fbaSOystein Eftevaag &command->profile->rsa_decrypt_scheme,
317*758e9fbaSOystein Eftevaag &null_data);
318*758e9fbaSOystein Eftevaag goto_if_error(r, "Error esys rsa encrypt", error_cleanup);
319*758e9fbaSOystein Eftevaag
320*758e9fbaSOystein Eftevaag context-> state = DATA_ENCRYPT_WAIT_FOR_RSA_ENCRYPTION;
321*758e9fbaSOystein Eftevaag } else {
322*758e9fbaSOystein Eftevaag goto_error_reset_state(r, TSS2_FAPI_RC_NOT_IMPLEMENTED,
323*758e9fbaSOystein Eftevaag "Size to big for RSA encryption.", error_cleanup);
324*758e9fbaSOystein Eftevaag }
325*758e9fbaSOystein Eftevaag } else {
326*758e9fbaSOystein Eftevaag goto_error(r, TSS2_FAPI_RC_NOT_IMPLEMENTED,
327*758e9fbaSOystein Eftevaag "Unsupported algorithm (%" PRIu16 ")",
328*758e9fbaSOystein Eftevaag error_cleanup, encKeyObject->misc.key.public.publicArea.type);
329*758e9fbaSOystein Eftevaag }
330*758e9fbaSOystein Eftevaag fallthrough;
331*758e9fbaSOystein Eftevaag
332*758e9fbaSOystein Eftevaag statecase(context->state, DATA_ENCRYPT_WAIT_FOR_RSA_ENCRYPTION);
333*758e9fbaSOystein Eftevaag r = Esys_RSA_Encrypt_Finish(context->esys, &tpmCipherText);
334*758e9fbaSOystein Eftevaag return_try_again(r);
335*758e9fbaSOystein Eftevaag goto_if_error_reset_state(r, "RSA encryption.", error_cleanup);
336*758e9fbaSOystein Eftevaag
337*758e9fbaSOystein Eftevaag /* Return cipherTextSize if requested by the caller. */
338*758e9fbaSOystein Eftevaag if (cipherTextSize)
339*758e9fbaSOystein Eftevaag *cipherTextSize = tpmCipherText->size;
340*758e9fbaSOystein Eftevaag
341*758e9fbaSOystein Eftevaag /* Duplicate the outputs for handling off to the caller. */
342*758e9fbaSOystein Eftevaag *cipherText = malloc(tpmCipherText->size);
343*758e9fbaSOystein Eftevaag goto_if_null2(*cipherText, "Out of memory", r, TSS2_FAPI_RC_MEMORY,
344*758e9fbaSOystein Eftevaag error_cleanup);
345*758e9fbaSOystein Eftevaag
346*758e9fbaSOystein Eftevaag memcpy(*cipherText, &tpmCipherText->buffer[0], tpmCipherText->size);
347*758e9fbaSOystein Eftevaag SAFE_FREE(tpmCipherText);
348*758e9fbaSOystein Eftevaag
349*758e9fbaSOystein Eftevaag /* Flush the key from the TPM. */
350*758e9fbaSOystein Eftevaag r = Esys_FlushContext_Async(context->esys,
351*758e9fbaSOystein Eftevaag command->key_handle);
352*758e9fbaSOystein Eftevaag goto_if_error(r, "Error: FlushContext", error_cleanup);
353*758e9fbaSOystein Eftevaag fallthrough;
354*758e9fbaSOystein Eftevaag
355*758e9fbaSOystein Eftevaag statecase(context->state, DATA_ENCRYPT_WAIT_FOR_FLUSH);
356*758e9fbaSOystein Eftevaag r = Esys_FlushContext_Finish(context->esys);
357*758e9fbaSOystein Eftevaag return_try_again(r);
358*758e9fbaSOystein Eftevaag
359*758e9fbaSOystein Eftevaag goto_if_error(r, "Error: FlushContext", error_cleanup);
360*758e9fbaSOystein Eftevaag command->key_handle = ESYS_TR_NONE;
361*758e9fbaSOystein Eftevaag fallthrough;
362*758e9fbaSOystein Eftevaag
363*758e9fbaSOystein Eftevaag statecase(context->state, DATA_ENCRYPT_CLEAN)
364*758e9fbaSOystein Eftevaag /* Cleanup the sessions. */
365*758e9fbaSOystein Eftevaag r = ifapi_cleanup_session(context);
366*758e9fbaSOystein Eftevaag try_again_or_error_goto(r, "Cleanup", error_cleanup);
367*758e9fbaSOystein Eftevaag
368*758e9fbaSOystein Eftevaag break;
369*758e9fbaSOystein Eftevaag
370*758e9fbaSOystein Eftevaag statecasedefault(context->state);
371*758e9fbaSOystein Eftevaag }
372*758e9fbaSOystein Eftevaag
373*758e9fbaSOystein Eftevaag context->state = _FAPI_STATE_INIT;
374*758e9fbaSOystein Eftevaag
375*758e9fbaSOystein Eftevaag error_cleanup:
376*758e9fbaSOystein Eftevaag /* Cleanup any intermediate results and state stored in the context. */
377*758e9fbaSOystein Eftevaag if (command->key_handle != ESYS_TR_NONE)
378*758e9fbaSOystein Eftevaag Esys_FlushContext(context->esys, command->key_handle);
379*758e9fbaSOystein Eftevaag ifapi_cleanup_ifapi_object(&context->loadKey.auth_object);
380*758e9fbaSOystein Eftevaag ifapi_cleanup_ifapi_object(context->loadKey.key_object);
381*758e9fbaSOystein Eftevaag ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
382*758e9fbaSOystein Eftevaag ifapi_cleanup_ifapi_object(command->key_object);
383*758e9fbaSOystein Eftevaag SAFE_FREE(tpmCipherText);
384*758e9fbaSOystein Eftevaag SAFE_FREE(command->keyPath);
385*758e9fbaSOystein Eftevaag SAFE_FREE(command->in_data);
386*758e9fbaSOystein Eftevaag SAFE_FREE(command->out_data);
387*758e9fbaSOystein Eftevaag ifapi_session_clean(context);
388*758e9fbaSOystein Eftevaag LOG_TRACE("finished");
389*758e9fbaSOystein Eftevaag return r;
390*758e9fbaSOystein Eftevaag }
391