Crypto++
lubyrack.h
Go to the documentation of this file.
1 // lubyrack.h - written and placed in the public domain by Wei Dai
2 
3 #ifndef CRYPTOPP_LUBYRACK_H
4 #define CRYPTOPP_LUBYRACK_H
5 
6 /** \file */
7 
8 #include "simple.h"
9 #include "secblock.h"
10 
11 NAMESPACE_BEGIN(CryptoPP)
12 
13 template <class T> struct DigestSizeDoubleWorkaround // VC60 workaround
14 {
15  CRYPTOPP_CONSTANT(RESULT = 2*T::DIGESTSIZE)
16 };
17 
18 //! algorithm info
19 template <class T>
20 struct LR_Info : public VariableKeyLength<16, 0, 2*(INT_MAX/2), 2>, public FixedBlockSize<DigestSizeDoubleWorkaround<T>::RESULT>
21 {
22  static std::string StaticAlgorithmName() {return std::string("LR/")+T::StaticAlgorithmName();}
23 };
24 
25 //! Luby-Rackoff
26 template <class T>
27 class LR : public LR_Info<T>, public BlockCipherDocumentation
28 {
29  class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<LR_Info<T> >
30  {
31  public:
32  // VC60 workaround: have to define these functions within class definition
33  void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
34  {
35  this->AssertValidKeyLength(length);
36 
37  L = length/2;
38  buffer.New(2*S);
39  digest.New(S);
40  key.Assign(userKey, 2*L);
41  }
42 
43  protected:
44  CRYPTOPP_CONSTANT(S=T::DIGESTSIZE)
45  unsigned int L; // key length / 2
46  SecByteBlock key;
47 
48  mutable T hm;
49  mutable SecByteBlock buffer, digest;
50  };
51 
52  class CRYPTOPP_NO_VTABLE Enc : public Base
53  {
54  public:
55 
56 #define KL this->key
57 #define KR this->key+this->L
58 #define BL this->buffer
59 #define BR this->buffer+this->S
60 #define IL inBlock
61 #define IR inBlock+this->S
62 #define OL outBlock
63 #define OR outBlock+this->S
64 
65  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
66  {
67  this->hm.Update(KL, this->L);
68  this->hm.Update(IL, this->S);
69  this->hm.Final(BR);
70  xorbuf(BR, IR, this->S);
71 
72  this->hm.Update(KR, this->L);
73  this->hm.Update(BR, this->S);
74  this->hm.Final(BL);
75  xorbuf(BL, IL, this->S);
76 
77  this->hm.Update(KL, this->L);
78  this->hm.Update(BL, this->S);
79  this->hm.Final(this->digest);
80  xorbuf(BR, this->digest, this->S);
81 
82  this->hm.Update(KR, this->L);
83  this->hm.Update(OR, this->S);
84  this->hm.Final(this->digest);
85  xorbuf(BL, this->digest, this->S);
86 
87  if (xorBlock)
88  xorbuf(outBlock, xorBlock, this->buffer, 2*this->S);
89  else
90  memcpy_s(outBlock, 2*this->S, this->buffer, 2*this->S);
91  }
92  };
93 
94  class CRYPTOPP_NO_VTABLE Dec : public Base
95  {
96  public:
97  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
98  {
99  this->hm.Update(KR, this->L);
100  this->hm.Update(IR, this->S);
101  this->hm.Final(BL);
102  xorbuf(BL, IL, this->S);
103 
104  this->hm.Update(KL, this->L);
105  this->hm.Update(BL, this->S);
106  this->hm.Final(BR);
107  xorbuf(BR, IR, this->S);
108 
109  this->hm.Update(KR, this->L);
110  this->hm.Update(BR, this->S);
111  this->hm.Final(this->digest);
112  xorbuf(BL, this->digest, this->S);
113 
114  this->hm.Update(KL, this->L);
115  this->hm.Update(OL, this->S);
116  this->hm.Final(this->digest);
117  xorbuf(BR, this->digest, this->S);
118 
119  if (xorBlock)
120  xorbuf(outBlock, xorBlock, this->buffer, 2*this->S);
121  else
122  memcpy(outBlock, this->buffer, 2*this->S);
123  }
124 #undef KL
125 #undef KR
126 #undef BL
127 #undef BR
128 #undef IL
129 #undef IR
130 #undef OL
131 #undef OR
132  };
133 
134 public:
137 };
138 
139 NAMESPACE_END
140 
141 #endif
algorithm info
Definition: lubyrack.h:20
These objects usually should not be used directly. See CipherModeDocumentation instead.
Definition: seckey.h:188
to be inherited by block ciphers with fixed block size
Definition: seckey.h:21
support query of variable key length, template parameters are default, min, max, multiple (default mu...
Definition: seckey.h:80
Luby-Rackoff.
Definition: lubyrack.h:27
interface for retrieving values given their names
Definition: cryptlib.h:224