00001
00002
00003
00004 #include "pch.h"
00005 #include "twofish.h"
00006 #include "secblock.h"
00007 #include "misc.h"
00008
00009 NAMESPACE_BEGIN(CryptoPP)
00010
00011
00012
00013 static inline unsigned int Mod(unsigned int c)
00014 {
00015 static const unsigned int modulus = 0x14d;
00016 unsigned int c2 = (c<<1) ^ ((c & 0x80) ? modulus : 0);
00017 unsigned int c1 = c2 ^ (c>>1) ^ ((c & 1) ? (modulus>>1) : 0);
00018 return c | (c1 << 8) | (c2 << 16) | (c1 << 24);
00019 }
00020
00021
00022
00023 static word32 ReedSolomon(word32 high, word32 low)
00024 {
00025 for (unsigned int i=0; i<8; i++)
00026 {
00027 high = Mod(high>>24) ^ (high<<8) ^ (low>>24);
00028 low <<= 8;
00029 }
00030 return high;
00031 }
00032
00033 inline word32 Twofish::Base::h0(word32 x, const word32 *key, unsigned int kLen)
00034 {
00035 x = x | (x<<8) | (x<<16) | (x<<24);
00036 switch(kLen)
00037 {
00038 #define Q(a, b, c, d, t) q[a][GETBYTE(t,0)] ^ (q[b][GETBYTE(t,1)] << 8) ^ (q[c][GETBYTE(t,2)] << 16) ^ (q[d][GETBYTE(t,3)] << 24)
00039 case 4: x = Q(1, 0, 0, 1, x) ^ key[6];
00040 case 3: x = Q(1, 1, 0, 0, x) ^ key[4];
00041 case 2: x = Q(0, 1, 0, 1, x) ^ key[2];
00042 x = Q(0, 0, 1, 1, x) ^ key[0];
00043 }
00044 return x;
00045 }
00046
00047 inline word32 Twofish::Base::h(word32 x, const word32 *key, unsigned int kLen)
00048 {
00049 x = h0(x, key, kLen);
00050 return mds[0][GETBYTE(x,0)] ^ mds[1][GETBYTE(x,1)] ^ mds[2][GETBYTE(x,2)] ^ mds[3][GETBYTE(x,3)];
00051 }
00052
00053 void Twofish::Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &)
00054 {
00055 AssertValidKeyLength(keylength);
00056
00057 unsigned int len = (keylength <= 16 ? 2 : (keylength <= 24 ? 3 : 4));
00058 SecBlock<word32> key(len*2);
00059 GetUserKey(LITTLE_ENDIAN_ORDER, key.begin(), len*2, userKey, keylength);
00060
00061 unsigned int i;
00062 for (i=0; i<40; i+=2)
00063 {
00064 word32 a = h(i, key, len);
00065 word32 b = rotlFixed(h(i+1, key+1, len), 8);
00066 m_k[i] = a+b;
00067 m_k[i+1] = rotlFixed(a+2*b, 9);
00068 }
00069
00070 SecBlock<word32> svec(2*len);
00071 for (i=0; i<len; i++)
00072 svec[2*(len-i-1)] = ReedSolomon(key[2*i+1], key[2*i]);
00073 for (i=0; i<256; i++)
00074 {
00075 word32 t = h0(i, svec, len);
00076 m_s[0*256+i] = mds[0][GETBYTE(t, 0)];
00077 m_s[1*256+i] = mds[1][GETBYTE(t, 1)];
00078 m_s[2*256+i] = mds[2][GETBYTE(t, 2)];
00079 m_s[3*256+i] = mds[3][GETBYTE(t, 3)];
00080 }
00081 }
00082
00083 #define G1(x) (m_s[0*256+GETBYTE(x,0)] ^ m_s[1*256+GETBYTE(x,1)] ^ m_s[2*256+GETBYTE(x,2)] ^ m_s[3*256+GETBYTE(x,3)])
00084 #define G2(x) (m_s[0*256+GETBYTE(x,3)] ^ m_s[1*256+GETBYTE(x,0)] ^ m_s[2*256+GETBYTE(x,1)] ^ m_s[3*256+GETBYTE(x,2)])
00085
00086 #define ENCROUND(n, a, b, c, d) \
00087 x = G1 (a); y = G2 (b); \
00088 x += y; y += x + k[2 * (n) + 1]; \
00089 (c) ^= x + k[2 * (n)]; \
00090 (c) = rotrFixed(c, 1); \
00091 (d) = rotlFixed(d, 1) ^ y
00092
00093 #define ENCCYCLE(n) \
00094 ENCROUND (2 * (n), a, b, c, d); \
00095 ENCROUND (2 * (n) + 1, c, d, a, b)
00096
00097 #define DECROUND(n, a, b, c, d) \
00098 x = G1 (a); y = G2 (b); \
00099 x += y; y += x; \
00100 (d) ^= y + k[2 * (n) + 1]; \
00101 (d) = rotrFixed(d, 1); \
00102 (c) = rotlFixed(c, 1); \
00103 (c) ^= (x + k[2 * (n)])
00104
00105 #define DECCYCLE(n) \
00106 DECROUND (2 * (n) + 1, c, d, a, b); \
00107 DECROUND (2 * (n), a, b, c, d)
00108
00109 typedef BlockGetAndPut<word32, LittleEndian> Block;
00110
00111 void Twofish::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
00112 {
00113 word32 x, y, a, b, c, d;
00114
00115 Block::Get(inBlock)(a)(b)(c)(d);
00116
00117 a ^= m_k[0];
00118 b ^= m_k[1];
00119 c ^= m_k[2];
00120 d ^= m_k[3];
00121
00122 const word32 *k = m_k+8;
00123 ENCCYCLE (0);
00124 ENCCYCLE (1);
00125 ENCCYCLE (2);
00126 ENCCYCLE (3);
00127 ENCCYCLE (4);
00128 ENCCYCLE (5);
00129 ENCCYCLE (6);
00130 ENCCYCLE (7);
00131
00132 c ^= m_k[4];
00133 d ^= m_k[5];
00134 a ^= m_k[6];
00135 b ^= m_k[7];
00136
00137 Block::Put(xorBlock, outBlock)(c)(d)(a)(b);
00138 }
00139
00140 void Twofish::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
00141 {
00142 word32 x, y, a, b, c, d;
00143
00144 Block::Get(inBlock)(c)(d)(a)(b);
00145
00146 c ^= m_k[4];
00147 d ^= m_k[5];
00148 a ^= m_k[6];
00149 b ^= m_k[7];
00150
00151 const word32 *k = m_k+8;
00152 DECCYCLE (7);
00153 DECCYCLE (6);
00154 DECCYCLE (5);
00155 DECCYCLE (4);
00156 DECCYCLE (3);
00157 DECCYCLE (2);
00158 DECCYCLE (1);
00159 DECCYCLE (0);
00160
00161 a ^= m_k[0];
00162 b ^= m_k[1];
00163 c ^= m_k[2];
00164 d ^= m_k[3];
00165
00166 Block::Put(xorBlock, outBlock)(a)(b)(c)(d);
00167 }
00168
00169 NAMESPACE_END