-
Notifications
You must be signed in to change notification settings - Fork 1
/
libzpaq.cpp
6552 lines (6058 loc) · 230 KB
/
libzpaq.cpp
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
/* libzpaq.cpp - LIBZPAQ Version 7.15 implementation - Aug. 17, 2016.
libdivsufsort.c for divsufsort 2.00, included within, is
(C) 2003-2008 Yuta Mori, all rights reserved.
It is released under the MIT license as described in the comments
at the beginning of that section.
Some of the code for AES is from libtomcrypt 1.17 by Tom St. Denis
and is public domain.
The Salsa20/8 code for Scrypt is by D. Bernstein and is public domain.
All of the remaining software is provided as-is, with no warranty.
I, Matt Mahoney, release this software into
the public domain. This applies worldwide.
In some countries this may not be legally possible; if so:
I grant anyone the right to use this software for any purpose,
without any conditions, unless such conditions are required by law.
LIBZPAQ is a C++ library for compression and decompression of data
conforming to the ZPAQ level 2 standard. See http://mattmahoney.net/zpaq/
See libzpaq.h for additional documentation.
*/
#include "libzpaq.h"
#include <string.h>
#include <stdio.h>
#include <windows.h>
namespace libzpaq {
// Read 16 bit little-endian number
int toU16(const char* p) {
return (p[0]&255)+256*(p[1]&255);
}
// Default read() and write()
int Reader::read(char* buf, int n) {
int i=0, c;
while (i<n && (c=get())>=0)
buf[i++]=c;
return i;
}
void Writer::write(const char* buf, int n) {
for (int i=0; i<n; ++i)
put(U8(buf[i]));
}
///////////////////////// allocx //////////////////////
// Allocate newsize > 0 bytes of executable memory and update
// p to point to it and newsize = n. Free any previously
// allocated memory first. If newsize is 0 then free only.
// Call error in case of failure. If NOJIT, ignore newsize
// and set p=0, n=0 without allocating memory.
void allocx(U8* &p, int &n, int newsize) {
#ifdef NOJIT
p=0;
n=0;
#else
if (p || n) {
if (p)
#ifdef unix
munmap(p, n);
#else // Windows
VirtualFree(p, 0, MEM_RELEASE);
#endif
p=0;
n=0;
}
if (newsize>0) {
#ifdef unix
p=(U8*)mmap(0, newsize, PROT_READ|PROT_WRITE|PROT_EXEC,
MAP_PRIVATE|MAP_ANON, -1, 0);
if ((void*)p==MAP_FAILED) p=0;
#else
p=(U8*)VirtualAlloc(0, newsize, MEM_RESERVE|MEM_COMMIT,
PAGE_EXECUTE_READWRITE);
#endif
if (p)
n=newsize;
else {
n=0;
error("allocx failed");
}
}
#endif
}
//////////////////////////// SHA1 ////////////////////////////
// SHA1 code, see http://en.wikipedia.org/wiki/SHA-1
// Start a new hash
void SHA1::init() {
len=0;
h[0]=0x67452301;
h[1]=0xEFCDAB89;
h[2]=0x98BADCFE;
h[3]=0x10325476;
h[4]=0xC3D2E1F0;
memset(w, 0, sizeof(w));
}
// Return old result and start a new hash
const char* SHA1::result() {
// pad and append length
const U64 s=len;
put(0x80);
while ((len&511)!=448)
put(0);
put(s>>56);
put(s>>48);
put(s>>40);
put(s>>32);
put(s>>24);
put(s>>16);
put(s>>8);
put(s);
// copy h to hbuf
for (int i=0; i<5; ++i) {
hbuf[4*i]=h[i]>>24;
hbuf[4*i+1]=h[i]>>16;
hbuf[4*i+2]=h[i]>>8;
hbuf[4*i+3]=h[i];
}
// return hash prior to clearing state
init();
return hbuf;
}
// Hash buf[0..n-1]
void SHA1::write(const char* buf, int64_t n) {
const unsigned char* p=(const unsigned char*) buf;
for (; n>0 && (U32(len)&511)!=0; --n) put(*p++);
for (; n>=64; n-=64) {
for (int i=0; i<16; ++i)
w[i]=p[0]<<24|p[1]<<16|p[2]<<8|p[3], p+=4;
len+=512;
process();
}
for (; n>0; --n) put(*p++);
}
// Hash 1 block of 64 bytes
void SHA1::process() {
U32 a=h[0], b=h[1], c=h[2], d=h[3], e=h[4];
static const U32 k[4]={0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6};
#define f(a,b,c,d,e,i) \
if (i>=16) \
w[(i)&15]^=w[(i-3)&15]^w[(i-8)&15]^w[(i-14)&15], \
w[(i)&15]=w[(i)&15]<<1|w[(i)&15]>>31; \
e+=(a<<5|a>>27)+k[(i)/20]+w[(i)&15] \
+((i)%40>=20 ? b^c^d : i>=40 ? (b&c)|(d&(b|c)) : d^(b&(c^d))); \
b=b<<30|b>>2;
#define r(i) f(a,b,c,d,e,i) f(e,a,b,c,d,i+1) f(d,e,a,b,c,i+2) \
f(c,d,e,a,b,i+3) f(b,c,d,e,a,i+4)
r(0) r(5) r(10) r(15) r(20) r(25) r(30) r(35)
r(40) r(45) r(50) r(55) r(60) r(65) r(70) r(75)
#undef f
#undef r
h[0]+=a; h[1]+=b; h[2]+=c; h[3]+=d; h[4]+=e;
}
//////////////////////////// SHA256 //////////////////////////
void SHA256::init() {
len0=len1=0;
s[0]=0x6a09e667;
s[1]=0xbb67ae85;
s[2]=0x3c6ef372;
s[3]=0xa54ff53a;
s[4]=0x510e527f;
s[5]=0x9b05688c;
s[6]=0x1f83d9ab;
s[7]=0x5be0cd19;
memset(w, 0, sizeof(w));
}
void SHA256::process() {
#define ror(a,b) ((a)>>(b)|(a<<(32-(b))))
#define m(i) \
w[(i)&15]+=w[(i-7)&15] \
+(ror(w[(i-15)&15],7)^ror(w[(i-15)&15],18)^(w[(i-15)&15]>>3)) \
+(ror(w[(i-2)&15],17)^ror(w[(i-2)&15],19)^(w[(i-2)&15]>>10))
#define r(a,b,c,d,e,f,g,h,i) { \
unsigned t1=ror(e,14)^e; \
t1=ror(t1,5)^e; \
h+=ror(t1,6)+((e&f)^(~e&g))+k[i]+w[(i)&15]; } \
d+=h; \
{unsigned t1=ror(a,9)^a; \
t1=ror(t1,11)^a; \
h+=ror(t1,2)+((a&b)^(c&(a^b))); }
#define mr(a,b,c,d,e,f,g,h,i) m(i); r(a,b,c,d,e,f,g,h,i);
#define r8(i) \
r(a,b,c,d,e,f,g,h,i); \
r(h,a,b,c,d,e,f,g,i+1); \
r(g,h,a,b,c,d,e,f,i+2); \
r(f,g,h,a,b,c,d,e,i+3); \
r(e,f,g,h,a,b,c,d,i+4); \
r(d,e,f,g,h,a,b,c,i+5); \
r(c,d,e,f,g,h,a,b,i+6); \
r(b,c,d,e,f,g,h,a,i+7);
#define mr8(i) \
mr(a,b,c,d,e,f,g,h,i); \
mr(h,a,b,c,d,e,f,g,i+1); \
mr(g,h,a,b,c,d,e,f,i+2); \
mr(f,g,h,a,b,c,d,e,i+3); \
mr(e,f,g,h,a,b,c,d,i+4); \
mr(d,e,f,g,h,a,b,c,i+5); \
mr(c,d,e,f,g,h,a,b,i+6); \
mr(b,c,d,e,f,g,h,a,i+7);
static const unsigned k[64]={
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
unsigned a=s[0];
unsigned b=s[1];
unsigned c=s[2];
unsigned d=s[3];
unsigned e=s[4];
unsigned f=s[5];
unsigned g=s[6];
unsigned h=s[7];
r8(0);
r8(8);
mr8(16);
mr8(24);
mr8(32);
mr8(40);
mr8(48);
mr8(56);
s[0]+=a;
s[1]+=b;
s[2]+=c;
s[3]+=d;
s[4]+=e;
s[5]+=f;
s[6]+=g;
s[7]+=h;
#undef mr8
#undef r8
#undef mr
#undef r
#undef m
#undef ror
}
// Return old result and start a new hash
const char* SHA256::result() {
// pad and append length
const unsigned s1=len1, s0=len0;
put(0x80);
while ((len0&511)!=448) put(0);
put(s1>>24);
put(s1>>16);
put(s1>>8);
put(s1);
put(s0>>24);
put(s0>>16);
put(s0>>8);
put(s0);
// copy s to hbuf
for (int i=0; i<8; ++i) {
hbuf[4*i]=s[i]>>24;
hbuf[4*i+1]=s[i]>>16;
hbuf[4*i+2]=s[i]>>8;
hbuf[4*i+3]=s[i];
}
// return hash prior to clearing state
init();
return hbuf;
}
//////////////////////////// AES /////////////////////////////
// Some AES code is derived from libtomcrypt 1.17 (public domain).
#define Te4_0 0x000000FF & Te4
#define Te4_1 0x0000FF00 & Te4
#define Te4_2 0x00FF0000 & Te4
#define Te4_3 0xFF000000 & Te4
// Extract byte n of x
static inline unsigned byte(unsigned x, unsigned n) {return (x>>(8*n))&255;}
// x = y[0..3] MSB first
static inline void LOAD32H(U32& x, const char* y) {
const unsigned char* u=(const unsigned char*)y;
x=u[0]<<24|u[1]<<16|u[2]<<8|u[3];
}
// y[0..3] = x MSB first
static inline void STORE32H(U32& x, unsigned char* y) {
y[0]=x>>24;
y[1]=x>>16;
y[2]=x>>8;
y[3]=x;
}
#define setup_mix(temp) \
((Te4_3[byte(temp, 2)]) ^ (Te4_2[byte(temp, 1)]) ^ \
(Te4_1[byte(temp, 0)]) ^ (Te4_0[byte(temp, 3)]))
// Initialize encryption tables and round key. keylen is 16, 24, or 32.
AES_CTR::AES_CTR(const char* key, int keylen, const char* iv) {
assert(key != NULL);
assert(keylen==16 || keylen==24 || keylen==32);
// Initialize IV (default 0)
iv0=iv1=0;
if (iv) {
LOAD32H(iv0, iv);
LOAD32H(iv1, iv+4);
}
// Initialize encryption tables
for (int i=0; i<256; ++i) {
unsigned s1=
"\x63\x7c\x77\x7b\xf2\x6b\x6f\xc5\x30\x01\x67\x2b\xfe\xd7\xab\x76"
"\xca\x82\xc9\x7d\xfa\x59\x47\xf0\xad\xd4\xa2\xaf\x9c\xa4\x72\xc0"
"\xb7\xfd\x93\x26\x36\x3f\xf7\xcc\x34\xa5\xe5\xf1\x71\xd8\x31\x15"
"\x04\xc7\x23\xc3\x18\x96\x05\x9a\x07\x12\x80\xe2\xeb\x27\xb2\x75"
"\x09\x83\x2c\x1a\x1b\x6e\x5a\xa0\x52\x3b\xd6\xb3\x29\xe3\x2f\x84"
"\x53\xd1\x00\xed\x20\xfc\xb1\x5b\x6a\xcb\xbe\x39\x4a\x4c\x58\xcf"
"\xd0\xef\xaa\xfb\x43\x4d\x33\x85\x45\xf9\x02\x7f\x50\x3c\x9f\xa8"
"\x51\xa3\x40\x8f\x92\x9d\x38\xf5\xbc\xb6\xda\x21\x10\xff\xf3\xd2"
"\xcd\x0c\x13\xec\x5f\x97\x44\x17\xc4\xa7\x7e\x3d\x64\x5d\x19\x73"
"\x60\x81\x4f\xdc\x22\x2a\x90\x88\x46\xee\xb8\x14\xde\x5e\x0b\xdb"
"\xe0\x32\x3a\x0a\x49\x06\x24\x5c\xc2\xd3\xac\x62\x91\x95\xe4\x79"
"\xe7\xc8\x37\x6d\x8d\xd5\x4e\xa9\x6c\x56\xf4\xea\x65\x7a\xae\x08"
"\xba\x78\x25\x2e\x1c\xa6\xb4\xc6\xe8\xdd\x74\x1f\x4b\xbd\x8b\x8a"
"\x70\x3e\xb5\x66\x48\x03\xf6\x0e\x61\x35\x57\xb9\x86\xc1\x1d\x9e"
"\xe1\xf8\x98\x11\x69\xd9\x8e\x94\x9b\x1e\x87\xe9\xce\x55\x28\xdf"
"\x8c\xa1\x89\x0d\xbf\xe6\x42\x68\x41\x99\x2d\x0f\xb0\x54\xbb\x16"
[i]&255;
unsigned s2=s1<<1;
if (s2>=0x100) s2^=0x11b;
unsigned s3=s1^s2;
Te0[i]=s2<<24|s1<<16|s1<<8|s3;
Te1[i]=s3<<24|s2<<16|s1<<8|s1;
Te2[i]=s1<<24|s3<<16|s2<<8|s1;
Te3[i]=s1<<24|s1<<16|s3<<8|s2;
Te4[i]=s1<<24|s1<<16|s1<<8|s1;
}
// setup the forward key
Nr = 10 + ((keylen/8)-2)*2; // 10, 12, or 14 rounds
int i = 0;
U32* rk = &ek[0];
U32 temp;
static const U32 rcon[10] = {
0x01000000UL, 0x02000000UL, 0x04000000UL, 0x08000000UL,
0x10000000UL, 0x20000000UL, 0x40000000UL, 0x80000000UL,
0x1B000000UL, 0x36000000UL}; // round constants
LOAD32H(rk[0], key );
LOAD32H(rk[1], key + 4);
LOAD32H(rk[2], key + 8);
LOAD32H(rk[3], key + 12);
if (keylen == 16) {
for (;;) {
temp = rk[3];
rk[4] = rk[0] ^ setup_mix(temp) ^ rcon[i];
rk[5] = rk[1] ^ rk[4];
rk[6] = rk[2] ^ rk[5];
rk[7] = rk[3] ^ rk[6];
if (++i == 10) {
break;
}
rk += 4;
}
}
else if (keylen == 24) {
LOAD32H(rk[4], key + 16);
LOAD32H(rk[5], key + 20);
for (;;) {
temp = rk[5];
rk[ 6] = rk[ 0] ^ setup_mix(temp) ^ rcon[i];
rk[ 7] = rk[ 1] ^ rk[ 6];
rk[ 8] = rk[ 2] ^ rk[ 7];
rk[ 9] = rk[ 3] ^ rk[ 8];
if (++i == 8) {
break;
}
rk[10] = rk[ 4] ^ rk[ 9];
rk[11] = rk[ 5] ^ rk[10];
rk += 6;
}
}
else if (keylen == 32) {
LOAD32H(rk[4], key + 16);
LOAD32H(rk[5], key + 20);
LOAD32H(rk[6], key + 24);
LOAD32H(rk[7], key + 28);
for (;;) {
temp = rk[7];
rk[ 8] = rk[ 0] ^ setup_mix(temp) ^ rcon[i];
rk[ 9] = rk[ 1] ^ rk[ 8];
rk[10] = rk[ 2] ^ rk[ 9];
rk[11] = rk[ 3] ^ rk[10];
if (++i == 7) {
break;
}
temp = rk[11];
rk[12] = rk[ 4] ^ setup_mix(temp<<24|temp>>8);
rk[13] = rk[ 5] ^ rk[12];
rk[14] = rk[ 6] ^ rk[13];
rk[15] = rk[ 7] ^ rk[14];
rk += 8;
}
}
}
// Encrypt to ct[16]
void AES_CTR::encrypt(U32 s0, U32 s1, U32 s2, U32 s3, unsigned char* ct) {
int r = Nr >> 1;
U32 *rk = &ek[0];
U32 t0=0, t1=0, t2=0, t3=0;
s0 ^= rk[0];
s1 ^= rk[1];
s2 ^= rk[2];
s3 ^= rk[3];
for (;;) {
t0 =
Te0[byte(s0, 3)] ^
Te1[byte(s1, 2)] ^
Te2[byte(s2, 1)] ^
Te3[byte(s3, 0)] ^
rk[4];
t1 =
Te0[byte(s1, 3)] ^
Te1[byte(s2, 2)] ^
Te2[byte(s3, 1)] ^
Te3[byte(s0, 0)] ^
rk[5];
t2 =
Te0[byte(s2, 3)] ^
Te1[byte(s3, 2)] ^
Te2[byte(s0, 1)] ^
Te3[byte(s1, 0)] ^
rk[6];
t3 =
Te0[byte(s3, 3)] ^
Te1[byte(s0, 2)] ^
Te2[byte(s1, 1)] ^
Te3[byte(s2, 0)] ^
rk[7];
rk += 8;
if (--r == 0) {
break;
}
s0 =
Te0[byte(t0, 3)] ^
Te1[byte(t1, 2)] ^
Te2[byte(t2, 1)] ^
Te3[byte(t3, 0)] ^
rk[0];
s1 =
Te0[byte(t1, 3)] ^
Te1[byte(t2, 2)] ^
Te2[byte(t3, 1)] ^
Te3[byte(t0, 0)] ^
rk[1];
s2 =
Te0[byte(t2, 3)] ^
Te1[byte(t3, 2)] ^
Te2[byte(t0, 1)] ^
Te3[byte(t1, 0)] ^
rk[2];
s3 =
Te0[byte(t3, 3)] ^
Te1[byte(t0, 2)] ^
Te2[byte(t1, 1)] ^
Te3[byte(t2, 0)] ^
rk[3];
}
// apply last round and map cipher state to byte array block:
s0 =
(Te4_3[byte(t0, 3)]) ^
(Te4_2[byte(t1, 2)]) ^
(Te4_1[byte(t2, 1)]) ^
(Te4_0[byte(t3, 0)]) ^
rk[0];
STORE32H(s0, ct);
s1 =
(Te4_3[byte(t1, 3)]) ^
(Te4_2[byte(t2, 2)]) ^
(Te4_1[byte(t3, 1)]) ^
(Te4_0[byte(t0, 0)]) ^
rk[1];
STORE32H(s1, ct+4);
s2 =
(Te4_3[byte(t2, 3)]) ^
(Te4_2[byte(t3, 2)]) ^
(Te4_1[byte(t0, 1)]) ^
(Te4_0[byte(t1, 0)]) ^
rk[2];
STORE32H(s2, ct+8);
s3 =
(Te4_3[byte(t3, 3)]) ^
(Te4_2[byte(t0, 2)]) ^
(Te4_1[byte(t1, 1)]) ^
(Te4_0[byte(t2, 0)]) ^
rk[3];
STORE32H(s3, ct+12);
}
// Encrypt or decrypt slice buf[0..n-1] at offset by XOR with AES(i) where
// i is the 128 bit big-endian distance from the start in 16 byte blocks.
void AES_CTR::encrypt(char* buf, int n, U64 offset) {
for (U64 i=offset/16; i<=(offset+n)/16; ++i) {
unsigned char ct[16];
encrypt(iv0, iv1, i>>32, i, ct);
for (int j=0; j<16; ++j) {
const int k=i*16-offset+j;
if (k>=0 && k<n)
buf[k]^=ct[j];
}
}
}
#undef setup_mix
#undef Te4_3
#undef Te4_2
#undef Te4_1
#undef Te4_0
//////////////////////////// stretchKey //////////////////////
// PBKDF2(pw[0..pwlen], salt[0..saltlen], c) to buf[0..dkLen-1]
// using HMAC-SHA256, for the special case of c = 1 iterations
// output size dkLen a multiple of 32, and pwLen <= 64.
static void pbkdf2(const char* pw, int pwLen, const char* salt, int saltLen,
int c, char* buf, int dkLen) {
assert(c==1);
assert(dkLen%32==0);
assert(pwLen<=64);
if (c>0)
c=1;
libzpaq::SHA256 sha256;
char b[32];
for (int i=1; i*32<=dkLen; ++i) {
for (int j=0; j<pwLen; ++j) sha256.put(pw[j]^0x36);
for (int j=pwLen; j<64; ++j) sha256.put(0x36);
for (int j=0; j<saltLen; ++j) sha256.put(salt[j]);
for (int j=24; j>=0; j-=8) sha256.put(i>>j);
memcpy(b, sha256.result(), 32);
for (int j=0; j<pwLen; ++j) sha256.put(pw[j]^0x5c);
for (int j=pwLen; j<64; ++j) sha256.put(0x5c);
for (int j=0; j<32; ++j) sha256.put(b[j]);
memcpy(buf+i*32-32, sha256.result(), 32);
}
}
// Hash b[0..15] using 8 rounds of salsa20
// Modified from http://cr.yp.to/salsa20.html (public domain) to 8 rounds
static void salsa8(U32* b) {
unsigned x[16]={0};
memcpy(x, b, 64);
for (int i=0; i<4; ++i) {
#define R(a,b) (((a)<<(b))+((a)>>(32-b)))
x[ 4] ^= R(x[ 0]+x[12], 7); x[ 8] ^= R(x[ 4]+x[ 0], 9);
x[12] ^= R(x[ 8]+x[ 4],13); x[ 0] ^= R(x[12]+x[ 8],18);
x[ 9] ^= R(x[ 5]+x[ 1], 7); x[13] ^= R(x[ 9]+x[ 5], 9);
x[ 1] ^= R(x[13]+x[ 9],13); x[ 5] ^= R(x[ 1]+x[13],18);
x[14] ^= R(x[10]+x[ 6], 7); x[ 2] ^= R(x[14]+x[10], 9);
x[ 6] ^= R(x[ 2]+x[14],13); x[10] ^= R(x[ 6]+x[ 2],18);
x[ 3] ^= R(x[15]+x[11], 7); x[ 7] ^= R(x[ 3]+x[15], 9);
x[11] ^= R(x[ 7]+x[ 3],13); x[15] ^= R(x[11]+x[ 7],18);
x[ 1] ^= R(x[ 0]+x[ 3], 7); x[ 2] ^= R(x[ 1]+x[ 0], 9);
x[ 3] ^= R(x[ 2]+x[ 1],13); x[ 0] ^= R(x[ 3]+x[ 2],18);
x[ 6] ^= R(x[ 5]+x[ 4], 7); x[ 7] ^= R(x[ 6]+x[ 5], 9);
x[ 4] ^= R(x[ 7]+x[ 6],13); x[ 5] ^= R(x[ 4]+x[ 7],18);
x[11] ^= R(x[10]+x[ 9], 7); x[ 8] ^= R(x[11]+x[10], 9);
x[ 9] ^= R(x[ 8]+x[11],13); x[10] ^= R(x[ 9]+x[ 8],18);
x[12] ^= R(x[15]+x[14], 7); x[13] ^= R(x[12]+x[15], 9);
x[14] ^= R(x[13]+x[12],13); x[15] ^= R(x[14]+x[13],18);
#undef R
}
for (int i=0; i<16; ++i) b[i]+=x[i];
}
// BlockMix_{Salsa20/8, r} on b[0..128*r-1]
static void blockmix(U32* b, int r) {
assert(r<=8);
U32 x[16];
U32 y[256];
memcpy(x, b+32*r-16, 64);
for (int i=0; i<2*r; ++i) {
for (int j=0; j<16; ++j) x[j]^=b[i*16+j];
salsa8(x);
memcpy(&y[i*16], x, 64);
}
for (int i=0; i<r; ++i) memcpy(b+i*16, &y[i*32], 64);
for (int i=0; i<r; ++i) memcpy(b+(i+r)*16, &y[i*32+16], 64);
}
// Mix b[0..128*r-1]. Uses 128*r*n bytes of memory and O(r*n) time
static void smix(char* b, int r, int n) {
libzpaq::Array<U32> x(32*r), v(32*r*n);
for (int i=0; i<r*128; ++i) x[i/4]+=(b[i]&255)<<i%4*8;
for (int i=0; i<n; ++i) {
memcpy(&v[i*r*32], &x[0], r*128);
blockmix(&x[0], r);
}
for (int i=0; i<n; ++i) {
U32 j=x[(2*r-1)*16]&(n-1);
for (int k=0; k<r*32; ++k) x[k]^=v[j*r*32+k];
blockmix(&x[0], r);
}
for (int i=0; i<r*128; ++i) b[i]=x[i/4]>>(i%4*8);
}
// Strengthen password pw[0..pwlen-1] and salt[0..saltlen-1]
// to produce key buf[0..buflen-1]. Uses O(n*r*p) time and 128*r*n bytes
// of memory. n must be a power of 2 and r <= 8.
void scrypt(const char* pw, int pwlen,
const char* salt, int saltlen,
int n, int r, int p, char* buf, int buflen) {
assert(r<=8);
assert(n>0 && (n&(n-1))==0); // power of 2?
libzpaq::Array<char> b(p*r*128);
pbkdf2(pw, pwlen, salt, saltlen, 1, &b[0], p*r*128);
for (int i=0; i<p; ++i) smix(&b[i*r*128], r, n);
pbkdf2(pw, pwlen, &b[0], p*r*128, 1, buf, buflen);
}
// Stretch key in[0..31], assumed to be SHA256(password), with
// NUL terminate salt to produce new key out[0..31]
void stretchKey(char* out, const char* in, const char* salt) {
scrypt(in, 32, salt, 32, 1<<14, 8, 1, out, 32);
}
//////////////////////////// random //////////////////////////
// Put n cryptographic random bytes in buf[0..n-1].
// The first byte will not be 'z' or '7' (start of a ZPAQ archive).
// For a pure random number, discard the first byte.
// In VC++, must link to advapi32.lib.
void random(char* buf, int n) {
#ifdef unix
FILE* in=fopen("/dev/urandom", "rb");
if (in && int(fread(buf, 1, n, in))==n)
fclose(in);
else {
error("key generation failed");
}
#else
HCRYPTPROV h;
if (CryptAcquireContext(&h, NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT) && CryptGenRandom(h, n, (BYTE*)buf))
CryptReleaseContext(h, 0);
else {
fprintf(stderr, "CryptGenRandom: error %d\n", int(GetLastError()));
error("key generation failed");
}
#endif
if (n>=1 && (buf[0]=='z' || buf[0]=='7'))
buf[0]^=0x80;
}
//////////////////////////// Component ///////////////////////
// A Component is a context model, indirect context model, match model,
// fixed weight mixer, adaptive 2 input mixer without or with current
// partial byte as context, adaptive m input mixer (without or with),
// or SSE (without or with).
const int compsize[256]={0,2,3,2,3,4,6,6,3,5};
void Component::init() {
limit=cxt=a=b=c=0;
cm.resize(0);
ht.resize(0);
a16.resize(0);
}
////////////////////////// StateTable ////////////////////////
// sns[i*4] -> next state if 0, next state if 1, n0, n1
static const U8 sns[1024]={
1, 2, 0, 0, 3, 5, 1, 0,
4, 6, 0, 1, 7, 9, 2, 0,
8, 11, 1, 1, 8, 11, 1, 1,
10, 12, 0, 2, 13, 15, 3, 0,
14, 17, 2, 1, 14, 17, 2, 1,
16, 19, 1, 2, 16, 19, 1, 2,
18, 20, 0, 3, 21, 23, 4, 0,
22, 25, 3, 1, 22, 25, 3, 1,
24, 27, 2, 2, 24, 27, 2, 2,
26, 29, 1, 3, 26, 29, 1, 3,
28, 30, 0, 4, 31, 33, 5, 0,
32, 35, 4, 1, 32, 35, 4, 1,
34, 37, 3, 2, 34, 37, 3, 2,
36, 39, 2, 3, 36, 39, 2, 3,
38, 41, 1, 4, 38, 41, 1, 4,
40, 42, 0, 5, 43, 33, 6, 0,
44, 47, 5, 1, 44, 47, 5, 1,
46, 49, 4, 2, 46, 49, 4, 2,
48, 51, 3, 3, 48, 51, 3, 3,
50, 53, 2, 4, 50, 53, 2, 4,
52, 55, 1, 5, 52, 55, 1, 5,
40, 56, 0, 6, 57, 45, 7, 0,
58, 47, 6, 1, 58, 47, 6, 1,
60, 63, 5, 2, 60, 63, 5, 2,
62, 65, 4, 3, 62, 65, 4, 3,
64, 67, 3, 4, 64, 67, 3, 4,
66, 69, 2, 5, 66, 69, 2, 5,
52, 71, 1, 6, 52, 71, 1, 6,
54, 72, 0, 7, 73, 59, 8, 0,
74, 61, 7, 1, 74, 61, 7, 1,
76, 63, 6, 2, 76, 63, 6, 2,
78, 81, 5, 3, 78, 81, 5, 3,
80, 83, 4, 4, 80, 83, 4, 4,
82, 85, 3, 5, 82, 85, 3, 5,
66, 87, 2, 6, 66, 87, 2, 6,
68, 89, 1, 7, 68, 89, 1, 7,
70, 90, 0, 8, 91, 59, 9, 0,
92, 77, 8, 1, 92, 77, 8, 1,
94, 79, 7, 2, 94, 79, 7, 2,
96, 81, 6, 3, 96, 81, 6, 3,
98, 101, 5, 4, 98, 101, 5, 4,
100, 103, 4, 5, 100, 103, 4, 5,
82, 105, 3, 6, 82, 105, 3, 6,
84, 107, 2, 7, 84, 107, 2, 7,
86, 109, 1, 8, 86, 109, 1, 8,
70, 110, 0, 9, 111, 59, 10, 0,
112, 77, 9, 1, 112, 77, 9, 1,
114, 97, 8, 2, 114, 97, 8, 2,
116, 99, 7, 3, 116, 99, 7, 3,
62, 101, 6, 4, 62, 101, 6, 4,
80, 83, 5, 5, 80, 83, 5, 5,
100, 67, 4, 6, 100, 67, 4, 6,
102, 119, 3, 7, 102, 119, 3, 7,
104, 121, 2, 8, 104, 121, 2, 8,
86, 123, 1, 9, 86, 123, 1, 9,
70, 124, 0, 10, 125, 59, 11, 0,
126, 77, 10, 1, 126, 77, 10, 1,
128, 97, 9, 2, 128, 97, 9, 2,
60, 63, 8, 3, 60, 63, 8, 3,
66, 69, 3, 8, 66, 69, 3, 8,
104, 131, 2, 9, 104, 131, 2, 9,
86, 133, 1, 10, 86, 133, 1, 10,
70, 134, 0, 11, 135, 59, 12, 0,
136, 77, 11, 1, 136, 77, 11, 1,
138, 97, 10, 2, 138, 97, 10, 2,
104, 141, 2, 10, 104, 141, 2, 10,
86, 143, 1, 11, 86, 143, 1, 11,
70, 144, 0, 12, 145, 59, 13, 0,
146, 77, 12, 1, 146, 77, 12, 1,
148, 97, 11, 2, 148, 97, 11, 2,
104, 151, 2, 11, 104, 151, 2, 11,
86, 153, 1, 12, 86, 153, 1, 12,
70, 154, 0, 13, 155, 59, 14, 0,
156, 77, 13, 1, 156, 77, 13, 1,
158, 97, 12, 2, 158, 97, 12, 2,
104, 161, 2, 12, 104, 161, 2, 12,
86, 163, 1, 13, 86, 163, 1, 13,
70, 164, 0, 14, 165, 59, 15, 0,
166, 77, 14, 1, 166, 77, 14, 1,
168, 97, 13, 2, 168, 97, 13, 2,
104, 171, 2, 13, 104, 171, 2, 13,
86, 173, 1, 14, 86, 173, 1, 14,
70, 174, 0, 15, 175, 59, 16, 0,
176, 77, 15, 1, 176, 77, 15, 1,
178, 97, 14, 2, 178, 97, 14, 2,
104, 181, 2, 14, 104, 181, 2, 14,
86, 183, 1, 15, 86, 183, 1, 15,
70, 184, 0, 16, 185, 59, 17, 0,
186, 77, 16, 1, 186, 77, 16, 1,
74, 97, 15, 2, 74, 97, 15, 2,
104, 89, 2, 15, 104, 89, 2, 15,
86, 187, 1, 16, 86, 187, 1, 16,
70, 188, 0, 17, 189, 59, 18, 0,
190, 77, 17, 1, 86, 191, 1, 17,
70, 192, 0, 18, 193, 59, 19, 0,
194, 77, 18, 1, 86, 195, 1, 18,
70, 196, 0, 19, 193, 59, 20, 0,
197, 77, 19, 1, 86, 198, 1, 19,
70, 196, 0, 20, 199, 77, 20, 1,
86, 200, 1, 20, 201, 77, 21, 1,
86, 202, 1, 21, 203, 77, 22, 1,
86, 204, 1, 22, 205, 77, 23, 1,
86, 206, 1, 23, 207, 77, 24, 1,
86, 208, 1, 24, 209, 77, 25, 1,
86, 210, 1, 25, 211, 77, 26, 1,
86, 212, 1, 26, 213, 77, 27, 1,
86, 214, 1, 27, 215, 77, 28, 1,
86, 216, 1, 28, 217, 77, 29, 1,
86, 218, 1, 29, 219, 77, 30, 1,
86, 220, 1, 30, 221, 77, 31, 1,
86, 222, 1, 31, 223, 77, 32, 1,
86, 224, 1, 32, 225, 77, 33, 1,
86, 226, 1, 33, 227, 77, 34, 1,
86, 228, 1, 34, 229, 77, 35, 1,
86, 230, 1, 35, 231, 77, 36, 1,
86, 232, 1, 36, 233, 77, 37, 1,
86, 234, 1, 37, 235, 77, 38, 1,
86, 236, 1, 38, 237, 77, 39, 1,
86, 238, 1, 39, 239, 77, 40, 1,
86, 240, 1, 40, 241, 77, 41, 1,
86, 242, 1, 41, 243, 77, 42, 1,
86, 244, 1, 42, 245, 77, 43, 1,
86, 246, 1, 43, 247, 77, 44, 1,
86, 248, 1, 44, 249, 77, 45, 1,
86, 250, 1, 45, 251, 77, 46, 1,
86, 252, 1, 46, 253, 77, 47, 1,
86, 254, 1, 47, 253, 77, 48, 1,
86, 254, 1, 48, 0, 0, 0, 0
};
// Initialize next state table ns[state*4] -> next if 0, next if 1, n0, n1
StateTable::StateTable() {
memcpy(ns, sns, sizeof(ns));
}
/////////////////////////// ZPAQL //////////////////////////
// Write header to out2, return true if HCOMP/PCOMP section is present.
// If pp is true, then write only the postprocessor code.
bool ZPAQL::write(Writer* out2, bool pp) {
if (header.size()<=6) return false;
assert(header[0]+256*header[1]==cend-2+hend-hbegin);
assert(cend>=7);
assert(hbegin>=cend);
assert(hend>=hbegin);
assert(out2);
if (!pp) { // if not a postprocessor then write COMP
for (int i=0; i<cend; ++i)
out2->put(header[i]);
}
else { // write PCOMP size only
out2->put((hend-hbegin)&255);
out2->put((hend-hbegin)>>8);
}
for (int i=hbegin; i<hend; ++i)
out2->put(header[i]);
return true;
}
// Read header from in2
int ZPAQL::read(Reader* in2) {
// Get header size and allocate
int hsize=in2->get();
hsize+=in2->get()*256;
header.resize(hsize+300);
cend=hbegin=hend=0;
header[cend++]=hsize&255;
header[cend++]=hsize>>8;
while (cend<7) header[cend++]=in2->get(); // hh hm ph pm n
// Read COMP
int n=header[cend-1];
for (int i=0; i<n; ++i) {
int type=in2->get(); // component type
if (type<0 || type>255) error("unexpected end of file");
header[cend++]=type; // component type
int size=compsize[type];
if (size<1) error("Invalid component type");
if (cend+size>hsize) error("COMP overflows header");
for (int j=1; j<size; ++j)
header[cend++]=in2->get();
}
if ((header[cend++]=in2->get())!=0) error("missing COMP END");
// Insert a guard gap and read HCOMP
hbegin=hend=cend+128;
if (hend>hsize+129) error("missing HCOMP");
while (hend<hsize+129) {
assert(hend<header.isize()-8);
int op=in2->get();
if (op==-1) error("unexpected end of file");
header[hend++]=op;
}
if ((header[hend++]=in2->get())!=0) error("missing HCOMP END");
assert(cend>=7 && cend<header.isize());
assert(hbegin==cend+128 && hbegin<header.isize());
assert(hend>hbegin && hend<header.isize());
assert(hsize==header[0]+256*header[1]);
assert(hsize==cend-2+hend-hbegin);
allocx(rcode, rcode_size, 0); // clear JIT code
return cend+hend-hbegin;
}
// Free memory, but preserve output, sha1 pointers
void ZPAQL::clear() {
cend=hbegin=hend=0; // COMP and HCOMP locations
a=b=c=d=f=pc=0; // machine state
header.resize(0);
h.resize(0);
m.resize(0);
r.resize(0);
allocx(rcode, rcode_size, 0);
}
// Constructor
ZPAQL::ZPAQL() {
output=0;
sha1=0;
rcode=0;
rcode_size=0;
clear();
outbuf.resize(1<<14);
bufptr=0;
}
ZPAQL::~ZPAQL() {
allocx(rcode, rcode_size, 0);
}
// Initialize machine state as HCOMP
void ZPAQL::inith() {
assert(header.isize()>6);
assert(output==0);
assert(sha1==0);
init(header[2], header[3]); // hh, hm
}
// Initialize machine state as PCOMP
void ZPAQL::initp() {
assert(header.isize()>6);
init(header[4], header[5]); // ph, pm
}
// Flush pending output
void ZPAQL::flush() {
if (output) output->write(&outbuf[0], bufptr);
if (sha1) sha1->write(&outbuf[0], bufptr);
bufptr=0;
}
// pow(2, x)
static double pow2(int x) {
double r=1;
for (; x>0; x--) r+=r;
return r;
}
// Return memory requirement in bytes
double ZPAQL::memory() {
double mem=pow2(header[2]+2)+pow2(header[3]) // hh hm
+pow2(header[4]+2)+pow2(header[5]) // ph pm
+header.size();
int cp=7; // start of comp list
for (int i=0; i<header[6]; ++i) { // n
assert(cp<cend);
double size=pow2(header[cp+1]); // sizebits
switch(header[cp]) {
case CM: mem+=4*size; break;
case ICM: mem+=64*size+1024; break;
case MATCH: mem+=4*size+pow2(header[cp+2]); break; // bufbits
case MIX2: mem+=2*size; break;
case MIX: mem+=4*size*header[cp+3]; break; // m
case ISSE: mem+=64*size+2048; break;
case SSE: mem+=128*size; break;
}
cp+=compsize[header[cp]];
}
return mem;
}
// Initialize machine state to run a program.