writeMessage2 method

Future<(Uint8List, NoiseTransport)> writeMessage2(
  1. List<int> payload
)

Returns message 2 (<- e, ee) with payload and the transport of the host.

A payload that would make the message longer than maxNoiseMessageLength throws NoiseError.messageTooLong and changes no state; a failed step throws NoiseError.handshakeFailed and breaks the responder.

Implementation

Future<(Uint8List, NoiseTransport)> writeMessage2(List<int> payload) async {
  _guard('write message 2', 1);
  requireBytes(payload, 'payload');
  if (_handshakeOverhead + payload.length > maxNoiseMessageLength) {
    throw NoiseException(
      NoiseError.messageTooLong,
      'write message 2',
      'payload of ${payload.length} bytes, limit ${maxNoiseMessageLength - _handshakeOverhead}',
    );
  }
  _step = -1;
  try {
    await _ss.mixHash(_ephemeralPublic);
    await _ss.mixKey(
      await _dh(_ephemeral, _initiatorEphemeral!, 'write message 2'),
    );
    final ct = await _ss.encryptAndHash(payload);
    final (toResponder, toInitiator) = await _ss.split();
    _step = 2;
    return (
      Uint8List.fromList([..._ephemeralPublic, ...ct]),
      newTransport(toInitiator, toResponder),
    );
  } on NoiseException {
    _broken = true;
    rethrow;
  } on Object {
    // Any other failure of a step ends the handshake as well (D-M2-38-8).
    _broken = true;
    throw const NoiseException(NoiseError.handshakeFailed, 'handshake step');
  }
}