xref: /aosp_15_r20/external/private-join-and-compute/private_join_and_compute/crypto/openssl_init.cc (revision a6aa18fbfbf9cb5cd47356a9d1b057768998488c)
1 /*
2  * Copyright 2019 Google LLC.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     https://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "private_join_and_compute/crypto/openssl_init.h"
17 
18 #include "private_join_and_compute/crypto/openssl.inc"
19 
20 #if !defined(OPENSSL_IS_BORINGSSL)
21 #include <pthread.h>
22 
23 #include <mutex>  // NOLINT(build/c++11): only using std::call_once, not mutex.
24 
25 #include "absl/log/check.h"
26 #endif
27 
28 namespace private_join_and_compute {
29 #if !defined(OPENSSL_IS_BORINGSSL)
30 namespace {
31 
32 void CryptoNewThreadID(CRYPTO_THREADID* tid);
33 void CryptoLockingCallback(int mode, int n, const char* file, int line);
34 
35 class OpenSSLInit {
36  public:
OpenSSLInit()37   OpenSSLInit() : mutexes(CRYPTO_num_locks()) {
38     for (int i = 0; i < CRYPTO_num_locks(); ++i) {
39       pthread_mutex_init(&(mutexes[i]), nullptr);
40     }
41   }
42 
LoadErrStrings()43   void LoadErrStrings() {
44     ERR_load_BN_strings();
45     ERR_load_BUF_strings();
46     ERR_load_CRYPTO_strings();
47     ERR_load_EC_strings();
48     ERR_load_ERR_strings();
49     ERR_load_EVP_strings();
50     ERR_load_RAND_strings();
51   }
52 
InitLocking()53   void InitLocking() {
54     CRYPTO_THREADID_set_callback(CryptoNewThreadID);
55     CRYPTO_set_locking_callback(CryptoLockingCallback);
56   }
57 
~OpenSSLInit()58   ~OpenSSLInit() {
59     CRYPTO_set_locking_callback(nullptr);
60     for (int i = 0; i < CRYPTO_num_locks(); ++i) {
61       pthread_mutex_destroy(&(mutexes[i]));
62     }
63     ERR_free_strings();
64   }
65 
66   std::vector<pthread_mutex_t> mutexes;
67 };
68 
69 static std::once_flag init_flag;
70 static OpenSSLInit openssl_init;
71 
CryptoNewThreadID(CRYPTO_THREADID * tid)72 void CryptoNewThreadID(CRYPTO_THREADID* tid) {
73   CRYPTO_THREADID_set_numeric(tid, static_cast<uint64_t>(pthread_self()));
74 }
75 
76 // See crypto/threads/mmtest.c for usage in OpenSSL library.
CryptoLockingCallback(int mode,int n,const char * file,int line)77 void CryptoLockingCallback(int mode, int n, const char* file, int line) {
78   CHECK_GE(n, 0);
79   pthread_mutex_t* mutex = &(openssl_init.mutexes[n]);
80   if (mode & CRYPTO_LOCK) {
81     pthread_mutex_lock(mutex);
82   } else {
83     pthread_mutex_unlock(mutex);
84   }
85 }
86 
OpenSSLInitHelper()87 static void OpenSSLInitHelper() {
88   openssl_init.LoadErrStrings();
89   openssl_init.InitLocking();
90 }
91 
92 }  // namespace
93 #endif
94 
OpenSSLInit()95 void OpenSSLInit() {
96 #if !defined(OPENSSL_IS_BORINGSSL)
97   std::call_once(init_flag, OpenSSLInitHelper);
98 #endif
99 }
100 
101 }  // namespace private_join_and_compute
102