Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

wvblowfish.h

Go to the documentation of this file.
00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Tunnel Vision Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  *
00005  * Blowfish cryptography abstractions.
00006  */
00007 #ifndef __WVBLOWFISH_H
00008 #define __WVBLOWFISH_H
00009 
00010 #include "wvencoder.h"
00011 #include "wvencoderstream.h"
00012 
00013 struct bf_key_st;
00014 
00015 /**
00016  * An encoder implementing the Blowfish encryption method.
00017  * 
00018  * Supports reset().
00019  * 
00020  */
00021 class WvBlowfishEncoder : public WvEncoder
00022 {
00023 public:
00024     enum Mode {
00025         ECBEncrypt, /*!< Encrypt using ECB mode (avoid) */
00026         ECBDecrypt, /*!< Decrypt using ECB mode (avoid) */
00027         CFBEncrypt, /*!< Encrypt using CFB mode (simulates a stream) */
00028         CFBDecrypt  /*!< Decrypt using CFB mode (simulates a stream) */
00029     };
00030 
00031     /**
00032      * Creates a new Blowfish cipher encoder.
00033      *
00034      * "mode" is the encryption mode
00035      * "key" is the initial key
00036      * "keysize" is the initial key size in bytes
00037      */
00038     WvBlowfishEncoder(Mode mode, const void *key, size_t keysize);
00039     virtual ~WvBlowfishEncoder();
00040 
00041     /**
00042      * Sets the current Blowfish key and resets the initialization
00043      * vector to all nulls.
00044      *
00045      * "key" is the new key
00046      * "keysize" is the key size in bytes
00047      */
00048     void setkey(const void *key, size_t keysize);
00049     
00050     /**
00051      * Sets the current Blowfish initialization vector.
00052      *
00053      * "iv" is the new IV must be 8 bytes
00054      */
00055     void setiv(const void *iv);
00056 
00057     /** Return true if mode is encrypting or false if decrypting. */
00058     bool is_encrypting() const {
00059         return (mode == ECBEncrypt || mode == CFBEncrypt);
00060     }
00061     
00062 protected:
00063     virtual bool _encode(WvBuf &in, WvBuf &out, bool flush);
00064     virtual bool _reset(); // supported: restores most recently set
00065         // key and initialization vector
00066 
00067     Mode mode;
00068     size_t keysize;
00069     unsigned char *key;
00070     struct bf_key_st *bfkey;
00071     unsigned char ivec[8]; // initialization vector
00072     int ivecoff; // current offset into initvec
00073 
00074     void preparekey();
00075 };
00076 
00077 
00078 /**
00079  * A crypto stream implementing Blowfish encryption.
00080  * 
00081  * By default, written data is encrypted using
00082  * WvBlowfishEncoder::CFBEncrypt, read data is decrypted using
00083  * WvBlowfishEncoder::CFBDecrypt.
00084  * 
00085  * @see WvBlowfishEncoder
00086  */
00087 class WvBlowfishStream : public WvEncoderStream
00088 {
00089 public:
00090     WvBlowfishStream(WvStream *_cloned,
00091         const void *key, size_t _keysize,
00092         WvBlowfishEncoder::Mode readmode = WvBlowfishEncoder::CFBDecrypt,
00093         WvBlowfishEncoder::Mode writemode = WvBlowfishEncoder::CFBEncrypt);
00094     virtual ~WvBlowfishStream() { }
00095 };
00096 
00097 #endif // __WVBLOWFISH_H

Generated on Sat Mar 13 14:55:29 2004 for WvStreams by doxygen 1.3.6-20040222