-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimpleqtcryptor.h
265 lines (216 loc) · 6.16 KB
/
simpleqtcryptor.h
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
// remove this line if you do not want RC5
//#define WITHRC5
// this line enables the print serpent_sbox.h functionality
// #define WITH_SERPENT_PRINT_SBOX_H
// remove this line if you want a slightly smaller and much slower
// binary (serpent only)
#define WITH_SERPENT_INCLUDE_FAST_SBOX
#ifndef SIMPLEQTCRYPTOR_H
#define SIMPLEQTCRYPTOR_H
#include <QtGlobal>
#include <QByteArray>
#include <QSharedPointer>
#include <QObject>
class QString;
namespace SimpleQtCryptor {
class Encryptor;
class Decryptor;
class LayerMode;
class CFB;
class CBC;
enum Algorithm {
NoAlgorithm = 0,
DetectAlgorithm,
#ifdef WITHRC5
RC5_FAST_32_20,
RC5_32_32_20,
RC5_64_32_20,
#endif
SERPENT_32
};
enum Mode {
NoMode = 0,
DetectMode,
ModeCBC,
ModeCFB
};
enum Checksum {
NoChecksum = 0,
DetectChecksum,
ChecksumSoft,
ChecksumHard
};
enum Error {
NoError = 0,
// ErrorNoKey,
ErrorNoAlgorithm,
ErrorNoMode,
ErrorInvalidKey,
ErrorNotEnoughData,
ErrorModeNotImplemented,
ErrorAlgorithmNotImplemented,
ErrorChecksumNotImplemented,
ErrorAlreadyError
};
enum State {
StateReset = 0,
StateOn,
StateError
};
class Info {
public:
static Algorithm fastRC5();
static QString errorText(Error e);
};
class Key : public QObject {
Q_OBJECT
public:
Key();
Key(const QByteArray &key);
Key(const QString &key);
~Key();
// not for use by end application
#ifdef WITHRC5
void expandKeyRc532();
void expandKeyRc564();
#endif
void expandKeySerpent();
// variables
QByteArray key;
#ifdef WITHRC5
QByteArray keyRc5;
quint32 *s32;
quint64 *s64;
#endif
QByteArray keySerpent;
quint32 *serpent;
private:
QByteArray resizeKey(int ks);
};
/*
* About end and reset()
* - If you encrypt/decrypt a piece of data (ie a file) in one chunk
* make sure end=true. After this, you can use the same LayerMode
* object to encrypt/decrypt something else
* - If you encrypt/decrypt i piece of data (ie a file or a network
* conversation) in more than one chunk, make sure the last chunk
* only has end=true.
* - Call reset() only if you want to start over after an error
* (typically ErrorInvalidKey or ErrorNotEnoughData);
* as long as you use end=true, you never need to reset().
*/
class Encryptor : public QObject {
Q_OBJECT
public:
Encryptor(QSharedPointer<Key> k, Algorithm a, Mode m, Checksum c);
~Encryptor();
Error encrypt(const QByteArray &plain, QByteArray &cipher, bool end);
void reset();
private:
QSharedPointer<Key> key;
Algorithm algorithm;
Mode mode;
Checksum checksum;
State state;
LayerMode *modex;
};
// will attempt all different combinations, and give you a
// Decryptor back to decrypt rest of data or more messages
// from the same source
class DecryptorWizardEntry;
class DecryptorWizard {
public:
DecryptorWizard();
DecryptorWizard(QSharedPointer<Key> k, Algorithm a = DetectAlgorithm, Mode m = DetectMode);
~DecryptorWizard();
void addParameters(QSharedPointer<Key> k, Algorithm a = DetectAlgorithm, Mode m = DetectMode);
Error decrypt(const QByteArray &cipher, QByteArray &plain, QSharedPointer<Decryptor> &decryptor, bool end = false);
Error decryptToEnd(const QByteArray &cipher, QByteArray &plain);
private:
QList<DecryptorWizardEntry*> entries;
};
class Decryptor : public QObject {
Q_OBJECT
public:
Decryptor(QSharedPointer<Key> k, Algorithm a, Mode m);
~Decryptor();
Error decrypt(const QByteArray &cipher, QByteArray &plain, bool end);
void reset();
Checksum getChecksumType();
private:
QSharedPointer<Key> key;
Algorithm algorithm;
Mode mode;
State state;
Checksum checksum;
LayerMode *modex;
};
class InitializationVector {
public:
static QByteArray getVector8();
static QByteArray getVector16();
static void initiate();
};
/* *** Layer 2 : mode layer *** */
/*
* A single LayerMode object can handle only one encrypt OR decrypt
* at a time
*/
class LayerMode {
public:
virtual QByteArray encrypt(const QByteArray plain, bool end) = 0;
virtual QByteArray decrypt(const QByteArray cipher, bool end) = 0;
virtual void reset() = 0;
virtual ~LayerMode() {};
};
class CFB : public LayerMode {
public:
CFB(QSharedPointer<Key> k, Algorithm a);
~CFB();
QByteArray encrypt(const QByteArray plain, bool end = false);
QByteArray decrypt(const QByteArray cipher, bool end = false);
void reset();
private:
QByteArray buffer;
int bufferpos;
Algorithm algorithm;
QSharedPointer<Key> key;
};
class CBC : public LayerMode {
public:
CBC(QSharedPointer<Key> k, Algorithm a);
~CBC();
QByteArray encrypt(const QByteArray plain, bool end);
QByteArray decrypt(const QByteArray cipher, bool end);
void reset();
private:
QByteArray buffer;
QByteArray cbcBuffer;
QByteArray padHostageBuffer;
int worksize;
Algorithm algorithm;
QSharedPointer<Key> key;
};
/* *** Layer 1 : block layer - experts only *** */
#ifdef WITHRC5
// input replaced by output
void rc5_32_encrypt_2w(quint32 &X1, quint32 &X2, const quint32 *s);
void rc5_64_encrypt_2w(quint64 &X1, quint64 &X2, const quint64 *s);
void rc5_32_decrypt_2w(quint32 &X1, quint32 &X2, const quint32 *s);
void rc5_64_decrypt_2w(quint64 &X1, quint64 &X2, const quint64 *s);
void rc5_32_encrypt_8b(const uchar *plain8, uchar *cipher8, const quint32 *s);
void rc5_64_encrypt_16b(const uchar *plain16, uchar *cipher16, const quint64 *s);
void rc5_32_decrypt_8b(const uchar *cipher8, uchar *plain8, const quint32 *s);
void rc5_64_decrypt_16b(const uchar *cipher16, uchar *plain16, const quint64 *s);
#endif
void serpent_encrypt_4w(quint32 &X1, quint32 &X2,
quint32 &X3, quint32 &X4, const quint32 *s);
void serpent_decrypt_4w(quint32 &X1, quint32 &X2,
quint32 &X3, quint32 &X4, const quint32 *s);
void serpent_encrypt_16b(const uchar *plain16, uchar *cipher16, const quint32 *s);
void serpent_decrypt_16b(const uchar *cipher16, uchar *plain16, const quint32 *s);
#ifdef WITH_SERPENT_PRINT_SBOX_H
void serpent_print_sbox_h();
#endif
} // namespace
#endif // SIMPLEQTCRYPTOR_H