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