2 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
15 #include <openssl/crypto.h>
16 #include <openssl/evp.h>
17 #include <openssl/aes.h>
18 #include <openssl/rsa.h>
21 static int do_fips = 0;
24 #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
26 typedef unsigned int thread_t;
28 static int run_thread(thread_t *t, void (*f)(void))
34 static int wait_for_thread(thread_t thread)
39 #elif defined(OPENSSL_SYS_WINDOWS)
41 typedef HANDLE thread_t;
43 static DWORD WINAPI thread_run(LPVOID arg)
47 *(void **) (&f) = arg;
53 static int run_thread(thread_t *t, void (*f)(void))
55 *t = CreateThread(NULL, 0, thread_run, *(void **) &f, 0, NULL);
59 static int wait_for_thread(thread_t thread)
61 return WaitForSingleObject(thread, INFINITE) == 0;
66 typedef pthread_t thread_t;
68 static void *thread_run(void *arg)
72 *(void **) (&f) = arg;
78 static int run_thread(thread_t *t, void (*f)(void))
80 return pthread_create(t, NULL, thread_run, *(void **) &f) == 0;
83 static int wait_for_thread(thread_t thread)
85 return pthread_join(thread, NULL) == 0;
90 static int test_lock(void)
92 CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
94 if (!TEST_true(CRYPTO_THREAD_read_lock(lock))
95 || !TEST_true(CRYPTO_THREAD_unlock(lock)))
98 CRYPTO_THREAD_lock_free(lock);
103 static CRYPTO_ONCE once_run = CRYPTO_ONCE_STATIC_INIT;
104 static unsigned once_run_count = 0;
106 static void once_do_run(void)
111 static void once_run_thread_cb(void)
113 CRYPTO_THREAD_run_once(&once_run, once_do_run);
116 static int test_once(void)
120 if (!TEST_true(run_thread(&thread, once_run_thread_cb))
121 || !TEST_true(wait_for_thread(thread))
122 || !CRYPTO_THREAD_run_once(&once_run, once_do_run)
123 || !TEST_int_eq(once_run_count, 1))
128 static CRYPTO_THREAD_LOCAL thread_local_key;
129 static unsigned destructor_run_count = 0;
130 static int thread_local_thread_cb_ok = 0;
132 static void thread_local_destructor(void *arg)
144 static void thread_local_thread_cb(void)
148 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
149 if (!TEST_ptr_null(ptr)
150 || !TEST_true(CRYPTO_THREAD_set_local(&thread_local_key,
151 &destructor_run_count)))
154 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
155 if (!TEST_ptr_eq(ptr, &destructor_run_count))
158 thread_local_thread_cb_ok = 1;
161 static int test_thread_local(void)
166 if (!TEST_true(CRYPTO_THREAD_init_local(&thread_local_key,
167 thread_local_destructor)))
170 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
171 if (!TEST_ptr_null(ptr)
172 || !TEST_true(run_thread(&thread, thread_local_thread_cb))
173 || !TEST_true(wait_for_thread(thread))
174 || !TEST_int_eq(thread_local_thread_cb_ok, 1))
177 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
179 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
180 if (!TEST_ptr_null(ptr))
183 # if !defined(OPENSSL_SYS_WINDOWS)
184 if (!TEST_int_eq(destructor_run_count, 1))
189 if (!TEST_true(CRYPTO_THREAD_cleanup_local(&thread_local_key)))
194 static int test_atomic(void)
196 int val = 0, ret = 0, testresult = 0;
197 uint64_t val64 = 1, ret64 = 0;
198 CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
203 if (CRYPTO_atomic_add(&val, 1, &ret, NULL)) {
204 /* This succeeds therefore we're on a platform with lockless atomics */
205 if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
208 /* This failed therefore we're on a platform without lockless atomics */
209 if (!TEST_int_eq(val, 0) || !TEST_int_eq(val, ret))
215 if (!TEST_true(CRYPTO_atomic_add(&val, 1, &ret, lock)))
217 if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
220 if (CRYPTO_atomic_or(&val64, 2, &ret64, NULL)) {
221 /* This succeeds therefore we're on a platform with lockless atomics */
222 if (!TEST_uint_eq((unsigned int)val64, 3)
223 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
226 /* This failed therefore we're on a platform without lockless atomics */
227 if (!TEST_uint_eq((unsigned int)val64, 1)
228 || !TEST_int_eq((unsigned int)ret64, 0))
234 if (!TEST_true(CRYPTO_atomic_or(&val64, 2, &ret64, lock)))
237 if (!TEST_uint_eq((unsigned int)val64, 3)
238 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
242 if (CRYPTO_atomic_load(&val64, &ret64, NULL)) {
243 /* This succeeds therefore we're on a platform with lockless atomics */
244 if (!TEST_uint_eq((unsigned int)val64, 3)
245 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
248 /* This failed therefore we're on a platform without lockless atomics */
249 if (!TEST_uint_eq((unsigned int)val64, 3)
250 || !TEST_int_eq((unsigned int)ret64, 0))
255 if (!TEST_true(CRYPTO_atomic_load(&val64, &ret64, lock)))
258 if (!TEST_uint_eq((unsigned int)val64, 3)
259 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
264 CRYPTO_THREAD_lock_free(lock);
268 static OSSL_LIB_CTX *multi_libctx = NULL;
269 static int multi_success;
271 static void thread_general_worker(void)
273 EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
274 EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
275 EVP_CIPHER_CTX *cipherctx = EVP_CIPHER_CTX_new();
276 EVP_CIPHER *ciph = EVP_CIPHER_fetch(multi_libctx, "AES-128-CBC", NULL);
277 const char *message = "Hello World";
278 size_t messlen = strlen(message);
279 /* Should be big enough for encryption output too */
280 unsigned char out[EVP_MAX_MD_SIZE];
281 const unsigned char key[AES_BLOCK_SIZE] = {
282 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
283 0x0c, 0x0d, 0x0e, 0x0f
285 const unsigned char iv[AES_BLOCK_SIZE] = {
286 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
287 0x0c, 0x0d, 0x0e, 0x0f
291 EVP_PKEY_CTX *pctx = NULL;
292 EVP_PKEY *pkey = NULL;
296 isfips = OSSL_PROVIDER_available(multi_libctx, "fips");
300 || !TEST_ptr(cipherctx)
305 for (i = 0; i < 5; i++) {
306 if (!TEST_true(EVP_DigestInit_ex(mdctx, md, NULL))
307 || !TEST_true(EVP_DigestUpdate(mdctx, message, messlen))
308 || !TEST_true(EVP_DigestFinal(mdctx, out, &mdoutl)))
311 for (i = 0; i < 5; i++) {
312 if (!TEST_true(EVP_EncryptInit_ex(cipherctx, ciph, NULL, key, iv))
313 || !TEST_true(EVP_EncryptUpdate(cipherctx, out, &ciphoutl,
314 (unsigned char *)message,
316 || !TEST_true(EVP_EncryptFinal(cipherctx, out, &ciphoutl)))
320 pctx = EVP_PKEY_CTX_new_from_name(multi_libctx, "RSA", NULL);
322 || !TEST_int_gt(EVP_PKEY_keygen_init(pctx), 0)
324 * We want the test to run quickly - not securely. Therefore we
325 * use an insecure bit length where we can (512). In the FIPS
326 * module though we must use a longer length.
328 || !TEST_int_gt(EVP_PKEY_CTX_set_rsa_keygen_bits(pctx,
329 isfips ? 2048 : 512),
331 || !TEST_int_gt(EVP_PKEY_keygen(pctx, &pkey), 0))
336 EVP_MD_CTX_free(mdctx);
338 EVP_CIPHER_CTX_free(cipherctx);
339 EVP_CIPHER_free(ciph);
340 EVP_PKEY_CTX_free(pctx);
346 static void thread_multi_simple_fetch(void)
348 EVP_MD *md = EVP_MD_fetch(NULL, "SHA2-256", NULL);
356 static EVP_PKEY *shared_evp_pkey = NULL;
358 static void thread_shared_evp_pkey(void)
360 char *msg = "Hello World";
361 unsigned char ctbuf[256];
362 unsigned char ptbuf[256];
363 size_t ptlen = sizeof(ptbuf), ctlen = sizeof(ctbuf);
364 EVP_PKEY_CTX *ctx = NULL;
368 for (i = 0; i < 1 + do_fips; i++) {
370 EVP_PKEY_CTX_free(ctx);
371 ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey,
372 i == 0 ? "provider=default"
377 if (!TEST_int_ge(EVP_PKEY_encrypt_init(ctx), 0)
378 || !TEST_int_ge(EVP_PKEY_encrypt(ctx, ctbuf, &ctlen,
379 (unsigned char *)msg, strlen(msg)),
383 EVP_PKEY_CTX_free(ctx);
384 ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey, NULL);
389 if (!TEST_int_ge(EVP_PKEY_decrypt_init(ctx), 0)
390 || !TEST_int_ge(EVP_PKEY_decrypt(ctx, ptbuf, &ptlen, ctbuf, ctlen),
392 || !TEST_mem_eq(msg, strlen(msg), ptbuf, ptlen))
399 EVP_PKEY_CTX_free(ctx);
405 * Do work in multiple worker threads at the same time.
406 * Test 0: General worker, using the default provider
407 * Test 1: General worker, using the fips provider
408 * Test 2: Simple fetch worker
409 * Test 3: Worker using a shared EVP_PKEY
411 static int test_multi(int idx)
413 thread_t thread1, thread2;
415 OSSL_PROVIDER *prov = NULL, *prov2 = NULL;
416 void (*worker)(void);
418 if (idx == 1 && !do_fips)
419 return TEST_skip("FIPS not supported");
422 multi_libctx = OSSL_LIB_CTX_new();
423 if (!TEST_ptr(multi_libctx))
425 prov = OSSL_PROVIDER_load(multi_libctx, (idx == 1) ? "fips" : "default");
432 worker = thread_general_worker;
435 worker = thread_multi_simple_fetch;
439 * If available we have both the default and fips providers for this
443 && !TEST_ptr(prov2 = OSSL_PROVIDER_load(multi_libctx, "fips")))
445 if (!TEST_ptr(shared_evp_pkey = load_pkey_pem(privkey, multi_libctx)))
447 worker = thread_shared_evp_pkey;
450 TEST_error("Invalid test index");
454 if (!TEST_true(run_thread(&thread1, worker))
455 || !TEST_true(run_thread(&thread2, worker)))
460 if (!TEST_true(wait_for_thread(thread1))
461 || !TEST_true(wait_for_thread(thread2))
462 || !TEST_true(multi_success))
468 OSSL_PROVIDER_unload(prov);
469 OSSL_PROVIDER_unload(prov2);
470 OSSL_LIB_CTX_free(multi_libctx);
471 EVP_PKEY_free(shared_evp_pkey);
472 shared_evp_pkey = NULL;
477 * This test attempts to load several providers at the same time, and if
478 * run with a thread sanitizer, should crash if the core provider code
479 * doesn't synchronize well enough.
481 #define MULTI_LOAD_THREADS 3
482 static void test_multi_load_worker(void)
486 TEST_ptr(prov = OSSL_PROVIDER_load(NULL, "default"));
487 TEST_true(OSSL_PROVIDER_unload(prov));
490 static int test_multi_load(void)
492 thread_t threads[MULTI_LOAD_THREADS];
495 for (i = 0; i < MULTI_LOAD_THREADS; i++)
496 TEST_true(run_thread(&threads[i], test_multi_load_worker));
498 for (i = 0; i < MULTI_LOAD_THREADS; i++)
499 TEST_true(wait_for_thread(threads[i]));
504 typedef enum OPTION_choice {
511 const OPTIONS *test_get_options(void)
513 static const OPTIONS options[] = {
514 OPT_TEST_OPTIONS_DEFAULT_USAGE,
515 { "fips", OPT_FIPS, '-', "Test the FIPS provider" },
521 int setup_tests(void)
526 while ((o = opt_next()) != OPT_EOF) {
538 if (!TEST_ptr(datadir = test_get_argument(0)))
541 privkey = test_mk_file_path(datadir, "rsakey.pem");
542 if (!TEST_ptr(privkey))
547 ADD_TEST(test_thread_local);
548 ADD_TEST(test_atomic);
549 ADD_TEST(test_multi_load);
550 ADD_ALL_TESTS(test_multi, 4);
554 void cleanup_tests(void)
556 OPENSSL_free(privkey);