-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
134 lines (110 loc) · 3.84 KB
/
main.c
File metadata and controls
134 lines (110 loc) · 3.84 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
#include <stdio.h>
#include "crypto.h"
void print_help(LPTSTR app) {
_tprintf(TEXT("cngcrypt (built on "
__TIMESTAMP__
")\n"
"Author: Thomas Lienbacher <lienbacher.tom@gmail.com>\n"
"Small file encryption CLI using the Windows CNG API and AES-128\n"
"\n"
"USAGE:\n"
" %s [FLAGS] <SECRET> <INPUT> <OUTPUT>\n"
"\n"
"FLAGS:\n"
" -e Encrypt input file to output file\n"
" -d Decrypt input file to output file\n"
" -S Print derived AES key and IV\n"
" -h Prints help information and exits\n"
"\nEncryption and decryption are mutually exclusive\n"
"\n"),
app);
}
char find_arg(LPTSTR *argv, int argc, LPTSTR arg) {
for (int i = 0; i < argc; ++i) {
if (_tcscmp(argv[i], arg) == 0) return 1;
}
return 0;
}
static inline void print_hex(unsigned char *data, int len) {
for (int i = 0; i < len; ++i) {
_tprintf(TEXT("%02x"), data[i]);
}
}
int main(int _argc, char *_argv[]) {
LPTSTR *szArglist;
int nArgs;
#ifdef UNICODE
szArglist = CommandLineToArgvW(GetCommandLine(), &nArgs);
#else
szArglist = _argv;
nArgs = _argc;
#endif
if (nArgs < 2) {
_tprintf(TEXT("Not enough arguments supplied!\n"
"Use -h to see help\n"));
return 1;
}
char flagEncrypt = find_arg(szArglist + 1, nArgs - 1, TEXT("-e"));
char flagDecrypt = find_arg(szArglist + 1, nArgs - 1, TEXT("-d"));
char flagPrintKeys = find_arg(szArglist + 1, nArgs - 1, TEXT("-S"));
char flagHelp = find_arg(szArglist + 1, nArgs - 1, TEXT("-h"));
if (!(flagEncrypt || flagDecrypt || flagPrintKeys || flagHelp)) {
_tprintf(TEXT("Wrong arguments supplied!\n"
"Use -h to see help\n"));
return 1;
}
if (flagHelp) {
print_help(szArglist[0]);
return 1;
}
if (flagDecrypt && flagEncrypt) {
_tprintf(TEXT("Can't encrypt and decrypt simultaneously!\n"
"Use -h to see help\n"));
return 1;
}
DWORD len = 0;
LPTSTR secret = szArglist[nArgs - 3];
BYTE *keyHash = hash(&len, secret, lstrlen(secret) * sizeof(TCHAR));
BYTE key[16] = {0};
data_half_collapse(key, keyHash, len);
LPTSTR input = szArglist[nArgs - 2];
LPTSTR output = szArglist[nArgs - 1];
//app flag S key input output
if (flagEncrypt) {
if (nArgs < (5 + flagPrintKeys)) {
_tprintf(TEXT("Not enough arguments supplied!\n"
"Use -h to see help\n"));
return 1;
}
UCHAR iv[BLOCK_SIZE];
secure_random(iv, BLOCK_SIZE);
if (flagPrintKeys) {
_tprintf(TEXT("AES Key: "));
print_hex(key, 16);
_tprintf(TEXT("\nIV: "));
print_hex(iv, 16);
_tprintf(TEXT("\n"));
}
_tprintf(TEXT("Encrypting: %s => %s\n"), input, output);
encrypt(input, output, key, iv);
}
if (flagDecrypt) {
if (nArgs < (5 + flagPrintKeys)) {
_tprintf(TEXT("Not enough arguments supplied!\n"
"Use -h to see help\n"));
return 1;
}
if (flagPrintKeys) {
_tprintf(TEXT("AES Key: "));
print_hex(key, 16);
_tprintf(TEXT("\n"));
}
_tprintf(TEXT("Decrypting: %s => %s\n"), input, output);
decrypt(input, output, key);
}
free(keyHash);
#ifdef UNICODE
LocalFree(szArglist);
#endif
return 0;
}