setKey method

void setKey(
  1. Uint8List key,
  2. Uint8List? nonce
)

Implementation

void setKey(Uint8List key, Uint8List? nonce) {
  if (key.length != 32) throw ArgumentError('Poly1305 key must be 256 bits.');
  if (cipher != null && (nonce == null || nonce.length != BLOCK_SIZE)) {
    throw ArgumentError('Poly1305-AES requires a 128 bit IV.');
  }

  var keyByteData = ByteData.view(key.buffer, key.offsetInBytes, key.length);
  var t0 = unpack32(keyByteData, 0, Endian.little);
  var t1 = unpack32(keyByteData, 4, Endian.little);
  var t2 = unpack32(keyByteData, 8, Endian.little);
  var t3 = unpack32(keyByteData, 12, Endian.little);

  r0 = t0 & (0x03FFFFFF);
  r1 = (cshiftr32(t0, 26) | shiftl32(t1, 6)) & 0x03FFFF03;
  r2 = (cshiftr32(t1, 20) | shiftl32(t2, 12)) & 0x03FFC0FF;
  r3 = (cshiftr32(t2, 14) | shiftl32(t3, 18)) & 0x03F03FFF;
  r4 = (cshiftr32(t3, 8)) & 0x000FFFFF;

  s1 = r1 * 5;
  s2 = r2 * 5;
  s3 = r3 * 5;
  s4 = r4 * 5;

  Uint8List kBytes;
  int kOff;
  if (cipher == null) {
    kBytes = key;
    kOff = BLOCK_SIZE;
  } else {
    kBytes = Uint8List(BLOCK_SIZE);
    kOff = 0;

    cipher!.init(true, KeyParameter.offset(key, BLOCK_SIZE, BLOCK_SIZE));
    cipher!.processBlock(nonce!, 0, kBytes, 0);
  }

  var kByteData =
      ByteData.view(kBytes.buffer, kBytes.offsetInBytes, kBytes.length);
  k0 = unpack32(kByteData, kOff, Endian.little);
  k1 = unpack32(kByteData, kOff + 4, Endian.little);
  k2 = unpack32(kByteData, kOff + 8, Endian.little);
  k3 = unpack32(kByteData, kOff + 12, Endian.little);
}