Crypto++  5.6.3
Free C++ class library of cryptographic schemes
wake.cpp
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 
8 NAMESPACE_BEGIN(CryptoPP)
9 
10 #if !defined(NDEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
11 void WAKE_TestInstantiations()
12 {
15 }
16 #endif
17 
18 inline word32 WAKE_Base::M(word32 x, word32 y)
19 {
20  word32 w = x+y;
21  return (w>>8) ^ t[w & 0xff];
22 }
23 
24 void WAKE_Base::GenKey(word32 k0, word32 k1, word32 k2, word32 k3)
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;
69  BlockGetAndPut<word32, BigEndian>::Get(key)(r3)(r4)(r5)(r6)(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;
91  CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(WAKE_OUTPUT, 0);
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 
114 NAMESPACE_END
Classes for automatic resource management.
#define CRYPTOPP_COMPILE_ASSERT(expr)
Compile time assertion.
Definition: misc.h:102
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode...
Definition: cryptlib.h:1089
Classes for WAKE stream cipher.
Crypto++ library namespace.
Interface for retrieving values given their names.
Definition: cryptlib.h:277