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 //! \class LR_Info
20 //! \brief Luby-Rackoff block cipher information
21 template <class T>
22 struct LR_Info : public VariableKeyLength<16, 0, 2*(INT_MAX/2), 2>, public FixedBlockSize<DigestSizeDoubleWorkaround<T>::RESULT>
23 {
24  static std::string StaticAlgorithmName() {return std::string("LR/")+T::StaticAlgorithmName();}
25 };
26 
27 //! \class LR
28 //! \brief Luby-Rackoff block cipher
29 template <class T>
30 class LR : public LR_Info<T>, public BlockCipherDocumentation
31 {
32  class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<LR_Info<T> >
33  {
34  public:
35  // VC60 workaround: have to define these functions within class definition
36  void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
37  {
38  this->AssertValidKeyLength(length);
39 
40  L = length/2;
41  buffer.New(2*S);
42  digest.New(S);
43  key.Assign(userKey, 2*L);
44  }
45 
46  protected:
47  CRYPTOPP_CONSTANT(S=T::DIGESTSIZE)
48  unsigned int L; // key length / 2
49  SecByteBlock key;
50 
51  mutable T hm;
52  mutable SecByteBlock buffer, digest;
53  };
54 
55  class CRYPTOPP_NO_VTABLE Enc : public Base
56  {
57  public:
58 
59 #define KL this->key
60 #define KR this->key+this->L
61 #define BL this->buffer
62 #define BR this->buffer+this->S
63 #define IL inBlock
64 #define IR inBlock+this->S
65 #define OL outBlock
66 #define OR outBlock+this->S
67 
68  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
69  {
70  this->hm.Update(KL, this->L);
71  this->hm.Update(IL, this->S);
72  this->hm.Final(BR);
73  xorbuf(BR, IR, this->S);
74 
75  this->hm.Update(KR, this->L);
76  this->hm.Update(BR, this->S);
77  this->hm.Final(BL);
78  xorbuf(BL, IL, this->S);
79 
80  this->hm.Update(KL, this->L);
81  this->hm.Update(BL, this->S);
82  this->hm.Final(this->digest);
83  xorbuf(BR, this->digest, this->S);
84 
85  this->hm.Update(KR, this->L);
86  this->hm.Update(OR, this->S);
87  this->hm.Final(this->digest);
88  xorbuf(BL, this->digest, this->S);
89 
90  if (xorBlock)
91  xorbuf(outBlock, xorBlock, this->buffer, 2*this->S);
92  else
93  memcpy_s(outBlock, 2*this->S, this->buffer, 2*this->S);
94  }
95  };
96 
97  class CRYPTOPP_NO_VTABLE Dec : public Base
98  {
99  public:
100  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
101  {
102  this->hm.Update(KR, this->L);
103  this->hm.Update(IR, this->S);
104  this->hm.Final(BL);
105  xorbuf(BL, IL, this->S);
106 
107  this->hm.Update(KL, this->L);
108  this->hm.Update(BL, this->S);
109  this->hm.Final(BR);
110  xorbuf(BR, IR, this->S);
111 
112  this->hm.Update(KR, this->L);
113  this->hm.Update(BR, this->S);
114  this->hm.Final(this->digest);
115  xorbuf(BL, this->digest, this->S);
116 
117  this->hm.Update(KL, this->L);
118  this->hm.Update(OL, this->S);
119  this->hm.Final(this->digest);
120  xorbuf(BR, this->digest, this->S);
121 
122  if (xorBlock)
123  xorbuf(outBlock, xorBlock, this->buffer, 2*this->S);
124  else
125  memcpy(outBlock, this->buffer, 2*this->S);
126  }
127 #undef KL
128 #undef KR
129 #undef BL
130 #undef BR
131 #undef IL
132 #undef IR
133 #undef OL
134 #undef OR
135  };
136 
137 public:
140 };
141 
142 NAMESPACE_END
143 
144 #endif
Classes providing basic library services.
Luby-Rackoff block cipher information.
Definition: lubyrack.h:22
Provides Encryption and Decryption typedefs used by derived classes to implement a block cipher...
Definition: seckey.h:396
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
Definition: misc.h:301
SecBlock typedef.
Definition: secblock.h:730
Classes and functions for secure memory allocations.
Inherited by algorithms with fixed block size.
Definition: seckey.h:34
Inherited by keyed algorithms with variable key length.
Definition: seckey.h:157
void xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
Definition: misc.cpp:28
Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers.
Definition: seckey.h:303
Crypto++ library namespace.
Luby-Rackoff block cipher.
Definition: lubyrack.h:30
Interface for retrieving values given their names.
Definition: cryptlib.h:277