process method

  1. @override
BuBytes process(
  1. List<int> data, {
  2. required List<int> nonce,
  3. int nonceInplaceCounterLength = 0,
  4. int seekBytes = 0,
  5. List<int>? dst,
})

Encrypts or decrypts data by XORing it with the output of the ChaCha stream cipher.

Parameters:

  • nonce: The nonce data, which must be either 8, 12, or 16 bytes in length depending on the value of nonceInplaceCounterLength.
  • data: The source data to be encrypted or decrypted.
  • dst: The destination data where the result will be written.
  • nonceInplaceCounterLength: An optional parameter to specify the length of the nonce inplace counter (0 for no counter, 16 bytes if a counter is included in the nonce).

Throws:

  • BuError if the destination is shorter than the source, or if the nonce length is invalid.

Implementation

@override
BuBytes process(
  List<int> data, {
  required List<int> nonce,
  int nonceInplaceCounterLength = 0,
  int seekBytes = 0,
  List<int>? dst,
}) {
  final out = BuBytes.unsafeMutable(dst ?? List<int>.filled(data.length, 0));

  if (out.length < data.length) {
    throw CoreErrors.invalidOperationArguments(
      "process",
      name: "dst",
      reason: "Invalid destination bytes length.",
    );
  }

  List<int> nc;
  int counterLength;

  if (nonceInplaceCounterLength == 0) {
    if (nonce.length != 8 && nonce.length != 12) {
      throw CoreErrors.invalidOperationArguments(
        "process",
        name: "nonce",
        reason: "Invalid nonce bytes length.",
      );
    }
    nc = List<int>.filled(16, 0);
    counterLength = nc.length - nonce.length;
    nc.setAll(counterLength, nonce);
  } else {
    if (nonce.length != 16) {
      throw CoreErrors.invalidOperationArguments(
        "process",
        name: "nonce",
        reason: "Invalid nonce bytes length.",
      );
    }
    nc = nonce;
    counterLength = nonceInplaceCounterLength;
  }
  BinaryOps.zero(out);
  final block = List<int>.filled(64, 0);

  final int blockSkip = seekBytes ~/ 64;
  final int byteSkip = seekBytes % 64;

  if (blockSkip != 0) {
    for (int b = 0; b < blockSkip; b++) {
      for (int i = 0; i < data.length; i += 64) {
        _core(block, nc, _key);

        for (int j = 0; j < 64 && i + j < data.length; j++) {
          out[i + j] = (data[i + j] & BinaryOps.mask8) ^ block[j];
        }

        _incrementCounter(nc, 0, counterLength);
      }
    }
  }
  int srcOffset = 0;

  if (byteSkip != 0) {
    _core(block, nc, _key);

    // XOR only after byteSkip
    for (
      int j = byteSkip;
      j < 64 && srcOffset < data.length;
      j++, srcOffset++
    ) {
      out[srcOffset] = (data[srcOffset] & BinaryOps.mask8) ^ block[j];
    }

    _incrementCounter(nc, 0, counterLength);
  }

  for (; srcOffset < data.length; srcOffset += 64) {
    _core(block, nc, _key);

    for (int j = 0; j < 64 && srcOffset + j < data.length; j++) {
      out[srcOffset + j] = (data[srcOffset + j] & BinaryOps.mask8) ^ block[j];
    }

    _incrementCounter(nc, 0, counterLength);
  }

  BinaryOps.zero(block);

  if (nonceInplaceCounterLength == 0) {
    BinaryOps.zero(nc);
  }

  return out;
}