finish method
Finalizes the Poly1305 authentication process and computes the authentication code (MAC).
This method completes the Poly1305 authentication operation, producing the message authentication code (MAC).
Parameters:
mac
: AList<int>
to store the computed MAC.macpos
: An optional parameter that specifies the starting position within themac
buffer to store the MAC.
Returns:
A reference to the Poly1305
instance, allowing method chaining if needed.
This method is used to complete the Poly1305 authentication, and the resulting MAC is stored in the mac
buffer.
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] &= mask13;
for (i = 2; i < 10; i++) {
_h[i] += c;
c = _h[i] >> 13;
_h[i] &= mask13;
}
_h[0] += (c * 5);
c = _h[0] >> 13;
_h[0] &= mask13;
_h[1] += c;
c = _h[1] >> 13;
_h[1] &= mask13;
_h[2] += c;
g[0] = _h[0] + 5;
c = g[0] >> 13;
g[0] &= mask13;
for (i = 1; i < 10; i++) {
g[i] = _h[i] + c;
c = g[i] >> 13;
g[i] &= 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)) & mask16;
_h[1] = ((_h[1] >> 3) | (_h[2] << 10)) & mask16;
_h[2] = ((_h[2] >> 6) | (_h[3] << 7)) & mask16;
_h[3] = ((_h[3] >> 9) | (_h[4] << 4)) & mask16;
_h[4] = ((_h[4] >> 12) | (_h[5] << 1) | (_h[6] << 14)) & mask16;
_h[5] = ((_h[6] >> 2) | (_h[7] << 11)) & mask16;
_h[6] = ((_h[7] >> 5) | (_h[8] << 8)) & mask16;
_h[7] = ((_h[8] >> 8) | (_h[9] << 5)) & mask16;
f = _h[0] + _pad[0];
_h[0] = f & mask16;
for (i = 1; i < 8; i++) {
f = (((_h[i] + _pad[i]) | 0) + (f >> 16)) | 0;
_h[i] = f & mask16;
}
for (int i = 0; i < 8; i++) {
writeUint16LE(_h[i], mac, i * 2);
}
_finished = true;
return this;
}