CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
crypto_util.cc
Go to the documentation of this file.
1 
5 #include "crypto/crypto_util.h"
6 
7 #include <openssl/crypto.h>
8 #include <openssl/rand.h>
9 #include <pthread.h>
10 
11 #include <cassert>
12 
13 #include "crypto/openssl_version.h"
14 #include "util/platform.h"
15 
16 #ifdef CVMFS_NAMESPACE_GUARD
17 namespace CVMFS_NAMESPACE_GUARD {
18 #endif
19 
20 #ifndef OPENSSL_API_INTERFACE_V11
21 namespace {
22 
23 pthread_mutex_t *gLibcryptoLocks = NULL;
24 
25 static void CallbackLibcryptoLock(int mode, int type, const char * /* file */,
26  int /* line */) {
27  int retval;
28 
29  if (mode & CRYPTO_LOCK) {
30  retval = pthread_mutex_lock(&(gLibcryptoLocks[type]));
31  } else {
32  retval = pthread_mutex_unlock(&(gLibcryptoLocks[type]));
33  }
34  assert(retval == 0);
35 }
36 
37 static unsigned long CallbackLibcryptoThreadId() { // NOLINT(runtime/int)
38  return platform_gettid();
39 }
40 
41 } // anonymous namespace
42 #endif
43 
44 
45 void crypto::InitRng() { RAND_poll(); }
46 
47 
49 #ifndef OPENSSL_API_INTERFACE_V11
50  if (gLibcryptoLocks != NULL)
51  return;
52 
53  gLibcryptoLocks = static_cast<pthread_mutex_t *>(
54  OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t)));
55  for (int i = 0; i < CRYPTO_num_locks(); ++i) {
56  int retval = pthread_mutex_init(&(gLibcryptoLocks[i]), NULL);
57  assert(retval == 0);
58  }
59 
60  CRYPTO_set_id_callback(CallbackLibcryptoThreadId);
61  CRYPTO_set_locking_callback(CallbackLibcryptoLock);
62 #endif
63 }
64 
65 
67 #ifndef OPENSSL_API_INTERFACE_V11
68  if (gLibcryptoLocks == NULL)
69  return;
70 
71  CRYPTO_set_locking_callback(NULL);
72  for (int i = 0; i < CRYPTO_num_locks(); ++i)
73  pthread_mutex_destroy(&(gLibcryptoLocks[i]));
74 
75  OPENSSL_free(gLibcryptoLocks);
76  gLibcryptoLocks = NULL;
77 #endif
78 }
79 
80 #ifdef CVMFS_NAMESPACE_GUARD
81 } // namespace CVMFS_NAMESPACE_GUARD
82 #endif
CVMFS_EXPORT void InitRng()
Definition: crypto_util.cc:45
pthread_t platform_gettid()
static void CallbackLibcryptoLock(int mode, int type, const char *, int)
Definition: crypto_util.cc:25
CVMFS_EXPORT void CleanupLibcryptoMt()
Definition: crypto_util.cc:66
assert((mem||(size==0))&&"Out Of Memory")
static unsigned long CallbackLibcryptoThreadId()
Definition: crypto_util.cc:37
CVMFS_EXPORT void SetupLibcryptoMt()
Definition: crypto_util.cc:48