26 #include <openssl/blowfish.h>
27 #include <openssl/evp.h>
29 using namespace basis;
49 #define LOG(t) CLASS_EMERGENCY_LOG(program_wide_logger::get(), t)
57 #define DISCUSS_KEY_SIZE(key_size) \
58 if (key_size < minimum_key_size()) { \
59 continuable_error(static_class_name(), func, \
60 a_sprintf("key size (%d bits) is less than minimum key size %d.", \
61 key_size, minimum_key_size())); \
63 if (key_size > maximum_key_size()) { \
64 continuable_error(static_class_name(), func, \
65 a_sprintf("key size (%d bits) is greater than maximum key size %d.", \
66 key_size, maximum_key_size())); \
71 #define DISCUSS_PROVIDED_KEY(key_size, key) \
72 if (key.length() * BITS_PER_BYTE < key_size) { \
73 continuable_error(static_class_name(), func, \
74 a_sprintf("key array length (%d) is less than required by key size " \
75 "(%d bits).", key.length(), key_size)); \
78 #define DISCUSS_PROVIDED_KEY(key_size, key)
79 #define DISCUSS_KEY_SIZE(key_size)
82 blowfish_crypto::blowfish_crypto(
int key_size)
83 : _key_size(key_size),
88 LOG(
"prior to key size discuss");
94 LOG(
"prior to generate key");
96 LOG(
"after generate key");
100 : _key_size(key_size),
103 FUNCDEF(
"ctor(byte_array,int)");
106 LOG(
"prior to key size discuss");
108 LOG(
"prior to provided key discuss");
110 LOG(
"prior to ssl static init");
112 LOG(
"after ssl static init");
117 _key_size(to_copy._key_size),
122 LOG(
"after ssl static init");
128 LOG(
"prior to key whack");
130 LOG(
"after key whack");
143 if (
this == &to_copy)
return *
this;
144 _key_size = to_copy._key_size;
145 *_key = *to_copy._key;
152 if (!new_key.
length())
return false;
173 new_key.
reset(bytes);
174 for (
int i = 0; i < bytes; i++)
184 static byte_array to_return(EVP_MAX_IV_LENGTH);
185 static bool initted =
false;
186 LOG(
"prior to initted check");
188 LOG(
"actually doing init");
189 for (
int i = 0; i < EVP_MAX_IV_LENGTH; i++)
190 to_return[i] = 214 - i;
193 LOG(
"leaving init check");
203 bool to_return =
true;
206 EVP_CIPHER_CTX *session = EVP_CIPHER_CTX_new();
208 EVP_CIPHER_CTX_init(session);
211 EVP_CIPHER_CTX_set_key_length(session, _key_size);
218 int enc_ret = EVP_EncryptUpdate(session, encoded.
access(), &encoded_len,
222 "result=%d.", enc_ret));
227 encoded.
zap(encoded_len, encoded.
last());
236 enc_ret = EVP_EncryptFinal_ex(session, encoded.
access(), &pad_len);
239 "failed, result=%d.", enc_ret));
243 encoded.
zap(pad_len, encoded.
last());
248 EVP_CIPHER_CTX_cleanup(session);
249 EVP_CIPHER_CTX_free(session);
259 bool to_return =
true;
260 EVP_CIPHER_CTX *session = EVP_CIPHER_CTX_new();
261 EVP_CIPHER_CTX_init(session);
265 EVP_CIPHER_CTX_set_key_length(session, _key_size);
271 int dec_ret = EVP_DecryptUpdate(session, decoded.
access(), &decoded_len,
277 LOG(
a_sprintf(
" decrypted size in bytes is %d.\n", decoded_len));
278 decoded.
zap(decoded_len, decoded.
last());
286 dec_ret = EVP_DecryptFinal_ex(session, decoded.
access(), &pad_len);
290 "failed, result=%d, padlen=%d, target had %d bytes.", dec_ret,
291 pad_len, target.
length()));
294 int dec_size = pad_len;
295 decoded.
zap(dec_size, decoded.
last());
300 EVP_CIPHER_CTX_cleanup(session);
301 EVP_CIPHER_CTX_free(session);
#define DISCUSS_KEY_SIZE(key_size)
#define DISCUSS_PROVIDED_KEY(key_size, key)
a_sprintf is a specialization of astring that provides printf style support.
void reset(int number=0, const contents *initial_contents=NULL_POINTER)
Resizes this array and sets the contents from an array of contents.
const contents * observe() const
Returns a pointer to the underlying C array of data.
contents * access()
A non-constant access of the underlying C-array. BE REALLY CAREFUL.
int length() const
Returns the current reported length of the allocated C array.
outcome zap(int start, int end)
Deletes from "this" the objects inclusively between "start" and "end".
int last() const
Returns the last valid element in the array.
auto_synchronizer simplifies concurrent code by automatically unlocking.
A very common template for a dynamic array of bytes.
Provides BlowFish encryption on byte_arrays using the OpenSSL package.
blowfish_crypto(int key_size)
this will create a new random key of the "key_size", in bits.
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.
static const basis::byte_array & init_vector()
returns the initialization vector that is used by this class.
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.
blowfish_crypto & operator=(const blowfish_crypto &to_copy)
static void generate_key(int size, basis::byte_array &new_key)
creates a "new_key" of the "size" (in bits) specified.
virtual ~blowfish_crypto()
bool set_key(const basis::byte_array &new_key, int key_size)
sets the encryption key to "new_key".
const basis::byte_array & get_key() const
returns our current key.
const mathematics::chaos & randomizer() const
provides a random number generator for any encryption routines.
int inclusive(int low, int high) const
< Returns a pseudo-random number r, such that "low" <= r <= "high".
#define continuable_error(c, f, i)
#define BITS_PER_BYTE
A fundamental constant measuring the number of bits in a byte.
#define NULL_POINTER
The value representing a pointer to nothing.
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
The guards collection helps in testing preconditions and reporting errors.
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
const ssl_init & static_ssl_initializer()
the main method for accessing the SSL initialization support.
A logger that sends to the console screen using the standard output device.
An extension to floating point primitives providing approximate equality.
A dynamic container class that holds any kind of object via pointers.
#define SAFE_STATIC(type, func_name, parms)
Statically defines a singleton object whose scope is the program's lifetime.