Crypto++ 8.6 Free C++ class library of cryptographic schemes

wake.cpp
1 // wake.cpp - originally 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 ANONYMOUS_NAMESPACE_BEGIN
9
10 const unsigned int TT[8]= {
11 0x726a8f3b, 0xe69a3b5c, 0xd3c71fe5, 0xab3c73d2,
12 0x4d3a8eb3, 0x0396d6e8, 0x3d4c2f7a, 0x9ee27cf3
13 } ;
14
15 ANONYMOUS_NAMESPACE_END
16
17 NAMESPACE_BEGIN(CryptoPP)
18
19 #if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
20 void WAKE_TestInstantiations()
21 {
22 WAKE_OFB<>::Encryption x2;
23 WAKE_OFB<>::Decryption x4;
24 }
25 #endif
26
27 inline word32 WAKE_Base::M(word32 x, word32 y)
28 {
29 word32 w = x+y;
30 return (w>>8) ^ t[w & 0xff];
31 }
32
33 void WAKE_Base::GenKey(word32 k0, word32 k1, word32 k2, word32 k3)
34 {
35 // this code is mostly copied from David Wheeler's paper "A Bulk Data Encryption Algorithm"
36 signed int x, z, p;
37
38 t[0] = k0;
39 t[1] = k1;
40 t[2] = k2;
41 t[3] = k3;
42 for (p=4 ; p<256 ; p++)
43 {
44 x=t[p-4]+t[p-1] ; // fill t
45 t[p]= (x>>3) ^ TT[x&7] ;
46 }
47
48 for (p=0 ; p<23 ; p++)
49 t[p]+=t[p+89] ; // mix first entries
50 x=t[33] ; z=t[59] | 0x01000001 ;
51 z=z&0xff7fffff ;
52 for (p=0 ; p<256 ; p++) { //change top byte to
53 x=(x&0xff7fffff)+z ; // a permutation etc
54 t[p]=(t[p] & 0x00ffffff) ^ x ; }
55
56 t[256]=t[0] ;
57 byte y=byte(x);
58 for (p=0 ; p<256 ; p++) { // further change perm.
59 t[p]=t[y=byte(t[p^y]^y)] ; // and other digits
60 t[y]=t[p+1] ; }
61 }
62
63 template <class B>
64 void WAKE_Policy<B>::CipherSetKey(const NameValuePairs &params, const byte *key, size_t length)
65 {
66 CRYPTOPP_UNUSED(params); CRYPTOPP_UNUSED(key); CRYPTOPP_UNUSED(length);
67 word32 k0, k1, k2, k3;
68 BlockGetAndPut<word32, BigEndian>::Get(key)(r3)(r4)(r5)(r6)(k0)(k1)(k2)(k3);
69 GenKey(k0, k1, k2, k3);
70 }
71
72 // OFB
73 template <class B>
74 void WAKE_Policy<B>::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
75 {
76 #define WAKE_OUTPUT(x)\
77 while (iterationCount--)\
78 {\
79 CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, B::ToEnum(), 0, r6);\
80 r3 = M(r3, r6);\
81 r4 = M(r4, r3);\
82 r5 = M(r5, r4);\
83 r6 = M(r6, r5);\
84 output += 4;\
85 if (!(x & INPUT_NULL))\
86 input += 4;\
87 }
88
89 typedef word32 WordType;
90 CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(WAKE_OUTPUT, 0);
91 }
92 /*
93 template <class B>
94 void WAKE_ROFB_Policy<B>::Iterate(KeystreamOperation operation, byte *output, const byte *input, unsigned int iterationCount)
95 {
96 KeystreamOutput<B> keystreamOperation(operation, output, input);
97
98 while (iterationCount--)
99 {
100 keystreamOperation(r6);
101 r3 = M(r3, r6);
102 r4 = M(r4, r3);
103 r5 = M(r5, r4);
104 r6 = M(r6, r5);
105 }
106 }
107 */
108 template class WAKE_Policy<BigEndian>;
109 template class WAKE_Policy<LittleEndian>;
110
111 NAMESPACE_END
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:327
SymmetricCipherFinal
SymmetricCipher implementation.
Definition: strciphr.h:684
SymmetricCipher
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode.
Definition: cryptlib.h:1296
WAKE_Policy
WAKE stream cipher operation.
Definition: wake.h:39
byte
unsigned char byte
8-bit unsigned datatype
Definition: config_int.h:66
word32
unsigned int word32
32-bit unsigned datatype
Definition: config_int.h:72
CryptoPP
Crypto++ library namespace.
pch.h
Precompiled header file.
smartptr.h
Classes for automatic resource management.
CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH
#define CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(x, y)
Helper macro to implement OperateKeystream.
Definition: strciphr.h:266
KeystreamOperation
KeystreamOperation
Keystream operation flags.
Definition: strciphr.h:88
BlockGetAndPut
Access a block of memory.
Definition: misc.h:3046
wake.h
Classes for WAKE stream cipher.