Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serdect: safer bytes handling #1112

Merged
merged 9 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
29 changes: 29 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions serdect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ bincode = "1"
ciborium = "0.2"
hex-literal = "0.4"
proptest = "1"
rmp-serde = "1"
serde = { version = "1.0.119", default-features = false, features = ["derive"] }
serde_json = "1"
serde-json-core = { version = "0.5", default-features = false, features = ["std"] }
Expand Down
9 changes: 9 additions & 0 deletions serdect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ other kinds of data-dependent branching on the contents of the serialized data,
using a constant-time hex serialization with human-readable formats should
help reduce the overall timing variability.

`serdect` is tested against the following crates:
- [`bincode`](https://crates.io/crates/bincode) v1
- [`ciborium`](https://crates.io/crates/ciborium) v0.2
- [`rmp-serde`](https://crates.io/crates/rmp-serde) v1
- [`serde-json-core`](https://crates.io/crates/serde-json-core) v0.5
- [`serde-json`](https://crates.io/crates/serde-json) v1
- [`toml`](https://crates.io/crates/toml) v0.7


## Minimum Supported Rust Version

Rust **1.60** or newer.
Expand Down
93 changes: 19 additions & 74 deletions serdect/src/array.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
//! Serialization primitives for arrays.

use core::fmt;
// Unfortunately, we currently cannot assert generically that we are serializing
// a fixed-size byte array.
fjarri marked this conversation as resolved.
Show resolved Hide resolved
// See https://github.com/serde-rs/serde/issues/2120 for the discussion.
// Therefore we have to fall back to the slice methods,
// which will add the size information in the binary formats.
// The only difference is that for the arrays we require the size of the data
// to be exactly equal to the size of the buffer during deserialization,
// while for slices the buffer can be larger than the deserialized data.

use core::marker::PhantomData;

use serde::de::{Error, SeqAccess, Visitor};
use serde::ser::SerializeTuple;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::slice;

#[cfg(feature = "zeroize")]
use zeroize::Zeroize;

Expand All @@ -16,17 +25,7 @@ where
S: Serializer,
T: AsRef<[u8]>,
{
if serializer.is_human_readable() {
crate::serialize_hex::<_, _, false>(value, serializer)
} else {
let mut seq = serializer.serialize_tuple(value.as_ref().len())?;

for byte in value.as_ref() {
seq.serialize_element(byte)?;
}

seq.end()
}
slice::serialize_hex_lower_or_bin(value, serializer)
}

/// Serialize the given type as upper case hex when using human-readable
Expand All @@ -36,17 +35,7 @@ where
S: Serializer,
T: AsRef<[u8]>,
{
if serializer.is_human_readable() {
crate::serialize_hex::<_, _, true>(value, serializer)
} else {
let mut seq = serializer.serialize_tuple(value.as_ref().len())?;

for byte in value.as_ref() {
seq.serialize_element(byte)?;
}

seq.end()
}
slice::serialize_hex_upper_or_bin(value, serializer)
}

/// Deserialize from hex when using human-readable formats or binary if the
Expand All @@ -57,56 +46,12 @@ where
D: Deserializer<'de>,
{
if deserializer.is_human_readable() {
struct StrVisitor<'b>(&'b mut [u8]);

impl<'de> Visitor<'de> for StrVisitor<'_> {
type Value = ();

fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "a string of length {}", self.0.len() * 2)
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
if v.len() != self.0.len() * 2 {
return Err(Error::invalid_length(v.len(), &self));
}

base16ct::mixed::decode(v, self.0).map_err(E::custom)?;

Ok(())
}
}

deserializer.deserialize_str(StrVisitor(buffer))
deserializer.deserialize_str(slice::StrVisitor::<slice::ExactLength>(buffer, PhantomData))
} else {
struct ArrayVisitor<'b>(&'b mut [u8]);

impl<'de> Visitor<'de> for ArrayVisitor<'_> {
type Value = ();

fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "an array of length {}", self.0.len())
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
for (index, byte) in self.0.iter_mut().enumerate() {
*byte = match seq.next_element()? {
Some(byte) => byte,
None => return Err(Error::invalid_length(index, &self)),
};
}

Ok(())
}
}

deserializer.deserialize_tuple(buffer.len(), ArrayVisitor(buffer))
deserializer.deserialize_byte_buf(slice::SliceVisitor::<slice::ExactLength>(
buffer,
PhantomData,
))
}
}

Expand Down
15 changes: 12 additions & 3 deletions serdect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,17 @@
//! let data = SecretData([42; 32]);
//!
//! let serialized = bincode::serialize(&data).unwrap();
//! // bincode, a binary serialization format, is serialized into bytes.
//! assert_eq!(serialized.as_slice(), [42; 32]);
//! // bincode, a binary serialization format is serialized into bytes.
//! assert_eq!(
//! serialized.as_slice(),
//! [
//! // Array size.
//! 32, 0, 0, 0, 0, 0, 0, 0,
//! // Actual data.
//! 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
//! 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
//! ]
//! );
//! # let deserialized: SecretData = bincode::deserialize(&serialized).unwrap();
//! # assert_eq!(deserialized, data);
//!
Expand Down Expand Up @@ -98,7 +107,7 @@
//! assert_eq!(
//! serialized.as_slice(),
//! [
//! // Not fixed-size, so a size will be encoded.
//! // Slice size.
//! 32, 0, 0, 0, 0, 0, 0, 0,
//! // Actual data.
//! 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
Expand Down
Loading