Skip to content

Commit

Permalink
as an alternative to dalek-cryptography#288 this updates serde `Seria…
Browse files Browse the repository at this point in the history
…lize` and `Deserialize` implementations to use a

custom visitor, removing the need for `alloc` or `std` for embedded use, and making this
consistent with implementations in
[curve25519-dalek](https://github.com/dalek-cryptography/curve25519-dalek/blob/a63e14f4ded078d6bf262ba0b3f47026bdd7f7c0/src/edwards.rs#L269).

@pinkforest seems like it'd be good to have some serde tests / this should go over dalek-cryptography#289?

Co-Authored-By: Vlad Semenov <[email protected]>
  • Loading branch information
ryankurte and semenov-vladyslav committed Mar 21, 2023
1 parent 9577d1e commit 863f1dc
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 28 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ jobs:
- uses: taiki-e/install-action@cargo-hack
# No default features build
- run: cargo build --target thumbv7em-none-eabi --release --no-default-features
# TODO: serde pending PR#288
- run: cargo hack build --target thumbv7em-none-eabi --release --each-feature --exclude-features default,std,serde
- run: cargo hack build --target thumbv7em-none-eabi --release --each-feature --exclude-features default,std

bench:
name: Check that benchmarks compile
Expand Down
10 changes: 0 additions & 10 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ sha2 = { version = "0.10", default-features = false }
merlin = { version = "3", default-features = false, optional = true }
rand_core = { version = "0.6.4", default-features = false, optional = true }
serde = { version = "1.0", default-features = false, optional = true }
serde_bytes = { version = "0.11", optional = true }
zeroize = { version = "1.5", default-features = false, optional = true }

[dev-dependencies]
Expand Down Expand Up @@ -68,5 +67,5 @@ legacy_compatibility = []
pkcs8 = ["ed25519/pkcs8"]
pem = ["alloc", "ed25519/pem", "pkcs8"]
rand_core = ["dep:rand_core"]
serde = ["dep:serde", "serde_bytes", "ed25519/serde"]
serde = ["dep:serde", "ed25519/serde"]
zeroize = ["dep:zeroize", "curve25519-dalek/zeroize"]
39 changes: 32 additions & 7 deletions src/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@ use ed25519::pkcs8;
#[cfg(any(test, feature = "rand_core"))]
use rand_core::CryptoRngCore;

#[cfg(feature = "serde")]
use serde::de::Error as SerdeError;
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[cfg(feature = "serde")]
use serde_bytes::{ByteBuf as SerdeByteBuf, Bytes as SerdeBytes};

use sha2::Sha512;

Expand Down Expand Up @@ -634,7 +630,7 @@ impl Serialize for SigningKey {
where
S: Serializer,
{
SerdeBytes::new(&self.secret_key).serialize(serializer)
serializer.serialize_bytes(&self.secret_key)
}
}

Expand All @@ -644,8 +640,37 @@ impl<'d> Deserialize<'d> for SigningKey {
where
D: Deserializer<'d>,
{
let bytes = <SerdeByteBuf>::deserialize(deserializer)?;
Self::try_from(bytes.as_ref()).map_err(SerdeError::custom)
struct SigningKeyVisitor;

impl<'de> serde::de::Visitor<'de> for SigningKeyVisitor {
type Value = SigningKey;

fn expecting(&self, formatter: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
write!(formatter, concat!("An ed25519 signing (private) key"))
}

fn visit_borrowed_bytes<E: serde::de::Error>(
self,
bytes: &'de [u8],
) -> Result<Self::Value, E> {
SigningKey::try_from(bytes.as_ref()).map_err(E::custom)
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let mut bytes = [0u8; 32];
for i in 0..32 {
bytes[i] = seq
.next_element()?
.ok_or_else(|| serde::de::Error::invalid_length(i, &"expected 32 bytes"))?;
}
SigningKey::try_from(bytes).map_err(serde::de::Error::custom)
}
}

deserializer.deserialize_bytes(SigningKeyVisitor)
}
}

Expand Down
39 changes: 32 additions & 7 deletions src/verifying.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@ use sha2::Sha512;
#[cfg(feature = "pkcs8")]
use ed25519::pkcs8;

#[cfg(feature = "serde")]
use serde::de::Error as SerdeError;
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[cfg(feature = "serde")]
use serde_bytes::{ByteBuf as SerdeByteBuf, Bytes as SerdeBytes};

#[cfg(feature = "digest")]
use crate::context::Context;
Expand Down Expand Up @@ -542,7 +538,7 @@ impl Serialize for VerifyingKey {
where
S: Serializer,
{
SerdeBytes::new(self.as_bytes()).serialize(serializer)
serializer.serialize_bytes(&self.as_bytes()[..])
}
}

Expand All @@ -552,7 +548,36 @@ impl<'d> Deserialize<'d> for VerifyingKey {
where
D: Deserializer<'d>,
{
let bytes = <SerdeByteBuf>::deserialize(deserializer)?;
VerifyingKey::try_from(bytes.as_ref()).map_err(SerdeError::custom)
struct VerifyingKeyVisitor;

impl<'de> serde::de::Visitor<'de> for VerifyingKeyVisitor {
type Value = VerifyingKey;

fn expecting(&self, formatter: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
write!(formatter, concat!("An ed25519 verifying (public) key"))
}

fn visit_borrowed_bytes<E: serde::de::Error>(
self,
bytes: &'de [u8],
) -> Result<Self::Value, E> {
VerifyingKey::try_from(bytes.as_ref()).map_err(E::custom)
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let mut bytes = [0u8; 32];
for i in 0..32 {
bytes[i] = seq
.next_element()?
.ok_or_else(|| serde::de::Error::invalid_length(i, &"expected 32 bytes"))?;
}
VerifyingKey::try_from(&bytes[..]).map_err(serde::de::Error::custom)
}
}

deserializer.deserialize_bytes(VerifyingKeyVisitor)
}
}

0 comments on commit 863f1dc

Please sign in to comment.