-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathzm_crypto_openssl.h
More file actions
109 lines (92 loc) · 3.23 KB
/
Copy pathzm_crypto_openssl.h
File metadata and controls
109 lines (92 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ZONEMINDER_SRC_ZM_CRYPTO_OPENSSL_H_
#define ZONEMINDER_SRC_ZM_CRYPTO_OPENSSL_H_
#ifdef HAVE_LIBOPENSSL
#include "zm_crypto_generics.h"
#include "zm_utils.h"
#include <openssl/evp.h>
namespace zm {
namespace crypto {
namespace impl {
namespace openssl {
typedef EVP_MD const *(*HashCreator)();
template <HashAlgorithms Algorithm>
struct HashAlgorithmMapper;
template <>
struct HashAlgorithmMapper<HashAlgorithms::kMD5> {
// TODO: Remove conditional once Jessie and CentOS 7 are deprecated
// This is needed since GCC 4.8 is faulty (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60199)
#if defined(__GNUC__) && __GNUC__ < 5
static HashCreator hash_creator() {
static constexpr HashCreator creator = EVP_md5;
return creator;
}
#else
static constexpr HashCreator hash_creator = EVP_md5;
#endif
};
template <>
struct HashAlgorithmMapper<HashAlgorithms::kSHA1> {
// TODO: Remove conditional once Jessie and CentOS 7 are deprecated
// This is needed since GCC 4.8 is faulty (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60199)
#if defined(__GNUC__) && __GNUC__ < 5
static HashCreator hash_creator() {
static constexpr HashCreator creator = EVP_sha1;
return creator;
}
#else
static constexpr HashCreator hash_creator = EVP_sha1;
#endif
};
template <HashAlgorithms Algorithm>
class GenericHashImpl : public GenericHash<GenericHashImpl<Algorithm>, Algorithm> {
public:
GenericHashImpl() {
// TODO: Use EVP_MD_CTX_new once we drop support for Jessie and CentOS 7 (OpenSSL > 1.1.0)
ctx_ = EVP_MD_CTX_create();
#if defined(__GNUC__) && __GNUC__ < 5
EVP_DigestInit_ex(ctx_, HashAlgorithmMapper<Algorithm>::hash_creator()(), nullptr);
#else
EVP_DigestInit_ex(ctx_, HashAlgorithmMapper<Algorithm>::hash_creator(), nullptr);
#endif
};
~GenericHashImpl() {
// TODO: Use EVP_MD_CTX_free once we drop support for Jessie and CentOS 7 (OpenSSL > 1.1.0)
EVP_MD_CTX_destroy(ctx_);
}
void DoUpdateData(const uint8 *data, size_t length) {
int32 res = EVP_DigestUpdate(ctx_, data, length);
ASSERT(res == 1);
}
void DoFinalize() {
uint32 length = 0;
int32 res = EVP_DigestFinal_ex(ctx_, digest_.data(), &length);
ASSERT(res == 1);
ASSERT(length == HashAlgorithm<Algorithm>::digest_length);
}
private:
EVP_MD_CTX *ctx_;
using Base = GenericHash<GenericHashImpl<Algorithm>, Algorithm>;
using Base::digest_;
};
} // namespace openssl
} // namespace impl
} // namespace crypto
} // namespace zm
#endif // HAVE_LIBOPENSSL
#endif // ZONEMINDER_SRC_ZM_CRYPTO_OPENSSL_H_