-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmi-thermal-crypt.c
67 lines (51 loc) · 1.77 KB
/
mi-thermal-crypt.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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <openssl/evp.h>
#include <openssl/aes.h>
#include "cmdline.h"
// taken from https://stackoverflow.com/a/24899425
/**
* Encrypt or decrypt, depending on flag 'should_encrypt'
*/
void en_de_crypt(int should_encrypt, FILE *ifp, FILE *ofp, unsigned char *ckey, unsigned char *ivec) {
const unsigned BUFSIZE=4096;
unsigned char *read_buf = malloc(BUFSIZE);
unsigned char *cipher_buf;
unsigned blocksize;
int out_len;
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
EVP_CIPHER_CTX_init(ctx);
EVP_CipherInit(ctx, EVP_aes_128_cbc(), ckey, ivec, should_encrypt);
blocksize = EVP_CIPHER_CTX_block_size(ctx);
cipher_buf = malloc(BUFSIZE + blocksize);
while (1) {
// Read in data in blocks until EOF. Update the ciphering with each read.
int numRead = fread(read_buf, sizeof(unsigned char), BUFSIZE, ifp);
EVP_CipherUpdate(ctx, cipher_buf, &out_len, read_buf, numRead);
fwrite(cipher_buf, sizeof(unsigned char), out_len, ofp);
if (numRead < BUFSIZE) { // EOF
break;
}
}
// Now cipher the final block and write it out.
EVP_CipherFinal(ctx, cipher_buf, &out_len);
fwrite(cipher_buf, sizeof(unsigned char), out_len, ofp);
// Free memory
EVP_CIPHER_CTX_free(ctx);
free(cipher_buf);
free(read_buf);
}
int main(int argc, char *argv[]) {
struct gengetopt_args_info ai;
unsigned char ckey[] = "thermalopenssl.h";
unsigned char ivec[] = "thermalopenssl.h";
FILE *fIN, *fOUT;
if (cmdline_parser(argc, argv, &ai) != 0) {
exit(1);
}
fIN = fopen(ai.infile_arg, "rb");
fOUT = fopen(ai.outfile_arg, "wb");
en_de_crypt(ai.encrypt_flag, fIN, fOUT, ckey, ivec);
return 0;
}