7 #include "wvblowfish.h"
9 #include <openssl/rand.h>
10 #include <openssl/blowfish.h>
15 const void *_key,
size_t _keysize) :
16 mode(_mode), key(NULL), bfkey(NULL)
22 WvBlowfishEncoder::~WvBlowfishEncoder()
40 key =
new unsigned char[keysize];
41 memcpy(key, _key, keysize);
48 memcpy(ivec, _iv,
sizeof(ivec));
53 void WvBlowfishEncoder::preparekey()
57 BF_set_key(bfkey, keysize, key);
58 memset(ivec, 0,
sizeof(ivec));
65 size_t len = in.
used();
71 size_t remainder = len & 7;
73 if (remainder != 0 &&
flush)
78 size_t padlen = 8 - remainder;
79 unsigned char *pad = in.
alloc(padlen);
80 RAND_pseudo_bytes(pad, padlen);
91 if (len == 0)
return success;
93 const unsigned char *data = in.
get(len);
94 unsigned char *crypt = out.
alloc(len);
103 BF_ecb_encrypt(data, crypt, bfkey,
104 mode ==
ECBEncrypt ? BF_ENCRYPT : BF_DECRYPT);
114 BF_cfb64_encrypt(data, crypt, len, bfkey, ivec, &ivecoff,
115 mode ==
CFBEncrypt ? BF_ENCRYPT : BF_DECRYPT);
124 WvBlowfishStream::WvBlowfishStream(
WvStream *_cloned,
125 const void *_key,
size_t _keysize,
130 _key, _keysize),
true);
132 _key, _keysize),
true);
const T * get(size_t count)
Reads exactly the specified number of elements and returns a pointer to a storage location owned by t...
WvEncoderStream chains a series of encoders on the input and output ports of the underlying stream to...
virtual bool _encode(WvBuf &in, WvBuf &out, bool flush)
Template method implementation of encode().
T * alloc(size_t count)
Allocates exactly the specified number of elements and returns a pointer to an UNINITIALIZED storage ...
void setkey(const void *key, size_t keysize)
Sets the current Blowfish key and resets the initialization vector to all nulls.
virtual bool _reset()
Template method implementation of reset().
void setiv(const void *iv)
Sets the current Blowfish initialization vector.
WvBlowfishEncoder(Mode mode, const void *key, size_t keysize)
Creates a new Blowfish cipher encoder.
Specialization of WvBufBase for unsigned char type buffers intended for use with raw memory buffers.
#define deletev
Remplacement for delete[].
Unified support for streams, that is, sequences of bytes that may or may not be ready for read/write ...
bool flush(WvBuf &inbuf, WvBuf &outbuf, bool finish=false)
Flushes the encoder and optionally finishes it.
size_t used() const
Returns the number of elements in the buffer currently available for reading.