Struct block_buffer::BlockBuffer
source · pub struct BlockBuffer<BS: BlockSizes, K: BufferKind> { /* private fields */ }Expand description
Buffer for block processing of data.
Implementations§
source§impl<BS: BlockSizes, K: BufferKind> BlockBuffer<BS, K>
impl<BS: BlockSizes, K: BufferKind> BlockBuffer<BS, K>
sourcepub fn try_new(buf: &[u8]) -> Result<Self, Error>
pub fn try_new(buf: &[u8]) -> Result<Self, Error>
Create new buffer from slice.
Returns an error if slice length is not valid for used buffer kind.
sourcepub fn digest_blocks(
&mut self,
input: &[u8],
compress: impl FnMut(&[Block<Self>])
)
pub fn digest_blocks(
&mut self,
input: &[u8],
compress: impl FnMut(&[Block<Self>])
)
Digest data in input in blocks of size BlockSize using
the compress function, which accepts slice of blocks.
sourcepub fn pad_with_zeros(&mut self) -> Block<Self>
pub fn pad_with_zeros(&mut self) -> Block<Self>
Pad remaining data with zeros and return resulting block.
sourcepub fn get_pos(&self) -> usize
pub fn get_pos(&self) -> usize
Return current cursor position.
Examples found in repository?
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
pub fn digest_blocks(&mut self, mut input: &[u8], mut compress: impl FnMut(&[Block<Self>])) {
let pos = self.get_pos();
// using `self.remaining()` for some reason
// prevents panic elimination
let rem = self.size() - pos;
let n = input.len();
// Note that checking condition `pos + n < BlockSize` is
// equivalent to checking `n < rem`, where `rem` is equal
// to `BlockSize - pos`. Using the latter allows us to work
// around compiler accounting for possible overflow of
// `pos + n` which results in it inserting unreachable
// panic branches. Using `unreachable_unchecked` in `get_pos`
// we convince compiler that `BlockSize - pos` never underflows.
if K::invariant(n, rem) {
// double slicing allows to remove panic branches
self.buffer[pos..][..n].copy_from_slice(input);
self.set_pos_unchecked(pos + n);
return;
}
if pos != 0 {
let (left, right) = input.split_at(rem);
input = right;
self.buffer[pos..].copy_from_slice(left);
compress(slice::from_ref(&self.buffer));
}
let (blocks, leftover) = K::split_blocks(input);
if !blocks.is_empty() {
compress(blocks);
}
let n = leftover.len();
self.buffer[..n].copy_from_slice(leftover);
self.set_pos_unchecked(n);
}
/// Reset buffer by setting cursor position to zero.
#[inline(always)]
pub fn reset(&mut self) {
self.set_pos_unchecked(0);
}
/// Pad remaining data with zeros and return resulting block.
#[inline(always)]
pub fn pad_with_zeros(&mut self) -> Block<Self> {
let pos = self.get_pos();
let mut res = self.buffer.clone();
res[pos..].iter_mut().for_each(|b| *b = 0);
self.set_pos_unchecked(0);
res
}
/// Return current cursor position.
#[inline(always)]
pub fn get_pos(&self) -> usize {
let pos = K::get_pos(&self.buffer, &self.pos);
if !K::invariant(pos, BS::USIZE) {
debug_assert!(false);
// SAFETY: `pos` never breaks the invariant
unsafe {
core::hint::unreachable_unchecked();
}
}
pos
}
/// Return slice of data stored inside the buffer.
#[inline(always)]
pub fn get_data(&self) -> &[u8] {
&self.buffer[..self.get_pos()]
}
/// Set buffer content and cursor position.
///
/// # Panics
/// If `pos` is bigger or equal to block size.
#[inline]
pub fn set(&mut self, buf: Block<Self>, pos: usize) {
assert!(K::invariant(pos, BS::USIZE));
self.buffer = buf;
self.set_pos_unchecked(pos);
}
/// Return size of the internal buffer in bytes.
#[inline(always)]
pub fn size(&self) -> usize {
BS::USIZE
}
/// Return number of remaining bytes in the internal buffer.
#[inline(always)]
pub fn remaining(&self) -> usize {
self.size() - self.get_pos()
}
#[inline(always)]
fn set_pos_unchecked(&mut self, pos: usize) {
debug_assert!(K::invariant(pos, BS::USIZE));
K::set_pos(&mut self.buffer, &mut self.pos, pos)
}
}
impl<BS: BlockSizes> BlockBuffer<BS, Eager> {
/// Compress remaining data after padding it with `delim`, zeros and
/// the `suffix` bytes. If there is not enough unused space, `compress`
/// will be called twice.
///
/// # Panics
/// If suffix length is bigger than block size.
#[inline(always)]
pub fn digest_pad(&mut self, delim: u8, suffix: &[u8], mut compress: impl FnMut(&Block<Self>)) {
if suffix.len() > BS::USIZE {
panic!("suffix is too long");
}
let pos = self.get_pos();
self.buffer[pos] = delim;
for b in &mut self.buffer[pos + 1..] {
*b = 0;
}
let n = self.size() - suffix.len();
if self.size() - pos - 1 < suffix.len() {
compress(&self.buffer);
let mut block = Block::<Self>::default();
block[n..].copy_from_slice(suffix);
compress(&block);
} else {
self.buffer[n..].copy_from_slice(suffix);
compress(&self.buffer);
}
self.set_pos_unchecked(0)
}
/// Pad message with 0x80, zeros and 64-bit message length using
/// big-endian byte order.
#[inline]
pub fn len64_padding_be(&mut self, data_len: u64, compress: impl FnMut(&Block<Self>)) {
self.digest_pad(0x80, &data_len.to_be_bytes(), compress);
}
/// Pad message with 0x80, zeros and 64-bit message length using
/// little-endian byte order.
#[inline]
pub fn len64_padding_le(&mut self, data_len: u64, compress: impl FnMut(&Block<Self>)) {
self.digest_pad(0x80, &data_len.to_le_bytes(), compress);
}
/// Pad message with 0x80, zeros and 128-bit message length using
/// big-endian byte order.
#[inline]
pub fn len128_padding_be(&mut self, data_len: u128, compress: impl FnMut(&Block<Self>)) {
self.digest_pad(0x80, &data_len.to_be_bytes(), compress);
}
/// Serialize buffer into a byte array.
#[inline]
pub fn serialize(&self) -> Block<Self> {
let mut res = self.buffer.clone();
let pos = self.get_pos();
// zeroize "garbage" data
for b in res[pos..BS::USIZE - 1].iter_mut() {
*b = 0;
}
res
}sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Return size of the internal buffer in bytes.
Examples found in repository?
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
pub fn digest_blocks(&mut self, mut input: &[u8], mut compress: impl FnMut(&[Block<Self>])) {
let pos = self.get_pos();
// using `self.remaining()` for some reason
// prevents panic elimination
let rem = self.size() - pos;
let n = input.len();
// Note that checking condition `pos + n < BlockSize` is
// equivalent to checking `n < rem`, where `rem` is equal
// to `BlockSize - pos`. Using the latter allows us to work
// around compiler accounting for possible overflow of
// `pos + n` which results in it inserting unreachable
// panic branches. Using `unreachable_unchecked` in `get_pos`
// we convince compiler that `BlockSize - pos` never underflows.
if K::invariant(n, rem) {
// double slicing allows to remove panic branches
self.buffer[pos..][..n].copy_from_slice(input);
self.set_pos_unchecked(pos + n);
return;
}
if pos != 0 {
let (left, right) = input.split_at(rem);
input = right;
self.buffer[pos..].copy_from_slice(left);
compress(slice::from_ref(&self.buffer));
}
let (blocks, leftover) = K::split_blocks(input);
if !blocks.is_empty() {
compress(blocks);
}
let n = leftover.len();
self.buffer[..n].copy_from_slice(leftover);
self.set_pos_unchecked(n);
}
/// Reset buffer by setting cursor position to zero.
#[inline(always)]
pub fn reset(&mut self) {
self.set_pos_unchecked(0);
}
/// Pad remaining data with zeros and return resulting block.
#[inline(always)]
pub fn pad_with_zeros(&mut self) -> Block<Self> {
let pos = self.get_pos();
let mut res = self.buffer.clone();
res[pos..].iter_mut().for_each(|b| *b = 0);
self.set_pos_unchecked(0);
res
}
/// Return current cursor position.
#[inline(always)]
pub fn get_pos(&self) -> usize {
let pos = K::get_pos(&self.buffer, &self.pos);
if !K::invariant(pos, BS::USIZE) {
debug_assert!(false);
// SAFETY: `pos` never breaks the invariant
unsafe {
core::hint::unreachable_unchecked();
}
}
pos
}
/// Return slice of data stored inside the buffer.
#[inline(always)]
pub fn get_data(&self) -> &[u8] {
&self.buffer[..self.get_pos()]
}
/// Set buffer content and cursor position.
///
/// # Panics
/// If `pos` is bigger or equal to block size.
#[inline]
pub fn set(&mut self, buf: Block<Self>, pos: usize) {
assert!(K::invariant(pos, BS::USIZE));
self.buffer = buf;
self.set_pos_unchecked(pos);
}
/// Return size of the internal buffer in bytes.
#[inline(always)]
pub fn size(&self) -> usize {
BS::USIZE
}
/// Return number of remaining bytes in the internal buffer.
#[inline(always)]
pub fn remaining(&self) -> usize {
self.size() - self.get_pos()
}
#[inline(always)]
fn set_pos_unchecked(&mut self, pos: usize) {
debug_assert!(K::invariant(pos, BS::USIZE));
K::set_pos(&mut self.buffer, &mut self.pos, pos)
}
}
impl<BS: BlockSizes> BlockBuffer<BS, Eager> {
/// Compress remaining data after padding it with `delim`, zeros and
/// the `suffix` bytes. If there is not enough unused space, `compress`
/// will be called twice.
///
/// # Panics
/// If suffix length is bigger than block size.
#[inline(always)]
pub fn digest_pad(&mut self, delim: u8, suffix: &[u8], mut compress: impl FnMut(&Block<Self>)) {
if suffix.len() > BS::USIZE {
panic!("suffix is too long");
}
let pos = self.get_pos();
self.buffer[pos] = delim;
for b in &mut self.buffer[pos + 1..] {
*b = 0;
}
let n = self.size() - suffix.len();
if self.size() - pos - 1 < suffix.len() {
compress(&self.buffer);
let mut block = Block::<Self>::default();
block[n..].copy_from_slice(suffix);
compress(&block);
} else {
self.buffer[n..].copy_from_slice(suffix);
compress(&self.buffer);
}
self.set_pos_unchecked(0)
}source§impl<BS: BlockSizes> BlockBuffer<BS, Eager>
impl<BS: BlockSizes> BlockBuffer<BS, Eager>
sourcepub fn digest_pad(
&mut self,
delim: u8,
suffix: &[u8],
compress: impl FnMut(&Block<Self>)
)
pub fn digest_pad(
&mut self,
delim: u8,
suffix: &[u8],
compress: impl FnMut(&Block<Self>)
)
Compress remaining data after padding it with delim, zeros and
the suffix bytes. If there is not enough unused space, compress
will be called twice.
Panics
If suffix length is bigger than block size.
Examples found in repository?
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
pub fn len64_padding_be(&mut self, data_len: u64, compress: impl FnMut(&Block<Self>)) {
self.digest_pad(0x80, &data_len.to_be_bytes(), compress);
}
/// Pad message with 0x80, zeros and 64-bit message length using
/// little-endian byte order.
#[inline]
pub fn len64_padding_le(&mut self, data_len: u64, compress: impl FnMut(&Block<Self>)) {
self.digest_pad(0x80, &data_len.to_le_bytes(), compress);
}
/// Pad message with 0x80, zeros and 128-bit message length using
/// big-endian byte order.
#[inline]
pub fn len128_padding_be(&mut self, data_len: u128, compress: impl FnMut(&Block<Self>)) {
self.digest_pad(0x80, &data_len.to_be_bytes(), compress);
}sourcepub fn len64_padding_be(
&mut self,
data_len: u64,
compress: impl FnMut(&Block<Self>)
)
pub fn len64_padding_be(
&mut self,
data_len: u64,
compress: impl FnMut(&Block<Self>)
)
Pad message with 0x80, zeros and 64-bit message length using big-endian byte order.
sourcepub fn len64_padding_le(
&mut self,
data_len: u64,
compress: impl FnMut(&Block<Self>)
)
pub fn len64_padding_le(
&mut self,
data_len: u64,
compress: impl FnMut(&Block<Self>)
)
Pad message with 0x80, zeros and 64-bit message length using little-endian byte order.
sourcepub fn len128_padding_be(
&mut self,
data_len: u128,
compress: impl FnMut(&Block<Self>)
)
pub fn len128_padding_be(
&mut self,
data_len: u128,
compress: impl FnMut(&Block<Self>)
)
Pad message with 0x80, zeros and 128-bit message length using big-endian byte order.
sourcepub fn deserialize(buffer: &Block<Self>) -> Result<Self, Error>
pub fn deserialize(buffer: &Block<Self>) -> Result<Self, Error>
Deserialize buffer from a byte array.
source§impl<BS: BlockSizes> BlockBuffer<BS, Lazy>
impl<BS: BlockSizes> BlockBuffer<BS, Lazy>
sourcepub fn serialize(&self) -> GenericArray<u8, Add1<BS>>where
BS: Add<B1>,
Add1<BS>: ArrayLength<u8>,
pub fn serialize(&self) -> GenericArray<u8, Add1<BS>>where
BS: Add<B1>,
Add1<BS>: ArrayLength<u8>,
Serialize buffer into a byte array.
sourcepub fn deserialize(buffer: &GenericArray<u8, Add1<BS>>) -> Result<Self, Error>where
BS: Add<B1>,
Add1<BS>: ArrayLength<u8>,
pub fn deserialize(buffer: &GenericArray<u8, Add1<BS>>) -> Result<Self, Error>where
BS: Add<B1>,
Add1<BS>: ArrayLength<u8>,
Deserialize buffer from a byte array.