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
49//const int TEST_RUNS_PER_KEY = 5; // encryption test cycles done on each key.
50const int TEST_RUNS_PER_KEY = 1008; // encryption test cycles done on each key.
51
52//const int THREAD_COUNT = 10; // number of threads testing blowfish at once.
53const int THREAD_COUNT = 1; // number of threads testing blowfish at once.
54
55//const int ITERATIONS = 4; // number of test runs in our testing threads.
56const int ITERATIONS = 80; // number of test runs in our testing threads.
57
58const int MAX_STRING = 20000; // largest chunk that we'll try to encrypt.
59
61
62class test_blowfish; // forward.
63
64class blowfish_thread : public ethread
65{
66public:
67 blowfish_thread(test_blowfish &parent) : ethread(), _parent(parent) {}
68
69 void perform_activity(void *ptr);
70 // try out random blowfish keys on randomly chosen chunks of the fodder.
71
72private:
73 test_blowfish &_parent;
74};
75
77
78class test_blowfish : virtual public unit_base, virtual public application_shell
79{
80public:
81 test_blowfish()
82 : _fodder(string_manipulation::make_random_name(MAX_STRING + 1, MAX_STRING + 1)) {}
83 DEFINE_CLASS_NAME("test_blowfish");
84
85 int execute();
86
87private:
88 astring _fodder; // chunks taken from this are encrypted and decrypted.
89 time_stamp _program_start; // the time at which we started executing.
90 thread_cabinet _threads; // manages our testing threads.
91 friend class blowfish_thread; // bad practice, but saves time in test app.
92};
93
94int test_blowfish::execute()
95{
96 FUNCDEF("execute");
97#ifdef DEBUG_BLOWFISH
98 LOG("starting blowfish test...");
99#endif
100 int left = THREAD_COUNT;
101 while (left--) {
102#ifdef DEBUG_BLOWFISH
103 LOG(a_sprintf("blowfish thread %d starting...", left));
104#endif
105 _threads.add_thread(new blowfish_thread(*this), true, NULL_POINTER);
106 }
107
108#ifdef DEBUG_BLOWFISH
109 LOG("started all threads...");
110#endif
111
112 while (_threads.threads()) {
113#ifdef DEBUG_BLOWFISH
114 LOG("periodic debris cleaning.");
115#endif
116 _threads.clean_debris();
118 }
119
120 int duration = int(time_stamp().value() - _program_start.value());
121 LOG(a_sprintf("duration for %d keys and encrypt/decrypt=%d ms,",
123 LOG(a_sprintf("that comes to %d ms per cycle.", int(double(duration
125
126 return final_report();
127}
128
130
131#undef UNIT_BASE_THIS_OBJECT
132#define UNIT_BASE_THIS_OBJECT (*dynamic_cast<unit_base *>(application_shell::single_instance()))
133
134void blowfish_thread::perform_activity(void *)
135{
136 FUNCDEF("perform_activity");
137 int left = ITERATIONS;
138 while (left--) {
139 time_stamp key_start;
140 blowfish_crypto bc(_parent.randomizer().inclusive
143#ifdef DEBUG_BLOWFISH
144 LOG(a_sprintf("%d bit key has:", bc.key_size()));
145 astring dumped_key = byte_formatter::text_dump(bc.get_key());
146 LOG(a_sprintf("%s", dumped_key.s()));
147#endif
148 int key_dur = int(time_stamp().value() - key_start.value());
149#ifdef DEBUG_BLOWFISH
150 LOG(a_sprintf(" key generation took %d ms", key_dur));
151#endif
152
153 for (int i = 0; i < TEST_RUNS_PER_KEY; i++) {
156LOG(a_sprintf("test run %d on this key.", i+1));
157 int string_start = _parent.randomizer().inclusive(0, MAX_STRING - 1);
158 int string_end = _parent.randomizer().inclusive(0, MAX_STRING - 1);
159 flip_increasing(string_start, string_end);
160 astring ranstring = _parent._fodder.substring(string_start, string_end);
161#ifdef DEBUG_BLOWFISH
162// LOG(a_sprintf("encoding %s", ranstring.s()));
163// LOG(a_sprintf("string length encoded: %d", ranstring.length()));
164#endif
165
166 byte_array target;
167 time_stamp test_start;
168 bool worked = bc.encrypt(byte_array(ranstring.length() + 1, (abyte*)ranstring.s()), target);
169 int enc_durat = int(time_stamp().value() - test_start.value());
170 ASSERT_TRUE(worked, "phase 1 should not fail to encrypt the string");
171
172 byte_array recovered;
173 test_start.reset();
174 worked = bc.decrypt(target, recovered);
175 int dec_durat = int(time_stamp().value() - test_start.value());
176 ASSERT_TRUE(worked, "phase 1 should not fail to decrypt the string");
177#ifdef DEBUG_BLOWFISH
178 astring jammer_piece = a_sprintf("--\noriginal has %d chars, recovered has %d chars",
179 ranstring.length(), recovered.length() - 1);
180#endif
181
182 astring teddro = (char *)recovered.observe();
183#ifdef DEBUG_BLOWFISH
184// LOG(a_sprintf("decoded %s", teddro.s()));
185#endif
186
187#ifdef DEBUG_BLOWFISH
188 if (teddro != ranstring) {
189 LOG(a_sprintf("error!\toriginal has %d chars, recovered has %d chars",
190 ranstring.length(), recovered.length() - 1));
191 LOG(a_sprintf("\tencoded %s", ranstring.s()));
192 LOG(a_sprintf("\tdecoded %s", teddro.s()));
193 }
194#endif
195 ASSERT_EQUAL(teddro, ranstring, "should not fail to regenerate the original string");
196
197#ifdef DEBUG_BLOWFISH
198 LOG(a_sprintf("%s\nencrypt %d ms, decrypt %d ms, data %d bytes",
199 jammer_piece.s(), enc_durat, dec_durat, string_end - string_start + 1));
200#endif
201 time_control::sleep_ms(0); // take a rest.
202 }
203 time_control::sleep_ms(0); // take a rest.
204 }
205}
206
207HOOPLE_MAIN(test_blowfish, )
208
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
Provides BlowFish encryption on byte_arrays using the OpenSSL package.
static int minimum_key_size()
returns the minimum key size (in bits) supported here.
bool decrypt(const basis::byte_array &source, basis::byte_array &target) const
decrypts the "target" array from the encrypted "source" array.
bool encrypt(const basis::byte_array &source, basis::byte_array &target) const
encrypts the "source" array into the "target" array.
static int maximum_key_size()
returns the maximum key size (in bits) supported here.
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