2 * Worldvisions Tunnel Vision Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
5 * Blowfish cryptography abstractions.
10 #include "wvencoder.h"
11 #include "wvencoderstream.h"
16 * An encoder implementing the Blowfish encryption method.
21 class WvBlowfishEncoder
: public WvEncoder
25 ECBEncrypt
, /*!< Encrypt using ECB mode (avoid) */
26 ECBDecrypt
, /*!< Decrypt using ECB mode (avoid) */
27 CFBEncrypt
, /*!< Encrypt using CFB mode (simulates a stream) */
28 CFBDecrypt
/*!< Decrypt using CFB mode (simulates a stream) */
32 * Creates a new Blowfish cipher encoder.
34 * "mode" is the encryption mode
35 * "key" is the initial key
36 * "keysize" is the initial key size in bytes
38 WvBlowfishEncoder(Mode mode
, const void *key
, size_t keysize
);
39 virtual ~WvBlowfishEncoder();
42 * Sets the current Blowfish key and resets the initialization
43 * vector to all nulls.
45 * "key" is the new key
46 * "keysize" is the key size in bytes
48 void setkey(const void *key
, size_t keysize
);
51 * Sets the current Blowfish initialization vector.
53 * "iv" is the new IV must be 8 bytes
55 void setiv(const void *iv
);
57 /** Return true if mode is encrypting or false if decrypting. */
58 bool is_encrypting() const {
59 return (mode
== ECBEncrypt
|| mode
== CFBEncrypt
);
63 virtual bool _encode(WvBuf
&in
, WvBuf
&out
, bool flush
);
64 virtual bool _reset(); // supported: restores most recently set
65 // key and initialization vector
70 struct bf_key_st
*bfkey
;
71 unsigned char ivec
[8]; // initialization vector
72 int ivecoff
; // current offset into initvec
79 * A crypto stream implementing Blowfish encryption.
81 * By default, written data is encrypted using
82 * WvBlowfishEncoder::CFBEncrypt, read data is decrypted using
83 * WvBlowfishEncoder::CFBDecrypt.
85 * @see WvBlowfishEncoder
87 class WvBlowfishStream
: public WvEncoderStream
90 WvBlowfishStream(WvStream
*_cloned
,
91 const void *key
, size_t _keysize
,
92 WvBlowfishEncoder::Mode readmode
= WvBlowfishEncoder::CFBDecrypt
,
93 WvBlowfishEncoder::Mode writemode
= WvBlowfishEncoder::CFBEncrypt
);
94 virtual ~WvBlowfishStream() { }
97 #endif // __WVBLOWFISH_H