• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /** Test the ESAPI function Esys_GetRandom.
21  *
22  * Tested ESAPI commands:
23  *  - Esys_GetRandom() (M)
24  *  - Esys_StartAuthSession() (M)
25  *
26  * @param[in,out] esys_context The ESYS_CONTEXT.
27  * @retval EXIT_FAILURE
28  * @retval EXIT_SUCCESS
29  */
30 int
test_esys_get_random(ESYS_CONTEXT * esys_context)31 test_esys_get_random(ESYS_CONTEXT * esys_context)
32 {
33 
34     TSS2_RC r;
35 
36     TPM2B_DIGEST *randomBytes;
37     r = Esys_GetRandom(esys_context, ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
38                        48, &randomBytes);
39     if (r != TPM2_RC_SUCCESS) {
40         LOG_ERROR("GetRandom FAILED! Response Code : 0x%x", r);
41         goto error;
42     }
43 
44     LOGBLOB_DEBUG(&randomBytes->buffer[0], randomBytes->size,
45                   "Randoms (count=%i):", randomBytes->size);
46     Esys_Free(randomBytes);
47 
48     LOG_INFO("GetRandom Test Passed!");
49 
50     ESYS_TR session = ESYS_TR_NONE;
51     const TPMT_SYM_DEF symmetric = {
52         .algorithm = TPM2_ALG_AES,
53         .keyBits = {.aes = 128},
54         .mode = {.aes = TPM2_ALG_CFB}
55     };
56 
57     r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
58                               ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
59                               NULL,
60                               TPM2_SE_HMAC, &symmetric, TPM2_ALG_SHA1,
61                               &session);
62     if (r != TPM2_RC_SUCCESS) {
63         LOG_ERROR("Esys_StartAuthSession FAILED! Response Code : 0x%x", r);
64         goto error;
65     }
66 
67     r = Esys_TRSess_SetAttributes(esys_context, session, TPMA_SESSION_AUDIT,
68                                   TPMA_SESSION_CONTINUESESSION | TPMA_SESSION_AUDIT);
69     if (r != TPM2_RC_SUCCESS) {
70         LOG_ERROR("SetAttributes on session FAILED! Response Code : 0x%x", r);
71         goto error_cleansession;
72     }
73 
74     r = Esys_GetRandom(esys_context, session, ESYS_TR_NONE, ESYS_TR_NONE, 48,
75                        &randomBytes);
76     if (r != TPM2_RC_SUCCESS) {
77         LOG_ERROR("GetRandom with session FAILED! Response Code : 0x%x", r);
78         goto error_cleansession;
79     }
80 
81     LOGBLOB_DEBUG(&randomBytes->buffer[0], randomBytes->size,
82                   "Randoms (count=%i):", randomBytes->size);
83     free(randomBytes);
84 
85       r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
86                               ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
87                               NULL,
88                               TPM2_SE_HMAC, &symmetric, TPM2_ALG_SHA1,
89                               &session);
90     if (r != TPM2_RC_SUCCESS) {
91         LOG_ERROR("Esys_StartAuthSession FAILED! Response Code : 0x%x", r);
92         goto error;
93     }
94 
95     r = Esys_TRSess_SetAttributes(esys_context, session, TPMA_SESSION_AUDIT,
96                                   TPMA_SESSION_CONTINUESESSION | TPMA_SESSION_AUDIT);
97     if (r != TPM2_RC_SUCCESS) {
98         LOG_ERROR("SetAttributes on session FAILED! Response Code : 0x%x", r);
99         goto error_cleansession;
100     }
101 
102     r = Esys_GetRandom(esys_context, session, ESYS_TR_NONE, ESYS_TR_NONE, 48,
103                        &randomBytes);
104     if (r != TPM2_RC_SUCCESS) {
105         LOG_ERROR("GetRandom with session FAILED! Response Code : 0x%x", r);
106         goto error_cleansession;
107     }
108 
109     LOGBLOB_DEBUG(&randomBytes->buffer[0], randomBytes->size,
110                   "Randoms (count=%i):", randomBytes->size);
111     free(randomBytes);
112 
113       r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
114                               ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
115                               NULL,
116                               TPM2_SE_HMAC, &symmetric, TPM2_ALG_SHA1,
117                               &session);
118     if (r != TPM2_RC_SUCCESS) {
119         LOG_ERROR("Esys_StartAuthSession FAILED! Response Code : 0x%x", r);
120         goto error;
121     }
122 
123     r = Esys_TRSess_SetAttributes(esys_context, session, TPMA_SESSION_AUDIT,
124                                   TPMA_SESSION_CONTINUESESSION | TPMA_SESSION_AUDIT);
125     if (r != TPM2_RC_SUCCESS) {
126         LOG_ERROR("SetAttributes on session FAILED! Response Code : 0x%x", r);
127         goto error_cleansession;
128     }
129 
130     r = Esys_GetRandom(esys_context, session, ESYS_TR_NONE, ESYS_TR_NONE, 48,
131                        &randomBytes);
132     if (r != TPM2_RC_SUCCESS) {
133         LOG_ERROR("GetRandom with session FAILED! Response Code : 0x%x", r);
134         goto error_cleansession;
135     }
136 
137     LOGBLOB_DEBUG(&randomBytes->buffer[0], randomBytes->size,
138                   "Randoms (count=%i):", randomBytes->size);
139     free(randomBytes);
140 
141       r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
142                               ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
143                               NULL,
144                               TPM2_SE_HMAC, &symmetric, TPM2_ALG_SHA1,
145                               &session);
146     if (r != TPM2_RC_SUCCESS) {
147         LOG_ERROR("Esys_StartAuthSession FAILED! Response Code : 0x%x", r);
148         goto error;
149     }
150 
151     r = Esys_TRSess_SetAttributes(esys_context, session, TPMA_SESSION_AUDIT,
152                                   TPMA_SESSION_CONTINUESESSION | TPMA_SESSION_AUDIT);
153     if (r != TPM2_RC_SUCCESS) {
154         LOG_ERROR("SetAttributes on session FAILED! Response Code : 0x%x", r);
155         goto error_cleansession;
156     }
157 
158     r = Esys_GetRandom(esys_context, session, ESYS_TR_NONE, ESYS_TR_NONE, 48,
159                        &randomBytes);
160     if (r != TPM2_RC_SUCCESS) {
161         LOG_ERROR("GetRandom with session FAILED! Response Code : 0x%x", r);
162         goto error_cleansession;
163     }
164 
165     LOGBLOB_DEBUG(&randomBytes->buffer[0], randomBytes->size,
166                   "Randoms (count=%i):", randomBytes->size);
167     free(randomBytes);
168 
169       r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
170                               ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
171                               NULL,
172                               TPM2_SE_HMAC, &symmetric, TPM2_ALG_SHA1,
173                               &session);
174     if (r != TPM2_RC_SUCCESS) {
175         LOG_ERROR("Esys_StartAuthSession FAILED! Response Code : 0x%x", r);
176         goto error;
177     }
178 
179     r = Esys_TRSess_SetAttributes(esys_context, session, TPMA_SESSION_AUDIT,
180                                   TPMA_SESSION_CONTINUESESSION | TPMA_SESSION_AUDIT);
181     if (r != TPM2_RC_SUCCESS) {
182         LOG_ERROR("SetAttributes on session FAILED! Response Code : 0x%x", r);
183         goto error_cleansession;
184     }
185 
186     r = Esys_GetRandom(esys_context, session, ESYS_TR_NONE, ESYS_TR_NONE, 48,
187                        &randomBytes);
188     if (r != TPM2_RC_SUCCESS) {
189         LOG_ERROR("GetRandom with session FAILED! Response Code : 0x%x", r);
190         goto error_cleansession;
191     }
192 
193     LOGBLOB_DEBUG(&randomBytes->buffer[0], randomBytes->size,
194                   "Randoms (count=%i):", randomBytes->size);
195     free(randomBytes);
196 
197     LOG_INFO("GetRandom with session Test Passed!");
198 
199     //r = Esys_FlushContext(esys_context, session);
200     if (r != TPM2_RC_SUCCESS) {
201         LOG_ERROR("FlushContext with session FAILED! Response Code : 0x%x", r);
202         goto error_cleansession;
203     }
204 
205     return EXIT_SUCCESS;
206 
207  error_cleansession:
208     r = Esys_FlushContext(esys_context, session);
209     if (r != TPM2_RC_SUCCESS) {
210         LOG_ERROR("FlushContext FAILED! Response Code : 0x%x", r);
211     }
212  error:
213     return EXIT_FAILURE;
214 }
215 
216 int
test_invoke_esapi(ESYS_CONTEXT * esys_context)217 test_invoke_esapi(ESYS_CONTEXT * esys_context) {
218     return test_esys_get_random(esys_context);
219 }
220