diff --git a/Cargo.lock b/Cargo.lock index 106a02de..216a3068 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,10 +29,22 @@ dependencies = [ ] [[package]] -name = "amplify_derive" -version = "2.11.3" +name = "amplify" +version = "4.0.0-beta.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c3de270e75f27a4468a7c344070109046656e85cb522141f7d40ab4b83803ac" +checksum = "62c3b7fe483c11f7a434ec6923a68749b3415c1328857f1a8e37bb2c792a25c0" +dependencies = [ + "amplify_derive", + "amplify_num", + "ascii", + "wasm-bindgen", +] + +[[package]] +name = "amplify_derive" +version = "4.0.0-alpha.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a258501c851f9dec1549e046d551b645b8b970e30aca85dbec2d6e36cda8e91" dependencies = [ "amplify_syn", "proc-macro2", @@ -40,6 +52,12 @@ dependencies = [ "syn", ] +[[package]] +name = "amplify_num" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f27d3d00d3d115395a7a8a4dc045feb7aa82b641e485f7e15f4e67ac16f4f56d" + [[package]] name = "amplify_syn" version = "1.1.6" @@ -78,6 +96,12 @@ version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6" +[[package]] +name = "ascii" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16" + [[package]] name = "async-trait" version = "0.1.59" @@ -564,9 +588,9 @@ dependencies = [ [[package]] name = "cyphernet" version = "0.1.0" -source = "git+https://github.com/internet2-wg/rust-cyphernet?rev=dee03a95abe4c964e5d9f8532c7dc76998dfeea7#dee03a95abe4c964e5d9f8532c7dc76998dfeea7" +source = "git+https://github.com/cyphernet-wg/rust-cyphernet#762f48e1175f9490c635996c0c1bd07ff1287cfc" dependencies = [ - "amplify_derive", + "amplify", "multibase", "socks", ] @@ -2167,6 +2191,7 @@ dependencies = [ name = "radicle-crypto" version = "0.1.0" dependencies = [ + "amplify", "base64", "cyphernet", "ed25519-compact", diff --git a/Cargo.toml b/Cargo.toml index 6b1b5dc7..c6e9ca9e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,8 +33,7 @@ rev = "90cc3eac67aa5cfd5f42cf7cb1e2b155af3214fb" version = "0.3.0" [patch.crates-io.cyphernet] -git = "https://github.com/internet2-wg/rust-cyphernet" -rev = "dee03a95abe4c964e5d9f8532c7dc76998dfeea7" +git = "https://github.com/cyphernet-wg/rust-cyphernet" version = "0.1.0" [patch.crates-io.radicle-git-ext] diff --git a/radicle-crypto/Cargo.toml b/radicle-crypto/Cargo.toml index f57e5d39..ba48285a 100644 --- a/radicle-crypto/Cargo.toml +++ b/radicle-crypto/Cargo.toml @@ -13,6 +13,7 @@ test = ["fastrand", "qcheck"] ssh = ["base64", "radicle-ssh", "ssh-key"] [dependencies] +amplify = { version = "4.0.0-beta.4" } ed25519-compact = { version = "2.0.2", features = ["pem"] } cyphernet = { version = "0", optional = true } multibase = { version = "0.9.1" } diff --git a/radicle-crypto/src/cyphernet.rs b/radicle-crypto/src/cyphernet.rs index 3f2a6fa3..9279ee93 100644 --- a/radicle-crypto/src/cyphernet.rs +++ b/radicle-crypto/src/cyphernet.rs @@ -1,89 +1,43 @@ -use std::ops::Deref; - -use cyphernet::crypto::{Ec, EcPrivKey, EcPubKey, EcSig}; +use amplify::{From, Wrapper}; +use cyphernet::crypto::{EcPk, EcSig, EcSk, Ecdh}; use ed25519_compact::x25519; use crate::{PublicKey, SecretKey, Signature}; -// Derivations required for automatic derivations of other types -#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)] -pub struct Ed25519; +#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)] +#[wrapper(Deref)] +pub struct SharedSecret([u8; 32]); -pub type SharedSecret = [u8; 32]; +impl EcPk for PublicKey {} -impl Ec for Ed25519 { - type PubKey = PublicKey; - type PrivKey = SecretKey; - type EcdhSecret = SharedSecret; - type EcdhErr = ed25519_compact::Error; +impl EcSk for SecretKey { + type Pk = PublicKey; + + fn to_pk(&self) -> Self::Pk { + self.public_key().into() + } } -impl EcPubKey for PublicKey { - type Raw = [u8; ed25519_compact::PublicKey::BYTES]; +impl Ecdh for SharedSecret { + type Sk = SecretKey; + type Err = ed25519_compact::Error; - fn from_raw(raw: Self::Raw) -> Self { - PublicKey::from(raw) - } - - fn into_raw(self) -> Self::Raw { - *self.0.deref() - } - - fn ecdh(self, sk: &SecretKey) -> Result { - let xpk = x25519::PublicKey::from_ed25519(&self.0)?; + fn ecdh(sk: &Self::Sk, pk: &::Pk) -> Result { + let xpk = x25519::PublicKey::from_ed25519(&pk.0)?; let xsk = x25519::SecretKey::from_ed25519(&sk.0)?; let ss = xpk.dh(&xsk)?; - Ok(*ss) + Ok(Self(*ss)) } } -impl EcPrivKey for SecretKey { - type Raw = [u8; ed25519_compact::SecretKey::BYTES]; +impl EcSig for Signature { + type Sk = SecretKey; - fn from_raw(raw: Self::Raw) -> Self { - SecretKey::from(raw) - } - - fn into_raw(self) -> Self::Raw { - *self.0.deref() - } - - fn to_raw(&self) -> Self::Raw { - *self.0.deref() - } - - fn as_raw(&self) -> &Self::Raw { - self.0.deref() - } - - fn to_public_key(&self) -> PublicKey { - self.0.public_key().into() - } - - fn ecdh(&self, pk: PublicKey) -> Result { - let xpk = x25519::PublicKey::from_ed25519(&pk.0)?; - let xsk = x25519::SecretKey::from_ed25519(&self.0)?; - let ss = xpk.dh(&xsk)?; - Ok(*ss) - } -} - -impl EcSig for Signature { - type Raw = [u8; ed25519_compact::Signature::BYTES]; - - fn from_raw(raw: Self::Raw) -> Self { - Signature::from(raw) - } - - fn into_raw(self) -> Self::Raw { - *self.0 - } - - fn sign(self, sk: SecretKey, msg: impl AsRef<[u8]>) -> Self { + fn sign(self, sk: &SecretKey, msg: impl AsRef<[u8]>) -> Self { sk.0.sign(msg, None).into() } - fn verify(self, pk: PublicKey, msg: impl AsRef<[u8]>) -> bool { + fn verify(self, pk: &PublicKey, msg: impl AsRef<[u8]>) -> bool { pk.0.verify(msg, &self.0).is_ok() } } diff --git a/radicle-crypto/src/lib.rs b/radicle-crypto/src/lib.rs index 6a73737a..03c7b1c8 100644 --- a/radicle-crypto/src/lib.rs +++ b/radicle-crypto/src/lib.rs @@ -16,8 +16,6 @@ pub mod ssh; #[cfg(feature = "cyphernet")] mod cyphernet; -#[cfg(feature = "cyphernet")] -pub use self::cyphernet::Ed25519; /// Verified (used as type witness). #[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize)]