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         WAKE_CFB<>::Encryption x1;
00011         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, false>::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         KeystreamOutput<B> keystreamOperation(operation, output, input);
00089 
00090         while (iterationCount--)
00091         {
00092                 keystreamOperation(r6);
00093                 r3 = M(r3, r6);
00094                 r4 = M(r4, r3);
00095                 r5 = M(r5, r4);
00096                 r6 = M(r6, r5);
00097         }
00098 }
00099 /*
00100 template <class B>
00101 void WAKE_ROFB_Policy<B>::Iterate(KeystreamOperation operation, byte *output, const byte *input, unsigned int iterationCount)
00102 {
00103         KeystreamOutput<B> keystreamOperation(operation, output, input);
00104 
00105         while (iterationCount--)
00106         {
00107                 keystreamOperation(r6);
00108                 r3 = M(r3, r6);
00109                 r4 = M(r4, r3);
00110                 r5 = M(r5, r4);
00111                 r6 = M(r6, r5);
00112         }
00113 }
00114 */
00115 template class WAKE_Policy<BigEndian>;
00116 template class WAKE_Policy<LittleEndian>;
00117 //template class WAKE_ROFB_Policy<BigEndian>;
00118 //template class WAKE_ROFB_Policy<LittleEndian>;
00119 
00120 NAMESPACE_END

Generated on Sat Dec 23 02:07:11 2006 for Crypto++ by  doxygen 1.5.1-p1