Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

3way.cpp

00001 // 3way.cpp - modifed by Wei Dai from Joan Daemen's 3way.c 00002 // The original code and all modifications are in the public domain. 00003 00004 #include "pch.h" 00005 #include "3way.h" 00006 #include "misc.h" 00007 00008 NAMESPACE_BEGIN(CryptoPP) 00009 00010 void ThreeWay_TestInstantiations() 00011 { 00012 ThreeWay::Encryption x1; 00013 ThreeWay::Decryption x2; 00014 } 00015 00016 static const word32 START_E = 0x0b0b; // round constant of first encryption round 00017 static const word32 START_D = 0xb1b1; // round constant of first decryption round 00018 static const word32 RC_MODULUS = 0x11011; 00019 00020 static inline word32 reverseBits(word32 a) 00021 { 00022 a = ((a & 0xAAAAAAAA) >> 1) | ((a & 0x55555555) << 1); 00023 a = ((a & 0xCCCCCCCC) >> 2) | ((a & 0x33333333) << 2); 00024 return ((a & 0xF0F0F0F0) >> 4) | ((a & 0x0F0F0F0F) << 4); 00025 } 00026 00027 #define mu(a0, a1, a2) \ 00028 { \ 00029 a1 = reverseBits(a1); \ 00030 word32 t = reverseBits(a0); \ 00031 a0 = reverseBits(a2); \ 00032 a2 = t; \ 00033 } 00034 00035 #define pi_gamma_pi(a0, a1, a2) \ 00036 { \ 00037 word32 b0, b2; \ 00038 b2 = rotlFixed(a2, 1U); \ 00039 b0 = rotlFixed(a0, 22U); \ 00040 a0 = rotlFixed(b0 ^ (a1|(~b2)), 1U); \ 00041 a2 = rotlFixed(b2 ^ (b0|(~a1)), 22U);\ 00042 a1 ^= (b2|(~b0)); \ 00043 } 00044 00045 // thanks to Paulo Barreto for this optimized theta() 00046 #define theta(a0, a1, a2) \ 00047 { \ 00048 word32 b0, b1, c; \ 00049 c = a0 ^ a1 ^ a2; \ 00050 c = rotlFixed(c, 16U) ^ rotlFixed(c, 8U); \ 00051 b0 = (a0 << 24) ^ (a2 >> 8) ^ (a1 << 8) ^ (a0 >> 24); \ 00052 b1 = (a1 << 24) ^ (a0 >> 8) ^ (a2 << 8) ^ (a1 >> 24); \ 00053 a0 ^= c ^ b0; \ 00054 a1 ^= c ^ b1; \ 00055 a2 ^= c ^ (b0 >> 16) ^ (b1 << 16); \ 00056 } 00057 00058 #define rho(a0, a1, a2) \ 00059 { \ 00060 theta(a0, a1, a2); \ 00061 pi_gamma_pi(a0, a1, a2); \ 00062 } 00063 00064 void ThreeWay::Base::UncheckedSetKey(CipherDir dir, const byte *uk, unsigned int length, unsigned int r) 00065 { 00066 AssertValidKeyLength(length); 00067 AssertValidRounds(r); 00068 00069 m_rounds = r; 00070 00071 for (unsigned int i=0; i<3; i++) 00072 m_k[i] = (word32)uk[4*i+3] | ((word32)uk[4*i+2]<<8) | ((word32)uk[4*i+1]<<16) | ((word32)uk[4*i]<<24); 00073 00074 if (dir == DECRYPTION) 00075 { 00076 theta(m_k[0], m_k[1], m_k[2]); 00077 mu(m_k[0], m_k[1], m_k[2]); 00078 m_k[0] = ByteReverse(m_k[0]); 00079 m_k[1] = ByteReverse(m_k[1]); 00080 m_k[2] = ByteReverse(m_k[2]); 00081 } 00082 } 00083 00084 void ThreeWay::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const 00085 { 00086 typedef BlockGetAndPut<word32, BigEndian> Block; 00087 00088 word32 a0, a1, a2; 00089 Block::Get(inBlock)(a0)(a1)(a2); 00090 00091 word32 rc = START_E; 00092 00093 for(unsigned i=0; i<m_rounds; i++) 00094 { 00095 a0 ^= m_k[0] ^ (rc<<16); 00096 a1 ^= m_k[1]; 00097 a2 ^= m_k[2] ^ rc; 00098 rho(a0, a1, a2); 00099 00100 rc <<= 1; 00101 if (rc&0x10000) rc ^= 0x11011; 00102 } 00103 a0 ^= m_k[0] ^ (rc<<16); 00104 a1 ^= m_k[1]; 00105 a2 ^= m_k[2] ^ rc; 00106 theta(a0, a1, a2); 00107 00108 Block::Put(xorBlock, outBlock)(a0)(a1)(a2); 00109 } 00110 00111 void ThreeWay::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const 00112 { 00113 typedef BlockGetAndPut<word32, LittleEndian> Block; 00114 00115 word32 a0, a1, a2; 00116 Block::Get(inBlock)(a0)(a1)(a2); 00117 00118 word32 rc = START_D; 00119 00120 mu(a0, a1, a2); 00121 for(unsigned i=0; i<m_rounds; i++) 00122 { 00123 a0 ^= m_k[0] ^ (rc<<16); 00124 a1 ^= m_k[1]; 00125 a2 ^= m_k[2] ^ rc; 00126 rho(a0, a1, a2); 00127 00128 rc <<= 1; 00129 if (rc&0x10000) rc ^= 0x11011; 00130 } 00131 a0 ^= m_k[0] ^ (rc<<16); 00132 a1 ^= m_k[1]; 00133 a2 ^= m_k[2] ^ rc; 00134 theta(a0, a1, a2); 00135 mu(a0, a1, a2); 00136 00137 Block::Put(xorBlock, outBlock)(a0)(a1)(a2); 00138 } 00139 00140 NAMESPACE_END

Generated on Wed Jul 21 19:15:19 2004 for Crypto++ by doxygen 1.3.7-20040704