-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
500 lines (408 loc) · 13.1 KB
/
utils.c
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#include "utils.h"
/*
* Computes the hash function SHA256 on "source" buffer, AND on the content of file "name_file" if it is non-NULL
* Returns the hash value, NULL in case of errors
*/
unsigned char* do_hash(unsigned char* source, int source_size, char* name_file){
int i;
unsigned char data[1024];
unsigned int digest_size = EVP_MD_size(HASH_FUN);
unsigned char* hash = malloc(digest_size);
int read_bytes;
FILE* input_file;
EVP_MD_CTX sha_ctx;
if( (source == NULL && name_file == NULL) || (source != NULL && source_size <= 0) ){
return NULL;
}
EVP_MD_CTX_init(&sha_ctx);
EVP_DigestInit(&sha_ctx, HASH_FUN);
//update sul file
if(name_file != NULL){
input_file = fopen(name_file, "r");
if(input_file==NULL){
printf("Impossible to open file.\n");
return NULL;
}
while( ( read_bytes = fread(data, 1, 1024, input_file) ) != 0 ){
EVP_DigestUpdate(&sha_ctx, data, read_bytes);
}
fclose(input_file);
}
//update sul buffer
if(source != NULL){
EVP_DigestUpdate(&sha_ctx, source, source_size);
}
EVP_DigestFinal(&sha_ctx, hash, &digest_size);
EVP_MD_CTX_cleanup(&sha_ctx);
/*--_DEBUG---
printf("sha256 of the passed data is: \n");
for(i = 0 ; i < digest_size ; i++)
printf("%02x", hash[i]);
printf("\n");*/
return hash;
}
/*
* Computes the hash function SHA256 on "source" buffer, AND on the content of file "name_file" if it is non-NULL
* then compares it with the content of "hash_val" buffer
* Returns 1 if the integrity is verified, 0 otherwise, -1 in case of errors
*/
int verify_hash(unsigned char* source, int source_size, char* name_file ,unsigned char* hash_val){
unsigned char* computed_hash;
int ret;
/*Checks*/
if( (source == NULL && name_file == NULL) || (source != NULL && source_size <= 0) ){
return -1;
}
/*Computes the hash function on source buffer and on file (if needed)*/
computed_hash = do_hash(source, source_size, name_file);
/*Checks if they are equals*/
ret = CRYPTO_memcmp(computed_hash, hash_val, EVP_MD_size(HASH_FUN));
free(computed_hash);
if(ret == 0)
return 1;
else
return 0;
}
/*
* Crypting function AES256-CBC on "source" buffer (if non-NULL), AND on the content of file "name_file" (if non-NULL)
* It is an error if "source" and "name_file" are both equal to NULL.
* Returns the ciphertext, NULL in case of errors
*/
unsigned char* sym_crypto_file(char* name_file, unsigned char* hash, int hash_size, unsigned char* key, int* cipher_len){
EVP_CIPHER_CTX* ctx;
FILE* fd;
unsigned char* ciphertext;
unsigned char* plaintext;
int res, msg_len, n;
int outlen, outlen_tot;
int ct_bufsize;
struct stat st;
int block_size = EVP_CIPHER_block_size(SYM_CIPHER);
//printf("block_size: %i\n", block_size);
if(hash == NULL || name_file == NULL || key == NULL)
return NULL;
fd = fopen(name_file, "r");
if(fd == NULL){
printf("Impossible to open %s file\n", name_file);
return NULL;
}
stat(name_file, &st); //file_size = st.st_size;
plaintext = (unsigned char*)malloc(st.st_size+hash_size); //plaintext grande come il file più hash
ctx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
EVP_CIPHER_CTX_init(ctx);
EVP_EncryptInit(ctx, SYM_CIPHER, key, NULL); //Cosa fare con l' IV?
//plaintext allocation
res=fread(plaintext, sizeof(char), st.st_size, fd); // in res ci sono i byte letti
if(res != st.st_size){
printf("reading error");
return NULL;
}
//append hash
memcpy(plaintext + st.st_size, hash, hash_size);
/* Buffer allocation for the ciphertext */
msg_len = st.st_size+hash_size;
ct_bufsize = msg_len + block_size;
ciphertext = (unsigned char*)malloc(ct_bufsize);
outlen = 0;
outlen_tot = 0;//dimensione testo output(ciphertext)
n=0;//dimensione testo input (plaintext)
while(n/block_size < msg_len/block_size){//block size serve nel caso in cui msg_len < block_size allora non devo entrare nel ciclo
EVP_EncryptUpdate(ctx, ciphertext + outlen_tot, &outlen, (unsigned char*)plaintext + n,block_size);
outlen_tot += outlen;
n += block_size;
}
EVP_EncryptUpdate(ctx, ciphertext + outlen_tot, &outlen, (unsigned char*)plaintext + n,msg_len % block_size);// cifro i byte restanti(quelli non multipli di block size
outlen_tot += outlen;
n += msg_len % block_size;
EVP_EncryptFinal(ctx, ciphertext + outlen_tot, &outlen);
outlen_tot += outlen;
EVP_CIPHER_CTX_cleanup(ctx);
free(ctx);
*cipher_len = outlen_tot;
//printf("ciphertext long: %i\n", outlen_tot);
return ciphertext;
}
/*
*
*/
unsigned char* sym_crypt(void* buf, int buf_size, unsigned char* key, int* cipher_len){
EVP_CIPHER_CTX* ctx;
FILE* fd;
unsigned char* ciphertext;
int res, msg_len, n;
int outlen, outlen_tot;
int ct_bufsize;
int block_size = EVP_CIPHER_block_size(SYM_CIPHER);
//printf("block_size: %i\n", block_size);
if(buf == NULL || key == NULL)
return NULL;
ctx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
EVP_CIPHER_CTX_init(ctx);
EVP_EncryptInit(ctx, SYM_CIPHER, key, NULL); //Cosa fare con l' IV?
/* Buffer allocation for the ciphertext */
msg_len = buf_size;
ct_bufsize = msg_len + block_size;
ciphertext = (unsigned char*)malloc(ct_bufsize);
outlen = 0;
outlen_tot = 0;//dimensione testo output(ciphertext)
n=0;//dimensione testo input (plaintext)
while(n/block_size < msg_len/block_size){//block size serve nel caso in cui msg_len < block_size allora non devo entrare nel ciclo
EVP_EncryptUpdate(ctx, ciphertext + outlen_tot, &outlen, (unsigned char*)buf + n,block_size);
outlen_tot += outlen;
n += block_size;
}
EVP_EncryptUpdate(ctx, ciphertext + outlen_tot, &outlen, (unsigned char*)buf + n,msg_len % block_size);// cifro i byte restanti(quelli non multipli di block size
outlen_tot += outlen;
n += msg_len % block_size;
EVP_EncryptFinal(ctx, ciphertext + outlen_tot, &outlen);
outlen_tot += outlen;
EVP_CIPHER_CTX_cleanup(ctx);
free(ctx);
*cipher_len = outlen_tot;
return ciphertext;
}
/*
* NB: questa funzione restituisce un buffer più grande del dovuto per cui
* fare sempre riferimento al valore di output_len
*/
char* sym_decrypt(unsigned char* cipher, int cipher_len, unsigned char* key, int* output_len){
char* plaintext;
int outlen, outlen_tot, n, len, res;
int block_size = EVP_CIPHER_block_size(SYM_CIPHER);
if(cipher == NULL || key == NULL){
return NULL;
}
plaintext = (char*)malloc(cipher_len + block_size);
EVP_CIPHER_CTX* ctx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
EVP_CIPHER_CTX_init(ctx);
EVP_DecryptInit(ctx, SYM_CIPHER, key, NULL);
len = cipher_len; //passaggio inutile ma serve per mantenere una continuità con le funzioni precedenti
/* Decryption */
outlen = 0;
outlen_tot = 0;// dim testo output (plaintext)
n=0;//dim testo input (ciphertext)
while(n/block_size < len/block_size){
EVP_DecryptUpdate(ctx, plaintext+ outlen_tot, &outlen, (char*)cipher+ n, block_size);
outlen_tot += outlen;
n += block_size;
}
EVP_DecryptUpdate(ctx, plaintext+ outlen_tot, &outlen, (char*)cipher+ n, len % block_size);
outlen_tot += outlen;
n += len % block_size;
res = EVP_DecryptFinal(ctx, plaintext + outlen_tot, &outlen);
/*if(res == 0){
printf("ERROR in decrypting.\n");
return NULL;
}*/
outlen_tot += outlen;
EVP_CIPHER_CTX_cleanup(ctx);
*output_len = outlen_tot;
return plaintext;
}
EVP_PKEY* retrieve_pubkey(const char* file_name) {
FILE* file;
EVP_PKEY* pubkey;
file = fopen(file_name, "r");
if(file == NULL){
fprintf(stderr, "Error: cannot read PEM file '%s'\n", file_name);
return NULL;
}
pubkey = PEM_read_PUBKEY(file, NULL, NULL, NULL);
fclose(file);
if(pubkey == NULL){
fprintf(stderr, "Error: PEM_read_PUBKEY returned NULL\n");
return NULL;
}
return pubkey;
}
/*
* RSA public cryptography function.
* Encrypt 'in_len' bytes from 'plaintext' buffer with the public key contained in 'pub_key_file'
* Parameter 'outlen' is the size of output buffer
* It returns the envelope which has to be sent to the receiver or NULL if an error occurs.
*/
unsigned char* asym_crypto(unsigned char* plaintext, int in_len, int* out_len, char* pub_key_file){
EVP_PKEY* pubkey;
int ret;
EVP_CIPHER_CTX* ctx;
unsigned char* encrypted_key;
int encrypted_key_len;
unsigned char* iv;
int iv_len;
unsigned char* ciphertext;
int cipher_len;
int app;
unsigned char* output;
int total_output_size = 0;
if(plaintext == NULL || in_len < 0 || out_len == NULL || pub_key_file == NULL)
return NULL;
//Reads the receiver's public key for its file
pubkey = retrieve_pubkey(pub_key_file);
if(pubkey == NULL)
return NULL;
encrypted_key_len = EVP_PKEY_size(pubkey);
total_output_size += encrypted_key_len;
iv_len = EVP_CIPHER_iv_length(SYM_CIPHER);
total_output_size += iv_len;
//Allocation of encrypted symmetric key and initialization vector
encrypted_key = malloc(encrypted_key_len);
iv = malloc(iv_len);
//Seeding the RNG
RAND_seed(iv, 8);
//Instantiate and initialize the context
ctx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
EVP_CIPHER_CTX_init(ctx);
ret = EVP_SealInit(ctx, SYM_CIPHER, &encrypted_key, &encrypted_key_len, iv, &pubkey ,1);
if(ret == 0){
fprintf(stderr, "Error in SealInit\n");
goto error;
}
//Encrypt the input buffer
cipher_len = in_len + EVP_CIPHER_block_size(SYM_CIPHER);
ciphertext = malloc(cipher_len);
cipher_len = 0;
ret = EVP_SealUpdate(ctx, ciphertext, &app, plaintext, in_len);
cipher_len += app;
if(ret == 0){
fprintf(stderr, "Error in SealUpdate\n");
goto error;
}
ret = EVP_SealFinal(ctx, ciphertext + app, &app);
cipher_len += app;
if(ret == 0){
fprintf(stderr, "Error in SealFinal\n");
goto error;
}
total_output_size += cipher_len;
//Concatenates the envelop in the outbuffer with format: <IV><Dim_Key><Ecrypt_KEY><Ciphertext>
total_output_size += sizeof(int);
output = malloc(total_output_size);
app = 0;
//<IV>
memcpy(output, iv, iv_len);
app += iv_len;
//<Dim_Key>
memcpy(output + app, &encrypted_key_len, sizeof(int));
app += sizeof(int);
//<Ecrypt_KEY>
memcpy(output + app, encrypted_key, encrypted_key_len);
app += encrypted_key_len;
//<Ciphertext>
memcpy(output + app, ciphertext, cipher_len);
app += encrypted_key_len;
*out_len = total_output_size;
//Cleanup
EVP_CIPHER_CTX_cleanup(ctx);
free(ctx);
free(ciphertext);
free(pubkey);
free(iv);
return output;
error:
if(ctx != NULL){
EVP_CIPHER_CTX_cleanup(ctx);
free(ctx);
}
if(encrypted_key != NULL)
free(ciphertext);
if(pubkey != NULL)
free(pubkey);
if(iv != NULL)
free(iv);
if(ciphertext != NULL)
free(ciphertext);
if(output != NULL)
free(output);
return NULL;
}
EVP_PKEY* read_priv_key(const char* file_name){
FILE* fp;
EVP_PKEY* priv_key;
fp = fopen(file_name, "r");
if(fp == NULL){
fprintf(stderr, "Error: cannot read PEM file '%s'\n", file_name);
return NULL;
}
priv_key = PEM_read_PrivateKey(fp, NULL, NULL, NULL);
fclose(fp);
if(priv_key == NULL){
fprintf(stderr, "Error: PEM_read_PrivateKey returned NULL\n");
return NULL;
}
return priv_key;
}
/*
* RSA public decryption function.
* Dencrypt 'in_len' bytes from 'envelope' buffer with the private key contained in 'priv_key_file'
* Parameter 'outlen' is the size of output buffer
* It returns the received plaintext or NULL if an error occurs.
*/
unsigned char* asym_decrypt(unsigned char* envelope, int in_len, int* out_len, char* priv_key_file){
EVP_PKEY* priv_key;
int ret;
EVP_CIPHER_CTX* ctx;
unsigned char* encrypted_key;
int encrypted_key_len;
unsigned char* iv;
int iv_len;
unsigned char* ciphertext;
int ciphertext_len;
unsigned char* output;
int output_len;
int app;
if(ciphertext == NULL || in_len < 0 || out_len == NULL || priv_key_file == NULL)
return NULL;
//Reads the receiver's public key for its file
priv_key = read_priv_key(priv_key_file);
//Note: envelope format -> <IV><Dim_Key><Ecrypt_KEY><Ciphertext>
//Set to the head the iv pointer
iv = envelope;
iv_len = EVP_CIPHER_iv_length(SYM_CIPHER);
//Read from the envelop the encrypted_key_len
memcpy(&encrypted_key_len, envelope + iv_len, sizeof(int));
//Set the encrypted_key pointer
encrypted_key = envelope + iv_len + sizeof(int);
//Set the ciphertext pointer
ciphertext = envelope + iv_len + sizeof(int) + encrypted_key_len;
ciphertext_len = in_len - iv_len - sizeof(int) - encrypted_key_len;
//Instantiate and initialize the context
ctx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
EVP_CIPHER_CTX_init(ctx);
ret = EVP_OpenInit(ctx, SYM_CIPHER, encrypted_key, encrypted_key_len, iv, priv_key);
if(ret == 0)
goto error;
//Decrypt the ciphertext
output_len = ciphertext_len;
output = malloc(output_len);
output_len = 0;
ret = EVP_OpenUpdate(ctx, output, &app, ciphertext, ciphertext_len);
if(ret == 0)
goto error;
output_len += app;
ret = EVP_OpenFinal(ctx, output + app, &app);
if(ret == 0)
goto error;
output_len += app;
*out_len = output_len;
//Cleanup
EVP_CIPHER_CTX_cleanup(ctx);
free(ctx);
free(priv_key);
return output;
error:
if(ctx != NULL){
EVP_CIPHER_CTX_cleanup(ctx);
free(ctx);
}
if(encrypted_key != NULL)
free(ciphertext);
if(priv_key != NULL)
free(priv_key);
if(iv != NULL)
free(iv);
if(ciphertext != NULL)
free(ciphertext);
if(output != NULL)
free(output);
return NULL;
}