update libressl to v2.7.4
[unleashed.git] / lib / libcrypto / crypto_init.c
blob08fb55ffd523bd8123f5ddc766a6e4a3c63769a7
1 /*
2 * Copyright (c) 2018 Bob Beck <beck@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 /* OpenSSL style init */
19 #include <pthread.h>
20 #include <stdio.h>
22 #include <openssl/objects.h>
23 #include <openssl/conf.h>
24 #include <openssl/evp.h>
25 #include <openssl/err.h>
26 #include "cryptlib.h"
28 int OpenSSL_config(const char *);
29 int OpenSSL_no_config(void);
31 static pthread_t crypto_init_thread;
33 static void
34 OPENSSL_init_crypto_internal(void)
36 crypto_init_thread = pthread_self();
37 OPENSSL_cpuid_setup();
38 ERR_load_crypto_strings();
39 OpenSSL_add_all_ciphers();
40 OpenSSL_add_all_digests();
43 int
44 OPENSSL_init_crypto(uint64_t opts, const void *settings)
46 static pthread_once_t once = PTHREAD_ONCE_INIT;
48 if (pthread_equal(pthread_self(), crypto_init_thread))
49 return 1; /* don't recurse */
51 if (pthread_once(&once, OPENSSL_init_crypto_internal) != 0)
52 return 0;
54 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) &&
55 (OpenSSL_no_config() == 0))
56 return 0;
58 if ((opts & OPENSSL_INIT_LOAD_CONFIG) &&
59 (OpenSSL_config(NULL) == 0))
60 return 0;
62 return 1;