forked from shanet/Crypto-Example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrypto.cpp
397 lines (290 loc) · 10 KB
/
Crypto.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include "Crypto.h"
using namespace std;
EVP_PKEY* Crypto::localKeypair;
Crypto::Crypto() {
localKeypair = NULL;
remotePubKey = NULL;
#ifdef PSUEDO_CLIENT
genTestClientKey();
#endif
init();
}
Crypto::Crypto(unsigned char *remotePubKey, size_t remotePubKeyLen) {
localKeypair = NULL;
this->remotePubKey = NULL;
setRemotePubKey(remotePubKey, remotePubKeyLen);
init();
}
Crypto::~Crypto() {
EVP_PKEY_free(remotePubKey);
EVP_CIPHER_CTX_cleanup(rsaEncryptCtx);
EVP_CIPHER_CTX_cleanup(aesEncryptCtx);
EVP_CIPHER_CTX_cleanup(rsaDecryptCtx);
EVP_CIPHER_CTX_cleanup(aesDecryptCtx);
free(rsaEncryptCtx);
free(aesEncryptCtx);
free(rsaDecryptCtx);
free(aesDecryptCtx);
free(aesKey);
free(aesIV);
}
int Crypto::rsaEncrypt(const unsigned char *msg, size_t msgLen, unsigned char **encMsg, unsigned char **ek, size_t *ekl, unsigned char **iv, size_t *ivl) {
size_t encMsgLen = 0;
size_t blockLen = 0;
*ek = (unsigned char*)malloc(EVP_PKEY_size(remotePubKey));
*iv = (unsigned char*)malloc(EVP_MAX_IV_LENGTH);
if(*ek == NULL || *iv == NULL) return FAILURE;
*ivl = EVP_MAX_IV_LENGTH;
*encMsg = (unsigned char*)malloc(msgLen + EVP_MAX_IV_LENGTH);
if(encMsg == NULL) return FAILURE;
if(!EVP_SealInit(rsaEncryptCtx, EVP_aes_256_cbc(), ek, (int*)ekl, *iv, &remotePubKey, 1)) {
return FAILURE;
}
if(!EVP_SealUpdate(rsaEncryptCtx, *encMsg + encMsgLen, (int*)&blockLen, (const unsigned char*)msg, (int)msgLen)) {
return FAILURE;
}
encMsgLen += blockLen;
if(!EVP_SealFinal(rsaEncryptCtx, *encMsg + encMsgLen, (int*)&blockLen)) {
return FAILURE;
}
encMsgLen += blockLen;
EVP_CIPHER_CTX_cleanup(rsaEncryptCtx);
return (int)encMsgLen;
}
int Crypto::aesEncrypt(const unsigned char *msg, size_t msgLen, unsigned char **encMsg) {
size_t blockLen = 0;
size_t encMsgLen = 0;
*encMsg = (unsigned char*)malloc(msgLen + AES_BLOCK_SIZE);
if(encMsg == NULL) return FAILURE;
if(!EVP_EncryptInit_ex(aesEncryptCtx, EVP_aes_256_cbc(), NULL, aesKey, aesIV)) {
return FAILURE;
}
if(!EVP_EncryptUpdate(aesEncryptCtx, *encMsg, (int*)&blockLen, (unsigned char*)msg, msgLen)) {
return FAILURE;
}
encMsgLen += blockLen;
if(!EVP_EncryptFinal_ex(aesEncryptCtx, *encMsg + encMsgLen, (int*)&blockLen)) {
return FAILURE;
}
EVP_CIPHER_CTX_cleanup(aesEncryptCtx);
return encMsgLen + blockLen;
}
int Crypto::rsaDecrypt(unsigned char *encMsg, size_t encMsgLen, unsigned char *ek, size_t ekl, unsigned char *iv, size_t ivl, unsigned char **decMsg) {
size_t decLen = 0;
size_t blockLen = 0;
EVP_PKEY *key;
*decMsg = (unsigned char*)malloc(encMsgLen + ivl);
if(decMsg == NULL) return FAILURE;
#ifdef PSUEDO_CLIENT
key = remotePubKey;
#else
key = localKeypair;
#endif
if(!EVP_OpenInit(rsaDecryptCtx, EVP_aes_256_cbc(), ek, ekl, iv, key)) {
return FAILURE;
}
if(!EVP_OpenUpdate(rsaDecryptCtx, (unsigned char*)*decMsg + decLen, (int*)&blockLen, encMsg, (int)encMsgLen)) {
return FAILURE;
}
decLen += blockLen;
if(!EVP_OpenFinal(rsaDecryptCtx, (unsigned char*)*decMsg + decLen, (int*)&blockLen)) {
return FAILURE;
}
decLen += blockLen;
EVP_CIPHER_CTX_cleanup(rsaDecryptCtx);
return (int)decLen;
}
int Crypto::aesDecrypt(unsigned char *encMsg, size_t encMsgLen, unsigned char **decMsg) {
size_t decLen = 0;
size_t blockLen = 0;
*decMsg = (unsigned char*)malloc(encMsgLen);
if(*decMsg == NULL) return FAILURE;
if(!EVP_DecryptInit_ex(aesDecryptCtx, EVP_aes_256_cbc(), NULL, aesKey, aesIV)) {
return FAILURE;
}
if(!EVP_DecryptUpdate(aesDecryptCtx, (unsigned char*)*decMsg, (int*)&blockLen, encMsg, (int)encMsgLen)) {
return FAILURE;
}
decLen += blockLen;
if(!EVP_DecryptFinal_ex(aesDecryptCtx, (unsigned char*)*decMsg + decLen, (int*)&blockLen)) {
return FAILURE;
}
decLen += blockLen;
EVP_CIPHER_CTX_cleanup(aesDecryptCtx);
return (int)decLen;
}
int Crypto::writeKeyToFile(FILE *fd, int key) {
switch(key) {
case KEY_SERVER_PRI:
if(!PEM_write_PrivateKey(fd, localKeypair, NULL, NULL, 0, 0, NULL)) {
return FAILURE;
}
break;
case KEY_SERVER_PUB:
if(!PEM_write_PUBKEY(fd, localKeypair)) {
return FAILURE;
}
break;
case KEY_CLIENT_PUB:
if(!PEM_write_PUBKEY(fd, remotePubKey)) {
return FAILURE;
}
break;
case KEY_AES:
fwrite(aesKey, 1, AES_KEYLEN, fd);
break;
case KEY_AES_IV:
fwrite(aesIV, 1, AES_KEYLEN, fd);
break;
default:
return FAILURE;
}
return SUCCESS;
}
int Crypto::getRemotePubKey(unsigned char **pubKey) {
BIO *bio = BIO_new(BIO_s_mem());
PEM_write_bio_PUBKEY(bio, remotePubKey);
int pubKeyLen = BIO_pending(bio);
*pubKey = (unsigned char*)malloc(pubKeyLen);
if(pubKey == NULL) return FAILURE;
BIO_read(bio, *pubKey, pubKeyLen);
// Insert the NUL terminator
(*pubKey)[pubKeyLen-1] = '\0';
BIO_free_all(bio);
return pubKeyLen;
}
int Crypto::setRemotePubKey(unsigned char* pubKey, size_t pubKeyLen) {
//BIO *bio = BIO_new(BIO_f_base64());
BIO *bio = BIO_new(BIO_s_mem());
if(BIO_write(bio, pubKey, pubKeyLen) != (int)pubKeyLen) {
return FAILURE;
}
RSA *_pubKey = (RSA*)malloc(sizeof(RSA));
if(_pubKey == NULL) return FAILURE;
PEM_read_bio_PUBKEY(bio, &remotePubKey, NULL, NULL);
BIO_free_all(bio);
return SUCCESS;
}
int Crypto::getLocalPubKey(unsigned char** pubKey) {
BIO *bio = BIO_new(BIO_s_mem());
PEM_write_bio_PUBKEY(bio, localKeypair);
int pubKeyLen = BIO_pending(bio);
*pubKey = (unsigned char*)malloc(pubKeyLen);
if(pubKey == NULL) return FAILURE;
BIO_read(bio, *pubKey, pubKeyLen);
// Insert the NUL terminator
(*pubKey)[pubKeyLen-1] = '\0';
BIO_free_all(bio);
return pubKeyLen;
}
int Crypto::getLocalPriKey(unsigned char **priKey) {
BIO *bio = BIO_new(BIO_s_mem());
PEM_write_bio_PrivateKey(bio, localKeypair, NULL, NULL, 0, 0, NULL);
int priKeyLen = BIO_pending(bio);
*priKey = (unsigned char*)malloc(priKeyLen + 1);
if(priKey == NULL) return FAILURE;
BIO_read(bio, *priKey, priKeyLen);
// Insert the NUL terminator
(*priKey)[priKeyLen] = '\0';
BIO_free_all(bio);
return priKeyLen;
}
int Crypto::getAESKey(unsigned char **aesKey) {
*aesKey = this->aesKey;
return AES_KEYLEN/8;
}
int Crypto::setAESKey(unsigned char *aesKey, size_t aesKeyLen) {
// Ensure the new key is the proper size
if((int)aesKeyLen != AES_KEYLEN/8) {
return FAILURE;
}
memcpy(this->aesKey, aesKey, AES_KEYLEN/8);
return SUCCESS;
}
int Crypto::getAESIv(unsigned char **aesIV) {
*aesIV = this->aesIV;
return AES_KEYLEN/16;
}
int Crypto::setAESIv(unsigned char *aesIV, size_t aesIVLen) {
// Ensure the new IV is the proper size
if((int)aesIVLen != AES_KEYLEN/16) {
return FAILURE;
}
memcpy(this->aesIV, aesIV, AES_KEYLEN/16);
return SUCCESS;
}
int Crypto::init() {
// Initalize contexts
rsaEncryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
aesEncryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
rsaDecryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
aesDecryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
// Always a good idea to check if malloc failed
if(rsaEncryptCtx == NULL || aesEncryptCtx == NULL || rsaDecryptCtx == NULL || aesDecryptCtx == NULL) {
return FAILURE;
}
// Init these here to make valgrind happy
EVP_CIPHER_CTX_init(rsaEncryptCtx);
EVP_CIPHER_CTX_init(aesEncryptCtx);
EVP_CIPHER_CTX_init(rsaDecryptCtx);
EVP_CIPHER_CTX_init(aesDecryptCtx);
// Init RSA
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
if(EVP_PKEY_keygen_init(ctx) <= 0) {
return FAILURE;
}
if(EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, RSA_KEYLEN) <= 0) {
return FAILURE;
}
if(EVP_PKEY_keygen(ctx, &localKeypair) <= 0) {
return FAILURE;
}
EVP_PKEY_CTX_free(ctx);
// Init AES
aesKey = (unsigned char*)malloc(AES_KEYLEN/8);
aesIV = (unsigned char*)malloc(AES_KEYLEN/8);
unsigned char *aesPass = (unsigned char*)malloc(AES_KEYLEN/8);
unsigned char *aesSalt = (unsigned char*)malloc(8);
if(aesKey == NULL || aesIV == NULL || aesPass == NULL || aesSalt == NULL) {
return FAILURE;
}
// For the AES key we have the option of using a PBKDF (password-baswed key derivation formula)
// or just using straight random data for the key and IV. Depending on your use case, you will
// want to pick one or another.
#ifdef USE_PBKDF
// Get some random data to use as the AES pass and salt
if(RAND_bytes(aesPass, AES_KEYLEN/8) == 0) {
return FAILURE;
}
if(RAND_bytes(aesSalt, 8) == 0) {
return FAILURE;
}
if(EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), aesSalt, aesPass, AES_KEYLEN/8, AES_ROUNDS, aesKey, aesIV) == 0) {
return FAILURE;
}
#else
if(RAND_bytes(aesKey, AES_KEYLEN/8) == 0) {
return FAILURE;
}
if(RAND_bytes(aesIV, AES_KEYLEN/8) == 0) {
return FAILURE;
}
#endif
free(aesPass);
free(aesSalt);
return SUCCESS;
}
int Crypto::genTestClientKey() {
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
if(EVP_PKEY_keygen_init(ctx) <= 0) {
return FAILURE;
}
if(EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, RSA_KEYLEN) <= 0) {
return FAILURE;
}
if(EVP_PKEY_keygen(ctx, &remotePubKey) <= 0) {
return FAILURE;
}
EVP_PKEY_CTX_free(ctx);
return SUCCESS;
}