00001
00002
00003 #ifndef CRYPTOPP_LUBYRACK_H
00004 #define CRYPTOPP_LUBYRACK_H
00005
00008 #include "cryptlib.h"
00009 #include "misc.h"
00010
00011 NAMESPACE_BEGIN(CryptoPP)
00012
00014 template <class T> class LRBase : public BlockTransformation, public VariableKeyLength<16, 0, UINT_MAX, 2>
00015 {
00016 public:
00017 enum {BLOCKSIZE = 2*T::DIGESTSIZE};
00018 unsigned int BlockSize() const {return BLOCKSIZE;}
00019
00020 protected:
00021 LRBase(const byte *userKey, unsigned int keyLen);
00022
00023 enum {S=T::DIGESTSIZE};
00024 const unsigned int L;
00025 SecByteBlock key;
00026
00027 mutable T hm;
00028 mutable SecByteBlock buffer;
00029 };
00030
00032 template <class T> class LREncryption : public LRBase<T>
00033 {
00034 public:
00035
00036 LREncryption(const byte *userKey, int keyLen=LRBase<T>::DEFAULT_KEYLENGTH)
00037 : LRBase<T>(userKey, keyLen) {}
00038
00039 void ProcessBlock(byte * inoutBlock) const
00040 {LREncryption<T>::ProcessBlock(inoutBlock, inoutBlock);}
00041
00042 void ProcessBlock(const byte *inBlock, byte * outBlock) const;
00043 };
00044
00046 template <class T> class LRDecryption : public LRBase<T>
00047 {
00048 public:
00049
00050 LRDecryption(const byte *userKey, int keyLen=LRBase<T>::DEFAULT_KEYLENGTH)
00051 : LRBase<T>(userKey, keyLen) {}
00052
00053 void ProcessBlock(byte * inoutBlock) const
00054 {LRDecryption<T>::ProcessBlock(inoutBlock, inoutBlock);}
00055
00056 void ProcessBlock(const byte *inBlock, byte * outBlock) const;
00057 };
00058
00059 template <class T> LRBase<T>::LRBase(const byte *userKey, unsigned int keyLen)
00060 : L(keyLen/2), key(2*L), buffer(2*S)
00061 {
00062 memcpy(key, userKey, 2*L);
00063 }
00064
00065 #define KL key
00066 #define KR key+L
00067 #define BL buffer
00068 #define BR buffer+S
00069 #define IL inBlock
00070 #define IR inBlock+S
00071 #define OL outBlock
00072 #define OR outBlock+S
00073
00074 template <class T> void LREncryption<T>::ProcessBlock(const byte *inBlock, byte * outBlock) const
00075 {
00076 hm.Update(KL, L);
00077 hm.Update(IL, S);
00078 hm.Final(BR);
00079 xorbuf(BR, IR, S);
00080
00081 hm.Update(KR, L);
00082 hm.Update(BR, S);
00083 hm.Final(BL);
00084 xorbuf(BL, IL, S);
00085
00086 hm.Update(KL, L);
00087 hm.Update(BL, S);
00088 hm.Final(OR);
00089 xorbuf(OR, BR, S);
00090
00091 hm.Update(KR, L);
00092 hm.Update(OR, S);
00093 hm.Final(OL);
00094 xorbuf(OL, BL, S);
00095 }
00096
00097 template <class T> void LRDecryption<T>::ProcessBlock(const byte *inBlock, byte * outBlock) const
00098 {
00099 hm.Update(KR, L);
00100 hm.Update(IR, S);
00101 hm.Final(BL);
00102 xorbuf(BL, IL, S);
00103
00104 hm.Update(KL, L);
00105 hm.Update(BL, S);
00106 hm.Final(BR);
00107 xorbuf(BR, IR, S);
00108
00109 hm.Update(KR, L);
00110 hm.Update(BR, S);
00111 hm.Final(OL);
00112 xorbuf(OL, BL, S);
00113
00114 hm.Update(KL, L);
00115 hm.Update(OL, S);
00116 hm.Final(OR);
00117 xorbuf(OR, BR, S);
00118 }
00119
00120 #undef KL
00121 #undef KR
00122 #undef BL
00123 #undef BR
00124 #undef IL
00125 #undef IR
00126 #undef OL
00127 #undef OR
00128
00129 NAMESPACE_END
00130
00131 #endif