From 830393c1ec153f9a1a1b1cc80e505ef159394cda Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Tue, 17 Oct 2023 16:36:03 -0400 Subject: [PATCH] Implement Debug --- src/lib.rs | 6 ++++-- src/p384.rs | 44 ++++++++++++++++++++++++++++++++++++++------ src/x25519.rs | 18 ++++++++++++++++++ 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cfd7366..513299f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ * https://www.zerotier.com/ */ +use std::fmt::Debug; use std::hash::Hash; use std::str::FromStr; @@ -19,6 +20,7 @@ use zerotier_common_utils::tofrombytes::ToFromBytes; pub trait Address: ToString + FromStr + + Debug + ToFromBytes + Sync + Send @@ -37,7 +39,7 @@ pub trait Address: /// A bundle of public key(s) securely identifying a participant on the network. pub trait Identity: - ToString + FromStr + ToFromBytes + Sync + Send + Clone + PartialEq + Eq + Hash + PartialOrd + Ord + 'static + ToString + FromStr + Debug + ToFromBytes + Sync + Send + Clone + PartialEq + Eq + Hash + PartialOrd + Ord + 'static { /// Number of bytes in this identity's byte serialized representation. const SIZE: usize; @@ -83,6 +85,6 @@ pub(crate) const IDENTITY_ERR: InvalidParameterError = InvalidParameterError("in pub use serde; pub use serde_cbor; pub use zeroize; -pub use zerotier_crypto_glue; pub use zerotier_common_utils; +pub use zerotier_crypto_glue; pub use zerotier_crypto_glue::zssp; diff --git a/src/p384.rs b/src/p384.rs index 4dfb3cf..9ccab4e 100644 --- a/src/p384.rs +++ b/src/p384.rs @@ -6,6 +6,7 @@ * https://www.zerotier.com/ */ +use std::fmt::Debug; use std::hash::Hash; use std::io::Write; use std::mem::{size_of, transmute}; @@ -181,6 +182,20 @@ impl ToString for ShortAddress { } } +impl Debug for Address { + #[inline] + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.to_string().as_str()) + } +} + +impl Debug for ShortAddress { + #[inline] + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.to_string().as_str()) + } +} + impl FromStr for Address { type Err = InvalidParameterError; @@ -430,6 +445,19 @@ impl ToString for Identity { } } +impl Debug for Identity { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("x25519::Identity") + .field("address", &self.address) + .field("master_signing_key", self.master_signing_key.as_bytes()) + .field("timestamp", &self.timestamp) + .field("ecdh", self.ecdh.as_bytes()) + .field("ecdsa", self.ecdsa.as_bytes()) + .field("master_signature", &self.master_signature) + .finish() + } +} + impl FromStr for Identity { type Err = InvalidParameterError; @@ -486,7 +514,6 @@ impl ToFromBytes for Identity { } } - impl Serialize for Identity { #[inline] fn serialize(&self, serializer: S) -> Result @@ -496,7 +523,12 @@ impl Serialize for Identity { if serializer.is_human_readable() { self.to_string().serialize(serializer) } else { - serializer.serialize_bytes(self.to_bytes_on_stack::<{P384_PUBLIC_KEY_SIZE + 8 + P384_PUBLIC_KEY_SIZE + P384_PUBLIC_KEY_SIZE + P384_ECDSA_SIGNATURE_SIZE}>().as_ref()) + serializer.serialize_bytes( + self.to_bytes_on_stack::<{ + P384_PUBLIC_KEY_SIZE + 8 + P384_PUBLIC_KEY_SIZE + P384_PUBLIC_KEY_SIZE + P384_ECDSA_SIGNATURE_SIZE + }>() + .as_ref(), + ) } } } @@ -508,8 +540,7 @@ impl<'de> Deserialize<'de> for Identity { D: Deserializer<'de>, { if deserializer.is_human_readable() { - Identity::from_str(<&str>::deserialize(deserializer)?) - .map_err(|_| serde::de::Error::custom(IDENTITY_ERR.0)) + Identity::from_str(<&str>::deserialize(deserializer)?).map_err(|_| serde::de::Error::custom(IDENTITY_ERR.0)) } else { struct Visitor; @@ -521,8 +552,9 @@ impl<'de> Deserialize<'de> for Identity { } fn visit_bytes(self, v: &[u8]) -> Result - where - E: serde::de::Error, { + where + E: serde::de::Error, + { Identity::from_bytes(v).map_err(|_| serde::de::Error::custom(IDENTITY_ERR.0)) } } diff --git a/src/x25519.rs b/src/x25519.rs index 0b55d62..6ae8e2f 100644 --- a/src/x25519.rs +++ b/src/x25519.rs @@ -7,6 +7,7 @@ */ use std::alloc::{alloc, Layout}; +use std::fmt::Debug; use std::hash::Hash; use std::mem::transmute_copy; use std::ptr::copy_nonoverlapping; @@ -72,6 +73,13 @@ impl ToString for Address { } } +impl Debug for Address { + #[inline] + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.to_string().as_str()) + } +} + impl FromStr for Address { type Err = InvalidParameterError; @@ -180,6 +188,16 @@ impl ToString for Identity { } } +impl Debug for Identity { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("x25519::Identity") + .field("address", &self.address) + .field("ecdh", &self.ecdh) + .field("eddsa", &self.eddsa) + .finish() + } +} + impl FromStr for Identity { type Err = InvalidParameterError;