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
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions ed25519/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ signature = { version = "3", default-features = false }
# optional dependencies
pkcs8 = { version = "0.11", optional = true }
serdect = { version = "0.4", optional = true, default-features = false }
serde_bytes = { version = "0.11", optional = true, default-features = false }
zeroize = { version = "1", optional = true, default-features = false }
zerocopy = { version = "0.8", optional = true, features = ["derive"] }

Expand All @@ -36,6 +37,7 @@ default = ["alloc"]
alloc = ["pkcs8?/alloc", "signature/alloc"]
pem = ["alloc", "pkcs8/pem"]
serde = ["dep:serdect"]
serde_bytes = ["serde", "dep:serde_bytes"]

[lints]
workspace = true
Expand Down
37 changes: 6 additions & 31 deletions ed25519/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,13 @@
#[cfg(feature = "alloc")]
extern crate alloc;

mod hex;

#[cfg(feature = "pkcs8")]
pub mod pkcs8;

mod hex;
#[cfg(feature = "serde")]
mod serde;

pub use signature::{self, Error, SignatureEncoding};

#[cfg(feature = "pkcs8")]
Expand All @@ -275,20 +277,15 @@ use core::fmt;

#[cfg(feature = "alloc")]
use alloc::vec::Vec;
#[cfg(feature = "serde")]
use serdect::serde::{Deserialize, Serialize, de, ser};

#[cfg(all(feature = "alloc", feature = "pkcs8"))]
use pkcs8::spki::{
SignatureBitStringEncoding,
der::{self, asn1::BitString},
};

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

#[cfg(feature = "zerocopy")]
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Unaligned};
#[cfg(feature = "zeroize")]
use zeroize::Zeroize;

/// Size of a single component of an Ed25519 signature.
const COMPONENT_SIZE: usize = 32;
Expand Down Expand Up @@ -450,28 +447,6 @@ impl fmt::Display for Signature {
}
}

#[cfg(feature = "serde")]
impl Serialize for Signature {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
serdect::array::serialize_hex_upper_or_bin(&self.to_bytes(), serializer)
}
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Signature {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
let mut bytes = [0u8; Signature::BYTE_SIZE];
serdect::array::deserialize_hex_or_bin(&mut bytes, deserializer)?;
Ok(bytes.into())
}
}

#[cfg(feature = "zeroize")]
impl Zeroize for Signature {
fn zeroize(&mut self) {
Expand Down
70 changes: 70 additions & 0 deletions ed25519/src/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//! `serde` support including optional `serde_bytes` support.

use crate::{Signature, SignatureBytes};
use core::fmt;
use serdect::serde::{Deserialize, Serialize, de, ser};

#[cfg(feature = "serde")]
impl Serialize for Signature {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
serdect::array::serialize_hex_upper_or_bin(&self.to_bytes(), serializer)
}
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Signature {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
let mut bytes = [0u8; Signature::BYTE_SIZE];
serdect::array::deserialize_hex_or_bin(&mut bytes, deserializer)?;
Ok(bytes.into())
}
}

#[cfg(feature = "serde_bytes")]
impl serde_bytes::Serialize for Signature {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
serializer.serialize_bytes(&self.to_bytes())
}
}

#[cfg(feature = "serde_bytes")]
impl<'de> serde_bytes::Deserialize<'de> for Signature {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
struct ByteArrayVisitor;

impl de::Visitor<'_> for ByteArrayVisitor {
type Value = SignatureBytes;

fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("bytestring of length 64")
}

fn visit_bytes<E>(self, bytes: &[u8]) -> Result<Self::Value, E>
where
E: de::Error,
{
use de::Error;

bytes
.try_into()
.map_err(|_| Error::invalid_length(bytes.len(), &self))
}
}

deserializer
.deserialize_bytes(ByteArrayVisitor)
.map(Into::into)
}
}
Loading