Repository navigation
perf(AESKDF): reuse the AES key schedule across rounds (~5x on AES-KDF vaults) - #1
Open
madalinignisca wants to merge 1 commit into
Open
madalinignisca wants to merge 1 commit into
madalinignisca wants to merge 1 commit into
Conversation
AES-KDF calls `AES.permute(_:key:)` once per 16-byte half per round.
That API takes a `SymmetricKey` on every call and internally calls
`AES_set_encrypt_key` each time, so the key schedule is recomputed once
per block per round rather than once per derivation.
For a real-world KDBX 4.1 vault using AES-KDF with 47,619,048 rounds,
that is ~95.2M key expansions, and the expansion dominates the AES work
it exists to support.
This adds a CommonCrypto fast path that creates one `CCCryptorRef` up
front and reuses it for every round. ECB encrypts each 16-byte block
independently, so a single 32-byte update covers both halves at once and
is exactly equivalent to permuting them separately. The existing
`AES.permute` loop is kept verbatim as the fallback for platforms
without CommonCrypto, and is also used if CommonCrypto declines at any
point, so a failure degrades to slow rather than to a wrong key.
Measured on an M3 MacBook Air, release build, unlocking the vault above:
before 4.72s
after 1.07s 4.4x
Inner-loop microbenchmark over 1,000,000 rounds:
AES.permute 0.116s
reused cryptor 0.021s 5.4x (byte-identical output)
1.07s is close to the hardware AES floor for this round count, so the
remaining cost is inherent to the parameters rather than the code.
No algorithm or format change. Full suite passes: 473 tests, 73 suites.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
Hi! First off — thanks for building this. I went looking for a KDBX library with
serious secret hygiene (
mlock, scoped reveal, zero-on-deinit) and this is theonly Swift one I found that takes it seriously. I'm using it as the foundation
for a small macOS CLI that unlocks a vault via Touch ID.
While benchmarking my unlock path I found the AES-KDF inner loop was costing far
more than it should, and traced it to something fixable.
The problem
AESKDF.derivecallsAES.permute(_:key:)once per 16-byte half, per round:AES.permutetakes aSymmetricKeyper call, and in swift-crypto that lands inpermuteBlock→CCryptoBoringSSL_AES_set_encrypt_keyon every invocation(Block Function.swift#L92-L105).
So the key schedule is rebuilt once per block per round. My vault is a KDBX 4.1
file using AES-KDF with 47,619,048 rounds — that's ~95.2M key expansions, and the
expansion ends up costing several times the AES work it exists to support.
Vaults using Argon2 are unaffected; this only bites AES-KDF, which is admittedly
the legacy choice, but KeePassXC and KeePassium both still produce it and it's
what my daily-driver vault happens to use.
The change
Create one
CCCryptorRefup front and reuse it for every round.ECB encrypts each 16-byte block independently, so a single 32-byte update covers
both halves in one call and is exactly equivalent to permuting them separately —
that also halves the per-round call overhead.
Kept deliberately conservative:
AES.permuteloop stays verbatim as the fallback, sonon-CommonCrypto platforms are byte-for-byte unchanged.
Bool. IfCCCryptorCreateor anyCCCryptorUpdatedeclines, we fall back rather than continue — a failure degrades to slow,
never to a wrong key.
the surrounding conventions.
Measurements
M3 MacBook Air, release build, unlocking the 47.6M-round vault:
Inner loop in isolation, 1,000,000 rounds, both halves:
AES.permuteper block per round5.4x on the loop, 4.4x end-to-end, byte-identical output (verified by
comparing the 32-byte result of both paths directly, not just by tests passing).
1.07s is close to the hardware AES throughput floor for this round count, so
what remains is inherent to the parameters rather than to the code.
Testing
(AES-256 and ChaCha20).
A note on how this was written
This patch was written by me working with Claude Code — I see from your
CLAUDE.mdthat you use a similar workflow, so I hope that's welcome rather thana red flag. Everything above was verified by execution rather than asserted: the
root cause was traced by reading swift-crypto's source, the numbers are from
actual runs on real vaults, and the equivalence claim is from comparing outputs
of both paths. Happy to share the benchmark harness if useful.
Also glad to rework any of this — split the CommonCrypto path into its own file,
drop the fallback restructuring, adjust naming to taste, whatever fits how you'd
like the KDF layer to look. And if a portable (non-Apple) fast path matters to
you, I'm happy to look at that as a follow-up.