00001 #ifndef CRYPTOPP_MODES_H
00002 #define CRYPTOPP_MODES_H
00003
00007 #include "cryptlib.h"
00008 #include "misc.h"
00009
00010 NAMESPACE_BEGIN(CryptoPP)
00011
00012 class CipherMode
00013 {
00014 protected:
00015 CipherMode(const BlockTransformation &cipher, const byte *IV);
00016
00017 const BlockTransformation &cipher;
00018 const int S;
00019 SecByteBlock reg, buffer;
00020 };
00021
00022 class FeedBackMode : protected CipherMode
00023 {
00024 protected:
00025
00026 FeedBackMode(const BlockTransformation &cipher, const byte *IV, int feedBackSize);
00027 void DoFeedBack();
00028
00029 const int FBS;
00030 int counter;
00031 };
00032
00034 class CFBEncryption : public StreamCipher, protected FeedBackMode
00035 {
00036 public:
00038 CFBEncryption(const BlockTransformation &cipher, const byte *IV, int feedBackSize = 0)
00039 : FeedBackMode(cipher, IV, feedBackSize) {}
00040
00041 byte ProcessByte(byte input)
00042 {
00043 if (counter==FBS)
00044 DoFeedBack();
00045 buffer[counter] ^= input;
00046 return buffer[counter++];
00047 }
00048
00049 void ProcessString(byte *outString, const byte *inString, unsigned int length);
00050 void ProcessString(byte *inoutString, unsigned int length);
00051 };
00052
00054 class CFBDecryption : public StreamCipher, protected FeedBackMode
00055 {
00056 public:
00058 CFBDecryption(const BlockTransformation &cipher, const byte *IV, int feedBackSize = 0)
00059 : FeedBackMode(cipher, IV, feedBackSize) {}
00060
00061 byte ProcessByte(byte input)
00062 {
00063 if (counter==FBS)
00064 DoFeedBack();
00065 byte b = buffer[counter] ^ input;
00066 buffer[counter++] = input;
00067 return (b);
00068 }
00069
00070 void ProcessString(byte *outString, const byte *inString, unsigned int length);
00071 void ProcessString(byte *inoutString, unsigned int length);
00072 };
00073
00075 class OFB : public RandomNumberGenerator, public StreamCipher, protected FeedBackMode
00076 {
00077 public:
00079 OFB(const BlockTransformation &cipher, const byte *IV, int feedBackSize = 0)
00080 : FeedBackMode(cipher, IV, feedBackSize) {}
00081
00082 byte GenerateByte()
00083 {
00084 if (counter==FBS)
00085 DoFeedBack();
00086 return buffer[counter++];
00087 }
00088
00089 byte ProcessByte(byte input)
00090 {return (input ^ OFB::GenerateByte());}
00091
00092 void ProcessString(byte *outString, const byte *inString, unsigned int length);
00093 void ProcessString(byte *inoutString, unsigned int length);
00094 };
00095
00097 class CounterMode : public RandomNumberGenerator, public RandomAccessStreamCipher, protected CipherMode
00098 {
00099 public:
00100
00101 CounterMode(const BlockTransformation &cipher, const byte *IV);
00102
00103 byte GenerateByte()
00104 {
00105 if (size==S)
00106 IncrementCounter();
00107 return buffer[size++];
00108 }
00109
00110 byte ProcessByte(byte input)
00111 {return (input ^ CounterMode::GenerateByte());}
00112
00113 void ProcessString(byte *outString, const byte *inString, unsigned int length);
00114 void ProcessString(byte *inoutString, unsigned int length);
00115
00116 void Seek(unsigned long position);
00117
00118 private:
00119 void IncrementCounter();
00120
00121 SecByteBlock IV;
00122 int size;
00123 };
00124
00126 class PGP_CFBEncryption : public CFBEncryption
00127 {
00128 public:
00130 PGP_CFBEncryption(const BlockTransformation &cipher, const byte *IV)
00131 : CFBEncryption(cipher, IV, 0) {}
00132
00133 void Sync();
00134 };
00135
00137 class PGP_CFBDecryption : public CFBDecryption
00138 {
00139 public:
00141 PGP_CFBDecryption(const BlockTransformation &cipher, const byte *IV)
00142 : CFBDecryption(cipher, IV, 0) {}
00143
00144 void Sync();
00145 };
00146
00147 NAMESPACE_END
00148
00149 #endif