summaryrefslogtreecommitdiff
path: root/square.cpp
diff options
context:
space:
mode:
authorweidai <weidai11@users.noreply.github.com>2010-06-18 01:52:34 +0000
committerweidai <weidai11@users.noreply.github.com>2010-06-18 01:52:34 +0000
commit03cfaa0e4614c6cc66ffd7c473a853597fd79919 (patch)
tree4eed69d5867429e79628ab0c386092e5eb9ec1d7 /square.cpp
parent8af9520702f19e037458d3059ee0fafef2643875 (diff)
downloadcryptopp-git-03cfaa0e4614c6cc66ffd7c473a853597fd79919.tar.gz
avoid SecBlock of arrays
Diffstat (limited to 'square.cpp')
-rw-r--r--square.cpp51
1 files changed, 27 insertions, 24 deletions
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<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]);
}