-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtpmoswin.cpp
152 lines (127 loc) · 2.99 KB
/
tpmoswin.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*++
Copyright (c) Alex Ionescu. All rights reserved.
Module Name:
tpmoswin.cpp
Abstract:
This module handles the Windows-specific functionality for accessing the
TPM2.0 interface of the operating system. It also provides the compiler
intrinsics for endian swapping.
Author:
Alex Ionescu (@aionescu) 11-Jun-2020 - Initial version
Environment:
Windows 8 and above, kernel mode (Tbs.sys) or user mode (Tbs.dll).
--*/
#include <stdint.h>
#include <Windows.h>
#include <tbs.h>
#include <intrin.h>
uint16_t
OsSwap16 (
_In_ uint16_t Input
)
{
return _byteswap_ushort(Input);
}
uint32_t
OsSwap32 (
_In_ uint32_t Input
)
{
return _byteswap_ulong(Input);
}
uint64_t
OsSwap64 (
_In_ uint64_t Input
)
{
return _byteswap_uint64(Input);
}
bool
TpmOsIssueCommand (
_In_ uintptr_t TpmHandle,
_In_ uint8_t* In,
_In_ uint32_t InLength,
_In_ uint8_t* Out,
_In_ uint32_t OutLength,
_Out_opt_ uint32_t* OsResult
)
{
uint32_t resultLength;
TBS_RESULT tbsResult;
//
// Use the TBSI stack to send the command to the TPM
//
resultLength = OutLength;
tbsResult = Tbsip_Submit_Command(reinterpret_cast<TBS_HCONTEXT>(TpmHandle),
TBS_COMMAND_LOCALITY_ZERO,
TBS_COMMAND_PRIORITY_NORMAL,
In,
InLength,
Out,
&resultLength);
if (tbsResult != TBS_SUCCESS)
{
//
// Clear the result on failure
//
resultLength = 0;
}
//
// Return the OS result if needed
//
if (OsResult != nullptr)
{
*OsResult = tbsResult;
}
//
// Return a boolean if the TPM command was issued. The actual TPM may still
// return an error code as part of the respone header.
//
return (tbsResult == TBS_SUCCESS);
}
bool
TpmOsOpen (
_Out_ uintptr_t* TpmHandle
)
{
TBS_CONTEXT_PARAMS2 pContextParams;
TBS_HCONTEXT hContext;
TBS_RESULT tbsResult;
bool result;
//
// Initialize for failure
//
*TpmHandle = 0;
//
// Request TPM 2.0 Access
//
pContextParams.asUINT32 = 0;
pContextParams.version = TBS_CONTEXT_VERSION_TWO;
pContextParams.includeTpm20 = 1;
tbsResult = Tbsi_Context_Create(reinterpret_cast<PCTBS_CONTEXT_PARAMS>(&pContextParams),
&hContext);
if (tbsResult != TBS_SUCCESS)
{
result = false;
goto Exit;
}
//
// Return a handle that can be used for further commands
//
*TpmHandle = reinterpret_cast<uintptr_t>(hContext);
result = true;
Exit:
return result;
}
bool
TpmOsClose (
_In_ uintptr_t TpmHandle
)
{
TBS_RESULT tbsResult;
//
// Close the context handle
//
tbsResult = Tbsip_Context_Close(reinterpret_cast<TBS_HCONTEXT>(TpmHandle));
return (tbsResult == TBS_SUCCESS);
}