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

fix serde / no_std incompatibility #291

Merged
merged 1 commit into from
Mar 21, 2023
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
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)
}
}