GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/crypto/crypto_util.cc
Date: 2024-04-28 02:33:07
Exec Total Coverage
Lines: 7 7 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #include "crypto/crypto_util.h"
6
7 #include <pthread.h>
8
9 #include <openssl/crypto.h>
10 #include <openssl/rand.h>
11
12 #include <cassert>
13
14 #include "crypto/openssl_version.h"
15 #include "util/platform.h"
16
17 #ifdef CVMFS_NAMESPACE_GUARD
18 namespace CVMFS_NAMESPACE_GUARD {
19 #endif
20
21 #ifndef OPENSSL_API_INTERFACE_V11
22 namespace {
23
24 pthread_mutex_t *gLibcryptoLocks = NULL;
25
26 static void CallbackLibcryptoLock(int mode, int type,
27 const char * /* file */, int /* line */)
28 {
29 int retval;
30
31 if (mode & CRYPTO_LOCK) {
32 retval = pthread_mutex_lock(&(gLibcryptoLocks[type]));
33 } else {
34 retval = pthread_mutex_unlock(&(gLibcryptoLocks[type]));
35 }
36 assert(retval == 0);
37 }
38
39 static unsigned long CallbackLibcryptoThreadId() { // NOLINT(runtime/int)
40 return platform_gettid();
41 }
42
43 } // anonymous namespace
44 #endif
45
46
47 6 void crypto::InitRng() {
48 6 RAND_poll();
49 6 }
50
51
52 75 void crypto::SetupLibcryptoMt() {
53 #ifndef OPENSSL_API_INTERFACE_V11
54 if (gLibcryptoLocks != NULL)
55 return;
56
57 gLibcryptoLocks = static_cast<pthread_mutex_t *>(OPENSSL_malloc(
58 CRYPTO_num_locks() * sizeof(pthread_mutex_t)));
59 for (int i = 0; i < CRYPTO_num_locks(); ++i) {
60 int retval = pthread_mutex_init(&(gLibcryptoLocks[i]), NULL);
61 assert(retval == 0);
62 }
63
64 CRYPTO_set_id_callback(CallbackLibcryptoThreadId);
65 CRYPTO_set_locking_callback(CallbackLibcryptoLock);
66 #endif
67 75 }
68
69
70 74 void crypto::CleanupLibcryptoMt() {
71 #ifndef OPENSSL_API_INTERFACE_V11
72 if (gLibcryptoLocks == NULL)
73 return;
74
75 CRYPTO_set_locking_callback(NULL);
76 for (int i = 0; i < CRYPTO_num_locks(); ++i)
77 pthread_mutex_destroy(&(gLibcryptoLocks[i]));
78
79 OPENSSL_free(gLibcryptoLocks);
80 gLibcryptoLocks = NULL;
81 #endif
82 74 }
83
84 #ifdef CVMFS_NAMESPACE_GUARD
85 } // namespace CVMFS_NAMESPACE_GUARD
86 #endif
87