fix: a fresh AES-GCM nonce for every message - #4
Open
NiKrause wants to merge 4 commits into
Open
Conversation
…tabases Problem: - When opening an encrypted OrbitDB database without encryption options, OrbitDB doesn't throw errors or emit error events - This makes it difficult to detect if a database is encrypted and needs a password, especially when opening databases via URL Solution: - Added isDatabaseEncrypted() function that detects encrypted databases by checking if entries exist but their values are undefined - When opening encrypted DB without encryption: entries have hashes but values are undefined (encrypted data cannot be read) - When opening unencrypted DB: entries have hashes AND readable values - This enables applications to detect encrypted databases and prompt for password when needed Changes: - Added isDatabaseEncrypted() function to src/index.js - Added comprehensive tests verifying the detection logic - Test covers encrypted, unencrypted, and empty database scenarios
…ation+data tests
The nonce was derived alongside the key and reused for up to ivInterval (32000) messages. In AES-GCM a repeated (key, nonce) pair discloses the XOR of the plaintexts and allows the GHASH authentication subkey to be recovered, so ciphertexts become forgeable as well as readable. The rotation does not bound this — a single repeat is sufficient. Generate the nonce per encrypt() call and keep caching the PBKDF2 key and its salt, which is where the cost actually is. Salt rotation at ivInterval is unchanged. decrypt() cached the derived key against the nonce, which was harmless only while the nonce never changed; with a nonce per message it would re-run PBKDF2 for every entry. Keyed on the salt now, which is what the derivation depends on. test/iv.test.js asserted rotation at the interval; it now asserts that no nonce repeats, that every message round-trips, and that the salt still rotates. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
NiKrause
added a commit
to NiKrause/simple-encryption
that referenced
this pull request
Aug 29, 2026
NiKrause
added a commit
to NiKrause/simple-encryption
that referenced
this pull request
Aug 29, 2026
Republishes with orbitdb#4 applied. 0.0.2 reused one nonce for up to 32000 messages under the same key, which in AES-GCM makes entries readable and forgeable. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Open
17 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3.
encrypt()derived the nonce insidederiveEncryptionKey, so one nonce covered every message until theivIntervalrotation — up to 32 000 under the same key. Measured on 0.0.2, four consecutive calls returned the identical IV.A repeated
(key, nonce)in AES-GCM discloses the XOR of the plaintexts and allows the GHASH authentication subkey to be recovered, so entries become forgeable as well as readable. The rotation does not bound this: one repeat is enough.This change
encrypt(). The PBKDF2 key and salt stay cached — the derivation is the expensive part,getRandomValuesis not. Salt rotation ativIntervalis unchanged.decrypt()now caches the derived key against the salt rather than the nonce. Keying it on the nonce was harmless only while the nonce never changed; with a nonce per message it would re-run PBKDF2 (32 767 iterations) for every entry.Wire format is unchanged:
salt ‖ nonce ‖ ciphertext. Data written by 0.0.2 still decrypts.Tests
test/iv.test.jsasserted rotation at the interval, which can no longer be what is interesting. It now asserts that 2000 consecutive messages carry 2000 distinct nonces, that every message round-trips, and that the salt still rotates ativInterval.npx mocha test/index.test.js test/iv.test.js→ 24 passing.test/orbitdb.test.js→ 10 passing.🤖 Generated with Claude Code