private Int32[] rcon;
private Byte[,] state;
-
+
public RijndaelTransform (Rijndael algo, bool encryption, byte[] key, byte[] iv) : base (algo, encryption, iv)
{
int keySize = algo.KeySize;
}
shifts = new int[2,4];
- // Encryption
- shifts [0,0] = -1; // Not used
- shifts [0,1] = 1;
- shifts [0,2] = (Nb == 8) ? 3 : 2;
- shifts [0,3] = (Nb == 8) ? 4 : 3;
-
- // Decryption
- shifts [1,0] = -1; // Not used
- shifts [1,1] = 3;
- shifts [1,2] = (Nb == 8) ? 1 : 2;
- shifts [1,3] = (Nb == 8) ? 0 : 1;
+ switch (Nb) {
+ case 8: // 256 bits
+ // encryption
+ shifts [0,0] = -1; // Not used
+ shifts [0,1] = 1;
+ shifts [0,2] = 3;
+ shifts [0,3] = 4;
+ // decryption
+ shifts [1,0] = -1; // Not used
+ shifts [1,1] = 7;
+ shifts [1,2] = 5;
+ shifts [1,3] = 4;
+ break;
+ case 6: // 192 bits
+ // encryption
+ shifts [0,0] = -1; // Not used
+ shifts [0,1] = 1;
+ shifts [0,2] = 2;
+ shifts [0,3] = 3;
+ // decryption
+ shifts [1,0] = -1; // Not used
+ shifts [1,1] = 5;
+ shifts [1,2] = 4;
+ shifts [1,3] = 3;
+ break;
+ case 4: // 128 bits
+ // encryption
+ shifts [0,0] = -1; // Not used
+ shifts [0,1] = 1;
+ shifts [0,2] = 2;
+ shifts [0,3] = 3;
+ // decryption
+ shifts [1,0] = -1; // Not used
+ shifts [1,1] = 3;
+ shifts [1,2] = 2;
+ shifts [1,3] = 1;
+ break;
+ }
int rcon_entries = (Nb * (Nr+1)) / Nk;
rcon = new Int32 [rcon_entries + 1];
AddRoundKey (0, encrypt);
if (encrypt) {
- for (int round = 1; round < Nr; round++)
- Round (round, true);
+ for (int round = 1; round < Nr; round++) {
+ ByteSub (true);
+ ShiftRow (true);
+ MixColumn ();
+ AddRoundKey (round, true);
+ }
ByteSub (true);
ShiftRow (true);
}
else {
ShiftRow (false);
ByteSub (false);
-
- for (int round = 1; round < Nr; round++)
- Round (round, false);
+ for (int round = 1; round < Nr; round++) {
+ AddRoundKey (round, false);
+ InvMixColumn ();
+ ShiftRow (false);
+ ByteSub (false);
+ }
}
AddRoundKey (Nr, encrypt);
}
}
- private void Round (int round, bool encrypt)
- {
- if (encrypt) {
- ByteSub (true);
- ShiftRow (true);
- MixColumn ();
- AddRoundKey (round, true);
- } else {
- AddRoundKey (round, false);
- InvMixColumn ();
- ShiftRow (false);
- ByteSub (false);
- }
- }
-
private Int32 SubByte (Int32 a)
{
// unrolled loop (no more multiply)