torann 0.3.2
torann: ^0.3.2 copied to clipboard
The Noise Protocol Framework for Flutter: native Swift (CryptoKit) and Kotlin (JCA and Bouncy Castle) implementations behind one Dart API.
Torann for Flutter #
Torann is the Noise Protocol Framework, revision 34, for Flutter on iOS 18 and later and on Android 13 (API 33) and later. The repository README explains Noise and the API that every Torann package shares.
Please see BlueConnect for an example implementation.
The plugin contains no cryptography of its own. On iOS it compiles a copy of the Swift implementation, which lib/sync-flutter-sources keeps identical to the original. On Android it depends on the Kotlin implementation, com.stonygrey:torann.
Dart reaches the native code through Pigeon, Flutter's generator for typed platform channels. Every Pigeon call is asynchronous, so every method that reaches the native side returns a Future.
Functions #
The plugin has the Swift function set on iOS and the Kotlin function set on Android. The repository README lists both. NoiseProtocol.parse throws UnsupportedFunctionException for a function the platform lacks.
Installation #
dependencies:
torann: ^0.3.2
The application's iOS deployment target must be 18.0 or later, and its Android minSdk 33 or later.
Usage #
import 'package:torann/torann.dart';
final protocol = await NoiseProtocol.parse('Noise_XX_25519_ChaChaPoly_SHA256');
final staticKey = await protocol.generateKeyPair(); // store await staticKey.privateKey() for reuse
final handshake = await protocol.createHandshake(initiator: true, localStatic: staticKey);
final first = await handshake.writeMessage(); // send first.message
// final reply = await handshake.readMessage(received);
// ... the final message's result carries handshakeHash and transport
// final ciphertext = await transport.writeMessage(payload);
// final plaintext = await transport.readMessage(ciphertext);
Errors are the subclasses of the sealed class TorannException, one per category. HostFailureException is an extra one for a failure that is not a Noise error (e.g. the platform channel is gone). Its message is the native exception's type name and nothing else.
Call close() on every KeyPair, HandshakeState, and Transport. After close(), every method throws ClosedException without calling the native side. A Finalizer releases the native object if the application did not close it, and in a debug build it prints one line naming the object.
Crypto #
The plugin keeps no keys in Dart. Each Dart object is an integer handle to an object that the Swift or Kotlin library owns, and close() closes that object. The limits in swift/README.md and kotlin/torann/README.md apply unchanged.
The platform channel adds copies. Pigeon's codec copies every Uint8List into channel buffers, and Dart cannot zero them.
| Data | Crosses the channel |
|---|---|
| Private keys | Only on privateKey() and createKeyPair |
| Pre-shared keys | On createHandshake |
| Public keys, messages, payloads, ciphertexts | On every call |
The Android host zeros the decoded array that carried a private key or a pre-shared key once the library has copied it. That removes one JVM copy. It does not remove the channel's copies or the garbage collector's.
When a Flutter engine detaches, both hosts close every object that is still open, which wipes its keys.
setSendNonce and setReceiveNonce take a Dart int, so the range is 0 to 2^63 − 1. A negative value throws InvalidArgumentException.
Tests #
flutter test # Dart unit tests against a fake native side
cd example && flutter test integration_test -d <device> # on a device or simulator
The integration test runs the shared vectors through the plugin, 440 on iOS and 1,352 on Android, and then handshakes with random keys.
The entry point that sets a fixed ephemeral key is exported from package:torann/testing.dart and not from package:torann/torann.dart. Dart has no privilege boundary inside a process, so the native method behind it exists on the platform channel in every build.