xref: /aosp_15_r20/external/tpm2-tss/test/integration/sapi-create-keyedhash-sha1-hmac.int.c (revision 758e9fba6fc9adbf15340f70c73baee7b168b1c9)
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 <stdlib.h>
12 
13 #include "tss2_tpm2_types.h"
14 
15 #include "inttypes.h"
16 #define LOGMODULE test
17 #include "util/log.h"
18 #include "sapi-util.h"
19 #include "test.h"
20 
21 int
test_invoke(TSS2_SYS_CONTEXT * sapi_context)22 test_invoke (TSS2_SYS_CONTEXT *sapi_context)
23 {
24     TSS2_RC                  rc                = TPM2_RC_SUCCESS;
25     TPM2_HANDLE              parent_handle     = 0;
26     TPM2B_SENSITIVE_CREATE  inSensitive       = { 0 };
27     TPM2B_DATA              outsideInfo       = { 0 };
28     TPML_PCR_SELECTION      creationPCR       = { 0 };
29 
30     TPM2B_PRIVATE       outPrivate             = TPM2B_PRIVATE_INIT;
31     TPM2B_PUBLIC        inPublic               = { 0 };
32     TPM2B_PUBLIC        outPublic              = { 0 };
33     TPM2B_CREATION_DATA creationData           = { 0 };
34     TPM2B_DIGEST        creationHash           = TPM2B_DIGEST_INIT;
35     TPMT_TK_CREATION    creationTicket         = { 0 };
36 
37     /* session parameters */
38     /* command session info */
39     TSS2L_SYS_AUTH_COMMAND  sessions_cmd         = {
40         .auths = {{ .sessionHandle = TPM2_RS_PW }},
41         .count = 1
42     };
43     /* response session info */
44     TSS2L_SYS_AUTH_RESPONSE  sessions_rsp         = {
45         .auths = { 0 },
46         .count = 0
47     };
48 
49     rc = create_primary_rsa_2048_aes_128_cfb (sapi_context, &parent_handle);
50     if (rc == TSS2_RC_SUCCESS) {
51         LOG_INFO("primary created successfully: 0x%" PRIx32, parent_handle);
52     } else {
53         LOG_ERROR("CreatePrimary failed with 0x%" PRIx32, rc);
54         return 99; /* fatal error */
55     }
56 
57     inPublic.publicArea.nameAlg = TPM2_ALG_SHA1;
58     inPublic.publicArea.type = TPM2_ALG_KEYEDHASH;
59     inPublic.publicArea.objectAttributes |= TPMA_OBJECT_SIGN_ENCRYPT;
60     inPublic.publicArea.objectAttributes |= TPMA_OBJECT_SENSITIVEDATAORIGIN;
61     inPublic.publicArea.parameters.keyedHashDetail.scheme.scheme = TPM2_ALG_HMAC;
62     inPublic.publicArea.parameters.keyedHashDetail.scheme.details.hmac.hashAlg = TPM2_ALG_SHA1;
63 
64     LOG_INFO("Create keyedhash SHA1 HMAC");
65     rc = TSS2_RETRY_EXP (Tss2_Sys_Create (sapi_context,
66                                           parent_handle,
67                                           &sessions_cmd,
68                                           &inSensitive,
69                                           &inPublic,
70                                           &outsideInfo,
71                                           &creationPCR,
72                                           &outPrivate,
73                                           &outPublic,
74                                           &creationData,
75                                           &creationHash,
76                                           &creationTicket,
77                                           &sessions_rsp));
78     if (rc == TPM2_RC_SUCCESS) {
79         LOG_INFO("success");
80     } else {
81         LOG_ERROR("Create FAILED! Response Code : 0x%x", rc);
82         return 1;
83     }
84 
85     rc = Tss2_Sys_FlushContext(sapi_context, parent_handle);
86     if (rc != TSS2_RC_SUCCESS) {
87         LOG_ERROR("Tss2_Sys_FlushContext failed with 0x%"PRIx32, rc);
88         return 99; /* fatal error */
89     }
90 
91     return 0;
92 }
93