-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimpleqtcryptor.cpp
1790 lines (1582 loc) · 50.5 KB
/
simpleqtcryptor.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
#include "simpleqtcryptor.h"
#ifdef WITH_SERPENT_INCLUDE_FAST_SBOX
#include "serpent_sbox.h"
#endif
#include <QString>
#include <QCryptographicHash>
#include <QtEndian>
#include <QDate>
#include <QTime>
#include <QDebug>
#define ROUNDS 32
#define KEYSIZE_RC5 20
#define KEYSIZE_SERPENT 32
#define SSIZE_RC5 66
#define SSIZE_SERPENT 132
#define P32 0xb7e17163
#define Q32 0x9e3779b9
#define P64 Q_UINT64_C(0xb7e151628aed2a6b);
#define Q64 Q_UINT64_C(0x9e3779b97f4a7c15);
#define ROTL32(x,y) (((x)<<(y)) | ((x)>>(32-(y))))
#define ROTR32(x,y) (((x)>>(y)) | ((x)<<(32-(y))))
#define ROTL64(x,y) (((x)<<(y)) | ((x)>>(64-(y))))
#define ROTR64(x,y) (((x)>>(y)) | ((x)<<(64-(y))))
namespace SimpleQtCryptor {
#ifdef WITHRC5
QByteArray header_RC5_32_32_20 = QString("RC5/32/32/20:").toAscii();
QByteArray header_RC5_64_32_20 = QString("RC5/64/32/20:").toAscii();
#endif
QByteArray header_SERPENT_32 = QString("SERPENT/32:").toLatin1();
QByteArray header_CBC = QString("CBC:PADN::").toLatin1();
QByteArray header_CFB = QString("CFB::").toLatin1();
#ifdef WITH_SERPENT_FAST_SBOX
inline quint32 serpent_sbox_fast(int sbox, quint32 X);
#else
void serpent_sbox_it(int sbox, quint32 &X1, quint32 &X2,
quint32 &X3, quint32 &X4);
#endif
#ifdef WITHRC5
Algorithm Info::fastRC5() {
#if (QT_POINTER_SIZE==4)
return RC5_32_32_20;
#else
return RC5_64_32_20;
#endif
}
#endif
QString Info::errorText(Error e) {
switch (e) {
case NoError:
return QString("NoError");
case ErrorNoAlgorithm:
return QString("ErrorNoAlgorithm");
case ErrorNoMode:
return QString("ErrorNoMode");
case ErrorInvalidKey:
return QString("ErrorInvalidKey");
case ErrorNotEnoughData:
return QString("ErrorNotEnoughData");
case ErrorModeNotImplemented:
return QString("ErrorModeNotImplemented");
case ErrorAlgorithmNotImplemented:
return QString("ErrorAlgorithmNotImplemented");
case ErrorChecksumNotImplemented:
return QString("ErrorChecksumNotImplemented");
case ErrorAlreadyError:
return QString("ErrorAlreadyError");
default:
return QString("UnknownError");
}
return QString();
}
Key::Key() {
#ifdef WITHRC5
s32 = 0;
s64 = 0;
#endif
serpent = 0;
}
Key::Key(const QByteArray &k) {
key = k;
#ifdef WITHRC5
s32 = 0;
s64 = 0;
#endif
serpent = 0;
}
Key::Key(const QString &k) {
QCryptographicHash qch(QCryptographicHash::Sha1);
qch.addData(k.toUtf8());
key = qch.result();
#ifdef WITHRC5
s32 = 0;
s64 = 0;
#endif
serpent = 0;
}
Key::~Key() {
#ifdef WITHRC5
delete[] s32;
delete[] s64;
#endif
delete serpent;
}
QByteArray Key::resizeKey(int ks) {
QByteArray newKey(ks, 0);
unsigned char *ok = (unsigned char *)(key.data());
unsigned char *nk = (unsigned char *)(newKey.data());
for ( int i = 0 ; i < key.size() ; i++ ) {
nk[i%ks] ^= ok[i];
}
return newKey;
}
#ifdef WITHRC5
void Key::expandKeyRc532() {
if (s32) return;
if ( KEYSIZE_RC5 != keyRc5.size() ) {
keyRc5 = resizeKey(KEYSIZE_RC5);
}
s32 = new quint32[SSIZE_RC5];
quint32 *s = s32;
unsigned char *k = (unsigned char *)(keyRc5.data());
quint32 L[5];
L[0] = qFromLittleEndian<quint32>(k);
L[1] = qFromLittleEndian<quint32>(k+4);
L[2] = qFromLittleEndian<quint32>(k+8);
L[3] = qFromLittleEndian<quint32>(k+12);
L[4] = qFromLittleEndian<quint32>(k+16);
s[0] = P32;
for ( int i = 1 ; i < SSIZE_RC5 ; i++ ) {
s[i] = s[i-1] + Q32;
}
int i=0 , j=0;
quint32 A=0 , B=0;
for ( int x = 0 ; x < ROUNDS ; x++ ) {
A = s[i] = ROTL32(s[i] + (A+B), 3);
B = L[j] = ROTL32(L[j] + (A+B), (A+B) & 31 );
i = (i + 1) % SSIZE_RC5;
j = (j + 1) % 5;
}
}
#endif
#ifdef WITHRC5
void Key::expandKeyRc564() {
if (s64) return;
if ( KEYSIZE_RC5 != keyRc5.size() ) {
keyRc5 = resizeKey(KEYSIZE_RC5);
}
s64 = new quint64[SSIZE_RC5];
quint64 *s = s64;
unsigned char *k = (unsigned char *)(keyRc5.data());
quint64 L[3];
L[0] = qFromLittleEndian<quint64>(k);
L[1] = qFromLittleEndian<quint64>(k+8);
L[2] = qFromLittleEndian<quint32>(k+16);
s[0] = P64;
for ( int i = 1 ; i < SSIZE_RC5 ; i++ ) {
s[i] = s[i-1] + Q64;
}
int i=0 , j=0;
quint64 A=0 , B=0;
for ( int x = 0 ; x < ROUNDS ; x++ ) {
A = s[i] = ROTL64(s[i] + (A+B), 3);
B = L[j] = ROTL64(L[j] + (A+B), (A+B) & 63 );
i = (i + 1) % SSIZE_RC5;
j = (j + 1) % 3;
}
}
#endif
void Key::expandKeySerpent() {
quint32 i;
quint32 tmp;
quint32 *s;
if (serpent) return;
if ( KEYSIZE_SERPENT != keySerpent.size() ) {
keySerpent = resizeKey(KEYSIZE_SERPENT);
}
serpent = new quint32[SSIZE_SERPENT];
s = new quint32[SSIZE_SERPENT + 8];
unsigned char *k = (unsigned char *)(keySerpent.data());
for ( i=0 ; i<8 ; i++ ) {
s[i] = qFromLittleEndian<quint32>(k + 4*i);
}
for(i=8 ; i < SSIZE_SERPENT + 8 ; i++) {
tmp = ( s[i-8] ^ s[i-5] ^ s[i-3] ^ s[i-1] ^ Q32 ^ (i-8) );
serpent[i-8] = s[i] = ROTL32(tmp, 11);
}
for(i=0;i<33;i++) {
#ifdef WITH_SERPENT_FAST_SBOX
tmp = (35-i) % 8;
for (int j=0;j<4;j++) {
serpent[4*i+j] = serpent_sbox_fast(tmp,serpent[4*i+j]);
}
#else
serpent_sbox_it( (35-i)%8 , serpent[4*i ], serpent[4*i+1],
serpent[4*i+2], serpent[4*i+3] );
#endif
}
delete s;
}
/* *** ENCRYPTOR *** */
Encryptor::Encryptor(QSharedPointer<Key> k, Algorithm a, Mode m, Checksum c) {
key = k;
algorithm = a;
mode = m;
checksum = c;
#ifdef WITHRC5
if ( algorithm == RC5_FAST_32_20 ) {
algorithm = Info::fastRC5();
}
#endif
state = StateReset;
modex = 0;
}
Encryptor::~Encryptor() {
delete modex;
}
Error Encryptor::encrypt(const QByteArray &plain, QByteArray &cipher, bool end) {
QByteArray tmpIn;
switch ( state ) {
case StateReset:
switch ( algorithm ) {
#ifdef WITHRC5
case RC5_32_32_20:
tmpIn.append(header_RC5_32_32_20);
break;
case RC5_64_32_20:
tmpIn.append(header_RC5_64_32_20);
break;
#endif
case SERPENT_32:
tmpIn.append(header_SERPENT_32);
break;
case NoAlgorithm:
case DetectAlgorithm:
#ifdef WITHRC5
case RC5_FAST_32_20:
#endif
state = StateError;
return ErrorNoAlgorithm;
}
switch ( mode ) {
case ModeCBC:
tmpIn.append(header_CBC);
if ( 0 == modex) modex = new CBC(key,algorithm);
break;
case ModeCFB:
tmpIn.append(header_CFB);
if ( 0 == modex) modex = new CFB(key, algorithm);
break;
case NoMode:
case DetectMode:
default:
state = StateError;
return ErrorNoMode;
}
if ( checksum != NoChecksum ) {
state = StateError;
return ErrorChecksumNotImplemented;
}
// switch (checksum) HERE
state = StateOn;
case StateOn:
tmpIn.append(plain);
cipher = modex->encrypt(tmpIn, end);
break;
case StateError:
default:
return ErrorAlreadyError;
}
if (end) {
state = StateReset;
}
return NoError;
}
/* *** DECRYPTOR *** */
Decryptor::Decryptor(QSharedPointer<Key> k, Algorithm a, Mode m) {
key = k;
algorithm = a;
mode = m;
state = StateReset;
checksum = NoChecksum;
modex = 0;
}
Decryptor::~Decryptor() {
delete modex;
}
Checksum Decryptor::getChecksumType() {
return checksum;
}
Error Decryptor::decrypt(const QByteArray &cipher, QByteArray &plain, bool end) {
QByteArray expectHeader;
QByteArray remainingFromHeader;
QByteArray tmpIn;
QByteArray tmpOut;
int neededForHeader = -1;
int neededForIv = -1;
switch ( state ) {
case StateReset:
switch ( algorithm ) {
#ifdef WITHRC5
case RC5_32_32_20:
expectHeader.append(header_RC5_32_32_20);
neededForIv = 8;
break;
case RC5_64_32_20:
expectHeader.append(header_RC5_64_32_20);
neededForIv = 16;
break;
#endif
case SERPENT_32:
expectHeader.append(header_SERPENT_32);
neededForIv = 16;
break;
case NoAlgorithm:
case DetectAlgorithm:
#ifdef WITHRC5
case RC5_FAST_32_20:
#endif
state = StateError;
return ErrorNoAlgorithm;
}
switch ( mode ) {
case ModeCBC:
expectHeader.append(header_CBC);
neededForHeader = (((neededForIv + expectHeader.size() - 1) / neededForIv) + 1) * neededForIv;
if ( 0 == modex) modex = new CBC(key, algorithm);
break;
case ModeCFB:
expectHeader.append(header_CFB);
neededForHeader = neededForIv + expectHeader.size();
if ( 0 == modex) modex = new CFB(key, algorithm);
break;
case NoMode:
case DetectMode:
default:
state = StateError;
return ErrorNoMode;
}
if ( cipher.size() < neededForHeader ) {
state = StateError;
return ErrorNotEnoughData;
}
tmpOut = modex->decrypt(cipher.left(neededForHeader), false);
if ( tmpOut.startsWith(expectHeader) ) {
remainingFromHeader = tmpOut.right(tmpOut.size() - expectHeader.size());
tmpOut.clear();
tmpIn = cipher.right(cipher.size() - neededForHeader);
state = StateOn;
} else {
state = StateError;
tmpOut.clear();
return ErrorInvalidKey;
}
case StateOn:
if ( tmpIn.isEmpty() ) {
tmpIn = cipher;
}
tmpOut = modex->decrypt(tmpIn, end);
break;
case StateError:
default:
return ErrorAlreadyError;
}
if ( ! remainingFromHeader.isEmpty() ) {
tmpOut.prepend(remainingFromHeader);
}
if (end) {
state = StateReset;
}
plain = tmpOut;
return NoError;
}
/* *** DECRYPTOR WIZARD ENTRY *** */
class DecryptorWizardEntry {
public:
QSharedPointer<Key> key;
Algorithm alg;
Mode mode;
Checksum csum;
};
/* *** DECRYPTOR WIZARD *** */
DecryptorWizard::DecryptorWizard() {
}
DecryptorWizard::DecryptorWizard(QSharedPointer<Key> k, Algorithm a, Mode m) {
addParameters(k, a, m);
}
DecryptorWizard::~DecryptorWizard() {
for ( int i=0 ; i < entries.size() ; i++ ) {
delete entries.at(i);
}
}
void DecryptorWizard::addParameters(QSharedPointer<Key> k, Algorithm a, Mode m) {
DecryptorWizardEntry *dwe = new DecryptorWizardEntry();
dwe->key = k;
dwe->alg = a;
dwe->mode = m;
entries.append(dwe);
}
Error DecryptorWizard::decrypt(const QByteArray &cipher, QByteArray &plain, QSharedPointer<Decryptor> &decryptor, bool end) {
#ifdef WITHRC5
Algorithm aList[3] = { RC5_32_32_20, RC5_64_32_20, SERPENT_32 };
int aL = 3;
#else
Algorithm aList[1] = { SERPENT_32 };
int aL = 1;
#endif
Mode mList[2] = { ModeCBC, ModeCFB };
int mL = 2;
int eL = entries.size();
int eI, aI, mI;
Decryptor *dx;
Error dxError;
Error retError = ErrorInvalidKey;
for (eI=0 ; eI<eL ; eI++) for (aI=0 ; aI<aL ; aI++) for (mI=0 ; mI<mL ; mI++) {
if ( (entries.at(eI)->alg != aList[aI]) && (entries.at(eI)->alg != DetectAlgorithm) ) continue;
if ( (entries.at(eI)->mode != mList[mI]) && (entries.at(eI)->mode != DetectMode) ) continue;
dx = new Decryptor(entries.at(eI)->key, aList[aI], mList[mI]);
dxError = dx->decrypt(cipher, plain, end);
switch (dxError) {
case NoError:
decryptor = QSharedPointer<Decryptor>(dx);
return NoError;
case ErrorNotEnoughData:
retError = ErrorNotEnoughData;
break;
case ErrorInvalidKey:
if ( ErrorNotEnoughData != retError ) {
retError = ErrorInvalidKey;
}
break;
default:
delete dx;
return dxError;
}
delete dx;
}
return retError;
}
Error DecryptorWizard::decryptToEnd(const QByteArray &cipher, QByteArray &plain) {
QSharedPointer<Decryptor> qspd;
Error er = decrypt(cipher, plain, qspd, true);
return er;
}
/* *** INITIALIZATION VECTOR *** */
QByteArray InitializationVector::getVector8() {
QByteArray ret(8, 0);
quint32 A = ((quint32)(qrand())) ^ ((quint32)(QTime::currentTime().msecsTo(QTime(23,59,59,999))));
quint32 B = ((quint32)(qrand())) ^ ((quint32)(QDate::currentDate().daysTo(QDate(2999,12,31))));
qToLittleEndian(A, (uchar *)(ret.data()));
qToLittleEndian(B, (uchar *)(ret.data() + 4));
ret[0] = ( (uchar)(ret[0]) | 128 );
return ret;
}
QByteArray InitializationVector::getVector16() {
QByteArray ret(16, 0);
quint32 A = ((quint32)(qrand())) ^ ((quint32)(QTime::currentTime().msecsTo(QTime(23,59,59,999))));
quint32 B = ((quint32)(qrand())) ^ ((quint32)(QDate::currentDate().daysTo(QDate(2999,12,31))));
quint32 C = (quint32)(qrand());
quint32 D = (quint32)(qrand());
qToLittleEndian(A, (uchar *)(ret.data()));
qToLittleEndian(B, (uchar *)(ret.data() + 4));
qToLittleEndian(C, (uchar *)(ret.data() + 8));
qToLittleEndian(D, (uchar *)(ret.data() + 12));
ret[0] = ( (uchar)(ret[0]) | 128 );
return ret;
}
void InitializationVector::initiate() {
qsrand((quint32)(QTime::currentTime().msecsTo(QTime(23,59,59,999))));
}
/* *** CBC *** */
CBC::CBC(QSharedPointer<Key> k, Algorithm a) {
algorithm = a;
key = k;
reset();
}
CBC::~CBC() {
}
void CBC::reset() {
worksize = -1;
buffer.clear();
cbcBuffer.clear();
padHostageBuffer.clear();
}
/*
*
* PSEUDO
*
* documentation missing
*
*/
QByteArray CBC::encrypt(const QByteArray plain, bool end) {
int cipherpos = 0;
int padsize = -1;
bool iv = false;
// set initialization vector if first data
if ( -1 == worksize ) {
switch (algorithm) {
#ifdef WITHRC5
case RC5_32_32_20:
cbcBuffer = InitializationVector::getVector8();
worksize = 8;
iv = true;
key->expandKeyRc532();
break;
case RC5_64_32_20:
cbcBuffer = InitializationVector::getVector16();
worksize = 16;
iv = true;
key->expandKeyRc564();
break;
#endif
case SERPENT_32:
cbcBuffer = InitializationVector::getVector16();
worksize = 16;
iv = true;
key->expandKeySerpent();
break;
default:
buffer.clear();
return QByteArray();
}
}
buffer.append(plain);
int cipherlen = ( buffer.size() / worksize ) * worksize;
padsize = worksize - (buffer.size() - cipherlen);
if (iv) cipherlen += cbcBuffer.size();
if (end) cipherlen += worksize;
QByteArray cipher = QByteArray(cipherlen, 0);
if (end) {
buffer.append(QByteArray(padsize, (char)padsize));
}
if ( iv ) while (cipherpos < worksize) {
cipher[cipherpos] = cbcBuffer[cipherpos];
cipherpos++;
}
uchar *bufdat = (uchar *)buffer.data();
uchar *cipdat = (uchar *)cipher.data();
uchar *cbcdat = (uchar *)cbcBuffer.data();
int bufpos = 0;
switch (algorithm) {
#ifdef WITHRC5
case RC5_32_32_20:
{
quint32 cbc1 = qFromLittleEndian<quint32>(cbcdat);
quint32 cbc2 = qFromLittleEndian<quint32>(cbcdat + 4);
quint32 buf1;
quint32 buf2;
while ( cipherpos < cipherlen ) {
buf1 = qFromLittleEndian<quint32>(bufdat + bufpos);
buf2 = qFromLittleEndian<quint32>(bufdat + bufpos + 4);
cbc1 ^= buf1;
cbc2 ^= buf2;
rc5_32_encrypt_2w(cbc1, cbc2, key->s32);
qToLittleEndian(cbc1, cipdat + cipherpos);
qToLittleEndian(cbc2, cipdat + cipherpos + 4);
cipherpos += worksize;
bufpos += worksize;
}
qToLittleEndian(cbc1, cbcdat);
qToLittleEndian(cbc2, cbcdat + 4);
}
break;
case RC5_64_32_20:
{
quint64 cbc1 = qFromLittleEndian<quint64>(cbcdat);
quint64 cbc2 = qFromLittleEndian<quint64>(cbcdat + 8);
quint64 buf1;
quint64 buf2;
while ( cipherpos < cipherlen ) {
buf1 = qFromLittleEndian<quint64>(bufdat + bufpos);
buf2 = qFromLittleEndian<quint64>(bufdat + bufpos + 8);
cbc1 ^= buf1;
cbc2 ^= buf2;
rc5_64_encrypt_2w(cbc1, cbc2, key->s64);
qToLittleEndian(cbc1, cipdat + cipherpos);
qToLittleEndian(cbc2, cipdat + cipherpos + 8);
cipherpos += worksize;
bufpos += worksize;
}
qToLittleEndian(cbc1, cbcdat);
qToLittleEndian(cbc2, cbcdat + 8);
}
break;
#endif
case SERPENT_32:
{
quint32 cbc1 = qFromLittleEndian<quint32>(cbcdat);
quint32 cbc2 = qFromLittleEndian<quint32>(cbcdat + 4);
quint32 cbc3 = qFromLittleEndian<quint32>(cbcdat + 8);
quint32 cbc4 = qFromLittleEndian<quint32>(cbcdat + 12);
quint32 buf1;
quint32 buf2;
quint32 buf3;
quint32 buf4;
while ( cipherpos < cipherlen ) {
buf1 = qFromLittleEndian<quint64>(bufdat + bufpos);
buf2 = qFromLittleEndian<quint64>(bufdat + bufpos + 4);
buf3 = qFromLittleEndian<quint64>(bufdat + bufpos + 8);
buf4 = qFromLittleEndian<quint64>(bufdat + bufpos + 12);
cbc1 ^= buf1;
cbc2 ^= buf2;
cbc3 ^= buf3;
cbc4 ^= buf4;
serpent_encrypt_4w(cbc1, cbc2, cbc3, cbc4, key->serpent);
qToLittleEndian(cbc1, cipdat + cipherpos);
qToLittleEndian(cbc2, cipdat + cipherpos + 4);
qToLittleEndian(cbc3, cipdat + cipherpos + 8);
qToLittleEndian(cbc4, cipdat + cipherpos + 12);
cipherpos += worksize;
bufpos += worksize;
}
qToLittleEndian(cbc1, cbcdat);
qToLittleEndian(cbc2, cbcdat + 4);
qToLittleEndian(cbc3, cbcdat + 8);
qToLittleEndian(cbc4, cbcdat + 12);
}
break;
default:
cipher.clear();
return cipher;
}
if (end) {
reset();
} else {
buffer = buffer.right( buffer.size() - bufpos );
}
return cipher;
}
QByteArray CBC::decrypt(const QByteArray cipher, bool end) {
int bufferpos = 0;
int plainpos = 0;
buffer.append(cipher);
if ( -1 == worksize ) {
switch (algorithm) {
#ifdef WITHRC5
case RC5_32_32_20:
if ( buffer.size() < 8 ) return QByteArray();
cbcBuffer = buffer.left(8);
worksize = 8;
bufferpos = 8;
key->expandKeyRc532();
break;
case RC5_64_32_20:
if ( buffer.size() < 16 ) return QByteArray();
cbcBuffer = buffer.left(16);
worksize = 16;
bufferpos = 16;
key->expandKeyRc564();
break;
#endif
case SERPENT_32:
if ( buffer.size() < 16 ) return QByteArray();
cbcBuffer = buffer.left(16);
worksize = 16;
bufferpos = 16;
key->expandKeySerpent();
break;
default:
buffer.clear();
return QByteArray();
}
}
int plainlen = ( (buffer.size() - bufferpos) / worksize ) * worksize + padHostageBuffer.size();
QByteArray plain(plainlen, 0);
while (plainpos < padHostageBuffer.size()) {
plain[plainpos] = padHostageBuffer[plainpos];
plainpos++;
}
padHostageBuffer.clear();
uchar *bufdat = (uchar *)buffer.data();
uchar *plndat = (uchar *)plain.data();
uchar *cbcdat = (uchar *)cbcBuffer.data();
switch (algorithm) {
#ifdef WITHRC5
case RC5_32_32_20:
{
quint32 cbc1 = qFromLittleEndian<quint32>(cbcdat);
quint32 cbc2 = qFromLittleEndian<quint32>(cbcdat + 4);
quint32 buf1;
quint32 buf2;
quint32 pln1;
quint32 pln2;
while ( plainpos < plainlen ) {
pln1 = buf1 = qFromLittleEndian<quint32>(bufdat + bufferpos);
pln2 = buf2 = qFromLittleEndian<quint32>(bufdat + bufferpos + 4);
rc5_32_decrypt_2w(pln1,pln2,key->s32);
qToLittleEndian( (pln1 ^ cbc1) , plndat + plainpos);
qToLittleEndian( (pln2 ^ cbc2) , plndat + plainpos + 4);
cbc1 = buf1;
cbc2 = buf2;
plainpos += worksize;
bufferpos += worksize;
}
qToLittleEndian(cbc1, cbcdat);
qToLittleEndian(cbc2, cbcdat + 4);
}
break;
case RC5_64_32_20:
{
quint64 cbc1 = qFromLittleEndian<quint64>(cbcdat);
quint64 cbc2 = qFromLittleEndian<quint64>(cbcdat + 8);
quint64 buf1;
quint64 buf2;
quint64 pln1;
quint64 pln2;
while ( plainpos < plainlen ) {
pln1 = buf1 = qFromLittleEndian<quint64>(bufdat + bufferpos);
pln2 = buf2 = qFromLittleEndian<quint64>(bufdat + bufferpos + 8);
rc5_64_decrypt_2w(pln1,pln2,key->s64);
qToLittleEndian( (pln1 ^ cbc1) , plndat + plainpos);
qToLittleEndian( (pln2 ^ cbc2) , plndat + plainpos + 8);
cbc1 = buf1;
cbc2 = buf2;
plainpos += worksize;
bufferpos += worksize;
}
qToLittleEndian(cbc1, cbcdat);
qToLittleEndian(cbc2, cbcdat + 8);
}
break;
#endif
case SERPENT_32:
{
quint32 cbc1 = qFromLittleEndian<quint32>(cbcdat);
quint32 cbc2 = qFromLittleEndian<quint32>(cbcdat + 4);
quint32 cbc3 = qFromLittleEndian<quint32>(cbcdat + 8);
quint32 cbc4 = qFromLittleEndian<quint32>(cbcdat + 12);
quint32 buf1;
quint32 buf2;
quint32 buf3;
quint32 buf4;
quint32 pln1;
quint32 pln2;
quint32 pln3;
quint32 pln4;
while ( plainpos < plainlen ) {
pln1 = buf1 = qFromLittleEndian<quint32>(bufdat+bufferpos);
pln2 = buf2 = qFromLittleEndian<quint32>(bufdat+bufferpos + 4);
pln3 = buf3 = qFromLittleEndian<quint32>(bufdat+bufferpos + 8);
pln4 = buf4 = qFromLittleEndian<quint32>(bufdat+bufferpos + 12);
serpent_decrypt_4w(pln1,pln2,pln3,pln4,key->serpent);
qToLittleEndian( (pln1 ^ cbc1) , plndat + plainpos);
qToLittleEndian( (pln2 ^ cbc2) , plndat + plainpos + 4);
qToLittleEndian( (pln3 ^ cbc3) , plndat + plainpos + 8);
qToLittleEndian( (pln4 ^ cbc4) , plndat + plainpos + 12);
cbc1 = buf1;
cbc2 = buf2;
cbc3 = buf3;
cbc4 = buf4;
plainpos += worksize;
bufferpos += worksize;
}
qToLittleEndian(cbc1, cbcdat);
qToLittleEndian(cbc2, cbcdat + 4);
qToLittleEndian(cbc3, cbcdat + 8);
qToLittleEndian(cbc4, cbcdat + 12);
}
break;
default:
plain.clear();
return plain;
}
if (end) {
// in case we dont have any valid padding, the only explanation
// is a transmission error, or someone modified the file
// however, this is not the layer where to discover such problems
// and I have nowhere to report a problem, so I just need
// to avoid crashing
int padc = 0;
if ( ! plain.isEmpty() ) {
padc = (int)plain[plainlen - 1];
if ( padc > plain.size() ) {
padc = 0;
}
}
if ( 0 < padc && padc <= 16 ) {
plain = plain.left(plainlen - padc);
}
reset();
} else {
buffer = buffer.right(buffer.size() - bufferpos);
// there is a chance, that we will not get more data,
// but end=false anyways. in this case, we must not
// return possible pad data as plain text
if ( buffer.size() == 0 && plainlen > 0 ) {
uchar lastByte = plain[plainlen - 1];
if ( 0 < lastByte && lastByte <= 16 ) {
QByteArray padPattern((int)lastByte,(char)lastByte);
if (plain.endsWith(padPattern)) {
padHostageBuffer = plain.right(worksize);
plain = plain.left(plainlen - worksize);
plainlen -= worksize;
}
}
}
}
return plain;
}
/* *** CFB *** */
CFB::CFB(QSharedPointer<Key> k, Algorithm a) {
algorithm = a;
key = k;
reset();
}
CFB::~CFB() {
}
void CFB::reset() {
bufferpos = -1;
buffer.clear();
}
/*
* PSEUDO:
*
* if first: make IV, put in buffer
*
* if unused bytes in buffer
* cipher = buffer XOR plain
* buffer = cipher
*
* while ( more to encrypt )
* encrypt buffer
* cipher = buffer XOR plain
* buffer = cipher
*
*
*/
QByteArray CFB::encrypt(const QByteArray plain, bool end) {
int plainpos = 0;
int plainlen = plain.size();
int cipherpos = 0;
int bufferlen = buffer.size();
int copysize = 0;
QByteArray cipher(plainlen, 0);
uchar *bufdat = 0;
// set initialization vector if first data
if ( -1 == bufferpos ) {
switch (algorithm) {
#ifdef WITHRC5
case RC5_32_32_20:
buffer = InitializationVector::getVector8();
key->expandKeyRc532();
bufferlen = 8;
break;
case RC5_64_32_20:
buffer = InitializationVector::getVector16();
key->expandKeyRc564();
bufferlen = 16;
break;
#endif
case SERPENT_32:
buffer = InitializationVector::getVector16();
key->expandKeySerpent();
bufferlen = 16;
break;
default:
buffer.clear();
return QByteArray();
}
cipher.prepend(buffer);
bufferpos = bufferlen;
cipherpos += bufferlen;
}
bufdat = (uchar *)(buffer.data());
uchar *cphdat = (uchar *)cipher.data();
uchar *plndat = (uchar *)plain.data();
copysize = qMin( bufferlen - bufferpos , plainlen - plainpos );
// in case the buffer contains unused data from last encrypt,
// use those bytes first
while ( 0 < copysize ) {
bufdat[bufferpos] = plndat[plainpos] ^ bufdat[bufferpos];
cphdat[cipherpos] = bufdat[bufferpos];
cipherpos++;
plainpos++;
bufferpos++;
copysize--;
}
copysize = qMin( bufferlen , plainlen - plainpos );
if ( bufferlen == copysize ) {