- Main Page
- Namespaces
- Classes
- Files

# wake.cpp

    00001 // wake.cpp - written and placed in the public domain by Wei Dai
    00002
    00003 #include "pch.h"
    00004 #include "wake.h"
    00005
    00006 NAMESPACE_BEGIN(CryptoPP)
    00007
    00008 void WAKE_TestInstantiations()
    00009 {
    00010         Weak::WAKE_CFB<>::Encryption x1;
    00011         Weak::WAKE_CFB<>::Decryption x3;
    00012         WAKE_OFB<>::Encryption x2;
    00013         WAKE_OFB<>::Decryption x4;
    00014 }
    00015
    00016 inline word32 WAKE_Base::M(word32 x, word32 y)
    00017 {
    00018         word32 w = x+y;
    00019         return (w>>8) ^ t[(byte)w];
    00020 }
    00021
    00022 void WAKE_Base::GenKey(word32 k0, word32 k1, word32 k2, word32 k3)
    00023 {
    00024         long x, z;
    00025         int p ;
    00026         static long tt[10]= {
    00027                 0x726a8f3bL,                                                             // table
    00028                 0xe69a3b5cL,
    00029                 0xd3c71fe5L,
    00030                 0xab3c73d2L,
    00031                 0x4d3a8eb3L,
    00032                 0x0396d6e8L,
    00033                 0x3d4c2f7aL,
    00034                 0x9ee27cf3L, } ;
    00035         t[0] = k0;
    00036         t[1] = k1;
    00037         t[2] = k2;
    00038         t[3] = k3;
    00039         for (p=4 ; p<256 ; p++)
    00040         {
    00041           x=t[p-4]+t[p-1] ;                                        // fill t
    00042           t[p]= (x>>3) ^ tt[byte(x&7)] ;
    00043         }
    00044
    00045         for (p=0 ; p<23 ; p++)
    00046                 t[p]+=t[p+89] ;                   // mix first entries
    00047         x=t[33] ; z=t[59] | 0x01000001L ;
    00048         z=z&0xff7fffffL ;
    00049         for (p=0 ; p<256 ; p++) {               //change top byte to
    00050           x=(x&0xff7fffffL)+z ;                  // a permutation etc
    00051           t[p]=(t[p] & 0x00ffffffL) ^ x ; }
    00052
    00053         t[256]=t[0] ;
    00054         byte y=byte(x);
    00055         for (p=0 ; p<256 ; p++) {         // further change perm.
    00056           t[p]=t[y=byte(t[p^y]^y)] ;  // and other digits
    00057           t[y]=t[p+1] ;  }
    00058 }
    00059
    00060 template <class B>
    00061 void WAKE_Policy<B>::CipherSetKey(const NameValuePairs &params, const byte *key, size_t length)
    00062 {
    00063         word32 k0, k1, k2, k3;
    00064         BlockGetAndPut<word32, BigEndian>::Get(key)(r3)(r4)(r5)(r6)(k0)(k1)(k2)(k3);
    00065         GenKey(k0, k1, k2, k3);
    00066 }
    00067
    00068 // CFB
    00069 template <class B>
    00070 void WAKE_Policy<B>::Iterate(byte *output, const byte *input, CipherDir dir, size_t iterationCount)
    00071 {
    00072         RegisterOutput<B> registerOutput(output, input, dir);
    00073
    00074         while (iterationCount--)
    00075         {
    00076                 r3 = M(r3, ConditionalByteReverse(B::ToEnum(), r6));
    00077                 r4 = M(r4, r3);
    00078                 r5 = M(r5, r4);
    00079                 r6 = M(r6, r5);
    00080                 registerOutput(r6);
    00081         }
    00082 }
    00083
    00084 // OFB
    00085 template <class B>
    00086 void WAKE_Policy<B>::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
    00087 {
    00088 #define WAKE_OUTPUT(x)\
    00089         while (iterationCount--)\
    00090         {\
    00091                 CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, B::ToEnum(), 0, r6);\
    00092                 r3 = M(r3, r6);\
    00093                 r4 = M(r4, r3);\
    00094                 r5 = M(r5, r4);\
    00095                 r6 = M(r6, r5);\
    00096                 output += 4;\
    00097                 if (x == XOR_KEYSTREAM)\
    00098                         input += 4;\
    00099         }
    00100
    00101         typedef word32 WordType;
    00102         CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(WAKE_OUTPUT, 0);
    00103 }
    00104 /*
    00105 template <class B>
    00106 void WAKE_ROFB_Policy<B>::Iterate(KeystreamOperation operation, byte *output, const byte *input, unsigned int iterationCount)
    00107 {
    00108         KeystreamOutput<B> keystreamOperation(operation, output, input);
    00109
    00110         while (iterationCount--)
    00111         {
    00112                 keystreamOperation(r6);
    00113                 r3 = M(r3, r6);
    00114                 r4 = M(r4, r3);
    00115                 r5 = M(r5, r4);
    00116                 r6 = M(r6, r5);
    00117         }
    00118 }
    00119 */
    00120 template class WAKE_Policy<BigEndian>;
    00121 template class WAKE_Policy<LittleEndian>;
    00122 //template class WAKE_ROFB_Policy<BigEndian>;
    00123 //template class WAKE_ROFB_Policy<LittleEndian>;
    00124
    00125 NAMESPACE_END