Make WvStreams compile with gcc 4.4.
[wvstreams.git] / include / wvblowfish.h
blobd1cb77bbee25d1fd20445b9b2ce9ae1caebfe93c
1 /* -*- Mode: C++ -*-
2 * Worldvisions Tunnel Vision Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
5 * Blowfish cryptography abstractions.
6 */
7 #ifndef __WVBLOWFISH_H
8 #define __WVBLOWFISH_H
10 #include "wvencoder.h"
11 #include "wvencoderstream.h"
13 struct bf_key_st;
15 /**
16 * An encoder implementing the Blowfish encryption method.
18 * Supports reset().
21 class WvBlowfishEncoder : public WvEncoder
23 public:
24 enum Mode {
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) */
31 /**
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();
41 /**
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);
50 /**
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);
62 protected:
63 virtual bool _encode(WvBuf &in, WvBuf &out, bool flush);
64 virtual bool _reset(); // supported: restores most recently set
65 // key and initialization vector
67 Mode mode;
68 size_t keysize;
69 unsigned char *key;
70 struct bf_key_st *bfkey;
71 unsigned char ivec[8]; // initialization vector
72 int ivecoff; // current offset into initvec
74 void preparekey();
78 /**
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
89 public:
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