pub struct Ctr<B: BlockCipher + Zeroize> { /* private fields */ }
Expand description
Counter mode implementation with secure memory handling
Implementations§
Source§impl<B: BlockCipher + Zeroize> Ctr<B>
impl<B: BlockCipher + Zeroize> Ctr<B>
Sourcepub fn new<const N: usize>(cipher: B, nonce: &Nonce<N>) -> Result<Self>where
Nonce<N>: AesCtrCompatible,
pub fn new<const N: usize>(cipher: B, nonce: &Nonce<N>) -> Result<Self>where
Nonce<N>: AesCtrCompatible,
Creates a new CTR mode instance with the default configuration
cipher
- The block cipher to usenonce
- The nonce (must be compatible with CTR mode)
This creates a standard CTR mode with the counter in the last 4 bytes and the nonce filling the beginning of the counter block.
Sourcepub fn with_counter_params<const N: usize>(
cipher: B,
nonce: &Nonce<N>,
counter_pos: CounterPosition,
counter_size: usize,
) -> Result<Self>where
Nonce<N>: AesCtrCompatible,
pub fn with_counter_params<const N: usize>(
cipher: B,
nonce: &Nonce<N>,
counter_pos: CounterPosition,
counter_size: usize,
) -> Result<Self>where
Nonce<N>: AesCtrCompatible,
Creates a new CTR mode instance with custom counter parameters
cipher
- The block cipher to usenonce
- The nonce (must be compatible with CTR mode)counter_pos
- Position of the counter within the counter blockcounter_size
- Size of the counter in bytes (1-8)
This allows for flexible counter block layouts to match different standards and implementations.
Sourcepub fn encrypt(&mut self, plaintext: &[u8]) -> Result<Vec<u8>>
pub fn encrypt(&mut self, plaintext: &[u8]) -> Result<Vec<u8>>
Encrypts a message using CTR mode
Sourcepub fn decrypt(&mut self, ciphertext: &[u8]) -> Result<Vec<u8>>
pub fn decrypt(&mut self, ciphertext: &[u8]) -> Result<Vec<u8>>
Decrypts a message using CTR mode In CTR mode, encryption and decryption are the same operation
Sourcepub fn process(&mut self, data: &mut [u8]) -> Result<()>
pub fn process(&mut self, data: &mut [u8]) -> Result<()>
Process data in place (encrypt or decrypt)
Sourcepub fn keystream(&mut self, output: &mut [u8]) -> Result<()>
pub fn keystream(&mut self, output: &mut [u8]) -> Result<()>
Generate keystream directly into an output buffer
Sourcepub fn seek(&mut self, block_offset: u32)
pub fn seek(&mut self, block_offset: u32)
Seek to a specific block position
block_offset
is the number of full blocks that have been consumed;
after seeking, the next generated block will be at block_offset + 1
.
Sourcepub fn set_counter(&mut self, counter: u32)
pub fn set_counter(&mut self, counter: u32)
Set the counter value directly
This allows for manual control of the counter, which can be useful for seeking to specific positions in the stream.
§Arguments
counter
- The new counter value
Sourcepub fn reset<const N: usize>(
&mut self,
nonce: Option<&Nonce<N>>,
counter: u32,
) -> Result<()>where
Nonce<N>: AesCtrCompatible,
pub fn reset<const N: usize>(
&mut self,
nonce: Option<&Nonce<N>>,
counter: u32,
) -> Result<()>where
Nonce<N>: AesCtrCompatible,
Reset to initial state with the same key and nonce
This resets the counter to 0 and clears any buffered keystream.
§Arguments
nonce
- Optional new nonce to use (if not provided, keeps the current nonce)counter
- Optional initial counter value (defaults to 0)