Fabcoin Core  0.16.2
P2P Digital Currency
lubyrack.h
Go to the documentation of this file.
1 // lubyrack.h - written and placed in the public domain by Wei Dai
2 
5 
6 #ifndef CRYPTOPP_LUBYRACK_H
7 #define CRYPTOPP_LUBYRACK_H
8 
9 #include "simple.h"
10 #include "secblock.h"
11 
13 
14 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 
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
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 
138 
139 #endif
#define KL
Definition: lubyrack.h:54
uint8_t byte
Definition: Common.h:57
#define OR
Definition: lubyrack.h:61
#define BL
Definition: lubyrack.h:56
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
#define T(i, x)
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
#define IR
Definition: lubyrack.h:59
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
Definition: misc.h:366
BlockCipherFinal< ENCRYPTION, Enc > Encryption
Definition: lubyrack.h:133
#define OL
Definition: lubyrack.h:60
BlockCipherFinal< DECRYPTION, Dec > Decryption
Definition: lubyrack.h:134
Classes and functions for secure memory allocations.
Inherited by algorithms with fixed block size.
Definition: seckey.h:40
#define BR
Definition: lubyrack.h:57
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
Encrypt or decrypt a block.
Definition: lubyrack.h:95
#define IL
Definition: lubyrack.h:58
#define KR
Definition: lubyrack.h:55
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
Encrypt or decrypt a block.
Definition: lubyrack.h:63
#define CRYPTOPP_CONSTANT(x)
Definition: config.h:540
void xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
Definition: misc.cpp:28
Inherited by keyed algorithms with variable key length.
Definition: seckey.h:169
#define CRYPTOPP_NO_VTABLE
Definition: config.h:369
void * memcpy(void *a, const void *b, size_t c)
static std::string StaticAlgorithmName()
Definition: lubyrack.h:19
#define NAMESPACE_END
Definition: config.h:201
Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers.
Definition: seckey.h:311
Luby-Rackoff block cipher.
Definition: lubyrack.h:25
#define S(a)
Definition: mars.cpp:50
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
Sets the key for this object without performing parameter validation.
Definition: lubyrack.h:31
Interface for retrieving values given their names.
Definition: cryptlib.h:279