forked from microsoft/TSS.MSR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTpmHelper.cs
362 lines (324 loc) · 14 KB
/
TpmHelper.cs
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See the LICENSE file in the project root for full license information.
*/
using System;
using System.Reflection;
using System.Linq;
using System.Threading;
using System.Diagnostics;
using Tpm2Lib;
namespace Tpm2Tester
{
public static class TpmHelper
{
public static ushort MaxOaepMsgSize(ushort keyBitLen, TpmAlgId hashAlg)
{
int keySize = keyBitLen / 8;
int maxMessageSize = keySize - 2 * TpmHash.DigestSize(hashAlg) - 2;
return (ushort)(maxMessageSize > keySize || maxMessageSize < 0 ? 0 : maxMessageSize);
}
// Returns reference to the byte buffer containing unique value of inPub.
// Note that in case of ECC keys, the returned buffer references only the
// first half of the unique value (i.e. x-coordinate of the ECC point).
public static FieldInfo GetUniqueBuffer(TpmPublic pub, out byte[] buf)
{
var keyType = pub.parameters.GetUnionSelector();
buf = null;
switch (keyType)
{
case TpmAlgId.Rsa:
buf = (pub.unique as Tpm2bPublicKeyRsa).buffer;
return pub.unique.GetType().GetField("buffer");
case TpmAlgId.Ecc:
buf = (pub.unique as EccPoint).x;
return pub.unique.GetType().GetField("x");
case TpmAlgId.Symcipher:
case TpmAlgId.Keyedhash:
buf = (pub.unique as Tpm2bDigest).buffer;
return pub.unique.GetType().GetField("buffer");
}
throw new NotImplementedException(
"GetUniqueBuffer: Unknown TpmPublic type " + keyType);
}
public static byte[] GetUniqueBuffer(TpmPublic pub)
{
byte[] buf;
if (pub.parameters.GetUnionSelector() == TpmAlgId.Ecc)
{
buf = (pub.unique as EccPoint).GetTpmRepresentation();
}
else
GetUniqueBuffer(pub, out buf);
return buf;
}
public static IAsymSchemeUnion GetScheme(TpmPublic pub)
{
return (pub.type == TpmAlgId.Keyedhash
? (IAsymSchemeUnion)(pub.parameters as KeyedhashParms).scheme
: pub.type == TpmAlgId.Rsa
? (IAsymSchemeUnion)(pub.parameters as RsaParms).scheme
: (IAsymSchemeUnion)(pub.parameters as EccParms).scheme);
}
public static TpmAlgId GetSchemeHash(IAsymSchemeUnion scheme)
{
if (scheme == null || scheme is NullUnion || scheme is Empty)
return TpmAlgId.Null;
var daaScheme = scheme as SchemeEcdaa;
return daaScheme != null ? daaScheme.hashAlg
: (scheme as SchemeHash).hashAlg;
}
public static TpmAlgId GetSchemeHash(ISigSchemeUnion scheme)
{
return GetSchemeHash(scheme as IAsymSchemeUnion);
}
public static TpmAlgId GetSchemeHash(TpmPublic pub)
{
TpmAlgId schemeAlg = GetSchemeHash(GetScheme(pub));
return schemeAlg == TpmAlgId.Null ? pub.nameAlg : schemeAlg;
}
public static TpmHandle[] GetAllLoadedEntities(Tpm2 tpm)
{
TpmHandle[] h0 = GetLoadedEntities(tpm, Ht.Transient);
TpmHandle[] h1 = GetLoadedEntities(tpm, Ht.LoadedSession);
TpmHandle[] h = new TpmHandle[h0.Length + h1.Length];
Array.Copy(h0, h, h0.Length);
Array.Copy(h1, 0, h, h0.Length, h1.Length);
return h;
}
public static TpmHandle[] GetLoadedEntities(Tpm2 tpm, Ht rangeToQuery)
{
uint maxHandles = UInt32.MaxValue;
ICapabilitiesUnion h = null;
byte moreData = tpm.GetCapability(Cap.Handles, ((uint)rangeToQuery) << 24,
maxHandles, out h);
if (moreData != 0)
{
throw new NotImplementedException(
"GetLoadedEntities: Too much data returned");
}
if (h.GetType() != typeof(HandleArray))
{
throw new Exception(
"GetLoadedEntities: Incorrect capability type requested");
}
return (h as HandleArray).handle;
}
public static bool FlushEntities(Tpm2 tpm, TpmHandle[] entities)
{
bool succeeded = true;
foreach (TpmHandle h in entities)
{
Debug.Assert(h.GetType() != Ht.NvIndex);
tpm._AllowErrors().FlushContext(h);
succeeded = succeeded && tpm._LastCommandSucceeded();
}
return succeeded;
}
// Makes sure that the size of data transferred in a single write operation
// does not exceed the limit on a command parameter size.
public static void SafeNvWrite(Tpm2 tpm, ushort maxNvOpSize,
TpmHandle nvHandle, byte[] contents)
{
for (ushort offset = 0; offset < contents.Length; offset += maxNvOpSize)
{
var chunkSize = contents.Length - offset < maxNvOpSize ?
contents.Length - offset : maxNvOpSize;
var data = Globs.CopyData(contents, offset, chunkSize);
tpm.NvWrite(nvHandle, nvHandle, data, offset);
}
}
public static byte[] SafeNvRead(Tpm2 tpm, ushort maxNvOpSize,
TpmHandle nvHandle, ushort size, ushort nvOffset = 0)
{
byte[] contents = new byte[size];
for (ushort offset = 0; offset < size; offset += maxNvOpSize,
nvOffset += maxNvOpSize)
{
var chunkSize = size - offset < maxNvOpSize ? size - offset : maxNvOpSize;
var chunk = tpm.NvRead(nvHandle, nvHandle, (ushort)chunkSize, nvOffset);
Array.Copy(chunk, 0, contents, offset, chunkSize);
}
return contents;
}
public static TpmPublic ReadPublic(Tpm2 tpm, TpmHandle h)
{
byte[] name, qualName;
return tpm.ReadPublic(h, out name, out qualName);
}
public static NvPublic NvReadPublic(Tpm2 tpm, TpmHandle h)
{
byte[] name;
return tpm.NvReadPublic(h, out name);
}
public static Tpm2bDigest[] SafePcrRead(Tpm2 tpm, PcrSelection sel)
{
PcrSelection[] selOut;
var selIn = new PcrSelection[] { sel.Copy() };
var pcrValues = new Tpm2bDigest[0];
do
{
Tpm2bDigest[] vals;
tpm.PcrRead(selIn, out selOut, out vals);
pcrValues = pcrValues.Concat(vals).ToArray();
Debug.Assert(selOut.Length == 1);
// The first part of the while condition is used to by pass not
// implemented PCRs
} while (!Globs.IsZeroBuffer(selOut[0].pcrSelect)
&& selIn[0].Clear(selOut[0]));
Debug.Assert(selIn[0].GetSelectedPcrs().Length == 0);
Debug.Assert(sel.GetSelectedPcrs().Length == pcrValues.Length);
return pcrValues;
}
public static Tpm2bDigest[] SafePcrRead(Tpm2 tpm, PcrSelection[] sel)
{
if (sel.Length == 0)
{
return new Tpm2bDigest[0];
}
var pcrValues = SafePcrRead(tpm, sel[0]);
for (int i = 1; i < sel.Length; ++i)
{
pcrValues = pcrValues.Concat(SafePcrRead(tpm, sel[i])).ToArray();
}
return pcrValues;
}
public static PcrValueCollection SafePcrReadAsColl(Tpm2 tpm, PcrSelection sel)
{
return new PcrValueCollection(new PcrSelection[] { sel }, SafePcrRead(tpm, sel));
}
public static void PowerCycle(Tpm2 tpm, Su shutdownMode, Su startupMode)
{
tpm._AllowErrors()
.Shutdown(shutdownMode);
tpm._GetUnderlyingDevice().PowerCycle();
tpm.Startup(startupMode);
}
public static bool AreAnySlotsFull(Tpm2 tpm)
{
var tagActiveSession = TpmHelpers.GetEnumerator<Ht>("ActiveSession", "SavedSession");
return TpmHelper.GetLoadedEntities(tpm, Ht.Transient).Length != 0 ||
TpmHelper.GetLoadedEntities(tpm, Ht.LoadedSession).Length != 0 ||
TpmHelper.GetLoadedEntities(tpm, Ht.LoadedSession).Length != 0;
}
public static TpmPrivate GetPlaintextPrivate(Tpm2 tpm, TpmHandle key, PolicyTree policy)
{
AuthSession sess = tpm.StartAuthSessionEx(TpmSe.Policy, policy.HashAlg);
sess.RunPolicy(tpm, policy);
TpmPrivate privPlain = null;
byte[] symSeed;
tpm[sess]._ExpectResponses(TpmRc.Success, TpmRc.Attributes)
.Duplicate(key, TpmRh.Null, null, new SymDefObject(),
out privPlain, out symSeed);
Debug.Assert(!tpm._LastCommandSucceeded() || symSeed.Length == 0);
tpm.FlushContext(sess);
return Globs.IsEmpty(privPlain?.buffer) ? null : privPlain;
}
public static SchemeEcdaa PrepareEcdaaScheme(Tpm2 tpm, TpmHandle signKey,
ISigSchemeUnion scheme)
{
var schemeEcdaa = scheme as SchemeEcdaa;
if (schemeEcdaa != null)
{
byte[] name, qualName;
var keyPub = tpm.ReadPublic(signKey, out name, out qualName);
ushort counter = 0;
EccPoint l, E;
EccPoint PP = keyPub.unique as EccPoint;
tpm.Commit(signKey, PP, null, null, out l, out E, out counter);
schemeEcdaa.count = counter;
}
return schemeEcdaa;
}
// Returns the ratio of a TPM clock second to one system time second
public static double MeasureClockRate(Tpm2 tpm)
{
const int MaxIter = 20;
int NumSamplesPerIter = 5;
int N = NumSamplesPerIter,
L = 0;
double[] tpmTimes = null;
double[] sysTimes = null;
double[] newTpmTimes = new double[N];
double[] newSysTimes = new double[N];
int iter = 0;
double stdDevSys = 0,
meanSys = 0,
stdDevTpm = 0,
meanTpm = 0;
int n = 0;
double sysTime = 0, tpmTime = 0;
do
{
tpmTimes = newTpmTimes;
sysTimes = newSysTimes;
TimeInfo tpmStart = null;
DateTime sysStart = DateTime.MinValue;
for (int i = L; i <= N; i++)
{
TimeInfo tpmStop = tpm.ReadClock();
DateTime sysStop = DateTime.Now;
if (tpmStart != null)
{
tpmTimes[i - 1] = tpmStop.time - tpmStart.time;
sysTimes[i - 1] = (sysStop - sysStart).TotalMilliseconds;
}
tpmStart = tpmStop;
sysStart = sysStop;
Thread.Sleep(600);
}
// Eliminate outliers that may be caused by the current thread having
// been preempted at a "wrong" point causing the measured rate distortion.
meanSys = meanTpm = stdDevSys = stdDevTpm = sysTime = tpmTime = 0;
for (int i = 0; i < N; i++)
{
meanSys += sysTimes[i];
meanTpm += tpmTimes[i];
}
meanSys /= N;
meanTpm /= N;
for (int i = 0; i < N; i++)
{
double d = (sysTimes[i] - meanSys);
stdDevSys += d * d;
d = (tpmTimes[i] - meanTpm);
stdDevTpm += d * d;
}
stdDevSys = Math.Sqrt(stdDevSys / N);
stdDevTpm = Math.Sqrt(stdDevTpm / N);
bool imprecise = stdDevSys / meanSys > 0.03 || stdDevTpm / meanTpm > 0.03;
if (imprecise)
{
newTpmTimes = new double[N + NumSamplesPerIter];
newSysTimes = new double[N + NumSamplesPerIter];
}
n = 0;
for (int i = 0; i < N; i++)
{
if (Math.Abs(sysTimes[i] - meanSys) < 2 * stdDevSys
&& Math.Abs(tpmTimes[i] - meanTpm) < 2 * stdDevTpm)
{
sysTime += sysTimes[i];
tpmTime += tpmTimes[i];
if (imprecise)
{
newSysTimes[n] = sysTimes[i];
newTpmTimes[n] = tpmTimes[i];
}
++n;
}
//else Console.Write("Dropped[{0}] = {1} ", i, sysTimes[i]);
}
if (!imprecise && n > 2)
break;
L = n;
N = L + NumSamplesPerIter;
} while (++iter < MaxIter);
if (iter == MaxIter)
throw new Exception("The system is likely overloaded. Cannot do reliable time measurements.");
//Console.WriteLine("ITER {0}, MEAN {1:F0}->{2:F0}, SD {3:F1}; Good {4}; RATE {5:F2}",
// iter+1, meanSys, sysTime / n, stdDevSys, n, tpmTime / sysTime);
return tpmTime / sysTime;
} // MeasureClockRate()
} // class TpmConfig
}