-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes256.c
316 lines (259 loc) · 8.03 KB
/
aes256.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
/*
* Author: Rodrigo Gutiérrez (r.gutierrezc80@gmail.com)
* AES implementation: https://github.com/kokke/tiny-AES-c
* Topics covered: File IO, string handling, using 3rd party code
*
*/
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <termios.h>
// include encryption header
#include "aes.h"
#define MAX_KEY_SIZE 32 // size in bytes. 32 for AES256, 24 AES192, 16 AES128
// modes
#define ENCRYPTION_MODE 0
#define DECRYPTION_MODE 1
#define UNDEFINED_MODE -1
struct AES_ctx;
off_t fsize(const char *filename) {
struct stat st;
return (stat(filename, &st) == 0 ? st.st_size : -1);
}
// designed with PKCS7 padding in mind
uint8_t calculate_padding(uint32_t size, uint8_t block) {
return (size%block==0 ? block : block - (size%block));
}
// both functions from:
// https://stackoverflow.com/questions/4553012/checking-if-a-file-is-a-directory-or-just-a-file
int is_regular_file(const char *path)
{
struct stat path_stat;
stat(path, &path_stat);
return S_ISREG(path_stat.st_mode);
}
void getPassword(char password[])
{
static struct termios oldt, newt;
int i = 0;
int c;
/*saving the old settings of STDIN_FILENO and copy settings for resetting*/
tcgetattr( STDIN_FILENO, &oldt);
newt = oldt;
/*setting the approriate bit in the termios struct*/
newt.c_lflag &= ~(ECHO);
/*setting the new bits*/
tcsetattr( STDIN_FILENO, TCSANOW, &newt);
/*reading the password from the console*/
while ((c = getchar())!= '\n' && c != EOF && i < MAX_KEY_SIZE){
password[i++] = c;
}
password[i] = '\0';
/*resetting our old STDIN_FILENO*/
tcsetattr( STDIN_FILENO, TCSANOW, &oldt);
}
int isDirectory(const char *path) {
struct stat statbuf;
if (stat(path, &statbuf) != 0)
return 0;
return S_ISDIR(statbuf.st_mode);
}
void processFile(const char* file, int8_t enc_mode, struct AES_ctx* ctx) {
FILE *fp;
char ch;
fp = fopen(file, "r"); // read mode
if (fp == NULL)
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
uint32_t total_size=0, file_size = 0;
uint8_t padding_space=0;
if (enc_mode==ENCRYPTION_MODE) {
// adding padding: PKCS7
padding_space = calculate_padding(fsize(file),AES_BLOCKLEN);
file_size = fsize(file);
total_size = fsize(file)+padding_space;
} else if (enc_mode==DECRYPTION_MODE){
// if magic bytes not present, error and exit.
fseek(fp,-16,SEEK_END);
char magic_bytes[16];
for (int i=0;i<16;i++) {
magic_bytes[i]=fgetc(fp);
}
//printf("magic bytes leidos: [%s]\n",magic_bytes);
if(strcmp(magic_bytes,"b30WuLf_y0u2L02\0")!=0) {
printf("Not an encrypted file, aborting...\n");
exit(EXIT_FAILURE);
}
fseek(fp,0,SEEK_SET);
total_size = fsize(file)-16;
printf("total size calculado es %d\n", total_size);
} else {
printf("Unknown operation mode, something weird is going on. Terminating.\n");
exit(EXIT_FAILURE);
}
// if we store the array in the stack, we might cause a
// segmentation fault if the file exceeds the allocated stack size
// malloc allocates space in the heap, no space problems! (hopefully...)
uint8_t* in = malloc(total_size);
// max file size is 2^32 bit, > 4TB
uint32_t x = 0;
while(x < (enc_mode==ENCRYPTION_MODE ? file_size : total_size)) {
ch = fgetc(fp);
in[x++] = ch;
}
fclose(fp);
// the beauty of PKCS7 lies on its simplicity:
// every byte of the padding contains
// the number of padded bytes, so even if the last byte of data
// before padding was that same number, we would never remove it
// after the file was decrypted
if (enc_mode==ENCRYPTION_MODE) {
while (x < total_size) {
in[x++] = padding_space;
}
// all data in place, let's move on with encryption
AES_CBC_encrypt_buffer(ctx, in, total_size);
} else {
AES_CBC_decrypt_buffer(ctx, in, total_size);
}
char dest_path[255];
strcpy(dest_path, file);
FILE *f = fopen(dest_path, "w");
// en in[total_size] -1 tenemos el numero de bytes del padding
fwrite(in, sizeof(char), (enc_mode==ENCRYPTION_MODE?total_size:total_size-in[total_size-1]), f);
fclose(f);
if (enc_mode==ENCRYPTION_MODE) {
//add magic bytes so we know that its our encrypted file.
f = fopen(dest_path,"a");
char* magic_bytes = (char*)malloc(16 * sizeof(uint8_t));
strcpy(magic_bytes, "b30WuLf_y0u2L02\0");
fwrite(magic_bytes,sizeof(uint8_t),16, f);
fclose(f);
}
}
void processDirEntry(const char* dir_entry, int8_t enc_mode, struct AES_ctx* ctx) {
if(is_regular_file(dir_entry)) {
if (enc_mode==ENCRYPTION_MODE)
printf("Encrypting ");
else printf("Decrypting ");
printf("file %s...\n",dir_entry);
processFile(dir_entry, enc_mode, ctx);
} else if (isDirectory(dir_entry)) {
printf("%s is a directory\n",dir_entry);
DIR *d;
struct dirent *dir;
d = opendir(dir_entry);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (strcmp(dir->d_name,".")!=0 && strcmp(dir->d_name,"..")!=0) {
char path[255];
strcpy(path, dir_entry);
strcat(path, "/");
strcat(path, dir->d_name);
processDirEntry(path, enc_mode, ctx);
}
}
closedir(d);
} else { printf("Couldn't open %s dir. Check permissions.\n",dir_entry);}
} else {
printf("Couldn't open %s.\n", dir_entry);
}
}
int main(int argc, char **argv) {
// using AES256 so we need 128 bit key
// we repeat what we read from keyboard until we get 256 bits
// todo: give option to read all files in folder and subfolders
// and cypher them
int8_t mode = UNDEFINED_MODE;
uint8_t mode_index = -1;
// if not a single argument is given, display usage error
if (argc < 2 ) {
printf("Usage: aes256 [-c|-d] [file|dir] [file|dir] ...\n");
printf("-c : encrypt, -d : decrypt. Folders passed will recursively (de)encrypt all files inside, subfolders included.\n");
printf("Error: [-c|-d] option is mandatory.\n");
exit(EXIT_FAILURE);
}
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-d")==0 ) {
if ( mode ==UNDEFINED_MODE) {
mode = DECRYPTION_MODE;
mode_index = i;
} else {
printf("Wrong arguments. Please use either -c or -d once.\n");
exit(EXIT_FAILURE);
}
} else if (strcmp(argv[i], "-c")==0) {
if ( mode ==UNDEFINED_MODE) {
mode = ENCRYPTION_MODE;
mode_index = i;
} else {
printf("Wrong arguments. Please use either -c or -d once.\n");
exit(EXIT_FAILURE);
}
}
}
if (mode == UNDEFINED_MODE) {
printf("Wrong arguments. Please use either -c or -d once.\n");
exit(EXIT_FAILURE);
}
// request password from user
uint8_t key[MAX_KEY_SIZE];
uint8_t repeat[MAX_KEY_SIZE];
char ch, user_input[512],*p;
FILE *fp;
printf("Please input password: ");
printf("\n");
getPassword(key);
printf("Please type it again: ");
getPassword(repeat);
if(strcmp(key,repeat)!=0) {
printf("Passwords did not match, aborting.\n");
exit(EXIT_FAILURE);
}
// fgets(key, MAX_KEY_SIZE, stdin);
printf("\n");
printf("The following files and folders will try to be ");
printf(mode==ENCRYPTION_MODE ? "ENCRYPTED!: " : "DECRYPTED!: ");
for (int i=1;i<argc;i++) {
if (i!=mode_index) {
printf("[%s] ",argv[i]);
}
}
printf("\nDo you want to continue? [Y/n]: ");
char reply = getchar();
if (reply=='Y') {
} else { exit(EXIT_SUCCESS);};
// find the end of key
uint8_t newLine = MAX_KEY_SIZE;
for (int i=0; i< MAX_KEY_SIZE;i++) {
if (key[i] == '\0') {
newLine = i;
break;
}
}
// key input by user is less than 32byte, we need to fill by repetition
uint16_t passLength = newLine;
while (newLine < MAX_KEY_SIZE) {
key[newLine] = key[newLine - passLength];
newLine++;
}
// our secret formula to generate 128bit IV:
uint8_t iv[] = { key[2], key[15], key[15], key[8], key[3], key[2], key[3], key[12],
key[0], key[1], key[7], key[6], key[7], key[1],key[11], key[2] };
struct AES_ctx ctx;
AES_init_ctx_iv(&ctx, key, iv);
for (int i=1; i<argc; i++) {
if (i!=mode_index) {
processDirEntry(argv[i], mode, &ctx);
}
}
return 0;
}