0.18 round 2 (decrypt dedup): adopt DecryptingSectorSource at the two
existing call sites — sweep producer and DiscStream demux Round 1 shipped the DecryptingSectorSource decorator (libfreemkv/src/sector/decrypting.rs) but the existing decrypt sites kept calling crate::decrypt::decrypt_sectors inline. This commit migrates both: - Disc::sweep (disc/mod.rs): producer wraps the input reader in DecryptingSectorSource::new(reader, keys) before the read loop. The inline decrypt_sectors call goes away — read_sectors yields plaintext directly. - DiscStream (mux/disc.rs): constructor wraps the underlying Box<dyn SectorReader> in DecryptingSectorSource so the internal fill_extents / read path sees plaintext bytes. The DecryptKeys field stays on DiscStream for metadata-side use; it just no longer drives decryption. Disc::patch carried the same inline decrypt step at three call sites (main read, backtrack read, non-NOT_READY retry read). All three migrated onto the same wrapping for a single audit surface. Two small support changes carry the migration without touching the round-1 decorator shape: - sector/mod.rs gains specific SectorSource impls for &mut dyn SectorReader and Box<dyn SectorReader>, mirroring std's Read forwarding pattern. Generic blankets would conflict with the existing SectorReader → SectorSource blanket under the orphan rule (downstream could impl SectorReader for &mut U), so the impls are scoped to the dyn-trait shape we actually consume. - sector/decrypting.rs gains DecryptingSectorSource::set_keys so DiscStream::set_raw() can flip the wrapped reader to a DecryptKeys::None pass-through without rebuilding the decorator (which would require moving the inner Box out from behind &mut self). After this commit, grep `decrypt_sectors` in src/ shows the function definition, its single use inside DecryptingSectorSource, plus comments only. One audit surface for AACS / CSS / passthrough correctness. Behaviour-preserving: same plaintext bytes flow through; the only difference is which type owns the decrypt step. See (internal)/memory/0_18_redesign.md. Single contributor: MattJackson.
This commit is contained in:
@@ -53,6 +53,16 @@ impl<S: SectorSource> DecryptingSectorSource<S> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Replace the configured keys without unwrapping the decorator.
|
||||
/// Used by `DiscStream::set_raw()` to flip from encrypted-disc
|
||||
/// decryption to a pass-through after the inner reader is already
|
||||
/// owned by the wrapper. For new construction prefer [`new`].
|
||||
///
|
||||
/// [`new`]: Self::new
|
||||
pub fn set_keys(&mut self, keys: DecryptKeys) {
|
||||
self.keys = keys;
|
||||
}
|
||||
|
||||
/// Borrow the inner source. Useful for tests and for adapters
|
||||
/// that want to introspect the underlying drive / file without
|
||||
/// unwrapping the decorator.
|
||||
|
||||
@@ -144,6 +144,54 @@ impl<T: SectorReader + ?Sized> SectorSource for T {
|
||||
}
|
||||
}
|
||||
|
||||
// Forwarding impls so callers can wrap `&mut dyn SectorReader` /
|
||||
// `Box<dyn SectorReader>` in [`DecryptingSectorSource`] without
|
||||
// having to unbox or re-borrow inside the lib's hot paths. The
|
||||
// generic `&mut T` / `Box<T>` blankets would conflict with the
|
||||
// `SectorReader → SectorSource` blanket above (a downstream crate
|
||||
// could `impl SectorReader for &mut U`); the specific
|
||||
// `dyn SectorReader` instantiations are unambiguous because
|
||||
// `SectorReader` is the very trait whose `dyn` we're targeting.
|
||||
impl SectorSource for &mut (dyn SectorReader + '_) {
|
||||
fn capacity_sectors(&self) -> u32 {
|
||||
<dyn SectorReader as SectorReader>::capacity(*self)
|
||||
}
|
||||
|
||||
fn read_sectors(
|
||||
&mut self,
|
||||
lba: u32,
|
||||
count: u16,
|
||||
buf: &mut [u8],
|
||||
recovery: bool,
|
||||
) -> Result<usize> {
|
||||
<dyn SectorReader as SectorReader>::read_sectors(*self, lba, count, buf, recovery)
|
||||
}
|
||||
|
||||
fn set_speed(&mut self, kbs: u16) {
|
||||
<dyn SectorReader as SectorReader>::set_speed(*self, kbs)
|
||||
}
|
||||
}
|
||||
|
||||
impl SectorSource for Box<dyn SectorReader> {
|
||||
fn capacity_sectors(&self) -> u32 {
|
||||
<dyn SectorReader as SectorReader>::capacity(&**self)
|
||||
}
|
||||
|
||||
fn read_sectors(
|
||||
&mut self,
|
||||
lba: u32,
|
||||
count: u16,
|
||||
buf: &mut [u8],
|
||||
recovery: bool,
|
||||
) -> Result<usize> {
|
||||
<dyn SectorReader as SectorReader>::read_sectors(&mut **self, lba, count, buf, recovery)
|
||||
}
|
||||
|
||||
fn set_speed(&mut self, kbs: u16) {
|
||||
<dyn SectorReader as SectorReader>::set_speed(&mut **self, kbs)
|
||||
}
|
||||
}
|
||||
|
||||
pub use decrypting::DecryptingSectorSource;
|
||||
pub use file::{FileSectorSink, FileSectorSource};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user