-
-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathcrypto_openssl.c
More file actions
337 lines (293 loc) · 8.96 KB
/
crypto_openssl.c
File metadata and controls
337 lines (293 loc) · 8.96 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* crypto_openssl.c - Crypto wrapper (openssl version)
*/
/*
* Copyright (C) 2011 Gernot Tenchio
* Copyright (C) 2019 Christian Beier
*
* This 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 software 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 software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#include <string.h>
#include <openssl/sha.h>
#include <openssl/md5.h>
#include <openssl/dh.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
#include <openssl/provider.h>
#endif
#include "crypto.h"
static unsigned char reverseByte(unsigned char b) {
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
return b;
}
int hash_md5(void *out, const void *in, const size_t in_len)
{
MD5_CTX md5;
if(!MD5_Init(&md5))
return 0;
if(!MD5_Update(&md5, in, in_len))
return 0;
if(!MD5_Final(out, &md5))
return 0;
return 1;
}
int hash_sha1(void *out, const void *in, const size_t in_len)
{
SHA_CTX sha1;
if(!SHA1_Init(&sha1))
return 0;
if(!SHA1_Update(&sha1, in, in_len))
return 0;
if(!SHA1_Final(out, &sha1))
return 0;
return 1;
}
int hash_sha512(void *out, const void *in, const size_t in_len)
{
EVP_MD_CTX *ctx = NULL;
int result = 0;
unsigned int digest_len = 0;
if (!(ctx = EVP_MD_CTX_new()))
goto out;
if (!EVP_DigestInit_ex(ctx, EVP_sha512(), NULL))
goto out;
if (!EVP_DigestUpdate(ctx, in, in_len))
goto out;
if (!EVP_DigestFinal_ex(ctx, out, &digest_len))
goto out;
result = digest_len == SHA512_HASH_SIZE;
out:
EVP_MD_CTX_free(ctx);
return result;
}
void random_bytes(void *out, size_t len)
{
RAND_bytes(out, len);
}
int encrypt_rfbdes(void *out, int *out_len, const unsigned char key[8], const void *in, const size_t in_len)
{
int result = 0;
EVP_CIPHER_CTX *des = NULL;
unsigned char mungedkey[8];
int i;
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
OSSL_PROVIDER *providerLegacy = NULL;
OSSL_PROVIDER *providerDefault = NULL;
#endif
for (i = 0; i < 8; i++)
mungedkey[i] = reverseByte(key[i]);
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
/* Load Multiple providers into the default (NULL) library context */
if (!(providerLegacy = OSSL_PROVIDER_load(NULL, "legacy")))
goto out;
if (!(providerDefault = OSSL_PROVIDER_load(NULL, "default")))
goto out;
#endif
if(!(des = EVP_CIPHER_CTX_new()))
goto out;
if(!EVP_EncryptInit_ex(des, EVP_des_ecb(), NULL, mungedkey, NULL))
goto out;
if(!EVP_EncryptUpdate(des, out, out_len, in, in_len))
goto out;
result = 1;
out:
if (des)
EVP_CIPHER_CTX_free(des);
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
if (providerLegacy)
OSSL_PROVIDER_unload(providerLegacy);
if (providerDefault)
OSSL_PROVIDER_unload(providerDefault);
#endif
return result;
}
int decrypt_rfbdes(void *out, int *out_len, const unsigned char key[8], const void *in, const size_t in_len)
{
int result = 0;
EVP_CIPHER_CTX *des = NULL;
unsigned char mungedkey[8];
int i;
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
OSSL_PROVIDER *providerLegacy = NULL;
OSSL_PROVIDER *providerDefault = NULL;
#endif
for (i = 0; i < 8; i++)
mungedkey[i] = reverseByte(key[i]);
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
/* Load Multiple providers into the default (NULL) library context */
if (!(providerLegacy = OSSL_PROVIDER_load(NULL, "legacy")))
goto out;
if (!(providerDefault = OSSL_PROVIDER_load(NULL, "default")))
goto out;
#endif
if(!(des = EVP_CIPHER_CTX_new()))
goto out;
if(!EVP_DecryptInit_ex(des, EVP_des_ecb(), NULL, mungedkey, NULL))
goto out;
if(!EVP_CIPHER_CTX_set_padding(des, 0))
goto out;
if(!EVP_DecryptUpdate(des, out, out_len, in, in_len))
goto out;
result = 1;
out:
if (des)
EVP_CIPHER_CTX_free(des);
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
if (providerLegacy)
OSSL_PROVIDER_unload(providerLegacy);
if (providerDefault)
OSSL_PROVIDER_unload(providerDefault);
#endif
return result;
}
int encrypt_aes128ecb(void *out, int *out_len, const unsigned char key[16], const void *in, const size_t in_len)
{
int result = 0;
EVP_CIPHER_CTX *aes;
if(!(aes = EVP_CIPHER_CTX_new()))
goto out;
EVP_CIPHER_CTX_set_padding(aes, 0);
if(!EVP_EncryptInit_ex(aes, EVP_aes_128_ecb(), NULL, key, NULL))
goto out;
if(!EVP_EncryptUpdate(aes, out, out_len, in, in_len))
goto out;
result = 1;
out:
EVP_CIPHER_CTX_free(aes);
return result;
}
int pbkdf2_hmac_sha512(const uint8_t *password, size_t password_len,
const uint8_t *salt, size_t salt_len, uint32_t rounds,
uint8_t *out, size_t out_len)
{
if (!password || !salt || !out || out_len == 0)
return 0;
return PKCS5_PBKDF2_HMAC((const char *)password, (int)password_len, salt,
(int)salt_len, rounds ? (int)rounds : 1,
EVP_sha512(), (int)out_len, out) == 1;
}
int encrypt_rsa_pkcs1_spki_der(uint8_t *out, size_t *out_len,
const uint8_t *der, size_t der_len,
const void *in, size_t in_len)
{
int result = 0;
EVP_PKEY *pkey = NULL;
EVP_PKEY_CTX *ctx = NULL;
const unsigned char *derp = der;
size_t required_len = 0;
if (!out || !out_len || !der || !in)
goto out;
pkey = d2i_PUBKEY(NULL, &derp, (long)der_len);
if (!pkey || EVP_PKEY_base_id(pkey) != EVP_PKEY_RSA)
goto out;
ctx = EVP_PKEY_CTX_new(pkey, NULL);
if (!ctx)
goto out;
if (EVP_PKEY_encrypt_init(ctx) <= 0)
goto out;
if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0)
goto out;
if (EVP_PKEY_encrypt(ctx, NULL, &required_len, in, in_len) <= 0)
goto out;
if (required_len > *out_len)
goto out;
if (EVP_PKEY_encrypt(ctx, out, &required_len, in, in_len) <= 0)
goto out;
*out_len = required_len;
result = 1;
out:
EVP_PKEY_CTX_free(ctx);
EVP_PKEY_free(pkey);
return result;
}
static void pad_leading_zeros(uint8_t *out, const size_t current_len, const size_t expected_len) {
if (current_len >= expected_len || expected_len < 1)
return;
size_t diff = expected_len - current_len;
memmove(out + diff, out, current_len);
memset(out, 0, diff);
}
int dh_generate_keypair(uint8_t *priv_out, uint8_t *pub_out, const uint8_t *gen, const size_t gen_len, const uint8_t *prime, const size_t keylen)
{
int result = 0;
DH *dh;
#if OPENSSL_VERSION_NUMBER >= 0x10100000L || \
(defined (LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x30500000)
const BIGNUM *pub_key = NULL;
const BIGNUM *priv_key = NULL;
#endif
if(!(dh = DH_new()))
goto out;
#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
(defined (LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x30500000)
dh->p = BN_bin2bn(prime, keylen, NULL);
dh->g = BN_bin2bn(gen, gen_len, NULL);
#else
if(!DH_set0_pqg(dh, BN_bin2bn(prime, keylen, NULL), NULL, BN_bin2bn(gen, gen_len, NULL)))
goto out;
#endif
if(!DH_generate_key(dh))
goto out;
#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
(defined (LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x30500000)
if(BN_bn2bin(dh->priv_key, priv_out) == 0)
goto out;
if(BN_bn2bin(dh->pub_key, pub_out) == 0)
goto out;
pad_leading_zeros(priv_out, BN_num_bytes(dh->priv_key), keylen);
pad_leading_zeros(pub_out, BN_num_bytes(dh->pub_key), keylen);
#else
DH_get0_key(dh, &pub_key, &priv_key);
if(BN_bn2binpad(priv_key, priv_out, keylen) == -1)
goto out;
if(BN_bn2binpad(pub_key, pub_out, keylen) == -1)
goto out;
#endif
result = 1;
out:
DH_free(dh);
return result;
}
int dh_compute_shared_key(uint8_t *shared_out, const uint8_t *priv, const uint8_t *pub, const uint8_t *prime, const size_t keylen)
{
int result = 0;
DH *dh;
if(!(dh = DH_new()))
goto out;
#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
(defined LIBRESSL_VERSION_NUMBER && LIBRESSL_VERSION_NUMBER < 0x30500000)
dh->p = BN_bin2bn(prime, keylen, NULL);
dh->priv_key = BN_bin2bn(priv, keylen, NULL);
#else
if(!DH_set0_pqg(dh, BN_bin2bn(prime, keylen, NULL), NULL, BN_new()))
goto out;
if(!DH_set0_key(dh, NULL, BN_bin2bn(priv, keylen, NULL)))
goto out;
#endif
int shared_len = DH_compute_key(shared_out, BN_bin2bn(pub, keylen, NULL), dh);
if(shared_len == -1)
goto out;
pad_leading_zeros(shared_out, shared_len, keylen);
result = 1;
out:
DH_free(dh);
return result;
}