From 03cfaa0e4614c6cc66ffd7c473a853597fd79919 Mon Sep 17 00:00:00 2001 From: weidai Date: Fri, 18 Jun 2010 01:52:34 +0000 Subject: [PATCH] avoid SecBlock of arrays --- skipjack.cpp | 18 +++++++++--------- skipjack.h | 2 +- square.cpp | 51 +++++++++++++++++++++++++++------------------------ square.h | 2 +- twofish.cpp | 12 ++++++------ twofish.h | 2 +- 6 files changed, 45 insertions(+), 42 deletions(-) diff --git a/skipjack.cpp b/skipjack.cpp index e9ec5866..dad14bd7 100644 --- a/skipjack.cpp +++ b/skipjack.cpp @@ -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]; diff --git a/skipjack.h b/skipjack.h index 2cf10d22..6b126473 100644 --- a/skipjack.h +++ b/skipjack.h @@ -27,7 +27,7 @@ class SKIPJACK : public SKIPJACK_Info, public BlockCipherDocumentation protected: static const byte fTable[256]; - FixedSizeSecBlock tab; + FixedSizeSecBlock tab; }; class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Enc : public Base diff --git a/square.cpp b/square.cpp index 3686eac2..00e6bddb 100644 --- a/square.cpp +++ b/square.cpp @@ -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 roundkeys; + FixedSizeSecBlock m_roundkeys; }; class CRYPTOPP_NO_VTABLE Enc : public Base diff --git a/twofish.cpp b/twofish.cpp index e78258d3..064f16c4 100644 --- a/twofish.cpp +++ b/twofish.cpp @@ -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); \ diff --git a/twofish.h b/twofish.h index 969fdb2e..9ba2903b 100644 --- a/twofish.h +++ b/twofish.h @@ -31,7 +31,7 @@ class Twofish : public Twofish_Info, public BlockCipherDocumentation static const word32 mds[4][256]; FixedSizeSecBlock m_k; - FixedSizeSecBlock m_s; + FixedSizeSecBlock m_s; }; class CRYPTOPP_NO_VTABLE Enc : public Base