From 30de02c67c9a8f9c1cbd5762b02eb93a41bcdade Mon Sep 17 00:00:00 2001 From: Kayh Date: Wed, 23 Oct 2024 22:10:04 -0400 Subject: [PATCH] remove partiall implemented ed25519 and p521 keys --- Cargo.lock | 61 ++++----- crates/xdid-method-key/Cargo.toml | 11 +- crates/xdid-method-key/src/keys/ed25519.rs | 88 ------------- crates/xdid-method-key/src/keys/mod.rs | 4 - crates/xdid-method-key/src/keys/p521.rs | 146 --------------------- crates/xdid-method-key/src/parser.rs | 4 - 6 files changed, 27 insertions(+), 287 deletions(-) delete mode 100644 crates/xdid-method-key/src/keys/ed25519.rs delete mode 100644 crates/xdid-method-key/src/keys/p521.rs diff --git a/Cargo.lock b/Cargo.lock index d0a10cc..7bd0084 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -190,7 +190,7 @@ dependencies = [ "proc-macro2", "quote", "strsim", - "syn 2.0.82", + "syn 2.0.85", ] [[package]] @@ -201,7 +201,7 @@ checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" dependencies = [ "darling_core", "quote", - "syn 2.0.82", + "syn 2.0.85", ] [[package]] @@ -645,20 +645,6 @@ dependencies = [ "sha2", ] -[[package]] -name = "p521" -version = "0.13.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fc9e2161f1f215afdfce23677034ae137bbd45016a880c2eb3ba8eb95f085b2" -dependencies = [ - "base16ct", - "ecdsa", - "elliptic-curve", - "primeorder", - "rand_core", - "sha2", -] - [[package]] name = "pem-rfc7468" version = "0.7.0" @@ -721,9 +707,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.88" +version = "1.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c3a7fc5db1e57d5a779a352c8cdb57b29aa4c40cc69c3a68a7fedc815fbf2f9" +checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" dependencies = [ "unicode-ident", ] @@ -841,22 +827,22 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.210" +version = "1.0.213" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" +checksum = "3ea7893ff5e2466df8d720bb615088341b295f849602c6956047f8f80f0e9bc1" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.210" +version = "1.0.213" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" +checksum = "7e85ad2009c50b58e87caa8cd6dac16bdf511bbfb7af6c33df902396aa480fa5" dependencies = [ "proc-macro2", "quote", - "syn 2.0.82", + "syn 2.0.85", ] [[package]] @@ -898,7 +884,7 @@ dependencies = [ "darling", "proc-macro2", "quote", - "syn 2.0.82", + "syn 2.0.85", ] [[package]] @@ -985,9 +971,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.82" +version = "2.0.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83540f837a8afc019423a8edb95b52a8effe46957ee402287f4292fae35be021" +checksum = "5023162dfcd14ef8f32034d8bcd4cc5ddc61ef7a247c024a33e24e1f24d21b56" dependencies = [ "proc-macro2", "quote", @@ -996,22 +982,22 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.64" +version = "1.0.65" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84" +checksum = "5d11abd9594d9b38965ef50805c5e469ca9cc6f197f883f717e0269a3057b3d5" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.64" +version = "1.0.65" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" +checksum = "ae71770322cbd277e69d762a16c444af02aa0575ac0d174f0b9562d3b37f8602" dependencies = [ "proc-macro2", "quote", - "syn 2.0.82", + "syn 2.0.85", ] [[package]] @@ -1047,9 +1033,9 @@ dependencies = [ [[package]] name = "tokio" -version = "1.40.0" +version = "1.41.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998" +checksum = "145f3413504347a2be84393cc8a7d2fb4d863b375909ea59f2158261aa258bbb" dependencies = [ "backtrace", "pin-project-lite", @@ -1064,7 +1050,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", - "syn 2.0.82", + "syn 2.0.85", ] [[package]] @@ -1125,7 +1111,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.82", + "syn 2.0.85", "wasm-bindgen-shared", ] @@ -1147,7 +1133,7 @@ checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" dependencies = [ "proc-macro2", "quote", - "syn 2.0.82", + "syn 2.0.85", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -1269,7 +1255,6 @@ dependencies = [ "multibase", "p256", "p384", - "p521", "rand", "ring", "serde_json", @@ -1303,7 +1288,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.82", + "syn 2.0.85", ] [[package]] diff --git a/crates/xdid-method-key/Cargo.toml b/crates/xdid-method-key/Cargo.toml index 999dc0d..45f6417 100644 --- a/crates/xdid-method-key/Cargo.toml +++ b/crates/xdid-method-key/Cargo.toml @@ -8,20 +8,17 @@ license.workspace = true keywords.workspace = true [features] -default = ["ed25519", "p256", "p384", "p521"] -ed25519 = [] -p256 = ["dep:p256", "dep:rand", "dep:serde_json"] -p384 = ["dep:p384", "dep:rand", "dep:serde_json"] -p521 = ["dep:p521", "dep:rand", "dep:serde_json"] +default = ["p256", "p384"] +p256 = ["dep:p256", "dep:rand", "dep:ring", "dep:serde_json"] +p384 = ["dep:p384", "dep:rand", "dep:ring", "dep:serde_json"] [dependencies] jose-jwk.workspace = true multibase = "0.9.1" p256 = { version = "0.13.2", features = ["jwk"], optional = true } p384 = { version = "0.13.0", features = ["jwk"], optional = true } -p521 = { version = "0.13.3", features = ["jwk"], optional = true } rand = { version = "0.8.5", optional = true } -ring = "0.17.8" +ring = { version = "0.17.8", optional = true } serde_json = { workspace = true, optional = true } thiserror.workspace = true unsigned-varint = { version = "0.8.0" } diff --git a/crates/xdid-method-key/src/keys/ed25519.rs b/crates/xdid-method-key/src/keys/ed25519.rs deleted file mode 100644 index bfb1b73..0000000 --- a/crates/xdid-method-key/src/keys/ed25519.rs +++ /dev/null @@ -1,88 +0,0 @@ -use jose_jwk::Jwk; -use ring::{rand::SystemRandom, signature::KeyPair}; - -use super::{DidKeyPair, KeyParser, Multicodec, PublicKey, SignError, Signer, WithMulticodec}; - -pub struct Ed25519KeyPair { - pair: ring::signature::Ed25519KeyPair, -} - -impl DidKeyPair for Ed25519KeyPair { - fn generate() -> Self { - let rng = SystemRandom::new(); - let document = ring::signature::Ed25519KeyPair::generate_pkcs8(&rng).unwrap(); - let pair = ring::signature::Ed25519KeyPair::from_pkcs8(document.as_ref()).unwrap(); - Self { pair } - } - - fn public(&self) -> impl PublicKey { - Ed25519PublicKey(self.pair.public_key().as_ref().to_owned()) - } - fn public_bytes(&self) -> Box<[u8]> { - self.pair.public_key().as_ref().to_vec().into() - } - fn secret_bytes(&self) -> Box<[u8]> { - todo!(); - } -} - -impl Signer for Ed25519KeyPair { - fn sign(&self, message: &[u8]) -> Result, SignError> { - Ok(self.pair.sign(message).as_ref().to_vec()) - } -} - -pub struct Ed25519PublicKey(Vec); - -impl PublicKey for Ed25519PublicKey { - fn as_did_bytes(&self) -> Box<[u8]> { - self.0.clone().into() - } - - fn to_jwk(&self) -> Jwk { - todo!("ed25519 currently unimplemented in jose_jwk"); - } -} - -impl WithMulticodec for Ed25519PublicKey { - fn codec(&self) -> Box { - Box::new(Ed25519Codec) - } -} - -pub struct Ed25519KeyParser; - -impl KeyParser for Ed25519KeyParser { - fn parse(&self, public_key: Vec) -> Box { - Box::new(Ed25519PublicKey(public_key)) - } -} - -impl WithMulticodec for Ed25519KeyParser { - fn codec(&self) -> Box { - Box::new(Ed25519Codec) - } -} - -struct Ed25519Codec; - -impl Multicodec for Ed25519Codec { - fn code_u64(&self) -> u64 { - 0xed - } -} - -#[cfg(test)] -mod tests { - - use super::*; - - #[test] - fn test_display() { - let pair = Ed25519KeyPair::generate(); - let did = pair.public().to_did(); - let did_str = did.to_string(); - println!("{}", did_str); - assert!(did_str.starts_with("did:key:z6Mk")); - } -} diff --git a/crates/xdid-method-key/src/keys/mod.rs b/crates/xdid-method-key/src/keys/mod.rs index 754bb15..9e32058 100644 --- a/crates/xdid-method-key/src/keys/mod.rs +++ b/crates/xdid-method-key/src/keys/mod.rs @@ -5,14 +5,10 @@ use xdid_core::did::{Did, MethodId, MethodName}; use crate::NAME; -#[cfg(feature = "ed25519")] -pub mod ed25519; #[cfg(feature = "p256")] pub mod p256; #[cfg(feature = "p384")] pub mod p384; -#[cfg(feature = "p521")] -pub mod p521; pub trait Signer { fn sign(&self, message: &[u8]) -> Result, SignError>; diff --git a/crates/xdid-method-key/src/keys/p521.rs b/crates/xdid-method-key/src/keys/p521.rs deleted file mode 100644 index d2fd2f1..0000000 --- a/crates/xdid-method-key/src/keys/p521.rs +++ /dev/null @@ -1,146 +0,0 @@ -use jose_jwk::Jwk; -use p521::{ - elliptic_curve::sec1::{FromEncodedPoint, ToEncodedPoint}, - SecretKey, -}; -use rand::rngs::OsRng; -use ring::{ - rand::SystemRandom, - signature::{EcdsaKeyPair, ECDSA_P384_SHA384_ASN1_SIGNING}, -}; - -use super::{DidKeyPair, KeyParser, Multicodec, PublicKey, SignError, Signer, WithMulticodec}; - -pub struct P521KeyPair { - secret: SecretKey, -} - -impl DidKeyPair for P521KeyPair { - fn generate() -> Self { - let mut rng = OsRng; - let secret = SecretKey::random(&mut rng); - Self { secret } - } - - fn public(&self) -> impl PublicKey { - P521PublicKey(self.secret.public_key()) - } - fn public_bytes(&self) -> Box<[u8]> { - self.secret.public_key().to_sec1_bytes() - } - fn secret_bytes(&self) -> Box<[u8]> { - self.secret.to_bytes().to_vec().into() - } -} - -impl Signer for P521KeyPair { - fn sign(&self, message: &[u8]) -> Result, SignError> { - let rng = SystemRandom::new(); - - let signer = EcdsaKeyPair::from_private_key_and_public_key( - &ECDSA_P384_SHA384_ASN1_SIGNING, - &self.secret_bytes(), - &self.public_bytes(), - &rng, - ) - .unwrap(); - - signer - .sign(&rng, message) - .map(|v| v.as_ref().to_vec()) - .map_err(|_| SignError::SigningFailed) - } -} - -struct P521PublicKey(p521::PublicKey); - -impl PublicKey for P521PublicKey { - fn as_did_bytes(&self) -> Box<[u8]> { - self.0.to_encoded_point(true).as_bytes().into() - } - - fn to_jwk(&self) -> Jwk { - let jwk_str = self.0.to_jwk_string(); - serde_json::from_str(&jwk_str).unwrap() - } -} - -impl WithMulticodec for P521PublicKey { - fn codec(&self) -> Box { - Box::new(P521Codec) - } -} - -pub(crate) struct P521KeyParser; - -impl KeyParser for P521KeyParser { - fn parse(&self, public_key: Vec) -> Box { - let point = p521::EncodedPoint::from_bytes(public_key).unwrap(); - let key = p521::PublicKey::from_encoded_point(&point).unwrap(); - Box::new(P521PublicKey(key)) - } -} - -impl WithMulticodec for P521KeyParser { - fn codec(&self) -> Box { - Box::new(P521Codec) - } -} - -struct P521Codec; - -impl Multicodec for P521Codec { - fn code_u64(&self) -> u64 { - 0x1202 - } -} - -#[cfg(test)] -mod tests { - use ring::signature::{VerificationAlgorithm, ECDSA_P384_SHA384_ASN1}; - - use crate::{p384::P384KeyPair, parser::DidKeyParser}; - - use super::*; - - #[test] - fn test_display() { - let pair = P521KeyPair::generate(); - let did = pair.public().to_did(); - - let did_str = did.to_string(); - println!("{}", did_str); - assert!(did_str.starts_with("did:key:z2J9")); - } - - #[test] - fn test_jwk() { - let pair = P521KeyPair::generate(); - let _ = pair.public().to_jwk(); - } - - #[test] - fn test_parse() { - let pair = P521KeyPair::generate(); - let did = pair.public().to_did(); - - let parser = DidKeyParser::default(); - let _ = parser.parse(&did).unwrap(); - } - - #[test] - fn test_sign() { - let pair = P384KeyPair::generate(); - - let msg = vec![0, 1, 2, 3, 4, 5, 6, 7, 8]; - let signature = pair.sign(&msg).unwrap(); - - assert!(ECDSA_P384_SHA384_ASN1 - .verify( - pair.public_bytes().to_vec().as_slice().into(), - msg.as_slice().into(), - signature.as_slice().into() - ) - .is_ok()); - } -} diff --git a/crates/xdid-method-key/src/parser.rs b/crates/xdid-method-key/src/parser.rs index 600b1ce..d1f1bc2 100644 --- a/crates/xdid-method-key/src/parser.rs +++ b/crates/xdid-method-key/src/parser.rs @@ -11,14 +11,10 @@ pub struct DidKeyParser { impl Default for DidKeyParser { fn default() -> Self { let parsers: Vec> = vec![ - #[cfg(feature = "ed25519")] - Box::new(crate::keys::ed25519::Ed25519KeyParser), #[cfg(feature = "p256")] Box::new(crate::keys::p256::P256KeyParser), #[cfg(feature = "p384")] Box::new(crate::keys::p384::P384KeyParser), - #[cfg(feature = "p521")] - Box::new(crate::keys::p521::P521KeyParser), ]; Self { parsers }