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