13 static inline
unsigned int Mod(
unsigned int c)
15 static const unsigned int modulus = 0x14d;
16 unsigned int c2 = (c<<1) ^ ((c & 0x80) ? modulus : 0);
17 unsigned int c1 = c2 ^ (c>>1) ^ ((c & 1) ? (modulus>>1) : 0);
18 return c | (c1 << 8) | (c2 << 16) | (c1 << 24);
25 for (
unsigned int i=0; i<8; i++)
27 high = Mod(high>>24) ^ (high<<8) ^ (low>>24);
35 x = x | (x<<8) | (x<<16) | (x<<24);
38 #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) 39 case 4: x =
Q(1, 0, 0, 1, x) ^ key[6];
40 case 3: x =
Q(1, 1, 0, 0, x) ^ key[4];
41 case 2: x =
Q(0, 1, 0, 1, x) ^ key[2];
42 x =
Q(0, 0, 1, 1, x) ^ key[0];
57 unsigned int len = (keylength <= 16 ? 2 : (keylength <= 24 ? 3 : 4));
72 svec[2*(len-i-1)] = ReedSolomon(key[2*i+1], key[2*i]);
83 #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)]) 84 #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)]) 86 #define ENCROUND(n, a, b, c, d) \ 87 x = G1 (a); y = G2 (b); \ 88 x += y; y += x + k[2 * (n) + 1]; \ 89 (c) ^= x + k[2 * (n)]; \ 90 (c) = rotrFixed(c, 1); \ 91 (d) = rotlFixed(d, 1) ^ y 94 ENCROUND (2 * (n), a, b, c, d); \ 95 ENCROUND (2 * (n) + 1, c, d, a, b) 97 #define DECROUND(n, a, b, c, d) \ 98 x = G1 (a); y = G2 (b); \ 100 (d) ^= y + k[2 * (n) + 1]; \ 101 (d) = rotrFixed(d, 1); \ 102 (c) = rotlFixed(c, 1); \ 103 (c) ^= (x + k[2 * (n)]) 105 #define DECCYCLE(n) \ 106 DECROUND (2 * (n) + 1, c, d, a, b); \ 107 DECROUND (2 * (n), a, b, c, d) static const word32 mds[4][256]
void GetUserKey(ByteOrder order, T *out, size_t outlen, const byte *in, size_t inlen)
FixedSizeSecBlock< word32, 4 *256 > m_s
Utility functions for the Crypto++ library.
T rotlFixed(T x, unsigned int y)
Performs a left rotate.
#define NAMESPACE_BEGIN(x)
static GetBlock< T, B, GA > Get(const void *block)
FixedSizeSecBlock< word32, 40 > m_k
Access a block of memory.
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
Encrypt or decrypt a block.
byte order is little-endian
Classes for the Twofish block cipher.
BlockGetAndPut< word32, LittleEndian > Block
static word32 h(word32 x, const word32 *key, unsigned int kLen)
Classes and functions for secure memory allocations.
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs ¶ms)
Sets the key for this object without performing parameter validation.
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
Encrypt or decrypt a block.
void AssertValidKeyLength(size_t length) const
Validates the key length.
iterator begin()
Provides an iterator pointing to the first element in the memory block.
static word32 h0(word32 x, const word32 *key, unsigned int kLen)
Interface for retrieving values given their names.