xref: /aosp_15_r20/external/tpm2-tss/test/integration/sapi-start-auth-session.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 <inttypes.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 
15 #include "tss2_sys.h"
16 
17 #define LOGMODULE test
18 #include "util/log.h"
19 #include "test.h"
20 /*
21  * This is an incredibly simple test to create the most simple session
22  * (which ends up being a trial policy) and then just tear it down.
23  */
24 int
test_invoke(TSS2_SYS_CONTEXT * sapi_context)25 test_invoke (TSS2_SYS_CONTEXT *sapi_context)
26 {
27     TSS2_RC rc;
28     TPM2B_NONCE nonce_caller = {
29         .size   = TPM2_SHA256_DIGEST_SIZE,
30         .buffer = {
31                 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
32                 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
33                 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
34                 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef
35             }
36     };
37     TPM2B_NONCE nonce_tpm = {
38         .size   = TPM2_SHA256_DIGEST_SIZE,
39         .buffer = { 0 }
40     };
41     TPM2B_ENCRYPTED_SECRET encrypted_salt = { 0 };
42     TPMI_SH_AUTH_SESSION   session_handle = 0;
43     TPMT_SYM_DEF           symmetric      = { .algorithm = TPM2_ALG_NULL };
44 
45     LOG_INFO("StartAuthSession for TPM2_SE_POLICY (policy session)");
46     rc = Tss2_Sys_StartAuthSession (sapi_context,
47                                     TPM2_RH_NULL,     /* tpmKey */
48                                     TPM2_RH_NULL,     /* bind */
49                                     0,               /* cmdAuthsArray */
50                                     &nonce_caller,   /* nonceCaller */
51                                     &encrypted_salt, /* encryptedSalt */
52                                     TPM2_SE_POLICY,   /* sessionType */
53                                     &symmetric,      /* symmetric */
54                                     TPM2_ALG_SHA256,  /* authHash */
55                                     &session_handle, /* sessionHandle */
56                                     &nonce_tpm,      /* nonceTPM */
57                                     0                /* rspAuthsArray */
58                                     );
59     if (rc != TSS2_RC_SUCCESS) {
60         LOG_ERROR("Tss2_Sys_StartAuthSession failed: 0x%" PRIx32, rc);
61         exit(1);
62     }
63     LOG_INFO("StartAuthSession for TPM2_SE_POLICY success! Session handle: "
64                "0x%" PRIx32, session_handle);
65     /*
66      * Clean out the session we've created. Would be nice if we didn't have
67      * to do this ...
68      */
69     rc = Tss2_Sys_FlushContext (sapi_context, session_handle);
70     if (rc != TSS2_RC_SUCCESS) {
71         LOG_ERROR("Tss2_Sys_FlushContext failed: 0x%" PRIx32, rc);
72         exit(1);
73     }
74     LOG_INFO("Flushed context for session handle: 0x%" PRIx32 " success!",
75                session_handle);
76 
77     return 0;
78 }
79