Crypto++
wake.cpp
1 // wake.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 #include "wake.h"
5 
6 NAMESPACE_BEGIN(CryptoPP)
7 
8 void WAKE_TestInstantiations()
9 {
12 }
13 
14 inline word32 WAKE_Base::M(word32 x, word32 y)
15 {
16  word32 w = x+y;
17  return (w>>8) ^ t[w & 0xff];
18 }
19 
20 void WAKE_Base::GenKey(word32 k0, word32 k1, word32 k2, word32 k3)
21 {
22  // this code is mostly copied from David Wheeler's paper "A Bulk Data Encryption Algorithm"
23  signed int x, z, p;
24  // 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
25  CRYPTOPP_COMPILE_ASSERT(sizeof(x) == 4);
26  static int tt[10]= {
27  0x726a8f3b, // table
28  0xe69a3b5c,
29  0xd3c71fe5,
30  0xab3c73d2,
31  0x4d3a8eb3,
32  0x0396d6e8,
33  0x3d4c2f7a,
34  0x9ee27cf3, } ;
35  t[0] = k0;
36  t[1] = k1;
37  t[2] = k2;
38  t[3] = k3;
39  for (p=4 ; p<256 ; p++)
40  {
41  x=t[p-4]+t[p-1] ; // fill t
42  t[p]= (x>>3) ^ tt[x&7] ;
43  }
44 
45  for (p=0 ; p<23 ; p++)
46  t[p]+=t[p+89] ; // mix first entries
47  x=t[33] ; z=t[59] | 0x01000001 ;
48  z=z&0xff7fffff ;
49  for (p=0 ; p<256 ; p++) { //change top byte to
50  x=(x&0xff7fffff)+z ; // a permutation etc
51  t[p]=(t[p] & 0x00ffffff) ^ x ; }
52 
53  t[256]=t[0] ;
54  byte y=byte(x);
55  for (p=0 ; p<256 ; p++) { // further change perm.
56  t[p]=t[y=byte(t[p^y]^y)] ; // and other digits
57  t[y]=t[p+1] ; }
58 }
59 
60 template <class B>
61 void WAKE_Policy<B>::CipherSetKey(const NameValuePairs &params, const byte *key, size_t length)
62 {
63  word32 k0, k1, k2, k3;
64  BlockGetAndPut<word32, BigEndian>::Get(key)(r3)(r4)(r5)(r6)(k0)(k1)(k2)(k3);
65  GenKey(k0, k1, k2, k3);
66 }
67 
68 // OFB
69 template <class B>
70 void WAKE_Policy<B>::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
71 {
72 #define WAKE_OUTPUT(x)\
73  while (iterationCount--)\
74  {\
75  CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, B::ToEnum(), 0, r6);\
76  r3 = M(r3, r6);\
77  r4 = M(r4, r3);\
78  r5 = M(r5, r4);\
79  r6 = M(r6, r5);\
80  output += 4;\
81  if (!(x & INPUT_NULL))\
82  input += 4;\
83  }
84 
85  typedef word32 WordType;
86  CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(WAKE_OUTPUT, 0);
87 }
88 /*
89 template <class B>
90 void WAKE_ROFB_Policy<B>::Iterate(KeystreamOperation operation, byte *output, const byte *input, unsigned int iterationCount)
91 {
92  KeystreamOutput<B> keystreamOperation(operation, output, input);
93 
94  while (iterationCount--)
95  {
96  keystreamOperation(r6);
97  r3 = M(r3, r6);
98  r4 = M(r4, r3);
99  r5 = M(r5, r4);
100  r6 = M(r6, r5);
101  }
102 }
103 */
104 template class WAKE_Policy<BigEndian>;
105 template class WAKE_Policy<LittleEndian>;
106 //template class WAKE_ROFB_Policy<BigEndian>;
107 //template class WAKE_ROFB_Policy<LittleEndian>;
108 
109 NAMESPACE_END
interface for one direction (encryption or decryption) of a stream cipher or cipher mode ...
Definition: cryptlib.h:611
interface for retrieving values given their names
Definition: cryptlib.h:225