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