1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /***********************************************************************
3 * Copyright (c) 2017-2018, Intel Corporation
4 *
5 * All rights reserved.
6 ***********************************************************************/
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include <inttypes.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #define LOGMODULE test
16 #include "util/log.h"
17 #include "sapi-util.h"
18 #include "test-esapi.h"
19 #include "test.h"
20
21 #define ENC_STR "test-data-test-data-test-data"
22
23 /*
24 * This test is intended to exercise the EncryptDecrypt2 command.
25 */
26 int
test_invoke(TSS2_SYS_CONTEXT * sapi_context)27 test_invoke (TSS2_SYS_CONTEXT *sapi_context)
28 {
29 TSS2_RC rc;
30 TPM2_HANDLE handle_parent, handle;
31 TPM2B_MAX_BUFFER data_in = { 0 };
32 TPM2B_MAX_BUFFER data_encrypted = TPM2B_MAX_BUFFER_INIT;
33 TPM2B_MAX_BUFFER data_decrypted = TPM2B_MAX_BUFFER_INIT;
34
35 data_in.size = strlen (ENC_STR);
36 strcpy ((char*)data_in.buffer, ENC_STR);
37
38 rc = create_primary_rsa_2048_aes_128_cfb (sapi_context, &handle_parent);
39 if (rc != TSS2_RC_SUCCESS) {
40 LOG_ERROR("Failed to create primary RSA 2048 key: 0x%" PRIx32 "",
41 rc);
42 exit(1);
43 }
44
45 rc = create_aes_128_cfb (sapi_context, handle_parent, &handle);
46 if (rc != TSS2_RC_SUCCESS) {
47 LOG_ERROR("Failed to create child AES 128 key: 0x%" PRIx32 "", rc);
48 exit(1);
49 }
50
51 LOG_INFO("Encrypting data: \"%s\" with key handle: 0x%08" PRIx32,
52 data_in.buffer, handle);
53 rc = tpm_encrypt_2_cfb (sapi_context, handle, &data_in, &data_encrypted);
54
55 if (rc == TPM2_RC_COMMAND_CODE) {
56 LOG_WARNING("Encrypt/Decrypt 2 not supported by TPM");
57 rc = Tss2_Sys_FlushContext(sapi_context, handle_parent);
58 if (rc != TSS2_RC_SUCCESS) {
59 LOG_ERROR("Tss2_Sys_FlushContext failed with 0x%"PRIx32, rc);
60 return 99; /* fatal error */
61 }
62 rc = Tss2_Sys_FlushContext(sapi_context, handle);
63 if (rc != TSS2_RC_SUCCESS) {
64 LOG_ERROR("Tss2_Sys_FlushContext failed with 0x%"PRIx32, rc);
65 return 99; /* fatal error */
66 }
67 return EXIT_SKIP;
68 }
69
70 if (rc != TSS2_RC_SUCCESS) {
71 LOG_ERROR("Failed to encrypt buffer: 0x%" PRIx32 "", rc);
72 exit(1);
73 }
74
75 rc = tpm_decrypt_2_cfb (sapi_context, handle, &data_encrypted, &data_decrypted);
76 if (rc != TSS2_RC_SUCCESS) {
77 LOG_ERROR("Failed to encrypt buffer: 0x%" PRIx32 "", rc);
78 exit(1);
79 }
80 LOG_INFO("Decrypted data: \"%s\" with key handle: 0x%08" PRIx32,
81 data_decrypted.buffer, handle);
82
83 if (strcmp ((char*)data_in.buffer, (char*)data_decrypted.buffer)) {
84 LOG_ERROR("Decrypt succeeded but decrypted data != to input data");
85 exit(1);
86 }
87
88 rc = Tss2_Sys_FlushContext(sapi_context, handle_parent);
89 if (rc != TSS2_RC_SUCCESS) {
90 LOG_ERROR("Tss2_Sys_FlushContext failed with 0x%"PRIx32, rc);
91 return 99; /* fatal error */
92 }
93 rc = Tss2_Sys_FlushContext(sapi_context, handle);
94 if (rc != TSS2_RC_SUCCESS) {
95 LOG_ERROR("Tss2_Sys_FlushContext failed with 0x%"PRIx32, rc);
96 return 99; /* fatal error */
97 }
98
99 return 0;
100 }
101