Skip to content

Commit 63760d1

Browse files
authored
crypto_kx: use serdect crate (#59)
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.
1 parent 52cb824 commit 63760d1

File tree

6 files changed

+57
-39
lines changed

6 files changed

+57
-39
lines changed

Cargo.lock

Lines changed: 1 addition & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crypto_kx/Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "crypto_kx"
3-
version = "0.1.0-pre" # Also update html_root_url in lib.rs when bumping this
3+
version = "0.1.0-pre"
44
description = "Pure Rust implementation of libsodium's crypto_kx using BLAKE2"
55
authors = ["C4DT", "RustCrypto Developers"]
66
license = "Apache-2.0 OR MIT"
@@ -16,8 +16,9 @@ rust-version = "1.56"
1616
[dependencies]
1717
blake2 = { version = "0.10", default-features = false }
1818
rand_core = "0.6"
19-
# renamed to allow having a "serde" feature
20-
our_serde = { package = "serde", version = "1", optional = true, features = ["derive"] }
19+
20+
# optional dependencies
21+
serdect = { version = "0.1", optional = true, default-features = false }
2122

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

3435
[features]
35-
serde = ["our_serde", "x25519-dalek/serde"]
36+
serde = ["serdect"]
3637
std = ["blake2/std", "rand_core/std", "x25519-dalek/std"]

crypto_kx/src/keypair.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ use rand_core::{CryptoRng, RngCore};
44
use crate::{ClientSessionKeys, PublicKey, SecretKey, ServerSessionKeys, SessionKey};
55

66
/// A [`SecretKey`] with its related [`PublicKey`].
7-
#[derive(Clone)]
8-
#[cfg_attr(
9-
feature = "serde",
10-
derive(our_serde::Deserialize, our_serde::Serialize)
11-
)]
12-
#[cfg_attr(feature = "serde", serde(crate = "our_serde"))]
137
pub struct KeyPair {
148
secret: SecretKey,
159
public: PublicKey,

crypto_kx/src/keys/public.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
33
use crate::errors::InvalidLength;
44

5+
#[cfg(feature = "serde")]
6+
use serdect::serde::{de, ser, Deserialize, Serialize};
7+
58
/// [`PublicKey`] which can be freely shared.
69
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
7-
#[cfg_attr(
8-
feature = "serde",
9-
derive(our_serde::Deserialize, our_serde::Serialize)
10-
)]
11-
#[cfg_attr(feature = "serde", serde(crate = "our_serde"))]
1210
pub struct PublicKey(x25519_dalek::PublicKey);
1311

1412
impl PublicKey {
@@ -46,3 +44,25 @@ impl TryFrom<&[u8]> for PublicKey {
4644
Ok(Self::from(array))
4745
}
4846
}
47+
48+
#[cfg(feature = "serde")]
49+
impl Serialize for PublicKey {
50+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
51+
where
52+
S: ser::Serializer,
53+
{
54+
serdect::array::serialize_hex_upper_or_bin(&self.0.to_bytes(), serializer)
55+
}
56+
}
57+
58+
#[cfg(feature = "serde")]
59+
impl<'de> Deserialize<'de> for PublicKey {
60+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
61+
where
62+
D: de::Deserializer<'de>,
63+
{
64+
let mut bytes = [0u8; Self::BYTES];
65+
serdect::array::deserialize_hex_or_bin(&mut bytes, deserializer)?;
66+
Self::try_from(&bytes[..]).map_err(de::Error::custom)
67+
}
68+
}

crypto_kx/src/keys/secret.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
use crate::errors::InvalidLength;
44
use rand_core::{CryptoRng, RngCore};
55

6+
#[cfg(feature = "serde")]
7+
use serdect::serde::{de, ser, Deserialize, Serialize};
8+
69
/// [`SecretKey`] that should be kept private.
710
#[derive(Clone)]
8-
#[cfg_attr(
9-
feature = "serde",
10-
derive(our_serde::Deserialize, our_serde::Serialize)
11-
)]
12-
#[cfg_attr(feature = "serde", serde(crate = "our_serde"))]
1311
pub struct SecretKey(x25519_dalek::StaticSecret);
1412

1513
impl SecretKey {
@@ -56,3 +54,25 @@ impl TryFrom<&[u8]> for SecretKey {
5654
Ok(Self::from(array))
5755
}
5856
}
57+
58+
#[cfg(feature = "serde")]
59+
impl Serialize for SecretKey {
60+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
61+
where
62+
S: ser::Serializer,
63+
{
64+
serdect::array::serialize_hex_upper_or_bin(&self.0.to_bytes(), serializer)
65+
}
66+
}
67+
68+
#[cfg(feature = "serde")]
69+
impl<'de> Deserialize<'de> for SecretKey {
70+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
71+
where
72+
D: de::Deserializer<'de>,
73+
{
74+
let mut bytes = [0u8; Self::BYTES];
75+
serdect::array::deserialize_hex_or_bin(&mut bytes, deserializer)?;
76+
Self::try_from(&bytes[..]).map_err(de::Error::custom)
77+
}
78+
}

crypto_kx/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@
4949
#![no_std]
5050
#![doc(
5151
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
52-
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
53-
html_root_url = "https://docs.rs/crypto_kx/0.0.2"
52+
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
5453
)]
5554
#![warn(missing_docs, rust_2018_idioms)]
5655

0 commit comments

Comments
 (0)