• 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 #include "test-esapi.h"
17 #define LOGMODULE test
18 #include "util/log.h"
19 #include "util/aux_util.h"
20 
21 /** This test is intended to test the ESAPI function Esys_EncryptDecrypt.
22  *
23  * First a primary key is generated. This key will be uses as parent fo a
24  * symmetric key, which will be used to encrypt and decrypt a tpm2b. The
25  * result will be compared.
26  *
27  * Tested ESAPI commands:
28  *  - Esys_Create() (M)
29  *  - Esys_CreatePrimary() (M)
30  *  - Esys_EncryptDecrypt() (O)
31  *  - Esys_FlushContext() (M)
32  *  - Esys_Load() (M)
33  *
34  * @param[in,out] esys_context The ESYS_CONTEXT.
35  * @retval EXIT_FAILURE
36  * @retval EXIT_SKIP
37  * @retval EXIT_SUCCESS
38  */
39 
40 int
test_esys_encrypt_decrypt(ESYS_CONTEXT * esys_context)41 test_esys_encrypt_decrypt(ESYS_CONTEXT * esys_context)
42 {
43     TSS2_RC r;
44     ESYS_TR primaryHandle = ESYS_TR_NONE;
45     ESYS_TR loadedKeyHandle = ESYS_TR_NONE;
46     int failure_return = EXIT_FAILURE;
47 
48     TPM2B_PUBLIC *outPublic = NULL;
49     TPM2B_CREATION_DATA *creationData = NULL;
50     TPM2B_DIGEST *creationHash = NULL;
51     TPMT_TK_CREATION *creationTicket = NULL;
52     TPM2B_MAX_BUFFER *outData = NULL;
53     TPM2B_IV *ivOut = NULL;
54 
55     TPM2B_PUBLIC *outPublic2 = NULL;
56     TPM2B_PRIVATE *outPrivate2 = NULL;
57     TPM2B_CREATION_DATA *creationData2 = NULL;
58     TPM2B_DIGEST *creationHash2 = NULL;
59     TPMT_TK_CREATION *creationTicket2 = NULL;
60     TPM2B_MAX_BUFFER *outData2 = NULL;
61     TPM2B_IV *ivOut2 = NULL;
62 
63     TPM2B_AUTH authValuePrimary = {
64         .size = 5,
65         .buffer = {1, 2, 3, 4, 5}
66     };
67 
68     TPM2B_SENSITIVE_CREATE inSensitivePrimary = {
69         .size = 0,
70         .sensitive = {
71             .userAuth = {
72                  .size = 0,
73                  .buffer = {0 },
74              },
75             .data = {
76                  .size = 0,
77                  .buffer = {0},
78              },
79         },
80     };
81 
82     inSensitivePrimary.sensitive.userAuth = authValuePrimary;
83 
84     TPM2B_PUBLIC inPublic = {
85         .size = 0,
86         .publicArea = {
87             .type = TPM2_ALG_RSA,
88             .nameAlg = TPM2_ALG_SHA256,
89             .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
90                                  TPMA_OBJECT_RESTRICTED |
91                                  TPMA_OBJECT_DECRYPT |
92                                  TPMA_OBJECT_FIXEDTPM |
93                                  TPMA_OBJECT_FIXEDPARENT |
94                                  TPMA_OBJECT_SENSITIVEDATAORIGIN),
95             .authPolicy = {
96                  .size = 0,
97              },
98             .parameters.rsaDetail = {
99                  .symmetric = {
100                      .algorithm = TPM2_ALG_AES,
101                      .keyBits.aes = 128,
102                      .mode.aes = TPM2_ALG_CFB},
103                  .scheme = {
104                       .scheme = TPM2_ALG_NULL
105                   },
106                  .keyBits = 2048,
107                  .exponent = 0,
108              },
109             .unique.rsa = {
110                  .size = 0,
111                  .buffer = {},
112              },
113         },
114     };
115 
116     TPM2B_DATA outsideInfo = {
117         .size = 0,
118         .buffer = {},
119     };
120 
121     TPML_PCR_SELECTION creationPCR = {
122         .count = 0,
123     };
124 
125     TPM2B_AUTH authValue = {
126         .size = 0,
127         .buffer = {}
128     };
129 
130     r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &authValue);
131     goto_if_error(r, "Error: TR_SetAuth", error);
132 
133     r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
134                            ESYS_TR_NONE, ESYS_TR_NONE,
135                            &inSensitivePrimary, &inPublic,
136                            &outsideInfo, &creationPCR, &primaryHandle,
137                            &outPublic, &creationData, &creationHash,
138                            &creationTicket);
139     goto_if_error(r, "Error esys create primary", error);
140 
141     r = Esys_TR_SetAuth(esys_context, primaryHandle, &authValuePrimary);
142     goto_if_error(r, "Error: TR_SetAuth", error);
143 
144     TPM2B_AUTH authKey2 = {
145         .size = 6,
146         .buffer = {6, 7, 8, 9, 10, 11}
147     };
148 
149     TPM2B_SENSITIVE_CREATE inSensitive2 = {
150         .size = 0,
151         .sensitive = {
152             .userAuth = {
153                  .size = 0,
154                  .buffer = {0}
155              },
156             .data = {
157                  .size = 16,
158                  .buffer = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16}
159              }
160         }
161     };
162 
163     inSensitive2.sensitive.userAuth = authKey2;
164 
165     TPM2B_PUBLIC inPublic2 = {
166         .size = 0,
167         .publicArea = {
168             .type = TPM2_ALG_SYMCIPHER,
169             .nameAlg = TPM2_ALG_SHA256,
170             .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
171                                  TPMA_OBJECT_SIGN_ENCRYPT |
172                                  TPMA_OBJECT_DECRYPT),
173 
174             .authPolicy = {
175                  .size = 0,
176              },
177             .parameters.symDetail = {
178                  .sym = {
179                      .algorithm = TPM2_ALG_AES,
180                      .keyBits = {.aes = 128},
181                      .mode = {.aes = TPM2_ALG_CFB}}
182              },
183             .unique.sym = {
184                  .size = 0,
185                  .buffer = {}
186              }
187         }
188     };
189 
190     TPM2B_DATA outsideInfo2 = {
191         .size = 0,
192         .buffer = {}
193         ,
194     };
195 
196     TPML_PCR_SELECTION creationPCR2 = {
197         .count = 0,
198     };
199 
200     r = Esys_Create(esys_context,
201                     primaryHandle,
202                     ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
203                     &inSensitive2,
204                     &inPublic2,
205                     &outsideInfo2,
206                     &creationPCR2,
207                     &outPrivate2,
208                     &outPublic2,
209                     &creationData2, &creationHash2, &creationTicket2);
210 
211     if (r == 0x2c2) { /*<< tpm:parameter(2):inconsistent attributes */
212         LOG_WARNING("Unsupported symmetric cipher.");
213         failure_return = EXIT_SKIP;
214         goto error;
215     }
216     goto_if_error(r, "Error esys create ", error);
217 
218     LOG_INFO("AES key created.");
219 
220     r = Esys_Load(esys_context,
221                   primaryHandle,
222                   ESYS_TR_PASSWORD,
223                   ESYS_TR_NONE,
224                   ESYS_TR_NONE, outPrivate2, outPublic2, &loadedKeyHandle);
225     goto_if_error(r, "Error esys load ", error);
226 
227     LOG_INFO("AES key loaded.");
228 
229     r = Esys_TR_SetAuth(esys_context, loadedKeyHandle, &authKey2);
230     goto_if_error(r, "Error esys TR_SetAuth ", error);
231 
232     ESYS_TR keyHandle_handle = loadedKeyHandle;
233     TPMI_YES_NO decrypt = TPM2_YES;
234     TPMI_YES_NO encrypt = TPM2_NO;
235     TPMI_ALG_SYM_MODE mode = TPM2_ALG_NULL;
236     TPM2B_IV ivIn = {
237         .size = 16,
238         .buffer = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16}
239     };
240     TPM2B_MAX_BUFFER inData = {
241         .size = 16,
242         .buffer = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16}
243     };
244 
245     r = Esys_EncryptDecrypt(
246         esys_context,
247         keyHandle_handle,
248         ESYS_TR_PASSWORD,
249         ESYS_TR_NONE,
250         ESYS_TR_NONE,
251         encrypt,
252         mode,
253         &ivIn,
254         &inData,
255         &outData,
256         &ivOut);
257 
258     if ((r == TPM2_RC_COMMAND_CODE) ||
259         (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_RC_LAYER)) ||
260         (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_TPM_RC_LAYER))) {
261         LOG_WARNING("Command TPM2_EncryptDecrypt not supported by TPM.");
262         failure_return = EXIT_SKIP;
263         goto error;
264     }
265 
266     goto_if_error(r, "Error: EncryptDecrypt", error);
267 
268 
269     r = Esys_EncryptDecrypt(
270         esys_context,
271         keyHandle_handle,
272         ESYS_TR_PASSWORD,
273         ESYS_TR_NONE,
274         ESYS_TR_NONE,
275         decrypt,
276         mode,
277         &ivIn,
278         outData,
279         &outData2,
280         &ivOut2);
281 
282     if ((r == TPM2_RC_COMMAND_CODE) ||
283         (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_RC_LAYER)) ||
284         (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_TPM_RC_LAYER))) {
285         LOG_WARNING("Command TPM2_EncryptDecrypt not supported by TPM.");
286         failure_return = EXIT_SKIP;
287         goto error;
288     }
289 
290     goto_if_error(r, "Error: EncryptDecrypt", error);
291 
292     LOGBLOB_DEBUG(&outData2->buffer[0], outData2->size, "** Decrypted data **");
293 
294     if (outData2->size != inData.size ||
295         memcmp(&outData2->buffer, &inData.buffer[0], outData2->size) != 0) {
296         LOG_ERROR("Error: decrypted text not  equal to origin");
297         goto error;
298     }
299 
300     r = Esys_FlushContext(esys_context, primaryHandle);
301     goto_if_error(r, "Error during FlushContext", error);
302 
303     primaryHandle = ESYS_TR_NONE;
304 
305     r = Esys_FlushContext(esys_context, loadedKeyHandle);
306     goto_if_error(r, "Error during FlushContext", error);
307 
308     Esys_Free(outPublic);
309     Esys_Free(creationData);
310     Esys_Free(creationHash);
311     Esys_Free(creationTicket);
312     Esys_Free(outData);
313     Esys_Free(ivOut);
314 
315     Esys_Free(outPublic2);
316     Esys_Free(outPrivate2);
317     Esys_Free(creationData2);
318     Esys_Free(creationHash2);
319     Esys_Free(creationTicket2);
320     Esys_Free(outData2);
321     Esys_Free(ivOut2);
322     return EXIT_SUCCESS;
323 
324  error:
325 
326     if (primaryHandle != ESYS_TR_NONE) {
327         if (Esys_FlushContext(esys_context, primaryHandle) != TSS2_RC_SUCCESS) {
328             LOG_ERROR("Cleanup primaryHandle failed.");
329         }
330     }
331 
332     if (loadedKeyHandle != ESYS_TR_NONE) {
333         if (Esys_FlushContext(esys_context, loadedKeyHandle) != TSS2_RC_SUCCESS) {
334             LOG_ERROR("Cleanup loadedKeyHandle failed.");
335         }
336     }
337     Esys_Free(outPublic);
338     Esys_Free(creationData);
339     Esys_Free(creationHash);
340     Esys_Free(creationTicket);
341     Esys_Free(outData);
342     Esys_Free(ivOut);
343 
344     Esys_Free(outPublic2);
345     Esys_Free(outPrivate2);
346     Esys_Free(creationData2);
347     Esys_Free(creationHash2);
348     Esys_Free(creationTicket2);
349     Esys_Free(outData2);
350     Esys_Free(ivOut2);
351     return failure_return;
352 }
353 
354 int
test_invoke_esapi(ESYS_CONTEXT * esys_context)355 test_invoke_esapi(ESYS_CONTEXT * esys_context) {
356     return test_esys_encrypt_decrypt(esys_context);
357 }
358