Subversion Repositories Kolibri OS

Rev

Rev 8251 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 8251 Rev 8472
Line 1... Line -...
1
/* Author: turbocat2001*/
-
 
2
 
1
/* Copyright (C) 2019-2020 Logaev Maxim (turbocat2001), GPLv3 */
Line 3... Line 2...
3
#include 
2
 
4
#include "tea.c"
-
 
5
#include 
3
#include 
6
#include 
4
#include 
7
#include 
5
#include 
8
#include "lang_en.c"
-
 
9
#include 
6
#include 
Line -... Line 7...
-
 
7
#include 
-
 
8
 
-
 
9
#include "lang_en.c"
10
 
10
#include "tea.c"
11
#define ENCRYPT 1       
11
 
Line 12... Line 12...
12
#define DECRYPT 2
12
#define ENCRYPT 1       
13
 
13
#define DECRYPT 2
Line 26... Line 26...
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
}
Line 40... Line 37...
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
{
Line 43... Line 40...
43
    
40
    FILE *input, *output; 
44
    if((input = fopen(in_file,"rb"))==NULL)
-
 
45
    {
41
    
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);
Line 48... Line 44...
48
    }
44
        exit(1);
Line 53... Line 49...
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
    }
Line 63... Line 58...
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); 
Line 68... Line 62...
68
            
62
        printf(FILE_ENCRYPTION); 
Line 79... Line 73...
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
    }
Line 83... Line 77...
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);        
Line 106... Line 99...
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;
Line 110... Line 103...
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);
Line 115... Line 107...
115
 
107
    }
Line 146... Line 138...
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);
Line 150... Line 142...
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);
Line 158... Line 147...
158
 
147
            }
Line 159... Line 148...
159
            size_f=size_f-8;
148
            
160
            
-
 
161
            if(size_f<0)
149
            size_f=size_f-8;
162
            {
150
            
163
                size_f=0;
151
            if(size_f<0){
Line 164... Line 152...
164
            }    
152
                size_f=0;
Line 193... Line 181...
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;}
Line 208... Line 195...
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
}
Line 227... Line 209...
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
    {
-
 
232
        printf(FILE_NOT_FOUND, key_file);
213
    if((keyfile = fopen(key_file,"rb"))==NULL){
233
        exit(-1);
214
        printf(FILE_NOT_FOUND, key_file);
234
    }
215
        exit(-1);
Line 235... Line 216...
235
 
216
    }
236
    if(size_orig_file(keyfile)==16)    
-
 
237
    {
217
 
238
    fread(key,sizeof(uint32_t),4, keyfile);     
-
 
239
    }
218
    if(size_orig_file(keyfile)==16)    {
240
    else
-
 
241
    {
219
        fread(key,sizeof(uint32_t),4, keyfile);     
242
        printf(INVALID_KEY_FORMAT);
220
    }else{
243
        exit(-1);
221
        printf(INVALID_KEY_FORMAT);
244
    }
-
 
245
 
222
        exit(-1);
246
    fclose(keyfile);
223
    }
Line 247... Line 224...
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
-
 
254
    {
230
    char found=0;
255
        if(!strcmp(argv[j],"-k"))
231
    for(int j=3; j
256
        {
232
    {
257
            found=1;
233
        if(!strcmp(argv[j],"-k")){
258
            key_con_read(argv[j+1]);
234
            found=1;
259
            break;
-
 
260
        }
235
            key_con_read(argv[j+1]);
261
        else if(!strcmp(argv[j],"-K"))
236
            break;
262
        {
237
        }
263
            found=1;
238
        else if(!strcmp(argv[j],"-K")){
264
            key_file_read(argv[j+1]);
239
            found=1;
Line 265... Line 240...
265
            break;
240
            key_file_read(argv[j+1]);
266
        }
-
 
267
    }
241
            break;
268
 
242
        }
269
    if(!found)
243
    }
Line 270... Line -...
270
    {
-
 
271
        printf(NO_KEY_OR_KEYFILE);
244
 
272
        exit(-1);
245
    if(!found){
273
    }
246
        printf(NO_KEY_OR_KEYFILE);
274
 
247
        exit(-1);
275
 
248
    }
Line 291... Line 264...
291
 
264
}
Line 292... Line 265...
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
    {
-
 
297
        printf(INCORRECT_FILE, keyfilename);
269
    if((keyfile = fopen(strcat(keyfilename, ".key"), "wb"))==NULL){
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
 
-
 
305
}
276
    exit(0);
Line 306... Line -...
306
 
-
 
307
 
277
}
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)
-
 
313
   {
282
   con_set_title("TEAtool\0"); 
314
         findopt(argc,argv, argv[1],argv[2]);
283
   if(argc==7){
315
   }
284
         findopt(argc,argv, argv[1],argv[2]);
316
   else if(argc==2 && !strcmp(argv[1],"-a"))
-
 
317
   {
285
   }
318
       show_about();
286
   else if(argc==2 && !strcmp(argv[1],"-a")){
319
       exit(0);
287
       show_about();
320
   }
-
 
321
 
288
       exit(0);
322
   else if(argc==2 && !strcmp(argv[1],"-h"))
-
 
323
   {
289
   }
324
       show_help();
290
   else if(argc==2 && !strcmp(argv[1],"-h")){
325
       exit(0);
291
       show_help();
326
   }
-
 
327
 
292
       exit(0);
328
   else if(argc==4 && !strcmp(argv[1],"-r"))
-
 
329
   {
293
   }
330
       key_con_read(argv[2]);
294
   else if(argc==4 && !strcmp(argv[1],"-r")){
331
       key_write_in_file(argv[3]);
295
       key_con_read(argv[2]);
332
   }
-
 
333
 
-
 
334
 
296
       key_write_in_file(argv[3]);
335
   else
-
 
336
   {
297
   }
337
       printf(INVALID_ARG);
298
   else{
338
       exit(0);
299
       printf(INVALID_ARG);
339
   }
-
 
340
 
300
       exit(0);
341
    return 0;
-
 
342
 
301
   }
343
}
-