WvStreams
wvtripledes.h
00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Tunnel Vision Software:
00003  *   Copyright (C) 1997-2003 Net Integration Technologies, Inc.
00004  *
00005  * TripleDES cryptography abstractions.
00006  */
00007 #ifndef __WVTRIPLEDES_H
00008 #define __WVTRIPLEDES_H
00009 
00010 #include "wvencoder.h"
00011 #include "wvencoderstream.h"
00012 #include "wvcrypto.h"
00013 
00014 #include <openssl/des.h>
00015 
00022 class WvTripleDESEncoder : public WvCryptoEncoder
00023 {
00024 public:
00025     enum Mode {
00026         ECBEncrypt, 
00027         ECBDecrypt, 
00028         CFBEncrypt, 
00029         CFBDecrypt, 
00030         CBCEncrypt, 
00031         CBCDecrypt  
00032     };
00033 
00034     /*
00035      * Creates a new TripleDES cipher encoder.
00036      *
00037      * "mode" is the encryption mode
00038      * "key[1-3]" are the initial keys
00039      */
00040     WvTripleDESEncoder(Mode mode, const void *key1, const void *key2, 
00041                        const void *key3);
00042 /*     virtual ~WvTripleDESEncoder(); */
00043 
00044     /*
00045      * Sets the current TripleDES keys and resets the initialization
00046      * vector to all nulls.
00047      *
00048      * "key[1-3]" are the new keys
00049      */
00050     virtual void setkey(const void *key)
00051     { 
00052         setkey(key, (unsigned char*)key+DES_KEY_SZ, 
00053                (unsigned char *)key+(DES_KEY_SZ*2)); 
00054         return; 
00055     }
00056     virtual void setkey(const void *_key1, const void *_key2, 
00057                         const void *_key3);
00058     
00059     /*
00060      * Sets the current TripleDES initialization vector.
00061      *
00062      * "iv" is the new IV must be 8 bytes
00063      */
00064     virtual void setiv(const void *iv);
00065 
00066 protected:
00067     virtual bool _encode(WvBuf &in, WvBuf &out, bool flush);
00068     virtual bool _reset(); // supported: restores most recently set
00069     // key and initialization vector
00070 
00071 private:
00072     Mode mode;
00073     des_cblock key;
00074     des_key_schedule deskey1;
00075     des_key_schedule deskey2;
00076     des_key_schedule deskey3;
00077     des_cblock ivec; // initialization vector
00078     int ivecoff; // current offset into initvec
00079 };
00080 
00081 
00091 class WvTripleDESStream : public WvEncoderStream
00092 {
00093 public:
00094     WvTripleDESStream(WvStream *_cloned, const void *_key1,
00095                       const void *_key2, const void *_key3,
00096                       WvTripleDESEncoder::Mode readmode = WvTripleDESEncoder::CFBDecrypt,
00097                       WvTripleDESEncoder::Mode writemode = WvTripleDESEncoder::CFBEncrypt);
00098     virtual ~WvTripleDESStream() { }
00099 };
00100 
00101 #endif // __WVTRIPLEDES_H