forked from mistydemeo/quickbms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perform.c
executable file
·2926 lines (2703 loc) · 153 KB
/
perform.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2009-2017 Luigi Auriemma
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
http://www.gnu.org/licenses/gpl-2.0.txt
*/
// QuickBMS perform* encryption and compression operations
#define string_to_execute_VAR (1 << 0)
#define string_to_execute_INPUT (1 << 1)
#define string_to_execute_INPUT_SIZE (1 << 2)
#define string_to_execute_OUTPUT (1 << 3)
#define string_to_execute_OUTPUT_SIZE (1 << 4)
#define string_to_execute_REDIRECT (1 << 5)
#define string_to_execute_MULTI (1 << 6)
u8 *string_to_execute(u8 *str, u8 *INPUT, u8 *INPUT_SIZE, u8 *OUTPUT, u8 *OUTPUT_SIZE, int *res, int exec_checks, int will_be_reparsed_by_bms) {
#define string_to_execute_realloc(X) \
tmp = X; \
if((exesz + tmp) >= totsz) { \
totsz = exesz + tmp + 1024; \
exe = realloc(exe, totsz + 1); \
if(!exe) STD_ERR(QUICKBMS_ERROR_MEMORY); \
}
int tmp,
idx,
exesz = 0,
totsz = 0;
u8 *VAR,
*exe,
*p,
*l,
*limit;
if(!str) return NULL;
// do NOT modify the content of str
if(res) *res = 0;
totsz = strlen(str) + 1024;
exe = malloc(totsz + 1);
if(!exe) STD_ERR(QUICKBMS_ERROR_MEMORY);
exe[0] = 0;
limit = str + strlen(str);
for(p = str; *p && (p < limit);) {
if((p[0] == '>') || (p[0] == '<')) {
if(res) *res |= string_to_execute_REDIRECT;
}
if((p[0] == ';') || (p[0] == '&') || (p[0] == '|')) {
if(res) *res |= string_to_execute_MULTI;
}
#define string_to_execute_assign(X,Y) \
if(X) { \
if(exec_checks && mystrchrs(X, "\"\'><;&|")) { \
} else { \
string_to_execute_realloc(1 + strlen(X) + 1) \
if(will_be_reparsed_by_bms || need_quote_delimiters(X)) { \
exesz += sprintf(exe + exesz, "\"%s\"", X); \
} else { \
exesz += sprintf(exe + exesz, "%s", X); \
} \
} \
} \
p += Y; \
if(res) *res |= string_to_execute_##X;
if(p[0] == '%') {
p++;
if(p[1] == '%') continue;
l = strchr(p, '%');
if(!l) continue;
idx = get_var_from_name(p, l - p);
if(idx < 0) continue;
VAR = get_var(idx);
if(!VAR) continue;
string_to_execute_assign(VAR, (l - p) + 1)
} else if(!strnicmp(p, "#INPUT#", 7)) {
string_to_execute_assign(INPUT, 7)
} else if(!strnicmp(p, "#INPUT_SIZE#", 12)) {
string_to_execute_assign(INPUT_SIZE, 12)
} else if(!strnicmp(p, "#OUTPUT#", 8)) {
string_to_execute_assign(OUTPUT, 8)
} else if(!strnicmp(p, "#OUTPUT_SIZE#", 13)) {
string_to_execute_assign(OUTPUT_SIZE, 13)
} else {
string_to_execute_realloc(1)
exe[exesz++] = *p;
p++;
}
}
exe = realloc(exe, exesz + 1);
if(!exe) STD_ERR(QUICKBMS_ERROR_MEMORY);
exe[exesz] = 0;
return(exe);
}
u8 *quickbms_execute_pipe_path(u8 *mycmd) {
int len,
tmp,
quote = 0,
new_exelen;
u8 *old_path,
*old_exe,
*new_exe,
*p,
*l;
for(p = mycmd; *p && (*p <= ' '); p++);
l = NULL;
if(*p == '\"') l = strchr(++p, '\"');
else if(*p == '\'') l = strchr(++p, '\'');
if(!l) for(l = p; *l && (*l > ' '); l++);
tmp = *l;
*l = 0;
old_path = mystrdup_simple(p);
len = strlen(old_path);
#ifdef WIN32
if((len < 4) || stricmp(old_path + len - 4, ".exe")) {
old_path = realloc(old_path, len + 4 + 1);
if(!old_path) STD_ERR(QUICKBMS_ERROR_MEMORY);
strcpy(old_path + len, ".exe");
}
#endif
old_exe = get_filename(old_path);
*l = tmp;
if(*l) l++;
if((old_path[0] == '/') || strchr(old_path, ':')) {
// do nothing, it's an absolute path
} else {
new_exe = quickbms_path_open(old_exe);
if(new_exe) {
if(mystrchrs(new_exe, " \t")) quote = 1;
tmp = l - mycmd;
len = strlen(l);
new_exelen = strlen(new_exe);
mycmd = realloc(mycmd, quote + new_exelen + quote + 1 + len + 1);
if(!mycmd) STD_ERR(QUICKBMS_ERROR_MEMORY);
mymemmove(mycmd + quote + new_exelen + quote + 1, mycmd + tmp, len + 1);
p = mycmd;
if(quote) *p++ = '\"';
memcpy(p, new_exe, new_exelen);
p += new_exelen;
if(quote) *p++ = '\"';
*p++ = ' ';
FREE(new_exe)
} else {
// return the original command, it will be found automatically by system()
}
}
FREE(old_path)
return(mycmd);
}
// it's necessary to use real files because
// the executed command may not support sequential data (like pipes)
// so I MUST guarantee the 100% of compatibility even if it's slower
// and occupy more resources
int quickbms_execute_pipe(u8 *cmdstr, u8 *in, int insz, u8 **out, int outsz, u8 *my_fname) {
FILE *fd;
int res,
t,
size = -1;
u8 *in_fname = NULL,
*out_fname = NULL,
*mycmd = NULL;
if(!cmdstr) return -1;
if(!in) {
if(!my_fname) return -1;
}
if(insz < 0) return -1;
if(!enable_execute_pipe) {
fprintf(stderr, "\n"
"- the script has requested to run an executable:\n"
" %s\n"
"\n"
" NOTE THAT I ASK THIS CONFIRMATION ONLY NOW SO CHECK THE SCRIPT BECAUSE YOU\n"
" WILL BE NO LONGER PROMPTED TO CONFIRM THE NEXT USAGE OF THE EXECUTE COMMAND!\n"
" THIS FEATURE IS DANGEROUS SO BE SURE TO KNOW WHAT YOU ARE DOING\n"
"\n"
" do you want to continue (y/N)? ",
cmdstr);
if(get_yesno(NULL) != 'y') myexit(QUICKBMS_ERROR_USER);
// *fixed, added warning*
// do NOT enable enable_execute_pipe because if we have
// multiple comtype then only the first one will be
// visualized and the others may be malicious!
enable_execute_pipe = 1;
}
if(!my_fname) {
quickbms_tmpname(&in_fname, NULL, "tmp");
mydump(in_fname, in, insz);
quickbms_tmpname(&out_fname, NULL, "tmp");
}
// I'm forced to use system() because it's possible that
// we have a command which uses stdout and so I must grant
// it's compatibility... yes I know that it's a big security
// problem!
mycmd = string_to_execute(cmdstr,
my_fname ? my_fname : in_fname,
NULL, // ???
my_fname ? my_fname : out_fname,
NULL, // ???
&res, 1,
0);
mycmd = quickbms_execute_pipe_path(mycmd);
invalid_chars_to_spaces(mycmd);
#ifdef WIN32
t = strlen(mycmd);
mycmd = realloc(mycmd, 1 + t + 1);
if(!mycmd) STD_ERR(QUICKBMS_ERROR_MEMORY);
mymemmove(mycmd + 1, mycmd, t + 1);
mycmd[0] = '@'; // crazy but necessary!
#endif
fprintf(stderr, "- execute:\n %s\n", mycmd);
system(mycmd); // do NOT check the return value
FREE(mycmd);
fd = NULL;
if(res & string_to_execute_OUTPUT) {
if(out_fname) {
fd = xfopen(out_fname, "rb"); // check if it exists
if(!fd) goto quit;
}
} else if(res & string_to_execute_INPUT) {
if(in_fname) {
fd = xfopen(in_fname, "rb"); // check if it exists
if(!fd) goto quit;
}
}
if(fd) {
fseek(fd, 0, SEEK_END);
size = ftell(fd);
fseek(fd, 0, SEEK_SET);
if(out) {
if(outsz < size) {
outsz = size;
if(size == (u_int)-1LL) ALLOC_ERR;
*out = realloc(*out, size + 1);
if(!*out) STD_ERR(QUICKBMS_ERROR_MEMORY);
(*out)[size] = 0;
}
size = fread(*out, 1, size, fd);
} else {
if(insz < size) size = insz;
size = fread(in, 1, size, fd);
}
FCLOSE(fd);
}
quit:
if(!my_fname) {
unlink(in_fname);
FREE(in_fname)
unlink(out_fname);
FREE(out_fname)
}
return size;
}
int parse_bms(FILE *fds, u8 *inputs, int cmd, int eol_mode);
int CMD_CallDLL_func(int cmd, u8 *input, int input_size, u8 *output, int output_size);
int quickbms_calldll_pipe(u8 *cmdstr, u8 *in, int insz, u8 *out, int outsz) {
int cmd,
ret;
u8 *calldll_cmd = NULL,
*p;
if(!cmdstr) return -1;
cmdstr = skip_delimit(cmdstr);
p = mystrchrs(cmdstr, " \t");
if(!p) return -1;
*p = 0;
if(!stricmp(cmdstr, "calldll")) cmdstr = p + 1;
*p = ' '; // restore the NULLed char
p = malloc(32 + strlen(cmdstr) + 1);
if(!p) STD_ERR(QUICKBMS_ERROR_MEMORY);
sprintf(p, "calldll %s", cmdstr);
// #INPUT# -> "#INPUT#" to avoid the parse_bms comments
if(in && out) calldll_cmd = string_to_execute(p, "#INPUT#", "#INPUT_SIZE#", "#OUTPUT#","#OUTPUT_SIZE#", &ret, 0, 1);
else if(!in && out) calldll_cmd = string_to_execute(p, "#OUTPUT#","#OUTPUT_SIZE#", "#OUTPUT#","#OUTPUT_SIZE#", &ret, 0, 1);
else if(in && !out) calldll_cmd = string_to_execute(p, "#INPUT#", "#INPUT_SIZE#", "#INPUT#", "#INPUT_SIZE#", &ret, 0, 1);
else calldll_cmd = mystrdup_simple(p);
FREE(p);
for(cmd = 0; CMD.type != CMD_NONE; cmd++);
cmd++;
//if((cmd + 1) < MAX_CMDS) // not needed because parse_bms already does this check
parse_bms(NULL, calldll_cmd, cmd, 0);
ret = CMD_CallDLL_func(cmd, in, insz, out, outsz);
if(ret & string_to_execute_INPUT) {
if(!(ret & string_to_execute_OUTPUT)) {
if(out) {
if(outsz > insz) outsz = insz;
memcpy(out, in, outsz);
}
}
}
FREE(calldll_cmd);
return(outsz);
}
ICE_KEY *do_ice_key(u8 *key, int keysz, int icecrypt) {
ICE_KEY *ik;
int i = 0,
k,
level = 0;
u8 buf[1024];
if(keysz == 8) {
level = 0;
} else if(!(keysz % 16)) {
level = keysz / 16;
} else {
fprintf(stderr, "\nError: your ICE key has an incorrect size\n");
myexit(QUICKBMS_ERROR_ENCRYPTION);
}
if(icecrypt) {
memset(buf, 0, sizeof(buf));
for(k = 0; k < keysz; k++) {
u8 c = key[k] & 0x7f;
int idx = i / 8;
int bit = i & 7;
if (bit == 0) {
buf[idx] = (c << 1);
} else if (bit == 1) {
buf[idx] |= c;
} else {
buf[idx] |= (c >> (bit - 1));
buf[idx + 1] = (c << (9 - bit));
}
i += 7;
}
key = buf;
}
ik = ice_key_create(level);
if(!ik) return NULL;
ice_key_set(ik, key);
return(ik);
}
#ifndef DISABLE_MCRYPT
MCRYPT quick_mcrypt_check(u8 *type) {
u8 tmp[64],
*p,
*mode,
*algo;
if(!type) type = "";
mystrcpy(tmp, type, sizeof(tmp));
// myisalnum gets also the '-' which is a perfect thing
// NEVER use '-' as delimiter because "rijndael-*" use it
algo = tmp;
if(!strnicmp(tmp, "mcrypt", 6)) {
for(algo = tmp + 6; *algo; algo++) {
if(myisalnum(*algo)) break;
}
}
p = mystrchrs(algo, "_, ");
if(p) {
*p = 0;
mode = p + 1;
} else {
mode = MCRYPT_ECB;
}
return(mcrypt_module_open(algo, NULL, mode, NULL));
}
#endif
#ifndef DISABLE_TOMCRYPT
// nonce:001122334455667788 header:aabbccddeeff0011 ivec:FFff00112233AAbb tweak:0011223344
void tomcrypt_lame_ivec(TOMCRYPT *ctx, u8 *ivec, int ivecsz) {
int t,
*y;
u8 *p,
*s,
*l,
**x,
*limit;
if(!ctx || !ivec || (ivecsz < 0)) return;
limit = ivec + ivecsz; // ivec is NULL delimited
for(p = ivec; p < limit; p = l + 1) {
while(*p && (*p <= ' ')) p++;
l = strchr(p, ' ');
if(!l) l = strchr(p, '\t');
if(!l) l = limit;
// ':' in all of the following
if((s = stristr(p, "nonce:")) || (s = stristr(p, "salt:")) ||
(s = stristr(p, "adata:")) || (s = stristr(p, "skey:")) ||
(s = stristr(p, "key2:")) || (s = stristr(p, /*salt_*/"key:"))) {
x = &ctx->nonce;
y = &ctx->noncelen;
} else if((s = stristr(p, "header:"))) {
x = &ctx->header;
y = &ctx->headerlen;
} else if((s = stristr(p, "ivec:"))) {
x = &ctx->ivec;
y = &ctx->ivecsz;
} else if((s = stristr(p, "tweak:"))) {
x = &ctx->tweak;
y = NULL;
} else {
break;
}
s = strchr(s, ':');
if(!s) break;
s++;
if(l < s) break;
*x = calloc((l - s) + 1, 1); // / 2, but it's ok (+1 is not needed)
if(!*x) STD_ERR(QUICKBMS_ERROR_MEMORY);
if(y) *y = 0;
t = unhex(s, l - s, *x, l - s);
if(t < 0) {
FREE(*x)
}
if((t >= 0) && y) *y = t;
}
if(!ctx->ivec) {
ctx->ivec = malloc(ivecsz);
if(!ctx->ivec) STD_ERR(QUICKBMS_ERROR_MEMORY);
memcpy(ctx->ivec, ivec, ivecsz);
ctx->ivecsz = ivecsz;
}
}
// badly implemented because it's intended only as a test
TOMCRYPT *tomcrypt_doit(TOMCRYPT *ctx, u8 *type, u8 *in, int insz, u8 *out, int outsz, i32 *ret) {
static int init = 0;
symmetric_ECB ecb;
symmetric_CFB cfb;
symmetric_OFB ofb;
symmetric_CBC cbc;
symmetric_CTR ctr;
symmetric_LRW lrw;
symmetric_F8 f8;
symmetric_xts xts;
long tmp;
i32 stat;
int i,
use_tomcrypt = 0;
u8 tag[64] = "",
desc[64],
*p,
*l;
ltc_mp = ltm_desc;
enum {
tomcrypt_enum_ecb,
tomcrypt_enum_cfb,
tomcrypt_enum_ofb,
tomcrypt_enum_cbc,
tomcrypt_enum_ctr,
tomcrypt_enum_lrw,
tomcrypt_enum_f8,
tomcrypt_enum_xts,
tomcrypt_enum_hmac,
tomcrypt_enum_omac,
tomcrypt_enum_pmac,
tomcrypt_enum_eax,
tomcrypt_enum_ocb3,
tomcrypt_enum_ocb,
tomcrypt_enum_ccm,
tomcrypt_enum_gcm,
tomcrypt_enum_pelican,
tomcrypt_enum_xcbc,
tomcrypt_enum_f9,
tomcrypt_enum_invalid
};
typedef struct {
int idx;
u8 *name;
u8 *type;
} quickbms_tomcrypt_t;
static int quickbms_tomcrypts = 0;
static quickbms_tomcrypt_t quickbms_tomcrypt[64];
#define TOMCRYPT_REGISTER(Z, X,Y) \
if(!init) { \
register_##Z(&X##_desc); \
quickbms_tomcrypt[quickbms_tomcrypts].idx = find_##Z(Y ? Y : #X); \
quickbms_tomcrypt[quickbms_tomcrypts].name = #X; \
quickbms_tomcrypt[quickbms_tomcrypts].type = #Z; \
quickbms_tomcrypts++; \
if(Y) { \
quickbms_tomcrypt[quickbms_tomcrypts].idx = quickbms_tomcrypt[quickbms_tomcrypts - 1].idx; \
quickbms_tomcrypt[quickbms_tomcrypts].name = Y; \
quickbms_tomcrypt[quickbms_tomcrypts].type = #Z; \
quickbms_tomcrypts++; \
} \
}
TOMCRYPT_REGISTER(cipher, blowfish, NULL)
TOMCRYPT_REGISTER(cipher, rc5, NULL)
TOMCRYPT_REGISTER(cipher, rc6, NULL)
TOMCRYPT_REGISTER(cipher, rc2, NULL)
TOMCRYPT_REGISTER(cipher, saferp, NULL)
TOMCRYPT_REGISTER(cipher, safer_k64, "safer-k64")
TOMCRYPT_REGISTER(cipher, safer_k128, "safer-k128")
TOMCRYPT_REGISTER(cipher, safer_sk64, "safer-sk64")
TOMCRYPT_REGISTER(cipher, safer_sk128, "safer-sk128")
TOMCRYPT_REGISTER(cipher, rijndael, NULL)
TOMCRYPT_REGISTER(cipher, aes, "rijndael")
TOMCRYPT_REGISTER(cipher, xtea, NULL)
TOMCRYPT_REGISTER(cipher, twofish, NULL)
TOMCRYPT_REGISTER(cipher, des, NULL)
TOMCRYPT_REGISTER(cipher, des3, "3des")
TOMCRYPT_REGISTER(cipher, cast5, NULL)
TOMCRYPT_REGISTER(cipher, noekeon, NULL)
TOMCRYPT_REGISTER(cipher, skipjack, NULL)
TOMCRYPT_REGISTER(cipher, khazad, NULL)
TOMCRYPT_REGISTER(cipher, anubis, NULL)
TOMCRYPT_REGISTER(cipher, kseed, "seed")
TOMCRYPT_REGISTER(cipher, kasumi, NULL)
TOMCRYPT_REGISTER(cipher, multi2, NULL)
TOMCRYPT_REGISTER(cipher, camellia, NULL)
TOMCRYPT_REGISTER(hash, chc, NULL)
TOMCRYPT_REGISTER(hash, whirlpool, NULL)
TOMCRYPT_REGISTER(hash, sha512, NULL)
TOMCRYPT_REGISTER(hash, sha384, NULL)
TOMCRYPT_REGISTER(hash, sha512_256, "sha512-256")
TOMCRYPT_REGISTER(hash, sha512_224, "sha512-224")
TOMCRYPT_REGISTER(hash, sha256, NULL)
TOMCRYPT_REGISTER(hash, sha224, NULL)
TOMCRYPT_REGISTER(hash, sha1, NULL)
TOMCRYPT_REGISTER(hash, md5, NULL)
TOMCRYPT_REGISTER(hash, md4, NULL)
TOMCRYPT_REGISTER(hash, md2, NULL)
TOMCRYPT_REGISTER(hash, tiger, NULL)
TOMCRYPT_REGISTER(hash, rmd128, NULL)
TOMCRYPT_REGISTER(hash, rmd160, NULL)
TOMCRYPT_REGISTER(hash, rmd256, NULL)
TOMCRYPT_REGISTER(hash, rmd320, NULL)
if(!init) init = 1;
if(type) {
mystrcpy(desc, type, sizeof(desc));
ctx = calloc(1, sizeof(TOMCRYPT));
if(!ctx) STD_ERR(QUICKBMS_ERROR_MEMORY);
ctx->idx = -1; // 0 is AES
#define TOMCRYPT_IDX(X,Y) \
else if(!stricmp(p, #X)) { \
ctx->idx = X##_idx; \
Y; \
}
for(p = desc; *p; p = l + 1) {
l = strchr(p, ' ');
if(l) *l = 0;
while(*p && (*p <= ' ')) p++;
if(!strnicmp(p, "lib", 3)) p += 3;
if(!strnicmp(p, "tomcrypt", 8)) {
use_tomcrypt = 1;
p += 8;
} else {
for(i = 0; i < quickbms_tomcrypts; i++) {
if(!stricmp(p, quickbms_tomcrypt[i].name)) {
ctx->idx = quickbms_tomcrypt[i].idx;
if(!stricmp(quickbms_tomcrypt[i].name, "cipher")) {
ctx->cipher = tomcrypt_enum_ecb;
} else if(!stricmp(quickbms_tomcrypt[i].name, "hash")) {
ctx->hash = TRUE;
}
}
}
#define TOMCRYPT_REGISTER_ENUM(X) \
else if(stristr(p, #X)) ctx->cipher = tomcrypt_enum_##X;
if(!stricmp(p, "")) {} // needed because the others are "else"
TOMCRYPT_REGISTER_ENUM(ecb)
TOMCRYPT_REGISTER_ENUM(cfb)
TOMCRYPT_REGISTER_ENUM(ofb)
TOMCRYPT_REGISTER_ENUM(cbc)
TOMCRYPT_REGISTER_ENUM(ctr)
TOMCRYPT_REGISTER_ENUM(lrw)
TOMCRYPT_REGISTER_ENUM(f8)
TOMCRYPT_REGISTER_ENUM(xts)
TOMCRYPT_REGISTER_ENUM(hmac)
TOMCRYPT_REGISTER_ENUM(omac)
TOMCRYPT_REGISTER_ENUM(pmac)
TOMCRYPT_REGISTER_ENUM(eax)
TOMCRYPT_REGISTER_ENUM(ocb3)
TOMCRYPT_REGISTER_ENUM(ocb)
TOMCRYPT_REGISTER_ENUM(ccm)
TOMCRYPT_REGISTER_ENUM(gcm)
TOMCRYPT_REGISTER_ENUM(pelican)
TOMCRYPT_REGISTER_ENUM(xcbc)
TOMCRYPT_REGISTER_ENUM(f9)
}
if(!l) break;
*l = ' ';
}
if(!use_tomcrypt || (ctx->idx < 0)) {
FREE(ctx)
}
return(ctx);
}
//if(outsz > insz) outsz = insz;
tmp = outsz;
int blocklen = 0;
int set_tomcrypt_blocklen(int size, int blocklen) {
if(blocklen > 0) {
size = (size / blocklen) * blocklen;
}
return size;
}
#define TOMCRYPT_CRYPT_MODE3(X) \
X##_done(&X);
#define TOMCRYPT_CRYPT_MODE2(X) \
if(g_encrypt_mode) { if(X##_encrypt(in, out, insz, &X)) if(X##_encrypt(in, out, set_tomcrypt_blocklen(insz, blocklen), &X)) goto quit; } \
else { if(X##_decrypt(in, out, insz, &X)) if(X##_decrypt(in, out, set_tomcrypt_blocklen(insz, blocklen), &X)) goto quit; } \
TOMCRYPT_CRYPT_MODE3(X)
#define TOMCRYPT_CRYPT_MODE(X) \
if(X##_setiv(ctx->ivec, ctx->ivecsz, &X)) goto quit; \
TOMCRYPT_CRYPT_MODE2(X)
if(ret) *ret = 0;
if(ctx->idx < 0) ctx->idx = 0;
if(ctx->hash) {
if(hash_memory(ctx->idx, in, insz, out, &tmp)) goto quit;
} else if(ctx->cipher == tomcrypt_enum_ecb) {
if(ecb_start(ctx->idx, ctx->key, ctx->keysz, 0, &ecb)) goto quit;
blocklen = ecb.blocklen;
TOMCRYPT_CRYPT_MODE2(ecb)
} else if(ctx->cipher == tomcrypt_enum_cfb) {
if(cfb_start(ctx->idx, ctx->ivec, ctx->key, ctx->keysz, 0, &cfb)) goto quit;
blocklen = cfb.blocklen;
TOMCRYPT_CRYPT_MODE(cfb)
} else if(ctx->cipher == tomcrypt_enum_ofb) {
if(ofb_start(ctx->idx, ctx->ivec, ctx->key, ctx->keysz, 0, &ofb)) goto quit;
blocklen = ofb.blocklen;
TOMCRYPT_CRYPT_MODE(ofb)
} else if(ctx->cipher == tomcrypt_enum_cbc) {
if(cbc_start(ctx->idx, ctx->ivec, ctx->key, ctx->keysz, 0, &cbc)) goto quit;
blocklen = cbc.blocklen;
TOMCRYPT_CRYPT_MODE(cbc)
} else if(ctx->cipher == tomcrypt_enum_ctr) {
if(ctr_start(ctx->idx, ctx->ivec, ctx->key, ctx->keysz, 0, CTR_COUNTER_LITTLE_ENDIAN, &ctr)) goto quit;
blocklen = ctr.blocklen;
TOMCRYPT_CRYPT_MODE(ctr)
} else if(ctx->cipher == tomcrypt_enum_lrw) {
if(lrw_start(ctx->idx, ctx->ivec, ctx->key, ctx->keysz, ctx->tweak, 0, &lrw)) goto quit;
//blocklen = lrw.blocklen;
TOMCRYPT_CRYPT_MODE(lrw)
} else if(ctx->cipher == tomcrypt_enum_f8) {
if(f8_start(ctx->idx, ctx->ivec, ctx->key, ctx->keysz, ctx->nonce, ctx->noncelen, 0, &f8)) goto quit;
blocklen = f8.blocklen;
TOMCRYPT_CRYPT_MODE(f8)
} else if(ctx->cipher == tomcrypt_enum_xts) {
if(xts_start(ctx->idx, ctx->key, ctx->nonce, ctx->keysz, 0, &xts)) goto quit;
//blocklen = xts.blocklen;
if(g_encrypt_mode) { if(xts_encrypt(in, insz, out, ctx->tweak, &xts)) goto quit; } \
else { if(xts_decrypt(in, insz, out, ctx->tweak, &xts)) goto quit; } \
TOMCRYPT_CRYPT_MODE3(xts)
} else if(ctx->cipher == tomcrypt_enum_hmac) {
if(hmac_memory(ctx->idx, ctx->key, ctx->keysz, in, insz, out, &tmp)) goto quit;
} else if(ctx->cipher == tomcrypt_enum_omac) {
if(omac_memory(ctx->idx, ctx->key, ctx->keysz, in, insz, out, &tmp)) goto quit;
} else if(ctx->cipher == tomcrypt_enum_pmac) {
if(pmac_memory(ctx->idx, ctx->key, ctx->keysz, in, insz, out, &tmp)) goto quit;
} else if(ctx->cipher == tomcrypt_enum_eax) {
tmp = sizeof(tag);
if(g_encrypt_mode) {
if(eax_encrypt_authenticate_memory(
ctx->idx,
ctx->key, ctx->keysz,
ctx->nonce, ctx->noncelen,
ctx->header, ctx->headerlen,
in, insz,
out,
tag, &tmp)) goto quit;
} else {
if(eax_decrypt_verify_memory(
ctx->idx,
ctx->key, ctx->keysz,
ctx->nonce, ctx->noncelen,
ctx->header, ctx->headerlen,
in, insz,
out,
tag, tmp,
&stat)) goto quit;
}
tmp = insz;
} else if(ctx->cipher == tomcrypt_enum_ocb3) {
tmp = sizeof(tag);
if(g_encrypt_mode) {
if(ocb3_encrypt_authenticate_memory(
ctx->idx,
ctx->key, ctx->keysz,
ctx->nonce, ctx->noncelen,
ctx->header, ctx->headerlen,
in, insz,
out,
tag, &tmp)) goto quit;
} else {
if(ocb3_decrypt_verify_memory(
ctx->idx,
ctx->key, ctx->keysz,
ctx->nonce, ctx->noncelen,
ctx->header, ctx->headerlen,
in, insz,
out,
tag, tmp,
&stat)) goto quit;
}
tmp = insz;
} else if(ctx->cipher == tomcrypt_enum_ocb) {
tmp = sizeof(tag);
if(g_encrypt_mode) {
if(ocb_encrypt_authenticate_memory(
ctx->idx,
ctx->key, ctx->keysz,
ctx->nonce,
in, insz,
out,
tag, &tmp)) goto quit;
} else {
if(ocb_decrypt_verify_memory(
ctx->idx,
ctx->key, ctx->keysz,
ctx->nonce,
in, insz,
out,
tag, tmp,
&stat)) goto quit;
}
tmp = insz;
} else if(ctx->cipher == tomcrypt_enum_ccm) {
tmp = sizeof(tag);
if(ccm_memory(
ctx->idx,
ctx->key, ctx->keysz,
NULL, //uskey,
ctx->nonce, ctx->noncelen,
ctx->header, ctx->headerlen,
in, insz,
out,
tag, &tmp,
g_encrypt_mode ? CCM_ENCRYPT: CCM_DECRYPT)) goto quit;
tmp = insz;
} else if(ctx->cipher == tomcrypt_enum_gcm) {
tmp = sizeof(tag);
if(gcm_memory(
ctx->idx,
ctx->key, ctx->keysz,
ctx->ivec, ctx->ivecsz,
ctx->nonce, ctx->noncelen, //adata, adatalen,
in, insz,
out,
tag, &tmp,
g_encrypt_mode ? GCM_ENCRYPT: GCM_DECRYPT)) goto quit;
tmp = insz;
} else if(ctx->cipher == tomcrypt_enum_pelican) {
if(pelican_memory(ctx->key, ctx->keysz, in, insz, out)) goto quit;
} else if(ctx->cipher == tomcrypt_enum_xcbc) {
if(xcbc_memory(ctx->idx, ctx->key, ctx->keysz, in, insz, out, &tmp)) goto quit;
} else if(ctx->cipher == tomcrypt_enum_f9) {
if(f9_memory(ctx->idx, ctx->key, ctx->keysz, in, insz, out, &tmp)) goto quit;
}
if(ret) *ret = tmp;
return(ctx);
quit:
return NULL;
}
#endif
#ifndef DISABLE_SSL
// original Java code by Spirolone
// http://www.slightlymagic.net/forum/viewtopic.php?f=117&t=14839&start=135#p180107
int modpow_zed(unsigned char *content, int content_length) {
inline void System_arraycopy(unsigned char *src, int srcPos, unsigned char *dest, int destPos, int length) {
memcpy(dest + destPos, src + srcPos, length);
}
int i;
unsigned char result[256] = "";
memset(result, 0, sizeof(result));
if (content_length == 256)
System_arraycopy(content, 0, result, 0, content_length); //result = content;
else if (content_length > 256)
System_arraycopy(content, 1, result, 0, 256);
else if (content_length < 256)
System_arraycopy(content, 0, result, 256-content_length, content_length);
unsigned char m[24] = "";
for (i=0; i<20; i++)
m[i] = result[235+i];
m[20] = 0; m[21] = 0; m[22] = 0;
unsigned char mask[240] = "";
//MessageDigest md = MessageDigest.getInstance("SHA-1");
for (i=0; i<12; i++){
m[23] = (byte)i;
SHA1(m, 24, mask + (i*20));
}
for (i=0; i<235; i++)
result[i] ^= mask[i];
int zeroes = 0;
// Remove non-data bytes
if (result[zeroes]==-0x80)
zeroes++;
while (result[zeroes]==0)
zeroes++;
int decoded_length = 214-zeroes;
System_arraycopy(result, zeroes+1, content, 0, decoded_length);
return decoded_length;
}
#endif
void DO_QUICKBMS_HASH(u8 *INPUT, int INPUT_SIZE) {
int OUTPUT_SIZE = INPUT_SIZE * 2;
u8 OUTPUT[OUTPUT_SIZE + 1];
add_var(0, "QUICKBMS_HASH", INPUT, 0, INPUT_SIZE);
byte2hex(INPUT, INPUT_SIZE, OUTPUT, sizeof(OUTPUT), 1);
add_var(0, "QUICKBMS_HEXHASH", OUTPUT, 0, -1);
mytolower(OUTPUT);
add_var(0, "QUICKBMS_HEXHASHL", OUTPUT, 0, -1);
}
int do_quickbms_hmac(u8 *data, int datalen, u8 *digest) {
#ifndef DISABLE_SSL
EVP_MD_CTX *tmpctx;
i32 tmp = 0;
tmpctx = calloc(1, sizeof(EVP_MD_CTX));
if(!tmpctx) STD_ERR(QUICKBMS_ERROR_MEMORY);
if(hmac_ctx) {
HMAC_Update(hmac_ctx, data, datalen);
EVP_MD_CTX_copy_ex(tmpctx, evpmd_ctx);
HMAC_Final(hmac_ctx, digest, &tmp);
} else {
EVP_DigestUpdate(evpmd_ctx, data, datalen);
EVP_MD_CTX_copy_ex(tmpctx, evpmd_ctx);
EVP_DigestFinal(evpmd_ctx, digest, &tmp);
}
FREE(evpmd_ctx);
evpmd_ctx = tmpctx;
DO_QUICKBMS_HASH(digest, tmp);
#endif
return 0;
}
// if datalen is negative then it will return 0 if encryption is enabled or -1 if disabled
int perform_encryption(u8 *data, int datalen) {
#define ENCRYPT_BLOCKS(X,Y) { \
tmp = datalen / X; \
while(tmp--) { \
Y; \
data += X; \
} \
}
#ifndef EVP_MAX_MD_SIZE
#define EVP_MAX_MD_SIZE 64
#endif
u8 digest[EVP_MAX_MD_SIZE];
u_int crc = 0;
int i = 0;
i32 tmp = 0,
stat = 0;
// if(datalen <= 0) NEVER ENABLE THIS because it's needed
// if(!data) NEVER
QUICK_CRYPT_CASE(wincrypt_ctx)
if(!g_encrypt_mode) datalen = wincrypt_decrypt(wincrypt_ctx, data, datalen);
else datalen = wincrypt_encrypt(wincrypt_ctx, data, datalen);
#ifndef DISABLE_SSL
} else QUICK_CRYPT_CASE(evp_ctx)
tmp = datalen;
if(g_reimport) {
i = evp_ctx->encrypt;
evp_ctx->encrypt = g_encrypt_mode;
}
EVP_CipherUpdate(evp_ctx, data, &tmp, data, datalen);
if(g_reimport) evp_ctx->encrypt = i;
//EVP_CipherFinal(evp_ctx, data + datalen, &tmp); // it causes tons of problems
//datalen += tmp;
} else QUICK_CRYPT_CASE(evpmd_ctx) // probably I seem crazy for all these operations... but it's perfect!
do_quickbms_hmac(data, datalen, digest);
} else QUICK_CRYPT_CASE(blowfish_ctx)
if(!g_encrypt_mode) ENCRYPT_BLOCKS(8, BF_decrypt((void *)data, blowfish_ctx))
else ENCRYPT_BLOCKS(8, BF_encrypt((void *)data, blowfish_ctx))
} else QUICK_CRYPT_CASE(aes_ctr_ctx)
// don't exist 192 and 256
switch(aes_ctr_ctx->type) {
case aes_ctr_ctx_ctr: AES_ctr128_encrypt(data, data, datalen, &aes_ctr_ctx->ctx, aes_ctr_ctx->ivec, aes_ctr_ctx->ecount, &aes_ctr_ctx->num); break;
case aes_ctr_ctx_ige: AES_ige_encrypt(data, data, datalen, &aes_ctr_ctx->ctx, aes_ctr_ctx->ivec, g_encrypt_mode); break;
case aes_ctr_ctx_bi_ige: AES_bi_ige_encrypt(data, data, datalen, &aes_ctr_ctx->ctx, &aes_ctr_ctx->ctx, aes_ctr_ctx->ivec, g_encrypt_mode); break; // placeholder, it's wrong
case aes_ctr_ctx_heat: {
tmp = datalen / AES_BLOCK_SIZE;
if(datalen % AES_BLOCK_SIZE) tmp++;
while(tmp--) {