Repository navigation
Avoid cloning the AAD header buffer in SelfEncrypt::seal - #220
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
The refactor preserves encryption format and behavior while eliminating the unnecessary clone.
Pull request overview
Optimizes SelfEncrypt::seal by constructing the encryption header once and avoiding a cloned buffer.
Changes:
- Introduces a shared header-length constant and fixed-size header.
- Preallocates AAD and output buffers, then copies the header into each.
File summaries
| File | Description |
|---|---|
src/selfencrypt.rs |
Refactors header and buffer construction in seal. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 0
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
Reviewed the refactor of SelfEncrypt::seal. The rewritten header construction is byte-for-byte equivalent to the old one (VERSION, key_id, 16-byte salt; encoded_len unchanged), the AAD still covers header-then-caller-AAD exactly as open() reconstructs it, and output[HEADER_LEN..] is the same destination slice as the old output[offset..]. Allocation count for the AAD buffer goes from two (the clone() produced a Vec with capacity == len, so write_all(aad) always reallocated) to one. No correctness or security concerns; no public API change. Only cleanup comments inline.
Two related observations that apply beyond the individual lines:
- Style drift between the two halves.
sealnow usesextend_from_slice, whileopen()still useswrite_all(...).unwrap_or_else(|_| unreachable!(...)). Converting those two calls the same way removes six lines of infallible-IO boilerplate and letsuse std::io::Write as _drop out of the imports (onlyfmt::Writewould remain). Worth folding into this PR since it's the same idea applied to the mirror function. - The tests don't really guard this code.
assert_bad_datais anif let Err(..), so it passes silently onOk—damage_salt,damage_ciphertext,damage_auth_tag, andtruncatewould all pass even ifopen()accepted tampered input. Alet Err(Error::Nss { name, .. }) = res else { panic!(...) }would make them meaningful. Header-layout regressions in this diff are caught indirectly (seal_open+damage_version), so I don't think new tests are needed, but tightening that helper is cheap.
No benchmark seems warranted — the win is a couple of small allocations per seal, and there's no existing benches/ dir to extend.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #220 +/- ##
==========================================
+ Coverage 73.57% 73.71% +0.14%
==========================================
Files 28 28
Lines 3315 3318 +3
Branches 3315 3318 +3
==========================================
+ Hits 2439 2446 +7
- Misses 662 663 +1
+ Partials 214 209 -5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Build extended_aad directly with Vec::with_capacity and write the header once, mirroring the approach already used in open(), instead of cloning the header Vec to extend it with the AAD.
71dfea9 to
8026ff1
Compare
There was a problem hiding this comment.
Pure refactor of SelfEncrypt::seal/open with no wire-format or cryptographic change: the header bytes (version ‖ key_id ‖ salt) are now written once into extended_aad and the prefix copied into output, replacing the enc.clone(). AAD construction, nonce (counter 0 over a fresh 16-byte random salt) and output layout are byte-identical to before, so the format stays compatible. Both of my earlier comments on the previous push are addressed: HEADER_LEN is hoisted to an associated const and used on both sides, and the header scratch array is gone.
Two residual observations that apply to the file as a whole rather than a single line:
- The zero-fill in
output.resize(encoded_len, 0)still memsetsplaintext.len() + expansion()bytes thatcipher.encryptoverwrites immediately. Eliminating it needsspare_capacity_mut+set_len(i.e.unsafe), which is probably not worth it here — but it is the larger remaining cost inseal, bigger than the clone this PR removes. If these paths are considered hot (session-ticket seal/open), a small Criterion benchmark would make that trade-off measurable instead of inferred. assert_bad_dataintests/selfencrypt.rssilently passes whenresisOk, so thedamage_*andtruncatetests would not fail if a future refactor accidentally made corrupted input decrypt successfully. Pre-existing and out of scope here, but worth tightening (e.g.assert!(matches!(res, Err(Error::Nss { ref name, .. }) if name == "SEC_ERROR_BAD_DATA"))) given this PR touches exactly the offset arithmetic those tests guard.
martinthomson
left a comment
There was a problem hiding this comment.
Avoids reallocation and copying of the AAD.
Build extended_aad directly with Vec::with_capacity and write the
header once, mirroring the approach already used in open(), instead
of cloning the header Vec to extend it with the AAD.