-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathOpenSSLCryptoOperations.cpp
216 lines (159 loc) · 7.2 KB
/
OpenSSLCryptoOperations.cpp
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "OpenSSLCryptoOperations.h"
#include <openssl/evp.h>
#include <cstring>
OpenSSLCryptoOperations::OpenSSLCryptoOperations() {
cipher_ctx = EVP_CIPHER_CTX_new();
md_ctx = EVP_MD_CTX_new();
mac_cmac = EVP_MAC_fetch(nullptr, "CMAC", nullptr);
mac_hmac = EVP_MAC_fetch(nullptr, "HMAC", nullptr);
cmac_ctx = EVP_MAC_CTX_new(mac_cmac);
hmac_ctx = EVP_MAC_CTX_new(mac_hmac);
cipher_aes_cbc = EVP_CIPHER_fetch(nullptr, "AES-128-CBC", nullptr);
cipher_aes_ctr = EVP_CIPHER_fetch(nullptr, "AES-128-CTR", nullptr);
cipher_aes_ecb = EVP_CIPHER_fetch(nullptr, "AES-128-ECB", nullptr);
md_sha1 = EVP_MD_fetch(nullptr, "SHA1", nullptr);
md_sha256 = EVP_MD_fetch(nullptr, "SHA256", nullptr);
hmac_sha1_param = new OSSL_PARAM[2];
hmac_sha1_param[0] = OSSL_PARAM_construct_utf8_string("digest", const_cast<char *>("SHA1"), 0);
hmac_sha1_param[1] = OSSL_PARAM_construct_end();
hmac_sha256_param = new OSSL_PARAM[2];
hmac_sha256_param[0] = OSSL_PARAM_construct_utf8_string("digest", const_cast<char *>("SHA256"), 0);
hmac_sha256_param[1] = OSSL_PARAM_construct_end();
}
OpenSSLCryptoOperations::~OpenSSLCryptoOperations() {
delete[] hmac_sha1_param;
delete[] hmac_sha256_param;
EVP_MD_free(md_sha1);
EVP_MD_free(md_sha256);
EVP_CIPHER_free(cipher_aes_cbc);
EVP_CIPHER_free(cipher_aes_ctr);
EVP_CIPHER_free(cipher_aes_ecb);
EVP_MAC_CTX_free(cmac_ctx);
EVP_MAC_CTX_free(hmac_ctx);
EVP_MAC_free(mac_cmac);
EVP_MAC_free(mac_hmac);
EVP_CIPHER_CTX_free(cipher_ctx);
EVP_MD_CTX_free(md_ctx);
}
int OpenSSLCryptoOperations::aes_cbc_encrypt(const unsigned char *src, unsigned char *dst, int size, const unsigned char *key, int key_size, unsigned char *iv) const {
if (size == 0)
return 0;
int result = cipher_encrypt(cipher_aes_cbc, src, dst, size, key, key_size, iv);
if (result != 0)
return result;
// the new IV is the last encoded block
std::memcpy(iv, dst + size - 0x10, 0x10);
return 0;
}
int OpenSSLCryptoOperations::aes_cbc_decrypt(const unsigned char *src, unsigned char *dst, int size, const unsigned char *key, int key_size, unsigned char *iv) const {
if (size == 0)
return 0;
// the new IV is the last encoded block
// copy it here in case src and dst are aliased
unsigned char new_iv[0x10];
std::memcpy(new_iv, src + size - 0x10, 0x10);
int result = cipher_decrypt(cipher_aes_cbc, src, dst, size, key, key_size, iv);
if (result != 0)
return result;
std::memcpy(iv, new_iv, 0x10);
return 0;
}
int OpenSSLCryptoOperations::aes_ctr_encrypt(const unsigned char *src, unsigned char *dst, int size, const unsigned char *key, int key_size, unsigned char *iv) {
return aes_ctr(src, dst, size, key, key_size, iv);
}
int OpenSSLCryptoOperations::aes_ctr_decrypt(const unsigned char *src, unsigned char *dst, int size, const unsigned char *key, int key_size, unsigned char *iv) {
return aes_ctr(src, dst, size, key, key_size, iv);
}
int OpenSSLCryptoOperations::aes_ecb_encrypt(const unsigned char *src, unsigned char *dst, int size, const unsigned char *key, int key_size) const {
return cipher_encrypt(cipher_aes_ecb, src, dst, size, key, key_size, nullptr);
}
int OpenSSLCryptoOperations::aes_ecb_decrypt(const unsigned char *src, unsigned char *dst, int size, const unsigned char *key, int key_size) const {
return cipher_decrypt(cipher_aes_ecb, src, dst, size, key, key_size, nullptr);
}
int OpenSSLCryptoOperations::aes_cmac(const unsigned char *src, unsigned char *dst, int size, const unsigned char *key, int key_size) const {
if (key_size != 128)
return -1;
OSSL_PARAM params[2] = {
OSSL_PARAM_construct_utf8_string("digest", const_cast<char *>("AES128"), 0),
OSSL_PARAM_construct_end()
};
if (EVP_MAC_init(cmac_ctx, key, key_size, params) != 1)
return -1;
if (EVP_MAC_update(cmac_ctx, src, size) != 1)
return -1;
size_t dstlen = 0x10;
if (EVP_MAC_final(cmac_ctx, dst, &dstlen, dstlen) != 1)
return -1;
return 0;
}
int OpenSSLCryptoOperations::sha1(const unsigned char *src, unsigned char *dst, int size) const {
return sha(md_sha1, src, dst, size);
}
int OpenSSLCryptoOperations::sha256(const unsigned char *src, unsigned char *dst, int size) const {
return sha(md_sha256, src, dst, size);
}
int OpenSSLCryptoOperations::hmac_sha1(const unsigned char *src, unsigned char *dst, int size, const unsigned char *key, int key_size) const {
return hmac_sha(hmac_sha1_param, 20, src, dst, size, key, key_size);
}
int OpenSSLCryptoOperations::hmac_sha256(const unsigned char *src, unsigned char *dst, int size, const unsigned char *key, int key_size) const {
return hmac_sha(hmac_sha256_param, 32, src, dst, size, key, key_size);
}
int OpenSSLCryptoOperations::aes_ctr(const unsigned char *src, unsigned char *dst, int size, const unsigned char *key, int key_size, unsigned char *iv) const {
int result = cipher_encrypt(cipher_aes_ctr, src, dst, size, key, key_size, iv);
if (result != 0)
return result;
// 128 bit big-endian addition
uint64_t to_add = size / 0x10;
for (int i = 15; i >= 0; i--) {
to_add += iv[i];
iv[i] = static_cast<uint8_t>(to_add);
to_add >>= 8;
}
return 0;
}
int OpenSSLCryptoOperations::cipher_encrypt(const EVP_CIPHER *cipher, const unsigned char *src, unsigned char *dst, int size, const unsigned char *key, int key_size, unsigned char *iv) const {
if (key_size != 128)
return -1;
if (EVP_EncryptInit_ex(cipher_ctx, cipher, nullptr, key, iv) != 1)
return -1;
EVP_CIPHER_CTX_set_padding(cipher_ctx, 0);
int len;
if (EVP_EncryptUpdate(cipher_ctx, dst, &len, src, size) != 1)
return -1;
if (EVP_EncryptFinal_ex(cipher_ctx, dst + len, &len) != 1)
return -1;
return 0;
}
int OpenSSLCryptoOperations::cipher_decrypt(const EVP_CIPHER *cipher, const unsigned char *src, unsigned char *dst, int size, const unsigned char *key, int key_size, unsigned char *iv) const {
if (key_size != 128)
return -1;
if (EVP_DecryptInit_ex(cipher_ctx, cipher, nullptr, key, iv) != 1)
return -1;
EVP_CIPHER_CTX_set_padding(cipher_ctx, 0);
int len;
if (EVP_DecryptUpdate(cipher_ctx, dst, &len, src, size) != 1)
return -1;
if (EVP_DecryptFinal_ex(cipher_ctx, dst + len, &len) != 1)
return -1;
return 0;
}
int OpenSSLCryptoOperations::sha(const EVP_MD *md, const unsigned char *src, unsigned char *dst, int size) const {
if (EVP_DigestInit_ex(md_ctx, md, nullptr) != 1)
return -1;
if (EVP_DigestUpdate(md_ctx, src, size) != 1)
return -1;
unsigned int len = 0;
if (EVP_DigestFinal_ex(md_ctx, dst, &len) != 1)
return -1;
return 0;
}
int OpenSSLCryptoOperations::hmac_sha(const OSSL_PARAM *param, int dstlen, const unsigned char *src, unsigned char *dst, int size, const unsigned char *key, int key_size) const {
if (EVP_MAC_init(hmac_ctx, key, key_size, param) != 1)
return -1;
if (EVP_MAC_update(hmac_ctx, src, size) != 1)
return -1;
std::size_t dst_len = dstlen;
if (EVP_MAC_final(hmac_ctx, dst, &dst_len, dstlen) != 1)
return -1;
return 0;
}