Skip to content

Commit e67c410

Browse files
Remove commented-out code and clarify ED25519 key support in TryFrom implementation and restore sec1 support for EcdsaSigningKey
1 parent efc4f60 commit e67c410

File tree

3 files changed

+58
-53
lines changed

3 files changed

+58
-53
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ aes-gcm = { version = "0.11.0-rc.1", default-features = false, optional = true }
2525
ccm = { version = "0.6.0-pre", default-features = false, optional = true, git = "https://github.com/RustCrypto/AEADs/" }
2626
chacha20poly1305 = { version = "0.11.0-rc.1", default-features = false, optional = true }
2727
cipher = "0.5.0-rc.1"
28-
# crrl = { git = "https://github.com/stevefan1999-personal/crrl", version = "0.9.0", default-features = false, optional = true }
2928
crypto-common = { version = "0.2.0-rc.4", default-features = false }
3029
der = { version = "0.8.0-rc.8", default-features = false, optional = true }
3130
digest = { version = "0.11.0-rc.1", default-features = false }

src/sign/ecdsa/nist.rs

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ use alloc::{boxed::Box, format, sync::Arc};
33
use core::fmt::Debug;
44
use core::marker::PhantomData;
55

6-
// #[cfg(feature = "sec1")]
7-
// use sec1::DecodeEcPrivateKey;
8-
96
use crate::sign::rand::GenericRandomizedSigner;
107
use rustls::sign::SigningKey;
118
use rustls::{SignatureAlgorithm, SignatureScheme};
@@ -17,52 +14,57 @@ trait EcdsaKey: Sized {
1714
const SCHEME: SignatureScheme;
1815
}
1916

20-
// #[cfg(all(feature = "pkcs8", not(feature = "sec1")))]
21-
// trait DecodePrivateKey: ::pkcs8::DecodePrivateKey {}
17+
#[cfg(all(feature = "pkcs8", not(feature = "sec1")))]
18+
trait DecodePrivateKey: ::pkcs8::DecodePrivateKey {}
2219

23-
// #[cfg(all(feature = "sec1", not(feature = "pkcs8")))]
24-
// trait DecodePrivateKey: ::sec1::DecodeEcPrivateKey {}
20+
#[cfg(all(feature = "sec1", not(feature = "pkcs8")))]
21+
trait DecodePrivateKey: ::sec1::DecodeEcPrivateKey {}
2522

26-
// #[cfg(all(feature = "pkcs8", feature = "sec1"))]
27-
// trait DecodePrivateKey: ::pkcs8::DecodePrivateKey + ::sec1::DecodeEcPrivateKey {}
23+
#[cfg(all(feature = "pkcs8", feature = "sec1"))]
24+
trait DecodePrivateKey: ::pkcs8::DecodePrivateKey + ::sec1::DecodeEcPrivateKey {}
2825

2926
#[cfg(feature = "der")]
30-
impl<SK, SIG> TryFrom<&PrivateKeyDer<'_>> for EcdsaSigningKey<SK, SIG>
27+
impl<SecretKey, SigningKey, Signature> TryFrom<&PrivateKeyDer<'_>>
28+
for EcdsaSigningKey<SecretKey, SigningKey, Signature>
3129
where
32-
SK: EcdsaKey + ::pkcs8::DecodePrivateKey + Send + Sync + 'static,
33-
SIG: Send + Sync + 'static,
30+
SecretKey: Debug + DecodePrivateKey,
31+
SigningKey: EcdsaKey + Send + Sync + 'static + From<SecretKey>,
32+
Signature: Send + Sync + 'static,
3433
{
3534
type Error = rustls::Error;
3635

3736
fn try_from(value: &PrivateKeyDer<'_>) -> Result<Self, Self::Error> {
3837
let pkey = match value {
3938
#[cfg(feature = "pkcs8")]
40-
PrivateKeyDer::Pkcs8(der) => SK::from_pkcs8_der(der.secret_pkcs8_der())
39+
PrivateKeyDer::Pkcs8(der) => SecretKey::from_pkcs8_der(der.secret_pkcs8_der())
40+
.map_err(|e| format!("failed to decrypt private key: {e}")),
41+
#[cfg(feature = "sec1")]
42+
PrivateKeyDer::Sec1(sec1) => SecretKey::from_sec1_der(sec1.secret_sec1_der())
4143
.map_err(|e| format!("failed to decrypt private key: {e}")),
42-
// #[cfg(feature = "sec1")]
43-
// PrivateKeyDer::Sec1(sec1) => SK::from_sec1_der(sec1.secret_sec1_der())
44-
// .map_err(|e| format!("failed to decrypt private key: {e}")),
4544
PrivateKeyDer::Pkcs1(_) => Err("ECDSA does not support PKCS#1 key".into()),
4645
_ => Err("not supported".into()),
4746
};
4847
pkey.map(|kp| Self {
49-
key: Arc::new(kp),
50-
scheme: SK::SCHEME,
48+
key: Arc::new(kp.into()),
49+
scheme: SigningKey::SCHEME,
5150
_phantom: PhantomData,
51+
_phantom_sk: PhantomData,
5252
})
5353
.map_err(rustls::Error::General)
5454
}
5555
}
5656

5757
#[derive(Debug)]
58-
pub struct EcdsaSigningKey<SK, SIG> {
58+
pub struct EcdsaSigningKey<SecretKey, SK, SIG> {
5959
key: Arc<SK>,
6060
scheme: SignatureScheme,
6161
_phantom: PhantomData<SIG>,
62+
_phantom_sk: PhantomData<SecretKey>,
6263
}
6364

64-
impl<SK, SIG> SigningKey for EcdsaSigningKey<SK, SIG>
65+
impl<SecretKey, SK, SIG> SigningKey for EcdsaSigningKey<SecretKey, SK, SIG>
6566
where
67+
SecretKey: Debug + Send + Sync,
6668
SK: Send + Sync + 'static + Debug + ecdsa::signature::RandomizedSigner<SIG>,
6769
SIG: Send + Sync + 'static + Debug + ecdsa::signature::SignatureEncoding,
6870
{
@@ -83,35 +85,39 @@ where
8385
}
8486
}
8587

86-
#[cfg(feature = "ecdsa-p256")]
87-
pub type EcdsaSigningKeyP256 =
88-
EcdsaSigningKey<::p256::ecdsa::SigningKey, ::p256::ecdsa::DerSignature>;
89-
90-
#[cfg(all(feature = "ecdsa-p256", feature = "hash-sha256"))]
91-
impl EcdsaKey for ::p256::ecdsa::SigningKey {
92-
const SCHEME: SignatureScheme = SignatureScheme::ECDSA_NISTP256_SHA256;
93-
}
94-
95-
// #[cfg(feature = "ecdsa-p384")]
96-
// impl DecodePrivateKey for ::p384::ecdsa::SigningKey {}
88+
macro_rules! impl_ecdsa_curve {
89+
($curve:ident, $scheme:expr, $type_name:ident) => {
90+
pub type $type_name = EcdsaSigningKey<
91+
::$curve::SecretKey,
92+
::$curve::ecdsa::SigningKey,
93+
::$curve::ecdsa::DerSignature,
94+
>;
9795

98-
#[cfg(feature = "ecdsa-p384")]
99-
pub type EcdsaSigningKeyP384 =
100-
EcdsaSigningKey<::p384::ecdsa::SigningKey, ::p384::ecdsa::DerSignature>;
96+
impl EcdsaKey for ::$curve::ecdsa::SigningKey {
97+
const SCHEME: SignatureScheme = $scheme;
98+
}
10199

102-
#[cfg(feature = "ecdsa-p521")]
103-
impl EcdsaKey for ::p384::ecdsa::SigningKey {
104-
const SCHEME: SignatureScheme = SignatureScheme::ECDSA_NISTP384_SHA384;
100+
impl DecodePrivateKey for ::$curve::SecretKey {}
101+
};
105102
}
106103

107-
// #[cfg(feature = "ecdsa-p521")]
108-
// impl DecodePrivateKey for ::p521::ecdsa::SigningKey {}
109-
110-
#[cfg(feature = "ecdsa-p521")]
111-
pub type EcdsaSigningKeyP521 =
112-
EcdsaSigningKey<::p521::ecdsa::SigningKey, ::p521::ecdsa::DerSignature>;
104+
#[cfg(all(feature = "ecdsa-p256", feature = "hash-sha256"))]
105+
impl_ecdsa_curve!(
106+
p256,
107+
SignatureScheme::ECDSA_NISTP256_SHA256,
108+
EcdsaSigningKeyP256
109+
);
110+
111+
#[cfg(all(feature = "ecdsa-p384", feature = "hash-sha384"))]
112+
impl_ecdsa_curve!(
113+
p384,
114+
SignatureScheme::ECDSA_NISTP384_SHA384,
115+
EcdsaSigningKeyP384
116+
);
113117

114118
#[cfg(all(feature = "ecdsa-p521", feature = "hash-sha512"))]
115-
impl EcdsaKey for ::p521::ecdsa::SigningKey {
116-
const SCHEME: SignatureScheme = SignatureScheme::ECDSA_NISTP521_SHA512;
117-
}
119+
impl_ecdsa_curve!(
120+
p521,
121+
SignatureScheme::ECDSA_NISTP521_SHA512,
122+
EcdsaSigningKeyP521
123+
);

src/sign/eddsa/ed25519.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ impl TryFrom<&PrivateKeyDer<'_>> for Ed25519SigningKey {
2626
SigningKey::from_pkcs8_der(der.secret_pkcs8_der())
2727
.map_err(|e| format!("failed to decrypt private key: {e}"))
2828
}
29-
// #[cfg(feature = "sec1")]
30-
// PrivateKeyDer::Sec1(sec1) => {
31-
// use sec1::DecodeEcPrivateKey;
32-
// SigningKey::from_sec1_der(sec1.secret_sec1_der())
33-
// .map_err(|e| format!("failed to decrypt private key: {e}"))
34-
// }
29+
30+
// (chat log from tony in zulip)
31+
// Per RFC 8410, only PKCS#8 is supported for ED25519 keys
32+
// https://datatracker.ietf.org/doc/html/rfc8410#section-7
33+
// So no SEC 1 support for ED25519 (despite we do have it compile before?!)
34+
PrivateKeyDer::Sec1(_) => Err("ED25519 does not support SEC 1 key".to_string()),
3535
PrivateKeyDer::Pkcs1(_) => Err("ED25519 does not support PKCS#1 key".to_string()),
3636
_ => Err("not supported".into()),
3737
};

0 commit comments

Comments
 (0)