forked from wolfSSL/wolfHSM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwh_test_crypto.c
More file actions
2521 lines (2367 loc) · 105 KB
/
wh_test_crypto.c
File metadata and controls
2521 lines (2367 loc) · 105 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.c
*
*/
#include "wolfhsm/wh_settings.h"
#ifndef WOLFHSM_CFG_NO_CRYPTO
#include <stdint.h>
#include <stdio.h> /* For printf */
#include <string.h> /* For memset, memcpy */
#include "wolfssl/wolfcrypt/settings.h"
#include "wolfssl/wolfcrypt/types.h"
#include "wolfhsm/wh_error.h"
#include "wolfhsm/wh_nvm.h"
#include "wolfhsm/wh_nvm_flash.h"
#include "wolfhsm/wh_flash_ramsim.h"
#include "wolfhsm/wh_comm.h"
#include "wolfhsm/wh_message.h"
#include "wolfhsm/wh_client.h"
#include "wolfhsm/wh_client_crypto.h"
#include "wolfhsm/wh_server.h"
#include "wolfhsm/wh_server_crypto.h"
#include "wolfhsm/wh_transport_mem.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 {
/* Total size needs to fit:
* - Transport CSR (whTransportMemCsr)
* - Comm header (whCommHeader)
* - Max data size (WOLFHSM_CFG_COMM_DATA_LEN)
*/
BUFFER_SIZE = sizeof(whTransportMemCsr) + sizeof(whCommHeader) +
WOLFHSM_CFG_COMM_DATA_LEN,
};
#define PLAINTEXT "mytextisbigplain"
#ifndef WOLFHSM_CFG_TEST_NO_CUSTOM_SERVERS
/* Flag causing the server loop to sleep(1) */
int serverDelay = 0;
#endif
#if defined(WOLFHSM_CFG_TEST_POSIX)
/* pointer to expose server context cancel sequence to the client cancel
* callback */
static uint16_t* cancelSeqP;
/* Test client cancel callback that directly sets the sequence to cancel in the
* server context */
static int _cancelCb(uint16_t seq)
{
*cancelSeqP = seq;
return 0;
}
#endif
#ifdef WOLFHSM_CFG_TEST_VERBOSE
static int whTest_ShowNvmAvailable(whClientContext* ctx)
{
int ret = 0;
int32_t server_rc = 0;
whNvmId avail_objects = 0;
whNvmId reclaim_objects = 0;
uint32_t avail_size = 0;
uint32_t reclaim_size = 0;
ret = wh_Client_NvmGetAvailable(ctx, &server_rc, &avail_size,
&avail_objects, &reclaim_size,
&reclaim_objects);
if (ret != 0) {
WH_ERROR_PRINT("Failed to get available NVM status\n");
} else {
printf("CRYPTO TEST NVM STATUS: 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);
}
return ret;
}
#endif /* WOLFHSM_CFG_TEST_VERBOSE */
static int whTest_CryptoRng(whClientContext* ctx, int devId, WC_RNG* rng)
{
(void)ctx; /* Unused */
#define WH_TEST_RNG_LIL 7
#define WH_TEST_RNG_MED 1024
#define WH_TEST_RNG_BIG (WOLFHSM_CFG_COMM_DATA_LEN * 2)
int ret;
uint8_t lil[WH_TEST_RNG_LIL];
uint8_t med[WH_TEST_RNG_MED];
uint8_t big[WH_TEST_RNG_BIG];
/* test rng. Note this rng is used for many tests so is left inited */
ret = wc_InitRng_ex(rng, NULL, devId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_InitRng_ex %d\n", ret);
} else {
ret = wc_RNG_GenerateBlock(rng, lil, sizeof(lil));
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_RNG_GenerateBlock %d\n", ret);
} else {
ret = wc_RNG_GenerateBlock(rng, med, sizeof(med));
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_RNG_GenerateBlock %d\n", ret);
} else {
ret = wc_RNG_GenerateBlock(rng, big, sizeof(big));
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_RNG_GenerateBlock %d\n", ret);
}
}
}
}
if (ret == 0) {
printf("RNG SUCCESS\n");
}
return ret;
}
#ifndef NO_RSA
static int whTest_CryptoRsa(whClientContext* ctx, int devId, WC_RNG* rng)
{
#define RSA_KEY_BITS 2048
#define RSA_KEY_BYTES (RSA_KEY_BITS/8)
#define RSA_EXPONENT WC_RSA_EXPONENT
int ret = WH_ERROR_OK;
RsaKey rsa[1];
char plainText[sizeof(PLAINTEXT)] = PLAINTEXT;
char cipherText[RSA_KEY_BYTES];
char finalText[RSA_KEY_BYTES];
whKeyId keyId = WH_KEYID_ERASED;
/* Using ephemeral key */
memset(cipherText, 0, sizeof(cipherText));
memset(finalText, 0, sizeof(finalText));
ret = wc_InitRsaKey_ex(rsa, NULL, devId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_InitRsaKey_ex %d\n", ret);
} else {
ret = wc_MakeRsaKey(rsa, RSA_KEY_BITS, RSA_EXPONENT, rng);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_MakeRsaKey %d\n", ret);
} else {
ret = wc_RsaPublicEncrypt((byte*)plainText, sizeof(plainText),
(byte*)cipherText, sizeof(cipherText), rsa, rng);
if (ret < 0) {
WH_ERROR_PRINT("Failed to wc_RsaPublicEncrypt %d\n", ret);
} else {
ret = wc_RsaPrivateDecrypt((byte*)cipherText, ret,
(byte*)finalText, sizeof(finalText), rsa);
if (ret < 0) {
WH_ERROR_PRINT("Failed to wc_RsaPrivateDecrypt %d\n", ret);
} else {
ret = 0;
if (memcmp(plainText, finalText, sizeof(plainText)) != 0) {
WH_ERROR_PRINT("Failed to match\n");
ret = -1;
}
}
}
}
(void)wc_FreeRsaKey(rsa);
}
if (ret == 0) {
/* Using client export key */
memset(cipherText, 0, sizeof(cipherText));
memset(finalText, 0, sizeof(finalText));
ret = wc_InitRsaKey_ex(rsa, NULL, WH_DEV_ID);
if (ret!= 0) {
WH_ERROR_PRINT("Failed to wc_InitRsaKey_ex %d\n", ret);
} else {
ret = wh_Client_RsaMakeExportKey(ctx, RSA_KEY_BITS, RSA_EXPONENT,
rsa);
if (ret != 0) {
WH_ERROR_PRINT("Failed to make exported key %d\n", ret);
} else {
ret = wc_RsaPublicEncrypt((byte*)plainText, sizeof(plainText),
(byte*)cipherText, sizeof(cipherText), rsa, rng);
if (ret < 0) {
WH_ERROR_PRINT("Failed to encrypt %d\n", ret);
} else {
ret = wc_RsaPrivateDecrypt((byte*)cipherText, ret,
(byte*)finalText, sizeof(finalText), rsa);
if (ret < 0) {
WH_ERROR_PRINT("Failed to decrypt %d\n", ret);
} else {
ret = 0;
if (memcmp(plainText, finalText,
sizeof(plainText)) != 0) {
WH_ERROR_PRINT("Failed to match\n");
ret = -1;
}
}
}
}
(void)wc_FreeRsaKey(rsa);
}
}
if (ret == 0) {
/* Using keyCache key */
memset(cipherText, 0, sizeof(cipherText));
memset(finalText, 0, sizeof(finalText));
ret = wh_Client_RsaMakeCacheKey(ctx, RSA_KEY_BITS, RSA_EXPONENT,
&keyId, WH_NVM_FLAGS_NONE, 0, NULL);
if (ret != 0) {
WH_ERROR_PRINT("Failed to make cached key %d\n", ret);
} else {
ret = wc_InitRsaKey_ex(rsa, NULL, WH_DEV_ID);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_InitRsaKey_ex %d\n", ret);
} else {
ret = wh_Client_RsaSetKeyId(rsa, keyId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wh_Client_SetKeyIdRsa %d\n", ret);
} else {
ret = wh_Client_RsaGetKeyId(rsa, &keyId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_GetKeyIdRsa %d\n", ret);
} else {
ret = wc_RsaPublicEncrypt(
(byte*)plainText, sizeof(plainText),
(byte*)cipherText, sizeof(cipherText), rsa,
rng);
if (ret < 0) {
WH_ERROR_PRINT("Failed to encrypt %d\n", ret);
} else {
ret = wc_RsaPrivateDecrypt(
(byte*)cipherText, ret,
(byte*)finalText, sizeof(finalText), rsa);
if (ret < 0) {
WH_ERROR_PRINT("Failed to decrypt %d\n", ret);
} else {
ret = 0;
if (memcmp(plainText, finalText,
sizeof(plainText)) != 0) {
WH_ERROR_PRINT("Failed to match\n");
ret = -1;
}
}
}
}
}
(void)wc_FreeRsaKey(rsa);
}
}
(void)wh_Client_KeyEvict(ctx, keyId);
}
if (ret == 0) {
printf("RSA SUCCESS\n");
}
return ret;
}
#endif /* !NO_RSA */
#ifdef HAVE_ECC
static int whTest_CryptoEcc(whClientContext* ctx, int devId, WC_RNG* rng)
{
int ret = WH_ERROR_OK;
ecc_key eccPrivate[1];
ecc_key eccPublic[1];
#define TEST_ECC_KEYSIZE 32
uint8_t shared_ab[TEST_ECC_KEYSIZE] = {0};
uint8_t shared_ba[TEST_ECC_KEYSIZE] = {0};
uint8_t hash[TEST_ECC_KEYSIZE] = {0};
uint8_t sig[ECC_MAX_SIG_SIZE] = {0};
#if 0
whNvmFlags flags = WH_NVM_FLAGS_NONE;
whKeyId key_id_a = WH_KEYID_ERASED;
uint8_t* label_a = (uint8_t*)("Ecc Label A");
whKeyId key_id_b = 24;
uint8_t* label_b = (uint8_t*)("Ecc Label B");
#endif
ret = wc_ecc_init_ex(eccPrivate, NULL, devId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_ecc_init_ex %d\n", ret);
} else {
ret = wc_ecc_init_ex(eccPublic, NULL, devId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_ecc_init_ex %d\n", ret);
} else {
ret = wc_ecc_make_key(rng, TEST_ECC_KEYSIZE, eccPrivate);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_ecc_make_key %d\n", ret);
} else {
ret = wc_ecc_make_key(rng, TEST_ECC_KEYSIZE, eccPublic);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_ecc_make_key %d\n", ret);
} else {
word32 secLen = TEST_ECC_KEYSIZE;
ret = wc_ecc_shared_secret(eccPrivate, eccPublic,
(byte*)shared_ab, &secLen);
if (ret != 0) {
WH_ERROR_PRINT("Failed to compute secret %d\n", ret);
} else {
ret = wc_ecc_shared_secret(eccPublic, eccPrivate,
(byte*)shared_ba, &secLen);
if (ret != 0) {
WH_ERROR_PRINT("Failed to compute secret %d\n",
ret);
} else {
if (memcmp(shared_ab, shared_ba, secLen) == 0) {
printf("ECDH SUCCESS\n");
} else {
WH_ERROR_PRINT("ECDH FAILED TO MATCH\n");
ret = -1;
}
}
}
/*Use the shared secret as a random hash */
memcpy(hash, shared_ba, sizeof(hash));
word32 sigLen = sizeof(sig);
ret = wc_ecc_sign_hash((void*)hash, sizeof(hash),
(void*)sig, &sigLen, rng, eccPrivate);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_ecc_sign_hash %d\n", ret);
} else {
int res = 0;
ret = wc_ecc_verify_hash((void*)sig, sigLen,
(void*)hash, sizeof(hash), &res,
eccPrivate);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_ecc_verify_hash %d\n",
ret);
} else {
if (res == 1) {
printf("ECC SIGN/VERIFY SUCCESS\n");
} else {
WH_ERROR_PRINT("ECC SIGN/VERIFY FAIL\n");
ret = -1;
}
}
}
}
}
wc_ecc_free(eccPublic);
}
wc_ecc_free(eccPrivate);
}
return ret;
}
#endif /* HAVE_ECC */
#ifdef HAVE_CURVE25519
static int whTest_CryptoCurve25519(whClientContext* ctx, int devId, WC_RNG* rng)
{
int ret = 0;
curve25519_key key_a[1] = {0};
curve25519_key key_b[1] = {0};
uint8_t shared_ab[CURVE25519_KEYSIZE] = {0};
uint8_t shared_ba[CURVE25519_KEYSIZE] = {0};
int key_size = CURVE25519_KEYSIZE;
whNvmFlags flags = WH_NVM_FLAGS_NONE;
whKeyId key_id_a = WH_KEYID_ERASED;
uint8_t label_a[WH_NVM_LABEL_LEN] = "Curve25519 Label A";
whKeyId key_id_b = 42;
uint8_t label_b[WH_NVM_LABEL_LEN] = "Curve25519 Label B";
word32 len = 0;
if (ret == 0) {
/* Use wolfcrypt ephemeral local keys */
ret = wc_curve25519_init_ex(key_a, NULL, devId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_curve25519_init_ex %d\n", ret);
} else {
ret = wc_curve25519_init_ex(key_b, NULL, devId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_curve25519_init_ex %d\n", ret);
} else {
ret = wc_curve25519_make_key(rng, key_size, key_a);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_curve25519_make_key %d\n",
ret);
}
if (ret == 0) {
ret = wc_curve25519_make_key(rng, key_size, key_b);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_curve25519_make_key %d\n",
ret);
}
}
if (ret == 0) {
len = sizeof(shared_ab);
ret = wc_curve25519_shared_secret(
key_a, key_b, shared_ab, &len);
if (ret != 0) {
WH_ERROR_PRINT("Failed to compute shared secret %d\n",
ret);
}
}
if (ret == 0) {
len = sizeof(shared_ba);
ret = wc_curve25519_shared_secret(
key_b, key_a, shared_ba, &len);
if (ret != 0) {
WH_ERROR_PRINT("Failed to compute shared secret %d\n",
ret);
}
}
if (ret == 0) {
if (XMEMCMP(shared_ab, shared_ba, len) != 0) {
WH_ERROR_PRINT("CURVE25519 secrets don't match\n");
ret = -1;
}
}
wc_curve25519_free(key_b);
}
wc_curve25519_free(key_a);
}
}
if (ret == 0) {
/* Test using wh_Client ephemeral local keys */
ret = wc_curve25519_init_ex(key_a, NULL, devId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_curve25519_init_ex %d\n", ret);
} else {
ret = wc_curve25519_init_ex(key_b, NULL, devId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_curve25519_init_ex %d\n", ret);
} else {
ret = wh_Client_Curve25519MakeExportKey(ctx, key_size, key_a);
if (ret != 0) {
WH_ERROR_PRINT("Failed to make exported key %d\n", ret);
}
if (ret == 0) {
ret = wh_Client_Curve25519MakeExportKey(ctx, key_size,
key_b);
if (ret != 0) {
WH_ERROR_PRINT("Failed to make exported key %d\n", ret);
}
}
if (ret == 0) {
len = sizeof(shared_ab);
ret = wc_curve25519_shared_secret(
key_a, key_b, shared_ab, &len);
if (ret != 0) {
WH_ERROR_PRINT("Failed to compute shared secret %d\n",
ret);
}
}
if (ret == 0) {
len = sizeof(shared_ba);
ret = wc_curve25519_shared_secret(
key_b, key_a, shared_ba, &len);
if (ret != 0) {
WH_ERROR_PRINT("Failed to compute shared secret %d\n",
ret);
}
}
if (ret == 0) {
if (XMEMCMP(shared_ab, shared_ba, len) != 0) {
WH_ERROR_PRINT("CURVE25519 secrets don't match\n");
ret = -1;
}
}
wc_curve25519_free(key_b);
}
wc_curve25519_free(key_a);
}
}
if (ret == 0) {
/* Test using wolfHSM server keys */
ret = wc_curve25519_init_ex(key_a, NULL, devId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_curve25519_init_ex %d\n", ret);
} else {
ret = wc_curve25519_init_ex(key_b, NULL, devId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_curve25519_init_ex %d\n", ret);
} else {
ret = wh_Client_Curve25519MakeCacheKey(ctx, key_size,
&key_id_a, flags, label_a, sizeof(label_a));
if (ret != 0) {
WH_ERROR_PRINT("Failed to make cached key %d\n", ret);
}
if (ret == 0) {
ret = wh_Client_Curve25519MakeCacheKey(ctx, key_size,
&key_id_b, flags, label_b, sizeof(label_b));
if (ret != 0) {
WH_ERROR_PRINT("Failed to make cached key %d\n", ret);
}
}
if (ret == 0) {
len = sizeof(shared_ab);
wh_Client_Curve25519SetKeyId(key_a, key_id_a);
wh_Client_Curve25519SetKeyId(key_b, key_id_b);
ret = wc_curve25519_shared_secret(
key_a, key_b, shared_ab, &len);
if (ret != 0) {
WH_ERROR_PRINT("Failed to compute shared secret %d\n",
ret);
}
}
if (ret == 0) {
len = sizeof(shared_ba);
ret = wc_curve25519_shared_secret(
key_b, key_a, shared_ba, &len);
if (ret != 0) {
WH_ERROR_PRINT("Failed to compute shared secret %d\n",
ret);
}
}
if (ret == 0) {
if (XMEMCMP(shared_ab, shared_ba, len) != 0) {
WH_ERROR_PRINT("CURVE25519 secrets don't match\n");
ret = -1;
}
}
wc_curve25519_free(key_b);
}
wc_curve25519_free(key_a);
}
}
if (ret == 0) {
printf("CURVE25519 SUCCESS\n");
}
return ret;
}
#endif /* HAVE_CURVE25519 */
#ifndef NO_SHA256
static int whTest_CryptoSha256(whClientContext* ctx, int devId, WC_RNG* rng)
{
(void)ctx; (void)rng; /* Not currently used */
int ret = WH_ERROR_OK;
wc_Sha256 sha256[1];
uint8_t out[WC_SHA256_DIGEST_SIZE];
/* Vector exactly one block size in length */
const char inOne[] =
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const uint8_t expectedOutOne[WC_SHA256_DIGEST_SIZE] = {
0xff, 0xe0, 0x54, 0xfe, 0x7a, 0xe0, 0xcb, 0x6d, 0xc6, 0x5c, 0x3a,
0xf9, 0xb6, 0x1d, 0x52, 0x09, 0xf4, 0x39, 0x85, 0x1d, 0xb4, 0x3d,
0x0b, 0xa5, 0x99, 0x73, 0x37, 0xdf, 0x15, 0x46, 0x68, 0xeb};
/* Vector long enough to span a SHA256 block */
const char inMulti[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX"
"YZ1234567890abcdefghi";
const uint8_t expectedOutMulti[WC_SHA256_DIGEST_SIZE] = {
0x7b, 0x54, 0x45, 0x86, 0xb3, 0x51, 0x43, 0x4e, 0xf6, 0x83, 0xdb,
0x78, 0x1d, 0x94, 0xd6, 0xb0, 0x36, 0x9b, 0x36, 0x56, 0x93, 0x0e,
0xf4, 0x47, 0x9b, 0xae, 0xff, 0xfa, 0x1f, 0x36, 0x38, 0x64};
/* Initialize SHA256 structure */
ret = wc_InitSha256_ex(sha256, NULL, devId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_InitSha256 on devId 0x%X: %d\n", devId,
ret);
} else {
/* Test SHA256 on a single block worth of data. Should trigger a server
* transaction */
ret = wc_Sha256Update(sha256,
(const byte*)inOne,
WC_SHA256_BLOCK_SIZE);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_Sha256Update %d\n", ret);
} else {
/* Finalize should trigger a server transaction with empty buffer */
ret = wc_Sha256Final(sha256, out);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_Sha256Final %d\n", ret);
} else {
/* Compare the computed hash with the expected output */
if (memcmp(out, expectedOutOne,
WC_SHA256_DIGEST_SIZE) != 0) {
WH_ERROR_PRINT("SHA256 hash does not match expected.\n");
ret = -1;
}
memset(out, 0, WC_SHA256_DIGEST_SIZE);
}
}
/* Reset state for multi block test */
(void)wc_Sha256Free(sha256);
}
if (ret == 0) {
/* Multiblock test */
ret = wc_InitSha256_ex(sha256, NULL, devId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_InitSha256 for devId 0x%X: %d\n",
devId, ret);
} else {
/* Update with a non-block aligned length. Will not trigger server
* transaction */
ret = wc_Sha256Update(sha256,
(const byte*)inMulti,
1);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_Sha256Update (first) %d\n", ret);
} else {
/* Update with a full block, will trigger block to be sent to
* server and one additional byte to be buffered */
ret = wc_Sha256Update(sha256,
(const byte*)inMulti + 1,
WC_SHA256_BLOCK_SIZE);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_Sha256Update (mid) %d\n", ret);
} else {
/* Update with the remaining data, should not trigger server
* transaction */
ret = wc_Sha256Update(sha256,
(const byte*)inMulti + 1 + WC_SHA256_BLOCK_SIZE,
strlen(inMulti) - 1 - WC_SHA256_BLOCK_SIZE);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_Sha256Update (last) %d\n",
ret);
} else {
/* Finalize should trigger a server transaction on the
* remaining partial buffer */
ret = wc_Sha256Final(sha256, out);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_Sha256Final %d\n",
ret);
} else {
/* Compare the computed hash with the expected
* output */
if (memcmp(out, expectedOutMulti,
WC_SHA256_DIGEST_SIZE) != 0) {
WH_ERROR_PRINT("SHA256 hash does not match the "
"expected output.\n");
ret = -1;
}
}
}
}
}
(void)wc_Sha256Free(sha256);
}
}
if (ret == 0) {
printf("SHA256 DEVID=0x%X SUCCESS\n", devId);
}
return ret;
}
#endif /* !NO_SHA256 */
static int whTest_CacheExportKey(whClientContext* ctx, whKeyId* inout_key_id,
uint8_t* label_in, uint8_t* label_out, uint16_t label_len,
uint8_t* key_in, uint8_t* key_out, uint16_t key_len)
{
int ret = 0;
uint16_t label_len_out = label_len;
uint16_t key_len_out = key_len;
whKeyId key_id_out = *inout_key_id;
ret = wh_Client_KeyCache(ctx, 0, label_in, label_len, key_in, key_len,
&key_id_out);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wh_Client_KeyCache %d\n", ret);
} else {
ret = wh_Client_KeyExport(ctx, key_id_out, label_out, label_len_out,
key_out, &key_len_out);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wh_Client_KeyExport %d\n", ret);
} else {
if ((key_len_out != key_len) ||
(memcmp(key_in, key_out, key_len_out) != 0) ||
(memcmp(label_in, label_out, label_len) != 0) ) {
ret = -1;
}
}
}
*inout_key_id = key_id_out;
return ret;
}
#ifdef WOLFHSM_CFG_DMA
static int whTest_CacheExportKeyDma(whClientContext* ctx, whKeyId* inout_key_id,
uint8_t* label_in, uint8_t* label_out,
uint16_t label_len, uint8_t* key_in,
uint8_t* key_out, uint16_t key_len)
{
int ret = 0;
uint16_t label_len_out = label_len;
uint16_t key_len_out = key_len;
whKeyId key_id_out = *inout_key_id;
ret = wh_Client_KeyCacheDma(ctx, 0, label_in, label_len, key_in, key_len,
&key_id_out);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wh_Client_KeyCacheDma %d\n", ret);
}
else {
ret = wh_Client_KeyExportDma(ctx, key_id_out, key_out, key_len_out,
label_out, label_len_out, &key_len_out);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wh_Client_KeyExportDma %d\n", ret);
}
else {
if ((key_len_out != key_len) ||
(memcmp(key_in, key_out, key_len_out) != 0) ||
(memcmp(label_in, label_out, label_len) != 0)) {
ret = -1;
}
}
}
*inout_key_id = key_id_out;
return ret;
}
#endif /* WOLFHSM_CFG_DMA */
static int whTest_KeyCache(whClientContext* ctx, int devId, WC_RNG* rng)
{
(void)devId; (void)rng; /* Unused */
#define WH_TEST_KEYCACHE_KEYSIZE 16
int ret;
int i;
uint16_t outLen;
uint16_t keyId;
uint8_t key[WH_TEST_KEYCACHE_KEYSIZE];
uint8_t keyOut[WH_TEST_KEYCACHE_KEYSIZE] = {0};
uint8_t labelIn[WH_NVM_LABEL_LEN] = "KeyCache Test Label";
uint8_t labelOut[WH_NVM_LABEL_LEN] = {0};
/* Randomize inputs */
ret = wc_RNG_GenerateBlock(rng, key, sizeof(key));
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_RNG_GenerateBlock %d\n", ret);
}
/* test regular cache/export */
keyId = WH_KEYID_ERASED;
if (ret == 0) {
ret = whTest_CacheExportKey(ctx, &keyId,
labelIn, labelOut, sizeof(labelIn),
key, keyOut, sizeof(key));
if (ret != 0) {
WH_ERROR_PRINT("Failed to Test CacheExportKey %d\n", ret);
} else {
printf("KEY CACHE/EXPORT SUCCESS\n");
}
}
#ifndef WOLFHSM_CFG_TEST_NO_CUSTOM_SERVERS
/* WOLFHSM_CFG_TEST_NO_CUSTOM_SERVERS protects the client test code that
* expects to interop with the custom server (also defined in this
* file), so that this test can be run against a standard server app
*
* TODO: This is a temporary bodge until we properly split tests into
* single client and multi client */
if (ret == 0) {
/* test cache with duplicate keyId for a different user */
ret = wh_Client_CommClose(ctx);
if (ret != 0) {
WH_ERROR_PRINT("Failed to CommClose:%d\n",ret);
} else {
ctx->comm->client_id = 2;
ret = wh_Client_CommInit(ctx, NULL, NULL);
if (ret != 0) {
WH_ERROR_PRINT("Failed to CommInit:%d\n", ret);
} else {
/* Check that evicting the other client's key fails */
ret = wh_Client_KeyEvict(ctx, keyId);
if (ret != WH_ERROR_NOTFOUND) {
WH_ERROR_PRINT("Failed to wh_Client_KeyEvict %d\n",
ret);
} else {
ret = whTest_CacheExportKey(ctx, &keyId,
labelIn, labelOut, sizeof(labelIn),
key, keyOut, sizeof(key));
if (ret != 0) {
WH_ERROR_PRINT("Failed to Test CacheExportKey %d\n",
ret);
} else {
/* evict for this client */
(void)wh_Client_KeyEvict(ctx, keyId);
}
}
/* switch back and verify original key */
(void)wh_Client_CommClose(ctx);
ctx->comm->client_id = 1;
ret = wh_Client_CommInit(ctx, NULL, NULL);
if (ret != 0) {
WH_ERROR_PRINT("Failed to reconnect: %d\n", ret);
} else {
outLen = sizeof(keyOut);
ret = wh_Client_KeyExport(ctx, keyId, labelOut,
sizeof(labelOut), keyOut, &outLen);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wh_Client_KeyExport %d\n",
ret);
} else {
if ( (outLen != sizeof(key)) ||
(memcmp(key, keyOut, outLen) != 0) ||
(memcmp(labelIn, labelOut,
sizeof(labelIn)) != 0) ) {
WH_ERROR_PRINT("Failed to match\n");
ret = -1;
} else {
printf("KEY CACHE USER EXCLUSION SUCCESS\n");
}
}
}
}
}
}
#endif /* !WOLFHSM_CFG_TEST_NO_CUSTOM_SERVERS */
if (ret == 0) {
/* test evict for original client */
ret = wh_Client_KeyEvict(ctx, keyId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wh_Client_KeyEvict %d\n", ret);
} else {
outLen = sizeof(keyOut);
ret = wh_Client_KeyExport(ctx, keyId, labelOut, sizeof(labelOut),
keyOut, &outLen);
if (ret != WH_ERROR_NOTFOUND) {
WH_ERROR_PRINT("Failed to not find evicted key %d\n", ret);
} else {
printf("KEY CACHE EVICT SUCCESS\n");
ret = 0;
}
}
}
if (ret == 0) {
/* test commit/erase */
keyId = WH_KEYID_ERASED;
ret = wh_Client_KeyCache(ctx, 0, labelIn, sizeof(labelIn), key,
sizeof(key), &keyId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wh_Client_KeyCache %d\n", ret);
} else {
ret = wh_Client_KeyCommit(ctx, keyId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wh_Client_KeyCommit %d\n", ret);
} else {
ret = wh_Client_KeyEvict(ctx, keyId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wh_Client_KeyEvict %d\n", ret);
} else {
outLen = sizeof(keyOut);
ret = wh_Client_KeyExport(ctx, keyId, labelOut,
sizeof(labelOut), keyOut, &outLen);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wh_Client_KeyExport %d\n",
ret);
} else {
if ((outLen != sizeof(key) ||
(memcmp(key, keyOut, outLen) != 0) ||
(memcmp(labelIn, labelOut,
sizeof(labelIn))) != 0) ) {
WH_ERROR_PRINT("Failed to match committed key\n");
ret = -1;
} else {
/* verify commit isn't using new nvm objects */
for (i = 0; i < WOLFHSM_CFG_NVM_OBJECT_COUNT; i++) {
ret = wh_Client_KeyCommit(ctx, keyId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to over commit %d\n",
ret);
}
}
if (ret == 0) {
ret = wh_Client_KeyErase(ctx, keyId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to erase key %d\n",
ret);
} else {
outLen = sizeof(keyOut);
ret = wh_Client_KeyExport(ctx, keyId,
labelOut, sizeof(labelOut), keyOut,
&outLen);
if (ret != WH_ERROR_NOTFOUND) {
WH_ERROR_PRINT("Failed to not find "
"erased key\n");
ret = -1;
} else {
ret = 0;
}
}
}
}
}
}
}
}
if (ret == 0) {
printf("KEY COMMIT/ERASE SUCCESS\n");
}
}
#ifdef WOLFHSM_CFG_DMA
/* test cache/export using DMA */
if (ret == 0) {
keyId = WH_KEYID_ERASED;
ret =
whTest_CacheExportKeyDma(ctx, &keyId, labelIn, labelOut,
sizeof(labelIn), key, keyOut, sizeof(key));
if (ret != WH_ERROR_OK) {
WH_ERROR_PRINT("Failed to Test CacheExportKeyDma %d\n", ret);
}
else {
ret = wh_Client_KeyEvict(ctx, keyId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wh_Client_KeyEvict %d\n", ret);
}
else {
printf("KEY CACHE/EXPORT DMA SUCCESS\n");
}
}
}
#endif /* WOLFHSM_CFG_DMA */
return ret;
}
#ifndef NO_AES
static int whTestCrypto_Aes(whClientContext* ctx, int devId, WC_RNG* rng)
{
#define WH_TEST_AES_KEYSIZE 16
#define WH_TEST_AES_TEXTSIZE 16
int ret = 0;
Aes aes[1];
uint8_t iv[AES_BLOCK_SIZE];
uint8_t key[WH_TEST_AES_KEYSIZE];
uint8_t plainIn[WH_TEST_AES_TEXTSIZE];
uint8_t cipher[WH_TEST_AES_TEXTSIZE] = { 0 };
uint8_t plainOut[WH_TEST_AES_TEXTSIZE] = { 0 };
whKeyId keyId = WH_KEYID_ERASED;
uint8_t labelIn[WH_NVM_LABEL_LEN] = "AES Key Label";
XMEMCPY(plainIn, PLAINTEXT, sizeof(plainIn));
/* Randomize inputs */
ret = wc_RNG_GenerateBlock(rng, key, sizeof(key));
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_RNG_GenerateBlock %d\n", ret);
} else {
ret = wc_RNG_GenerateBlock(rng, iv, sizeof(iv));
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_RNG_GenerateBlock %d\n", ret);
}
}
#ifdef HAVE_AES_CBC
if (ret == 0) {
/* test aes CBC with client side key */
ret = wc_AesInit(aes, NULL, devId);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_AesInit %d\n", ret);
} else {
ret = wc_AesSetKey(aes, key, sizeof(key), iv, AES_ENCRYPTION);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_AesSetKey %d\n", ret);
} else {
ret = wc_AesCbcEncrypt(aes, cipher, plainIn,
sizeof(plainIn));
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_AesCbcEncrypt %d\n", ret);
} else {
ret = wc_AesSetKey(aes, key, sizeof(key), iv,
AES_DECRYPTION);
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_AesSetKey %d\n", ret);
} else {
ret = wc_AesCbcDecrypt(aes, plainOut, cipher,
sizeof(cipher));
if (ret != 0) {
WH_ERROR_PRINT("Failed to wc_AesCbcDecrypt %d\n",
ret);
} else {
if (memcmp(plainIn, plainOut,