-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtpmcmd.hpp
132 lines (109 loc) · 3.78 KB
/
tpmcmd.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*++
Copyright (c) Alex Ionescu. All rights reserved.
Module Name:
tpmcmd.hpp
Abstract:
This header provides helper macros that simplify the construction of some
of the variable-sized TPM2.0 command and reply structures that are needed,
especially when using password authentication.
Author:
Alex Ionescu (@aionescu) 11-Jun-2020 - Initial version
Environment:
Portable to any environment.
--*/
#pragma once
#include <type_traits>
//
// This macro calculates the size of a variable data command made up of
// A static-size header (including TPM_CMD_HEADER)
// A dynamic-size data blob
// A dynamic-size authorization session
// A static-size footer
//
#define TpmVariableCmdSize(header, authSize, data, size, footer) \
((offsetof(std::remove_reference<decltype(*header)>::type, \
AuthSession.Password)) + \
(authSize) + \
(offsetof(std::remove_reference<decltype(*data)>::type, \
Data)) + \
(size) + \
(sizeof(*footer)))
//
// This macro calculates the size of a fixed data command made up of
// A static-size header (including TPM_CMD_HEADER)
// A dynamic-size authorization session
// A static-size footer
//
#define TpmFixedCmdSize(header, authSize, footer) \
((offsetof(std::remove_reference<decltype(*header)>::type, \
AuthSession.Password)) + \
(authSize) + \
(sizeof(*footer)))
//
// This macro calculates the size of an empty data command made up of
// A static-size header (including TPM_CMD_HEADER)
// A dynamic-size authorization session
//
#define TpmEmptyCmdSize(header, authSize) \
((offsetof(std::remove_reference<decltype(*header)>::type, \
AuthSession.Password)) + \
(authSize))
//
// This macro allocates a TPM 2.0 Command Buffer on the stack
//
#define TpmpAllocateCommand(header, commandSize) \
reinterpret_cast<decltype(header)>(alloca(commandSize));
//
// This macro calculates the size of a fixed data response made up of
// A static-size header (including TPM_REPLY_HEADER)
// The 32-bit parameter size
// An empty authorization response
//
#define TpmFixedResponseSize(x) \
(sizeof(*x))
//
// This macro calculates the size of a variable data response made up of
// A static-size header (including TPM_REPLY_HEADER)
// The 32-bit parameter size
// A dynamic-size data blob
// An empty authorization response
//
#define TpmVariableResponseSize(x, y) \
((offsetof(std::remove_reference<decltype(*x)>::type, \
Data)) + \
(y) + \
(sizeof(TPMS_AUTH_RESPONSE_NO_NONCE)))
//
// This macro allocates a TPM 2.0 Response Buffer on the stack
//
#define TpmpAllocateResponse(x, y) \
reinterpret_cast<decltype(x)>(alloca(y));
//
// This macro returns a TPM 2.0 Result Code from a Response Buffer
//
#define TpmReadResponseCode(response) \
static_cast<TPM_RC>(OsSwap32((response)->Header.ResponseCode))
//
// Internal Routines that require OS Support
//
uint16_t
OsSwap16 (
uint16_t Input
);
uint32_t
OsSwap32 (
uint32_t Input
);
uint64_t
OsSwap64 (
uint64_t Input
);
bool
TpmOsIssueCommand (
uintptr_t TpmHandle,
uint8_t* In,
uint32_t InLength,
uint8_t* Out,
uint32_t OutLength,
uint32_t* OsResult
);