1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /***********************************************************************
3  * Copyright (c) 2015 - 2018 Intel Corporation
4  * All rights reserved.
5  ***********************************************************************/
6 #ifndef TCTI_COMMON_H
7 #define TCTI_COMMON_H
8 
9 #include <errno.h>
10 #include <stdbool.h>
11 
12 #include "tss2_tcti.h"
13 
14 #define TCTI_VERSION 0x2
15 
16 #define TPM_HEADER_SIZE (sizeof (TPM2_ST) + sizeof (UINT32) + sizeof (UINT32))
17 
18 typedef struct {
19     TPM2_ST tag;
20     UINT32 size;
21     UINT32 code;
22 } tpm_header_t;
23 /*
24  * The elements in this enumeration represent the possible states that the
25  * TCTI can be in. The state machine is as follows:
26  * An instantiated TCTI context begins in the TRANSMIT state:
27  *   TRANSMIT:
28  *     transmit:    success transitions the state machine to RECEIVE
29  *                  failure leaves the state unchanged
30  *     receive:     produces TSS2_TCTI_RC_BAD_SEQUENCE
31  *     finalize:    transitions state machine to FINAL state
32  *     cancel:      produces TSS2_TCTI_RC_BAD_SEQUENCE
33  *     setLocality: success or failure leaves state unchanged
34  *   RECEIVE:
35  *     transmit:    produces TSS2_TCTI_RC_BAD_SEQUENCE
36  *     receive:     success transitions the state machine to TRANSMIT
37  *                  failure with the following RCs leave the state unchanged:
38  *                    TRY_AGAIN, INSUFFICIENT_BUFFER, BAD_CONTEXT,
39  *                    BAD_REFERENCE, BAD_VALUE, BAD_SEQUENCE
40  *                  all other failures transition state machine to
41  *                    TRANSMIT (not recoverable)
42  *     finalize:    transitions state machine to FINAL state
43  *     cancel:      success transitions state machine to TRANSMIT
44  *                  failure leaves state unchanged
45  *     setLocality: produces TSS2_TCTI_RC_BAD_SEQUENCE
46  *   FINAL:
47  *     all function calls produce TSS2_TCTI_RC_BAD_SEQUENCE
48  */
49 typedef enum {
50     TCTI_STATE_FINAL,
51     TCTI_STATE_TRANSMIT,
52     TCTI_STATE_RECEIVE,
53 } tcti_state_t;
54 
55 typedef struct {
56     TSS2_TCTI_CONTEXT_COMMON_V2 v2;
57     tcti_state_t state;
58     tpm_header_t header;
59     uint8_t locality;
60     bool partial_read_supported;
61     bool partial;
62 } TSS2_TCTI_COMMON_CONTEXT;
63 
64 /*
65  */
66 TSS2_TCTI_COMMON_CONTEXT*
67 tcti_common_context_cast (TSS2_TCTI_CONTEXT *ctx);
68 /*
69  * This function is used to "down cast" the Intel TCTI context to the opaque
70  * context type.
71  */
72 TSS2_TCTI_CONTEXT*
73 tcti_common_down_cast (TSS2_TCTI_COMMON_CONTEXT *ctx);
74 /*
75  * This function performs checks on the common context structure passed to a
76  * TCTI 'cancel' function.
77  */
78 TSS2_RC
79 tcti_common_cancel_checks (
80     TSS2_TCTI_COMMON_CONTEXT *tcti_common,
81     uint64_t magic);
82 /*
83  * This function performs common checks on the context structure and the
84  * buffer passed into TCTI 'transmit' functions.
85  */
86 TSS2_RC
87 tcti_common_transmit_checks (
88     TSS2_TCTI_COMMON_CONTEXT *tcti_common,
89     const uint8_t *command_buffer,
90     uint64_t magic);
91 /*
92  * This function performs common checks on the context structure, buffer and
93  * size parameter passed to the TCTI 'receive' functions.
94  */
95 TSS2_RC
96 tcti_common_receive_checks (
97     TSS2_TCTI_COMMON_CONTEXT *tcti_common,
98     size_t *response_size,
99     uint64_t magic);
100 /*
101  * This function performs checks on the common context structure passed to a
102  * TCTI 'set_locality' function.
103  */
104 TSS2_RC
105 tcti_common_set_locality_checks (
106     TSS2_TCTI_COMMON_CONTEXT *tcti_common,
107     uint64_t magic);
108 /*
109  * Just a function with the right prototype that returns the not implemented
110  * RC for the TCTI layer.
111  */
112 TSS2_RC
113 tcti_make_sticky_not_implemented (
114     TSS2_TCTI_CONTEXT *tctiContext,
115     TPM2_HANDLE *handle,
116     uint8_t sticky);
117 /*
118  * Utility to function to parse the first 10 bytes of a buffer and populate
119  * the 'header' structure with the results. The provided buffer is assumed to
120  * be at least 10 bytes long.
121  */
122 TSS2_RC
123 header_unmarshal (
124     const uint8_t *buf,
125     tpm_header_t *header);
126 /*
127  */
128 TSS2_RC
129 header_marshal (
130     const tpm_header_t *header,
131     uint8_t *buf);
132 
133 #endif
134