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

Generated on Thu Mar 30 22:11:49 2006 for Crypto++ by  doxygen 1.4.6