Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions block-buffer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## UNRELEASED
### Added
- `ReadBuffer` type ([#823])
- `serialize` and `deserialize` methods ([#823])
- Optional implementation of the `Zeroize` trait ([#963])
- Generic `serialize` and `deserialize` methods ([#1200])

### Changed
- Block sizes must be bigger than 0 and smaller than 256.
Expand All @@ -20,11 +20,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Removed
- `EagerBuffer::set_data` method. Use the `ReadBuffer` type instead. ([#823])

[#823]: https://github.com/RustCrypto/utils/pull/823
[#963]: https://github.com/RustCrypto/utils/pull/963
[#1115]: https://github.com/RustCrypto/utils/pull/1115
[#1115]: https://github.com/RustCrypto/utils/pull/1116
[#1149]: https://github.com/RustCrypto/utils/pull/1149
[#1200]: https://github.com/RustCrypto/utils/pull/1200

## 0.10.3 (2022-09-04)
### Added
Expand Down
111 changes: 43 additions & 68 deletions block-buffer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,8 @@

pub use hybrid_array as array;

use array::{
Array, ArraySize,
typenum::{Add1, B1},
};
use core::{fmt, mem::MaybeUninit, ops::Add, ptr, slice};
use array::{Array, ArraySize, typenum::Sum};
use core::{fmt, mem::MaybeUninit, ptr, slice};

#[cfg(feature = "zeroize")]
use zeroize::{Zeroize, ZeroizeOnDrop};
Expand Down Expand Up @@ -314,6 +311,47 @@ impl<BS: ArraySize, K: BufferKind> BlockBuffer<BS, K> {
}
}

/// Size of serialized `BlockBuffer` in bytes.
pub type SerializedBufferSize<BS, K> = Sum<BS, <K as sealed::Sealed>::Overhead>;
/// `BlockBuffer` serialized as a byte array.
pub type SerializedBuffer<BS, K> = Array<u8, SerializedBufferSize<BS, K>>;

impl<BS: ArraySize, K: BufferKind> BlockBuffer<BS, K>
where
BS: core::ops::Add<K::Overhead>,
Sum<BS, K::Overhead>: ArraySize,
{
/// Serialize buffer into a byte array.
pub fn serialize(&self) -> SerializedBuffer<BS, K> {
let mut buf = SerializedBuffer::<BS, K>::default();
let data = self.get_data();
let (pos, block) = buf.split_at_mut(1);
pos[0] = u8::try_from(data.len()).expect("buffer size is smaller than 256");
block[..data.len()].copy_from_slice(data);
buf
}

/// Deserialize buffer from a byte array.
pub fn deserialize(buf: &SerializedBuffer<BS, K>) -> Result<Self, Error> {
let (pos, block) = buf.split_at(1);
let pos = usize::from(pos[0]);

if !<K as sealed::Sealed>::invariant(pos, BS::USIZE) {
return Err(Error);
}

let (data, tail) = block.split_at(pos);

if tail.iter().any(|&b| b != 0) {
return Err(Error);
}

let mut res = Self::default();
unsafe { res.set_data_unchecked(data) };
Ok(res)
}
}

impl<BS: ArraySize> BlockBuffer<BS, Eager> {
/// Compress remaining data after padding it with `delim`, zeros and
/// the `suffix` bytes. If there is not enough unused space, `compress`
Expand Down Expand Up @@ -368,69 +406,6 @@ impl<BS: ArraySize> BlockBuffer<BS, Eager> {
pub fn len128_padding_be(&mut self, data_len: u128, compress: impl FnMut(&Array<u8, BS>)) {
self.digest_pad(0x80, &data_len.to_be_bytes(), compress);
}

/// Serialize buffer into a byte array.
#[inline]
pub fn serialize(&self) -> Array<u8, BS> {
let mut res = Array::<u8, BS>::default();
let data = self.get_data();
res[..data.len()].copy_from_slice(data);
res[BS::USIZE - 1] = data.len() as u8;
res
}

/// Deserialize buffer from a byte array.
#[inline]
pub fn deserialize(buffer: &Array<u8, BS>) -> Result<Self, Error> {
let pos = buffer[BS::USIZE - 1] as usize;
if !<Eager as sealed::Sealed>::invariant(pos, BS::USIZE) {
return Err(Error);
}
if buffer[pos..BS::USIZE - 1].iter().any(|&b| b != 0) {
return Err(Error);
}
Ok(Self {
buffer: MaybeUninit::new(buffer.clone()),
pos: Default::default(),
})
}
}

impl<BS: ArraySize> BlockBuffer<BS, Lazy> {
/// Serialize buffer into a byte array.
#[inline]
pub fn serialize(&self) -> Array<u8, Add1<BS>>
where
BS: Add<B1>,
Add1<BS>: ArraySize,
{
let mut res = Array::<u8, Add1<BS>>::default();
res[0] = self.pos;
let data = self.get_data();
res[1..][..data.len()].copy_from_slice(data);
res
}

/// Deserialize buffer from a byte array.
#[inline]
pub fn deserialize(buffer: &Array<u8, Add1<BS>>) -> Result<Self, Error>
where
BS: Add<B1>,
Add1<BS>: ArraySize,
{
let pos = buffer[0];
if !<Lazy as sealed::Sealed>::invariant(pos as usize, BS::USIZE) {
return Err(Error);
}
if buffer[1..][pos as usize..].iter().any(|&b| b != 0) {
return Err(Error);
}
let buf = Array::try_from(&buffer[1..]).expect("slice has correct length");
Ok(Self {
buffer: MaybeUninit::new(buf),
pos,
})
}
}

#[cfg(feature = "zeroize")]
Expand Down
6 changes: 6 additions & 0 deletions block-buffer/src/sealed.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use hybrid_array::sizes::{U0, U1};

use super::{Array, ArraySize};
use core::{mem::MaybeUninit, ptr, slice};

Expand All @@ -10,6 +12,8 @@ pub trait Sealed {
#[cfg(feature = "zeroize")]
type Pos: Default + Clone + zeroize::Zeroize;

type Overhead: ArraySize;

const NAME: &'static str;

fn get_pos<N: ArraySize>(buf: &Block<N>, pos: &Self::Pos) -> usize;
Expand All @@ -26,6 +30,7 @@ pub trait Sealed {

impl Sealed for super::Eager {
type Pos = ();
type Overhead = U0;
const NAME: &'static str = "BlockBuffer<Eager>";

fn get_pos<N: ArraySize>(buf: &Block<N>, _pos: &Self::Pos) -> usize {
Expand Down Expand Up @@ -72,6 +77,7 @@ impl Sealed for super::Eager {

impl Sealed for super::Lazy {
type Pos = u8;
type Overhead = U1;
const NAME: &'static str = "BlockBuffer<Lazy>";

fn get_pos<N: ArraySize>(_buf_val: &Block<N>, pos: &Self::Pos) -> usize {
Expand Down
10 changes: 5 additions & 5 deletions block-buffer/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fn test_eager_serialize() {
buf1.digest_blocks(&[41, 42], |_| {});

let ser1 = buf1.serialize();
assert_eq!(&ser1[..], &[41, 42, 0, 2]);
assert_eq!(&ser1[..], &[2, 41, 42, 0]);

let mut buf2 = Buf::deserialize(&ser1).unwrap();
assert_eq!(buf1.serialize(), ser1);
Expand All @@ -192,7 +192,7 @@ fn test_eager_serialize() {
buf2.digest_blocks(&[43], |_| {});

let ser2 = buf1.serialize();
assert_eq!(&ser2[..], &[41, 42, 43, 3]);
assert_eq!(&ser2[..], &[3, 41, 42, 43]);
assert_eq!(buf1.serialize(), ser2);

let mut buf3 = Buf::deserialize(&ser2).unwrap();
Expand All @@ -213,11 +213,11 @@ fn test_eager_serialize() {
let buf = Array([0, 0, 0, 10]);
assert!(Buf::deserialize(&buf).is_err());
// "Garbage" bytes are not zeroized
let buf = Array([1, 0, 0, 0]);
let buf = Array([0, 1, 0, 0]);
assert!(Buf::deserialize(&buf).is_err());
let buf = Array([0, 1, 0, 1]);
let buf = Array([1, 0, 1, 0]);
assert!(Buf::deserialize(&buf).is_err());
let buf = Array([0, 0, 1, 2]);
let buf = Array([2, 0, 0, 1]);
assert!(Buf::deserialize(&buf).is_err());
}

Expand Down