Crypto++
|
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(const byte *uk, unsigned int length, const NameValuePairs ¶ms) 00065 { 00066 AssertValidKeyLength(length); 00067 00068 m_rounds = GetRoundsAndThrowIfInvalid(params, this); 00069 00070 for (unsigned int i=0; i<3; i++) 00071 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); 00072 00073 if (!IsForwardTransformation()) 00074 { 00075 theta(m_k[0], m_k[1], m_k[2]); 00076 mu(m_k[0], m_k[1], m_k[2]); 00077 m_k[0] = ByteReverse(m_k[0]); 00078 m_k[1] = ByteReverse(m_k[1]); 00079 m_k[2] = ByteReverse(m_k[2]); 00080 } 00081 } 00082 00083 void ThreeWay::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const 00084 { 00085 typedef BlockGetAndPut<word32, BigEndian> Block; 00086 00087 word32 a0, a1, a2; 00088 Block::Get(inBlock)(a0)(a1)(a2); 00089 00090 word32 rc = START_E; 00091 00092 for(unsigned i=0; i<m_rounds; i++) 00093 { 00094 a0 ^= m_k[0] ^ (rc<<16); 00095 a1 ^= m_k[1]; 00096 a2 ^= m_k[2] ^ rc; 00097 rho(a0, a1, a2); 00098 00099 rc <<= 1; 00100 if (rc&0x10000) rc ^= 0x11011; 00101 } 00102 a0 ^= m_k[0] ^ (rc<<16); 00103 a1 ^= m_k[1]; 00104 a2 ^= m_k[2] ^ rc; 00105 theta(a0, a1, a2); 00106 00107 Block::Put(xorBlock, outBlock)(a0)(a1)(a2); 00108 } 00109 00110 void ThreeWay::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const 00111 { 00112 typedef BlockGetAndPut<word32, LittleEndian> Block; 00113 00114 word32 a0, a1, a2; 00115 Block::Get(inBlock)(a0)(a1)(a2); 00116 00117 word32 rc = START_D; 00118 00119 mu(a0, a1, a2); 00120 for(unsigned i=0; i<m_rounds; i++) 00121 { 00122 a0 ^= m_k[0] ^ (rc<<16); 00123 a1 ^= m_k[1]; 00124 a2 ^= m_k[2] ^ rc; 00125 rho(a0, a1, a2); 00126 00127 rc <<= 1; 00128 if (rc&0x10000) rc ^= 0x11011; 00129 } 00130 a0 ^= m_k[0] ^ (rc<<16); 00131 a1 ^= m_k[1]; 00132 a2 ^= m_k[2] ^ rc; 00133 theta(a0, a1, a2); 00134 mu(a0, a1, a2); 00135 00136 Block::Put(xorBlock, outBlock)(a0)(a1)(a2); 00137 } 00138 00139 NAMESPACE_END