forked from wolfSSL/wolfTPM
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunit_tests.c
More file actions
5867 lines (5002 loc) · 209 KB
/
Copy pathunit_tests.c
File metadata and controls
5867 lines (5002 loc) · 209 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
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
/* unit_tests.c
*
* Copyright (C) 2006-2026 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 3 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
*/
/* wolfTPM 2.0 unit tests */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolftpm/tpm2.h>
#include <wolftpm/tpm2_wrap.h>
#include <wolftpm/tpm2_param_enc.h>
#include <wolftpm/tpm2_asn.h>
#include <wolftpm/tpm2_swtpm.h>
#include <wolftpm/tpm2_tis.h>
#include <wolftpm/tpm2_spdm.h>
#include <hal/tpm_io.h>
#include <examples/tpm_test.h>
#include <examples/tpm_test_keys.h>
#include <examples/wrap/wrap_test.h>
#include <stdio.h>
#if defined(__linux__) || defined(__APPLE__) || defined(__unix__)
#include <fcntl.h>
#endif
/* Test Fail Helpers */
#ifndef NO_ABORT
#ifndef XABORT
#include <stdlib.h>
#define XABORT() abort()
#endif
#else
#undef XABORT
#define XABORT()
#endif
#define Fail(description, result) do { \
printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \
printf("\n expected: "); printf description; \
printf("\n result: "); printf result; printf("\n\n"); \
fflush(stdout); \
XABORT(); \
} while(0)
#define Assert(test, description, result) if (!(test)) Fail(description, result)
#define AssertTrue(x) Assert( (x), ("%s is true", #x), (#x " => FALSE"))
#define AssertFalse(x) Assert(!(x), ("%s is false", #x), (#x " => TRUE"))
#define AssertNotNull(x) Assert( (x), ("%s is not null", #x), (#x " => NULL"))
#define AssertNull(x) do { \
void* _x = (void *) (x); \
Assert(!_x, ("%s is null", #x), (#x " => %p", _x)); \
} while(0)
#define AssertInt(x, y, op, er) do { \
int _x = (int)x; \
int _y = (int)y; \
Assert(_x op _y, ("%s " #op " %s", #x, #y), ("%d(0x%x) " #er " %d(0x%x)", \
_x, _x, _y, _y)); \
} while(0)
#define AssertIntEQ(x, y) AssertInt(x, y, ==, !=)
#define AssertIntNE(x, y) AssertInt(x, y, !=, ==)
#define AssertIntGT(x, y) AssertInt(x, y, >, <=)
#define AssertIntLT(x, y) AssertInt(x, y, <, >=)
#define AssertIntGE(x, y) AssertInt(x, y, >=, <)
#define AssertIntLE(x, y) AssertInt(x, y, <=, >)
#define AssertStr(x, y, op, er) do { \
const char* _x = x; \
const char* _y = y; \
int _z = (_x && _y) ? strcmp(_x, _y) : -1; \
Assert(_z op 0, ("%s " #op " %s", #x, #y), \
("\"%s\" " #er " \"%s\"", _x, _y));\
} while(0)
#define AssertStrEQ(x, y) AssertStr(x, y, ==, !=)
#define AssertStrNE(x, y) AssertStr(x, y, !=, ==)
#define AssertStrGT(x, y) AssertStr(x, y, >, <=)
#define AssertStrLT(x, y) AssertStr(x, y, <, >=)
#define AssertStrGE(x, y) AssertStr(x, y, >=, <)
#define AssertStrLE(x, y) AssertStr(x, y, <=, >)
#ifndef WOLFTPM2_NO_WRAPPER
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(HAVE_ECC) && \
!defined(WOLFTPM2_NO_ASN)
/* Query TPM_CAP_ALGS to see if a given algorithm is supported.
* Returns 1 if supported, 0 otherwise. Used to skip test iterations on TPMs
* that don't implement a given hash (e.g. Nuvoton NPCT75x lacks SHA512).
* Guarded by the same ifdef as its only caller (test_wolfTPM2_EccSignVerifyDig)
* so non-ECC builds don't trip -Werror=unused-function. */
static int test_tpm_alg_supported(TPM_ALG_ID alg)
{
GetCapability_In in;
GetCapability_Out out;
word32 i;
XMEMSET(&in, 0, sizeof(in));
XMEMSET(&out, 0, sizeof(out));
in.capability = TPM_CAP_ALGS;
in.property = alg;
in.propertyCount = 1;
if (TPM2_GetCapability(&in, &out) != TPM_RC_SUCCESS) {
return 1; /* On error, assume supported and let the real call fail */
}
for (i = 0; i < out.capabilityData.data.algorithms.count; i++) {
if (out.capabilityData.data.algorithms.algProperties[i].alg == alg) {
return 1;
}
}
return 0;
}
#endif /* !WOLFTPM2_NO_WOLFCRYPT && HAVE_ECC && !WOLFTPM2_NO_ASN */
static void test_wolfTPM2_Init(void)
{
int rc;
WOLFTPM2_DEV dev;
/* Test first argument, wolfTPM2 context */
rc = wolfTPM2_Init(NULL, TPM2_IoCb, NULL);
AssertIntNE(rc, 0);
/* Test second argument, TPM2 IO Callbacks */
rc = wolfTPM2_Init(&dev, NULL, NULL);
#if defined(WOLFTPM_LINUX_DEV) || defined(WOLFTPM_SWTPM) || \
defined(WOLFTPM_WINAPI)
/* Custom IO Callbacks are not needed for Linux TIS driver */
AssertIntEQ(rc, 0);
#else
/* IO Callbacks are required for SPIdev/I2C and must be valid */
AssertIntNE(rc, 0);
#endif
/* Test success */
rc = wolfTPM2_Init(&dev, TPM2_IoCb, NULL);
AssertIntEQ(rc, 0);
wolfTPM2_Cleanup(&dev);
printf("Test TPM Wrapper: %-40s %s\n", "Init:",
rc == 0 ? "Passed" : "Failed");
}
/* test for WOLFTPM2_DEV restore */
static void test_wolfTPM2_OpenExisting(void)
{
int rc;
WOLFTPM2_DEV dev;
WOLFTPM2_CAPS caps;
/* Init the TPM2 device */
rc = wolfTPM2_Init(&dev, TPM2_IoCb, NULL);
AssertIntEQ(rc, 0);
/* Test access to TPM by getting capabilities */
rc = wolfTPM2_GetCapabilities(&dev, &caps);
AssertIntEQ(rc, 0);
/* Perform cleanup, but don't shutdown TPM module */
rc = wolfTPM2_Cleanup_ex(&dev, 0);
AssertIntEQ(rc, 0);
/* Restore TPM access */
rc = wolfTPM2_OpenExisting(&dev, TPM2_IoCb, NULL);
AssertIntEQ(rc, 0);
/* Test access to TPM by getting capabilities */
rc = wolfTPM2_GetCapabilities(&dev, &caps);
AssertIntEQ(rc, 0);
wolfTPM2_Cleanup(&dev);
printf("Test TPM Wrapper: %-40s %s\n", "Open Existing:",
rc == 0 ? "Passed" : "Failed");
}
/* test for wolfTPM2_GetCapabilities */
static void test_wolfTPM2_GetCapabilities(void)
{
int rc;
WOLFTPM2_DEV dev;
WOLFTPM2_CAPS caps;
rc = wolfTPM2_Init(&dev, TPM2_IoCb, NULL);
AssertIntEQ(rc, 0);
/* Test Arguments */
rc = wolfTPM2_GetCapabilities(NULL, &caps);
AssertIntNE(rc, 0);
rc = wolfTPM2_GetCapabilities(&dev, NULL);
AssertIntNE(rc, 0);
/* Test success */
rc = wolfTPM2_GetCapabilities(&dev, &caps);
AssertIntEQ(rc, 0);
#ifdef DEBUG_WOLFTPM
printf("Mfg %s (%d), Vendor %s, Fw %u.%u (%u), FIPS %s, CC-EAL4 %d\n",
caps.mfgStr, caps.mfg, caps.vendorStr, caps.fwVerMajor,
caps.fwVerMinor, caps.fwVerVendor,
TPM2_GetCapsFipsStr(caps.fips140_3, caps.fips140_2),
caps.cc_eal4);
#endif
wolfTPM2_Cleanup(&dev);
printf("Test TPM Wrapper: %-40s %s\n", "Get Capabilities:",
rc == 0 ? "Passed" : "Failed");
}
static void test_wolfTPM2_DictionaryAttack(void)
{
/* Argument validation (non-destructive; the functional lockout/recovery
* path is exercised by examples/management/da_check and the fwTPM unit
* tests). */
AssertIntEQ(wolfTPM2_DictionaryAttackLockReset(NULL), BAD_FUNC_ARG);
AssertIntEQ(wolfTPM2_DictionaryAttackParameters(NULL, 32, 0, 0),
BAD_FUNC_ARG);
/* newMaxTries of 0 is rejected client-side */
AssertIntEQ(wolfTPM2_DictionaryAttackParameters((WOLFTPM2_DEV*)1, 0, 0, 0),
BAD_FUNC_ARG);
printf("Test TPM Wrapper: %-40s %s\n", "Dictionary Attack args:",
"Passed");
}
/* test for wolfTPM2_ReadPublicKey */
static void test_wolfTPM2_ReadPublicKey(void)
{
int rc;
WOLFTPM2_DEV dev;
WOLFTPM2_KEY storageKey;
rc = wolfTPM2_Init(&dev, TPM2_IoCb, NULL);
AssertIntEQ(rc, 0);
/* Test arguments */
rc = wolfTPM2_ReadPublicKey(NULL, &storageKey, TPM2_DEMO_STORAGE_KEY_HANDLE);
AssertIntNE(rc, 0);
rc = wolfTPM2_ReadPublicKey(&dev, NULL, TPM2_DEMO_STORAGE_KEY_HANDLE);
AssertIntNE(rc, 0);
/* Test success: read storage primary key */
rc = wolfTPM2_ReadPublicKey(&dev, &storageKey,
TPM2_DEMO_STORAGE_KEY_HANDLE);
if ((rc & RC_MAX_FMT1) == TPM_RC_HANDLE) {
rc = 0; /* okay if not found */
}
AssertIntEQ(rc, 0);
wolfTPM2_Cleanup(&dev);
printf("Test TPM Wrapper: %-40s %s\n", "Read Public Key:",
rc == 0 ? "Passed" : "Failed");
}
#ifdef WOLFTPM_FIRMWARE_UPGRADE
#if defined(WOLFTPM_ST33) || defined(WOLFTPM_AUTODETECT)
/* Test ST33 firmware upgrade APIs (function availability and
* parameter validation). LMS vs non-LMS format is auto-detected
* from manifest size (177 bytes = non-LMS, 2697 bytes = LMS). */
static void test_wolfTPM2_ST33_FirmwareUpgrade(void)
{
int rc;
WOLFTPM2_DEV dev;
WOLFTPM2_CAPS caps;
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(WOLFSSL_SHA384)
/* Invalid manifest size (not 177 or 2697) for testing auto-detection */
uint8_t dummy_manifest[10] = {0};
#endif
/* Initialize TPM */
rc = wolfTPM2_Init(&dev, TPM2_IoCb, NULL);
if (rc != 0) {
printf("Test ST33 FW: %-40s Skipped (TPM not available)\n", "Init:");
return;
}
rc = wolfTPM2_GetCapabilities(&dev, &caps);
AssertIntEQ(rc, 0);
#ifdef DEBUG_WOLFTPM
/* Display firmware version info */
if (caps.mfg == TPM_MFG_STM) {
printf("ST33 TPM - Firmware: %u.%u (0x%x), Format: %s\n",
caps.fwVerMajor, caps.fwVerMinor, caps.fwVerVendor,
(caps.fwVerMinor >= 512) ? "LMS" : "non-LMS");
}
#endif
/* ===== Test NULL dev parameter handling ===== */
/* wolfTPM2_FirmwareUpgradeCancel - NULL dev */
rc = wolfTPM2_FirmwareUpgradeCancel(NULL);
AssertIntNE(rc, 0);
/* wolfTPM2_FirmwareUpgradeHash - NULL dev */
rc = wolfTPM2_FirmwareUpgradeHash(NULL, TPM_ALG_SHA384, NULL, 0, NULL,
0, NULL, NULL);
AssertIntNE(rc, 0);
/* wolfTPM2_FirmwareUpgradeRecover - NULL dev */
rc = wolfTPM2_FirmwareUpgradeRecover(NULL, NULL, 0, NULL, NULL);
AssertIntNE(rc, 0);
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(WOLFSSL_SHA384)
/* wolfTPM2_FirmwareUpgrade - NULL dev */
rc = wolfTPM2_FirmwareUpgrade(NULL, NULL, 0, NULL, NULL);
AssertIntNE(rc, 0);
#endif /* !WOLFTPM2_NO_WOLFCRYPT && WOLFSSL_SHA384 */
/* ===== Test NULL/invalid parameter combinations ===== */
/* wolfTPM2_FirmwareUpgradeHash - valid dev, NULL manifest */
rc = wolfTPM2_FirmwareUpgradeHash(&dev, TPM_ALG_SHA384, NULL, 0, NULL,
0, NULL, NULL);
AssertIntNE(rc, 0);
/* wolfTPM2_FirmwareUpgradeRecover - valid dev, NULL manifest */
rc = wolfTPM2_FirmwareUpgradeRecover(&dev, NULL, 0, NULL, NULL);
AssertIntNE(rc, 0);
/* wolfTPM2_FirmwareUpgradeCancel - valid dev (may succeed or fail
* depending on TPM state) */
rc = wolfTPM2_FirmwareUpgradeCancel(&dev);
/* Note: This may return success or error depending on TPM state -
* just verify it doesn't crash */
(void)rc;
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(WOLFSSL_SHA384)
/* wolfTPM2_FirmwareUpgrade - valid dev, NULL manifest */
rc = wolfTPM2_FirmwareUpgrade(&dev, NULL, 0, NULL, NULL);
AssertIntNE(rc, 0);
/* wolfTPM2_FirmwareUpgrade - valid dev, NULL callback */
rc = wolfTPM2_FirmwareUpgrade(&dev, dummy_manifest, sizeof(dummy_manifest),
NULL, NULL);
AssertIntNE(rc, 0);
/* Test ST33-specific manifest size validation if we have an ST33 TPM.
* Invalid manifest size (not 177 or 2697) should return BAD_FUNC_ARG. */
if (caps.mfg == TPM_MFG_STM) {
/* wolfTPM2_FirmwareUpgradeHash - invalid manifest size (10 bytes).
* Should fail with BAD_FUNC_ARG because manifest_sz must be
* exactly 177 (non-LMS) or 2697 (LMS). */
rc = wolfTPM2_FirmwareUpgradeHash(&dev, TPM_ALG_SHA384, NULL, 0,
dummy_manifest, sizeof(dummy_manifest), NULL, NULL);
AssertIntEQ(rc, BAD_FUNC_ARG);
}
#endif /* !WOLFTPM2_NO_WOLFCRYPT && WOLFSSL_SHA384 */
wolfTPM2_Cleanup(&dev);
printf("Test ST33 FW: %-40s Passed\n", "API Availability:");
}
#endif /* WOLFTPM_ST33 || WOLFTPM_AUTODETECT */
#endif /* WOLFTPM_FIRMWARE_UPGRADE */
static void test_wolfTPM2_GetRandom(void)
{
int rc;
WOLFTPM2_DEV dev;
WOLFTPM2_BUFFER rngData;
rc = wolfTPM2_Init(&dev, TPM2_IoCb, NULL);
AssertIntEQ(rc, 0);
/* Test arguments */
rc = wolfTPM2_GetRandom(NULL, rngData.buffer, sizeof(rngData.buffer));
AssertIntNE(rc, 0);
rc = wolfTPM2_GetRandom(&dev, NULL, sizeof(rngData.buffer));
AssertIntNE(rc, 0);
rc = wolfTPM2_GetRandom(&dev, rngData.buffer, 0);
AssertIntEQ(rc, 0);
/* Test success */
rc = wolfTPM2_GetRandom(&dev, rngData.buffer, sizeof(rngData.buffer));
AssertIntEQ(rc, 0);
wolfTPM2_Cleanup(&dev);
printf("Test TPM Wrapper: %-40s %s\n", "Get Random:",
rc == 0 ? "Passed" : "Failed");
}
static void test_wolfTPM2_HashFinish_BufferTooSmall(void)
{
#ifndef WOLFTPM2_NO_WOLFCRYPT
int rc;
WOLFTPM2_DEV dev;
WOLFTPM2_HASH hash;
byte digest[TPM_SHA256_DIGEST_SIZE];
word32 digestSz;
rc = wolfTPM2_Init(&dev, TPM2_IoCb, NULL);
AssertIntEQ(rc, 0);
XMEMSET(&hash, 0, sizeof(hash));
rc = wolfTPM2_HashStart(&dev, &hash, TPM_ALG_SHA256, NULL, 0);
AssertIntEQ(rc, 0);
rc = wolfTPM2_HashUpdate(&dev, &hash, (const byte*)"abc", 3);
AssertIntEQ(rc, 0);
/* Undersized buffer must be rejected, not silently truncated, and the
* required size reported back to the caller. */
digestSz = 1;
rc = wolfTPM2_HashFinish(&dev, &hash, digest, &digestSz);
AssertIntEQ(BUFFER_E, rc);
AssertIntEQ(TPM_SHA256_DIGEST_SIZE, (int)digestSz);
wolfTPM2_Cleanup(&dev);
printf("Test TPM Wrapper: %-40s Passed\n", "HashFinish BufferTooSmall:");
#else
printf("Test TPM Wrapper: %-40s Skipped\n", "HashFinish BufferTooSmall:");
#endif
}
static void test_TPM2_PCRSel(void)
{
int rc = 0;
TPML_PCR_SELECTION pcr;
byte pcrArray[PCR_SELECT_MAX];
word32 pcrArraySz;
XMEMSET(&pcr, 0, sizeof(pcr));
XMEMSET(pcrArray, 0, sizeof(pcrArray));
pcrArraySz = 0;
pcrArray[pcrArraySz++] = 1;
pcrArray[pcrArraySz++] = 2;
pcrArray[pcrArraySz++] = 3;
TPM2_SetupPCRSelArray(&pcr, TPM_ALG_SHA, pcrArray, pcrArraySz);
pcrArraySz = 0;
pcrArray[pcrArraySz++] = 4;
pcrArray[pcrArraySz++] = 5;
pcrArray[pcrArraySz++] = 6;
TPM2_SetupPCRSelArray(&pcr, TPM_ALG_SHA256, pcrArray, pcrArraySz);
if (pcr.count != 2 ||
pcr.pcrSelections[0].hash != TPM_ALG_SHA ||
pcr.pcrSelections[0].pcrSelect[0] != 0x0E ||
pcr.pcrSelections[1].hash != TPM_ALG_SHA256 ||
pcr.pcrSelections[1].pcrSelect[0] != 0x70
) {
rc = BAD_FUNC_ARG;
}
AssertIntEQ(rc, 0);
/* Test bad case - invalid PCR */
XMEMSET(&pcr, 0, sizeof(pcr));
pcrArray[0] = PCR_LAST+1;
TPM2_SetupPCRSelArray(&pcr, TPM_ALG_SHA256, pcrArray, 1);
if (pcr.count != 0) {
rc = BAD_FUNC_ARG;
}
AssertIntEQ(rc, 0);
/* Test bad case - too many hash algorithms */
XMEMSET(&pcr, 0, sizeof(pcr));
pcrArray[0] = 1;
TPM2_SetupPCRSelArray(&pcr, TPM_ALG_SHA, pcrArray, 1);
pcrArray[0] = 2;
TPM2_SetupPCRSelArray(&pcr, TPM_ALG_SHA256, pcrArray, 1);
pcrArray[0] = 3;
TPM2_SetupPCRSelArray(&pcr, TPM_ALG_SHA384, pcrArray, 1);
pcrArray[0] = 4;
TPM2_SetupPCRSelArray(&pcr, TPM_ALG_SHA512, pcrArray, 1);
pcrArray[0] = 5;
TPM2_SetupPCRSelArray(&pcr, TPM_ALG_SHA3_256, pcrArray, 1);
pcrArray[0] = 6;
TPM2_SetupPCRSelArray(&pcr, TPM_ALG_SHA3_384, pcrArray, 1);
pcrArray[0] = 7;
TPM2_SetupPCRSelArray(&pcr, TPM_ALG_SHA3_512, pcrArray, 1);
if (pcr.count != HASH_COUNT) {
rc = BAD_FUNC_ARG;
}
AssertIntEQ(rc, 0);
printf("Test TPM Wrapper: %-40s %s\n", "PCR Select Array:",
rc == 0 ? "Passed" : "Failed");
}
/* Test NULL input handling for policy commands (security fix) */
static void test_TPM2_Policy_NULL_Args(void)
{
int rc;
/* Test NULL input handling for policy commands */
rc = TPM2_PolicyPhysicalPresence(NULL);
AssertIntEQ(rc, BAD_FUNC_ARG);
rc = TPM2_PolicyAuthValue(NULL);
AssertIntEQ(rc, BAD_FUNC_ARG);
rc = TPM2_PolicyPassword(NULL);
AssertIntEQ(rc, BAD_FUNC_ARG);
printf("Test TPM2: %-40s Passed\n", "Policy NULL Args:");
}
static void test_wolfTPM2_PolicyAuthValue_AuthOffset(void)
{
#if !defined(WOLFTPM2_NO_WOLFCRYPT)
WOLFTPM2_DEV dev;
WOLFTPM2_SESSION session;
const byte testAuth[] = {0x11, 0x22, 0x33, 0x44};
int authDigestSz;
int i;
XMEMSET(&dev, 0, sizeof(dev));
XMEMSET(&session, 0, sizeof(session));
(void)wolfTPM2_Init(&dev, TPM2_IoCb, NULL);
/* Configure session with SHA-256 auth hash */
session.authHash = TPM_ALG_SHA256;
authDigestSz = TPM2_GetHashDigestSize(TPM_ALG_SHA256);
AssertIntEQ(authDigestSz, TPM_SHA256_DIGEST_SIZE);
/* Pre-fill the HMAC key region with a sentinel */
XMEMSET(session.handle.auth.buffer, 0xFF, authDigestSz);
/* Call PolicyAuthValue - ignore return (TPM command may fail without
* a real session handle, but auth buffer placement happens first) */
(void)wolfTPM2_PolicyAuthValue(&dev, &session, testAuth,
(int)sizeof(testAuth));
/* Verify auth.size = authDigestSz + authSz */
AssertIntEQ(session.handle.auth.size,
authDigestSz + (int)sizeof(testAuth));
/* Verify HMAC key slot [0..authDigestSz-1] is preserved (still 0xFF) */
for (i = 0; i < authDigestSz; i++) {
AssertIntEQ(session.handle.auth.buffer[i], 0xFF);
}
/* Verify auth placed at offset [authDigestSz..authDigestSz+authSz-1] */
AssertIntEQ(XMEMCMP(&session.handle.auth.buffer[authDigestSz],
testAuth, sizeof(testAuth)), 0);
/* Verify policyAuth flag is set */
AssertIntEQ(session.handle.policyAuth, 1);
wolfTPM2_Cleanup(&dev);
printf("Test TPM Wrapper: %-40s Passed\n", "PolicyAuthValue Offset:");
#endif
}
static void test_wolfTPM2_SetAuthHandle_PolicyAuthOffset(void)
{
#if !defined(WOLFTPM2_NO_WOLFCRYPT)
int rc;
WOLFTPM2_DEV dev;
WOLFTPM2_HANDLE handle;
int authDigestSz;
int i;
XMEMSET(&dev, 0, sizeof(dev));
XMEMSET(&handle, 0, sizeof(handle));
(void)wolfTPM2_Init(&dev, TPM2_IoCb, NULL);
/* Configure session 0 with SHA-256 auth hash and a non-PW session handle */
dev.session[0].authHash = TPM_ALG_SHA256;
dev.session[0].sessionHandle = 0x02000000; /* HMAC session handle */
authDigestSz = TPM2_GetHashDigestSize(TPM_ALG_SHA256);
AssertIntEQ(authDigestSz, TPM_SHA256_DIGEST_SIZE);
/* Pre-fill the HMAC key region with sentinel */
XMEMSET(dev.session[0].auth.buffer, 0xFF, authDigestSz);
/* Set up handle with policyAuth and auth data */
handle.policyAuth = 1;
handle.auth.size = 4;
handle.auth.buffer[0] = 0x11;
handle.auth.buffer[1] = 0x22;
handle.auth.buffer[2] = 0x33;
handle.auth.buffer[3] = 0x44;
handle.name.size = 2;
handle.name.name[0] = 0xAA;
handle.name.name[1] = 0xBB;
/* Test wolfTPM2_SetAuthHandle policyAuth branch */
rc = wolfTPM2_SetAuthHandle(&dev, 0, &handle);
AssertIntEQ(rc, TPM_RC_SUCCESS);
/* Verify auth.size = authDigestSz + authSz */
AssertIntEQ(dev.session[0].auth.size,
authDigestSz + (int)handle.auth.size);
/* Verify HMAC key slot [0..authDigestSz-1] preserved */
for (i = 0; i < authDigestSz; i++) {
AssertIntEQ(dev.session[0].auth.buffer[i], 0xFF);
}
/* Verify auth at offset [authDigestSz..] */
AssertIntEQ(XMEMCMP(&dev.session[0].auth.buffer[authDigestSz],
handle.auth.buffer, handle.auth.size), 0);
/* Now test wolfTPM2_SetAuthHandleName policyAuth branch */
XMEMSET(dev.session[0].auth.buffer, 0xEE, authDigestSz);
dev.session[0].auth.size = 0;
rc = wolfTPM2_SetAuthHandleName(&dev, 0, &handle);
AssertIntEQ(rc, TPM_RC_SUCCESS);
/* Verify auth.size = authDigestSz + authSz */
AssertIntEQ(dev.session[0].auth.size,
authDigestSz + (int)handle.auth.size);
/* Verify HMAC key slot [0..authDigestSz-1] preserved */
for (i = 0; i < authDigestSz; i++) {
AssertIntEQ(dev.session[0].auth.buffer[i], 0xEE);
}
/* Verify auth at offset [authDigestSz..] */
AssertIntEQ(XMEMCMP(&dev.session[0].auth.buffer[authDigestSz],
handle.auth.buffer, handle.auth.size), 0);
wolfTPM2_Cleanup(&dev);
printf("Test TPM Wrapper: %-40s Passed\n", "SetAuthHandle PolicyAuth:");
#endif
}
/* Verify wolfTPM2_StartSession enables encrypt/decrypt attributes for
* salted (tpmKey-only, bind == NULL) sessions when caller selects a
* symmetric algorithm. Per TPM 2.0 spec, salted sessions have valid
* shared-secret state for parameter encryption. */
static void test_wolfTPM2_StartSession_SaltedEncryptAttrs(void)
{
#if !defined(WOLFTPM2_NO_WOLFCRYPT)
int rc;
WOLFTPM2_DEV dev;
WOLFTPM2_KEY tpmKey;
WOLFTPM2_SESSION session;
TPMA_SESSION expected = TPMA_SESSION_decrypt | TPMA_SESSION_encrypt;
XMEMSET(&dev, 0, sizeof(dev));
XMEMSET(&tpmKey, 0, sizeof(tpmKey));
XMEMSET(&session, 0, sizeof(session));
/* Initialize so TPM2_GetNonceNoLock and dependent code paths have a
* valid context. Skip if no TPM is reachable. */
rc = wolfTPM2_Init(&dev, TPM2_IoCb, NULL);
if (rc != 0) {
printf("Test TPM Wrapper:\tStartSession salted enc attrs:\tSkipped\n");
return;
}
/* tpmKey with a non-NULL handle, no auth */
tpmKey.handle.hndl = 0x80000000;
/* The call will fail later (no real key with that handle) but the
* SetAuth path that sets sessionAttributes runs first. */
(void)wolfTPM2_StartSession(&dev, &session, &tpmKey, NULL,
TPM_SE_HMAC, TPM_ALG_CFB);
AssertIntEQ((int)(dev.session[0].sessionAttributes & expected),
(int)expected);
wolfTPM2_Cleanup(&dev);
printf("Test TPM Wrapper:\tStartSession salted enc attrs:\tPassed\n");
#endif
}
static void test_wolfTPM2_StartSession_ex_authHash(void)
{
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(WOLFSSL_SHA512)
int rc;
WOLFTPM2_DEV dev;
WOLFTPM2_SESSION session;
XMEMSET(&dev, 0, sizeof(dev));
XMEMSET(&session, 0, sizeof(session));
rc = wolfTPM2_Init(&dev, TPM2_IoCb, NULL);
if (rc != 0) {
printf("Test TPM Wrapper:\tStartSession_ex SHA512:\tSkipped\n");
return;
}
/* default (SHA-256) selected via TPM_ALG_NULL */
rc = wolfTPM2_StartSession_ex(&dev, &session, NULL, NULL,
TPM_SE_POLICY, TPM_ALG_NULL, TPM_ALG_NULL);
if (rc == TPM_RC_SUCCESS) {
AssertIntEQ(session.authHash, WOLFTPM2_WRAP_DIGEST);
wolfTPM2_UnloadHandle(&dev, &session.handle);
}
/* explicit SHA-512 selection */
rc = wolfTPM2_StartSession_ex(&dev, &session, NULL, NULL,
TPM_SE_POLICY, TPM_ALG_NULL, TPM_ALG_SHA512);
if (rc == TPM_RC_SUCCESS) {
AssertIntEQ(session.authHash, TPM_ALG_SHA512);
wolfTPM2_UnloadHandle(&dev, &session.handle);
printf("Test TPM Wrapper:\tStartSession_ex SHA512:\tPassed\n");
}
else {
printf("Test TPM Wrapper:\tStartSession_ex SHA512:\tSkipped\n");
}
/* a session hash weaker than the default is rejected (no TPM needed) */
#ifndef NO_SHA
if (TPM2_GetHashDigestSize(TPM_ALG_SHA1) <
TPM2_GetHashDigestSize(WOLFTPM2_WRAP_DIGEST)) {
rc = wolfTPM2_StartSession_ex(&dev, &session, NULL, NULL,
TPM_SE_POLICY, TPM_ALG_NULL, TPM_ALG_SHA1);
AssertIntEQ(rc, BAD_FUNC_ARG);
}
#endif
wolfTPM2_Cleanup(&dev);
#endif
}
static void test_wolfTPM2_PolicyHash(void)
{
#ifndef WOLFTPM2_NO_WOLFCRYPT
int rc;
byte digest[TPM_SHA256_DIGEST_SIZE];
byte digest0[TPM_SHA256_DIGEST_SIZE];
byte digestFirst[TPM_SHA256_DIGEST_SIZE];
word32 digestSz;
const byte input[] = {0x01, 0x02, 0x03, 0x04};
/* Test 1: cc=0 (no command code, used by PolicyRefMake) */
XMEMSET(digest, 0xAA, sizeof(digest));
digestSz = TPM_SHA256_DIGEST_SIZE;
rc = wolfTPM2_PolicyHash(TPM_ALG_SHA256, digest, &digestSz,
0, input, sizeof(input));
AssertIntEQ(rc, 0);
XMEMCPY(digest0, digest, digestSz);
/* Test 2: cc=TPM_CC_FIRST (0x11F boundary) - must differ from cc=0 */
XMEMSET(digest, 0xAA, sizeof(digest));
digestSz = TPM_SHA256_DIGEST_SIZE;
rc = wolfTPM2_PolicyHash(TPM_ALG_SHA256, digest, &digestSz,
TPM_CC_FIRST, input, sizeof(input));
AssertIntEQ(rc, 0);
XMEMCPY(digestFirst, digest, digestSz);
/* cc=0 and cc=TPM_CC_FIRST must produce different digests */
AssertIntNE(XMEMCMP(digest0, digestFirst, digestSz), 0);
/* Test 3: cc=TPM_CC_PolicyPCR (above boundary) - must differ from both */
XMEMSET(digest, 0xAA, sizeof(digest));
digestSz = TPM_SHA256_DIGEST_SIZE;
rc = wolfTPM2_PolicyHash(TPM_ALG_SHA256, digest, &digestSz,
TPM_CC_PolicyPCR, input, sizeof(input));
AssertIntEQ(rc, 0);
AssertIntNE(XMEMCMP(digest0, digest, digestSz), 0);
AssertIntNE(XMEMCMP(digestFirst, digest, digestSz), 0);
printf("Test TPM Wrapper: %-40s Passed\n", "PolicyHash:");
#else
printf("Test TPM Wrapper: %-40s Skipped\n", "PolicyHash:");
#endif
}
static void test_wolfTPM2_SensitiveToPrivate(void)
{
#ifdef WOLFTPM2_PRIVATE_IMPORT
int rc;
TPM2B_SENSITIVE sens;
TPM2B_PRIVATE priv;
TPM2B_NAME name;
TPM2B_DATA symSeed;
TPMT_SYM_DEF_OBJECT sym;
const byte expected[] = {
0x00, 0x20, 0x2b, 0x59, 0xc0, 0x69, 0xf6, 0x63,
0x7c, 0x2a, 0xe0, 0x62, 0xcf, 0x42, 0x37, 0x8b,
0x79, 0x5d, 0xb6, 0x61, 0x4f, 0x9f, 0x93, 0x38,
0x82, 0x06, 0x2e, 0x28, 0xbf, 0xd3, 0x5c, 0x82,
0x1c, 0x03, 0xb5, 0x90, 0x49, 0x7a, 0x93, 0x46,
0x31, 0x51, 0xe2, 0xdd, 0x4f, 0x0a, 0x22, 0x9b,
0x2e, 0xd7, 0x5d, 0xc6, 0xe3, 0x97, 0xf4, 0x75,
0xcf, 0xfd, 0xa9, 0xe9, 0xd3, 0xa4, 0x5f, 0x95,
0xa0, 0x70, 0x2f, 0x71, 0x6c, 0xb8, 0x90, 0x39,
0x32, 0x54, 0x91, 0x87, 0x34, 0x9b, 0xac, 0xef
};
/* Fixed test inputs */
XMEMSET(&sens, 0, sizeof(sens));
XMEMSET(&priv, 0, sizeof(priv));
XMEMSET(&name, 0, sizeof(name));
XMEMSET(&symSeed, 0, sizeof(symSeed));
XMEMSET(&sym, 0, sizeof(sym));
/* Set up a minimal sensitive area */
sens.sensitiveArea.sensitiveType = TPM_ALG_RSA;
sens.sensitiveArea.authValue.size = 4;
XMEMSET(sens.sensitiveArea.authValue.buffer, 0xAA, 4);
sens.sensitiveArea.seedValue.size = TPM_SHA256_DIGEST_SIZE;
XMEMSET(sens.sensitiveArea.seedValue.buffer, 0xBB,
TPM_SHA256_DIGEST_SIZE);
/* Set up a name (hash alg + digest) */
name.size = 2 + TPM_SHA256_DIGEST_SIZE;
/* name[0..1] = TPM_ALG_SHA256 big-endian */
name.name[0] = 0x00;
name.name[1] = 0x0B; /* TPM_ALG_SHA256 */
XMEMSET(&name.name[2], 0xCC, TPM_SHA256_DIGEST_SIZE);
/* Set up symmetric algorithm (AES-128-CFB) */
sym.algorithm = TPM_ALG_AES;
sym.keyBits.sym = 128;
sym.mode.sym = TPM_ALG_CFB;
/* Set up a symmetric seed (triggers outer wrap / KDFa) */
symSeed.size = TPM_SHA256_DIGEST_SIZE;
XMEMSET(symSeed.buffer, 0xDD, TPM_SHA256_DIGEST_SIZE);
/* Expected output - pins KDFa "STORAGE" and "INTEGRITY" labels.
* Bytes 0-1: integrity size (0x0020 = 32),
* Bytes 2-33: HMAC integrity (via "INTEGRITY" label KDFa),
* Bytes 34-79: AES-CFB encrypted sensitive (via "STORAGE" label KDFa) */
rc = wolfTPM2_SensitiveToPrivate(&sens, &priv,
TPM_ALG_SHA256, &name, NULL, &sym, &symSeed);
AssertIntEQ(rc, 0);
AssertIntEQ(priv.size, (int)sizeof(expected));
AssertIntEQ(XMEMCMP(priv.buffer, expected, sizeof(expected)), 0);
printf("Test TPM Wrapper: %-40s Passed\n", "SensitiveToPrivate:");
#else
printf("Test TPM Wrapper: %-40s Skipped\n", "SensitiveToPrivate:");
#endif
}
static void test_TPM2_KDFa_SessionLabels(void)
{
#ifndef WOLFTPM2_NO_WOLFCRYPT
int rc;
#define TEST_KDFA_LABEL_KEYSZ TPM_SHA256_DIGEST_SIZE
TPM2B_DATA keyIn = {
.size = 16,
.buffer = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10}
};
TPM2B_NONCE nonceTPM = {
.size = 16,
.buffer = {0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0}
};
TPM2B_NONCE nonceCaller = {
.size = 16,
.buffer = {0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8,
0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0}
};
const byte expATH[] = {
0x0d, 0x17, 0x5f, 0xf7, 0xac, 0xf9, 0x41, 0x9a,
0x73, 0x75, 0x7c, 0xa6, 0x42, 0x82, 0x49, 0x61,
0xa2, 0xc9, 0x72, 0xd9, 0x13, 0xdc, 0xbf, 0x72,
0x06, 0xe6, 0x73, 0xe7, 0x21, 0x5f, 0x99, 0x6a
};
const byte expSECRET[] = {
0x1a, 0xc4, 0xc1, 0x34, 0x78, 0x87, 0x67, 0x5e,
0x91, 0xd1, 0xa2, 0xcd, 0xcb, 0xac, 0xdb, 0x62,
0xed, 0x4e, 0xfe, 0x44, 0xed, 0x52, 0x34, 0x3b,
0xf1, 0x87, 0xfb, 0x8b, 0xa9, 0xec, 0x43, 0x59
};
const byte expDUPLICATE[] = {
0xa3, 0xe5, 0x57, 0xc6, 0x49, 0x4c, 0xe5, 0x4f,
0x45, 0xae, 0xf7, 0x19, 0x4d, 0x9e, 0x21, 0xa2,
0x91, 0xeb, 0x05, 0x2d, 0x43, 0x06, 0x9f, 0xfb,
0x69, 0x67, 0x1f, 0x99, 0x00, 0xb0, 0xcc, 0x39
};
byte key[TEST_KDFA_LABEL_KEYSZ];
/* Test "ATH" label (session key derivation, TPM 2.0 Part 1 s19.6.8) */
rc = TPM2_KDFa(TPM_ALG_SHA256, &keyIn, "ATH", &nonceTPM, &nonceCaller,
key, TEST_KDFA_LABEL_KEYSZ);
AssertIntEQ(TEST_KDFA_LABEL_KEYSZ, rc);
AssertIntEQ(XMEMCMP(key, expATH, sizeof(expATH)), 0);
/* Test "SECRET" label (salt encryption, TPM 2.0 Part 1 s19.6.8) */
rc = TPM2_KDFa(TPM_ALG_SHA256, &keyIn, "SECRET", &nonceTPM, &nonceCaller,
key, TEST_KDFA_LABEL_KEYSZ);
AssertIntEQ(TEST_KDFA_LABEL_KEYSZ, rc);
AssertIntEQ(XMEMCMP(key, expSECRET, sizeof(expSECRET)), 0);
/* Test "DUPLICATE" label (key import, TPM 2.0 Part 1 s23.3) */
rc = TPM2_KDFa(TPM_ALG_SHA256, &keyIn, "DUPLICATE", &nonceTPM, &nonceCaller,
key, TEST_KDFA_LABEL_KEYSZ);
AssertIntEQ(TEST_KDFA_LABEL_KEYSZ, rc);
AssertIntEQ(XMEMCMP(key, expDUPLICATE, sizeof(expDUPLICATE)), 0);
printf("Test TPM Wrapper: %-40s Passed\n", "KDFa Session Labels:");
#else
printf("Test TPM Wrapper: %-40s Skipped\n", "KDFa Session Labels:");
#endif
}
static void test_wolfTPM2_EncryptSecret(void)
{
int rc;
WOLFTPM2_DEV dev;
WOLFTPM2_KEY tpmKey;
TPM2B_DATA data;
TPM2B_ENCRYPTED_SECRET secret;
#if defined(WOLFTPM_PQC) && !defined(WOLFTPM2_NO_WOLFCRYPT) && \
(defined(WOLFSSL_HAVE_MLKEM) || defined(WOLFSSL_KYBER512) || \
defined(WOLFSSL_KYBER768) || defined(WOLFSSL_KYBER1024))
WOLFTPM2_KEY mlkemKey;
TPMT_PUBLIC mlkemPub;
#endif
XMEMSET(&tpmKey, 0, sizeof(tpmKey));
XMEMSET(&data, 0, sizeof(data));
XMEMSET(&secret, 0, sizeof(secret));
rc = wolfTPM2_Init(&dev, TPM2_IoCb, NULL);
AssertIntEQ(rc, 0);
/* Test NULL tpmKey returns success (unsalted session) */
rc = wolfTPM2_EncryptSecret(&dev, NULL, &data, &secret, "SECRET");
AssertIntEQ(rc, TPM_RC_SUCCESS);
/* Test NULL dev returns BAD_FUNC_ARG */
rc = wolfTPM2_EncryptSecret(NULL, &tpmKey, &data, &secret, "SECRET");
AssertIntEQ(rc, BAD_FUNC_ARG);
/* Test NULL data returns BAD_FUNC_ARG */
rc = wolfTPM2_EncryptSecret(&dev, &tpmKey, NULL, &secret, "SECRET");
AssertIntEQ(rc, BAD_FUNC_ARG);
/* Test NULL secret returns BAD_FUNC_ARG */
rc = wolfTPM2_EncryptSecret(&dev, &tpmKey, &data, NULL, "SECRET");
AssertIntEQ(rc, BAD_FUNC_ARG);
#if defined(WOLFTPM_PQC) && !defined(WOLFTPM2_NO_WOLFCRYPT) && \
(defined(WOLFSSL_HAVE_MLKEM) || defined(WOLFSSL_KYBER512) || \
defined(WOLFSSL_KYBER768) || defined(WOLFSSL_KYBER1024))
/* MLKEM path (v1.85 Part 1 Sec.24): caller encapsulates under the TPM's
* ML-KEM public key; the shared secret (32 bytes) becomes the session
* salt, the ciphertext (1088 bytes for MLKEM-768) goes on the wire. */
XMEMSET(&mlkemKey, 0, sizeof(mlkemKey));
XMEMSET(&mlkemPub, 0, sizeof(mlkemPub));
XMEMSET(&data, 0, sizeof(data));
XMEMSET(&secret, 0, sizeof(secret));
rc = wolfTPM2_GetKeyTemplate_MLKEM(&mlkemPub,
TPMA_OBJECT_decrypt | TPMA_OBJECT_fixedTPM |
TPMA_OBJECT_fixedParent | TPMA_OBJECT_sensitiveDataOrigin |
TPMA_OBJECT_userWithAuth, TPM_MLKEM_768);
AssertIntEQ(rc, TPM_RC_SUCCESS);
rc = wolfTPM2_CreatePrimaryKey(&dev, &mlkemKey, TPM_RH_OWNER,
&mlkemPub, NULL, 0);
if (rc == TPM_RC_VALUE || rc == TPM_RC_SCHEME ||
rc == TPM_RC_COMMAND_CODE || rc == (int)(RC_VER1 + 0x043)) {
printf("Test TPM Wrapper: %-40s Skipped (not supported)\n",
"EncryptSecret MLKEM:");
}
else {
AssertIntEQ(rc, 0);
rc = wolfTPM2_EncryptSecret(&dev, &mlkemKey, &data, &secret,
"SECRET");
AssertIntEQ(rc, 0);
AssertIntEQ(data.size, 32); /* MLKEM shared secret */
AssertIntEQ(secret.size, 1088); /* MLKEM-768 ciphertext */
printf("Test TPM Wrapper: %-40s Passed\n",
"EncryptSecret MLKEM:");
wolfTPM2_UnloadHandle(&dev, &mlkemKey.handle);
}
#endif
wolfTPM2_Cleanup(&dev);
printf("Test TPM Wrapper: %-40s %s\n", "EncryptSecret:",
rc == 0 || rc == BAD_FUNC_ARG ? "Passed" : "Failed");
}
static void test_wolfTPM2_Cleanup(void)
{
int rc;
WOLFTPM2_DEV dev;
/* Test arguments */
rc = wolfTPM2_Cleanup(NULL);