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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
/*
* Copyright (c) 2013 Remko Tronçon
* Licensed under the GNU General Public License.
* See the COPYING file for more information.
*/
#include <Swiften/Crypto/OpenSSLCryptoProvider.h>
#include <openssl/sha.h>
#include <openssl/md5.h>
#include <openssl/hmac.h>
#include <cassert>
#include <boost/numeric/conversion/cast.hpp>
#include <Swiften/Crypto/Hash.h>
#include <Swiften/Base/ByteArray.h>
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
using namespace Swift;
namespace {
class SHA1Hash : public Hash {
public:
SHA1Hash() : finalized(false) {
if (!SHA1_Init(&context)) {
assert(false);
}
}
~SHA1Hash() {
}
virtual Hash& update(const ByteArray& data) SWIFTEN_OVERRIDE {
return updateInternal(data);
}
virtual Hash& update(const SafeByteArray& data) SWIFTEN_OVERRIDE {
return updateInternal(data);
}
virtual std::vector<unsigned char> getHash() {
assert(!finalized);
std::vector<unsigned char> result(SHA_DIGEST_LENGTH);
SHA1_Final(vecptr(result), &context);
return result;
}
private:
template<typename ContainerType>
Hash& updateInternal(const ContainerType& data) {
assert(!finalized);
if (!SHA1_Update(&context, vecptr(data), data.size())) {
assert(false);
}
return *this;
}
private:
SHA_CTX context;
bool finalized;
};
class MD5Hash : public Hash {
public:
MD5Hash() : finalized(false) {
if (!MD5_Init(&context)) {
assert(false);
}
}
~MD5Hash() {
}
virtual Hash& update(const ByteArray& data) SWIFTEN_OVERRIDE {
return updateInternal(data);
}
virtual Hash& update(const SafeByteArray& data) SWIFTEN_OVERRIDE {
return updateInternal(data);
}
virtual std::vector<unsigned char> getHash() {
assert(!finalized);
std::vector<unsigned char> result(MD5_DIGEST_LENGTH);
MD5_Final(vecptr(result), &context);
return result;
}
private:
template<typename ContainerType>
Hash& updateInternal(const ContainerType& data) {
assert(!finalized);
if (!MD5_Update(&context, vecptr(data), data.size())) {
assert(false);
}
return *this;
}
private:
MD5_CTX context;
bool finalized;
};
template<typename T>
class HMACHash : public Hash {
public:
HMACHash(const T& key) : finalized(false) {
HMAC_CTX_init(&context);
HMAC_Init(&context, vecptr(key), boost::numeric_cast<int>(key.size()), EVP_sha1());
}
~HMACHash() {
HMAC_CTX_cleanup(&context);
}
virtual Hash& update(const ByteArray& data) SWIFTEN_OVERRIDE {
return updateInternal(data);
}
virtual Hash& update(const SafeByteArray& data) SWIFTEN_OVERRIDE {
return updateInternal(data);
}
virtual std::vector<unsigned char> getHash() {
assert(!finalized);
std::vector<unsigned char> result(SHA_DIGEST_LENGTH);
unsigned int len = SHA_DIGEST_LENGTH;
HMAC_Final(&context, vecptr(result), &len);
return result;
}
private:
template<typename ContainerType>
Hash& updateInternal(const ContainerType& data) {
assert(!finalized);
HMAC_Update(&context, vecptr(data), data.size());
return *this;
}
private:
HMAC_CTX context;
bool finalized;
};
}
OpenSSLCryptoProvider::OpenSSLCryptoProvider() {
}
OpenSSLCryptoProvider::~OpenSSLCryptoProvider() {
}
Hash* OpenSSLCryptoProvider::createSHA1() {
return new SHA1Hash();
}
Hash* OpenSSLCryptoProvider::createMD5() {
return new MD5Hash();
}
Hash* OpenSSLCryptoProvider::createHMACSHA1(const SafeByteArray& key) {
return new HMACHash<SafeByteArray>(key);
}
Hash* OpenSSLCryptoProvider::createHMACSHA1(const ByteArray& key) {
return new HMACHash<ByteArray>(key);
}
bool OpenSSLCryptoProvider::isMD5AllowedForCrypto() const {
return true;
}
|