finish method

Poly1305 finish(
  1. List<int> mac, [
  2. int macpos = 0
])

Finalizes the Poly1305 authentication process and computes the authentication code (MAC).

Parameters:

  • mac: To store the computed MAC.
  • macpos: An optional parameter that specifies the starting position within the mac buffer to store the MAC.

Implementation

Poly1305 finish(List<int> mac, [int macpos = 0]) {
  final g = List<int>.filled(10, 0);
  int c;
  int mask;
  int f;
  int i;

  if (_leftover != 0) {
    i = _leftover;
    _buffer[i++] = 1;
    for (; i < 16; i++) {
      _buffer[i] = 0;
    }
    _fin = 1;
    _blocks(_buffer, 0, 16);
  }

  c = _h[1] >> 13;
  _h[1] &= BinaryOps.mask13;
  for (i = 2; i < 10; i++) {
    _h[i] += c;
    c = _h[i] >> 13;
    _h[i] &= BinaryOps.mask13;
  }
  _h[0] += (c * 5);
  c = _h[0] >> 13;
  _h[0] &= BinaryOps.mask13;
  _h[1] += c;
  c = _h[1] >> 13;
  _h[1] &= BinaryOps.mask13;
  _h[2] += c;

  g[0] = _h[0] + 5;
  c = g[0] >> 13;
  g[0] &= BinaryOps.mask13;
  for (i = 1; i < 10; i++) {
    g[i] = _h[i] + c;
    c = g[i] >> 13;
    g[i] &= BinaryOps.mask13;
  }
  g[9] -= (1 << 13);

  mask = (c ^ 1) - 1;
  for (i = 0; i < 10; i++) {
    g[i] &= mask;
  }
  mask = ~mask;
  for (i = 0; i < 10; i++) {
    _h[i] = (_h[i] & mask) | g[i];
  }

  _h[0] = ((_h[0]) | (_h[1] << 13)) & BinaryOps.mask16;
  _h[1] = ((_h[1] >> 3) | (_h[2] << 10)) & BinaryOps.mask16;
  _h[2] = ((_h[2] >> 6) | (_h[3] << 7)) & BinaryOps.mask16;
  _h[3] = ((_h[3] >> 9) | (_h[4] << 4)) & BinaryOps.mask16;
  _h[4] = ((_h[4] >> 12) | (_h[5] << 1) | (_h[6] << 14)) & BinaryOps.mask16;
  _h[5] = ((_h[6] >> 2) | (_h[7] << 11)) & BinaryOps.mask16;
  _h[6] = ((_h[7] >> 5) | (_h[8] << 8)) & BinaryOps.mask16;
  _h[7] = ((_h[8] >> 8) | (_h[9] << 5)) & BinaryOps.mask16;

  f = _h[0] + _pad[0];
  _h[0] = f & BinaryOps.mask16;
  for (i = 1; i < 8; i++) {
    f = (((_h[i] + _pad[i]) | 0) + (f >> 16)) | 0;
    _h[i] = f & BinaryOps.mask16;
  }
  for (int i = 0; i < 8; i++) {
    BinaryOps.writeUint16LE(_h[i], mac, i * 2);
  }

  _finished = true;
  return this;
}