Skip to content

Commit

Permalink
crypto_kx: use serdect crate
Browse files Browse the repository at this point in the history
Replaces the handwritten serde visitor implementation with the
same-shaped implementation in the `serdect` crate, similar to the
changes to the `crypto_box` crate in #51.

This reduces the maintenance burden of `serde`-related code, by keeping
all of the complexity and interop testing in the `serdect` crate.
  • Loading branch information
tarcieri committed Aug 14, 2022
1 parent 52cb824 commit effb71a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 39 deletions.
18 changes: 1 addition & 17 deletions Cargo.lock

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

9 changes: 5 additions & 4 deletions crypto_kx/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "crypto_kx"
version = "0.1.0-pre" # Also update html_root_url in lib.rs when bumping this
version = "0.1.0-pre"
description = "Pure Rust implementation of libsodium's crypto_kx using BLAKE2"
authors = ["C4DT", "RustCrypto Developers"]
license = "Apache-2.0 OR MIT"
Expand All @@ -16,8 +16,9 @@ rust-version = "1.56"
[dependencies]
blake2 = { version = "0.10", default-features = false }
rand_core = "0.6"
# renamed to allow having a "serde" feature
our_serde = { package = "serde", version = "1", optional = true, features = ["derive"] }

# optional dependencies
serdect = { version = "0.1", optional = true, default-features = false }

[dependencies.x25519-dalek]
version = "1"
Expand All @@ -32,5 +33,5 @@ rand_core = { version = "0.6", features = ["std"] }
sodiumoxide = "0.2"

[features]
serde = ["our_serde", "x25519-dalek/serde"]
serde = ["serdect"]
std = ["blake2/std", "rand_core/std", "x25519-dalek/std"]
6 changes: 0 additions & 6 deletions crypto_kx/src/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ use rand_core::{CryptoRng, RngCore};
use crate::{ClientSessionKeys, PublicKey, SecretKey, ServerSessionKeys, SessionKey};

/// A [`SecretKey`] with its related [`PublicKey`].
#[derive(Clone)]
#[cfg_attr(
feature = "serde",
derive(our_serde::Deserialize, our_serde::Serialize)
)]
#[cfg_attr(feature = "serde", serde(crate = "our_serde"))]
pub struct KeyPair {
secret: SecretKey,
public: PublicKey,
Expand Down
30 changes: 25 additions & 5 deletions crypto_kx/src/keys/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
use crate::errors::InvalidLength;

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

/// [`PublicKey`] which can be freely shared.
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
#[cfg_attr(
feature = "serde",
derive(our_serde::Deserialize, our_serde::Serialize)
)]
#[cfg_attr(feature = "serde", serde(crate = "our_serde"))]
pub struct PublicKey(x25519_dalek::PublicKey);

impl PublicKey {
Expand Down Expand Up @@ -46,3 +44,25 @@ impl TryFrom<&[u8]> for PublicKey {
Ok(Self::from(array))
}
}

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

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for PublicKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
let mut bytes = [0u8; Self::BYTES];
serdect::array::deserialize_hex_or_bin(&mut bytes, deserializer)?;
Self::try_from(bytes.as_slice()).map_err(de::Error::custom)
}
}
30 changes: 25 additions & 5 deletions crypto_kx/src/keys/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
use crate::errors::InvalidLength;
use rand_core::{CryptoRng, RngCore};

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

/// [`SecretKey`] that should be kept private.
#[derive(Clone)]
#[cfg_attr(
feature = "serde",
derive(our_serde::Deserialize, our_serde::Serialize)
)]
#[cfg_attr(feature = "serde", serde(crate = "our_serde"))]
pub struct SecretKey(x25519_dalek::StaticSecret);

impl SecretKey {
Expand Down Expand Up @@ -56,3 +54,25 @@ impl TryFrom<&[u8]> for SecretKey {
Ok(Self::from(array))
}
}

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

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for SecretKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
let mut bytes = [0u8; Self::BYTES];
serdect::array::deserialize_hex_or_bin(&mut bytes, deserializer)?;
Self::try_from(bytes.as_slice()).map_err(de::Error::custom)
}
}
3 changes: 1 addition & 2 deletions crypto_kx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
#![no_std]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
html_root_url = "https://docs.rs/crypto_kx/0.0.2"
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
)]
#![warn(missing_docs, rust_2018_idioms)]

Expand Down

0 comments on commit effb71a

Please sign in to comment.