DEFINE_CLASS_NAME("borked_blowfish_crypto");
// blowfish relevant values for appropriate key sizes.
- virtual int minimum_key_size() const { return 64; } // officially, this is 32.
- virtual int maximum_key_size() const { return 448; }
+ virtual int minimum_key_size_in_bits() const { return 64; } // officially, this is 32.
+ virtual int maximum_key_size_in_bits() const { return 448; }
};
} //namespace.
// this macro checks on the validity of the key sizes (in bits).
#define DISCUSS_KEY_SIZE(key_size) \
- if (key_size < minimum_key_size()) { \
+ if (key_size < minimum_key_size_in_bits()) { \
ERROR_BAILOUT(static_class_name(), func, \
a_sprintf("key size (%d bits) is less than minimum key size %d.", \
- key_size, minimum_key_size())); \
+ key_size, minimum_key_size_in_bits())); \
return false; \
} \
- if (key_size > maximum_key_size()) { \
+ if (key_size > maximum_key_size_in_bits()) { \
ERROR_BAILOUT(static_class_name(), func, \
a_sprintf("key size (%d bits) is greater than maximum key size %d.", \
- key_size, maximum_key_size())); \
+ key_size, maximum_key_size_in_bits())); \
return false; \
}
{
FUNCDEF("set_key(int)");
DISCUSS_KEY_SIZE(key_size);
-/// if (key_size < minimum_key_size())
-/// _key_size = minimum_key_size();
-/// if (key_size > maximum_key_size())
-/// _key_size = maximum_key_size();
LOG("prior to generate key");
_key_size = key_size;
-/// WHACK(_key); // clear any existing key.
-/// _key = new byte_array();
bool to_return = generate_key(_key_size, *_key);
LOG("after generate key");
return to_return;
}
-/*
-bool cryptical_envelopment::set_key(const byte_array &key, int key_size)
-{
- FUNCDEF("set_key(byte_array,int)");
-//// static_ssl_initializer();
- DISCUSS_KEY_SIZE(key_size);
- DISCUSS_PROVIDED_KEY(key_size, key);
- _key_size = key_size;
- _key = new byte_array(key);
- return true;
-}
-*/
-
int cryptical_envelopment::key_size() const { return _key_size; }
const byte_array &cryptical_envelopment::get_key() const { return *_key; }
bool cryptical_envelopment::set_key(const byte_array &new_key, int key_size)
{
FUNCDEF("set_key(byte_array,int)");
-// if (!new_key.length()) return false;
DISCUSS_KEY_SIZE(key_size);
DISCUSS_PROVIDED_KEY(key_size, new_key);
-// if ( (key_size < minimum_key_size()) || (key_size > maximum_key_size()) )
-// return false;
-// if (new_key.length() * BITS_PER_BYTE < key_size) return false;
-//// if (!_key)
-//// _key = new byte_array();
_key_size = key_size;
*_key = new_key;
return true;
{
FUNCDEF("generate_key");
static_ssl_initializer();
-//// if (!_key)
-//// _key = new byte_array();
DISCUSS_KEY_SIZE(size);
-// if (size < minimum_key_size())
-// size = minimum_key_size();
-// else if (size > maximum_key_size())
-// size = maximum_key_size();
int bytes = size / BITS_PER_BYTE; // calculate the number of bytes needed.
if (size % BITS_PER_BYTE) bytes++; // add one for non-integral portion.
new_key.reset(bytes);
int key_size() const; // returns the size of our key, in bits.
// derived classes must provide the min and max for key sizes.
- virtual int minimum_key_size() const = 0;
- //!< returns the minimum key size (in bits) supported here.
- virtual int maximum_key_size() const = 0;
- //!< returns the maximum key size (in bits) supported here.
+ virtual int minimum_key_size_in_bits() const = 0;
+ //!< returns the minimum key size in bits supported here.
+ virtual int maximum_key_size_in_bits() const = 0;
+ //!< returns the maximum key size in bits supported here.
const basis::byte_array &get_key() const; //!< returns our current key.
DEFINE_CLASS_NAME("twofish_crypto");
// twofish relevant values for appropriate key sizes.
- virtual int minimum_key_size() const { return 92; }
+ virtual int minimum_key_size_in_bits() const { return 92; }
/*
note that the lower bound above was discovered for openssl library through experimentation,
and is not officially documented for the twofish algorithm anywhere we can find (yet).
would succeed on the same key length but then would trigger an 'invalid key length' error.
not the kind of deterministic behavior we might expect.
*/
- virtual int maximum_key_size() const { return 256; }
+ virtual int maximum_key_size_in_bits() const { return 256; }
};
} //namespace.
* Author : Chris Koeritz
* Purpose: Exercises the BlowFish encryption methods in the crypto library.
**
-* Copyright (c) 2005-$now By Author. This program is free software; you can *
-* redistribute it and/or modify it under the terms of the GNU General Public *
-* License as published by the Free Software Foundation; either version 2 of *
-* the License or (at your option) any later version. This is online at: *
-* http://www.fsf.org/copyleft/gpl.html *
-* Please send any updates to: fred@gruntose.com *
+* Copyright (c) 2005-$now By Author. This program is free software; you can
+* redistribute it and/or modify it under the terms of the GNU General Public
+* License as published by the Free Software Foundation; either version 2 of
+* the License or (at your option) any later version. This is online at:
+* http://www.fsf.org/copyleft/gpl.html
+* Please send any updates to: fred@gruntose.com
*/
#include <application/hoople_main.h>
const int MAX_STRING = 20000; // largest chunk that we'll try to encrypt.
// some constants snagged from older version of borked_blowfish_crypto class...
-const int borked_blowfish_crypto_minimum_key_size_in_bits = 32;
-const int borked_blowfish_crypto_maximum_key_size_in_bits = 448;
+///const int borked_blowfish_crypto_minimum_key_size_in_bits = 32;
+///const int borked_blowfish_crypto_maximum_key_size_in_bits = 448;
//////////////
int left = ITERATIONS;
while (left--) {
time_stamp key_start;
- borked_blowfish_crypto bc(_parent.randomizer().inclusive(borked_blowfish_crypto_minimum_key_size_in_bits,
- borked_blowfish_crypto_maximum_key_size_in_bits));
+ borked_blowfish_crypto bc(256);
+ bc = borked_blowfish_crypto(_parent.randomizer().inclusive(bc.minimum_key_size_in_bits(), bc.maximum_key_size_in_bits()));
#ifdef DEBUG_BLOWFISH
LOG(a_sprintf("%d bit key has:", bc.key_size()));
astring dumped_key = byte_formatter::text_dump(bc.get_key());
// some constants snagged from older version of twofish_crypto class...
//const int twofish_crypto_minimum_key_size_in_bits = 92;
//const int twofish_crypto_maximum_key_size_in_bits = 256;
-/*
- Q: is twofish truly requiring 128/192/256 key sizes only?
- A: no, schneier says any key size up to 256 bits.
- Q: can twofish really support *any* key length up to 256?
- A: well, not in openssl. we are getting invalid key length complaints
- with keys below 92 bits. this was calculated by testing for quite
- a while with only those size keys, and 92 seems reliable as a minimum.
-*/
//////////////
int left = ITERATIONS;
while (left--) {
time_stamp key_start;
- twofish_crypto bc(_parent.randomizer().inclusive(twofish_crypto_minimum_key_size_in_bits,
- twofish_crypto_maximum_key_size_in_bits));
+ twofish_crypto tc(256);
+ tc = twofish_crypto(_parent.randomizer().inclusive(tc.minimum_key_size_in_bits(), tc.maximum_key_size_in_bits()));
#ifdef DEBUG_TWOFISH
- LOG(a_sprintf("%d bit key has:", bc.key_size()));
- astring dumped_key = byte_formatter::text_dump(bc.get_key());
+ LOG(a_sprintf("%d bit key has:", tc.key_size()));
+ astring dumped_key = byte_formatter::text_dump(tc.get_key());
LOG(a_sprintf("%s", dumped_key.s()));
#endif
int key_dur = int(time_stamp().value() - key_start.value());
byte_array target;
time_stamp test_start;
- bool worked = bc.encrypt(byte_array(ranstring.length() + 1, (abyte*)ranstring.s()), target);
+ bool worked = tc.encrypt(byte_array(ranstring.length() + 1, (abyte*)ranstring.s()), target);
int enc_durat = int(time_stamp().value() - test_start.value());
ASSERT_TRUE(worked, "phase 1 should not fail to encrypt the string");
byte_array recovered;
test_start.reset();
- worked = bc.decrypt(target, recovered);
+ worked = tc.decrypt(target, recovered);
int dec_durat = int(time_stamp().value() - test_start.value());
ASSERT_TRUE(worked, "phase 1 should not fail to decrypt the string");
#ifdef DEBUG_TWOFISH