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
13 #include "tss2_esys.h"
14
15 #include "esys_iutil.h"
16 #define LOGMODULE test
17 #include "util/log.h"
18 #include "util/aux_util.h"
19
20 /** This test is intended to test the ESAPI command Esys_HMAC with password
21 * authentication.
22 *
23 * We create a symmetric HMAC key signing key which will be used
24 * for signing. This key will be used to create the HMAC for a test
25 * buffer.
26 *
27 * Tested ESAPI commands:
28 * - Esys_CreatePrimary() (M)
29 * - Esys_FlushContext() (M)
30 * - Esys_HMAC() (O)
31 *
32 * @param[in,out] esys_context The ESYS_CONTEXT.
33 * @retval EXIT_FAILURE
34 * @retval EXIT_SUCCESS
35 */
36
37 int
test_esys_hmac(ESYS_CONTEXT * esys_context)38 test_esys_hmac(ESYS_CONTEXT * esys_context)
39 {
40 TSS2_RC r;
41 ESYS_TR primaryHandle = ESYS_TR_NONE;
42
43 TPM2B_PUBLIC *outPublic = NULL;
44 TPM2B_CREATION_DATA *creationData = NULL;
45 TPM2B_DIGEST *creationHash = NULL;
46 TPMT_TK_CREATION *creationTicket = NULL;
47 TPM2B_DIGEST *outHMAC = NULL;
48
49 TPM2B_AUTH authValuePrimary = {
50 .size = 5,
51 .buffer = {1, 2, 3, 4, 5}
52 };
53
54 TPM2B_SENSITIVE_CREATE inSensitivePrimary = {
55 .size = 0,
56 .sensitive = {
57 .userAuth = {
58 .size = 0,
59 .buffer = {0 },
60 },
61 .data = {
62 .size = 0,
63 .buffer = {0},
64 },
65 },
66 };
67 inSensitivePrimary.sensitive.userAuth = authValuePrimary;
68 TPM2B_PUBLIC inPublic = { 0 };
69
70 TPM2B_DATA outsideInfo = {
71 .size = 0,
72 .buffer = {},
73 };
74 TPML_PCR_SELECTION creationPCR = {
75 .count = 0,
76 };
77
78 inPublic.publicArea.nameAlg = TPM2_ALG_SHA1;
79 inPublic.publicArea.type = TPM2_ALG_KEYEDHASH;
80 inPublic.publicArea.objectAttributes |= TPMA_OBJECT_SIGN_ENCRYPT;
81 inPublic.publicArea.objectAttributes |= TPMA_OBJECT_USERWITHAUTH;
82 inPublic.publicArea.objectAttributes |= TPMA_OBJECT_SENSITIVEDATAORIGIN;
83 inPublic.publicArea.parameters.keyedHashDetail.scheme.scheme = TPM2_ALG_HMAC;
84 inPublic.publicArea.parameters.keyedHashDetail.scheme.details.hmac.hashAlg = TPM2_ALG_SHA1;
85
86 r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
87 ESYS_TR_NONE, ESYS_TR_NONE, &inSensitivePrimary,
88 &inPublic, &outsideInfo, &creationPCR,
89 &primaryHandle, &outPublic, &creationData,
90 &creationHash, &creationTicket);
91 goto_if_error(r, "Error: CreatePrimary", error);
92
93 r = Esys_TR_SetAuth(esys_context, primaryHandle, &authValuePrimary);
94 goto_if_error(r, "Error: TR_SetAuth", error);
95
96 TPM2B_MAX_BUFFER test_buffer = { .size = 20,
97 .buffer={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
98 1, 2, 3, 4, 5, 6, 7, 8, 9}} ;
99 r = Esys_HMAC(
100 esys_context,
101 primaryHandle,
102 ESYS_TR_PASSWORD,
103 ESYS_TR_NONE,
104 ESYS_TR_NONE,
105 &test_buffer,
106 TPM2_ALG_SHA1,
107 &outHMAC);
108 goto_if_error(r, "Error: HMAC", error);
109
110 r = Esys_FlushContext(esys_context, primaryHandle);
111 goto_if_error(r, "Error: FlushContext", error);
112
113 Esys_Free(outPublic);
114 Esys_Free(creationData);
115 Esys_Free(creationHash);
116 Esys_Free(creationTicket);
117 Esys_Free(outHMAC);
118 return EXIT_SUCCESS;
119
120 error:
121
122 if (primaryHandle != ESYS_TR_NONE) {
123 if (Esys_FlushContext(esys_context, primaryHandle) != TSS2_RC_SUCCESS) {
124 LOG_ERROR("Cleanup primaryHandle failed.");
125 }
126 }
127
128 Esys_Free(outPublic);
129 Esys_Free(creationData);
130 Esys_Free(creationHash);
131 Esys_Free(creationTicket);
132 Esys_Free(outHMAC);
133 return EXIT_FAILURE;
134 }
135
136 int
test_invoke_esapi(ESYS_CONTEXT * esys_context)137 test_invoke_esapi(ESYS_CONTEXT * esys_context) {
138 return test_esys_hmac(esys_context);
139 }
140