-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtpm.cpp
More file actions
500 lines (416 loc) · 13.8 KB
/
tpm.cpp
File metadata and controls
500 lines (416 loc) · 13.8 KB
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include <assert.h>
#include <windows.h>
#include <bcrypt.h>
#include <ncrypt.h>
#include <iostream>
#ifndef NT_SUCCESS
#define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)
#endif
#pragma comment(lib, "bcrypt.lib")
#pragma comment(lib, "ncrypt.lib")
bool bTPMSupport=false;
//#define KeyProviderName MS_PLATFORM_CRYPTO_PROVIDER
#define KEY_NAME L"ECDH Key"
void ReportError(SECURITY_STATUS dwErrCode)
{
printf("Error: 0x%08x\n", dwErrCode);
printf("0x%08x: NTE_BAD_FLAGS\n", NTE_BAD_FLAGS);
printf("0x%08x: NTE_NO_MEMORY\n", NTE_NO_MEMORY);
printf("0x%08x: NTE_EXISTS\n", NTE_EXISTS);
printf("0x%08x: NTE_SILENT_CONTEXT\n", NTE_SILENT_CONTEXT);
printf("0x%08x: NTE_INVALID_HANDLE\n", NTE_INVALID_HANDLE);
printf("0x%08x: NTE_INVALID_PARAMETER\n", NTE_INVALID_PARAMETER);
printf("0x%08x: NTE_NOT_SUPPORTED\n", NTE_NOT_SUPPORTED);
printf("0x%08x: NTE_NO_MORE_ITEMS\n", NTE_NO_MORE_ITEMS);
}
void EnumProviders()
{
NTSTATUS status;
ULONG cbBuffer = 0;
PCRYPT_PROVIDERS pBuffer = NULL;
//
// Get the providers, letting the BCryptEnumRegisteredProviders
// function allocate the memory.
//
status = BCryptEnumRegisteredProviders(&cbBuffer, &pBuffer);
if (NT_SUCCESS(status))
{
if (pBuffer != NULL)
{
// Enumerate the providers.
for (ULONG i = 0; i < pBuffer->cProviders; i++)
{
printf("%S\n", pBuffer->rgpszProviders[i]);
if (wcscmp(pBuffer->rgpszProviders[i], MS_PLATFORM_CRYPTO_PROVIDER)==0) {
bTPMSupport = true;
}
}
}
}
else
{
ReportError(status);
}
if (NULL != pBuffer)
{
/*
Free the memory allocated by the
BCryptEnumRegisteredProviders function.
*/
BCryptFreeBuffer(pBuffer);
}
}
NCRYPT_PROV_HANDLE GetKeyStorageProvider(LPCWSTR sProvider)
{
SECURITY_STATUS status;
NCRYPT_PROV_HANDLE hProvider;
status = NCryptOpenStorageProvider(&hProvider, sProvider, 0);
if (status != ERROR_SUCCESS) {
printf("Fail on NCryptOpenStorageProvider!\n");
ReportError(status);
exit(-1);
}
//printf("Key Storage Provider=0x%08x\n", hProvider);
return hProvider;
}
void dump_key(PBYTE pData, DWORD len)
{
DWORD i;
printf("Key:\n");
printf("length=%d\n", len);
for (i = 0; i < len; i++) {
printf("0x%02x ", pData[i]);
}
printf("\n");
}
void write_key_to_file(const char *pFileName, PBYTE PubBlob, DWORD PubBlobLength)
{
FILE *fp;
errno_t err;
size_t size;
err = fopen_s(&fp, pFileName, "w");
assert(fp != NULL);
size = fwrite(PubBlob, PubBlobLength, 1, fp);
assert(size == 1);
fclose(fp);
}
void read_key_from_file(const char* pFileName, PBYTE *pByte, DWORD *pLength)
{
FILE* fp;
errno_t err;
size_t size;
struct stat st;
stat(pFileName, &st);
*pLength = st.st_size;
printf("length=%d\n", *pLength);
*pByte = (PBYTE)malloc(*pLength);
err = fopen_s(&fp, pFileName, "r");
assert(fp != NULL);
size = fread(*pByte, *pLength, 1, fp);
assert(size == 1);
fclose(fp);
}
void NExportKey(NCRYPT_KEY_HANDLE key_handle, PBYTE *pPubBlob, DWORD *pPubBlobLength)
{
SECURITY_STATUS status;
*pPubBlobLength = 0;
*pPubBlob = NULL;
// Export Public Key
status = NCryptExportKey(key_handle, NULL, BCRYPT_ECCPUBLIC_BLOB, NULL, NULL, 0, pPubBlobLength, 0);
if (status != ERROR_SUCCESS) {
printf("NCryptExportKey failed!\n");
ReportError(status);
goto cleanup;
}
*pPubBlob = (PBYTE)HeapAlloc(GetProcessHeap(), 0, *pPubBlobLength);
if (NULL == pPubBlob)
{
status = NTE_NO_MEMORY;
ReportError(status);
goto cleanup;
}
status = NCryptExportKey(key_handle, NULL, BCRYPT_ECCPUBLIC_BLOB, NULL, *pPubBlob, *pPubBlobLength, pPubBlobLength, 0);
if (status != ERROR_SUCCESS) {
printf("NCryptExportKey failed, line=%d!\n", __LINE__);
ReportError(status);
goto cleanup;
}
printf("Key exported\n");
cleanup:
0;
}
void CreateKey(NCRYPT_PROV_HANDLE hProvider)
{
SECURITY_STATUS status;
NCRYPT_KEY_HANDLE key_handle;
DWORD KeyPolicy = 0;
// delete existing key
status = NCryptOpenKey(hProvider, &key_handle, KEY_NAME, 0, NCRYPT_MACHINE_KEY_FLAG);
if (status == ERROR_SUCCESS) {
printf("Key exists\n");
status = NCryptDeleteKey(key_handle, 0);
if (status != ERROR_SUCCESS) {
ReportError(status);
goto cleanup;
}
key_handle = NULL;
printf("Existing key deleted\n");
}
// generate an ECDH key
status = NCryptCreatePersistedKey(hProvider, &key_handle, NCRYPT_ECDH_P256_ALGORITHM, KEY_NAME, 0, NCRYPT_MACHINE_KEY_FLAG);
if (status == ERROR_SUCCESS) {
//printf("CreateKey with handle: 0x%08x\n", key_handle);
}
else {
printf("NCryptCreatePersistedKey failed!\n");
ReportError(status);
}
#if 0
// Make the key exportable
KeyPolicy = NCRYPT_ALLOW_EXPORT_FLAG;
status = NCryptSetProperty(key_handle, NCRYPT_EXPORT_POLICY_PROPERTY, (PBYTE)&KeyPolicy, sizeof(KeyPolicy), NCRYPT_PERSIST_FLAG);
if (status != ERROR_SUCCESS) {
printf("NCryptSetProperty failed!\n");
ReportError(status);
goto cleanup;
}
#endif
// Finalize Key
status = NCryptFinalizeKey(key_handle, 0);
if (status != ERROR_SUCCESS) {
printf("NCryptFinalizeKey failed!\n");
ReportError(status);
goto cleanup;
}
printf("Key finalized\n");
cleanup:
if (key_handle != NULL) {
NCryptFreeObject(key_handle);
}
}
void EnumKeys(NCRYPT_PROV_HANDLE hProvider)
{
NCryptKeyName *pKeyName = NULL;
PVOID pEnumState = NULL;
SECURITY_STATUS status;
status = NCryptEnumKeys(hProvider, NULL, &pKeyName, &pEnumState, NCRYPT_MACHINE_KEY_FLAG);
if (status == ERROR_SUCCESS) {
printf("Key Name: %S\n", pKeyName->pszName);
}
else {
printf("NCryptEnumKeys failed!\n");
ReportError(status);
}
if(pKeyName!=NULL) {
NCryptFreeBuffer(pKeyName);
}
}
NCRYPT_KEY_HANDLE target_open_key(NCRYPT_PROV_HANDLE hProvider)
{
SECURITY_STATUS status;
NCRYPT_KEY_HANDLE key_handle = NULL;
status = NCryptOpenKey(hProvider, &key_handle, KEY_NAME, 0, NCRYPT_MACHINE_KEY_FLAG);
if (status != ERROR_SUCCESS) {
printf("NCryptOpenKey failed, line=%d!\n", __LINE__);
ReportError(status);
goto cleanup;
}
cleanup:
0;
return key_handle;
}
void target_create_key(NCRYPT_PROV_HANDLE hProvider)
{
CreateKey(hProvider);
}
void target_export_public_key(NCRYPT_PROV_HANDLE hProvider, NCRYPT_KEY_HANDLE key_handle)
{
PBYTE PubBlob;
DWORD PubBlobLength;
EnumKeys(hProvider);
NExportKey(key_handle, &PubBlob, &PubBlobLength);
dump_key(PubBlob, PubBlobLength);
write_key_to_file("target.key", PubBlob, PubBlobLength);
}
void target_generate_shared_secret(NCRYPT_PROV_HANDLE hProvider)
{
DWORD BlobLenMaster = 0;
PBYTE BlobMaster = NULL;
SECURITY_STATUS status;
NCRYPT_KEY_HANDLE key_handle_master;
status = NCryptImportKey(hProvider, NULL, BCRYPT_ECCPUBLIC_BLOB, NULL, &key_handle_master, BlobMaster, BlobLenMaster, 0);
if (status != ERROR_SUCCESS) {
printf("NCryptImportKey failed, line=%d!\n", __LINE__);
ReportError(status);
goto cleanup;
}
cleanup:
0;
}
void BExportKey(BCRYPT_KEY_HANDLE key_handle, PBYTE* pPubBlob, DWORD* pPubBlobLength)
{
SECURITY_STATUS status;
// Export Public Key
status = BCryptExportKey(key_handle, NULL, BCRYPT_ECCPUBLIC_BLOB, NULL, 0, pPubBlobLength, 0);
if (status != ERROR_SUCCESS) {
printf("BCryptExportKey failed!\n");
ReportError(status);
goto cleanup;
}
*pPubBlob = (PBYTE)HeapAlloc(GetProcessHeap(), 0, *pPubBlobLength);
if (NULL == *pPubBlob)
{
status = NTE_NO_MEMORY;
ReportError(status);
goto cleanup;
}
status = BCryptExportKey(key_handle, NULL, BCRYPT_ECCPUBLIC_BLOB, *pPubBlob, *pPubBlobLength, pPubBlobLength, 0);
if (status != ERROR_SUCCESS) {
printf("BCryptExportKey failed, line=%d!\n", __LINE__);
ReportError(status);
goto cleanup;
}
printf("Key exported\n");
cleanup:
0;
}
void master_export_public_key()
{
SECURITY_STATUS status;
BCRYPT_ALG_HANDLE ExchAlgHandle = NULL;
BCRYPT_KEY_HANDLE key_handle;
PBYTE PubBlob;
DWORD PubBlobLength;
status = BCryptOpenAlgorithmProvider(&ExchAlgHandle, BCRYPT_ECDH_P256_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
if (status != ERROR_SUCCESS) {
printf("BCryptOpenAlgorithmProvider failed, line=%d!\n", __LINE__);
ReportError(status);
goto cleanup;
}
status = BCryptGenerateKeyPair(ExchAlgHandle, &key_handle, 256, 0);
if (status != ERROR_SUCCESS) {
printf("BCryptGenerateKeyPair failed, line=%d!\n", __LINE__);
ReportError(status);
goto cleanup;
}
status = BCryptFinalizeKeyPair(key_handle, 0);
if (status != ERROR_SUCCESS) {
printf("BCryptFinalizeKeyPair failed, line=%d!\n", __LINE__);
ReportError(status);
goto cleanup;
}
BExportKey(key_handle, &PubBlob, &PubBlobLength);
dump_key(PubBlob, PubBlobLength);
write_key_to_file("master.key", PubBlob, PubBlobLength);
cleanup:
0;
}
void master_derive_shared_secret()
{
}
static const BYTE SecretPrependArray[] =
{
0x12, 0x34, 0x56
};
static const BYTE SecretAppendArray[] =
{
0xab, 0xcd, 0xef
};
void target_derive_shared_secret(NCRYPT_PROV_HANDLE hProvider, NCRYPT_KEY_HANDLE key_handle_target)
{
SECURITY_STATUS status;
NCRYPT_KEY_HANDLE key_handle_master_public;
PBYTE PubBlob = NULL;
DWORD PubBlobLength;
PBYTE AgreedSecret = NULL;
DWORD AgreedSecretLength;
NCRYPT_SECRET_HANDLE AgreedSecretHandle = NULL;
const DWORD BufferLength = 3;
BCryptBuffer BufferArray[BufferLength] = { 0 };
BCryptBufferDesc ParameterList = { 0 };
//
// target import master's public key
//
read_key_from_file("master.key", &PubBlob, &PubBlobLength);
dump_key(PubBlob, PubBlobLength);
status = NCryptImportKey(hProvider, NULL, BCRYPT_ECCPUBLIC_BLOB, NULL, &key_handle_master_public, PubBlob, PubBlobLength, 0);
if (status != ERROR_SUCCESS) {
printf("NCryptImportKey failed, line=%d!\n", __LINE__);
ReportError(status);
goto cleanup;
}
//
// target generates the agreed secret
//
status = NCryptSecretAgreement(key_handle_target, key_handle_master_public, &AgreedSecretHandle, 0);
if (status != ERROR_SUCCESS) {
printf("NCryptSecretAgreement failed, line=%d!\n", __LINE__);
ReportError(status);
goto cleanup;
}
//
// Build KDF parameter list
//
//specify hash algorithm
BufferArray[0].BufferType = KDF_HASH_ALGORITHM;
BufferArray[0].cbBuffer = (DWORD)((wcslen(BCRYPT_SHA256_ALGORITHM) + 1) * sizeof(WCHAR));
BufferArray[0].pvBuffer = (PVOID)BCRYPT_SHA256_ALGORITHM;
//specify secret to append
BufferArray[1].BufferType = KDF_SECRET_APPEND;
BufferArray[1].cbBuffer = sizeof(SecretAppendArray);
BufferArray[1].pvBuffer = (PVOID)SecretAppendArray;
//specify secret to prepend
BufferArray[2].BufferType = KDF_SECRET_PREPEND;
BufferArray[2].cbBuffer = sizeof(SecretPrependArray);
BufferArray[2].pvBuffer = (PVOID)SecretPrependArray;
ParameterList.cBuffers = 3;
ParameterList.pBuffers = BufferArray;
ParameterList.ulVersion = BCRYPTBUFFER_VERSION;
status = NCryptDeriveKey(AgreedSecretHandle, BCRYPT_KDF_HASH , &ParameterList, NULL, 0, &AgreedSecretLength, KDF_USE_SECRET_AS_HMAC_KEY_FLAG);
if (status != ERROR_SUCCESS) {
printf("NCryptDeriveKey failed, line=%d!\n", __LINE__);
ReportError(status);
goto cleanup;
}
AgreedSecret = (PBYTE)HeapAlloc(GetProcessHeap(), 0, AgreedSecretLength);
if (NULL == AgreedSecret)
{
status = NTE_NO_MEMORY;
ReportError(status);
goto cleanup;
}
status = NCryptDeriveKey(AgreedSecretHandle, BCRYPT_KDF_HMAC, &ParameterList, AgreedSecret, AgreedSecretLength, &AgreedSecretLength, KDF_USE_SECRET_AS_HMAC_KEY_FLAG);
if (NULL == AgreedSecret)
{
printf("NCryptDeriveKey failed, line=%d!\n", __LINE__);
ReportError(status);
goto cleanup;
}
dump_key(AgreedSecret, AgreedSecretLength);
cleanup:
if (PubBlob!= NULL) {
free(PubBlob);
}
}
int main()
{
NCRYPT_PROV_HANDLE hTPMProvider = NULL;
NCRYPT_PROV_HANDLE hMSProvider = NULL;
NCRYPT_KEY_HANDLE key_handle_target = NULL;
EnumProviders();
if (bTPMSupport == true) {
printf("Have TPM Support!\n");
} else {
printf("No TPM Support!\n");
exit(-1);
}
// MS_PLATFORM_CRYPTO_PROVIDER: Identifies the TPM key storage provider
// MS_KEY_STORAGE_PROVIDER: Identifies the software key storage provider
hTPMProvider = GetKeyStorageProvider(MS_PLATFORM_CRYPTO_PROVIDER);
hMSProvider = GetKeyStorageProvider(MS_KEY_STORAGE_PROVIDER);
key_handle_target = target_open_key(hTPMProvider);
//target_export_public_key();
//master_export_public_key();
//master_derive_shared_secret();
target_derive_shared_secret(hTPMProvider, key_handle_target);
}