encryptBlock method

void encryptBlock(
  1. List<int> xk,
  2. List<int> src,
  3. List<int> dst
)

Implementation

void encryptBlock(List<int> xk, List<int> src, List<int> dst) {
  int s0 = readUint32BE(src, 0);
  int s1 = readUint32BE(src, 4);
  int s2 = readUint32BE(src, 8);
  int s3 = readUint32BE(src, 12);

  // First round just XORs input with key.
  s0 ^= xk[0];
  s1 ^= xk[1];
  s2 ^= xk[2];
  s3 ^= xk[3];

  int t0 = 0, t1 = 0, t2 = 0, t3 = 0;

  /// Middle rounds shuffle using tables.
  /// Number of rounds is set by the length of the expanded key.
  final nr = xk.length ~/ 4 - 2; // - 2: one above, one more below
  int k = 4;

  for (int r = 0; r < nr; r++) {
    t0 = xk[k + 0] ^
        _te0[(s0 >> 24) & mask8] ^
        _te1[(s1 >> 16) & mask8] ^
        _te2[(s2 >> 8) & mask8] ^
        _te3[s3 & mask8];

    t1 = xk[k + 1] ^
        _te0[(s1 >> 24) & mask8] ^
        _te1[(s2 >> 16) & mask8] ^
        _te2[(s3 >> 8) & mask8] ^
        _te3[s0 & mask8];

    t2 = xk[k + 2] ^
        _te0[(s2 >> 24) & mask8] ^
        _te1[(s3 >> 16) & mask8] ^
        _te2[(s0 >> 8) & mask8] ^
        _te3[s1 & mask8];

    t3 = xk[k + 3] ^
        _te0[(s3 >> 24) & mask8] ^
        _te1[(s0 >> 16) & mask8] ^
        _te2[(s1 >> 8) & mask8] ^
        _te3[s2 & mask8];

    k += 4;
    s0 = t0;
    s1 = t1;
    s2 = t2;
    s3 = t3;
  }

  /// Last round uses s-box directly and XORs to produce output.
  s0 = (_sbox0[t0 >> 24] << 24) |
      (_sbox0[(t1 >> 16) & mask8]) << 16 |
      (_sbox0[(t2 >> 8) & mask8]) << 8 |
      (_sbox0[t3 & mask8]);

  s1 = (_sbox0[t1 >> 24] << 24) |
      (_sbox0[(t2 >> 16) & mask8]) << 16 |
      (_sbox0[(t3 >> 8) & mask8]) << 8 |
      (_sbox0[t0 & mask8]);

  s2 = (_sbox0[t2 >> 24] << 24) |
      (_sbox0[(t3 >> 16) & mask8]) << 16 |
      (_sbox0[(t0 >> 8) & mask8]) << 8 |
      (_sbox0[t1 & mask8]);

  s3 = (_sbox0[t3 >> 24] << 24) |
      (_sbox0[(t0 >> 16) & mask8]) << 16 |
      (_sbox0[(t1 >> 8) & mask8]) << 8 |
      (_sbox0[t2 & mask8]);

  s0 ^= xk[k + 0];
  s1 ^= xk[k + 1];
  s2 ^= xk[k + 2];
  s3 ^= xk[k + 3];

  writeUint32BE(s0, dst, 0);
  writeUint32BE(s1, dst, 4);
  writeUint32BE(s2, dst, 8);
  writeUint32BE(s3, dst, 12);
}