Skip to content

Commit 43a16f0

Browse files
authored
Implement DynSignatureAlgorithmIdentifier trait for ed25519 (#712)
1 parent cbf794d commit 43a16f0

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

ed25519-dalek/src/signing.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,20 @@ impl pkcs8::EncodePrivateKey for SigningKey {
665665
}
666666
}
667667

668+
#[cfg(all(feature = "alloc", feature = "pkcs8"))]
669+
impl pkcs8::spki::DynSignatureAlgorithmIdentifier for SigningKey {
670+
fn signature_algorithm_identifier(
671+
&self,
672+
) -> pkcs8::spki::Result<pkcs8::spki::AlgorithmIdentifierOwned> {
673+
// From https://datatracker.ietf.org/doc/html/rfc8410
674+
// `id-Ed25519 OBJECT IDENTIFIER ::= { 1 3 101 112 }`
675+
Ok(pkcs8::spki::AlgorithmIdentifier {
676+
oid: ed25519::pkcs8::ALGORITHM_OID,
677+
parameters: None,
678+
})
679+
}
680+
}
681+
668682
#[cfg(feature = "pkcs8")]
669683
impl TryFrom<pkcs8::KeypairBytes> for SigningKey {
670684
type Error = pkcs8::Error;

ed25519-dalek/src/verifying.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,20 @@ impl pkcs8::EncodePublicKey for VerifyingKey {
580580
}
581581
}
582582

583+
#[cfg(all(feature = "alloc", feature = "pkcs8"))]
584+
impl pkcs8::spki::DynSignatureAlgorithmIdentifier for VerifyingKey {
585+
fn signature_algorithm_identifier(
586+
&self,
587+
) -> pkcs8::spki::Result<pkcs8::spki::AlgorithmIdentifierOwned> {
588+
// From https://datatracker.ietf.org/doc/html/rfc8410
589+
// `id-Ed25519 OBJECT IDENTIFIER ::= { 1 3 101 112 }`
590+
Ok(ed25519::pkcs8::spki::AlgorithmIdentifierOwned {
591+
oid: ed25519::pkcs8::ALGORITHM_OID,
592+
parameters: None,
593+
})
594+
}
595+
}
596+
583597
#[cfg(feature = "pkcs8")]
584598
impl TryFrom<pkcs8::PublicKeyBytes> for VerifyingKey {
585599
type Error = pkcs8::spki::Error;

ed25519-dalek/tests/pkcs8.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
//! RFC5958 (PKCS#8) and RFC5280 (SPKI).
55
66
#![cfg(feature = "pkcs8")]
7-
87
use ed25519_dalek::pkcs8::{DecodePrivateKey, DecodePublicKey};
98
use ed25519_dalek::{SigningKey, VerifyingKey};
109
use hex_literal::hex;
1110

1211
#[cfg(feature = "alloc")]
1312
use ed25519_dalek::pkcs8::{EncodePrivateKey, EncodePublicKey};
1413

14+
#[cfg(all(feature = "alloc", feature = "pkcs8"))]
15+
use ed25519_dalek::pkcs8::spki::DynSignatureAlgorithmIdentifier;
16+
1517
/// Ed25519 PKCS#8 v1 private key encoded as ASN.1 DER.
1618
const PKCS8_V1_DER: &[u8] = include_bytes!("examples/pkcs8-v1.der");
1719

@@ -69,3 +71,17 @@ fn encode_verifying_key() {
6971
let verifying_key2 = VerifyingKey::from_public_key_der(verifying_key_der.as_bytes()).unwrap();
7072
assert_eq!(verifying_key, verifying_key2);
7173
}
74+
75+
#[test]
76+
#[cfg(feature = "alloc")]
77+
fn get_algo_identifier() {
78+
let verifying_key = VerifyingKey::from_public_key_der(PUBLIC_KEY_DER).unwrap();
79+
let identifier = verifying_key.signature_algorithm_identifier().unwrap();
80+
assert!(identifier.parameters.is_none()); // According to rfc8410 this must be None
81+
assert_eq!(identifier.oid, ed25519::pkcs8::ALGORITHM_OID);
82+
83+
let signing_key = SigningKey::from_bytes(&SK_BYTES);
84+
let identifer = signing_key.signature_algorithm_identifier().unwrap();
85+
assert!(identifer.parameters.is_none()); // According to rfc8410 this must be None
86+
assert_eq!(identifer.oid, ed25519::pkcs8::ALGORITHM_OID);
87+
}

0 commit comments

Comments
 (0)