xref: /aosp_15_r20/external/ms-tpm-20-ref/TPMCmd/tpm/include/TableMarshal.h (revision 5c591343844d1f9da7da26467c4bf7efc8a7a413)
1*5c591343SA. Cody Schuffelen /* Microsoft Reference Implementation for TPM 2.0
2*5c591343SA. Cody Schuffelen  *
3*5c591343SA. Cody Schuffelen  *  The copyright in this software is being made available under the BSD License,
4*5c591343SA. Cody Schuffelen  *  included below. This software may be subject to other third party and
5*5c591343SA. Cody Schuffelen  *  contributor rights, including patent rights, and no such rights are granted
6*5c591343SA. Cody Schuffelen  *  under this license.
7*5c591343SA. Cody Schuffelen  *
8*5c591343SA. Cody Schuffelen  *  Copyright (c) Microsoft Corporation
9*5c591343SA. Cody Schuffelen  *
10*5c591343SA. Cody Schuffelen  *  All rights reserved.
11*5c591343SA. Cody Schuffelen  *
12*5c591343SA. Cody Schuffelen  *  BSD License
13*5c591343SA. Cody Schuffelen  *
14*5c591343SA. Cody Schuffelen  *  Redistribution and use in source and binary forms, with or without modification,
15*5c591343SA. Cody Schuffelen  *  are permitted provided that the following conditions are met:
16*5c591343SA. Cody Schuffelen  *
17*5c591343SA. Cody Schuffelen  *  Redistributions of source code must retain the above copyright notice, this list
18*5c591343SA. Cody Schuffelen  *  of conditions and the following disclaimer.
19*5c591343SA. Cody Schuffelen  *
20*5c591343SA. Cody Schuffelen  *  Redistributions in binary form must reproduce the above copyright notice, this
21*5c591343SA. Cody Schuffelen  *  list of conditions and the following disclaimer in the documentation and/or
22*5c591343SA. Cody Schuffelen  *  other materials provided with the distribution.
23*5c591343SA. Cody Schuffelen  *
24*5c591343SA. Cody Schuffelen  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25*5c591343SA. Cody Schuffelen  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26*5c591343SA. Cody Schuffelen  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27*5c591343SA. Cody Schuffelen  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28*5c591343SA. Cody Schuffelen  *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29*5c591343SA. Cody Schuffelen  *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30*5c591343SA. Cody Schuffelen  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31*5c591343SA. Cody Schuffelen  *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32*5c591343SA. Cody Schuffelen  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*5c591343SA. Cody Schuffelen  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*5c591343SA. Cody Schuffelen  */
35*5c591343SA. Cody Schuffelen 
36*5c591343SA. Cody Schuffelen #ifndef _TABLE_MARSHAL_H_
37*5c591343SA. Cody Schuffelen #define _TABLE_MARSHAL_H_
38*5c591343SA. Cody Schuffelen 
39*5c591343SA. Cody Schuffelen // These are the basic unmarshaling types. This is in the first byte of
40*5c591343SA. Cody Schuffelen // each structure descriptor that is passed to Marshal()/Unmarshal() for processing.
41*5c591343SA. Cody Schuffelen #define UINT_MTYPE          0
42*5c591343SA. Cody Schuffelen #define VALUES_MTYPE        (UINT_MTYPE + 1)
43*5c591343SA. Cody Schuffelen #define TABLE_MTYPE         (VALUES_MTYPE + 1)
44*5c591343SA. Cody Schuffelen #define MIN_MAX_MTYPE       (TABLE_MTYPE + 1)
45*5c591343SA. Cody Schuffelen #define ATTRIBUTES_MTYPE    (MIN_MAX_MTYPE + 1)
46*5c591343SA. Cody Schuffelen #define STRUCTURE_MTYPE     (ATTRIBUTES_MTYPE + 1)
47*5c591343SA. Cody Schuffelen #define TPM2B_MTYPE         (STRUCTURE_MTYPE + 1)
48*5c591343SA. Cody Schuffelen #define TPM2BS_MTYPE        (TPM2B_MTYPE + 1)
49*5c591343SA. Cody Schuffelen #define LIST_MTYPE          (TPM2BS_MTYPE + 1) // TPML
50*5c591343SA. Cody Schuffelen #define ERROR_MTYPE         (LIST_MTYPE + 1)
51*5c591343SA. Cody Schuffelen #define NULL_MTYPE          (ERROR_MTYPE + 1)
52*5c591343SA. Cody Schuffelen #define COMPOSITE_MTYPE     (NULL_MTYPE + 1)
53*5c591343SA. Cody Schuffelen 
54*5c591343SA. Cody Schuffelen //*** The Marshal Index
55*5c591343SA. Cody Schuffelen // A structure is used to hold the values that guide the marshaling/unmarshaling of
56*5c591343SA. Cody Schuffelen // each of the types. Each structure has a name and an address. For a structure to
57*5c591343SA. Cody Schuffelen // define a TPMS_name, the structure is a TPMS_name_MARSHAL_STRUCT and its
58*5c591343SA. Cody Schuffelen // index is TPMS_name_MARSHAL_INDEX. So, to get the proper structure, use the
59*5c591343SA. Cody Schuffelen // associated marshal index. The marshal index is passed to Marshal() or Unmarshal()
60*5c591343SA. Cody Schuffelen // and those functions look up the proper structure.
61*5c591343SA. Cody Schuffelen //
62*5c591343SA. Cody Schuffelen // To handle structures that allow a null value, the upper bit of each marshal
63*5c591343SA. Cody Schuffelen // index indicates if the null value is allowed. This is the NULL_FLAG. It is defined
64*5c591343SA. Cody Schuffelen // in TableMarshalIndex.h because it is needed by code outside of the marshaling
65*5c591343SA. Cody Schuffelen // code.
66*5c591343SA. Cody Schuffelen 
67*5c591343SA. Cody Schuffelen // A structure will have a list of marshal indexes to indicate what to unmarshal. When
68*5c591343SA. Cody Schuffelen // that index appears in a structure/union, the value will contain a flag to indicate
69*5c591343SA. Cody Schuffelen // that the NULL_FLAG should be SET on the call to Unmarshal() to unmarshal the type.
70*5c591343SA. Cody Schuffelen // The caller simply takes the entry and passes it to Unmarshal() to indicate that the
71*5c591343SA. Cody Schuffelen // NULL_FLAG is SET. There is also the opportunity to SET the NULL_FLAG in the called
72*5c591343SA. Cody Schuffelen // structure if the NULL_FLAG was set in the call to the calling structure. This is
73*5c591343SA. Cody Schuffelen // indicated by:
74*5c591343SA. Cody Schuffelen #define NULL_MASK       ~(NULL_FLAG)
75*5c591343SA. Cody Schuffelen 
76*5c591343SA. Cody Schuffelen // When looking up the value to marshal, the upper bit of the marshal index is
77*5c591343SA. Cody Schuffelen // masked to yield the actual index. The MSb is the flag bit that indicates if a
78*5c591343SA. Cody Schuffelen // null flag is set. Code does not verify that the bit is clear when the called object
79*5c591343SA. Cody Schuffelen // does not take a flag as this is a benign error.
80*5c591343SA. Cody Schuffelen 
81*5c591343SA. Cody Schuffelen // the modifier byte as used by each MTYPE shown as a structure. They are expressed
82*5c591343SA. Cody Schuffelen // as a bit maps below. However, the code uses masking and not bit fields. The types
83*5c591343SA. Cody Schuffelen // show below are just to help in understanding.
84*5c591343SA. Cody Schuffelen // NOTE: LSb0 bit numbering is assumed in these typedefs.
85*5c591343SA. Cody Schuffelen //
86*5c591343SA. Cody Schuffelen // When used in an UINT_MTYPE
87*5c591343SA. Cody Schuffelen typedef struct integerModifier {
88*5c591343SA. Cody Schuffelen     unsigned            size : 2;
89*5c591343SA. Cody Schuffelen     unsigned            sign : 1;
90*5c591343SA. Cody Schuffelen     unsigned            unused : 7;
91*5c591343SA. Cody Schuffelen } integerModifier;
92*5c591343SA. Cody Schuffelen 
93*5c591343SA. Cody Schuffelen // When used in a VALUES_MTYPE
94*5c591343SA. Cody Schuffelen typedef struct valuesModifier {
95*5c591343SA. Cody Schuffelen     unsigned            size : 2;
96*5c591343SA. Cody Schuffelen     unsigned            sign : 1;
97*5c591343SA. Cody Schuffelen     unsigned            unused : 5;
98*5c591343SA. Cody Schuffelen     unsigned            takesNull : 1;
99*5c591343SA. Cody Schuffelen } valuesModifier;
100*5c591343SA. Cody Schuffelen 
101*5c591343SA. Cody Schuffelen // When used in a TABLE_MTYPE
102*5c591343SA. Cody Schuffelen typedef struct tableModifier {
103*5c591343SA. Cody Schuffelen     unsigned            size : 2;
104*5c591343SA. Cody Schuffelen     unsigned            sign : 1;
105*5c591343SA. Cody Schuffelen     unsigned            unused : 3;
106*5c591343SA. Cody Schuffelen     unsigned            hasBits : 1;
107*5c591343SA. Cody Schuffelen     unsigned            takesNull : 1;
108*5c591343SA. Cody Schuffelen } tableModifier;
109*5c591343SA. Cody Schuffelen 
110*5c591343SA. Cody Schuffelen // the modifier byte for MIN_MAX_MTYPE
111*5c591343SA. Cody Schuffelen typedef struct minMaxModifier {
112*5c591343SA. Cody Schuffelen     unsigned            size : 2;
113*5c591343SA. Cody Schuffelen     unsigned            sign : 1;
114*5c591343SA. Cody Schuffelen     unsigned            unused : 3;
115*5c591343SA. Cody Schuffelen     unsigned            hasBits : 1;
116*5c591343SA. Cody Schuffelen     unsigned            takesNull : 1;
117*5c591343SA. Cody Schuffelen } minMaxModifier;
118*5c591343SA. Cody Schuffelen 
119*5c591343SA. Cody Schuffelen // the modifier byte for ATTRIBUTES_MTYPE
120*5c591343SA. Cody Schuffelen typedef struct attributesModifier {
121*5c591343SA. Cody Schuffelen     unsigned            size : 2;
122*5c591343SA. Cody Schuffelen     unsigned            sign : 1;
123*5c591343SA. Cody Schuffelen     unsigned            unused : 5;
124*5c591343SA. Cody Schuffelen } attributesModifier;
125*5c591343SA. Cody Schuffelen 
126*5c591343SA. Cody Schuffelen // the modifier byte is not present in a STRUCTURE_MTYPE or an TPM2B_MTYPE
127*5c591343SA. Cody Schuffelen 
128*5c591343SA. Cody Schuffelen // the modifier byte for a TPM2BS_MTYPE
129*5c591343SA. Cody Schuffelen typedef struct tpm2bsModifier {
130*5c591343SA. Cody Schuffelen     unsigned            offset : 4;
131*5c591343SA. Cody Schuffelen     unsigned            unused : 2;
132*5c591343SA. Cody Schuffelen     unsigned            sizeEqual : 1;
133*5c591343SA. Cody Schuffelen     unsigned            propigateNull : 1;
134*5c591343SA. Cody Schuffelen } tpm2bsModifier;
135*5c591343SA. Cody Schuffelen 
136*5c591343SA. Cody Schuffelen // the modifier byte for a LIST_MTYPE
137*5c591343SA. Cody Schuffelen typedef struct listModifier {
138*5c591343SA. Cody Schuffelen     unsigned            offset : 4;
139*5c591343SA. Cody Schuffelen     unsigned            unused : 2;
140*5c591343SA. Cody Schuffelen     unsigned            sizeEqual : 1;
141*5c591343SA. Cody Schuffelen     unsigned            propigateNull : 1;
142*5c591343SA. Cody Schuffelen } listModifier;
143*5c591343SA. Cody Schuffelen 
144*5c591343SA. Cody Schuffelen 
145*5c591343SA. Cody Schuffelen //*** Modifier Octet Values
146*5c591343SA. Cody Schuffelen // These are in used in anything that is an integer value. Theses would not be in
147*5c591343SA. Cody Schuffelen // structure modifier bytes (they would be used in values in structures but not the
148*5c591343SA. Cody Schuffelen // STRUCTURE_MTYPE header.
149*5c591343SA. Cody Schuffelen #define ONE_BYTES           (0)
150*5c591343SA. Cody Schuffelen #define TWO_BYTES           (1)
151*5c591343SA. Cody Schuffelen #define FOUR_BYTES          (2)
152*5c591343SA. Cody Schuffelen #define EIGHT_BYTES         (3)
153*5c591343SA. Cody Schuffelen #define SIZE_MASK           (0x3)
154*5c591343SA. Cody Schuffelen #define IS_SIGNED           (1 << 2)    // when the unmarshaled type is a signed value
155*5c591343SA. Cody Schuffelen #define SIGNED_MASK         (SIZE_MASK | IS_SIGNED)
156*5c591343SA. Cody Schuffelen 
157*5c591343SA. Cody Schuffelen // This may be used for any type except a UINT_MTYPE
158*5c591343SA. Cody Schuffelen #define TAKES_NULL          (1 << 7)    // when the type takes a null
159*5c591343SA. Cody Schuffelen 
160*5c591343SA. Cody Schuffelen // When referencing a structure, this flag indicates if a null is to be propagated
161*5c591343SA. Cody Schuffelen // to the referenced structure or type.
162*5c591343SA. Cody Schuffelen #define PROPAGATE_NULL      (TAKES_NULL)
163*5c591343SA. Cody Schuffelen 
164*5c591343SA. Cody Schuffelen // Can be used in min-max or table structures.
165*5c591343SA. Cody Schuffelen #define HAS_BITS            (1 << 6)    // when bit mask is present
166*5c591343SA. Cody Schuffelen 
167*5c591343SA. Cody Schuffelen // In a union, we need to know if this is a union of constant arrays.
168*5c591343SA. Cody Schuffelen #define IS_ARRAY_UNION      (1 << 6)
169*5c591343SA. Cody Schuffelen 
170*5c591343SA. Cody Schuffelen // In a TPM2BS_MTYPE
171*5c591343SA. Cody Schuffelen #define SIZE_EQUAL          (1 << 6)
172*5c591343SA. Cody Schuffelen #define OFFSET_MASK         (0xF)
173*5c591343SA. Cody Schuffelen 
174*5c591343SA. Cody Schuffelen // Right now, there are three spare bits in the modifiers field.
175*5c591343SA. Cody Schuffelen 
176*5c591343SA. Cody Schuffelen // Within the descriptor word of each entry in a StructMarsh_mst, there is a selector
177*5c591343SA. Cody Schuffelen // field to determine which of the sub-types the entry represents and a field that is
178*5c591343SA. Cody Schuffelen // used to reference another structure entry. This is a 6-bit field allowing a
179*5c591343SA. Cody Schuffelen // structure to have 64 entries. This should be more than enough as the structures are
180*5c591343SA. Cody Schuffelen // not that long. As of now, only 10-bits of the descriptor word leaving room for
181*5c591343SA. Cody Schuffelen // expansion.
182*5c591343SA. Cody Schuffelen 
183*5c591343SA. Cody Schuffelen // These are the values used in a STRUCTURE_MTYPE to identify the sub-type of the
184*5c591343SA. Cody Schuffelen // thing being processed
185*5c591343SA. Cody Schuffelen #define SIMPLE_STYPE                0
186*5c591343SA. Cody Schuffelen #define UNION_STYPE                 1
187*5c591343SA. Cody Schuffelen #define ARRAY_STYPE                 2
188*5c591343SA. Cody Schuffelen 
189*5c591343SA. Cody Schuffelen // The code used GET_ to get the element type and the compiler uses SET_ to initialize
190*5c591343SA. Cody Schuffelen // the value. The element type is the three bits (2:0).
191*5c591343SA. Cody Schuffelen #define GET_ELEMENT_TYPE(val)       (val & 7)
192*5c591343SA. Cody Schuffelen #define SET_ELEMENT_TYPE(val)       (val & 7)
193*5c591343SA. Cody Schuffelen 
194*5c591343SA. Cody Schuffelen // When an entry is an array or union, this references the structure entry that
195*5c591343SA. Cody Schuffelen // contains the dimension or selector value. The code then uses this number to look up
196*5c591343SA. Cody Schuffelen // the structure entry for that element to find out what it and where is it in memory.
197*5c591343SA. Cody Schuffelen // When this is not a reference, it is a simple type and it could be used as an array
198*5c591343SA. Cody Schuffelen // value or a union selector. When a simple value, this field contains the size
199*5c591343SA. Cody Schuffelen // of the associated value (ONE_BYTES, TWO_BYTES ...)
200*5c591343SA. Cody Schuffelen //
201*5c591343SA. Cody Schuffelen // The entry size/number is 6 bits (13:8).
202*5c591343SA. Cody Schuffelen #define GET_ELEMENT_NUMBER(val)     (((val) >> 8) & 0x3F)
203*5c591343SA. Cody Schuffelen #define SET_ELEMENT_NUMBER(val)     (((val) & 0x3F) << 8)
204*5c591343SA. Cody Schuffelen #define GET_ELEMENT_SIZE(val)       GET_ELEMENT_NUMBER(val)
205*5c591343SA. Cody Schuffelen #define SET_ELEMENT_SIZE(val)       SET_ELEMENT_NUMBER(val)
206*5c591343SA. Cody Schuffelen // This determines if the null flag is propagated to this type. If generate, the
207*5c591343SA. Cody Schuffelen // NULL_FLAG is SET in the index value. This flag is one bit (7)
208*5c591343SA. Cody Schuffelen #define ELEMENT_PROPAGATE           (PROPAGATE_NULL)
209*5c591343SA. Cody Schuffelen 
210*5c591343SA. Cody Schuffelen #define INDEX_MASK                  ((UINT16)NULL_MASK)
211*5c591343SA. Cody Schuffelen 
212*5c591343SA. Cody Schuffelen // This is used in all bit-field checks. These are used when a value that is checked
213*5c591343SA. Cody Schuffelen // is conditional (dependent on the compilation). For example, if AES_128 is (NO),
214*5c591343SA. Cody Schuffelen // then the bit associated with AES_128 will be 0. In some cases, the bit value is
215*5c591343SA. Cody Schuffelen // found by checking that the input is within the range of the table, and then using
216*5c591343SA. Cody Schuffelen // the (val - min) value to index the bit. This would be used when verifying that
217*5c591343SA. Cody Schuffelen // a particular algorithm is implemented. In other cases, there is a bit for each
218*5c591343SA. Cody Schuffelen // value in a table. For example, if checking the key sizes, there is a list of
219*5c591343SA. Cody Schuffelen // possible key sizes allowed by the algorithm registry and a bit field to indicate
220*5c591343SA. Cody Schuffelen // if that key size is allowed in the implementation. The smallest bit field has
221*5c591343SA. Cody Schuffelen // 32-bits because it is implemented as part of the 'values' array in structures
222*5c591343SA. Cody Schuffelen // that allow bit fields.
223*5c591343SA. Cody Schuffelen #define IS_BIT_SET32(bit, bits)                                                     \
224*5c591343SA. Cody Schuffelen                         ((((UINT32 *)bits)[bit >> 5] & (1 << (bit & 0x1F))) != 0)
225*5c591343SA. Cody Schuffelen 
226*5c591343SA. Cody Schuffelen // For a COMPOSITE_MTYPE, the qualifiers byte has an element size and count.
227*5c591343SA. Cody Schuffelen #define SET_ELEMENT_COUNT(count)    ((count & 0x1F) << 3)
228*5c591343SA. Cody Schuffelen #define GET_ELEMENT_COUNT(val)      ((val  >> 3) & 0x1F)
229*5c591343SA. Cody Schuffelen 
230*5c591343SA. Cody Schuffelen #endif // _TABLE_MARSHAL_H_
231