avoid SecBlock of arrays

pull/2/head
weidai 2010-06-18 01:52:34 +00:00
parent 8af9520702
commit 03cfaa0e46
6 changed files with 45 additions and 42 deletions

View File

@ -46,10 +46,10 @@ const byte SKIPJACK::Base::fTable[256] = {
*/
#define g(tab, w, i, j, k, l) \
{ \
w ^= (word)tab[i][w & 0xff] << 8; \
w ^= (word)tab[j][w >> 8]; \
w ^= (word)tab[k][w & 0xff] << 8; \
w ^= (word)tab[l][w >> 8]; \
w ^= (word)tab[i*256 + (w & 0xff)] << 8; \
w ^= (word)tab[j*256 + (w >> 8)]; \
w ^= (word)tab[k*256 + (w & 0xff)] << 8; \
w ^= (word)tab[l*256 + (w >> 8)]; \
}
#define g0(tab, w) g(tab, w, 0, 1, 2, 3)
@ -63,10 +63,10 @@ const byte SKIPJACK::Base::fTable[256] = {
*/
#define h(tab, w, i, j, k, l) \
{ \
w ^= (word)tab[l][w >> 8]; \
w ^= (word)tab[k][w & 0xff] << 8; \
w ^= (word)tab[j][w >> 8]; \
w ^= (word)tab[i][w & 0xff] << 8; \
w ^= (word)tab[l*256 + (w >> 8)]; \
w ^= (word)tab[k*256 + (w & 0xff)] << 8; \
w ^= (word)tab[j*256 + (w >> 8)]; \
w ^= (word)tab[i*256 + (w & 0xff)] << 8; \
}
#define h0(tab, w) h(tab, w, 0, 1, 2, 3)
@ -85,7 +85,7 @@ void SKIPJACK::Base::UncheckedSetKey(const byte *key, unsigned int length, const
/* tab[i][c] = fTable[c ^ key[i]] */
int i;
for (i = 0; i < 10; i++) {
byte *t = tab[i], k = key[9-i];
byte *t = tab+i*256, k = key[9-i];
int c;
for (c = 0; c < 256; c++) {
t[c] = fTable[c ^ k];

View File

@ -27,7 +27,7 @@ class SKIPJACK : public SKIPJACK_Info, public BlockCipherDocumentation
protected:
static const byte fTable[256];
FixedSizeSecBlock<byte[256], 10> tab;
FixedSizeSecBlock<byte, 10*256> tab;
};
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Enc : public Base

View File

@ -31,6 +31,9 @@ static void SquareTransform (word32 in[4], word32 out[4])
}
}
#define roundkeys(i, j) m_roundkeys[(i)*4+(j)]
#define roundkeys4(i) (m_roundkeys+(i)*4)
void Square::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &)
{
AssertValidKeyLength(length);
@ -40,29 +43,29 @@ void Square::Base::UncheckedSetKey(const byte *userKey, unsigned int length, con
0x10000000UL, 0x20000000UL, 0x40000000UL, 0x80000000UL,
};
GetUserKey(BIG_ENDIAN_ORDER, roundkeys[0], KEYLENGTH/4, userKey, KEYLENGTH);
GetUserKey(BIG_ENDIAN_ORDER, m_roundkeys.data(), KEYLENGTH/4, userKey, KEYLENGTH);
/* apply the key evolution function */
for (int i = 1; i < ROUNDS+1; i++)
{
roundkeys[i][0] = roundkeys[i-1][0] ^ rotlFixed(roundkeys[i-1][3], 8U) ^ offset[i-1];
roundkeys[i][1] = roundkeys[i-1][1] ^ roundkeys[i][0];
roundkeys[i][2] = roundkeys[i-1][2] ^ roundkeys[i][1];
roundkeys[i][3] = roundkeys[i-1][3] ^ roundkeys[i][2];
roundkeys(i, 0) = roundkeys(i-1, 0) ^ rotlFixed(roundkeys(i-1, 3), 8U) ^ offset[i-1];
roundkeys(i, 1) = roundkeys(i-1, 1) ^ roundkeys(i, 0);
roundkeys(i, 2) = roundkeys(i-1, 2) ^ roundkeys(i, 1);
roundkeys(i, 3) = roundkeys(i-1, 3) ^ roundkeys(i, 2);
}
/* produce the round keys */
if (IsForwardTransformation())
{
for (int i = 0; i < ROUNDS; i++)
SquareTransform (roundkeys[i], roundkeys[i]);
SquareTransform (roundkeys4(i), roundkeys4(i));
}
else
{
for (int i = 0; i < ROUNDS/2; i++)
for (int j = 0; j < 4; j++)
std::swap(roundkeys[i][j], roundkeys[ROUNDS-i][j]);
SquareTransform (roundkeys[ROUNDS], roundkeys[ROUNDS]);
std::swap(roundkeys(i, j), roundkeys(ROUNDS-i, j));
SquareTransform (roundkeys4(ROUNDS), roundkeys4(ROUNDS));
}
}
@ -127,21 +130,21 @@ void Square::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock,
Block::Get(inBlock)(text[0])(text[1])(text[2])(text[3]);
/* initial key addition */
text[0] ^= roundkeys[0][0];
text[1] ^= roundkeys[0][1];
text[2] ^= roundkeys[0][2];
text[3] ^= roundkeys[0][3];
text[0] ^= roundkeys(0, 0);
text[1] ^= roundkeys(0, 1);
text[2] ^= roundkeys(0, 2);
text[3] ^= roundkeys(0, 3);
/* ROUNDS - 1 full rounds */
for (int i=1; i+1<ROUNDS; i+=2)
{
squareRound (text, temp, Te[0], Te[1], Te[2], Te[3], roundkeys[i]);
squareRound (temp, text, Te[0], Te[1], Te[2], Te[3], roundkeys[i+1]);
squareRound (text, temp, Te[0], Te[1], Te[2], Te[3], roundkeys4(i));
squareRound (temp, text, Te[0], Te[1], Te[2], Te[3], roundkeys4(i+1));
}
squareRound (text, temp, Te[0], Te[1], Te[2], Te[3], roundkeys[ROUNDS-1]);
squareRound (text, temp, Te[0], Te[1], Te[2], Te[3], roundkeys4(ROUNDS-1));
/* last round (diffusion becomes only transposition) */
squareFinal (text, temp, Se, roundkeys[ROUNDS]);
squareFinal (text, temp, Se, roundkeys4(ROUNDS));
Block::Put(xorBlock, outBlock)(text[0])(text[1])(text[2])(text[3]);
}
@ -152,21 +155,21 @@ void Square::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock,
Block::Get(inBlock)(text[0])(text[1])(text[2])(text[3]);
/* initial key addition */
text[0] ^= roundkeys[0][0];
text[1] ^= roundkeys[0][1];
text[2] ^= roundkeys[0][2];
text[3] ^= roundkeys[0][3];
text[0] ^= roundkeys(0, 0);
text[1] ^= roundkeys(0, 1);
text[2] ^= roundkeys(0, 2);
text[3] ^= roundkeys(0, 3);
/* ROUNDS - 1 full rounds */
for (int i=1; i+1<ROUNDS; i+=2)
{
squareRound (text, temp, Td[0], Td[1], Td[2], Td[3], roundkeys[i]);
squareRound (temp, text, Td[0], Td[1], Td[2], Td[3], roundkeys[i+1]);
squareRound (text, temp, Td[0], Td[1], Td[2], Td[3], roundkeys4(i));
squareRound (temp, text, Td[0], Td[1], Td[2], Td[3], roundkeys4(i+1));
}
squareRound (text, temp, Td[0], Td[1], Td[2], Td[3], roundkeys[ROUNDS-1]);
squareRound (text, temp, Td[0], Td[1], Td[2], Td[3], roundkeys4(ROUNDS-1));
/* last round (diffusion becomes only transposition) */
squareFinal (text, temp, Sd, roundkeys[ROUNDS]);
squareFinal (text, temp, Sd, roundkeys4(ROUNDS));
Block::Put(xorBlock, outBlock)(text[0])(text[1])(text[2])(text[3]);
}

View File

@ -24,7 +24,7 @@ class Square : public Square_Info, public BlockCipherDocumentation
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
protected:
FixedSizeSecBlock<word32[4], ROUNDS+1> roundkeys;
FixedSizeSecBlock<word32, 4*(ROUNDS+1)> m_roundkeys;
};
class CRYPTOPP_NO_VTABLE Enc : public Base

View File

@ -72,15 +72,15 @@ void Twofish::Base::UncheckedSetKey(const byte *userKey, unsigned int keylength,
for (i=0; i<256; i++)
{
word32 t = h0(i, svec, len);
m_s[0][i] = mds[0][GETBYTE(t, 0)];
m_s[1][i] = mds[1][GETBYTE(t, 1)];
m_s[2][i] = mds[2][GETBYTE(t, 2)];
m_s[3][i] = mds[3][GETBYTE(t, 3)];
m_s[0*256+i] = mds[0][GETBYTE(t, 0)];
m_s[1*256+i] = mds[1][GETBYTE(t, 1)];
m_s[2*256+i] = mds[2][GETBYTE(t, 2)];
m_s[3*256+i] = mds[3][GETBYTE(t, 3)];
}
}
#define G1(x) (m_s[0][GETBYTE(x,0)] ^ m_s[1][GETBYTE(x,1)] ^ m_s[2][GETBYTE(x,2)] ^ m_s[3][GETBYTE(x,3)])
#define G2(x) (m_s[0][GETBYTE(x,3)] ^ m_s[1][GETBYTE(x,0)] ^ m_s[2][GETBYTE(x,1)] ^ m_s[3][GETBYTE(x,2)])
#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)])
#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)])
#define ENCROUND(n, a, b, c, d) \
x = G1 (a); y = G2 (b); \

View File

@ -31,7 +31,7 @@ class Twofish : public Twofish_Info, public BlockCipherDocumentation
static const word32 mds[4][256];
FixedSizeSecBlock<word32, 40> m_k;
FixedSizeSecBlock<word32[256], 4> m_s;
FixedSizeSecBlock<word32, 4*256> m_s;
};
class CRYPTOPP_NO_VTABLE Enc : public Base