-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathwh_test_she.c
More file actions
1573 lines (1429 loc) · 59 KB
/
wh_test_she.c
File metadata and controls
1573 lines (1429 loc) · 59 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
/*
* Copyright (C) 2024 wolfSSL Inc.
*
* This file is part of wolfHSM.
*
* wolfHSM 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.
*
* wolfHSM 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 wolfHSM. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* test/wh_test_she.c
*
*/
#include <stdint.h>
#include <stdio.h> /* For printf */
#include <string.h> /* For memset, memcpy */
#include "wolfhsm/wh_settings.h"
#include "wolfhsm/wh_common.h"
#include "wolfhsm/wh_error.h"
#ifdef WOLFHSM_CFG_ENABLE_SERVER
#include "wolfhsm/wh_nvm.h"
#include "wolfhsm/wh_nvm_flash.h"
#include "wolfhsm/wh_flash_ramsim.h"
#include "wolfhsm/wh_server_keystore.h"
#endif
#include "wolfhsm/wh_comm.h"
#include "wolfhsm/wh_message.h"
#include "wolfhsm/wh_message_she.h"
#ifdef WOLFHSM_CFG_ENABLE_SERVER
#include "wolfhsm/wh_server.h"
#include "wolfhsm/wh_server_she.h"
#endif
#ifdef WOLFHSM_CFG_ENABLE_CLIENT
#include "wolfhsm/wh_client.h"
#endif
#include "wolfhsm/wh_transport_mem.h"
#ifdef WOLFHSM_CFG_SHE_EXTENSION
#include "wolfhsm/wh_she_common.h"
#include "wolfhsm/wh_she_crypto.h"
#ifdef WOLFHSM_CFG_ENABLE_CLIENT
#include "wolfhsm/wh_client_she.h"
#endif
#ifndef WOLFHSM_CFG_NO_CRYPTO
#include "wolfssl/wolfcrypt/settings.h"
#include "wolfssl/wolfcrypt/types.h"
#include "wolfssl/wolfcrypt/cmac.h"
#include "wh_test_common.h"
#if defined(WOLFHSM_CFG_TEST_POSIX)
#include <unistd.h> /* For sleep */
#include <pthread.h> /* For pthread_create/cancel/join/_t */
#include "port/posix/posix_transport_tcp.h"
#include "port/posix/posix_flash_file.h"
#endif
enum {
REQ_SIZE = 32,
RESP_SIZE = 64,
BUFFER_SIZE = sizeof(whTransportMemCsr) + sizeof(whCommHeader) +
WOLFHSM_CFG_COMM_DATA_LEN,
};
#define FLASH_RAM_SIZE (1024 * 1024) /* 1MB */
#define FLASH_SECTOR_SIZE (128 * 1024) /* 128KB */
#define FLASH_PAGE_SIZE (8) /* 8B */
#ifdef WOLFHSM_CFG_ENABLE_CLIENT
/* Helper function to destroy a SHE key so the unit tests don't
* leak NVM objects across invocations. Necessary, as SHE doesn't expose a
* destroy key API since SHE keys are supposed to be fixed hardware keys */
static int _destroySheKey(whClientContext* client, whNvmId clientSheKeyId)
{
int rc = 0;
int32_t serverRc = 0;
whNvmId id = WH_MAKE_KEYID(WH_KEYTYPE_SHE, client->comm->client_id, clientSheKeyId);
rc = wh_Client_NvmDestroyObjects(client, 1, &id, &serverRc);
if (rc == WH_ERROR_OK) {
rc = serverRc;
}
return rc;
}
int whTest_SheClientConfig(whClientConfig* config)
{
int ret = 0;
WC_RNG rng[1];
Cmac cmac[1];
whClientContext client[1] = {0};
uint8_t key[16] = {0};
uint32_t keySz = sizeof(key);
uint8_t iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
uint8_t plainText[64];
uint8_t cipherText[64];
uint8_t finalText[64];
/* secretKey and prngSeed are taken from SHE test vectors */
uint8_t sheUid[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
uint8_t secretKey[] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab,
0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
uint8_t prngSeed[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9,
0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
uint8_t zeros[WH_SHE_BOOT_MAC_PREFIX_LEN] = {0};
uint8_t bootloader[512];
uint8_t bootMacDigest[16] = {0};
uint8_t vectorMasterEcuKey[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
uint32_t digestSz = sizeof(bootMacDigest);
uint32_t bootloaderSz = sizeof(bootloader);
uint8_t vectorMessageOne[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x41};
uint8_t vectorMessageTwo[] = {0x2b, 0x11, 0x1e, 0x2d, 0x93, 0xf4, 0x86,
0x56, 0x6b, 0xcb, 0xba, 0x1d, 0x7f, 0x7a, 0x97, 0x97, 0xc9, 0x46, 0x43,
0xb0, 0x50, 0xfc, 0x5d, 0x4d, 0x7d, 0xe1, 0x4c, 0xff, 0x68, 0x22, 0x03,
0xc3};
uint8_t vectorMessageThree[] = {0xb9, 0xd7, 0x45, 0xe5, 0xac, 0xe7, 0xd4,
0x18, 0x60, 0xbc, 0x63, 0xc2, 0xb9, 0xf5, 0xbb, 0x46};
uint8_t vectorMessageFour[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x41, 0xb4, 0x72, 0xe8,
0xd8, 0x72, 0x7d, 0x70, 0xd5, 0x72, 0x95, 0xe7, 0x48, 0x49, 0xa2, 0x79,
0x17};
uint8_t vectorMessageFive[] = {0x82, 0x0d, 0x8d, 0x95, 0xdc, 0x11, 0xb4,
0x66, 0x88, 0x78, 0x16, 0x0c, 0xb2, 0xa4, 0xe2, 0x3e};
uint8_t vectorRawKey[] = {0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00};
uint8_t outMessageFour[sizeof(vectorMessageFour)];
uint8_t outMessageFive[sizeof(vectorMessageFive)];
uint8_t entropy[] = {0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e,
0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51};
uint8_t sreg;
uint8_t messageOne[WH_SHE_M1_SZ];
uint8_t messageTwo[WH_SHE_M2_SZ];
uint8_t messageThree[WH_SHE_M3_SZ];
uint8_t messageFour[WH_SHE_M4_SZ];
uint8_t messageFive[WH_SHE_M5_SZ];
uint32_t outClientId = 0;
uint32_t outServerId = 0;
const uint32_t SHE_TEST_VECTOR_KEY_ID = 4;
if (config == NULL) {
return WH_ERROR_BADARGS;
}
WH_TEST_RETURN_ON_FAIL(wh_Client_Init(client, config));
WH_TEST_RETURN_ON_FAIL(wh_Client_CommInit(client, &outClientId, &outServerId));
{
int32_t server_rc = 0;
whNvmId avail_objects = 0;
whNvmId reclaim_objects = 0;
uint32_t avail_size = 0;
uint32_t reclaim_size = 0;
WH_TEST_RETURN_ON_FAIL(
ret = wh_Client_NvmGetAvailable(client, &server_rc, &avail_size,
&avail_objects, &reclaim_size,
&reclaim_objects));
WH_TEST_DEBUG_PRINT("PRE-SHE TEST: NvmGetAvailable:%d, server_rc:%d avail_size:%d "
"avail_objects:%d, reclaim_size:%d reclaim_objects:%d\n",
ret, (int)server_rc, (int)avail_size, (int)avail_objects,
(int)reclaim_size, (int)reclaim_objects);
}
/* generate a new cmac key */
if ((ret = wc_InitRng_ex(rng, NULL, WH_DEV_ID)) != 0) {
WH_ERROR_PRINT("Failed to wc_InitRng_ex %d\n", ret);
goto exit;
}
if ((ret = wc_RNG_GenerateBlock(rng, key, sizeof(key))) != 0) {
WH_ERROR_PRINT("Failed to wc_RNG_GenerateBlock %d\n", ret);
goto exit;
}
/* generate a fake bootloader */
if ((ret = wc_RNG_GenerateBlock(rng, bootloader, sizeof(bootloader))) != 0) {
WH_ERROR_PRINT("Failed to wc_RNG_GenerateBlock %d\n", ret);
goto exit;
}
/* Done generating test data, free RNG */
wc_FreeRng(rng);
/* cmac 0..0 | size | bootloader */
if ((ret = wc_InitCmac(cmac, key, sizeof(key), WC_CMAC_AES, NULL)) != 0) {
WH_ERROR_PRINT("Failed to wc_InitCmac %d\n", ret);
goto exit;
}
if ((ret = wc_CmacUpdate(cmac, zeros, sizeof(zeros))) != 0) {
WH_ERROR_PRINT("Failed to wc_CmacUpdate %d\n", ret);
goto exit;
}
if ((ret = wc_CmacUpdate(cmac, (uint8_t*)&bootloaderSz, sizeof(bootloaderSz))) != 0) {
WH_ERROR_PRINT("Failed to wc_CmacUpdate %d\n", ret);
goto exit;
}
if ((ret = wc_CmacUpdate(cmac, bootloader, sizeof(bootloader))) != 0) {
WH_ERROR_PRINT("Failed to wc_CmacUpdate %d\n", ret);
goto exit;
}
digestSz = AES_BLOCK_SIZE;
if ((ret = wc_CmacFinal(cmac, bootMacDigest, (word32*)&digestSz)) != 0) {
WH_ERROR_PRINT("Failed to wc_CmacFinal %d\n", ret);
goto exit;
}
/* store cmac key */
if ((ret = wh_Client_ShePreProgramKey(client, WH_SHE_BOOT_MAC_KEY_ID, 0, key, sizeof(key))) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_ShePreProgramKey %d\n", ret);
goto exit;
}
/* store cmac digest */
if ((ret = wh_Client_ShePreProgramKey(client, WH_SHE_BOOT_MAC, 0, bootMacDigest, sizeof(bootMacDigest))) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_ShePreProgramKey %d\n", ret);
goto exit;
}
/* set the she uid */
if ((ret = wh_Client_SheSetUid(client, sheUid, sizeof(sheUid))) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheSetUid %d\n", ret);
goto exit;
}
/* verify bootloader */
if ((ret = wh_Client_SheSecureBoot(client, bootloader, bootloaderSz)) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheSecureBoot %d\n", ret);
goto exit;
}
/* get status */
if ((ret = wh_Client_SheGetStatus(client, &sreg)) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheGetStatus %d\n", ret);
goto exit;
}
/* verify bootOk, bootFinished and secureBoot */
if ((sreg & WH_SHE_SREG_BOOT_OK) == 0 ||
(sreg & WH_SHE_SREG_BOOT_FINISHED) == 0 ||
(sreg & WH_SHE_SREG_SECURE_BOOT) == 0) {
ret = WH_ERROR_ABORTED;
WH_ERROR_PRINT("Failed to secureBoot with SHE CMAC\n");
goto exit;
}
WH_TEST_PRINT("SHE secure boot SUCCESS\n");
/* load the secret key using pre program */
if ((ret = wh_Client_ShePreProgramKey(client, WH_SHE_SECRET_KEY_ID, 0, secretKey, sizeof(secretKey))) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_ShePreProgramKey %d\n", ret);
goto exit;
}
/* load the prng seed using pre program */
if ((ret = wh_Client_ShePreProgramKey(client, WH_SHE_PRNG_SEED_ID, 0, prngSeed, sizeof(prngSeed))) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_ShePreProgramKey %d\n", ret);
goto exit;
}
/* load the vector master ecu key */
if ((ret = wh_She_GenerateLoadableKey(WH_SHE_MASTER_ECU_KEY_ID, WH_SHE_SECRET_KEY_ID, 1, 0, sheUid, vectorMasterEcuKey, secretKey, messageOne, messageTwo, messageThree, messageFour, messageFive)) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheGenerateLoadableKey %d\n", ret);
goto exit;
}
if ((ret = wh_Client_SheLoadKey(client, messageOne, messageTwo, messageThree, outMessageFour, outMessageFive)) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheLoadKey %d\n", ret);
goto exit;
}
/* verify that our helper function output matches the vector */
if ((ret = wh_She_GenerateLoadableKey(SHE_TEST_VECTOR_KEY_ID, WH_SHE_MASTER_ECU_KEY_ID, 1, 0, sheUid, vectorRawKey, vectorMasterEcuKey, messageOne, messageTwo, messageThree, messageFour, messageFive)) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheGenerateLoadableKey %d\n", ret);
goto exit;
}
if (memcmp(messageOne, vectorMessageOne, sizeof(vectorMessageOne)) != 0 ||
memcmp(messageTwo, vectorMessageTwo, sizeof(vectorMessageTwo)) != 0 ||
memcmp(messageThree, vectorMessageThree, sizeof(vectorMessageThree)) != 0 ||
memcmp(messageFour, vectorMessageFour, sizeof(vectorMessageFour)) != 0 ||
memcmp(messageFive, vectorMessageFive, sizeof(vectorMessageFive)) != 0) {
ret = WH_ERROR_ABORTED;
WH_ERROR_PRINT("Failed to generate a loadable key to match the vector\n");
goto exit;
}
WH_TEST_PRINT("SHE wh_SheGenerateLoadableKey SUCCESS\n");
/* test CMD_LOAD_KEY with test vector */
if ((ret = wh_Client_SheLoadKey(client, vectorMessageOne, vectorMessageTwo, vectorMessageThree, outMessageFour, outMessageFive)) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheLoadKey %d\n", ret);
goto exit;
}
if (memcmp(outMessageFour, vectorMessageFour, sizeof(vectorMessageFour))
!= 0 || memcmp(outMessageFive, vectorMessageFive,
sizeof(vectorMessageFive)) != 0) {
ret = WH_ERROR_ABORTED;
WH_ERROR_PRINT("wh_Client_SheLoadKey FAILED TO MATCH\n");
goto exit;
}
WH_TEST_PRINT("SHE LOAD KEY SUCCESS\n");
if ((ret = wh_Client_SheInitRnd(client)) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheInitRnd %d\n", ret);
goto exit;
}
if ((ret = wh_Client_SheRnd(client, key, &keySz)) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheInitRnd %d\n", ret);
goto exit;
}
if ((ret = wh_Client_SheExtendSeed(client, entropy, sizeof(entropy))) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheExtendSeed %d\n", ret);
goto exit;
}
WH_TEST_PRINT("SHE RND SUCCESS\n");
if ((ret = wh_Client_SheLoadPlainKey(client, key, sizeof(key))) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheLoadPlainKey %d\n", ret);
goto exit;
}
if ((ret = wh_Client_SheEncEcb(client, WH_SHE_RAM_KEY_ID, plainText, cipherText, sizeof(plainText))) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheEncEcb %d\n", ret);
goto exit;
}
if ((ret = wh_Client_SheExportRamKey(client, messageOne, messageTwo, messageThree, messageFour, messageFive)) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheExportRamKey %d\n", ret);
goto exit;
}
if ((ret = wh_Client_SheLoadKey(client, messageOne, messageTwo, messageThree, messageFour, messageFive)) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheLoadKey %d\n", ret);
goto exit;
}
if ((ret = wh_Client_SheDecEcb(client, WH_SHE_RAM_KEY_ID, cipherText, finalText, sizeof(cipherText))) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheEncEcb %d\n", ret);
goto exit;
}
if (memcmp(finalText, plainText, sizeof(plainText)) != 0) {
ret = WH_ERROR_ABORTED;
WH_ERROR_PRINT("SHE ECB FAILED TO MATCH\n");
goto exit;
}
WH_TEST_PRINT("SHE ECB SUCCESS\n");
if ((ret = wh_Client_SheEncCbc(client, WH_SHE_RAM_KEY_ID, iv, sizeof(iv), plainText, cipherText, sizeof(plainText))) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheEncCbc %d\n", ret);
goto exit;
}
if ((ret = wh_Client_SheDecCbc(client, WH_SHE_RAM_KEY_ID, iv, sizeof(iv), cipherText, finalText, sizeof(cipherText))) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheDecCbc %d\n", ret);
goto exit;
}
if (memcmp(finalText, plainText, sizeof(plainText)) != 0) {
ret = WH_ERROR_ABORTED;
WH_ERROR_PRINT("SHE CBC FAILED TO MATCH\n");
goto exit;
}
WH_TEST_PRINT("SHE CBC SUCCESS\n");
if ((ret = wh_Client_SheGenerateMac(client, WH_SHE_RAM_KEY_ID, plainText, sizeof(plainText), cipherText, sizeof(cipherText))) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheGenerateMac %d\n", ret);
goto exit;
}
if ((ret = wh_Client_SheVerifyMac(client, WH_SHE_RAM_KEY_ID, plainText, sizeof(plainText), cipherText, sizeof(cipherText), &sreg)) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheVerifyMac %d\n", ret);
goto exit;
}
if (sreg != 0) {
ret = WH_ERROR_ABORTED;
WH_ERROR_PRINT("SHE CMAC FAILED TO VERIFY\n");
goto exit;
}
/* destroy "pre-programmed" keys so we don't leak NVM */
if ((ret = _destroySheKey(client, WH_SHE_BOOT_MAC_KEY_ID)) != 0) {
WH_ERROR_PRINT("Failed to _destroySheKey, ret=%d\n", ret);
goto exit;
}
if ((ret = _destroySheKey(client, WH_SHE_BOOT_MAC)) != 0) {
WH_ERROR_PRINT("Failed to _destroySheKey, ret=%d\n", ret);
goto exit;
}
if ((ret = _destroySheKey(client, WH_SHE_SECRET_KEY_ID)) != 0) {
WH_ERROR_PRINT("Failed to _destroySheKey, ret=%d\n", ret);
goto exit;
}
if ((ret = _destroySheKey(client, WH_SHE_PRNG_SEED_ID)) != 0) {
WH_ERROR_PRINT("Failed to _destroySheKey, ret=%d\n", ret);
goto exit;
}
if ((ret = _destroySheKey(client, WH_SHE_MASTER_ECU_KEY_ID)) != 0) {
WH_ERROR_PRINT("Failed to _destroySheKey, ret=%d\n", ret);
goto exit;
}
if ((ret = _destroySheKey(client, SHE_TEST_VECTOR_KEY_ID)) != 0) {
WH_ERROR_PRINT("Failed to _destroySheKey, ret=%d\n", ret);
goto exit;
}
WH_TEST_PRINT("SHE CMAC SUCCESS\n");
{
int32_t server_rc = 0;
whNvmId avail_objects = 0;
whNvmId reclaim_objects = 0;
uint32_t avail_size = 0;
uint32_t reclaim_size = 0;
WH_TEST_RETURN_ON_FAIL(
ret = wh_Client_NvmGetAvailable(client, &server_rc, &avail_size,
&avail_objects, &reclaim_size,
&reclaim_objects));
WH_TEST_DEBUG_PRINT("POST-SHE TEST: NvmGetAvailable:%d, server_rc:%d avail_size:%d "
"avail_objects:%d, reclaim_size:%d reclaim_objects:%d\n",
ret, (int)server_rc, (int)avail_size, (int)avail_objects,
(int)reclaim_size, (int)reclaim_objects);
}
exit:
/* Tell server to close */
WH_TEST_RETURN_ON_FAIL(wh_Client_CommClose(client));
if (ret == 0) {
WH_TEST_RETURN_ON_FAIL(wh_Client_Cleanup(client));
}
else {
wh_Client_Cleanup(client);
}
return ret;
}
#if defined(WOLFHSM_CFG_TEST_POSIX) && defined(WOLFHSM_CFG_ENABLE_CLIENT) && \
defined(WOLFHSM_CFG_ENABLE_SERVER)
static int whTest_SheClientConfigBoundarySecureBoot(whClientConfig* config)
{
int ret = 0;
WC_RNG rng[1];
Cmac cmac[1];
whClientContext client[1] = {0};
uint8_t key[16] = {0};
uint8_t zeros[WH_SHE_BOOT_MAC_PREFIX_LEN] = {0};
uint8_t sheUid[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
uint8_t bootMacDigest[16] = {0};
uint8_t sreg = 0;
uint8_t bootloaderBoundary[WOLFHSM_CFG_COMM_DATA_LEN -
sizeof(whMessageShe_SecureBootUpdateRequest)];
uint32_t digestSz = sizeof(bootMacDigest);
uint32_t bootloaderSz;
uint32_t serverCommDataLen = WOLFHSM_CFG_COMM_DATA_LEN;
uint32_t maxBoundaryUpdateChunk =
WOLFHSM_CFG_COMM_DATA_LEN -
sizeof(whMessageShe_SecureBootUpdateRequest);
uint32_t outClientId = 0;
uint32_t outServerId = 0;
if (config == NULL) {
return WH_ERROR_BADARGS;
}
WH_TEST_RETURN_ON_FAIL(wh_Client_Init(client, config));
WH_TEST_RETURN_ON_FAIL(
wh_Client_CommInit(client, &outClientId, &outServerId));
WH_TEST_RETURN_ON_FAIL(wh_Client_CommInfo(
client, NULL, NULL, &serverCommDataLen, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL));
if (serverCommDataLen <= sizeof(whMessageShe_SecureBootUpdateRequest)) {
WH_ERROR_PRINT("Invalid server cfg_comm_data_len %u\n",
(unsigned int)serverCommDataLen);
ret = WH_ERROR_ABORTED;
goto exit_boundary;
}
if (serverCommDataLen < WOLFHSM_CFG_COMM_DATA_LEN) {
maxBoundaryUpdateChunk =
serverCommDataLen - sizeof(whMessageShe_SecureBootUpdateRequest);
}
bootloaderSz = maxBoundaryUpdateChunk;
if ((ret = wc_InitRng_ex(rng, NULL, WH_DEV_ID)) != 0) {
WH_ERROR_PRINT("Failed to wc_InitRng_ex %d\n", ret);
goto exit_boundary;
}
if ((ret = wc_RNG_GenerateBlock(rng, key, sizeof(key))) != 0) {
WH_ERROR_PRINT("Failed to wc_RNG_GenerateBlock %d\n", ret);
goto exit_boundary;
}
if ((ret = wc_RNG_GenerateBlock(rng, bootloaderBoundary,
maxBoundaryUpdateChunk)) != 0) {
WH_ERROR_PRINT("Failed to wc_RNG_GenerateBlock %d\n", ret);
goto exit_boundary;
}
wc_FreeRng(rng);
if ((ret = wc_InitCmac(cmac, key, sizeof(key), WC_CMAC_AES, NULL)) != 0) {
WH_ERROR_PRINT("Failed to wc_InitCmac %d\n", ret);
goto exit_boundary;
}
if ((ret = wc_CmacUpdate(cmac, zeros, sizeof(zeros))) != 0) {
WH_ERROR_PRINT("Failed to wc_CmacUpdate %d\n", ret);
goto exit_boundary;
}
if ((ret = wc_CmacUpdate(cmac, (uint8_t*)&bootloaderSz,
sizeof(bootloaderSz))) != 0) {
WH_ERROR_PRINT("Failed to wc_CmacUpdate %d\n", ret);
goto exit_boundary;
}
if ((ret = wc_CmacUpdate(cmac, bootloaderBoundary, bootloaderSz)) != 0) {
WH_ERROR_PRINT("Failed to wc_CmacUpdate %d\n", ret);
goto exit_boundary;
}
digestSz = AES_BLOCK_SIZE;
if ((ret = wc_CmacFinal(cmac, bootMacDigest, (word32*)&digestSz)) != 0) {
WH_ERROR_PRINT("Failed to wc_CmacFinal %d\n", ret);
goto exit_boundary;
}
if ((ret = wh_Client_ShePreProgramKey(client, WH_SHE_BOOT_MAC_KEY_ID, 0,
key, sizeof(key))) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_ShePreProgramKey %d\n", ret);
goto exit_boundary;
}
if ((ret = wh_Client_ShePreProgramKey(client, WH_SHE_BOOT_MAC, 0,
bootMacDigest,
sizeof(bootMacDigest))) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_ShePreProgramKey %d\n", ret);
goto exit_boundary;
}
if ((ret = wh_Client_SheSetUid(client, sheUid, sizeof(sheUid))) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheSetUid %d\n", ret);
goto exit_boundary;
}
if ((ret = wh_Client_SheSecureBoot(client, bootloaderBoundary,
bootloaderSz)) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheSecureBoot boundary %d\n", ret);
goto exit_boundary;
}
if ((ret = wh_Client_SheGetStatus(client, &sreg)) != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SheGetStatus %d\n", ret);
goto exit_boundary;
}
if ((sreg & WH_SHE_SREG_BOOT_OK) == 0 ||
(sreg & WH_SHE_SREG_BOOT_FINISHED) == 0 ||
(sreg & WH_SHE_SREG_SECURE_BOOT) == 0) {
ret = WH_ERROR_ABORTED;
WH_ERROR_PRINT("Failed secureBoot boundary with SHE CMAC\n");
goto exit_boundary;
}
WH_TEST_PRINT("SHE secure boot boundary SUCCESS\n");
if ((ret = _destroySheKey(client, WH_SHE_BOOT_MAC_KEY_ID)) != 0) {
WH_ERROR_PRINT("Failed to _destroySheKey, ret=%d\n", ret);
goto exit_boundary;
}
if ((ret = _destroySheKey(client, WH_SHE_BOOT_MAC)) != 0) {
WH_ERROR_PRINT("Failed to _destroySheKey, ret=%d\n", ret);
goto exit_boundary;
}
exit_boundary:
/* Tell server to close */
WH_TEST_RETURN_ON_FAIL(wh_Client_CommClose(client));
if (ret == 0) {
WH_TEST_RETURN_ON_FAIL(wh_Client_Cleanup(client));
}
else {
wh_Client_Cleanup(client);
}
return ret;
}
#endif /* WOLFHSM_CFG_TEST_POSIX && WOLFHSM_CFG_ENABLE_CLIENT && \
WOLFHSM_CFG_ENABLE_SERVER */
#if defined(WOLFHSM_CFG_TEST_POSIX) && \
defined(WOLFHSM_CFG_ENABLE_CLIENT) && \
defined(WOLFHSM_CFG_ENABLE_SERVER)
/* Test that a key with WH_SHE_FLAG_WRITE_PROTECT cannot be overwritten
* via SHE LoadKey, and that ERC_WRITE_PROTECTED is returned */
static int whTest_SheWriteProtect(whClientConfig* config)
{
int ret = 0;
WC_RNG rng[1];
Cmac cmac[1];
whClientContext client[1] = {0};
uint8_t sheUid[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01};
uint8_t secretKey[] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2,
0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf,
0x4f, 0x3c};
uint8_t rawKey[] = {0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09,
0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02,
0x01, 0x00};
uint8_t zeros[WH_SHE_BOOT_MAC_PREFIX_LEN] = {0};
uint8_t bootMacKey[WH_SHE_KEY_SZ] = {0};
uint8_t bootloader[512];
uint8_t bootMacDigest[WH_SHE_KEY_SZ] = {0};
uint32_t digestSz = sizeof(bootMacDigest);
uint32_t bootloaderSz = sizeof(bootloader);
uint8_t messageOne[WH_SHE_M1_SZ];
uint8_t messageTwo[WH_SHE_M2_SZ];
uint8_t messageThree[WH_SHE_M3_SZ];
uint8_t messageFour[WH_SHE_M4_SZ];
uint8_t messageFive[WH_SHE_M5_SZ];
uint32_t outClientId = 0;
uint32_t outServerId = 0;
const uint32_t WP_TEST_KEY_ID = 4;
if (config == NULL) {
return WH_ERROR_BADARGS;
}
WH_TEST_RETURN_ON_FAIL(
wh_Client_Init(client, config));
WH_TEST_RETURN_ON_FAIL(
wh_Client_CommInit(client, &outClientId, &outServerId));
/* generate boot MAC key and fake bootloader */
if ((ret = wc_InitRng_ex(rng, NULL, WH_DEV_ID)) != 0) {
WH_ERROR_PRINT(
"Failed to wc_InitRng_ex %d\n", ret);
goto exit_wp;
}
if ((ret = wc_RNG_GenerateBlock(
rng, bootMacKey, sizeof(bootMacKey))) != 0) {
WH_ERROR_PRINT(
"Failed to wc_RNG_GenerateBlock %d\n", ret);
wc_FreeRng(rng);
goto exit_wp;
}
if ((ret = wc_RNG_GenerateBlock(
rng, bootloader, sizeof(bootloader))) != 0) {
WH_ERROR_PRINT(
"Failed to wc_RNG_GenerateBlock %d\n", ret);
wc_FreeRng(rng);
goto exit_wp;
}
wc_FreeRng(rng);
/* compute boot MAC digest: CMAC(0..0 | size | bootloader) */
if ((ret = wc_InitCmac(cmac, bootMacKey,
sizeof(bootMacKey), WC_CMAC_AES, NULL)) != 0) {
WH_ERROR_PRINT(
"Failed to wc_InitCmac %d\n", ret);
goto exit_wp;
}
if ((ret = wc_CmacUpdate(
cmac, zeros, sizeof(zeros))) != 0) {
WH_ERROR_PRINT(
"Failed to wc_CmacUpdate %d\n", ret);
goto exit_wp;
}
if ((ret = wc_CmacUpdate(cmac,
(uint8_t*)&bootloaderSz,
sizeof(bootloaderSz))) != 0) {
WH_ERROR_PRINT(
"Failed to wc_CmacUpdate %d\n", ret);
goto exit_wp;
}
if ((ret = wc_CmacUpdate(
cmac, bootloader, sizeof(bootloader))) != 0) {
WH_ERROR_PRINT(
"Failed to wc_CmacUpdate %d\n", ret);
goto exit_wp;
}
digestSz = AES_BLOCK_SIZE;
if ((ret = wc_CmacFinal(cmac, bootMacDigest,
(word32*)&digestSz)) != 0) {
WH_ERROR_PRINT(
"Failed to wc_CmacFinal %d\n", ret);
goto exit_wp;
}
/* pre-program boot MAC key and digest for secure boot */
if ((ret = wh_Client_ShePreProgramKey(
client, WH_SHE_BOOT_MAC_KEY_ID, 0,
bootMacKey, sizeof(bootMacKey))) != 0) {
WH_ERROR_PRINT(
"Failed to pre-program boot MAC key %d\n", ret);
goto exit_wp;
}
if ((ret = wh_Client_ShePreProgramKey(
client, WH_SHE_BOOT_MAC, 0,
bootMacDigest, sizeof(bootMacDigest))) != 0) {
WH_ERROR_PRINT(
"Failed to pre-program boot MAC digest %d\n",
ret);
goto exit_wp;
}
/* set the SHE UID */
if ((ret = wh_Client_SheSetUid(
client, sheUid, sizeof(sheUid))) != 0) {
WH_ERROR_PRINT(
"Failed to wh_Client_SheSetUid %d\n", ret);
goto exit_wp;
}
/* secure boot must succeed before SHE LoadKey is allowed */
if ((ret = wh_Client_SheSecureBoot(
client, bootloader, bootloaderSz)) != 0) {
WH_ERROR_PRINT(
"Failed to wh_Client_SheSecureBoot %d\n", ret);
goto exit_wp;
}
/* pre-program the secret key as auth key */
if ((ret = wh_Client_ShePreProgramKey(
client, WH_SHE_SECRET_KEY_ID, 0,
secretKey, sizeof(secretKey))) != 0) {
WH_ERROR_PRINT(
"Failed to pre-program secret key %d\n", ret);
goto exit_wp;
}
/* pre-program the target key WITH write protect flag */
if ((ret = wh_Client_ShePreProgramKey(
client, WP_TEST_KEY_ID,
WH_SHE_FLAG_WRITE_PROTECT,
rawKey, sizeof(rawKey))) != 0) {
WH_ERROR_PRINT(
"Failed to pre-program write-protected key %d\n",
ret);
goto exit_wp;
}
/* generate loadable key messages for the protected slot */
if ((ret = wh_She_GenerateLoadableKey(
WP_TEST_KEY_ID, WH_SHE_SECRET_KEY_ID,
1, 0, sheUid, rawKey, secretKey,
messageOne, messageTwo, messageThree,
messageFour, messageFive)) != 0) {
WH_ERROR_PRINT(
"Failed to generate loadable key %d\n", ret);
goto exit_wp;
}
/* attempt to load key into the write-protected slot */
ret = wh_Client_SheLoadKey(client, messageOne, messageTwo,
messageThree, messageFour,
messageFive);
if (ret != WH_SHE_ERC_WRITE_PROTECTED) {
WH_ERROR_PRINT(
"Expected WH_SHE_ERC_WRITE_PROTECTED, got %d\n",
ret);
ret = WH_ERROR_ABORTED;
goto exit_wp;
}
/* load succeeded (returned the expected error) */
ret = 0;
WH_TEST_PRINT("SHE write protect test SUCCESS\n");
/* destroy test keys to prevent NVM leaks */
if ((ret = _destroySheKey(
client, WH_SHE_BOOT_MAC_KEY_ID)) != 0) {
WH_ERROR_PRINT(
"Failed to _destroySheKey, ret=%d\n", ret);
goto exit_wp;
}
if ((ret = _destroySheKey(
client, WH_SHE_BOOT_MAC)) != 0) {
WH_ERROR_PRINT(
"Failed to _destroySheKey, ret=%d\n", ret);
goto exit_wp;
}
if ((ret = _destroySheKey(
client, WH_SHE_SECRET_KEY_ID)) != 0) {
WH_ERROR_PRINT(
"Failed to _destroySheKey, ret=%d\n", ret);
goto exit_wp;
}
if ((ret = _destroySheKey(client, WP_TEST_KEY_ID)) != 0) {
WH_ERROR_PRINT(
"Failed to _destroySheKey, ret=%d\n", ret);
goto exit_wp;
}
exit_wp:
WH_TEST_RETURN_ON_FAIL(wh_Client_CommClose(client));
if (ret == 0) {
WH_TEST_RETURN_ON_FAIL(wh_Client_Cleanup(client));
}
else {
wh_Client_Cleanup(client);
}
return ret;
}
#endif /* WOLFHSM_CFG_TEST_POSIX && WOLFHSM_CFG_ENABLE_CLIENT && \
WOLFHSM_CFG_ENABLE_SERVER */
#endif /* WOLFHSM_CFG_ENABLE_CLIENT */
#ifdef WOLFHSM_CFG_ENABLE_SERVER
int whTest_SheServerConfig(whServerConfig* config)
{
whServerContext server[1] = {0};
whCommConnected am_connected = WH_COMM_CONNECTED;
int ret = 0;
if (config == NULL) {
return WH_ERROR_BADARGS;
}
WH_TEST_RETURN_ON_FAIL(wh_Server_Init(server, config));
WH_TEST_RETURN_ON_FAIL(wh_Server_SetConnected(server, am_connected));
while(am_connected == WH_COMM_CONNECTED) {
ret = wh_Server_HandleRequestMessage(server);
if ((ret != WH_ERROR_NOTREADY) &&
(ret != WH_ERROR_OK)) {
WH_ERROR_PRINT("Failed to wh_Server_HandleRequestMessage: %d\n", ret);
break;
}
wh_Server_GetConnected(server, &am_connected);
}
if ((ret == 0) || (ret == WH_ERROR_NOTREADY)){
WH_TEST_RETURN_ON_FAIL(wh_Server_Cleanup(server));
} else {
ret = wh_Server_Cleanup(server);
}
return ret;
}
#endif /* WOLFHSM_CFG_ENABLE_SERVER */
#if defined(WOLFHSM_CFG_TEST_POSIX) && defined(WOLFHSM_CFG_ENABLE_CLIENT) && \
!defined(WOLFHSM_CFG_TEST_CLIENT_ONLY)
typedef int (*whTestSheClientFn)(whClientConfig* config);
typedef struct {
whClientConfig* clientConfig;
whTestSheClientFn clientFn;
} whTestSheClientTaskCtx;
static void* _whClientTask(void* cf)
{
whTestSheClientTaskCtx* ctx = (whTestSheClientTaskCtx*)cf;
WH_TEST_ASSERT(0 == ctx->clientFn(ctx->clientConfig));
return NULL;
}
#endif /* WOLFHSM_CFG_TEST_POSIX && WOLFHSM_CFG_ENABLE_CLIENT && \
!defined(WOLFHSM_CFG_TEST_CLIENT_ONLY_TCP) */
#if defined(WOLFHSM_CFG_TEST_POSIX) && defined(WOLFHSM_CFG_ENABLE_SERVER)
static void* _whServerTask(void* cf)
{
WH_TEST_ASSERT(0 == whTest_SheServerConfig(cf));
return NULL;
}
#endif /* WOLFHSM_CFG_TEST_POSIX && WOLFHSM_CFG_ENABLE_SERVER */
#if defined(WOLFHSM_CFG_TEST_POSIX) && defined(WOLFHSM_CFG_ENABLE_CLIENT) && \
defined(WOLFHSM_CFG_ENABLE_SERVER)
static void _whClientServerThreadTest(whClientConfig* c_conf,
whServerConfig* s_conf,
whTestSheClientFn clientFn)
{
pthread_t cthread = {0};
pthread_t sthread = {0};
whTestSheClientTaskCtx cTaskCtx = {
.clientConfig = c_conf,
.clientFn = clientFn,
};
void* retval;
int rc = 0;
rc = pthread_create(&sthread, NULL, _whServerTask, s_conf);
if (rc == 0) {
rc = pthread_create(&cthread, NULL, _whClientTask, &cTaskCtx);
if (rc == 0) {
/* All good. Block on joining */
pthread_join(cthread, &retval);
pthread_join(sthread, &retval);
} else {
/* Cancel the server thread */
pthread_cancel(sthread);
pthread_join(sthread, &retval);
}
}
}
static int wh_ClientServer_MemThreadTest(whTestSheClientFn clientFn)
{
uint8_t req[BUFFER_SIZE] = {0};
uint8_t resp[BUFFER_SIZE] = {0};
whTransportMemConfig tmcf[1] = {{
.req = (whTransportMemCsr*)req,
.req_size = sizeof(req),
.resp = (whTransportMemCsr*)resp,
.resp_size = sizeof(resp),
}};
/* Client configuration/contexts */
whTransportClientCb tccb[1] = {WH_TRANSPORT_MEM_CLIENT_CB};
whTransportMemClientContext tmcc[1] = {0};
whCommClientConfig cc_conf[1] = {{
.transport_cb = tccb,
.transport_context = (void*)tmcc,
.transport_config = (void*)tmcf,
.client_id = WH_TEST_DEFAULT_CLIENT_ID,
}};
whClientConfig c_conf[1] = {{
.comm = cc_conf,
}};
/* Server configuration/contexts */
whTransportServerCb tscb[1] = {WH_TRANSPORT_MEM_SERVER_CB};
whTransportMemServerContext tmsc[1] = {0};
whCommServerConfig cs_conf[1] = {{
.transport_cb = tscb,
.transport_context = (void*)tmsc,
.transport_config = (void*)tmcf,
.server_id = 124,
}};
/* RamSim Flash state and configuration */
uint8_t memory[FLASH_RAM_SIZE] = {0};
whFlashRamsimCtx fc[1] = {0};
whFlashRamsimCfg fc_conf[1] = {{
.size = FLASH_RAM_SIZE, /* 1MB Flash */
.sectorSize = FLASH_SECTOR_SIZE, /* 128KB Sector Size */
.pageSize = FLASH_PAGE_SIZE, /* 8B Page Size */
.erasedByte = ~(uint8_t)0,
.memory = memory,
}};
const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB};
/* NVM Flash Configuration using RamSim HAL Flash */
whNvmFlashConfig nf_conf[1] = {{
.cb = fcb,
.context = fc,
.config = fc_conf,
}};
whNvmFlashContext nfc[1] = {0};
whNvmCb nfcb[1] = {WH_NVM_FLASH_CB};
whNvmConfig n_conf[1] = {{
.cb = nfcb,
.context = nfc,
.config = nf_conf,
}};
whNvmContext nvm[1] = {{0}};
/* Crypto context */
whServerCryptoContext crypto[1] = {0};
whServerSheContext she[1];
memset(she, 0, sizeof(she));
whServerConfig s_conf[1] = {{
.comm_config = cs_conf,
.nvm = nvm,
.crypto = crypto,
.she = she,
.devId = INVALID_DEVID,
}};
WH_TEST_RETURN_ON_FAIL(wh_Nvm_Init(nvm, n_conf));
WH_TEST_RETURN_ON_FAIL(wolfCrypt_Init());
WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, INVALID_DEVID));
_whClientServerThreadTest(c_conf, s_conf, clientFn);
wh_Nvm_Cleanup(nvm);
wc_FreeRng(crypto->rng);
wolfCrypt_Cleanup();
return WH_ERROR_OK;
}
#endif /* WOLFHSM_CFG_TEST_POSIX && WOLFHSM_CFG_ENABLE_CLIENT && \
WOLFHSM_CFG_ENABLE_SERVER */
#if defined(WOLFHSM_CFG_ENABLE_SERVER)
static int wh_She_TestMasterEcuKeyFallback(void)
{
int ret = 0;
whServerContext server[1] = {0};
whNvmMetadata outMeta[1] = {0};
uint8_t keyBuf[WH_SHE_KEY_SZ] = {0};
uint32_t keySz = sizeof(keyBuf);
uint8_t zeros[WH_SHE_KEY_SZ] = {0};
whKeyId masterEcuKeyId;
/* Transport (not used, but required for server init) */
uint8_t reqBuf[BUFFER_SIZE] = {0};
uint8_t respBuf[BUFFER_SIZE] = {0};
whTransportMemConfig tmcf[1] = {{
.req = (whTransportMemCsr*)reqBuf,
.req_size = sizeof(reqBuf),