Dev.to Security 🔐 Cybersecurity 👁 0 📖 3 min read

flutter_secure_storage: one Dart call, two hardware vaults, and what to put in them

flutter_secure_storage looks like a key-value store. You call write, you call read, done. But that one Dart call ends up in two very different places depending on the platform, and knowing where it lands changes what you

flutter_secure_storage looks like a key-value store. You call write, you call read, done. But that one Dart call ends up in two very different places depending on the platform, and knowing where it lands changes what you should store there.

The short version: it is a vault for keys, not a database for your data.

Where write actually goes

The package is an abstraction layer. It gives you one API and routes each call to the native vault of the device:

  • iOS: the Keychain, through the C-based security APIs.
  • Android: the Keystore system.
const storage = FlutterSecureStorage();

await storage.write(key: 'vault_key', value: derivedKeyBase64);
final key = await storage.read(key: 'vault_key');

Same two lines on both platforms. What happens underneath is not the same.

iOS: the Keychain

The Keychain is not a hidden file in your app folder. It is an encrypted SQLite database run by the securityd daemon, outside your app sandbox.

  • Every item is encrypted before it reaches the disk.
  • The data is tied to the device's Secure Enclave. Copying the raw database off the phone with forensic tools gives an attacker encrypted noise.
  • Your app needs the right entitlements configured in Xcode.

You also decide when iOS will hand an item back. For example:

const storage = FlutterSecureStorage(
  iOptions: IOSOptions(
    accessibility: KeychainAccessibility.firstUnlockThisDevice,
  ),
);

With this setting the item stays locked after a reboot until the user unlocks the device for the first time.

Android: the Keystore

The Keystore reaches the same goal by a different route. On modern devices it runs inside a Trusted Execution Environment (TEE) or a dedicated Hardware Security Module (HSM), isolated from the main Android OS.

  • Keys can be generated inside the hardware. The raw key material never enters main memory.
  • Operations can require biometrics. A decrypt can be blocked until the user gives a fresh fingerprint or PIN.
  • Your app only holds an alias. It never touches the private key. It asks the Keystore to sign or decrypt using a name.

Android versions vary a lot, and the package handles that. On older versions it falls back to EncryptedSharedPreferences, which you can switch on explicitly:

const storage = FlutterSecureStorage(
  aOptions: AndroidOptions(encryptedSharedPreferences: true),
);

The mistake: using the vault as your database

Since the storage is so convenient, it is tempting to write every password or note straight into it. That mixes two jobs. The pattern that holds up keeps authentication and storage apart, in four layers.

1. Never store the master password. Run the user's input through a key derivation function such as PBKDF2 or Argon2 to get a high-entropy key.

import 'package:cryptography/cryptography.dart';

final kdf = Pbkdf2(
  macAlgorithm: Hmac.sha256(),
  iterations: kdfIterations, // pick from current guidance
  bits: 256,
);

final secretKey = await kdf.deriveKeyFromPassword(
  password: masterPassword,
  nonce: salt,
);

2. Encrypt the data vault with that key. Keep the user's records in a normal local database (SQLite or Isar, for example) and encrypt it with a symmetric cipher such as AES-256-GCM.

final aes = AesGcm.with256bits();
final box = await aes.encrypt(plainBytes, secretKey: secretKey);

3. Put the derived key in secure storage. This is the one value that goes into the Keychain or Keystore.

final keyBytes = await secretKey.extractBytes();
await storage.write(key: 'vault_key', value: base64Encode(keyBytes));

4. Gate it with biometrics. When the user comes back, don't ask for the master password again. Ask the OS for Face ID or a fingerprint, and only on success read the key and decrypt the vault.

Why this split works

Picture the stolen phone. The attacker copies the database file off the device. It is encrypted with AES-256, and the key that opens it is held by the Keychain or Keystore, behind the user's face or finger. The file alone is useless.

Compare that with storing everything in the cloud: one server breach exposes every user at once. Here, each user's data is locked to their own device hardware.

The same blueprint fits more than password managers: local document vaults, offline medical charting and self-custodial crypto wallets all use it.

Checklist

  • Treat flutter_secure_storage as a key vault, not a data store.
  • Derive the key with PBKDF2 or Argon2. Never persist the master password.
  • Encrypt the local database with AES-256-GCM using the derived key.
  • Store only the derived key in secure storage.
  • Set iOS accessibility (for example firstUnlockThisDevice) and Android options on purpose.
  • Unlock with biometrics instead of asking for the password every time.

Further reading: Securing Local-First Applications: A Flutter Architecture Study.

What do you keep in secure storage today: only keys, or user data as well?

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.