2 * Name : test blowfish encryption
3 * Author : Chris Koeritz
4 * Purpose: Exercises the BlowFish encryption methods in the crypto library.
6 * Copyright (c) 2005-$now By Author. This program is free software; you can *
7 * redistribute it and/or modify it under the terms of the GNU General Public *
8 * License as published by the Free Software Foundation; either version 2 of *
9 * the License or (at your option) any later version. This is online at: *
10 * http://www.fsf.org/copyleft/gpl.html *
11 * Please send any updates to: fred@gruntose.com *
14 #include <application/hoople_main.h>
15 #include <basis/byte_array.h>
16 #include <basis/astring.h>
17 #include <crypto/blowfish_crypto.h>
18 #include <mathematics/chaos.h>
19 #include <processes/ethread.h>
20 #include <processes/thread_cabinet.h>
21 #include <structures/static_memory_gremlin.h>
22 #include <structures/unique_id.h>
23 #include <textual/byte_formatter.h>
24 #include <textual/string_manipulation.h>
25 #include <timely/time_control.h>
26 #include <timely/time_stamp.h>
27 #include <unit_test/unit_base.h>
32 using namespace application;
33 using namespace basis;
34 using namespace crypto;
35 using namespace filesystem;
36 using namespace loggers;
37 using namespace mathematics;
38 using namespace processes;
39 using namespace structures;
40 using namespace textual;
41 using namespace timely;
42 using namespace unit_test;
44 #define LOG(to_print) EMERGENCY_LOG(program_wide_logger::get(), to_print)
46 //#define DEBUG_BLOWFISH
47 // uncomment for noisier run.
49 const int TEST_RUNS_PER_KEY = 5; // encryption test cycles done on each key.
51 const int THREAD_COUNT = 10; // number of threads testing blowfish at once.
53 const int ITERATIONS = 4; // number of test runs in our testing threads.
55 const int MAX_STRING = 20000; // largest chunk that we'll try to encrypt.
59 class test_blowfish; // forward.
61 class blowfish_thread : public ethread
64 blowfish_thread(test_blowfish &parent) : ethread(), _parent(parent) {}
66 void perform_activity(void *ptr);
67 // try out random blowfish keys on randomly chosen chunks of the fodder.
70 test_blowfish &_parent;
75 class test_blowfish : virtual public unit_base, virtual public application_shell
79 : _fodder(string_manipulation::make_random_name(MAX_STRING + 1, MAX_STRING + 1)) {}
80 DEFINE_CLASS_NAME("test_blowfish");
85 astring _fodder; // chunks taken from this are encrypted and decrypted.
86 time_stamp _program_start; // the time at which we started executing.
87 thread_cabinet _threads; // manages our testing threads.
88 friend class blowfish_thread; // bad practice, but saves time in test app.
91 int test_blowfish::execute()
94 int left = THREAD_COUNT;
96 _threads.add_thread(new blowfish_thread(*this), true, NIL);
99 while (_threads.threads()) {
100 #ifdef DEBUG_BLOWFISH
101 LOG(astring("cleaning debris."));
103 _threads.clean_debris();
104 time_control::sleep_ms(1000);
107 #ifdef DEBUG_BLOWFISH
108 int duration = int(time_stamp().value() - _program_start.value());
109 LOG(a_sprintf("duration for %d keys and encrypt/decrypt=%d ms,",
110 ITERATIONS * TEST_RUNS_PER_KEY * THREAD_COUNT, duration));
111 LOG(a_sprintf("that comes to %d ms per cycle.\n", int(double(duration
112 / TEST_RUNS_PER_KEY / ITERATIONS / THREAD_COUNT))));
115 return final_report();
120 #undef UNIT_BASE_THIS_OBJECT
121 #define UNIT_BASE_THIS_OBJECT (*dynamic_cast<unit_base *>(application_shell::single_instance()))
123 void blowfish_thread::perform_activity(void *)
125 FUNCDEF("perform_activity");
126 int left = ITERATIONS;
128 time_stamp key_start;
129 blowfish_crypto bc(_parent.randomizer().inclusive
130 (blowfish_crypto::minimum_key_size(),
131 blowfish_crypto::maximum_key_size()));
132 #ifdef DEBUG_BLOWFISH
133 LOG(a_sprintf("%d bit key has:", bc.key_size()));
134 astring dumped_key = byte_formatter::text_dump(bc.get_key());
135 LOG(a_sprintf("%s", dumped_key.s()));
137 int key_dur = int(time_stamp().value() - key_start.value());
139 #ifdef DEBUG_BLOWFISH
140 LOG(a_sprintf(" key generation took %d ms", key_dur));
143 for (int i = 0; i < TEST_RUNS_PER_KEY; i++) {
147 int string_start = _parent.randomizer().inclusive(0, MAX_STRING - 1);
148 int string_end = _parent.randomizer().inclusive(0, MAX_STRING - 1);
149 flip_increasing(string_start, string_end);
150 astring ranstring = _parent._fodder.substring(string_start, string_end);
151 //LOG(a_sprintf("encoding %s\n", ranstring.s());
152 //LOG(a_sprintf("string length encoded: %d\n", ranstring.length());
156 time_stamp test_start;
157 bool worked = bc.encrypt(byte_array(ranstring.length() + 1,
158 (abyte*)ranstring.s()), target);
159 int enc_durat = int(time_stamp().value() - test_start.value());
160 ASSERT_TRUE(worked, "phase 1 should not fail to encrypt the string");
162 byte_array recovered;
164 worked = bc.decrypt(target, recovered);
165 int dec_durat = int(time_stamp().value() - test_start.value());
166 ASSERT_TRUE(worked, "phase 1 should not fail to decrypt the string");
167 // LOG(a_sprintf("original has %d chars, recovered has %d chars\n",
168 // ranstring.length(), recovered.length() - 1));
170 astring teddro = (char *)recovered.observe();
171 //LOG(a_sprintf("decoded %s\n", teddro.s()));
173 #ifdef DEBUG_BLOWFISH
174 if (teddro != ranstring) {
175 LOG(a_sprintf("error!\toriginal has %d chars, recovered has %d chars\n",
176 ranstring.length(), recovered.length() - 1));
177 LOG(a_sprintf("\tencoded %s\n", ranstring.s()));
178 LOG(a_sprintf("\tdecoded %s\n", teddro.s()));
181 ASSERT_EQUAL(teddro, ranstring, "should not fail to regenerate the original string");
183 #ifdef DEBUG_BLOWFISH
184 LOG(a_sprintf(" encrypt %d ms, decrypt %d ms, data %d bytes\n",
185 enc_durat, dec_durat, string_end - string_start + 1));
187 time_control::sleep_ms(0); // take a rest.
189 time_control::sleep_ms(0); // take a rest.
193 HOOPLE_MAIN(test_blowfish, )