xref: /aosp_15_r20/external/tpm2-tss/test/integration/fapi-unseal.int.c (revision 758e9fba6fc9adbf15340f70c73baee7b168b1c9)
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 <stdlib.h>
12 #include <string.h>
13 
14 #include "tss2_fapi.h"
15 
16 #include "test-fapi.h"
17 #define LOGMODULE test
18 #include "util/log.h"
19 #include "util/aux_util.h"
20 
21 
22 /** Test the FAPI functions for sealing.
23  *
24  * Tested FAPI commands:
25  *  - Fapi_Provision()
26  *  - Fapi_CreateSeal()
27  *  - Fapi_Unseal()
28  *  - Fapi_Delete()
29  *
30  * @param[in,out] context The FAPI_CONTEXT.
31  * @retval EXIT_FAILURE
32  * @retval EXIT_SUCCESS
33  */
34 int
test_fapi_unseal(FAPI_CONTEXT * context)35 test_fapi_unseal(FAPI_CONTEXT *context)
36 {
37     TSS2_RC r;
38     size_t resultSize;
39     uint8_t *result = NULL;
40 
41     TPM2B_DIGEST digest = {
42         .size = 20,
43         .buffer = {
44             0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
45             0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f
46         }
47     };
48 
49     r = Fapi_Provision(context, NULL, NULL, NULL);
50     goto_if_error(r, "Error Fapi_Provision", error);
51 
52     r = Fapi_CreateSeal(context, "/HS/SRK/mySealObject", "noDa",
53                         digest.size,
54                         "", "",  &digest.buffer[0]);
55     goto_if_error(r, "Error Fapi_CreateSeal", error);
56 
57     r = Fapi_Unseal(context, "/HS/SRK/mySealObject", &result,
58                     &resultSize);
59     goto_if_error(r, "Error Fapi_CreateSeal", error);
60 
61     r = Fapi_Delete(context, "/HS/SRK/mySealObject");
62     goto_if_error(r, "Error Fapi_Delete", error);
63 
64     r = Fapi_Delete(context, "/HS/SRK");
65     goto_if_error(r, "Error Fapi_Delete", error);
66 
67     if (resultSize != digest.size ||
68             memcmp(result, &digest.buffer[0], resultSize) != 0) {
69         LOG_ERROR("Error: unealed data not  equal to origin");
70         goto error;
71     }
72 
73     SAFE_FREE(result);
74     return EXIT_SUCCESS;
75 
76 error:
77     SAFE_FREE(result);
78     return EXIT_FAILURE;
79 }
80 
81 int
test_invoke_fapi(FAPI_CONTEXT * fapi_context)82 test_invoke_fapi(FAPI_CONTEXT *fapi_context)
83 {
84     return test_fapi_unseal(fapi_context);
85 }
86