summaryrefslogtreecommitdiff
path: root/lubyrack.h
diff options
context:
space:
mode:
authorweidai <weidai11@users.noreply.github.com>2002-10-04 17:31:41 +0000
committerweidai <weidai11@users.noreply.github.com>2002-10-04 17:31:41 +0000
commita3b6ece7ab341b5b14135baeccea7d5e4c086771 (patch)
tree8b045309c238226c32a563b1df6b9c30a2f0e0b3 /lubyrack.h
downloadcryptopp-git-a3b6ece7ab341b5b14135baeccea7d5e4c086771.tar.gz
Initial revision
Diffstat (limited to 'lubyrack.h')
-rw-r--r--lubyrack.h138
1 files changed, 138 insertions, 0 deletions
diff --git a/lubyrack.h b/lubyrack.h
new file mode 100644
index 00000000..6228b296
--- /dev/null
+++ b/lubyrack.h
@@ -0,0 +1,138 @@
+// lubyrack.h - written and placed in the public domain by Wei Dai
+
+#ifndef CRYPTOPP_LUBYRACK_H
+#define CRYPTOPP_LUBYRACK_H
+
+/** \file */
+
+#include "simple.h"
+#include "secblock.h"
+
+NAMESPACE_BEGIN(CryptoPP)
+
+template <class T> struct DigestSizeDoubleWorkaround {enum {RESULT = 2*T::DIGESTSIZE};}; // VC60 workaround
+
+//! .
+template <class T>
+struct LR_Info : public VariableKeyLength<16, 0, 2*(UINT_MAX/2), 2>, public FixedBlockSize<DigestSizeDoubleWorkaround<T>::RESULT>
+{
+ static std::string StaticAlgorithmName() {return std::string("LR/")+T::StaticAlgorithmName();}
+};
+
+//! Luby-Rackoff
+template <class T>
+class LR : public LR_Info<T>, public BlockCipherDocumentation
+{
+ class Base : public BlockCipherBaseTemplate<LR_Info<T> >
+ {
+ public:
+ // VC60 workaround: have to define these functions within class definition
+ void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length)
+ {
+ AssertValidKeyLength(length);
+
+ L = length/2;
+ buffer.New(2*S);
+ digest.New(S);
+ key.Assign(userKey, 2*L);
+ }
+
+ protected:
+ enum {S=T::DIGESTSIZE};
+ unsigned int L; // key length / 2
+ SecByteBlock key;
+
+ mutable T hm;
+ mutable SecByteBlock buffer, digest;
+ };
+
+ class Enc : public Base
+ {
+ public:
+
+#define KL key
+#define KR key+L
+#define BL buffer
+#define BR buffer+S
+#define IL inBlock
+#define IR inBlock+S
+#define OL outBlock
+#define OR outBlock+S
+
+ void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
+ {
+ hm.Update(KL, L);
+ hm.Update(IL, S);
+ hm.Final(BR);
+ xorbuf(BR, IR, S);
+
+ hm.Update(KR, L);
+ hm.Update(BR, S);
+ hm.Final(BL);
+ xorbuf(BL, IL, S);
+
+ hm.Update(KL, L);
+ hm.Update(BL, S);
+ hm.Final(digest);
+ xorbuf(BR, digest, S);
+
+ hm.Update(KR, L);
+ hm.Update(OR, S);
+ hm.Final(digest);
+ xorbuf(BL, digest, S);
+
+ if (xorBlock)
+ xorbuf(outBlock, xorBlock, buffer, 2*S);
+ else
+ memcpy(outBlock, buffer, 2*S);
+ }
+ };
+
+ class Dec : public Base
+ {
+ public:
+ void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
+ {
+ hm.Update(KR, L);
+ hm.Update(IR, S);
+ hm.Final(BL);
+ xorbuf(BL, IL, S);
+
+ hm.Update(KL, L);
+ hm.Update(BL, S);
+ hm.Final(BR);
+ xorbuf(BR, IR, S);
+
+ hm.Update(KR, L);
+ hm.Update(BR, S);
+ hm.Final(digest);
+ xorbuf(BL, digest, S);
+
+ hm.Update(KL, L);
+ hm.Update(OL, S);
+ hm.Final(digest);
+ xorbuf(BR, digest, S);
+
+ if (xorBlock)
+ xorbuf(outBlock, xorBlock, buffer, 2*S);
+ else
+ memcpy(outBlock, buffer, 2*S);
+ }
+#undef KL
+#undef KR
+#undef BL
+#undef BR
+#undef IL
+#undef IR
+#undef OL
+#undef OR
+ };
+
+public:
+ typedef BlockCipherTemplate<ENCRYPTION, Enc> Encryption;
+ typedef BlockCipherTemplate<DECRYPTION, Dec> Decryption;
+};
+
+NAMESPACE_END
+
+#endif