feisty meow concerns codebase 2.140
test_blowfish_crypto.cpp
Go to the documentation of this file.
1/*
2* Name : test blowfish encryption
3* Author : Chris Koeritz
4* Purpose: Exercises the BlowFish encryption methods in the crypto library.
5**
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
12*/
13
15#include <basis/byte_array.h>
16#include <basis/astring.h>
18#include <mathematics/chaos.h>
19#include <processes/ethread.h>
25#include <timely/time_control.h>
26#include <timely/time_stamp.h>
27#include <unit_test/unit_base.h>
28
29#include <stdio.h>
30#include <string.h>
31
32using namespace application;
33using namespace basis;
34using namespace crypto;
35using namespace filesystem;
36using namespace loggers;
37using namespace mathematics;
38using namespace processes;
39using namespace structures;
40using namespace textual;
41using namespace timely;
42using namespace unit_test;
43
44#define LOG(to_print) EMERGENCY_LOG(program_wide_logger::get(), astring(to_print))
45
46#define DEBUG_BLOWFISH
47 // uncomment for noisier run.
48
49const int TEST_RUNS_PER_KEY = 5; // normal encryption test cycles done on each key.
50//const int TEST_RUNS_PER_KEY = 1008; // LARGE encryption test cycles done on each key.
51
52const int THREAD_COUNT = 10; // number of threads testing blowfish at once.
53
54const int ITERATIONS = 4; // normal number of test runs in our testing threads.
55//const int ITERATIONS = 80; // LARGE number of test runs in our testing threads.
56
57const int MAX_STRING = 20000; // largest chunk that we'll try to encrypt.
58
60
61class test_blowfish; // forward.
62
63class blowfish_thread : public ethread
64{
65public:
66 blowfish_thread(test_blowfish &parent) : ethread(), _parent(parent) {}
67
68 void perform_activity(void *ptr);
69 // try out random blowfish keys on randomly chosen chunks of the fodder.
70
71private:
72 test_blowfish &_parent;
73};
74
76
77class test_blowfish : virtual public unit_base, virtual public application_shell
78{
79public:
80 test_blowfish()
81 : _fodder(string_manipulation::make_random_name(MAX_STRING + 1, MAX_STRING + 1)) {}
82 DEFINE_CLASS_NAME("test_blowfish");
83
84 int execute();
85
86private:
87 astring _fodder; // chunks taken from this are encrypted and decrypted.
88 time_stamp _program_start; // the time at which we started executing.
89 thread_cabinet _threads; // manages our testing threads.
90 friend class blowfish_thread; // bad practice, but saves time in test app.
91};
92
93int test_blowfish::execute()
94{
95 FUNCDEF("execute");
96#ifdef DEBUG_BLOWFISH
97 LOG("starting blowfish test...");
98#endif
99
100 /*
101 a known hack--we have bad failures in this test, thanks to openssl as far
102 as we can tell. so we signal not to fail in the test results.
103 */
104 set_failures_are_critial(false);
105
106 int left = THREAD_COUNT;
107 while (left--) {
108#ifdef DEBUG_BLOWFISH
109 LOG(a_sprintf("blowfish thread %d starting...", left));
110#endif
111 _threads.add_thread(new blowfish_thread(*this), true, NULL_POINTER);
112 }
113
114#ifdef DEBUG_BLOWFISH
115 LOG("started all threads...");
116#endif
117
118 while (_threads.threads()) {
119#ifdef DEBUG_BLOWFISH
120 LOG("periodic debris cleaning.");
121#endif
122 _threads.clean_debris();
124 }
125
126 int duration = int(time_stamp().value() - _program_start.value());
127 LOG(a_sprintf("duration for %d keys and encrypt/decrypt=%d ms,",
129 LOG(a_sprintf("that comes to %d ms per cycle.", int(double(duration
131
132 return final_report();
133}
134
136
137#undef UNIT_BASE_THIS_OBJECT
138#define UNIT_BASE_THIS_OBJECT (*dynamic_cast<unit_base *>(application_shell::single_instance()))
139
140void blowfish_thread::perform_activity(void *)
141{
142 FUNCDEF("perform_activity");
143 int left = ITERATIONS;
144 while (left--) {
145 time_stamp key_start;
148#ifdef DEBUG_BLOWFISH
149 LOG(a_sprintf("%d bit key has:", bc.key_size()));
150 astring dumped_key = byte_formatter::text_dump(bc.get_key());
151 LOG(a_sprintf("%s", dumped_key.s()));
152#endif
153 int key_dur = int(time_stamp().value() - key_start.value());
154#ifdef DEBUG_BLOWFISH
155 LOG(a_sprintf(" key generation took %d ms", key_dur));
156#endif
157
158 for (int i = 0; i < TEST_RUNS_PER_KEY; i++) {
161LOG(a_sprintf("test run %d on this key.", i+1));
162 int string_start = _parent.randomizer().inclusive(0, MAX_STRING - 1);
163 int string_end = _parent.randomizer().inclusive(0, MAX_STRING - 1);
164 flip_increasing(string_start, string_end);
165 astring ranstring = _parent._fodder.substring(string_start, string_end);
166#ifdef DEBUG_BLOWFISH
167// LOG(a_sprintf("encoding %s", ranstring.s()));
168// LOG(a_sprintf("string length encoded: %d", ranstring.length()));
169#endif
170
171 byte_array target;
172 time_stamp test_start;
173 bool worked = bc.encrypt(byte_array(ranstring.length() + 1, (abyte*)ranstring.s()), target);
174 int enc_durat = int(time_stamp().value() - test_start.value());
175 ASSERT_TRUE(worked, "phase 1 should not fail to encrypt the string");
176
177 byte_array recovered;
178 test_start.reset();
179 worked = bc.decrypt(target, recovered);
180 int dec_durat = int(time_stamp().value() - test_start.value());
181 ASSERT_TRUE(worked, "phase 1 should not fail to decrypt the string");
182#ifdef DEBUG_BLOWFISH
183 astring jammer_piece = a_sprintf("--\noriginal has %d chars, recovered has %d chars",
184 ranstring.length(), recovered.length() - 1);
185#endif
186
187 astring teddro = (char *)recovered.observe();
188#ifdef DEBUG_BLOWFISH
189// LOG(a_sprintf("decoded %s", teddro.s()));
190#endif
191
192#ifdef DEBUG_BLOWFISH
193 if (teddro != ranstring) {
194 LOG(a_sprintf("error!\toriginal has %d chars, recovered has %d chars",
195 ranstring.length(), recovered.length() - 1));
196 LOG(a_sprintf("\tencoded %s", ranstring.s()));
197 LOG(a_sprintf("\tdecoded %s", teddro.s()));
198 }
199#endif
200 ASSERT_EQUAL(teddro, ranstring, "should not fail to regenerate the original string");
201
202#ifdef DEBUG_BLOWFISH
203 LOG(a_sprintf("%s\nencrypt %d ms, decrypt %d ms, data %d bytes",
204 jammer_piece.s(), enc_durat, dec_durat, string_end - string_start + 1));
205#endif
206 time_control::sleep_ms(0); // take a rest.
207 }
208 time_control::sleep_ms(0); // take a rest.
209 }
210}
211
212HOOPLE_MAIN(test_blowfish, )
213
The application_shell is a base object for console programs.
virtual int execute()=0
< retrieves the command line from the /proc hierarchy on linux.
a_sprintf is a specialization of astring that provides printf style support.
Definition astring.h:440
const contents * observe() const
Returns a pointer to the underlying C array of data.
Definition array.h:172
int length() const
Returns the current reported length of the allocated C array.
Definition array.h:115
Provides a dynamically resizable ASCII character string.
Definition astring.h:35
const char * s() const
synonym for observe. the 's' stands for "string", if that helps.
Definition astring.h:113
bool substring(astring &target, int start, int end) const
a version that stores the substring in an existing "target" string.
Definition astring.cpp:868
int length() const
Returns the current length of the string.
Definition astring.cpp:132
A very common template for a dynamic array of bytes.
Definition byte_array.h:36
bool encrypt(const basis::byte_array &source, basis::byte_array &target) const
encrypts the "source" array into the "target" array.
bool decrypt(const basis::byte_array &source, basis::byte_array &target) const
decrypts the "target" array from the encrypted "source" array.
const basis::byte_array & get_key() const
returns our current key.
Provides a platform-independent object for adding threads to a program.
Definition ethread.h:36
virtual void perform_activity(void *thread_data)=0
< invoked just after after start(), when the OS thread is created.
Manages a collection of threads.
static void text_dump(basis::astring &output, const basis::abyte *location, basis::un_int length, basis::un_int label=0, const char *eol="\n")
prints out a block of memory in a human readable form.
Provides various functions for massaging strings.
static void sleep_ms(basis::un_int msec)
a system independent name for a forced snooze measured in milliseconds.
Represents a point in time relative to the operating system startup time.
Definition time_stamp.h:38
void reset()
sets the stamp time back to now.
time_representation value() const
returns the time_stamp in terms of the lower level type.
Definition time_stamp.h:61
#define NULL_POINTER
The value representing a pointer to nothing.
Definition definitions.h:32
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition enhance_cpp.h:42
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition enhance_cpp.h:54
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition hoople_main.h:61
Implements an application lock to ensure only one is running at once.
The guards collection helps in testing preconditions and reporting errors.
Definition array.h:30
unsigned char abyte
A fairly important unit which is seldom defined...
Definition definitions.h:51
void flip_increasing(type &a, type &b)
Makes sure that two values are in increasing order (a < b).
Definition functions.h:95
A platform independent way to obtain the timestamp of a file.
A logger that sends to the console screen using the standard output device.
An extension to floating point primitives providing approximate equality.
Definition averager.h:21
A dynamic container class that holds any kind of object via pointers.
Definition amorph.h:55
#include <time.h>
Useful support functions for unit testing, especially within hoople.
Definition unit_base.cpp:35
const int ITERATIONS
const int MAX_STRING
const int THREAD_COUNT
const int TEST_RUNS_PER_KEY
#define LOG(to_print)
#define ASSERT_EQUAL(a, b, test_name)
Definition unit_base.h:38
#define ASSERT_TRUE(a, test_name)
Definition unit_base.h:46