diff --git a/Cargo.lock b/Cargo.lock index 9e55b33c..8a1ef0a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -404,6 +404,7 @@ dependencies = [ "serde", "serde_bytes", "signature", + "zerocopy", "zeroize", ] diff --git a/ed25519/Cargo.toml b/ed25519/Cargo.toml index 90c882d9..c2874bcb 100644 --- a/ed25519/Cargo.toml +++ b/ed25519/Cargo.toml @@ -25,6 +25,7 @@ pkcs8 = { version = "0.11.0-rc.8", optional = true } serde = { version = "1", 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"] } [dev-dependencies] #ed25519-dalek = { version = "2", features = ["rand_core"] } diff --git a/ed25519/src/lib.rs b/ed25519/src/lib.rs index 8b6447f5..2f38509b 100644 --- a/ed25519/src/lib.rs +++ b/ed25519/src/lib.rs @@ -296,6 +296,9 @@ use pkcs8::spki::{ #[cfg(feature = "zeroize")] use zeroize::Zeroize; +#[cfg(feature = "zerocopy")] +use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Unaligned}; + /// Size of a single component of an Ed25519 signature. const COMPONENT_SIZE: usize = 32; @@ -314,6 +317,10 @@ pub type SignatureBytes = [u8; Signature::BYTE_SIZE]; /// Signature verification libraries are expected to reject invalid field /// elements at the time a signature is verified. #[derive(Copy, Clone, Eq, PartialEq)] +#[cfg_attr( + feature = "zerocopy", + derive(FromBytes, IntoBytes, Immutable, KnownLayout, Unaligned) +)] #[repr(C)] pub struct Signature { R: ComponentBytes, diff --git a/ed25519/src/pkcs8.rs b/ed25519/src/pkcs8.rs index 1104081f..2fc25d40 100644 --- a/ed25519/src/pkcs8.rs +++ b/ed25519/src/pkcs8.rs @@ -35,6 +35,9 @@ use core::str; #[cfg(feature = "zeroize")] use zeroize::Zeroize; +#[cfg(feature = "zerocopy")] +use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Unaligned}; + /// Algorithm [`ObjectIdentifier`] for the Ed25519 digital signature algorithm /// (`id-Ed25519`). /// @@ -226,6 +229,11 @@ impl str::FromStr for KeypairBytes { /// Note that this type operates on raw bytes and performs no validation that /// public keys represent valid compressed Ed25519 y-coordinates. #[derive(Clone, Copy, Eq, PartialEq)] +#[cfg_attr( + feature = "zerocopy", + derive(IntoBytes, FromBytes, Unaligned, KnownLayout, Immutable,) +)] +#[repr(transparent)] pub struct PublicKeyBytes(pub [u8; Self::BYTE_SIZE]); impl PublicKeyBytes {