-
Notifications
You must be signed in to change notification settings - Fork 4
/
encrypt.c
92 lines (77 loc) · 2.74 KB
/
encrypt.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
/*
Copyright (C) 2016 Yann Diorcet
This file is part of IDS. IDS 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, version 2.
This program 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 program; if not, write to the Free Software Foundation, Inc., 51
Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/evp.h>
#define EVP_CHECK(q, r) {int ret = q;if(ret != r){fprintf(stderr, "Error executing %s result: %d != %d\n", #q, ret, r); goto fail;}}
#define TRY_FREE(b) {if(b!=NULL){free(b);}}
long int get_file_size(FILE *file) {
fseek(file, 0L, SEEK_END);
long int l = ftell(file);
fseek(file, 0L, SEEK_SET);
return l;
}
int main(int argc, char *argv[]) {
int ret = 0;
if (argc != 3) {
fprintf(stderr, "No arg\n");
return -1;
}
FILE *password_file = fopen(argv[1], "rb");
long int password_size = get_file_size(password_file);
char *password = (char *) malloc(password_size);
fread(password, 1, password_size, password_file);
fclose(password_file);
FILE *file = fopen(argv[2], "rb");
long int file_size = get_file_size(file);
char *content = (char *) malloc(file_size);
fread(content, 1, file_size, file);
fclose(file);
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
const EVP_CIPHER *cipher = EVP_des_ede3_cbc();
const EVP_MD *md = EVP_md5();
char salt[8];
memset(salt, '\0', 8);
RAND_bytes(salt, 8);
char *key = (char *) malloc(32);
memset(key, '\0', 32);
char *iv = (char *) malloc(16);
memset(iv, '\0', 16);
char *out = NULL;
EVP_CHECK(EVP_BytesToKey(cipher, md, salt, password, password_size, 1, key, iv), 24);
EVP_CHECK(EVP_EncryptInit_ex(&ctx, cipher, 0, key, iv), 1);
size_t ctxbz = EVP_CIPHER_CTX_block_size(&ctx);
int outl;
out = (char *) malloc(file_size + ctxbz);
EVP_CHECK(EVP_EncryptUpdate(&ctx, out, &outl, content, file_size), 1);
int outl2;
EVP_CHECK(EVP_EncryptFinal_ex(&ctx, out + outl, &outl2), 1);
EVP_CHECK(EVP_CIPHER_CTX_cleanup(&ctx), 1);
fputs("Salted__", stdout);
fwrite(salt, 1, 8, stdout);
fwrite(out, 1, outl + outl2, stdout);
fflush(stdout);
goto exit;
fail:
ret = 1;
exit:
TRY_FREE(password);
TRY_FREE(key);
TRY_FREE(iv);
TRY_FREE(out);
free(content);
return ret;
}