Add additional jumbling-up in feistel rounds

This commit is contained in:
Leonetienne 2022-05-26 02:44:22 +02:00
parent bc3dae96a3
commit 101a1e0fd6
No known key found for this signature in database
GPG Key ID: C33879CD92E9708C
2 changed files with 37 additions and 13 deletions

View File

@ -32,7 +32,7 @@ namespace Leonetienne::GCrypt {
private:
//! Will run the feistel rounds, with either regular key
//! order or reversed key order
Block Run(const Block& data, bool reverseKeys);
Block Run(const Block& data, bool modeEncrypt);
//! Arbitrary cipher function
static Halfblock F(Halfblock m, const Key& key);

View File

@ -30,7 +30,7 @@ namespace Leonetienne::GCrypt {
return Run(data, true);
}
Block Feistel::Run(const Block& data, bool reverseKeys) {
Block Feistel::Run(const Block& data, bool modeEncrypt) {
const auto splitData = FeistelSplit(data);
Halfblock l = splitData.first;
Halfblock r = splitData.second;
@ -38,19 +38,43 @@ namespace Leonetienne::GCrypt {
Halfblock tmp;
for (std::size_t i = 0; i < N_ROUNDS; i++) {
// Calculate key index
std::size_t keyIndex;
if (reverseKeys) {
keyIndex = N_ROUNDS - i - 1;
}
else {
keyIndex = i;
}
// Encryption
if (modeEncrypt) {
const std::size_t keyIndex = i;
// Do a feistel round
tmp = r;
r = l ^ F(r, roundKeys[keyIndex]);
l = tmp;
// Jumble it up a bit more
l.ShiftRowsUpInplace();
l.ShiftCellsRightInplace();
l.ShiftBitsLeftInplace();
l.ShiftColumnsLeftInplace();
// Seal all these operations with a key
l += ReductionFunction(roundKeys[keyIndex]);
}
// Decryption
else {
// Decryption needs keys in reverse order
const std::size_t keyIndex = N_ROUNDS - i - 1;
// Unjumble the jumble
r -= ReductionFunction(roundKeys[keyIndex]);
r.ShiftColumnsRightInplace();
r.ShiftBitsRightInplace();
r.ShiftCellsLeftInplace();
r.ShiftRowsDownInplace();
// Do a feistel round
tmp = r;
r = l ^ F(r, roundKeys[keyIndex]);
l = tmp;
}
}
// Block has finished de*ciphering.