xref: /aosp_15_r20/external/tpm2-tss/test/integration/sapi-pcr-extension.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 <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "tss2_sys.h"
16 
17 #define LOGMODULE test
18 #include "util/log.h"
19 #include "test.h"
20 #include "sapi-util.h"
21 #define PCR_8   8
22 /**
23  * This program contains integration test for SAPI Tss2_Sys_PCR_Read
24  * and Tss2_Sys_PCR_Extend. This is an use case scenario on PCR extend.
25  * First, we will get the list of PCR available through getcapability
26  * SAPI. Then, PCR_Read SAPI is called to list out the PCR value and
27  * PCR_Extend SAPI is called next to update the PCR value. Last,
28  * PCR_Read SAPI is called again to check the PCR values are changed.
29  */
30 int
test_invoke(TSS2_SYS_CONTEXT * sapi_context)31 test_invoke (TSS2_SYS_CONTEXT *sapi_context)
32 {
33     TSS2_RC rc;
34     TPMI_YES_NO more_data;
35     TPMS_CAPABILITY_DATA capability_data;
36     UINT16 i, digest_size;
37     TPML_PCR_SELECTION  pcr_selection;
38     UINT32 pcr_update_counter_before_extend;
39     UINT32 pcr_update_counter_after_extend;
40     UINT8 pcr_before_extend[20];
41     UINT8 pcr_after_extend[20];
42     TPML_DIGEST pcr_values;
43     TPML_DIGEST_VALUES digests;
44     TPML_PCR_SELECTION pcr_selection_out;
45 
46     TSS2L_SYS_AUTH_COMMAND sessions_data = {
47         .count = 1,
48         .auths = {{.sessionHandle = TPM2_RS_PW,
49             .sessionAttributes = 0,
50             .nonce={.size=0},
51             .hmac={.size=0}}}};
52 
53     LOG_INFO("PCR Extension tests started.");
54     rc = Tss2_Sys_GetCapability(sapi_context, 0, TPM2_CAP_PCR_PROPERTIES, TPM2_PT_PCR_COUNT, 1, &more_data, &capability_data, 0);
55     if (rc != TSS2_RC_SUCCESS) {
56         LOG_ERROR("GetCapability FAILED! Response Code : 0x%x", rc);
57         exit(1);
58     }
59     digests.count = 1;
60     digests.digests[0].hashAlg = TPM2_ALG_SHA1;
61     digest_size = GetDigestSize( digests.digests[0].hashAlg );
62 
63     for( i = 0; i < digest_size; i++ )
64     {
65         digests.digests[0].digest.sha1[i] = (UINT8)(i % 256);
66     }
67     pcr_selection.count = 1;
68     pcr_selection.pcrSelections[0].hash = TPM2_ALG_SHA1;
69     pcr_selection.pcrSelections[0].sizeofSelect = 3;
70     pcr_selection.pcrSelections[0].pcrSelect[0] = 0;
71     pcr_selection.pcrSelections[0].pcrSelect[1] = 0;
72     pcr_selection.pcrSelections[0].pcrSelect[2] = 0;
73     pcr_selection.pcrSelections[0].pcrSelect[PCR_8 / 8] = 1 << (PCR_8 % 8);
74 
75     rc = Tss2_Sys_PCR_Read(sapi_context, 0, &pcr_selection, &pcr_update_counter_before_extend, &pcr_selection_out, &pcr_values, 0);
76     if (rc != TSS2_RC_SUCCESS) {
77         LOG_ERROR("PCR_Read FAILED! Response Code : 0x%x", rc);
78         exit(1);
79     }
80     memcpy(&(pcr_before_extend[0]), &(pcr_values.digests[0].buffer[0]), pcr_values.digests[0].size);
81 
82     rc = Tss2_Sys_PCR_Extend(sapi_context, PCR_8, &sessions_data, &digests, 0);
83     if (rc != TSS2_RC_SUCCESS) {
84         LOG_ERROR("PCR_Extend FAILED! Response Code : 0x%x", rc);
85         exit(1);
86     }
87     rc = Tss2_Sys_PCR_Read(sapi_context, 0, &pcr_selection, &pcr_update_counter_after_extend, &pcr_selection_out, &pcr_values, 0);
88     if (rc != TSS2_RC_SUCCESS) {
89         LOG_ERROR("PCR_Read FAILED! Response Code : 0x%x", rc);
90         exit(1);
91     }
92     memcpy(&(pcr_after_extend[0]), &(pcr_values.digests[0].buffer[0]), pcr_values.digests[0].size);
93 
94     if(pcr_update_counter_before_extend == pcr_update_counter_after_extend) {
95         LOG_ERROR("ERROR!! pcr_update_counter didn't change value");
96         exit(1);
97     }
98     if(memcmp(&(pcr_before_extend[0]), &(pcr_after_extend[0]), 20) == 0) {
99         LOG_ERROR("ERROR!! PCR didn't change value");
100         exit(1);
101     }
102     LOG_INFO("PCR Extension Test Passed!");
103     return 0;
104 }
105