9#include <openssl/rand.h>
10#include <openssl/blowfish.h>
15 const void *_key,
size_t _keysize) :
16 mode(_mode), key(NULL), bfkey(NULL)
22WvBlowfishEncoder::~WvBlowfishEncoder()
40 key =
new unsigned char[keysize];
41 memcpy(key, _key, keysize);
48 memcpy(ivec, _iv,
sizeof(ivec));
53void 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);
124WvBlowfishStream::WvBlowfishStream(
WvStream *_cloned,
125 const void *_key,
size_t _keysize,
130 _key, _keysize),
true);
132 _key, _keysize),
true);
WvBlowfishEncoder(Mode mode, const void *key, size_t keysize)
Creates a new Blowfish cipher encoder.
virtual bool _reset()
Template method implementation of reset().
virtual bool _encode(WvBuf &in, WvBuf &out, bool flush)
Template method implementation of encode().
void setiv(const void *iv)
Sets the current Blowfish initialization vector.
void setkey(const void *key, size_t keysize)
Sets the current Blowfish key and resets the initialization vector to all nulls.
const T * get(size_t count)
Reads exactly the specified number of elements and returns a pointer to a storage location owned by t...
T * alloc(size_t count)
Allocates exactly the specified number of elements and returns a pointer to an UNINITIALIZED storage ...
size_t used() const
Returns the number of elements in the buffer currently available for reading.
Specialization of WvBufBase for unsigned char type buffers intended for use with raw memory buffers.
WvEncoderStream chains a series of encoders on the input and output ports of the underlying stream to...
bool flush(WvBuf &inbuf, WvBuf &outbuf, bool finish=false)
Flushes the encoder and optionally finishes it.
Unified support for streams, that is, sequences of bytes that may or may not be ready for read/write ...
#define deletev
Remplacement for delete[].