-
Notifications
You must be signed in to change notification settings - Fork 55
/
tpm2.c
6232 lines (4964 loc) · 190 KB
/
tpm2.c
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* tpm2.c
*
* Copyright (C) 2006-2022 wolfSSL Inc.
*
* This file is part of wolfTPM.
*
* wolfTPM is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfTPM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include <wolftpm/tpm2.h>
#include <wolftpm/tpm2_packet.h>
#include <wolftpm/tpm2_tis.h>
#include <wolftpm/tpm2_linux.h>
#include <wolftpm/tpm2_swtpm.h>
#include <wolftpm/tpm2_winapi.h>
#include <wolftpm/tpm2_param_enc.h>
#include <hal/tpm_io.h>
/******************************************************************************/
/* --- Local Variables -- */
/******************************************************************************/
static THREAD_LS_T TPM2_CTX* gActiveTPM;
#ifndef WOLFTPM2_NO_WOLFCRYPT
static volatile int gWolfCryptRefCount = 0;
#endif
#ifdef WOLFTPM_LINUX_DEV
#define INTERNAL_SEND_COMMAND TPM2_LINUX_SendCommand
#define TPM2_INTERNAL_CLEANUP(ctx)
#elif defined(WOLFTPM_SWTPM)
#define INTERNAL_SEND_COMMAND TPM2_SWTPM_SendCommand
#define TPM2_INTERNAL_CLEANUP(ctx)
#elif defined(WOLFTPM_WINAPI)
#define INTERNAL_SEND_COMMAND TPM2_WinApi_SendCommand
#define TPM2_INTERNAL_CLEANUP(ctx) TPM2_WinApi_Cleanup(ctx)
#else
#define INTERNAL_SEND_COMMAND TPM2_TIS_SendCommand
#define TPM2_INTERNAL_CLEANUP(ctx)
#endif
/******************************************************************************/
/* --- Local Functions -- */
/******************************************************************************/
static TPM_RC TPM2_AcquireLock(TPM2_CTX* ctx)
{
#if defined(WOLFTPM2_NO_WOLFCRYPT) || defined(SINGLE_THREADED)
(void)ctx;
#else
int ret;
if (!ctx->hwLockInit) {
if (wc_InitMutex(&ctx->hwLock) != 0) {
#ifdef DEBUG_WOLFTPM
printf("TPM Mutex Init failed\n");
#endif
return TPM_RC_FAILURE;
}
ctx->hwLockInit = 1;
ctx->lockCount = 0;
}
if (ctx->lockCount == 0) {
ret = wc_LockMutex(&ctx->hwLock);
if (ret != 0)
return TPM_RC_FAILURE;
}
ctx->lockCount++;
#endif
return TPM_RC_SUCCESS;
}
static void TPM2_ReleaseLock(TPM2_CTX* ctx)
{
#if defined(WOLFTPM2_NO_WOLFCRYPT) || defined(SINGLE_THREADED)
(void)ctx;
#else
ctx->lockCount--;
if (ctx->lockCount == 0) {
wc_UnLockMutex(&ctx->hwLock);
}
#endif
}
/* Send Command Wrapper */
typedef enum CmdFlags {
CMD_FLAG_NONE = 0x00,
CMD_FLAG_ENC2 = 0x01, /* 16-bit size of first command parameter */
CMD_FLAG_ENC4 = 0x02, /* 32-bit size (not used) */
CMD_FLAG_DEC2 = 0x04, /* 16-bit size of first response parameter */
CMD_FLAG_DEC4 = 0x08, /* 32-bit size (not used) */
} CmdFlags_t;
/* Command Details */
typedef struct {
int authCnt; /* number of authentication handles - determined at run-time */
int inHandleCnt; /* number of input handles - fixed */
int outHandleCnt; /* number of output handles - fixed */
int flags; /* If command allows param enc or dec - fixed */
} CmdInfo_t;
static int TPM2_CommandProcess(TPM2_CTX* ctx, TPM2_Packet* packet,
CmdInfo_t* info, TPM_CC cmdCode, UINT32 cmdSz)
{
int rc = TPM_RC_SUCCESS;
UINT32 authSz;
BYTE *param, *encParam = NULL;
int paramSz, encParamSz = 0;
int i, authPos;
int tmpSz = 0; /* Used to calculate the new total size of the Auth Area */
#ifndef WOLFTPM2_NO_WOLFCRYPT
UINT32 handleValue1, handleValue2, handleValue3;
int handlePos;
#endif
/* Skip the header and handles area */
packet->pos = TPM2_HEADER_SIZE + (info->inHandleCnt * sizeof(TPM_HANDLE));
/* Parse Auth */
TPM2_Packet_ParseU32(packet, &authSz);
packet->pos -= sizeof(authSz);
/* Later Auth Area size is updated */
TPM2_Packet_MarkU32(packet, &tmpSz);
/* Mark the position of the Auth Area data */
authPos = packet->pos;
packet->pos += authSz;
/* Mark parameter data */
param = &packet->buf[packet->pos];
paramSz = cmdSz - packet->pos;
/* Mark "first" encryption parameter */
if (info->flags & CMD_FLAG_ENC2) {
UINT16 tempSz;
TPM2_Packet_ParseU16(packet, &tempSz);
encParam = param + sizeof(UINT16);
encParamSz = tempSz;
}
else if (info->flags & CMD_FLAG_ENC4) {
UINT32 tempSz;
TPM2_Packet_ParseU32(packet, &tempSz);
encParam = param + sizeof(UINT32);
encParamSz = tempSz;
}
#ifdef WOLFTPM_DEBUG_VERBOSE
printf("CommandProcess: Handles (Auth %d, In %d), CmdSz %d, AuthSz %d, ParamSz %d, EncSz %d\n",
info->authCnt, info->inHandleCnt, cmdSz, authSz, paramSz, encParamSz);
#else
(void)paramSz;
#endif
/* Get Handle */
#ifndef WOLFTPM2_NO_WOLFCRYPT
handlePos = packet->pos;
packet->pos = TPM2_HEADER_SIZE; /* Handles are right after header */
TPM2_Packet_ParseU32(packet, &handleValue1);
TPM2_Packet_ParseU32(packet, &handleValue2);
TPM2_Packet_ParseU32(packet, &handleValue3);
packet->pos = handlePos;
#endif
for (i=0; i<info->authCnt; i++) {
TPM2_AUTH_SESSION* session = &ctx->session[i];
TPMS_AUTH_COMMAND authCmd;
if (session->sessionHandle != TPM_RS_PW) {
/* Generate fresh nonce */
rc = TPM2_GetNonce(session->nonceCaller.buffer,
session->nonceCaller.size);
if (rc != TPM_RC_SUCCESS) {
return rc;
}
}
/* Note: Copy between TPM2_AUTH_SESSION and TPMS_AUTH_COMMAND is allowed */
XMEMCPY(&authCmd, session, sizeof(TPMS_AUTH_COMMAND));
if (TPM2_IS_HMAC_SESSION(session->sessionHandle) ||
TPM2_IS_POLICY_SESSION(session->sessionHandle))
{
#ifndef WOLFTPM2_NO_WOLFCRYPT
TPM2B_NAME name1, name2, name3;
TPM2B_DIGEST hash;
#endif
/* if param enc is not supported for this command then clear flag */
/* session attribute flags are from TPM perspective */
if ((info->flags & (CMD_FLAG_ENC2 | CMD_FLAG_ENC4)) == 0) {
authCmd.sessionAttributes &= ~TPMA_SESSION_decrypt;
}
if ((info->flags & (CMD_FLAG_DEC2 | CMD_FLAG_DEC4)) == 0) {
authCmd.sessionAttributes &= ~TPMA_SESSION_encrypt;
}
/* Handle session request for encryption */
if (encParam && authCmd.sessionAttributes & TPMA_SESSION_decrypt) {
/* Encrypt the first command parameter */
rc = TPM2_ParamEnc_CmdRequest(session, encParam, encParamSz);
if (rc != TPM_RC_SUCCESS) {
#ifdef DEBUG_WOLFTPM
printf("Command parameter encryption failed\n");
#endif
return rc;
}
}
#ifndef WOLFTPM2_NO_WOLFCRYPT
rc = TPM2_GetName(ctx, handleValue1, info->inHandleCnt, 0, &name1);
rc |= TPM2_GetName(ctx, handleValue2, info->inHandleCnt, 1, &name2);
rc |= TPM2_GetName(ctx, handleValue3, info->inHandleCnt, 2, &name3);
if (rc != TPM_RC_SUCCESS) {
#ifdef DEBUG_WOLFTPM
printf("Error getting names for cpHash!\n");
#endif
return BAD_FUNC_ARG;
}
/* calculate "cpHash" hash for command code, names and parameters */
rc = TPM2_CalcCpHash(session->authHash, cmdCode, &name1,
&name2, &name3, param, paramSz, &hash);
if (rc != TPM_RC_SUCCESS) {
#ifdef DEBUG_WOLFTPM
printf("Error calculating cpHash!\n");
#endif
return rc;
}
/* Calculate HMAC for policy, hmac or salted sessions */
/* this is done after encryption */
rc = TPM2_CalcHmac(session->authHash, &session->auth, &hash,
&session->nonceCaller, &session->nonceTPM,
authCmd.sessionAttributes, &authCmd.hmac);
if (rc != TPM_RC_SUCCESS) {
#ifdef DEBUG_WOLFTPM
printf("Error calculating command HMAC!\n");
#endif
return rc;
}
#endif /* !WOLFTPM2_NO_WOLFCRYPT */
}
/* Replace auth in session */
packet->pos = authPos;
TPM2_Packet_AppendAuthCmd(packet, &authCmd);
authPos = packet->pos; /* update auth position */
}
/* Update the Auth Area size in the command packet */
TPM2_Packet_PlaceU32(packet, tmpSz);
(void)cmdCode;
return rc;
}
static int TPM2_ResponseProcess(TPM2_CTX* ctx, TPM2_Packet* packet,
CmdInfo_t* info, TPM_CC cmdCode, UINT32 respSz)
{
int rc = TPM_RC_SUCCESS;
BYTE *param, *decParam = NULL;
UINT32 paramSz, decParamSz = 0, authPos;
int i;
/* Skip the header output handles */
packet->pos = TPM2_HEADER_SIZE + (info->outHandleCnt * sizeof(TPM_HANDLE));
/* Response Parameter Size */
TPM2_Packet_ParseU32(packet, ¶mSz);
param = &packet->buf[packet->pos]; /* Mark parameter data */
authPos = packet->pos + paramSz;
/* Mark "first" decryption parameter */
if (info->flags & CMD_FLAG_DEC2) {
UINT16 tempSz;
TPM2_Packet_ParseU16(packet, &tempSz);
decParam = param + sizeof(UINT16);
decParamSz = tempSz;
}
else if (info->flags & CMD_FLAG_DEC4) {
UINT32 tempSz;
TPM2_Packet_ParseU32(packet, &tempSz);
decParam = param + sizeof(UINT32);
decParamSz = tempSz;
}
#ifdef WOLFTPM_DEBUG_VERBOSE
printf("ResponseProcess: Handles (Out %d), RespSz %d, ParamSz %d, DecSz %d, AuthSz %d\n",
info->outHandleCnt, respSz, paramSz, decParamSz, respSz - authPos);
#endif
for (i=0; i<info->authCnt; i++) {
TPM2_AUTH_SESSION* session = &ctx->session[i];
TPMS_AUTH_RESPONSE authRsp;
XMEMSET(&authRsp, 0, sizeof(authRsp));
/* Parse Auth - if exists */
if (respSz > authPos) {
packet->pos = authPos;
TPM2_Packet_ParseAuth(packet, &authRsp);
authPos = packet->pos;
}
if (session->sessionHandle != TPM_RS_PW) {
/* update nonceTPM */
if (authRsp.nonce.size > 0) {
session->nonceTPM.size = authRsp.nonce.size;
XMEMCPY(session->nonceTPM.buffer, authRsp.nonce.buffer,
authRsp.nonce.size);
}
#ifndef WOLFTPM2_NO_WOLFCRYPT
if (authRsp.hmac.size > 0) {
TPM2B_DIGEST hash;
TPM2B_AUTH hmac;
/* calculate "rpHash" hash for command code and parameters */
rc = TPM2_CalcRpHash(session->authHash, cmdCode, param, paramSz,
&hash);
if (rc != TPM_RC_SUCCESS) {
#ifdef DEBUG_WOLFTPM
printf("Error calculating rpHash!\n");
#endif
return rc;
}
/* Calculate HMAC prior to decryption */
rc = TPM2_CalcHmac(session->authHash, &session->auth, &hash,
&session->nonceTPM, &session->nonceCaller,
authRsp.sessionAttributes, &hmac);
if (rc != TPM_RC_SUCCESS) {
#ifdef DEBUG_WOLFTPM
printf("Error calculating response HMAC!\n");
#endif
return rc;
}
/* Verify HMAC */
if (hmac.size != authRsp.hmac.size ||
XMEMCMP(hmac.buffer, authRsp.hmac.buffer, hmac.size) != 0) {
#ifdef DEBUG_WOLFTPM
printf("Response HMAC verification failed!\n");
#endif
return TPM_RC_HMAC;
}
}
#else
(void)cmdCode;
#endif
/* Handle session request for decryption */
/* If the response supports decryption */
if (decParam && authRsp.sessionAttributes & TPMA_SESSION_encrypt) {
/* Decrypt the first response parameter */
rc = TPM2_ParamDec_CmdResponse(session, decParam, decParamSz);
if (rc != TPM_RC_SUCCESS) {
#ifdef DEBUG_WOLFTPM
printf("Response parameter decryption failed\n");
#endif
return rc;
}
}
}
}
return rc;
}
static TPM_RC TPM2_SendCommandAuth(TPM2_CTX* ctx, TPM2_Packet* packet,
CmdInfo_t* info)
{
TPM_RC rc = TPM_RC_FAILURE;
TPM_ST tag;
TPM_CC cmdCode;
BYTE *cmd;
UINT32 cmdSz, respSz;
if (ctx == NULL || packet == NULL || info == NULL)
return BAD_FUNC_ARG;
cmd = packet->buf;
cmdSz = packet->pos;
(void)cmd;
/* restart the unmarshalling position */
packet->pos = 0;
TPM2_Packet_ParseU16(packet, &tag);
TPM2_Packet_ParseU32(packet, NULL);
TPM2_Packet_ParseU32(packet, &cmdCode); /* Extract TPM Command Code */
/* Is auth session required for this TPM command? */
if (tag == TPM_ST_SESSIONS) {
/* Is there at least one auth session present? */
if (info->authCnt < 1 || ctx->session == NULL)
return TPM_RC_AUTH_MISSING;
#ifdef WOLFTPM_DEBUG_VERBOSE
printf("Found %d auth sessions\n", info->authCnt);
#endif
rc = TPM2_CommandProcess(ctx, packet, info, cmdCode, cmdSz);
if (rc != 0)
return rc;
}
/* reset packet->pos to total command length (send command requires it) */
packet->pos = cmdSz;
/* submit command and wait for response */
rc = (TPM_RC)INTERNAL_SEND_COMMAND(ctx, packet);
if (rc != 0)
return rc;
/* parse response */
rc = TPM2_Packet_Parse(rc, packet);
respSz = packet->size;
/* restart the unmarshalling position */
packet->pos = 0;
TPM2_Packet_ParseU16(packet, &tag);
/* Is auth session required for this TPM command? */
if (rc == TPM_RC_SUCCESS && tag == TPM_ST_SESSIONS) {
rc = TPM2_ResponseProcess(ctx, packet, info, cmdCode, respSz);
}
/* Caller expects packet position to be at end of header */
packet->pos = TPM2_HEADER_SIZE;
return rc;
}
static TPM_RC TPM2_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
{
TPM_RC rc;
if (ctx == NULL || packet == NULL)
return BAD_FUNC_ARG;
/* submit command and wait for response */
rc = (TPM_RC)INTERNAL_SEND_COMMAND(ctx, packet);
if (rc != 0)
return rc;
return TPM2_Packet_Parse(rc, packet);
}
static TPM_ST TPM2_GetTag(TPM2_CTX* ctx)
{
TPM_ST st = TPM_ST_NO_SESSIONS;
if (ctx && ctx->session) {
int authCount = TPM2_GetSessionAuthCount(ctx);
if (authCount == 1 && ctx->session[0].sessionHandle != TPM_RS_PW) {
st = TPM_ST_SESSIONS;
}
}
return st;
}
#ifndef WOLFTPM2_NO_WOLFCRYPT
static inline int TPM2_WolfCrypt_Init(void)
{
int rc = 0;
/* track reference count for wolfCrypt initialization */
if (gWolfCryptRefCount == 0) {
#ifdef DEBUG_WOLFSSL
wolfSSL_Debugging_ON();
#endif
rc = wolfCrypt_Init();
#ifdef WC_RNG_SEED_CB
if (rc == 0)
rc = wc_SetSeed_Cb(wc_GenerateSeed);
#endif
}
gWolfCryptRefCount++;
return rc;
}
#endif
/******************************************************************************/
/* --- Public Functions -- */
/******************************************************************************/
TPM2_CTX* TPM2_GetActiveCtx(void)
{
return gActiveTPM;
}
void TPM2_SetActiveCtx(TPM2_CTX* ctx)
{
gActiveTPM = ctx;
}
TPM_RC TPM2_SetSessionAuth(TPM2_AUTH_SESSION* session)
{
TPM_RC rc;
TPM2_CTX* ctx = TPM2_GetActiveCtx();
if (ctx == NULL)
return BAD_FUNC_ARG;
rc = TPM2_AcquireLock(ctx);
if (rc == TPM_RC_SUCCESS) {
ctx->session = session;
TPM2_ReleaseLock(ctx);
}
return rc;
}
/* Finds the number of active Auth Session in the given TPM2 context */
int TPM2_GetSessionAuthCount(TPM2_CTX* ctx)
{
int sessionCount, sessionHandle;
if (ctx == NULL || ctx->session == NULL)
return BAD_FUNC_ARG;
for (sessionCount = 0; sessionCount < MAX_SESSION_NUM; sessionCount++) {
sessionHandle = ctx->session[sessionCount].sessionHandle;
/* According to the TCG Spec, Part 1, Chapter 15.4
* Session Handles have most significant octet at
* 0x02 for HMAC sessions
* 0x03 for Policy sessions
* Password sessions use predefined value of TPM_RS_PW
* Trial sessions are not of interest
*/
if (sessionHandle != TPM_RS_PW) {
/* Not a password session, mask the most significant octet(MSO) */
sessionHandle &= 0xFF000000;
/* Check MSO for an HMAC or Policy session, otherwise invalid */
if ((sessionHandle ^ 0x02000000) && (sessionHandle ^ 0x03000000))
break;
}
}
return sessionCount;
}
TPM_RC TPM2_ChipStartup(TPM2_CTX* ctx, int timeoutTries)
{
TPM_RC rc;
if (ctx == NULL) {
return BAD_FUNC_ARG;
}
rc = TPM2_AcquireLock(ctx);
if (rc == TPM_RC_SUCCESS) {
/* Wait for chip startup to complete */
rc = TPM2_TIS_StartupWait(ctx, timeoutTries);
if (rc == TPM_RC_SUCCESS) {
/* Request locality for TPM module */
rc = TPM2_TIS_RequestLocality(ctx, timeoutTries);
if (rc == TPM_RC_SUCCESS) {
/* Get device information */
rc = TPM2_TIS_GetInfo(ctx);
}
}
TPM2_ReleaseLock(ctx);
}
return rc;
}
TPM_RC TPM2_SetHalIoCb(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx)
{
TPM_RC rc;
if (ctx == NULL || ioCb == NULL) {
return BAD_FUNC_ARG;
}
rc = TPM2_AcquireLock(ctx);
if (rc == TPM_RC_SUCCESS) {
ctx->ioCb = ioCb;
ctx->userCtx = userCtx;
TPM2_ReleaseLock(ctx);
}
return rc;
}
/* If timeoutTries <= 0 then it will not try and startup chip and will
use existing default locality */
TPM_RC TPM2_Init_ex(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx,
int timeoutTries)
{
TPM_RC rc = TPM_RC_SUCCESS;
if (ctx == NULL) {
return BAD_FUNC_ARG;
}
XMEMSET(ctx, 0, sizeof(TPM2_CTX));
#ifndef WOLFTPM2_NO_WOLFCRYPT
rc = TPM2_WolfCrypt_Init();
if (rc != 0)
return rc;
#endif
#if defined(WOLFTPM_SWTPM)
ctx->tcpCtx.fd = -1;
#endif
#if defined(WOLFTPM_LINUX_DEV) || defined(WOLFTPM_SWTPM) || \
defined(WOLFTPM_WINAPI)
if (ioCb != NULL || userCtx != NULL) {
return BAD_FUNC_ARG;
}
#else
#ifdef WOLFTPM_MMIO
if (ioCb == NULL)
ioCb = TPM2_IoCb_Mmio;
#endif
/* Setup HAL IO Callback */
rc = TPM2_SetHalIoCb(ctx, ioCb, userCtx);
if (rc != TPM_RC_SUCCESS)
return rc;
#endif
/* Set the active TPM global */
TPM2_SetActiveCtx(ctx);
if (timeoutTries > 0) {
/* Perform chip startup and assign locality */
rc = TPM2_ChipStartup(ctx, timeoutTries);
}
else {
/* use existing locality */
ctx->locality = WOLFTPM_LOCALITY_DEFAULT;
}
return rc;
}
TPM_RC TPM2_Init_minimal(TPM2_CTX* ctx)
{
return TPM2_Init_ex(ctx, NULL, NULL, 0);
}
TPM_RC TPM2_Init(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx)
{
return TPM2_Init_ex(ctx, ioCb, userCtx, TPM_TIMEOUT_TRIES);
}
TPM_RC TPM2_Cleanup(TPM2_CTX* ctx)
{
TPM_RC rc;
if (ctx == NULL)
return BAD_FUNC_ARG;
/* clear global */
rc = TPM2_AcquireLock(ctx);
if (rc == TPM_RC_SUCCESS) {
if (TPM2_GetActiveCtx() == ctx) {
TPM2_INTERNAL_CLEANUP(ctx);
/* set non-active */
TPM2_SetActiveCtx(NULL);
}
TPM2_ReleaseLock(ctx);
}
#ifndef WOLFTPM2_NO_WOLFCRYPT
#ifdef WOLFTPM2_USE_WOLF_RNG
if (ctx->rngInit) {
ctx->rngInit = 0;
wc_FreeRng(&ctx->rng);
}
#endif
#ifndef SINGLE_THREADED
if (ctx->hwLockInit) {
ctx->hwLockInit = 0;
wc_FreeMutex(&ctx->hwLock);
}
#endif
/* track wolf initialize reference count in wolfTPM. wolfCrypt does not
properly track reference count in v4.1 or older releases */
gWolfCryptRefCount--;
if (gWolfCryptRefCount < 0)
gWolfCryptRefCount = 0;
if (gWolfCryptRefCount == 0) {
wolfCrypt_Cleanup();
}
#endif /* !WOLFTPM2_NO_WOLFCRYPT */
return TPM_RC_SUCCESS;
}
/******************************************************************************/
/* --- BEGIN Standard TPM API's -- */
/******************************************************************************/
TPM_RC TPM2_Startup(Startup_In* in)
{
TPM_RC rc;
TPM2_CTX* ctx = TPM2_GetActiveCtx();
if (ctx == NULL || in == NULL)
return BAD_FUNC_ARG;
rc = TPM2_AcquireLock(ctx);
if (rc == TPM_RC_SUCCESS) {
TPM2_Packet packet;
TPM2_Packet_Init(ctx, &packet);
TPM2_Packet_AppendU16(&packet, in->startupType);
TPM2_Packet_Finalize(&packet, TPM_ST_NO_SESSIONS, TPM_CC_Startup);
/* send command */
rc = TPM2_SendCommand(ctx, &packet);
TPM2_ReleaseLock(ctx);
}
return rc;
}
TPM_RC TPM2_Shutdown(Shutdown_In* in)
{
TPM_RC rc;
TPM2_CTX* ctx = TPM2_GetActiveCtx();
if (ctx == NULL || in == NULL)
return BAD_FUNC_ARG;
rc = TPM2_AcquireLock(ctx);
if (rc == TPM_RC_SUCCESS) {
TPM2_Packet packet;
TPM2_Packet_Init(ctx, &packet);
TPM2_Packet_AppendU16(&packet, in->shutdownType);
TPM2_Packet_Finalize(&packet, TPM_ST_NO_SESSIONS, TPM_CC_Shutdown);
/* send command */
rc = TPM2_SendCommand(ctx, &packet);
TPM2_ReleaseLock(ctx);
}
return rc;
}
TPM_RC TPM2_SelfTest(SelfTest_In* in)
{
TPM_RC rc;
TPM2_CTX* ctx = TPM2_GetActiveCtx();
if (ctx == NULL || in == NULL)
return BAD_FUNC_ARG;
rc = TPM2_AcquireLock(ctx);
if (rc == TPM_RC_SUCCESS) {
TPM2_Packet packet;
TPM2_Packet_Init(ctx, &packet);
TPM2_Packet_AppendU8(&packet, in->fullTest);
TPM2_Packet_Finalize(&packet, TPM_ST_NO_SESSIONS, TPM_CC_SelfTest);
/* send command */
rc = TPM2_SendCommand(ctx, &packet);
TPM2_ReleaseLock(ctx);
}
return rc;
}
TPM_RC TPM2_IncrementalSelfTest(IncrementalSelfTest_In* in,
IncrementalSelfTest_Out* out)
{
TPM_RC rc;
TPM2_CTX* ctx = TPM2_GetActiveCtx();
if (ctx == NULL || in == NULL || out == NULL)
return BAD_FUNC_ARG;
rc = TPM2_AcquireLock(ctx);
if (rc == TPM_RC_SUCCESS) {
int i;
TPM2_Packet packet;
TPM2_Packet_Init(ctx, &packet);
TPM2_Packet_AppendU32(&packet, in->toTest.count);
for (i=0; i<(int)in->toTest.count; i++) {
TPM2_Packet_AppendU16(&packet, in->toTest.algorithms[i]);
}
TPM2_Packet_Finalize(&packet, TPM_ST_NO_SESSIONS,
TPM_CC_IncrementalSelfTest);
/* send command */
rc = TPM2_SendCommand(ctx, &packet);
if (rc == TPM_RC_SUCCESS) {
TPM2_Packet_ParseU32(&packet, &out->toDoList.count);
for (i=0; i<(int)out->toDoList.count; i++) {
TPM2_Packet_ParseU16(&packet, &out->toDoList.algorithms[i]);
}
}
TPM2_ReleaseLock(ctx);
}
return rc;
}
TPM_RC TPM2_GetTestResult(GetTestResult_Out* out)
{
TPM_RC rc;
TPM2_CTX* ctx = TPM2_GetActiveCtx();
if (ctx == NULL || out == NULL)
return BAD_FUNC_ARG;
rc = TPM2_AcquireLock(ctx);
if (rc == TPM_RC_SUCCESS) {
TPM2_Packet packet;
TPM2_Packet_Init(ctx, &packet);
TPM2_Packet_Finalize(&packet, TPM_ST_NO_SESSIONS, TPM_CC_GetTestResult);
/* send command */
rc = TPM2_SendCommand(ctx, &packet);
if (rc == TPM_RC_SUCCESS) {
TPM2_Packet_ParseU16(&packet, &out->outData.size);
TPM2_Packet_ParseBytes(&packet, out->outData.buffer,
out->outData.size);
TPM2_Packet_ParseU16(&packet, &out->testResult);
}
TPM2_ReleaseLock(ctx);
}
return rc;
}
TPM_RC TPM2_GetCapability(GetCapability_In* in, GetCapability_Out* out)
{
TPM_RC rc;
TPM2_CTX* ctx = TPM2_GetActiveCtx();
if (ctx == NULL || in == NULL || out == NULL)
return BAD_FUNC_ARG;
rc = TPM2_AcquireLock(ctx);
if (rc == TPM_RC_SUCCESS) {
int i;
TPM2_Packet packet;
TPM2_Packet_Init(ctx, &packet);
TPM2_Packet_AppendU32(&packet, in->capability);
TPM2_Packet_AppendU32(&packet, in->property);
TPM2_Packet_AppendU32(&packet, in->propertyCount);
TPM2_Packet_Finalize(&packet, TPM_ST_NO_SESSIONS, TPM_CC_GetCapability);
/* send command */
rc = TPM2_SendCommand(ctx, &packet);
if (rc == TPM_RC_SUCCESS) {
TPM2_Packet_ParseU8(&packet, &out->moreData);
TPM2_Packet_ParseU32(&packet, &out->capabilityData.capability);
switch (out->capabilityData.capability) {
case TPM_CAP_TPM_PROPERTIES: {
TPML_TAGGED_TPM_PROPERTY* prop =
&out->capabilityData.data.tpmProperties;
TPM2_Packet_ParseU32(&packet, &prop->count);
for (i=0; i<(int)prop->count; i++) {
TPM2_Packet_ParseU32(&packet,
&prop->tpmProperty[i].property);
TPM2_Packet_ParseU32(&packet,
&prop->tpmProperty[i].value);
}
break;
}
default:
#ifdef DEBUG_WOLFTPM
printf("Unknown capability type 0x%x\n",
(unsigned int)out->capabilityData.capability);
#endif
rc = -1;
break;
}
}
TPM2_ReleaseLock(ctx);
}
return rc;
}
TPM_RC TPM2_GetRandom(GetRandom_In* in, GetRandom_Out* out)
{
TPM_RC rc;
TPM2_CTX* ctx = TPM2_GetActiveCtx();
if (ctx == NULL || in == NULL || out == NULL)
return BAD_FUNC_ARG;
rc = TPM2_AcquireLock(ctx);
if (rc == TPM_RC_SUCCESS) {
TPM2_Packet packet;
TPM2_Packet_Init(ctx, &packet);
TPM2_Packet_AppendU16(&packet, in->bytesRequested);
TPM2_Packet_Finalize(&packet, TPM_ST_NO_SESSIONS, TPM_CC_GetRandom);
/* send command */
rc = TPM2_SendCommand(ctx, &packet);
if (rc == TPM_RC_SUCCESS) {
TPM2_Packet_ParseU16(&packet, &out->randomBytes.size);
TPM2_Packet_ParseBytes(&packet, out->randomBytes.buffer,
out->randomBytes.size);
}
TPM2_ReleaseLock(ctx);
}
return rc;
}
TPM_RC TPM2_StirRandom(StirRandom_In* in)
{
TPM_RC rc;
TPM2_CTX* ctx = TPM2_GetActiveCtx();
if (ctx == NULL || in == NULL)
return BAD_FUNC_ARG;
rc = TPM2_AcquireLock(ctx);
if (rc == TPM_RC_SUCCESS) {
TPM2_Packet packet;
TPM2_Packet_Init(ctx, &packet);
TPM2_Packet_AppendU16(&packet, in->inData.size);
TPM2_Packet_AppendBytes(&packet, in->inData.buffer, in->inData.size);
TPM2_Packet_Finalize(&packet, TPM_ST_NO_SESSIONS, TPM_CC_StirRandom);
/* send command */
rc = TPM2_SendCommand(ctx, &packet);
TPM2_ReleaseLock(ctx);
}
return rc;
}
TPM_RC TPM2_PCR_Read(PCR_Read_In* in, PCR_Read_Out* out)
{
TPM_RC rc;
TPM2_CTX* ctx = TPM2_GetActiveCtx();
if (ctx == NULL || in == NULL || out == NULL)
return BAD_FUNC_ARG;
rc = TPM2_AcquireLock(ctx);
if (rc == TPM_RC_SUCCESS) {
int i;
TPM2_Packet packet;
TPM2_Packet_Init(ctx, &packet);
TPM2_Packet_AppendPCR(&packet, &in->pcrSelectionIn);
TPM2_Packet_Finalize(&packet, TPM_ST_NO_SESSIONS, TPM_CC_PCR_Read);
/* send command */
rc = TPM2_SendCommand(ctx, &packet);
if (rc == TPM_RC_SUCCESS) {
TPM2_Packet_ParseU32(&packet, &out->pcrUpdateCounter);
TPM2_Packet_ParsePCR(&packet, &out->pcrSelectionOut);
TPM2_Packet_ParseU32(&packet, &out->pcrValues.count);
for (i=0; i<(int)out->pcrValues.count; i++) {
TPM2_Packet_ParseU16(&packet, &out->pcrValues.digests[i].size);
TPM2_Packet_ParseBytes(&packet,
out->pcrValues.digests[i].buffer,
out->pcrValues.digests[i].size);
}
}
TPM2_ReleaseLock(ctx);
}
return rc;
}
TPM_RC TPM2_PCR_Extend(PCR_Extend_In* in)
{
TPM_RC rc;
TPM2_CTX* ctx = TPM2_GetActiveCtx();
if (ctx == NULL || in == NULL || ctx->session == NULL)
return BAD_FUNC_ARG;
rc = TPM2_AcquireLock(ctx);