Fabcoin Core  0.16.2
P2P Digital Currency
wake.cpp
Go to the documentation of this file.
1 // wake.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 
5 #include "wake.h"
6 #include "smartptr.h"
7 
9 
10 #if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
11 void WAKE_TestInstantiations()
12 {
15 }
16 #endif
17 
19 {
20  word32 w = x+y;
21  return (w>>8) ^ t[w & 0xff];
22 }
23 
25 {
26  // this code is mostly copied from David Wheeler's paper "A Bulk Data Encryption Algorithm"
27  signed int x, z, p;
28  // x and z were declared as "long" in Wheeler's paper, which is a signed type. I don't know if that was intentional, but it's too late to change it now. -- Wei 7/4/2010
29  CRYPTOPP_COMPILE_ASSERT(sizeof(x) == 4);
30  static unsigned int tt[10]= {
31  0x726a8f3b, // table
32  0xe69a3b5c,
33  0xd3c71fe5,
34  0xab3c73d2,
35  0x4d3a8eb3,
36  0x0396d6e8,
37  0x3d4c2f7a,
38  0x9ee27cf3, } ;
39  t[0] = k0;
40  t[1] = k1;
41  t[2] = k2;
42  t[3] = k3;
43  for (p=4 ; p<256 ; p++)
44  {
45  x=t[p-4]+t[p-1] ; // fill t
46  t[p]= (x>>3) ^ tt[x&7] ;
47  }
48 
49  for (p=0 ; p<23 ; p++)
50  t[p]+=t[p+89] ; // mix first entries
51  x=t[33] ; z=t[59] | 0x01000001 ;
52  z=z&0xff7fffff ;
53  for (p=0 ; p<256 ; p++) { //change top byte to
54  x=(x&0xff7fffff)+z ; // a permutation etc
55  t[p]=(t[p] & 0x00ffffff) ^ x ; }
56 
57  t[256]=t[0] ;
58  byte y=byte(x);
59  for (p=0 ; p<256 ; p++) { // further change perm.
60  t[p]=t[y=byte(t[p^y]^y)] ; // and other digits
61  t[y]=t[p+1] ; }
62 }
63 
64 template <class B>
65 void WAKE_Policy<B>::CipherSetKey(const NameValuePairs &params, const byte *key, size_t length)
66 {
67  CRYPTOPP_UNUSED(params); CRYPTOPP_UNUSED(key); CRYPTOPP_UNUSED(length);
68  word32 k0, k1, k2, k3;
70  GenKey(k0, k1, k2, k3);
71 }
72 
73 // OFB
74 template <class B>
75 void WAKE_Policy<B>::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
76 {
77 #define WAKE_OUTPUT(x)\
78  while (iterationCount--)\
79  {\
80  CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, B::ToEnum(), 0, r6);\
81  r3 = M(r3, r6);\
82  r4 = M(r4, r3);\
83  r5 = M(r5, r4);\
84  r6 = M(r6, r5);\
85  output += 4;\
86  if (!(x & INPUT_NULL))\
87  input += 4;\
88  }
89 
90  typedef word32 WordType;
92 }
93 /*
94 template <class B>
95 void WAKE_ROFB_Policy<B>::Iterate(KeystreamOperation operation, byte *output, const byte *input, unsigned int iterationCount)
96 {
97  KeystreamOutput<B> keystreamOperation(operation, output, input);
98 
99  while (iterationCount--)
100  {
101  keystreamOperation(r6);
102  r3 = M(r3, r6);
103  r4 = M(r4, r3);
104  r5 = M(r5, r4);
105  r6 = M(r6, r5);
106  }
107 }
108 */
109 template class WAKE_Policy<BigEndian>;
110 template class WAKE_Policy<LittleEndian>;
111 //template class WAKE_ROFB_Policy<BigEndian>;
112 //template class WAKE_ROFB_Policy<LittleEndian>;
113 
word32 t[257]
Definition: wake.h:31
uint8_t byte
Definition: Common.h:57
#define k3
Definition: ripemd.cpp:21
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
static GetBlock< T, B, GA > Get(const void *block)
Definition: misc.h:2236
Classes for automatic resource management.
#define CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(x, y)
Helper macro to implement OperateKeystream.
Definition: strciphr.h:235
word32 r4
Definition: wake.h:32
void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length)
Key the cipher.
Definition: wake.cpp:65
word32 r5
Definition: wake.h:32
#define x(i)
#define CRYPTOPP_COMPILE_ASSERT(assertion)
Definition: misc.h:139
word32 M(word32 x, word32 y)
Definition: wake.cpp:18
void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
Operates the keystream.
Definition: wake.cpp:75
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode...
Definition: cryptlib.h:1103
#define k0
Definition: ripemd.cpp:18
word32 r6
Definition: wake.h:32
WAKE stream cipher operation.
Definition: wake.h:40
Classes for WAKE stream cipher.
#define WAKE_OUTPUT(x)
#define CRYPTOPP_UNUSED(x)
Definition: config.h:741
#define k2
Definition: ripemd.cpp:20
uint8_t byte
Definition: Common.h:10
void GenKey(word32 k0, word32 k1, word32 k2, word32 k3)
Definition: wake.cpp:24
#define k1
Definition: ripemd.cpp:19
#define NAMESPACE_END
Definition: config.h:201
KeystreamOperation
Keystream operation flags.
Definition: strciphr.h:90
SymmetricCipher implementation.
Definition: strciphr.h:584
#define z(i)
unsigned int word32
Definition: config.h:231
word32 r3
Definition: wake.h:32
Interface for retrieving values given their names.
Definition: cryptlib.h:279