Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

# lubyrack.h

Go to the documentation of this file.

    00001 // lubyrack.h - written and placed in the public domain by Wei Dai
    00002
    00003 #ifndef CRYPTOPP_LUBYRACK_H
    00004 #define CRYPTOPP_LUBYRACK_H
    00005
    00006 /** \file */
    00007
    00008 #include "simple.h"
    00009 #include "secblock.h"
    00010
    00011 NAMESPACE_BEGIN(CryptoPP)
    00012
    00013 template <class T> struct DigestSizeDoubleWorkaround {enum {RESULT = 2*T::DIGESTSIZE};};        // VC60 workaround
    00014
    00015 //! .
    00016 template <class T>
    00017 struct LR_Info : public VariableKeyLength<16, 0, 2*(UINT_MAX/2), 2>, public FixedBlockSize<DigestSizeDoubleWorkaround<T>::RESULT>
    00018 {
    00019         static std::string StaticAlgorithmName() {return std::string("LR/")+T::StaticAlgorithmName();}
    00020 };
    00021
    00022 //! Luby-Rackoff
    00023 template <class T>
    00024 class LR : public LR_Info<T>, public BlockCipherDocumentation
    00025 {
    00026         class Base : public BlockCipherBaseTemplate<LR_Info<T> >
    00027         {
    00028         public:
    00029                 // VC60 workaround: have to define these functions within class definition
    00030                 void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length)
    00031                 {
    00032                         AssertValidKeyLength(length);
    00033
    00034                         L = length/2;
    00035                         buffer.New(2*S);
    00036                         digest.New(S);
    00037                         key.Assign(userKey, 2*L);
    00038                 }
    00039
    00040         protected:
    00041                 enum {S=T::DIGESTSIZE};
    00042                 unsigned int L; // key length / 2
    00043                 SecByteBlock key;
    00044
    00045                 mutable T hm;
    00046                 mutable SecByteBlock buffer, digest;
    00047         };
    00048
    00049         class Enc : public Base
    00050         {
    00051         public:
    00052
    00053 #define KL key
    00054 #define KR key+L
    00055 #define BL buffer
    00056 #define BR buffer+S
    00057 #define IL inBlock
    00058 #define IR inBlock+S
    00059 #define OL outBlock
    00060 #define OR outBlock+S
    00061
    00062                 void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
    00063                 {
    00064                         hm.Update(KL, L);
    00065                         hm.Update(IL, S);
    00066                         hm.Final(BR);
    00067                         xorbuf(BR, IR, S);
    00068
    00069                         hm.Update(KR, L);
    00070                         hm.Update(BR, S);
    00071                         hm.Final(BL);
    00072                         xorbuf(BL, IL, S);
    00073
    00074                         hm.Update(KL, L);
    00075                         hm.Update(BL, S);
    00076                         hm.Final(digest);
    00077                         xorbuf(BR, digest, S);
    00078
    00079                         hm.Update(KR, L);
    00080                         hm.Update(OR, S);
    00081                         hm.Final(digest);
    00082                         xorbuf(BL, digest, S);
    00083
    00084                         if (xorBlock)
    00085                                 xorbuf(outBlock, xorBlock, buffer, 2*S);
    00086                         else
    00087                                 memcpy(outBlock, buffer, 2*S);
    00088                 }
    00089         };
    00090
    00091         class Dec : public Base
    00092         {
    00093         public:
    00094                 void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
    00095                 {
    00096                         hm.Update(KR, L);
    00097                         hm.Update(IR, S);
    00098                         hm.Final(BL);
    00099                         xorbuf(BL, IL, S);
    00100
    00101                         hm.Update(KL, L);
    00102                         hm.Update(BL, S);
    00103                         hm.Final(BR);
    00104                         xorbuf(BR, IR, S);
    00105
    00106                         hm.Update(KR, L);
    00107                         hm.Update(BR, S);
    00108                         hm.Final(digest);
    00109                         xorbuf(BL, digest, S);
    00110
    00111                         hm.Update(KL, L);
    00112                         hm.Update(OL, S);
    00113                         hm.Final(digest);
    00114                         xorbuf(BR, digest, S);
    00115
    00116                         if (xorBlock)
    00117                                 xorbuf(outBlock, xorBlock, buffer, 2*S);
    00118                         else
    00119                                 memcpy(outBlock, buffer, 2*S);
    00120                 }
    00121 #undef KL
    00122 #undef KR
    00123 #undef BL
    00124 #undef BR
    00125 #undef IL
    00126 #undef IR
    00127 #undef OL
    00128 #undef OR
    00129         };
    00130
    00131 public:
    00132         typedef BlockCipherTemplate<ENCRYPTION, Enc> Encryption;
    00133         typedef BlockCipherTemplate<DECRYPTION, Dec> Decryption;
    00134 };
    00135
    00136 NAMESPACE_END
    00137
    00138 #endif