Rev 8472 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
8542 | superturbo | 1 | /* Copyright (C) 2019-2021 Logaev Maxim (turbocat2001), GPLv2 */ |
8251 | superturbo | 2 | |
3 | #include |
||
4 | #include |
||
5 | #include |
||
6 | #include |
||
7 | #include |
||
8 | |||
8472 | superturbo | 9 | #include "lang_en.c" |
10 | #include "tea.c" |
||
11 | |||
8251 | superturbo | 12 | #define ENCRYPT 1 |
13 | #define DECRYPT 2 |
||
14 | |||
15 | typedef unsigned char flag; |
||
16 | uint32_t key[4]; |
||
17 | |||
18 | long size_orig_file(FILE* file) |
||
19 | { |
||
20 | fseek(file, 0, SEEK_END); |
||
21 | long size = ftell(file); |
||
22 | fseek(file, 0, SEEK_SET); |
||
23 | return size; |
||
24 | } |
||
25 | |||
26 | long size_xcrypt_file(FILE* file) |
||
27 | { |
||
28 | fseek(file, 0, SEEK_END); |
||
29 | long size = ftell(file); |
||
30 | fseek(file, 0, SEEK_SET); |
||
8472 | superturbo | 31 | if(size%8==0) { |
8251 | superturbo | 32 | return size; |
8472 | superturbo | 33 | }else{ |
8251 | superturbo | 34 | return (size/8+1)*8; |
35 | } |
||
36 | } |
||
37 | |||
38 | void xcrypt_file_speed(char *in_file, char* out_file, char arg) |
||
39 | { |
||
40 | FILE *input, *output; |
||
41 | |||
8472 | superturbo | 42 | if((input = fopen(in_file,"rb"))==NULL){ |
8251 | superturbo | 43 | printf(FILE_NOT_FOUND, in_file); |
44 | exit(1); |
||
45 | } |
||
46 | |||
47 | output = fopen(out_file,"wb"); |
||
48 | |||
49 | long size_f=size_xcrypt_file(input); |
||
50 | uint8_t size_diff; |
||
51 | size_diff=(uint8_t)(size_f-size_orig_file(input)); |
||
52 | uint32_t *buff; |
||
53 | buff=malloc(size_f); |
||
8472 | superturbo | 54 | if(!buff) { |
8251 | superturbo | 55 | puts(MEMORY_ERROR); |
56 | exit(-1); |
||
57 | } |
||
58 | |||
8472 | superturbo | 59 | if(arg==ENCRYPT){ |
8251 | superturbo | 60 | printf(LOAD_IN_RAM,in_file); |
61 | fread(buff, 1,size_f, input); |
||
62 | printf(FILE_ENCRYPTION); |
||
63 | |||
64 | for(long i=0; i<(size_f/4); i=i+2) |
||
65 | { |
||
66 | TEA_encrypt(buff+i,key); |
||
67 | } |
||
68 | |||
69 | printf(RECORD_ENCRYPT_DATA); |
||
70 | fwrite(&size_diff,1, 1, output); |
||
71 | fwrite(buff,1,size_f, output); |
||
72 | fclose(input); |
||
73 | fclose(output); |
||
74 | printf(DATA_ENCRYPT,in_file,out_file); |
||
75 | exit(0); |
||
76 | } |
||
77 | |||
8472 | superturbo | 78 | else if(arg==DECRYPT){ |
8251 | superturbo | 79 | long size_f=size_orig_file(input); |
80 | printf(LOAD_IN_RAM,in_file); |
||
81 | fread(&size_diff,1,1,input); |
||
82 | fread(buff,1,size_f-1, input); |
||
83 | printf(FILE_DECRYPTION); |
||
84 | |||
85 | for(long i=0; i |
||
86 | { |
||
87 | TEA_decrypt(buff+i,key); |
||
88 | } |
||
89 | |||
90 | printf(RECORD_DECRYPT_DATA); |
||
91 | fwrite(buff,1,size_f-size_diff-1, output); |
||
92 | fclose(input); |
||
93 | fclose(output); |
||
94 | printf(DATA_DECRYPT,in_file,out_file); |
||
95 | exit(0); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | void xcrypt_file(char *in_file, char* out_file, char arg) |
||
100 | { |
||
101 | uint32_t temp_block[2]; |
||
102 | FILE *input, *output; |
||
103 | |||
8472 | superturbo | 104 | if((input = fopen(in_file,"rb"))==NULL){ |
8251 | superturbo | 105 | printf(FILE_NOT_FOUND, in_file); |
106 | exit(1); |
||
107 | } |
||
108 | |||
109 | output = fopen(out_file,"wb"); |
||
110 | |||
111 | register long size_f=size_xcrypt_file(input); |
||
112 | uint8_t size_diff=(uint8_t)(size_f-size_orig_file(input)); |
||
113 | |||
114 | if(arg==ENCRYPT){ |
||
115 | fwrite(&size_diff,1,1,output); |
||
116 | printf(FILE_ENCRYPTION); |
||
117 | |||
118 | while(!feof(input)) |
||
119 | { |
||
120 | memset(temp_block, 0x00, 2); |
||
121 | fread(temp_block, sizeof(uint32_t), 2, input) ; |
||
122 | TEA_encrypt(temp_block,key); |
||
123 | fwrite(temp_block, sizeof(uint32_t),2, output); |
||
124 | } |
||
125 | |||
126 | fclose(input); |
||
127 | fclose(output); |
||
128 | printf(DATA_ENCRYPT,in_file,out_file); |
||
129 | exit(0); |
||
130 | |||
131 | } |
||
132 | else if(arg==DECRYPT){ |
||
133 | size_f = size_orig_file(input); |
||
134 | fread(&size_diff,1,1,input); |
||
135 | size_f=size_f-size_diff-1; |
||
136 | printf(FILE_DECRYPTION); |
||
137 | |||
138 | while(!feof(input)) |
||
139 | { |
||
140 | fread(temp_block, sizeof(uint32_t), 2, input); |
||
141 | TEA_decrypt(temp_block,key); |
||
142 | |||
8472 | superturbo | 143 | if(size_f>=8){ |
8251 | superturbo | 144 | fwrite(temp_block,sizeof(uint32_t),2,output); |
8472 | superturbo | 145 | }else{ |
8251 | superturbo | 146 | fwrite(temp_block,1,size_f,output); |
147 | } |
||
8472 | superturbo | 148 | |
8251 | superturbo | 149 | size_f=size_f-8; |
150 | |||
8472 | superturbo | 151 | if(size_f<0){ |
8251 | superturbo | 152 | size_f=0; |
153 | } |
||
154 | } |
||
155 | |||
156 | fclose(input); |
||
157 | fclose(output); |
||
158 | printf(DATA_DECRYPT,in_file,out_file); |
||
159 | exit(0); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | |||
164 | |||
165 | void str_to_strkey(char *str, char str_key[4][9]) |
||
166 | { |
||
167 | int count=0; |
||
168 | for(int i=0; i<4; i++) |
||
169 | { |
||
170 | int j=0; |
||
171 | while (j<8) |
||
172 | { |
||
173 | str_key[i][j]=str[count]; |
||
174 | count++; |
||
175 | j++; |
||
176 | } |
||
177 | } |
||
178 | } |
||
179 | |||
180 | int valid_key(char *str) |
||
181 | { |
||
182 | int count=0; |
||
183 | char hex[]={"abcdefABCDEF0123456789"}; |
||
184 | for(int i=0; i<32; i++) |
||
185 | { |
||
8472 | superturbo | 186 | if(strchr(hex,str[i])!=NULL){ |
8251 | superturbo | 187 | count++; |
188 | } |
||
189 | } |
||
190 | if(count==32){return 1;} |
||
191 | else{ return 0;} |
||
192 | } |
||
193 | |||
194 | |||
195 | void key_con_read(char *str) |
||
196 | { |
||
197 | char str_key[4][9]; |
||
198 | if(valid_key(str)&&(strlen(str)==32)) |
||
199 | { |
||
8472 | superturbo | 200 | for(int i=0; i<4; i++){ |
8251 | superturbo | 201 | str_to_strkey(str, str_key); |
202 | key[i]=(uint32_t)strtol(str_key[i],NULL,16); |
||
203 | } |
||
8472 | superturbo | 204 | }else{ |
8251 | superturbo | 205 | printf(INVALID_KEY_FORMAT); |
206 | exit(-1); |
||
207 | } |
||
208 | } |
||
209 | |||
210 | void key_file_read(char *key_file) |
||
211 | { |
||
212 | FILE *keyfile; |
||
8472 | superturbo | 213 | if((keyfile = fopen(key_file,"rb"))==NULL){ |
8251 | superturbo | 214 | printf(FILE_NOT_FOUND, key_file); |
215 | exit(-1); |
||
216 | } |
||
217 | |||
8472 | superturbo | 218 | if(size_orig_file(keyfile)==16) { |
219 | fread(key,sizeof(uint32_t),4, keyfile); |
||
220 | }else{ |
||
8251 | superturbo | 221 | printf(INVALID_KEY_FORMAT); |
222 | exit(-1); |
||
223 | } |
||
224 | fclose(keyfile); |
||
225 | } |
||
226 | |||
227 | |||
228 | void findopt(int argc, char *argv[],char *in_file, char *out_file) |
||
229 | { |
||
230 | char found=0; |
||
231 | for(int j=3; j |
||
232 | { |
||
8472 | superturbo | 233 | if(!strcmp(argv[j],"-k")){ |
8251 | superturbo | 234 | found=1; |
235 | key_con_read(argv[j+1]); |
||
236 | break; |
||
237 | } |
||
8472 | superturbo | 238 | else if(!strcmp(argv[j],"-K")){ |
8251 | superturbo | 239 | found=1; |
240 | key_file_read(argv[j+1]); |
||
241 | break; |
||
242 | } |
||
243 | } |
||
244 | |||
8472 | superturbo | 245 | if(!found){ |
8251 | superturbo | 246 | printf(NO_KEY_OR_KEYFILE); |
247 | exit(-1); |
||
248 | } |
||
8472 | superturbo | 249 | |
8251 | superturbo | 250 | for(int i=3;i |
251 | if(!strcmp(argv[i],"-e")) |
||
252 | { |
||
253 | if(!strcmp(argv[i+1],"normal")){xcrypt_file(in_file, out_file, ENCRYPT);} |
||
254 | if(!strcmp(argv[i+1],"speed")){xcrypt_file_speed(in_file, out_file, ENCRYPT);} |
||
255 | } |
||
256 | if(!strcmp(argv[i],"-d")) |
||
257 | { |
||
258 | if(!strcmp(argv[i+1],"normal")){xcrypt_file(in_file, out_file, DECRYPT);} |
||
259 | if(!strcmp(argv[i+1],"speed")){xcrypt_file_speed(in_file, out_file, DECRYPT);} |
||
260 | } |
||
261 | } |
||
262 | printf(INVALID_ARG); |
||
263 | exit(0); |
||
264 | } |
||
265 | |||
266 | void key_write_in_file(char *keyfilename) |
||
267 | { |
||
268 | FILE *keyfile; |
||
8472 | superturbo | 269 | if((keyfile = fopen(strcat(keyfilename, ".key"), "wb"))==NULL){ |
8251 | superturbo | 270 | printf(INCORRECT_FILE, keyfilename); |
271 | exit(-1); |
||
272 | } |
||
273 | fwrite(key,sizeof(uint8_t), 16, keyfile); |
||
274 | printf(KEY_RECORD_IN_FILE, keyfilename); |
||
275 | fclose(keyfile); |
||
276 | exit(0); |
||
277 | } |
||
278 | |||
279 | int main(int argc, char **argv) |
||
280 | { |
||
281 | con_init_console_dll(); |
||
282 | con_set_title("TEAtool\0"); |
||
8472 | superturbo | 283 | if(argc==7){ |
8251 | superturbo | 284 | findopt(argc,argv, argv[1],argv[2]); |
285 | } |
||
8472 | superturbo | 286 | else if(argc==2 && !strcmp(argv[1],"-a")){ |
8251 | superturbo | 287 | show_about(); |
288 | exit(0); |
||
289 | } |
||
8472 | superturbo | 290 | else if(argc==2 && !strcmp(argv[1],"-h")){ |
8251 | superturbo | 291 | show_help(); |
292 | exit(0); |
||
293 | } |
||
8472 | superturbo | 294 | else if(argc==4 && !strcmp(argv[1],"-r")){ |
8251 | superturbo | 295 | key_con_read(argv[2]); |
296 | key_write_in_file(argv[3]); |
||
297 | } |
||
8472 | superturbo | 298 | else{ |
8251 | superturbo | 299 | printf(INVALID_ARG); |
300 | exit(0); |
||
301 | } |
||
302 | return 0; |
||
303 | }4;>32;>8) |