Skip to content

Cryptographic Format

mksdr edited this page Oct 30, 2026 · 8 revisions

Cryptographic format

English · 한국어

Compatibility reference for the client write format and byte-level contracts; this is not an independent security audit or proof of security. Preserve existing format definitions and explain migrations when introducing later formats.

Versions

Object Identifier/version
IndexedDB private-journal, version 4
Local entry crypto 3
Sync envelope crypto 1
Recovery format base32-hkdf-sha256-v1
Backup miroven-encrypted-backup, version 2 (v1 read compatibility below)
Recent-search crypto 1, device-only AES-256-GCM

Stores: entries, device-keys, vault-envelopes, sync-outbox, sync-meta, remote-stage. Names and versions are fixed by compatibility tests. Product releases do not implicitly change these identifiers.

Keys and recovery

A 25-byte CSPRNG recovery secret derives a recovery wrapping key through HKDF-SHA-256 with a random 16-byte salt. This wraps the 32-byte vault key. The vault key wraps per-entry/per-sync-object data keys, encrypts recent searches, and optionally has a device PIN envelope.

Web Crypto keys are generated/imported as extractable for PIN wrapping. With PIN locking enabled, a verified unlock holds the vault key only in the current document's memory; it is never newly written to IndexedDB device-keys/primary. Without a PIN the existing persistent-key behavior remains. PIN locking cannot protect a compromised browser/device.

Recovery field Contract
Secret 25 random bytes, 200 bits
Alphabet ABCDEFGHJKLMNPQRSTUVWXYZ23456789
Code 40 Base32 characters plus the first 2 Base32 characters of the SHA-256 digest
Display Eight groups of 5 characters, followed by the 2-character checksum
HKDF SHA-256, random 16-byte salt
HKDF info private-journal:recovery-wrap-key:v1
Envelope AES-256-GCM, random 12-byte nonce
AAD private-journal:vault-key-envelope:v1
Plain payload 32-byte vault key
Wrapped payload 48 bytes including the GCM tag

The two-character checksum detects typing errors; it is not a password hash or attack defense.

Local entry crypto v3

Each entry JSON contains text, createdAt, photo data URLs, dimensions, and MIME types. New entries use attachmentVersion: 2 and ordered images with unique image ids, up to four images. Read legacy image as a single photo without a bulk rewrite.

  1. Generate a random 32-byte entry data key.
  2. Encrypt JSON using AES-256-GCM, random 12-byte IV, AAD private-journal:entry:{entry_id}:v3.
  3. Wrap the raw data key using the vault key, AES-256-GCM, random 12-byte nonce, AAD private-journal:data-key:{entry_id}:v3.
  4. Zero the raw key buffer after use.

v1/v2 records have read compatibility paths; new writes use v3. Backup export rejects remaining non-v3 records.

Remote content carries each photo's blobId and metadata. Photos use separate encrypted envelopes. New sync operations use attachment_version: 2 and blob_ids; legacy single-photo payloads and old Outbox blob fields remain readable. Backup v1 preserves optional blobs and attachmentVersion, validating all files before import. Multi-photo support does not change AAD or crypto versions.

Sync envelope v1

Each entry and blob gets a fresh random 32-byte data key. Content and wrapped-key nonces are independently random 12-byte values. Both use AES-256-GCM.

Content AAD: private-journal:sync:{entry|blob}:{id}:r{revision}:v1
Data-key AAD: private-journal:sync:{entry|blob}:data-key:{id}:v1

The server stores ciphertext, nonces, wrapped keys, revisions, IDs, and operational metadata. E2EE does not hide identifiers, revisions, sizes, or receipt times.

PIN envelope

PINs contain 4–8 digits; prefer at least 6 digits. New envelopes use PBKDF2-SHA-256 with 300,000 iterations and a random 16-byte salt. Legacy envelopes without a stored iteration count use 100,000. Wrapping uses AES-256-GCM with a random 12-byte IV. After five failures, UI backoff uses 30/60/120/300 seconds.

PINs are a convenience lock, not a strong defense against offline attacks on copied browser storage. Four, six, and eight digits provide about 13.3, 19.9, and 26.6 bits respectively. Store the high-entropy recovery code separately.

Creating the first recovery code replaces the device key and re-encrypts local entries. This operation rejects any configured PIN, including on an unlocked vault. Recovery creation and PIN key mutations share the vault mutation lock; recovery creation also rechecks PIN and recovery-envelope absence in its final write transaction. Web Locks coordinate participating same-origin tabs; the fallback queue coordinates only the current JavaScript runtime. PIN and recovery envelope formats remain unchanged.

Backup v2 and legacy v1

Filename: miroven-YYYY-MM-DD.miroven-backup.json. Maximum: 100 MiB. Only miroven-encrypted-backup is accepted. Contents include the vault key envelope, v3 local encrypted entries, serialized sync Outbox (possibly photo envelopes), cursor, and sync state. The recovery code is never included.

The outer JSON is not encrypted as a whole: format, export time, IDs, revisions, sync state, and ciphertext sizes can be visible. Text, photos, data keys, and the vault key remain encrypted/wrapped.

Import limits entry and Outbox counts to 100,000 each, validates field types, buffer lengths, and positive revisions, and uses the recovery code to decrypt the vault envelope, authenticate v2 manifests, and validate encrypted records and upserts. It refuses to overwrite a nonempty local vault. Export records the API cursor as numeric zero; restore obtains a fresh API/account binding and reconciles a full snapshot rather than reusing a signed server cursor.

Recent searches v1

Up to ten search strings remain on-device, excluded from backup, sync, and diagnostics. Encode the array as UTF-8 JSON and encrypt with the vault key, AES-256-GCM, random 12-byte IV, AAD private-journal:recent-searches:v1. Store at device-keys/recent-searches. Legacy plaintext localStorage miroven:recent_searches is encrypted and immediately removed on first read.

Compatibility changes

Changing AAD, database/store names, or format versions requires a new version, migration, and compatibility tests. Do not silently redefine old versions. Add independent review and old-format fixture tests before changing crypto formats. IndexedDB v4 adds remote-stage for atomic snapshot reconciliation while preserving ciphertext and AAD.

See backup and recovery, security, and test fixtures.

Authenticated backup v2 and migration

New exports use miroven-encrypted-backup, version 2. The v1 fields remain, with authentication: {"algorithm":"hmac-sha256","tag":"<base64>"} added. HMAC-SHA-256 authenticates the entire root JSON object excluding only its authentication property: entries, array order, Outbox deletions, revisions, statuses, export time, cursor, format/version, and vault envelope. Canonical bytes are UTF-8 of compact ECMAScript JSON.stringify with object keys recursively sorted using JavaScript's default string sort; arrays retain their order. JSON whitespace and object property order do not affect verification. This is an integrity layer, not whole-file encryption or proof of freshness.

Derive a 256-bit HMAC key with HKDF-SHA-256 from the raw 32-byte vault key, the envelope's 16-byte recoverySalt, and UTF-8 info private-journal:backup-auth:v2. Encode the 32-byte tag as canonical padded standard Base64. The nonextractable derived key is separate from entry and recovery keys; erase the temporary exported vault-key buffer after derivation. Unwrap the vault envelope with the recovery code and verify the complete manifest before normalizing operations or writing any vault data. Continue the existing per-record checks. The recovery code is never written into the file.

Compatibility:

  • A v2 backup requires an updated client; v1-only clients reject it. Existing IndexedDB data, entry/sync ciphertext, AAD, and recovery codes are unchanged. No API migration is needed.
  • A trusted v1 file without deletion operations can still be imported, including encrypted pending/conflicting upserts and photos. Re-export it from the updated client to create v2. v1 metadata remains unauthenticated; migration cannot retroactively establish its integrity.
  • Reject a v1 file containing any pending or conflicting delete before writing data. Keep the file intact. Use the original device with the updated app to export an authenticated backup. If that device is unavailable, retain the original file and use a separately reviewed recovery process; do not remove operations or downgrade the file by hand.
  • Stripping the v2 tag and changing its version to v1 cannot enable unsigned deletion import. Legitimate v2 deletions and conflict status round-trip unchanged.

Keep originals until restoration and synchronization are verified.

Independent compatibility fixture

backup-v2.json reuses that synthetic v1 fixture and test-only recovery code with version 2 and a whole-manifest authentication tag computed independently using Python cryptography HKDF/AESGCM and Python hmac. It verifies the cross-implementation HKDF info, salt, canonical JSON, and tag encoding. No user data or production keys are included.

PIN key lifetime and existing vaults

PIN unlock is scoped to one document context. Reloads, new tabs, navigation, and browser-process restarts require the PIN again. Pagehide (including bfcache entry) revokes the current context; returning from bfcache rechecks the locked state. Manual/idle locks and PIN changes revoke memory sessions and clear decrypted views in participating tabs through BroadcastChannel, with storage-event notification as a fallback. No key or PIN is sent through either channel. If both messaging mechanisms are unavailable, other tabs cannot be notified immediately and must lock independently. Unlocking one tab does not unlock another. A pending slow PIN derivation cannot restore a session revoked while it was running.

Migration does not change IndexedDB version/store names, encrypted records, AAD, PIN envelope, PBKDF2 parameters, recovery codes, or backup format. The centralized key reader ignores a legacy persistent primary key whenever a PIN envelope exists. Existing plaintext-capable legacy keys are retained until the first correct PIN proves that they match the envelope, then removed before creating the memory session. This avoids silently deleting the only usable key in an inconsistent legacy vault. Until that first verified migration, the legacy key still exists on disk; complete a PIN unlock after updating. If keys do not match, fail closed and preserve the original browser data and recovery code for recovery rather than deleting either key.

Close/reload all old-version tabs after updating: old JavaScript cannot be retroactively revoked and may still use the old persistence path. The updated app never uses a legacy stored key to bypass PIN entry. PIN removal requires the correct PIN and atomically stores the verified key while deleting the PIN envelope, preserving local-only operation across restarts. Recovery/import refuses to install a persistent key underneath an existing PIN; remove the PIN first on an empty vault. Backup export uses the memory key while unlocked. Existing encrypted entries, drafts, recent searches, photos, Outbox work, and conflicts remain intact.

The PIN is still a convenience lock with limited offline entropy. This change does not defend against a compromised client/browser, XSS, OS memory inspection, or someone accessing an already unlocked tab.

On a fresh PIN-protected session, encrypted draft restoration waits until the vault is unlocked. Autosave starts only after restoration completes, so an empty locked-session editor cannot overwrite or delete the saved draft.

Clone this wiki locally