Skip to main content

rind 0.2.0 copy "rind: ^0.2.0" to clipboard
rind: ^0.2.0 copied to clipboard

Fast, misuse-resistant cryptography for Dart and Flutter, through FFI to rind, a pure-Rust library: AEADs, hashes, HMAC, HKDF, PBKDF2, X25519, Ed25519, ECDSA, ECDH, RSA, and ML-KEM.

rind for Dart and Flutter #

Fast, misuse-resistant cryptography for Dart and Flutter, through FFI to rind, a pure-Rust library: AES-GCM, ChaCha20-Poly1305, and XChaCha20-Poly1305, SHA-2 and SHA-3, HMAC, HKDF, PBKDF2, X25519 and ECDH, Ed25519, ECDSA, and RSA signatures, RSA-OAEP, and ML-KEM-768, and the unauthenticated AES-CBC, AES-CTR, and ChaCha20 for protocols that pair them with their own MAC.

It is 7 to 2800 times faster than pure-Dart cryptography. On an Intel i7-8700, in microseconds per operation (dart run benchmark/compare.dart):

Operation rind pointycastle cryptography (Dart)
SHA-256, 1 KiB 2.1 19 15
HMAC-SHA256, 64 B 0.51 10 9.2
AES-256-GCM seal, 1350 B 0.85 2400 176
ChaCha20-Poly1305 seal, 1350 B 1.4 59 44
X25519 agree 21 845
Ed25519 sign 8.0 2700
Ed25519 verify 40 4800
ECDSA P-256 sign 27 9900
ECDSA P-256 verify 77 14000

On a Pixel 10 Pro (Tensor G5, Android 17), with rind 0.1.0 in a release-mode Flutter 3.50 app (Dart 3.14), the median of five runs, which spread up to 3x as Android moves the thread between cores:

Operation rind pointycastle cryptography (Dart)
SHA-256, 1 KiB 1.6 340 99
HMAC-SHA256, 64 B 0.93 150 38
AES-256-GCM seal, 1350 B 4.1 5300 340
ChaCha20-Poly1305 seal, 1350 B 9.1 300 300
X25519 agree 230 1700
Ed25519 sign 35 8300
Ed25519 verify 160 11000
ECDSA P-256 sign 57 24000
ECDSA P-256 verify 180 42000

Setup #

dart pub add rind

The package's build hook downloads rind's prebuilt library for each target the app builds for from this version's GitHub release, and refuses one whose SHA-256 differs from the one the package pins. It honors the HTTPS_PROXY environment variable.

To build rind from source instead, set this in the app's pubspec.yaml:

hooks:
  user_defines:
    rind:
      build_from_source: true

That needs Rust 1.90 or later with the standard library of each target the app builds for, and for Android the NDK that Flutter's Android toolchain provides:

# Android
rustup target add aarch64-linux-android armv7-linux-androideabi x86_64-linux-android
# iOS
rustup target add aarch64-apple-ios aarch64-apple-ios-sim x86_64-apple-ios

Platforms #

Android on arm64, armv7, and x86_64; iOS on devices and the simulator; macOS, Linux, and Windows on arm64 and x86_64; and Linux on armv7. The build fails for any other target, such as Android's 32-bit x86, rather than falling back to slower code. There is no web build: the package calls rind through dart:ffi, which Dart's web compilers lack, though rind itself builds for WebAssembly.

rind's arm64 build enables NEON, which every Android and Apple device has; a source build for a target without NEON uses rind's portable backend.

Use #

import 'dart:convert';
import 'dart:typed_data';

import 'package:rind/ecdsa.dart' as ecdsa;
import 'package:rind/ed25519.dart' as ed25519;
import 'package:rind/rind.dart';
import 'package:rind/rsa.dart' as rsa;

void main() {
  final key = Uint8List(32);
  fillRandom(key);

  final aead = AeadKey(AeadAlgorithm.chacha20Poly1305, key);
  // Never reuse a nonce with one key; RandomizedNonceKey picks its own.
  final nonce = Nonce.assumeUniqueForKey(Uint8List(12));
  final sealed = aead.seal(nonce, utf8.encode('hello'));
  print(utf8.decode(aead.open(nonce, sealed)));

  final message = utf8.encode('hello');

  final ecdsaPair = ecdsa.KeyPair.generate(
    ecdsa.SigningAlgorithm.p256Sha256Asn1,
  );
  ecdsa.PublicKey(
    ecdsa.VerificationAlgorithm.p256Sha256Asn1,
    ecdsaPair.publicKey.bytes,
  ).verify(message, ecdsaPair.sign(message));

  final ed25519Pair = ed25519.KeyPair.generate();
  ed25519.PublicKey(
    ed25519Pair.publicKey.bytes,
  ).verify(message, ed25519Pair.sign(message));

  // An RSA public key verifies with any algorithm whose modulus floor it
  // meets.
  final rsaPair = rsa.KeyPair.generate(rsa.KeySize.rsa2048);
  rsa.PublicKey(rsaPair.publicKey.bytes).verify(
    rsa.VerificationAlgorithm.pssSha256,
    message,
    rsaPair.sign(rsa.SigningAlgorithm.pssSha256, message),
  );
}

The API is rind's own: each algorithm is a constant, and each key type has one role. ECDSA, Ed25519, and RSA each have a library, imported under its name as rind's modules are, with a KeyPair and a PublicKey: a public key's constructor takes the algorithm's own encoding (an X9.62 point, 32 bytes, or PKCS#1), and fromSpki a SubjectPublicKeyInfo. RSA-OAEP is in package:rind/rsa.dart too. rind_cryptography offers the cryptography package's API on it instead. Failures throw a RindException whose error says what failed, as rind's Error does, and a refused key says why in its reason. SHA-1 and MD5 are in package:rind/legacy.dart, and RSA's SHA-1 and its moduli below 2048 bits in package:rind/rsa_legacy.dart, imported as rsa beside package:rind/rsa.dart.

Keys live in native memory, and rind wipes that native copy when Dart frees the key. It does not wipe a Uint8List you passed in; clear that yourself.

Generating RSA keys, RSA's private-key operations, and PBKDF2 take milliseconds to seconds, so call them off the UI isolate, as with Isolate.run.

License: Apache-2.0 OR ISC OR MIT-0

0
likes
140
points
408
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Fast, misuse-resistant cryptography for Dart and Flutter, through FFI to rind, a pure-Rust library: AEADs, hashes, HMAC, HKDF, PBKDF2, X25519, Ed25519, ECDSA, ECDH, RSA, and ML-KEM.

Repository (GitHub)
View/report issues
Contributing

Topics

#cryptography #crypto #ffi #encryption

License

unknown (license)

Dependencies

code_assets, crypto, ffi, hooks

More

Packages that depend on rind