1 /* unit.c API unit tests driver
2  *
3  * Copyright (C) 2006-2021 wolfSSL Inc.
4  *
5  * This file is part of wolfSSL.
6  *
7  * wolfSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * wolfSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20  */
21 
22 
23 /* Name change compatibility layer no longer need to be included here */
24 
25 #ifdef HAVE_CONFIG_H
26     #include <config.h>
27 #endif
28 
29 #include <wolfssl/wolfcrypt/settings.h>
30 
31 #include <stdio.h>
32 #include <tests/unit.h>
33 #include <wolfssl/wolfcrypt/fips_test.h>
34 
35 
36 int myoptind = 0;
37 char* myoptarg = NULL;
38 int unit_test(int argc, char** argv);
39 
40 #ifndef NO_TESTSUITE_MAIN_DRIVER
main(int argc,char ** argv)41 int main(int argc, char** argv)
42 {
43     return unit_test(argc, argv);
44 }
45 #endif
46 
unit_test(int argc,char ** argv)47 int unit_test(int argc, char** argv)
48 {
49     int ret = 0;
50 
51     (void)argc;
52     (void)argv;
53 
54 #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
55     if (argc > 1) {
56         int memFailCount = atoi(argv[1]);
57         printf("\n--- SET RNG MALLOC FAIL AT %d---\n", memFailCount);
58         wolfSSL_SetMemFailCount(memFailCount);
59     }
60 #endif
61 
62     printf("starting unit tests...\n");
63 
64 #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
65     wolfSSL_Debugging_ON();
66 #endif
67 
68 #ifdef WC_RNG_SEED_CB
69     wc_SetSeed_Cb(wc_GenerateSeed);
70 #endif
71 #ifdef HAVE_WNR
72     if (wc_InitNetRandom(wnrConfig, NULL, 5000) != 0)
73         err_sys("Whitewood netRandom global config failed");
74 #endif /* HAVE_WNR */
75 
76 #ifndef WOLFSSL_TIRTOS
77     ChangeToWolfRoot();
78 #endif
79 
80 #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 5)
81 #if !defined(NO_AES) && !defined(NO_AES_CBC)
82     if (wc_RunCast_fips(FIPS_CAST_AES_CBC) != 0) {
83         err_sys("AES-CBC CAST failed");
84     }
85 #endif
86 #ifdef HAVE_AESGCM
87     if (wc_RunCast_fips(FIPS_CAST_AES_GCM) != 0) {
88         err_sys("AES-GCM CAST failed");
89     }
90 #endif
91 #ifndef NO_SHA
92     if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA1) != 0) {
93         err_sys("HMAC-SHA1 CAST failed");
94     }
95 #endif
96     /* the only non-optional CAST */
97     if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA2_256) != 0) {
98         err_sys("HMAC-SHA2-256 CAST failed");
99     }
100 #ifdef WOLFSSL_SHA512
101     if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA2_512) != 0) {
102         err_sys("HMAC-SHA2-512 CAST failed");
103     }
104 #endif
105 #ifdef WOLFSSL_SHA3
106     if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA3_256) != 0) {
107         err_sys("HMAC-SHA3-256 CAST failed");
108     }
109 #endif
110 #ifdef HAVE_HASHDRBG
111     if (wc_RunCast_fips(FIPS_CAST_DRBG) != 0) {
112         err_sys("Hash_DRBG CAST failed");
113     }
114 #endif
115 #ifndef NO_RSA
116     if (wc_RunCast_fips(FIPS_CAST_RSA_SIGN_PKCS1v15) != 0) {
117         err_sys("RSA sign CAST failed");
118     }
119 #endif
120 #if defined(HAVE_ECC_CDH) && defined(HAVE_ECC_CDH_CAST)
121     if (wc_RunCast_fips(FIPS_CAST_ECC_CDH) != 0) {
122         err_sys("RSA sign CAST failed");
123     }
124 #endif
125 #ifdef HAVE_ECC_DHE
126     if (wc_RunCast_fips(FIPS_CAST_ECC_PRIMITIVE_Z) != 0) {
127         err_sys("ECC Primitive Z CAST failed");
128     }
129 #endif
130 #ifdef HAVE_ECC
131     if (wc_RunCast_fips(FIPS_CAST_ECDSA) != 0) {
132         err_sys("ECDSA CAST failed");
133     }
134 #endif
135 #ifndef NO_DH
136     if (wc_RunCast_fips(FIPS_CAST_DH_PRIMITIVE_Z) != 0) {
137         err_sys("DH Primitive Z CAST failed");
138     }
139 #endif
140 #ifdef WOLFSSL_HAVE_PRF
141     if (wc_RunCast_fips(FIPS_CAST_KDF_TLS12) != 0) {
142         err_sys("KDF TLSv1.2 CAST failed");
143     }
144 #endif
145 #if defined(WOLFSSL_HAVE_PRF) && defined(WOLFSSL_TLS13)
146     if (wc_RunCast_fips(FIPS_CAST_KDF_TLS13) != 0) {
147         err_sys("KDF TLSv1.3 CAST failed");
148     }
149 #endif
150 #ifdef WOLFSSL_WOLFSSH
151     if (wc_RunCast_fips(FIPS_CAST_KDF_SSH) != 0) {
152         err_sys("KDF SSHv2.0 CAST failed");
153     }
154 #endif
155 #endif /* HAVE_FIPS && HAVE_FIPS_VERSION == 5 */
156 #ifdef WOLFSSL_ALLOW_SKIP_UNIT_TESTS
157     if (argc == 1)
158 #endif
159     {
160         ApiTest();
161 
162         if ( (ret = HashTest()) != 0){
163             printf("hash test failed with %d\n", ret);
164             goto exit;
165         }
166     }
167 
168 #ifndef NO_WOLFSSL_CIPHER_SUITE_TEST
169 #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
170 #ifndef SINGLE_THREADED
171     if ( (ret = SuiteTest(argc, argv)) != 0){
172         printf("suite test failed with %d\n", ret);
173         goto exit;
174     }
175 #endif
176 #endif
177 #endif /* NO_WOLFSSL_CIPHER_SUITE_TEST */
178 
179     SrpTest();
180 
181 exit:
182 #ifdef HAVE_WNR
183     if (wc_FreeNetRandom() < 0)
184         err_sys("Failed to free netRandom context");
185 #endif /* HAVE_WNR */
186 
187     return ret;
188 }
189 
190 
191 
wait_tcp_ready(func_args * args)192 void wait_tcp_ready(func_args* args)
193 {
194 #ifdef SINGLE_THREADED
195     (void)args;
196 #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
197     pthread_mutex_lock(&args->signal->mutex);
198 
199     if (!args->signal->ready)
200         pthread_cond_wait(&args->signal->cond, &args->signal->mutex);
201     args->signal->ready = 0; /* reset */
202 
203     pthread_mutex_unlock(&args->signal->mutex);
204 #else
205     (void)args;
206 #endif
207 }
208 
209 
start_thread(THREAD_FUNC fun,func_args * args,THREAD_TYPE * thread)210 void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread)
211 {
212 #ifdef SINGLE_THREADED
213     (void)fun;
214     (void)args;
215     (void)thread;
216 #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
217     pthread_create(thread, 0, fun, args);
218     return;
219 #elif defined (WOLFSSL_TIRTOS)
220     /* Initialize the defaults and set the parameters. */
221     Task_Params taskParams;
222     Task_Params_init(&taskParams);
223     taskParams.arg0 = (UArg)args;
224     taskParams.stackSize = 65535;
225     *thread = Task_create((Task_FuncPtr)fun, &taskParams, NULL);
226     if (*thread == NULL) {
227         printf("Failed to create new Task\n");
228     }
229     Task_yield();
230 #else
231     *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0);
232 #endif
233 }
234 
235 
join_thread(THREAD_TYPE thread)236 void join_thread(THREAD_TYPE thread)
237 {
238 #ifdef SINGLE_THREADED
239     (void)thread;
240 #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
241     pthread_join(thread, 0);
242 #elif defined (WOLFSSL_TIRTOS)
243     while(1) {
244         if (Task_getMode(thread) == Task_Mode_TERMINATED) {
245             Task_sleep(5);
246             break;
247         }
248         Task_yield();
249     }
250 #else
251     int res = WaitForSingleObject((HANDLE)thread, INFINITE);
252     assert(res == WAIT_OBJECT_0);
253     res = CloseHandle((HANDLE)thread);
254     assert(res);
255     (void)res; /* Suppress un-used variable warning */
256 #endif
257 }
258 
259 
260