Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:04

0001 // lubyrack.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file lubyrack.h

0004 /// \brief Classes for the Luby-Rackoff block cipher

0005 
0006 #ifndef CRYPTOPP_LUBYRACK_H
0007 #define CRYPTOPP_LUBYRACK_H
0008 
0009 #include "simple.h"
0010 #include "secblock.h"
0011 
0012 NAMESPACE_BEGIN(CryptoPP)
0013 
0014 /// \brief Luby-Rackoff block cipher information

0015 template <class T>
0016 struct LR_Info : public VariableKeyLength<16, 0, 2*(INT_MAX/2), 2>, public FixedBlockSize<2*T::DIGESTSIZE>
0017 {
0018     static std::string StaticAlgorithmName() {return std::string("LR/")+T::StaticAlgorithmName();}
0019 };
0020 
0021 /// \brief Luby-Rackoff block cipher

0022 template <class T>
0023 class LR : public LR_Info<T>, public BlockCipherDocumentation
0024 {
0025     class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<LR_Info<T> >
0026     {
0027     public:
0028         // VC60 workaround: have to define these functions within class definition

0029         void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
0030         {
0031             this->AssertValidKeyLength(length);
0032 
0033             L = length/2;
0034             buffer.New(2*S);
0035             digest.New(S);
0036             key.Assign(userKey, 2*L);
0037         }
0038 
0039     protected:
0040         CRYPTOPP_CONSTANT(S=T::DIGESTSIZE);
0041         unsigned int L; // key length / 2

0042         SecByteBlock key;
0043 
0044         mutable T hm;
0045         mutable SecByteBlock buffer, digest;
0046     };
0047 
0048     class CRYPTOPP_NO_VTABLE Enc : public Base
0049     {
0050     public:
0051 
0052 #define KL this->key
0053 #define KR this->key+this->L
0054 #define BL this->buffer
0055 #define BR this->buffer+this->S
0056 #define IL inBlock
0057 #define IR inBlock+this->S
0058 #define OL outBlock
0059 #define OR outBlock+this->S
0060 
0061         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
0062         {
0063             this->hm.Update(KL, this->L);
0064             this->hm.Update(IL, this->S);
0065             this->hm.Final(BR);
0066             xorbuf(BR, IR, this->S);
0067 
0068             this->hm.Update(KR, this->L);
0069             this->hm.Update(BR, this->S);
0070             this->hm.Final(BL);
0071             xorbuf(BL, IL, this->S);
0072 
0073             this->hm.Update(KL, this->L);
0074             this->hm.Update(BL, this->S);
0075             this->hm.Final(this->digest);
0076             xorbuf(BR, this->digest, this->S);
0077 
0078             this->hm.Update(KR, this->L);
0079             this->hm.Update(OR, this->S);
0080             this->hm.Final(this->digest);
0081             xorbuf(BL, this->digest, this->S);
0082 
0083             if (xorBlock)
0084                 xorbuf(outBlock, xorBlock, this->buffer, 2*this->S);
0085             else
0086                 memcpy_s(outBlock, 2*this->S, this->buffer, 2*this->S);
0087         }
0088     };
0089 
0090     class CRYPTOPP_NO_VTABLE Dec : public Base
0091     {
0092     public:
0093         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
0094         {
0095             this->hm.Update(KR, this->L);
0096             this->hm.Update(IR, this->S);
0097             this->hm.Final(BL);
0098             xorbuf(BL, IL, this->S);
0099 
0100             this->hm.Update(KL, this->L);
0101             this->hm.Update(BL, this->S);
0102             this->hm.Final(BR);
0103             xorbuf(BR, IR, this->S);
0104 
0105             this->hm.Update(KR, this->L);
0106             this->hm.Update(BR, this->S);
0107             this->hm.Final(this->digest);
0108             xorbuf(BL, this->digest, this->S);
0109 
0110             this->hm.Update(KL, this->L);
0111             this->hm.Update(OL, this->S);
0112             this->hm.Final(this->digest);
0113             xorbuf(BR, this->digest, this->S);
0114 
0115             if (xorBlock)
0116                 xorbuf(outBlock, xorBlock, this->buffer, 2*this->S);
0117             else
0118                 std::memcpy(outBlock, this->buffer, 2*this->S);
0119         }
0120 #undef KL
0121 #undef KR
0122 #undef BL
0123 #undef BR
0124 #undef IL
0125 #undef IR
0126 #undef OL
0127 #undef OR
0128     };
0129 
0130 public:
0131     typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
0132     typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
0133 };
0134 
0135 NAMESPACE_END
0136 
0137 #endif