Add additional jumbling-up in feistel rounds
This commit is contained in:
parent
bc3dae96a3
commit
101a1e0fd6
@ -32,7 +32,7 @@ namespace Leonetienne::GCrypt {
|
|||||||
private:
|
private:
|
||||||
//! Will run the feistel rounds, with either regular key
|
//! Will run the feistel rounds, with either regular key
|
||||||
//! order or reversed key order
|
//! order or reversed key order
|
||||||
Block Run(const Block& data, bool reverseKeys);
|
Block Run(const Block& data, bool modeEncrypt);
|
||||||
|
|
||||||
//! Arbitrary cipher function
|
//! Arbitrary cipher function
|
||||||
static Halfblock F(Halfblock m, const Key& key);
|
static Halfblock F(Halfblock m, const Key& key);
|
||||||
|
@ -30,7 +30,7 @@ namespace Leonetienne::GCrypt {
|
|||||||
return Run(data, true);
|
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);
|
const auto splitData = FeistelSplit(data);
|
||||||
Halfblock l = splitData.first;
|
Halfblock l = splitData.first;
|
||||||
Halfblock r = splitData.second;
|
Halfblock r = splitData.second;
|
||||||
@ -38,19 +38,43 @@ namespace Leonetienne::GCrypt {
|
|||||||
Halfblock tmp;
|
Halfblock tmp;
|
||||||
|
|
||||||
for (std::size_t i = 0; i < N_ROUNDS; i++) {
|
for (std::size_t i = 0; i < N_ROUNDS; i++) {
|
||||||
// Calculate key index
|
|
||||||
std::size_t keyIndex;
|
// Encryption
|
||||||
if (reverseKeys) {
|
if (modeEncrypt) {
|
||||||
keyIndex = N_ROUNDS - i - 1;
|
const std::size_t keyIndex = i;
|
||||||
}
|
|
||||||
else {
|
// Do a feistel round
|
||||||
keyIndex = i;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do a feistel round
|
|
||||||
tmp = r;
|
|
||||||
r = l ^ F(r, roundKeys[keyIndex]);
|
|
||||||
l = tmp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Block has finished de*ciphering.
|
// Block has finished de*ciphering.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user