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);
T * alloc(size_t count)
Allocates exactly the specified number of elements and returns a pointer to an UNINITIALIZED storage ...
WvEncoderStream chains a series of encoders on the input and output ports of the underlying stream to...
virtual bool _reset()
Template method implementation of reset().
void setkey(const void *key, size_t keysize)
Sets the current Blowfish key and resets the initialization vector to all nulls.
Unified support for streams, that is, sequences of bytes that may or may not be ready for read/write ...
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...
size_t used() const
Returns the number of elements in the buffer currently available for reading.
void setiv(const void *iv)
Sets the current Blowfish initialization vector.
#define deletev
Remplacement for delete[].
virtual bool _encode(WvBuf &in, WvBuf &out, bool flush)
Template method implementation of encode().
const T * get(size_t count)
Reads exactly the specified number of elements and returns a pointer to a storage location owned by t...