-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_test.cpp
305 lines (268 loc) · 6.85 KB
/
run_test.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
#include <iomanip>
#include <iostream>
#include <memory>
#include <sstream>
#include "cipher.hpp"
#include "crypt.hpp"
#include "hash.hpp"
#include "hmac.hpp"
#include "luks.hpp"
char *prog;
namespace test {
using namespace fluks;
uint8_t
dehex(char c)
{
if ('0' <= c && c <= '9')
return c - '0';
if ('A' <= c && c <= 'F')
return 10 + c - 'A';
if ('a' <= c && c <= 'f')
return 10 + c - 'a';
Assert(0, std::string("bad hex character: ") + c);
return 0;
}
void
dehex(const std::string &hex, uint8_t *buf)
{
char byte[2];
for (size_t i = 0; i < hex.size(); i++) {
byte[i&1] = hex[i];
if (i & 1) {
*buf++ = dehex(byte[0]) << 4 | dehex(byte[1]);
}
}
}
std::string
hex(const uint8_t *buf, size_t sz)
{
std::ostringstream out;
out << std::hex << std::setfill('0');
for (size_t i = 0; i < sz; i++) {
if (i && i % 32 == 0)
out << '\n';
out << std::setw(2) << (short)buf[i];
}
return out.str();
}
class Test {
public:
virtual void run() = 0;
};
class Cipher_test : public Test {
public:
Cipher_test(cipher_type, const uint8_t *key, size_t sz_key,
const uint8_t *block, size_t sz_blk, crypt_direction);
void run();
private:
cipher_type _type;
crypt_direction _dir;
std::unique_ptr<uint8_t> _key;
std::unique_ptr<uint8_t> _block;
size_t _sz_key;
};
Cipher_test::Cipher_test(cipher_type type, const uint8_t *key, size_t sz_key,
const uint8_t *block, size_t sz_blk, crypt_direction dir) :
_type(type),
_dir(dir),
_key(new uint8_t[sz_key]),
_block(new uint8_t[Cipher_traits::traits(type)->block_size]),
_sz_key(sz_key)
{
Assert(Cipher_traits::traits(type)->block_size == sz_blk,
"Cipher_test block size wrong");
std::copy(key, key + sz_key, _key.get());
std::copy(block, block + sz_blk, _block.get());
}
void
Cipher_test::run()
{
std::shared_ptr<Cipher> cipher = Cipher::create(_type);
uint8_t buf[cipher->traits()->block_size];
cipher->init(_key.get(), _sz_key);
if (_dir == DIR_ENCRYPT)
cipher->encrypt(_block.get(), buf);
else
cipher->decrypt(_block.get(), buf);
std::cout
// << "KEY=" << hex(_key.get(), _sz_key) << '\n'
// << (_dir == DIR_ENCRYPT ? 'P' : 'C') << "T="
// << hex(_block.get(), cipher->traits()->block_size) << '\n'
// << (_dir == DIR_ENCRYPT ? 'C' : 'P') << "T="
<< hex(buf, cipher->traits()->block_size) << '\n';
}
class Hash_test : public Test {
public:
Hash_test(hash_type type, const uint8_t *data, size_t sz);
void run();
private:
hash_type _type;
std::unique_ptr<uint8_t> _data;
size_t _sz;
};
Hash_test::Hash_test(hash_type type, const uint8_t *data, size_t sz) :
_type(type),
_data(new uint8_t[sz]),
_sz(sz)
{
std::copy(data, data + sz, _data.get());
}
void
Hash_test::run()
{
std::shared_ptr<Hash_function> hash = Hash_function::create(_type);
uint8_t buf[hash->traits()->digest_size];
hash->init();
hash->add(_data.get(), _sz);
hash->end(buf);
std::cout
// << "DATA=" << hex(_data.get(), _sz) << '\n'
// << "DIGEST="
<< hex(buf, hash->traits()->digest_size) << '\n';
}
class Hmac_test : public Test {
public:
Hmac_test(hash_type type, const uint8_t *key, size_t sz_key,
const uint8_t *data, size_t sz_data);
void run();
private:
hash_type _type;
std::unique_ptr<uint8_t> _key;
std::unique_ptr<uint8_t> _data;
size_t _sz_key;
size_t _sz_data;
};
Hmac_test::Hmac_test(hash_type type, const uint8_t *key, size_t sz_key,
const uint8_t *data, size_t sz_data) :
_type(type),
_key(new uint8_t[sz_key]),
_data(new uint8_t[sz_data]),
_sz_key(sz_key),
_sz_data(sz_data)
{
std::copy(key, key + sz_key, _key.get());
std::copy(data, data + sz_data, _data.get());
}
void
Hmac_test::run()
{
std::shared_ptr<Hmac_function> hmac = Hmac_function::create(_type);
uint8_t buf[hmac->traits()->digest_size];
hmac->init(_key.get(), _sz_key);
hmac->add(_data.get(), _sz_data);
hmac->end(buf);
std::cout << "KEY=" << hex(_key.get(), _sz_key)
<< "\nDATA=" << hex(_data.get(), _sz_data)
<< "\nDIGEST=" << hex(buf, hmac->traits()->digest_size) << '\n';
}
} // end test namespace
void
usage()
{
std::cout
<< "usage: " << prog
<< " cipher TYPE (encrypt | decrypt) KEY DATA\n"
<< " " << prog << " hash TYPE DATA\n"
<< " " << prog << " hmac TYPE KEY DATA\n\n"
<< "TYPE: name of the cipher/hash function\n"
<< "KEY: key in hex\n"
<< "DATA: data in hex; can also be plain text if preceded\n"
<< " immediately by a '-' character\n"
<< "Result is the ciphertext/plaintext, hash digest,\n"
<< " or hmac digest\n";
}
int
main(int argc, char **argv)
{
using namespace fluks;
using namespace test;
std::unique_ptr<Test> test;
prog = *argv;
if (argc < 2) {
usage();
return 1;
}
std::string type = argv[1];
if (type == "cipher") {
if (argc != 6) {
usage();
return 1;
}
std::string cipher = argv[2];
std::string dir = argv[3];
std::string key = argv[4];
std::string data = argv[5];
cipher_type cipher_ = Cipher_traits::type(cipher);
Assert(cipher_ != CT_UNDEFINED, "undefined cipher: " + cipher);
crypt_direction dir_;
if (dir == "encrypt")
dir_ = crypt_direction::ENCRYPT;
else if (dir == "decrypt")
dir_ = crypt_direction::DECRYPT;
else {
Assert(0, "undefined crypt direction: " + dir);
return 1; // won't return
}
uint8_t keybuf[key.size()/2];
dehex(key, keybuf);
uint8_t databuf[data.size()];
size_t datasz;
if (data[0] == '-') {
std::copy(data.begin() + 1, data.end(),
reinterpret_cast<char *>(databuf));
datasz = data.size()-1;
} else {
dehex(data, databuf);
datasz = data.size()/2;
}
test.reset(new Cipher_test(cipher_, keybuf, key.size()/2,
databuf, datasz, dir_));
} else if (type == "hash") {
if (argc != 4) {
usage();
return 1;
}
std::string hash = argv[2];
std::string data = argv[3];
hash_type hash_type = Hash_traits::type(hash);
Assert(hash_type != HT_UNDEFINED, "undefined hash: " + hash);
uint8_t databuf[data.size()];
size_t datasz;
if (data[0] == '-') {
std::copy(data.begin() + 1, data.end(),
reinterpret_cast<char *>(databuf));
datasz = data.size()-1;
} else {
dehex(data, databuf);
datasz = data.size()/2;
}
test.reset(new Hash_test(hash_type, databuf, datasz));
} else if (type == "hmac") {
if (argc != 5) {
usage();
return 1;
}
std::string hash = argv[2];
std::string key = argv[3];
std::string data = argv[4];
hash_type hash_ = Hash_traits::type(hash);
Assert(hash_ != HT_UNDEFINED, "undefined hash: " + hash);
uint8_t keybuf[key.size()/2];
dehex(key, keybuf);
uint8_t databuf[data.size()];
size_t datasz;
if (data[0] == '-') {
std::copy(data.begin() + 1, data.end(),
reinterpret_cast<char *>(databuf));
datasz = data.size()-1;
} else {
dehex(data, databuf);
datasz = data.size()/2;
}
test.reset(new Hmac_test(hash_, keybuf, key.size()/2,
databuf, datasz));
} else
Assert(0, "undefined test type: " + type);
test->run();
return 0;
}