Crypto++  5.6.3
Free C++ class library of cryptographic schemes
lubyrack.h
Go to the documentation of this file.
1 // lubyrack.h - written and placed in the public domain by Wei Dai
2 
3 //! \file lubyrack.h
4 //! \brief Classes for the Luby-Rackoff block cipher
5 
6 #ifndef CRYPTOPP_LUBYRACK_H
7 #define CRYPTOPP_LUBYRACK_H
8 
9 #include "simple.h"
10 #include "secblock.h"
11 
12 NAMESPACE_BEGIN(CryptoPP)
13 
14 template <class T> struct DigestSizeDoubleWorkaround // VC60 workaround
15 {
16  CRYPTOPP_CONSTANT(RESULT = 2*T::DIGESTSIZE)
17 };
18 
19 //! algorithm info
20 template <class T>
21 struct LR_Info : public VariableKeyLength<16, 0, 2*(INT_MAX/2), 2>, public FixedBlockSize<DigestSizeDoubleWorkaround<T>::RESULT>
22 {
23  static std::string StaticAlgorithmName() {return std::string("LR/")+T::StaticAlgorithmName();}
24 };
25 
26 //! Luby-Rackoff
27 template <class T>
28 class LR : public LR_Info<T>, public BlockCipherDocumentation
29 {
30  class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<LR_Info<T> >
31  {
32  public:
33  // VC60 workaround: have to define these functions within class definition
34  void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
35  {
36  this->AssertValidKeyLength(length);
37 
38  L = length/2;
39  buffer.New(2*S);
40  digest.New(S);
41  key.Assign(userKey, 2*L);
42  }
43 
44  protected:
45  CRYPTOPP_CONSTANT(S=T::DIGESTSIZE)
46  unsigned int L; // key length / 2
47  SecByteBlock key;
48 
49  mutable T hm;
50  mutable SecByteBlock buffer, digest;
51  };
52 
53  class CRYPTOPP_NO_VTABLE Enc : public Base
54  {
55  public:
56 
57 #define KL this->key
58 #define KR this->key+this->L
59 #define BL this->buffer
60 #define BR this->buffer+this->S
61 #define IL inBlock
62 #define IR inBlock+this->S
63 #define OL outBlock
64 #define OR outBlock+this->S
65 
66  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
67  {
68  this->hm.Update(KL, this->L);
69  this->hm.Update(IL, this->S);
70  this->hm.Final(BR);
71  xorbuf(BR, IR, this->S);
72 
73  this->hm.Update(KR, this->L);
74  this->hm.Update(BR, this->S);
75  this->hm.Final(BL);
76  xorbuf(BL, IL, this->S);
77 
78  this->hm.Update(KL, this->L);
79  this->hm.Update(BL, this->S);
80  this->hm.Final(this->digest);
81  xorbuf(BR, this->digest, this->S);
82 
83  this->hm.Update(KR, this->L);
84  this->hm.Update(OR, this->S);
85  this->hm.Final(this->digest);
86  xorbuf(BL, this->digest, this->S);
87 
88  if (xorBlock)
89  xorbuf(outBlock, xorBlock, this->buffer, 2*this->S);
90  else
91  memcpy_s(outBlock, 2*this->S, this->buffer, 2*this->S);
92  }
93  };
94 
95  class CRYPTOPP_NO_VTABLE Dec : public Base
96  {
97  public:
98  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
99  {
100  this->hm.Update(KR, this->L);
101  this->hm.Update(IR, this->S);
102  this->hm.Final(BL);
103  xorbuf(BL, IL, this->S);
104 
105  this->hm.Update(KL, this->L);
106  this->hm.Update(BL, this->S);
107  this->hm.Final(BR);
108  xorbuf(BR, IR, this->S);
109 
110  this->hm.Update(KR, this->L);
111  this->hm.Update(BR, this->S);
112  this->hm.Final(this->digest);
113  xorbuf(BL, this->digest, this->S);
114 
115  this->hm.Update(KL, this->L);
116  this->hm.Update(OL, this->S);
117  this->hm.Final(this->digest);
118  xorbuf(BR, this->digest, this->S);
119 
120  if (xorBlock)
121  xorbuf(outBlock, xorBlock, this->buffer, 2*this->S);
122  else
123  memcpy(outBlock, this->buffer, 2*this->S);
124  }
125 #undef KL
126 #undef KR
127 #undef BL
128 #undef BR
129 #undef IL
130 #undef IR
131 #undef OL
132 #undef OR
133  };
134 
135 public:
138 };
139 
140 NAMESPACE_END
141 
142 #endif
Classes providing simple keying interfaces.
algorithm info
Definition: lubyrack.h:21
Provides Encryption and Decryption typedefs used by derived classes to implement a block cipher...
Definition: seckey.h:385
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
Definition: misc.h:299
SecByteBlock is a SecBlock<byte> typedef.
Definition: secblock.h:722
Classes and functions for secure memory allocations.
Inherited by block ciphers with fixed block size.
Definition: seckey.h:34
Inherited by keyed algorithms with variable key length.
Definition: seckey.h:152
void xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
Definition: misc.cpp:28
Provides class member functions to access BlockCipher constants.
Definition: seckey.h:292
Crypto++ library namespace.
Luby-Rackoff.
Definition: lubyrack.h:28
Interface for retrieving values given their names.
Definition: cryptlib.h:261