26#include <openssl/blowfish.h>
27#include <openssl/err.h>
28#include <openssl/evp.h>
49#define ALWAYS_LOG(t) CLASS_EMERGENCY_LOG(program_wide_logger::get(), t)
52 #define LOG(t) CLASS_EMERGENCY_LOG(program_wide_logger::get(), t)
59#define GET_SSL_ERROR() \
60 ERR_error_string(ERR_get_error(), NULL_POINTER)
64 #define DISCUSS_KEY_SIZE(key_size) \
65 if (key_size < minimum_key_size()) { \
66 deadly_error(static_class_name(), func, \
67 a_sprintf("key size (%d bits) is less than minimum key size %d.", \
68 key_size, minimum_key_size())); \
70 if (key_size > maximum_key_size()) { \
71 deadly_error(static_class_name(), func, \
72 a_sprintf("key size (%d bits) is greater than maximum key size %d.", \
73 key_size, maximum_key_size())); \
78 #define DISCUSS_PROVIDED_KEY(key_size, key) \
79 if (key.length() * BITS_PER_BYTE < key_size) { \
80 deadly_error(static_class_name(), func, \
81 a_sprintf("key array length (%d) is less than required by key size " \
82 "(%d bits).", key.length(), key_size)); \
85 #define DISCUSS_PROVIDED_KEY(key_size, key)
86 #define DISCUSS_KEY_SIZE(key_size)
95 LOG(
"prior to key size discuss");
101 LOG(
"prior to generate key");
103 LOG(
"after generate key");
107: _key_size(key_size),
110 FUNCDEF(
"ctor(byte_array,int)");
114 LOG(
"prior to key size discuss");
116 LOG(
"prior to provided key discuss");
118 LOG(
"prior to ssl static init");
119 LOG(
"after ssl static init");
124 _key_size(to_copy._key_size),
129 LOG(
"after ssl static init");
135 LOG(
"prior to key whack");
137 LOG(
"after key whack");
150 if (
this == &to_copy)
return *
this;
151 _key_size = to_copy._key_size;
152 *_key = *to_copy._key;
159 if (!new_key.
length())
return false;
181 new_key.
reset(bytes);
182 for (
int i = 0; i < bytes; i++)
193 static byte_array to_return(EVP_MAX_IV_LENGTH);
194 static bool initted =
false;
196 LOG(
"actually doing init");
197 for (
int i = 0; i < to_return.
length(); i++)
198 to_return[i] =
abyte(214 - i);
200 LOG(
"finished init process");
212 bool to_return =
true;
217 EVP_CIPHER_CTX *session = EVP_CIPHER_CTX_new();
218 EVP_CIPHER_CTX_init(session);
231 LOG(
a_sprintf(
" calling set key len with key size of %d", _key_size));
234 EVP_CIPHER_CTX_set_key_length(session, _key_size);
235 EVP_CIPHER_CTX_ctrl(session, EVP_CTRL_AEAD_SET_IVLEN,
init_vector().length(), NULL);
249 int enc_ret = EVP_EncryptUpdate(session, encoded.
access(), &encoded_len,
257 LOG(
a_sprintf(
" chopping extra bytes %d to %d.", encoded_len, encoded.
last()));
258 encoded.
zap(encoded_len, encoded.
last());
267 enc_ret = EVP_EncryptFinal_ex(session, encoded.
access(), &pad_len);
270 "failed, result=%d with error=%s.", enc_ret,
GET_SSL_ERROR()));
274 encoded.
zap(pad_len, encoded.
last());
279 EVP_CIPHER_CTX_cleanup(session);
280 EVP_CIPHER_CTX_free(session);
292 bool to_return =
true;
293 EVP_CIPHER_CTX *session = EVP_CIPHER_CTX_new();
294 EVP_CIPHER_CTX_init(session);
295 LOG(
a_sprintf(
" using key size with %d bits.", _key_size));
306 EVP_CIPHER_CTX_set_key_length(session, _key_size);
307 EVP_CIPHER_CTX_ctrl(session, EVP_CTRL_AEAD_SET_IVLEN,
init_vector().length(), NULL);
319 int dec_ret = EVP_DecryptUpdate(session, decoded.
access(), &decoded_len,
325 LOG(
a_sprintf(
" first part decrypted size in bytes is %d.", decoded_len));
326 decoded.
zap(decoded_len, decoded.
last());
334 dec_ret = EVP_DecryptFinal_ex(session, decoded.
access(), &pad_len);
337 "failed, result=%d, padlen=%d, target had %d bytes, error=%s.", dec_ret,
342 decoded.
zap(pad_len, decoded.
last());
347 EVP_CIPHER_CTX_cleanup(session);
348 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.
contents * access()
A non-constant access of the underlying C-array. BE REALLY CAREFUL.
const contents * observe() const
Returns a pointer to the underlying C array of data.
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 deadly_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.
unsigned char abyte
A fairly important unit which is seldom defined...
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.