1 | /* $Id: tpm.h 91005 2021-08-30 16:32:25Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT, TPM common definitions (this is actually a protocol and not a format).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2021 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | #ifndef IPRT_INCLUDED_formats_tpm_h
|
---|
28 | #define IPRT_INCLUDED_formats_tpm_h
|
---|
29 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
30 | # pragma once
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #include <iprt/asm.h>
|
---|
34 | #include <iprt/cdefs.h>
|
---|
35 | #include <iprt/types.h>
|
---|
36 | #include <iprt/assertcompile.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * TPM request header (everything big endian).
|
---|
42 | */
|
---|
43 | #pragma pack(1)
|
---|
44 | typedef struct TPMREQHDR
|
---|
45 | {
|
---|
46 | /** The tag for this request. */
|
---|
47 | uint16_t u16Tag;
|
---|
48 | /** Size of the request in bytes. */
|
---|
49 | uint32_t cbReq;
|
---|
50 | /** The request ordinal to execute. */
|
---|
51 | uint32_t u32Ordinal;
|
---|
52 | } TPMREQHDR;
|
---|
53 | #pragma pack()
|
---|
54 | AssertCompileSize(TPMREQHDR, 2 + 4 + 4);
|
---|
55 | /** Pointer to a TPM request header. */
|
---|
56 | typedef TPMREQHDR *PTPMREQHDR;
|
---|
57 | /** Pointer to a const TPM request header. */
|
---|
58 | typedef const TPMREQHDR *PCTPMREQHDR;
|
---|
59 |
|
---|
60 |
|
---|
61 | /** @name TPM 1.2 request tags
|
---|
62 | * @{ */
|
---|
63 | /** Command with no authentication. */
|
---|
64 | #define TPM_TAG_RQU_COMMAND UINT16_C(0x00c1)
|
---|
65 | /** An authenticated command with one authentication handle. */
|
---|
66 | #define TPM_TAG_RQU_AUTH1_COMMAND UINT16_C(0x00c2)
|
---|
67 | /** An authenticated command with two authentication handles. */
|
---|
68 | #define TPM_TAG_RQU_AUTH2_COMMAND UINT16_C(0x00c3)
|
---|
69 | /** @} */
|
---|
70 |
|
---|
71 |
|
---|
72 | /** @name TPM 2.0 request/response tags
|
---|
73 | * @{ */
|
---|
74 | /** Command with no associated session. */
|
---|
75 | #define TPM2_ST_NO_SESSIONS UINT16_C(0x8001)
|
---|
76 | /** Command with an associated session. */
|
---|
77 | #define TPM2_ST_SESSIONS UINT16_C(0x8002)
|
---|
78 | /** @} */
|
---|
79 |
|
---|
80 |
|
---|
81 | /** @name TPM 1.2 request ordinals.
|
---|
82 | * @{ */
|
---|
83 | /** Perform a full self test. */
|
---|
84 | #define TPM_ORD_SELFTESTFULL UINT32_C(80)
|
---|
85 | /** Continue the selftest. */
|
---|
86 | #define TPM_ORD_CONTINUESELFTEST UINT32_C(83)
|
---|
87 | /** Return the test result. */
|
---|
88 | #define TPM_ORD_GETTESTRESULT UINT32_C(84)
|
---|
89 | /** Get a capability. */
|
---|
90 | #define TPM_ORD_GETCAPABILITY UINT32_C(101)
|
---|
91 | /** @} */
|
---|
92 |
|
---|
93 |
|
---|
94 | /** @name TPM 2.0 command codes.
|
---|
95 | * @{ */
|
---|
96 | /** Get a capability. */
|
---|
97 | #define TPM2_CC_GET_CAPABILITY UINT32_C(378)
|
---|
98 | /** @} */
|
---|
99 |
|
---|
100 |
|
---|
101 | /** @name Defines related to TPM_ORD_GETCAPABILITY.
|
---|
102 | * @{ */
|
---|
103 | /** Return a TPM related property. */
|
---|
104 | #define TPM_CAP_PROPERTY UINT32_C(5)
|
---|
105 |
|
---|
106 | /** Returns the size of the input buffer. */
|
---|
107 | #define TPM_CAP_PROP_INPUT_BUFFER UINT32_C(0x124)
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * TPM_ORD_GETCAPABILITY request.
|
---|
111 | */
|
---|
112 | #pragma pack(1)
|
---|
113 | typedef struct TPMREQGETCAPABILITY
|
---|
114 | {
|
---|
115 | /** Request header. */
|
---|
116 | TPMREQHDR Hdr;
|
---|
117 | /** The capability group to query. */
|
---|
118 | uint32_t u32Cap;
|
---|
119 | /** Length of the capability. */
|
---|
120 | uint32_t u32Length;
|
---|
121 | /** The sub capability to query. */
|
---|
122 | uint32_t u32SubCap;
|
---|
123 | } TPMREQGETCAPABILITY;
|
---|
124 | #pragma pack()
|
---|
125 | /** Pointer to a TPM_ORD_GETCAPABILITY request. */
|
---|
126 | typedef TPMREQGETCAPABILITY *PTPMREQGETCAPABILITY;
|
---|
127 | /** Pointer to a const TPM_ORD_GETCAPABILITY request. */
|
---|
128 | typedef const TPMREQGETCAPABILITY *PCTPMREQGETCAPABILITY;
|
---|
129 | /** @} */
|
---|
130 |
|
---|
131 |
|
---|
132 | /** @name Defines related to TPM2_CC_GET_CAPABILITY.
|
---|
133 | * @{ */
|
---|
134 | /** Return a TPM related property. */
|
---|
135 | #define TPM2_CAP_TPM_PROPERTIES UINT32_C(6)
|
---|
136 |
|
---|
137 | /** Returns the size of the input buffer. */
|
---|
138 | #define TPM2_PT_INPUT_BUFFER UINT32_C(0x10d)
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * TPM2_CC_GET_CAPABILITY request.
|
---|
142 | */
|
---|
143 | #pragma pack(1)
|
---|
144 | typedef struct TPM2REQGETCAPABILITY
|
---|
145 | {
|
---|
146 | /** Request header. */
|
---|
147 | TPMREQHDR Hdr;
|
---|
148 | /** The capability group to query. */
|
---|
149 | uint32_t u32Cap;
|
---|
150 | /** Property to query. */
|
---|
151 | uint32_t u32Property;
|
---|
152 | /** Number of values to return. */
|
---|
153 | uint32_t u32Count;
|
---|
154 | } TPM2REQGETCAPABILITY;
|
---|
155 | #pragma pack()
|
---|
156 | /** Pointer to a TPM2_CC_GET_CAPABILITY request. */
|
---|
157 | typedef TPM2REQGETCAPABILITY *PTPM2REQGETCAPABILITY;
|
---|
158 | /** Pointer to a const TPM2_CC_GET_CAPABILITY request. */
|
---|
159 | typedef const TPM2REQGETCAPABILITY *PCTPM2REQGETCAPABILITY;
|
---|
160 | /** @} */
|
---|
161 |
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * TPM response header (everything big endian).
|
---|
165 | */
|
---|
166 | #pragma pack(1)
|
---|
167 | typedef struct TPMRESPHDR
|
---|
168 | {
|
---|
169 | /** The tag for this request. */
|
---|
170 | uint16_t u16Tag;
|
---|
171 | /** Size of the response in bytes. */
|
---|
172 | uint32_t cbResp;
|
---|
173 | /** The error code for the response. */
|
---|
174 | uint32_t u32ErrCode;
|
---|
175 | } TPMRESPHDR;
|
---|
176 | #pragma pack()
|
---|
177 | AssertCompileSize(TPMRESPHDR, 2 + 4 + 4);
|
---|
178 | /** Pointer to a TPM response header. */
|
---|
179 | typedef TPMRESPHDR *PTPMRESPHDR;
|
---|
180 | /** Pointer to a const TPM response header. */
|
---|
181 | typedef const TPMRESPHDR *PCTPMRESPHDR;
|
---|
182 |
|
---|
183 |
|
---|
184 | /** @name TPM 1.2 response tags
|
---|
185 | * @{ */
|
---|
186 | /** A response from a command with no authentication. */
|
---|
187 | #define TPM_TAG_RSP_COMMAND UINT16_C(0x00c4)
|
---|
188 | /** An authenticated response with one authentication handle. */
|
---|
189 | #define TPM_TAG_RSP_AUTH1_COMMAND UINT16_C(0x00c5)
|
---|
190 | /** An authenticated response with two authentication handles. */
|
---|
191 | #define TPM_TAG_RSP_AUTH2_COMMAND UINT16_C(0x00c6)
|
---|
192 | /** @} */
|
---|
193 |
|
---|
194 |
|
---|
195 | /** @name TPM status codes.
|
---|
196 | * @{ */
|
---|
197 | /** Request executed successfully. */
|
---|
198 | #define TPM_SUCCESS UINT32_C(0)
|
---|
199 | /** Authentication failed. */
|
---|
200 | #define TPM_AUTHFAIL UINT32_C(1)
|
---|
201 | /** An index is malformed. */
|
---|
202 | #define TPM_BADINDEX UINT32_C(2)
|
---|
203 | /** A request parameter is invalid. */
|
---|
204 | #define TPM_BAD_PARAMETER UINT32_C(3)
|
---|
205 | /** The TPM failed to execute the request. */
|
---|
206 | #define TPM_FAIL UINT32_C(9)
|
---|
207 | /** @todo Extend as need arises. */
|
---|
208 | /** @} */
|
---|
209 |
|
---|
210 |
|
---|
211 | /* Some inline helpers to account for the unaligned members of the request and response headers. */
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Returns the request tag of the given TPM request header.
|
---|
215 | *
|
---|
216 | * @returns TPM request tag in bytes.
|
---|
217 | * @param pTpmReqHdr Pointer to the TPM request header.
|
---|
218 | */
|
---|
219 | DECLINLINE(uint16_t) RTTpmReqGetTag(PCTPMREQHDR pTpmReqHdr)
|
---|
220 | {
|
---|
221 | return RT_BE2H_U16(pTpmReqHdr->u16Tag);
|
---|
222 | }
|
---|
223 |
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Returns the request size of the given TPM request header.
|
---|
227 | *
|
---|
228 | * @returns TPM request size in bytes.
|
---|
229 | * @param pTpmReqHdr Pointer to the TPM request header.
|
---|
230 | */
|
---|
231 | DECLINLINE(size_t) RTTpmReqGetSz(PCTPMREQHDR pTpmReqHdr)
|
---|
232 | {
|
---|
233 | uint32_t cbReq;
|
---|
234 | memcpy(&cbReq, &pTpmReqHdr->cbReq, sizeof(pTpmReqHdr->cbReq));
|
---|
235 | return RT_BE2H_U32(cbReq);
|
---|
236 | }
|
---|
237 |
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Returns the request ordinal of the given TPM request header.
|
---|
241 | *
|
---|
242 | * @returns TPM request ordinal in bytes.
|
---|
243 | * @param pTpmReqHdr Pointer to the TPM request header.
|
---|
244 | */
|
---|
245 | DECLINLINE(uint32_t) RTTpmReqGetOrdinal(PCTPMREQHDR pTpmReqHdr)
|
---|
246 | {
|
---|
247 | uint32_t u32Ordinal;
|
---|
248 | memcpy(&u32Ordinal, &pTpmReqHdr->u32Ordinal, sizeof(pTpmReqHdr->u32Ordinal));
|
---|
249 | return RT_BE2H_U32(u32Ordinal);
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * Returns the response tag of the given TPM response header.
|
---|
255 | *
|
---|
256 | * @returns TPM request tag in bytes.
|
---|
257 | * @param pTpmRespHdr Pointer to the TPM response header.
|
---|
258 | */
|
---|
259 | DECLINLINE(uint16_t) RTTpmRespGetTag(PCTPMRESPHDR pTpmRespHdr)
|
---|
260 | {
|
---|
261 | return RT_BE2H_U16(pTpmRespHdr->u16Tag);
|
---|
262 | }
|
---|
263 |
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Returns the response size included in the given TPM response header.
|
---|
267 | *
|
---|
268 | * @returns TPM response size in bytes.
|
---|
269 | * @param pTpmRespHdr Pointer to the TPM response header.
|
---|
270 | */
|
---|
271 | DECLINLINE(size_t) RTTpmRespGetSz(PCTPMRESPHDR pTpmRespHdr)
|
---|
272 | {
|
---|
273 | uint32_t cbResp;
|
---|
274 | memcpy(&cbResp, &pTpmRespHdr->cbResp, sizeof(pTpmRespHdr->cbResp));
|
---|
275 | return RT_BE2H_U32(cbResp);
|
---|
276 | }
|
---|
277 |
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * Returns the error code of the given TPM response header.
|
---|
281 | *
|
---|
282 | * @returns TPM response error code.
|
---|
283 | * @param pTpmRespHdr Pointer to the TPM response header.
|
---|
284 | */
|
---|
285 | DECLINLINE(uint32_t) RTTpmRespGetErrCode(PCTPMRESPHDR pTpmRespHdr)
|
---|
286 | {
|
---|
287 | uint32_t u32ErrCode;
|
---|
288 | memcpy(&u32ErrCode, &pTpmRespHdr->u32ErrCode, sizeof(pTpmRespHdr->u32ErrCode));
|
---|
289 | return RT_BE2H_U32(u32ErrCode);
|
---|
290 | }
|
---|
291 |
|
---|
292 | #endif /* !IPRT_INCLUDED_formats_tpm_h */
|
---|
293 |
|
---|