From a1155b6090e627d464b3d7717eaeb92c3582b523 Mon Sep 17 00:00:00 2001 From: Julius Date: Wed, 2 Oct 2024 17:59:25 -0700 Subject: [PATCH 1/4] feat: impl DynSignatureAlgorithmIdentifier for ed5519-dalek --- ed25519-dalek/src/signature.rs | 2 +- ed25519-dalek/src/signing.rs | 9 +++++++++ ed25519-dalek/src/verifying.rs | 9 +++++++++ ed25519-dalek/tests/pkcs8.rs | 16 ++++++++++++++-- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/ed25519-dalek/src/signature.rs b/ed25519-dalek/src/signature.rs index af8276834..673598465 100644 --- a/ed25519-dalek/src/signature.rs +++ b/ed25519-dalek/src/signature.rs @@ -174,4 +174,4 @@ impl From for ed25519::Signature { fn from(sig: InternalSignature) -> ed25519::Signature { ed25519::Signature::from_components(*sig.R.as_bytes(), *sig.s.as_bytes()) } -} +} \ No newline at end of file diff --git a/ed25519-dalek/src/signing.rs b/ed25519-dalek/src/signing.rs index 3d911dd3a..d70371b10 100644 --- a/ed25519-dalek/src/signing.rs +++ b/ed25519-dalek/src/signing.rs @@ -665,6 +665,15 @@ impl pkcs8::EncodePrivateKey for SigningKey { } } +#[cfg(feature = "pkcs8")] +impl pkcs8::spki::DynSignatureAlgorithmIdentifier for SigningKey { + fn signature_algorithm_identifier(&self) -> pkcs8::spki::Result { + // From https://datatracker.ietf.org/doc/html/rfc8410 + // `id-Ed25519 OBJECT IDENTIFIER ::= { 1 3 101 112 }` + Ok(pkcs8::spki::AlgorithmIdentifier { oid: ed25519::pkcs8::ALGORITHM_OID, parameters: None }) + } +} + #[cfg(feature = "pkcs8")] impl TryFrom for SigningKey { type Error = pkcs8::Error; diff --git a/ed25519-dalek/src/verifying.rs b/ed25519-dalek/src/verifying.rs index d82a5fbe1..39373fab6 100644 --- a/ed25519-dalek/src/verifying.rs +++ b/ed25519-dalek/src/verifying.rs @@ -580,6 +580,15 @@ impl pkcs8::EncodePublicKey for VerifyingKey { } } +#[cfg(feature = "pkcs8")] +impl pkcs8::spki::DynSignatureAlgorithmIdentifier for VerifyingKey { + fn signature_algorithm_identifier(&self) -> pkcs8::spki::Result { + // From https://datatracker.ietf.org/doc/html/rfc8410 + // `id-Ed25519 OBJECT IDENTIFIER ::= { 1 3 101 112 }` + Ok(ed25519::pkcs8::spki::AlgorithmIdentifierOwned { oid: ed25519::pkcs8::ALGORITHM_OID, parameters: None }) + } +} + #[cfg(feature = "pkcs8")] impl TryFrom for VerifyingKey { type Error = pkcs8::spki::Error; diff --git a/ed25519-dalek/tests/pkcs8.rs b/ed25519-dalek/tests/pkcs8.rs index fecdba94e..49604ec90 100644 --- a/ed25519-dalek/tests/pkcs8.rs +++ b/ed25519-dalek/tests/pkcs8.rs @@ -4,8 +4,7 @@ //! RFC5958 (PKCS#8) and RFC5280 (SPKI). #![cfg(feature = "pkcs8")] - -use ed25519_dalek::pkcs8::{DecodePrivateKey, DecodePublicKey}; +use ed25519_dalek::pkcs8::{spki::DynSignatureAlgorithmIdentifier, DecodePrivateKey, DecodePublicKey}; use ed25519_dalek::{SigningKey, VerifyingKey}; use hex_literal::hex; @@ -69,3 +68,16 @@ fn encode_verifying_key() { let verifying_key2 = VerifyingKey::from_public_key_der(verifying_key_der.as_bytes()).unwrap(); assert_eq!(verifying_key, verifying_key2); } + +#[test] +fn get_algo_identifier() { + let verifying_key = VerifyingKey::from_public_key_der(PUBLIC_KEY_DER).unwrap(); + let identifier = verifying_key.signature_algorithm_identifier().unwrap(); + assert!(identifier.parameters.is_none()); // According to rfc8410 this must be None + assert_eq!(identifier.oid, ed25519::pkcs8::ALGORITHM_OID); + + let signing_key = SigningKey::from_bytes(&SK_BYTES); + let identifer = signing_key.signature_algorithm_identifier().unwrap(); + assert!(identifer.parameters.is_none()); // According to rfc8410 this must be None + assert_eq!(identifer.oid, ed25519::pkcs8::ALGORITHM_OID); +} \ No newline at end of file From b46e6d53866bf41654fba4e1eaa54f9c18025ac8 Mon Sep 17 00:00:00 2001 From: Julius Date: Thu, 3 Oct 2024 13:01:03 -0700 Subject: [PATCH 2/4] chore: cargo fmt --- ed25519-dalek/src/signature.rs | 2 +- ed25519-dalek/src/signing.rs | 9 +++++++-- ed25519-dalek/src/verifying.rs | 9 +++++++-- ed25519-dalek/tests/pkcs8.rs | 6 ++++-- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/ed25519-dalek/src/signature.rs b/ed25519-dalek/src/signature.rs index 673598465..af8276834 100644 --- a/ed25519-dalek/src/signature.rs +++ b/ed25519-dalek/src/signature.rs @@ -174,4 +174,4 @@ impl From for ed25519::Signature { fn from(sig: InternalSignature) -> ed25519::Signature { ed25519::Signature::from_components(*sig.R.as_bytes(), *sig.s.as_bytes()) } -} \ No newline at end of file +} diff --git a/ed25519-dalek/src/signing.rs b/ed25519-dalek/src/signing.rs index d70371b10..8445c2467 100644 --- a/ed25519-dalek/src/signing.rs +++ b/ed25519-dalek/src/signing.rs @@ -667,10 +667,15 @@ impl pkcs8::EncodePrivateKey for SigningKey { #[cfg(feature = "pkcs8")] impl pkcs8::spki::DynSignatureAlgorithmIdentifier for SigningKey { - fn signature_algorithm_identifier(&self) -> pkcs8::spki::Result { + fn signature_algorithm_identifier( + &self, + ) -> pkcs8::spki::Result { // From https://datatracker.ietf.org/doc/html/rfc8410 // `id-Ed25519 OBJECT IDENTIFIER ::= { 1 3 101 112 }` - Ok(pkcs8::spki::AlgorithmIdentifier { oid: ed25519::pkcs8::ALGORITHM_OID, parameters: None }) + Ok(pkcs8::spki::AlgorithmIdentifier { + oid: ed25519::pkcs8::ALGORITHM_OID, + parameters: None, + }) } } diff --git a/ed25519-dalek/src/verifying.rs b/ed25519-dalek/src/verifying.rs index 39373fab6..986eff07d 100644 --- a/ed25519-dalek/src/verifying.rs +++ b/ed25519-dalek/src/verifying.rs @@ -582,10 +582,15 @@ impl pkcs8::EncodePublicKey for VerifyingKey { #[cfg(feature = "pkcs8")] impl pkcs8::spki::DynSignatureAlgorithmIdentifier for VerifyingKey { - fn signature_algorithm_identifier(&self) -> pkcs8::spki::Result { + fn signature_algorithm_identifier( + &self, + ) -> pkcs8::spki::Result { // From https://datatracker.ietf.org/doc/html/rfc8410 // `id-Ed25519 OBJECT IDENTIFIER ::= { 1 3 101 112 }` - Ok(ed25519::pkcs8::spki::AlgorithmIdentifierOwned { oid: ed25519::pkcs8::ALGORITHM_OID, parameters: None }) + Ok(ed25519::pkcs8::spki::AlgorithmIdentifierOwned { + oid: ed25519::pkcs8::ALGORITHM_OID, + parameters: None, + }) } } diff --git a/ed25519-dalek/tests/pkcs8.rs b/ed25519-dalek/tests/pkcs8.rs index 49604ec90..9a7362cb4 100644 --- a/ed25519-dalek/tests/pkcs8.rs +++ b/ed25519-dalek/tests/pkcs8.rs @@ -4,7 +4,9 @@ //! RFC5958 (PKCS#8) and RFC5280 (SPKI). #![cfg(feature = "pkcs8")] -use ed25519_dalek::pkcs8::{spki::DynSignatureAlgorithmIdentifier, DecodePrivateKey, DecodePublicKey}; +use ed25519_dalek::pkcs8::{ + spki::DynSignatureAlgorithmIdentifier, DecodePrivateKey, DecodePublicKey, +}; use ed25519_dalek::{SigningKey, VerifyingKey}; use hex_literal::hex; @@ -80,4 +82,4 @@ fn get_algo_identifier() { let identifer = signing_key.signature_algorithm_identifier().unwrap(); assert!(identifer.parameters.is_none()); // According to rfc8410 this must be None assert_eq!(identifer.oid, ed25519::pkcs8::ALGORITHM_OID); -} \ No newline at end of file +} From 31e8a61a02c0b4c9c0fd4a7d59038806b3f492a8 Mon Sep 17 00:00:00 2001 From: Julius Date: Mon, 7 Oct 2024 15:00:43 -0700 Subject: [PATCH 3/4] fix: add "alloc" feature to cfg --- ed25519-dalek/src/signing.rs | 2 +- ed25519-dalek/src/verifying.rs | 2 +- ed25519-dalek/tests/pkcs8.rs | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/ed25519-dalek/src/signing.rs b/ed25519-dalek/src/signing.rs index 8445c2467..f3c1053b2 100644 --- a/ed25519-dalek/src/signing.rs +++ b/ed25519-dalek/src/signing.rs @@ -665,7 +665,7 @@ impl pkcs8::EncodePrivateKey for SigningKey { } } -#[cfg(feature = "pkcs8")] +#[cfg(all(feature = "alloc", feature = "pkcs8"))] impl pkcs8::spki::DynSignatureAlgorithmIdentifier for SigningKey { fn signature_algorithm_identifier( &self, diff --git a/ed25519-dalek/src/verifying.rs b/ed25519-dalek/src/verifying.rs index 986eff07d..2bb40ebd7 100644 --- a/ed25519-dalek/src/verifying.rs +++ b/ed25519-dalek/src/verifying.rs @@ -580,7 +580,7 @@ impl pkcs8::EncodePublicKey for VerifyingKey { } } -#[cfg(feature = "pkcs8")] +#[cfg(all(feature = "alloc", feature = "pkcs8"))] impl pkcs8::spki::DynSignatureAlgorithmIdentifier for VerifyingKey { fn signature_algorithm_identifier( &self, diff --git a/ed25519-dalek/tests/pkcs8.rs b/ed25519-dalek/tests/pkcs8.rs index 9a7362cb4..8191fabcf 100644 --- a/ed25519-dalek/tests/pkcs8.rs +++ b/ed25519-dalek/tests/pkcs8.rs @@ -72,6 +72,7 @@ fn encode_verifying_key() { } #[test] +#[cfg(feature = "alloc")] fn get_algo_identifier() { let verifying_key = VerifyingKey::from_public_key_der(PUBLIC_KEY_DER).unwrap(); let identifier = verifying_key.signature_algorithm_identifier().unwrap(); From 531c32a54095f3735eb812dcff8ed5c44ec95ebc Mon Sep 17 00:00:00 2001 From: Julius Date: Mon, 7 Oct 2024 15:03:40 -0700 Subject: [PATCH 4/4] fix: cfg settings in test --- ed25519-dalek/tests/pkcs8.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ed25519-dalek/tests/pkcs8.rs b/ed25519-dalek/tests/pkcs8.rs index 8191fabcf..4a12318b0 100644 --- a/ed25519-dalek/tests/pkcs8.rs +++ b/ed25519-dalek/tests/pkcs8.rs @@ -4,15 +4,16 @@ //! RFC5958 (PKCS#8) and RFC5280 (SPKI). #![cfg(feature = "pkcs8")] -use ed25519_dalek::pkcs8::{ - spki::DynSignatureAlgorithmIdentifier, DecodePrivateKey, DecodePublicKey, -}; +use ed25519_dalek::pkcs8::{DecodePrivateKey, DecodePublicKey}; use ed25519_dalek::{SigningKey, VerifyingKey}; use hex_literal::hex; #[cfg(feature = "alloc")] use ed25519_dalek::pkcs8::{EncodePrivateKey, EncodePublicKey}; +#[cfg(all(feature = "alloc", feature = "pkcs8"))] +use ed25519_dalek::pkcs8::spki::DynSignatureAlgorithmIdentifier; + /// Ed25519 PKCS#8 v1 private key encoded as ASN.1 DER. const PKCS8_V1_DER: &[u8] = include_bytes!("examples/pkcs8-v1.der");