forked from wolfSSL/wolfssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwc_port.c
More file actions
5152 lines (4306 loc) · 129 KB
/
wc_port.c
File metadata and controls
5152 lines (4306 loc) · 129 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
/* port.c
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL 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.
*
* wolfSSL 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
*/
/*
wolfCrypt Porting Build Options:
Threading/Mutex options:
* SINGLE_THREADED: No-op mutex/threading implementations default: off
* WOLFSSL_PTHREADS: Use pthread-based mutex/threading default: off
* (auto-detected on most POSIX systems)
* WOLFSSL_MUTEX_INITIALIZER: Use static mutex initialization default: off
* WC_MUTEX_OPS_INLINE: Use inlined mutex operations default: off
* WOLFSSL_USER_MUTEX: User-provided mutex implementation default: off
* WOLFSSL_COND: Enable condition variable support default: off
* WOLFSSL_USE_RWLOCK: Enable reader-writer lock support default: off
* WOLFSSL_THREAD_NO_JOIN: Create threads without join default: off
* WOLFSSL_ALGO_HW_MUTEX: Per-algorithm hardware mutex locks default: off
* Controls AES, hash, PK, and RNG mutexes.
* WOLFSSL_CRYPT_HW_MUTEX: Cryptography hardware mutex default: off
* Master control for all HW mutex init.
* NO_AES_MUTEX: Disable AES hardware mutex default: off
* NO_HASH_MUTEX: Disable hash hardware mutex default: off
* NO_PK_MUTEX: Disable public-key hardware mutex default: off
* NO_RNG_MUTEX: Disable RNG hardware mutex default: off
*
* Memory options:
* USE_WOLFSSL_MEMORY: Enable custom memory allocation hooks default: on
* WOLFSSL_STATIC_MEMORY: Use static memory pools instead of default: off
* dynamic allocation.
* WOLFSSL_TRACK_MEMORY: Enable memory allocation tracking default: off
* WOLFSSL_TRACK_MEMORY_VERBOSE: Verbose memory tracking output default: off
* WOLFSSL_FORCE_MALLOC_FAIL_TEST: Force malloc failures for default: off
* testing error handling paths.
* WOLFSSL_MEM_FAIL_COUNT: Count malloc failures for testing default: off
* WOLFSSL_CHECK_MEM_ZERO: Verify sensitive memory is zeroed default: off
* on free. Debug tool for key material.
*
* Filesystem options:
* NO_FILESYSTEM: Disable all filesystem operations default: off
* NO_WOLFSSL_DIR: Disable directory listing/iteration default: off
*
* Time options:
* WOLFSSL_GMTIME: Provide custom gmtime implementation default: off
* HAVE_TIME_T_TYPE: Platform provides time_t default: auto
* TIME_OVERRIDES: Application provides custom time funcs default: off
* USER_TICKS: Application provides tick counter default: off
* USE_WOLF_TM: Use wolfSSL struct tm definition default: off
*
* String function options:
* STRING_USER: User provides all string functions default: off
* USE_WOLF_STRTOK: Use wolfSSL strtok implementation default: off
* USE_WOLF_STRSEP: Use wolfSSL strsep implementation default: off
* USE_WOLF_STRLCPY: Use wolfSSL strlcpy implementation default: off
* USE_WOLF_STRLCAT: Use wolfSSL strlcat implementation default: off
* USE_WOLF_STRCASECMP: Use wolfSSL strcasecmp implementation default: off
* USE_WOLF_STRNCASECMP:Use wolfSSL strncasecmp implementation default: off
* USE_WOLF_STRDUP: Use wolfSSL strdup implementation default: off
*
* Atomic operation options:
* WOLFSSL_ATOMIC_OPS: Enable atomic operations for thread default: off
* safety without full mutexes.
* WOLFSSL_USER_DEFINED_ATOMICS: User-provided atomic impl default: off
* WOLFSSL_HAVE_ATOMIC_H: Has C11 atomic.h header default: off
*
* General options:
* WOLFCRYPT_ONLY: Exclude TLS/SSL, wolfCrypt only build default: off
* WOLFSSL_LEANPSK: Lean PSK build, minimal features default: off
* WOLF_C89: C89 compatibility mode default: off
* WOLFSSL_SMALL_STACK: Reduce stack usage by allocating from default: off
* heap instead. Slower but needed for
* constrained environments.
* DEBUG_WOLFSSL_VERBOSE: Enable verbose debug logging default: off
*/
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
#ifdef __APPLE__
#include <AvailabilityMacros.h>
#endif
#include <wolfssl/wolfcrypt/cpuid.h>
#ifdef HAVE_ENTROPY_MEMUSE
#include <wolfssl/wolfcrypt/wolfentropy.h>
#endif
#ifdef HAVE_ECC
#include <wolfssl/wolfcrypt/ecc.h>
#endif
#ifdef WOLFSSL_ASYNC_CRYPT
#include <wolfssl/wolfcrypt/async.h>
#endif
#ifdef FREESCALE_LTC_TFM
#include <wolfssl/wolfcrypt/port/nxp/ksdk_port.h>
#endif
#if defined(WOLFSSL_MAX3266X) || defined(WOLFSSL_MAX3266X_OLD)
#include <wolfssl/wolfcrypt/port/maxim/max3266x.h>
#ifdef WOLF_CRYPTO_CB
#include <wolfssl/wolfcrypt/port/maxim/max3266x-cryptocb.h>
#endif
#endif
#ifdef WOLFSSL_PSOC6_CRYPTO
#include <wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h>
#endif
#ifdef MAXQ10XX_MODULE_INIT
#include <wolfssl/wolfcrypt/port/maxim/maxq10xx.h>
#endif
#if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A) || \
defined(WOLFSSL_ATECC608A)
#include <wolfssl/wolfcrypt/port/atmel/atmel.h>
#endif
#if defined(WOLFSSL_RENESAS_TSIP)
#include <wolfssl/wolfcrypt/port/Renesas/renesas_tsip_internal.h>
#endif
#if defined(WOLFSSL_RENESAS_FSPSM)
#include <wolfssl/wolfcrypt/port/Renesas/renesas_fspsm_internal.h>
#endif
#if defined(WOLFSSL_RENESAS_RX64_HASH)
#include <wolfssl/wolfcrypt/port/Renesas/renesas-rx64-hw-crypt.h>
#endif
#ifdef WOLFSSL_STSAFE
#include <wolfssl/wolfcrypt/port/st/stsafe.h>
#endif
#if defined(WOLFSSL_TROPIC01)
#include <wolfssl/wolfcrypt/port/tropicsquare/tropic01.h>
#endif
#if (defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)) \
&& !defined(WOLFCRYPT_ONLY)
#include <wolfssl/openssl/evp.h>
#endif
#include <wolfssl/wolfcrypt/memory.h>
#if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY)
#include <wolfssl/wolfcrypt/mem_track.h>
#endif
#if defined(WOLFSSL_CAAM)
#include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
#endif
#if defined(HAVE_ARIA)
#include <wolfssl/wolfcrypt/port/aria/aria-cryptocb.h>
#endif
#if defined(WOLFSSL_DEVCRYPTO)
#include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h>
#endif
#ifdef WOLFSSL_IMXRT_DCP
#include <wolfssl/wolfcrypt/port/nxp/dcp_port.h>
#endif
#ifdef WOLF_CRYPTO_CB
#include <wolfssl/wolfcrypt/cryptocb.h>
#endif
#ifdef HAVE_INTEL_QA_SYNC
#include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
#endif
#ifdef HAVE_CAVIUM_OCTEON_SYNC
#include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
#endif
#if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_INIT)
#include <wolfssl/wolfcrypt/port/nxp/se050_port.h>
#endif
#ifdef WOLFSSL_SCE
#include "hal_data.h"
#endif
#if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD)
#include "rpcmem.h"
#endif
#ifdef _MSC_VER
/* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
#pragma warning(disable: 4996)
#endif
#if defined(WOLFSSL_HAVE_PSA)
#include <wolfssl/wolfcrypt/port/psa/psa.h>
#endif
#if defined(HAVE_LIBOQS)
#include <wolfssl/wolfcrypt/port/liboqs/liboqs.h>
#endif
#if defined(FREERTOS) && defined(WOLFSSL_ESPIDF)
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
/* The Espressif-specific platform include: */
#include <pthread.h>
#endif
#if defined(WOLFSSL_ZEPHYR)
#if defined(CONFIG_BOARD_NATIVE_POSIX) || defined(CONFIG_BOARD_NATIVE_SIM)
#include "native_rtc.h"
#define CONFIG_RTC
#endif
#endif
/* prevent multiple mutex initializations */
#ifdef WOLFSSL_ATOMIC_OPS
wolfSSL_Atomic_Int initRefCount = WOLFSSL_ATOMIC_INITIALIZER(0);
#else
static int initRefCount = 0;
#endif
#if defined(__aarch64__) && defined(WOLFSSL_ARMASM_BARRIER_DETECT)
int aarch64_use_sb = 0;
#endif
/* Used to initialize state for wolfcrypt
return 0 on success
*/
WOLFSSL_ABI
int wolfCrypt_Init(void)
{
int ret = 0;
int my_initRefCount = wolfSSL_Atomic_Int_FetchAdd(&initRefCount, 1);
if (my_initRefCount == 0) {
WOLFSSL_ENTER("wolfCrypt_Init");
#if defined(__aarch64__) && defined(WOLFSSL_ARMASM_BARRIER_DETECT)
aarch64_use_sb = IS_AARCH64_SB(cpuid_get_flags());
#endif
#ifdef WOLFSSL_CHECK_MEM_ZERO
/* Initialize the mutex for access to the list of memory locations that
* must be freed. */
wc_MemZero_Init();
#endif
#ifdef WOLFSSL_MEM_FAIL_COUNT
wc_MemFailCount_Init();
#endif
#ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
{
word32 rngMallocFail;
time_t seed = time(NULL);
srand((word32)seed);
rngMallocFail = rand() % 2000; /* max 2000 */
fprintf(stderr, "\n--- RNG MALLOC FAIL AT %u ---\n", rngMallocFail);
wolfSSL_SetMemFailCount(rngMallocFail);
}
#endif
#ifdef WOLF_CRYPTO_CB
wc_CryptoCb_Init();
#endif
#ifdef WOLFSSL_ASYNC_CRYPT
ret = wolfAsync_HardwareStart();
if (ret != 0) {
WOLFSSL_MSG("Async hardware start failed");
/* don't return failure, allow operation to continue */
}
#endif
#if defined(WOLFSSL_RENESAS_TSIP)
ret = tsip_Open( );
if( ret != TSIP_SUCCESS ) {
WOLFSSL_MSG("RENESAS TSIP Open failed");
/* not return 1 since WOLFSSL_SUCCESS=1*/
ret = -1;/* FATAL ERROR */
return ret;
}
#endif
#if defined(WOLFSSL_RENESAS_RX64_HASH)
ret = rx64_hw_Open();
if( ret != 0 ) {
WOLFSSL_MSG("Renesas RX64 HW Open failed");
/* not return 1 since WOLFSSL_SUCCESS=1*/
ret = -1;/* FATAL ERROR */
return ret;
}
#endif
#if defined(WOLFSSL_RENESAS_FSPSM)
ret = wc_fspsm_Open( );
if( ret != FSP_SUCCESS ) {
WOLFSSL_MSG("RENESAS SCE Open failed");
/* not return 1 since WOLFSSL_SUCCESS=1*/
ret = -1;/* FATAL ERROR */
return ret;
}
#endif
#if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
ret = InitMemoryTracker();
if (ret != 0) {
WOLFSSL_MSG("InitMemoryTracker failed");
return ret;
}
#endif
#if defined(WOLFSSL_USE_SAVE_VECTOR_REGISTERS) && defined(WOLFSSL_LINUXKM)
ret = allocate_wolfcrypt_linuxkm_fpu_states();
if (ret != 0) {
WOLFSSL_MSG("allocate_wolfcrypt_linuxkm_fpu_states failed");
return ret;
}
#endif
#if WOLFSSL_CRYPT_HW_MUTEX
/* If crypto hardware mutex protection is enabled, then initialize it */
ret = wolfSSL_CryptHwMutexInit();
if (ret != 0) {
WOLFSSL_MSG("Hw crypt mutex init failed");
return ret;
}
#endif
#if defined(FREESCALE_LTC_TFM) || defined(FREESCALE_LTC_ECC)
ret = ksdk_port_init();
if (ret != 0) {
WOLFSSL_MSG("KSDK port init failed");
return ret;
}
#endif
/* Crypto Callbacks only works on AES for MAX32666/5 HW */
#if defined(MAX3266X_AES) && defined(WOLF_CRYPTO_CB)
ret = wc_CryptoCb_RegisterDevice(WOLFSSL_MAX3266X_DEVID, wc_MxcCryptoCb,
NULL);
if(ret != 0) {
return ret;
}
#endif
#if defined(MAX3266X_RTC)
ret = wc_MXC_RTC_Init();
if (ret != 0) {
WOLFSSL_MSG("MXC RTC Init Failed");
return WC_HW_E;
}
#endif
#if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A) || \
defined(WOLFSSL_ATECC608A)
ret = atmel_init();
if (ret != 0) {
WOLFSSL_MSG("CryptoAuthLib init failed");
return ret;
}
#endif
#if defined(WOLFSSL_CRYPTOCELL)
/* enable and initialize the ARM CryptoCell 3xx runtime library */
ret = cc310_Init();
if (ret != 0) {
WOLFSSL_MSG("CRYPTOCELL init failed");
return ret;
}
#endif
#ifdef WOLFSSL_STSAFE
ret = stsafe_interface_init();
if (ret != 0) {
WOLFSSL_MSG("STSAFE init failed");
return ret;
}
#endif
#if defined(WOLFSSL_TROPIC01)
ret = Tropic01_Init();
if (ret != 0) {
WOLFSSL_MSG("Tropic01 init failed");
return ret;
}
#endif
#if defined(WOLFSSL_PSOC6_CRYPTO)
ret = psoc6_crypto_port_init();
if (ret != 0) {
WOLFSSL_MSG("PSoC6 crypto engine init failed");
return ret;
}
#endif
#ifdef MAXQ10XX_MODULE_INIT
ret = maxq10xx_port_init();
if (ret != 0) {
WOLFSSL_MSG("MAXQ10xx port init failed");
return ret;
}
#endif
#ifdef WOLFSSL_SILABS_SE_ACCEL
/* init handles if it is already initialized */
ret = sl_se_init();
#endif
#if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_INIT)
ret = wc_se050_init(NULL);
if (ret != 0) {
WOLFSSL_MSG("SE050 init failed");
return ret;
}
#endif
#ifdef WOLFSSL_ARMASM
WOLFSSL_MSG("Using ARM hardware acceleration");
#endif
#ifdef WOLFSSL_AFALG
WOLFSSL_MSG("Using AF_ALG for crypto acceleration");
#endif
#if !defined(WOLFCRYPT_ONLY) && defined(OPENSSL_EXTRA)
wolfSSL_EVP_init();
#endif
#if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
if ((ret = wc_LoggingInit()) != 0) {
WOLFSSL_MSG("Error creating logging mutex");
return ret;
}
#endif
#if defined(WOLFSSL_HAVE_PSA)
if ((ret = wc_psa_init()) != 0)
return ret;
#endif
#if defined(USE_WINDOWS_API) && defined(WIN_REUSE_CRYPT_HANDLE)
/* A failure here should not happen, but if it does the actual RNG seed
* call will fail. This init is for a shared crypt provider handle for
* RNG */
(void)wc_WinCryptHandleInit();
#endif
#ifdef HAVE_ENTROPY_MEMUSE
ret = Entropy_Init();
if (ret != 0) {
WOLFSSL_MSG("Error initializing entropy");
return ret;
}
#endif
#ifdef HAVE_ECC
#ifdef FP_ECC
wc_ecc_fp_init();
#endif
#ifdef ECC_CACHE_CURVE
if ((ret = wc_ecc_curve_cache_init()) != 0) {
WOLFSSL_MSG("Error creating curve cache");
return ret;
}
#endif
#if defined(HAVE_OID_ENCODING) && (!defined(HAVE_FIPS) || \
(defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)))
if ((ret = wc_ecc_oid_cache_init()) != 0) {
WOLFSSL_MSG("Error creating ECC oid cache");
return ret;
}
#endif
#endif
#ifdef WOLFSSL_SCE
ret = (int)WOLFSSL_SCE_GSCE_HANDLE.p_api->open(
WOLFSSL_SCE_GSCE_HANDLE.p_ctrl, WOLFSSL_SCE_GSCE_HANDLE.p_cfg);
if (ret == SSP_ERR_CRYPTO_SCE_ALREADY_OPEN) {
WOLFSSL_MSG("SCE already open");
ret = 0;
}
if (ret != SSP_SUCCESS) {
WOLFSSL_MSG("Error opening SCE");
return -1; /* FATAL_ERROR */
}
#endif
#if defined(WOLFSSL_DEVCRYPTO)
if ((ret = wc_DevCryptoInit()) != 0) {
return ret;
}
#endif
#if defined(WOLFSSL_CAAM)
if ((ret = wc_caamInit()) != 0) {
return ret;
}
#endif
#if defined(HAVE_ARIA)
if ((ret = wc_AriaInit()) != 0) {
return ret;
}
#endif
#ifdef WOLFSSL_IMXRT_DCP
if ((ret = wc_dcp_init()) != 0) {
return ret;
}
#endif
#if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD)
if ((ret = wolfSSL_InitHandle()) != 0) {
return ret;
}
rpcmem_init();
#endif
#if defined(HAVE_LIBOQS)
if ((ret = wolfSSL_liboqsInit()) != 0) {
return ret;
}
#endif
/* increment to 2, to signify successful initialization: */
(void)wolfSSL_Atomic_Int_FetchAdd(&initRefCount, 1);
}
else {
if (my_initRefCount < 2) {
(void)wolfSSL_Atomic_Int_FetchSub(&initRefCount, 1);
ret = BUSY_E;
}
}
return ret;
}
#if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
long wolfCrypt_heap_peakAllocs_checkpoint(void) {
long ret = ourMemStats.peakAllocsTripOdometer;
ourMemStats.peakAllocsTripOdometer = ourMemStats.totalAllocs -
ourMemStats.totalDeallocs;
return ret;
}
long wolfCrypt_heap_peakBytes_checkpoint(void) {
long ret = ourMemStats.peakBytesTripOdometer;
ourMemStats.peakBytesTripOdometer = ourMemStats.currentBytes;
return ret;
}
#endif
/* return success value is the same as wolfCrypt_Init */
WOLFSSL_ABI
int wolfCrypt_Cleanup(void)
{
int ret = 0;
int my_initRefCount = wolfSSL_Atomic_Int_SubFetch(&initRefCount, 1);
if (my_initRefCount == 1) {
WOLFSSL_ENTER("wolfCrypt_Cleanup");
#ifdef HAVE_ECC
#ifdef FP_ECC
wc_ecc_fp_free();
#endif
#ifdef ECC_CACHE_CURVE
wc_ecc_curve_cache_free();
#endif
#if defined(HAVE_OID_ENCODING) && (!defined(HAVE_FIPS) || \
(defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)))
wc_ecc_oid_cache_free();
#endif
#endif /* HAVE_ECC */
#if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
ret = wc_LoggingCleanup();
#endif
#if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
ShowMemoryTracker();
#endif
#ifdef WOLFSSL_ASYNC_CRYPT
wolfAsync_HardwareStop();
#endif
#ifdef WOLFSSL_RENESAS_TSIP
tsip_Close();
#endif
#if defined(WOLFSSL_RENESAS_RX64_HASH)
rx64_hw_Close();
#endif
#if defined(WOLFSSL_RENESAS_FSPSM)
wc_fspsm_Close();
#endif
#ifdef WOLFSSL_SCE
WOLFSSL_SCE_GSCE_HANDLE.p_api->close(WOLFSSL_SCE_GSCE_HANDLE.p_ctrl);
#endif
#if defined(WOLFSSL_CAAM)
wc_caamFree();
#endif
#if defined(WOLFSSL_CRYPTOCELL)
cc310_Free();
#endif
#ifdef WOLFSSL_SILABS_SE_ACCEL
ret = sl_se_deinit();
#endif
#if defined(WOLFSSL_TROPIC01)
Tropic01_Deinit();
#endif
#if defined(WOLFSSL_RENESAS_TSIP)
tsip_Close();
#endif
#if defined(WOLFSSL_DEVCRYPTO)
wc_DevCryptoCleanup();
#endif
#if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD)
rpcmem_deinit();
wolfSSL_CleanupHandle();
#endif
#if defined(WOLFSSL_USE_SAVE_VECTOR_REGISTERS) && defined(WOLFSSL_LINUXKM)
free_wolfcrypt_linuxkm_fpu_states();
#endif
#ifdef HAVE_ENTROPY_MEMUSE
Entropy_Final();
#endif
#if defined(USE_WINDOWS_API) && defined(WIN_REUSE_CRYPT_HANDLE)
wc_WinCryptHandleCleanup();
#endif
#ifdef WOLF_CRYPTO_CB
wc_CryptoCb_Cleanup();
#endif
#if defined(WOLFSSL_MEM_FAIL_COUNT) && defined(WOLFCRYPT_ONLY)
wc_MemFailCount_Free();
#endif
#ifdef WOLFSSL_CHECK_MEM_ZERO
/* Free the mutex for access to the list of memory locations that
* must be freed. */
wc_MemZero_Free();
#endif
(void)wolfSSL_Atomic_Int_SubFetch(&initRefCount, 1);
#if defined(HAVE_LIBOQS)
wolfSSL_liboqsClose();
#endif
}
else if (my_initRefCount < 0) {
(void)wolfSSL_Atomic_Int_AddFetch(&initRefCount, 1);
WOLFSSL_MSG("wolfCrypt_Cleanup() called with initRefCount <= 0.");
ret = ALREADY_E;
}
return ret;
}
#ifndef NO_FILESYSTEM
/* Helpful function to load file into allocated buffer */
int wc_FileLoad(const char* fname, unsigned char** buf, size_t* bufLen,
void* heap)
{
int ret;
ssize_t fileSz;
XFILE f;
if (fname == NULL || buf == NULL || bufLen == NULL) {
return BAD_FUNC_ARG;
}
/* set defaults */
*buf = NULL;
*bufLen = 0;
/* open file (read-only binary) */
f = XFOPEN(fname, "rb");
if (!f) {
WOLFSSL_MSG("wc_LoadFile file load error");
return BAD_PATH_ERROR;
}
if (XFSEEK(f, 0, XSEEK_END) != 0) {
WOLFSSL_MSG("wc_LoadFile file seek error");
XFCLOSE(f);
return BAD_PATH_ERROR;
}
fileSz = XFTELL(f);
if (fileSz < 0) {
WOLFSSL_MSG("wc_LoadFile ftell error");
XFCLOSE(f);
return BAD_PATH_ERROR;
}
if (XFSEEK(f, 0, XSEEK_SET) != 0) {
WOLFSSL_MSG("wc_LoadFile file seek error");
XFCLOSE(f);
return BAD_PATH_ERROR;
}
if (fileSz > 0) {
*bufLen = (size_t)fileSz;
*buf = (byte*)XMALLOC(*bufLen, heap, DYNAMIC_TYPE_TMP_BUFFER);
if (*buf == NULL) {
WOLFSSL_MSG("wc_LoadFile memory error");
ret = MEMORY_E;
}
else {
size_t readLen = XFREAD(*buf, 1, *bufLen, f);
/* check response code */
ret = (readLen == *bufLen) ? 0 : -1;
}
}
else {
ret = BUFFER_E;
}
XFCLOSE(f);
(void)heap;
return ret;
}
#if !defined(NO_WOLFSSL_DIR) && \
!defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2)
/* File Handling Helper */
/* returns 0 if file exists, WC_ISFILEEXIST_NOFILE if file doesn't exist */
int wc_FileExists(const char* fname)
{
struct ReadDirCtx ctx;
XMEMSET(&ctx, 0, sizeof(ctx));
if (fname == NULL)
return 0;
if (XSTAT(fname, &ctx.s) != 0) {
WOLFSSL_MSG("stat on name failed");
return BAD_PATH_ERROR;
} else {
#if defined(USE_WINDOWS_API)
if (XS_ISREG(ctx.s.st_mode)) {
return 0;
}
#elif defined(WOLFSSL_ZEPHYR)
if (XS_ISREG(ctx.s.type)) {
return 0;
}
#elif defined(WOLFSSL_TELIT_M2MB)
if (XS_ISREG(ctx.s.st_mode)) {
return 0;
}
#else
if (XS_ISREG(ctx.s.st_mode)) {
return 0;
}
#endif
}
return WC_ISFILEEXIST_NOFILE;
}
/* File Handling Helpers */
/* returns 0 if file found, WC_READDIR_NOFILE if no files or negative error */
int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name)
{
int ret = WC_READDIR_NOFILE; /* default to no files found */
int pathLen = 0;
if (name)
*name = NULL;
if (ctx != NULL)
XMEMSET(ctx, 0, sizeof(ReadDirCtx));
if (ctx == NULL || path == NULL) {
return BAD_FUNC_ARG;
}
pathLen = (int)XSTRLEN(path);
#ifdef USE_WINDOWS_API
if (pathLen > MAX_FILENAME_SZ - 3)
return BAD_PATH_ERROR;
XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 3);
XSTRNCPY(ctx->name + pathLen, "\\*", (size_t)(MAX_FILENAME_SZ - pathLen));
ctx->hFind = FindFirstFileA(ctx->name, &ctx->FindFileData);
if (ctx->hFind == INVALID_HANDLE_VALUE) {
WOLFSSL_MSG("FindFirstFile for path verify locations failed");
return BAD_PATH_ERROR;
}
do {
if (!(ctx->FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
int dnameLen = (int)XSTRLEN(ctx->FindFileData.cFileName);
if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
return BAD_PATH_ERROR;
}
XSTRNCPY(ctx->name, path, (size_t)pathLen + 1);
ctx->name[pathLen] = '\\';
XSTRNCPY(ctx->name + pathLen + 1,
ctx->FindFileData.cFileName,
(size_t)(MAX_FILENAME_SZ - pathLen - 1));
if (name)
*name = ctx->name;
return 0;
}
} while (FindNextFileA(ctx->hFind, &ctx->FindFileData));
#elif defined(INTIME_RTOS)
if (pathLen > MAX_FILENAME_SZ - 3)
return BAD_PATH_ERROR;
XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 3);
XSTRNCPY(ctx->name + pathLen, "\\*", MAX_FILENAME_SZ - pathLen);
if (!IntimeFindFirst(ctx->name, &ctx->FindFileData)) {
WOLFSSL_MSG("FindFirstFile for path verify locations failed");
return BAD_PATH_ERROR;
}
do {
int dnameLen = (int)XSTRLEN(IntimeFilename(ctx));
if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
return BAD_PATH_ERROR;
}
XSTRNCPY(ctx->name, path, pathLen + 1);
ctx->name[pathLen] = '\\';
XSTRNCPY(ctx->name + pathLen + 1,
IntimeFilename(ctx),
MAX_FILENAME_SZ - pathLen - 1);
if (0 == wc_FileExists(ctx->name)) {
if (name)
*name = ctx->name;
return 0;
}
} while (IntimeFindNext(&ctx->FindFileData));
#elif defined(WOLFSSL_ZEPHYR)
if (fs_opendir(&ctx->dir, path) != 0) {
WOLFSSL_MSG("opendir path verify locations failed");
return BAD_PATH_ERROR;
}
ctx->dirp = &ctx->dir;
while ((fs_readdir(&ctx->dir, &ctx->entry)) != 0) {
int dnameLen = (int)XSTRLEN(ctx->entry.name);
if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
ret = BAD_PATH_ERROR;
break;
}
XSTRNCPY(ctx->name, path, pathLen + 1);
ctx->name[pathLen] = '/';
/* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
* of earlier check it is known that dnameLen is less than
* MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
XSTRNCPY(ctx->name + pathLen + 1, ctx->entry.name, dnameLen + 1);
if ((ret = wc_FileExists(ctx->name)) == 0) {
if (name)
*name = ctx->name;
return 0;
}
}
#elif defined(WOLFSSL_TELIT_M2MB)
ctx->dir = m2mb_fs_opendir((const CHAR*)path);
if (ctx->dir == NULL) {
WOLFSSL_MSG("opendir path verify locations failed");
return BAD_PATH_ERROR;
}
while ((ctx->entry = m2mb_fs_readdir(ctx->dir)) != NULL) {
int dnameLen = (int)XSTRLEN(ctx->entry->d_name);
if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
ret = BAD_PATH_ERROR;
break;
}
XSTRNCPY(ctx->name, path, pathLen + 1);
ctx->name[pathLen] = '/';
/* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
* of earlier check it is known that dnameLen is less than
* MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1);
if ((ret = wc_FileExists(ctx->name)) == 0) {
if (name)
*name = ctx->name;
return 0;
}
}
#else
ctx->dir = opendir(path);
if (ctx->dir == NULL) {
WOLFSSL_MSG("opendir path verify locations failed");
return BAD_PATH_ERROR;
}
while ((ctx->entry = readdir(ctx->dir)) != NULL) {
int dnameLen = (int)XSTRLEN(ctx->entry->d_name);
if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
ret = BAD_PATH_ERROR;
break;
}
XSTRNCPY(ctx->name, path, (size_t)pathLen + 1);
ctx->name[pathLen] = '/';
/* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
* of earlier check it is known that dnameLen is less than
* MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, (size_t)dnameLen + 1);
if ((ret = wc_FileExists(ctx->name)) == 0) {
if (name)
*name = ctx->name;
return 0;
}
}
#endif
wc_ReadDirClose(ctx);
return ret;
}
/* returns 0 if file found, WC_READDIR_NOFILE if no more files */
int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name)
{
int ret = WC_READDIR_NOFILE; /* default to no file found */
int pathLen = 0;
if (name)
*name = NULL;
if (ctx == NULL || path == NULL) {
return BAD_FUNC_ARG;
}
XMEMSET(ctx->name, 0, MAX_FILENAME_SZ);
pathLen = (int)XSTRLEN(path);
#ifdef USE_WINDOWS_API
while (FindNextFileA(ctx->hFind, &ctx->FindFileData)) {
if (!(ctx->FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
int dnameLen = (int)XSTRLEN(ctx->FindFileData.cFileName);
if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
return BAD_PATH_ERROR;
}
XSTRNCPY(ctx->name, path, (size_t)pathLen + 1);
ctx->name[pathLen] = '\\';
XSTRNCPY(ctx->name + pathLen + 1,
ctx->FindFileData.cFileName,
(size_t)(MAX_FILENAME_SZ - pathLen - 1));
if (name)
*name = ctx->name;
return 0;
}
}
#elif defined(INTIME_RTOS)
while (IntimeFindNext(&ctx->FindFileData)) {
int dnameLen = (int)XSTRLEN(IntimeFilename(ctx));
if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
return BAD_PATH_ERROR;
}
XSTRNCPY(ctx->name, path, pathLen + 1);
ctx->name[pathLen] = '\\';
XSTRNCPY(ctx->name + pathLen + 1,
IntimeFilename(ctx),
MAX_FILENAME_SZ - pathLen - 1);
if (0 == wc_FileExists(ctx->name)) {
if (name)
*name = ctx->name;
return 0;
}
}
#elif defined(WOLFSSL_ZEPHYR)
while ((fs_readdir(&ctx->dir, &ctx->entry)) != 0) {
int dnameLen = (int)XSTRLEN(ctx->entry.name);
if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
ret = BAD_PATH_ERROR;
break;
}