pub struct Sm4CtrCipher { /* private fields */ }Expand description
Streaming SM4-CTR cipher.
Construct with Sm4CtrCipher::new, feed input/output buffer
pairs through update, drop or finalize when done. Single
struct serves both encrypt and decrypt (CTR is symmetric).
State machine:
counter: next 128-bit BE counter to evaluate.leftover: the most recently evaluated keystream block.leftover_pos: in0..=16. Bytes[leftover_pos..16]ofleftoverare unconsumed keystream from a previous partial call; on nextupdatethey’re consumed first before the counter advances.
Internal invariant: when leftover_pos == 16, no carried-over
keystream; the next byte requires a fresh encrypt_block of
counter. Initial state is leftover_pos = 16 (no leftover).
Implementations§
Source§impl Sm4CtrCipher
impl Sm4CtrCipher
Sourcepub fn new(key: &[u8; 16], counter: &[u8; 16]) -> Self
pub fn new(key: &[u8; 16], counter: &[u8; 16]) -> Self
Construct from a 16-byte key and a 16-byte initial counter.
Counter is treated as a 128-bit BE integer; per-block
keystream is SM4_E(key, counter + i) for i = 0..N-1.
Sourcepub fn update(&mut self, input: &[u8], output: &mut [u8])
pub fn update(&mut self, input: &[u8], output: &mut [u8])
Consume input and write input.len() bytes of output. The
output buffer must be at least as long as the input; only the
leading input.len() bytes are written.
§Panics
Panics if output.len() < input.len().
Sourcepub fn finalize(self)
pub fn finalize(self)
Finalize and drop the cipher. CTR has no padding to flush and
no authenticity bits to emit, so this is a stateless drop.
Provided for symmetry with super::cbc_streaming and so
the call site reads intuitively.