Skip to content

Commit 04402d6

Browse files
authored
Remove PKCS#8 blanket impls for SEC1 private key traits (#1930)
Companion PR to RustCrypto/formats#1927 which properly composes the PKCS#8 impl in terms of the SEC1 impl
1 parent c88bf24 commit 04402d6

File tree

5 files changed

+37
-19
lines changed

5 files changed

+37
-19
lines changed

Cargo.lock

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ members = [
1616
[patch.crates-io]
1717
digest = { path = "digest" }
1818
signature = { path = "signature" }
19+
20+
sec1 = { git = "https://github.com/RustCrypto/formats/" }

elliptic-curve/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ ecdh = ["arithmetic", "digest", "dep:hkdf"]
6666
group = ["dep:group", "ff"]
6767
jwk = ["dep:base64ct", "dep:serde_json", "alloc", "serde", "zeroize/alloc"]
6868
pkcs8 = ["dep:pkcs8", "sec1"]
69-
pem = ["dep:pem-rfc7468", "alloc", "arithmetic", "pkcs8", "sec1/pem"]
69+
pem = ["dep:pem-rfc7468", "alloc", "arithmetic", "pkcs8/pem", "sec1/pem"]
7070
serde = ["dep:serdect", "alloc", "pkcs8", "sec1/serde"]
7171

7272
[package.metadata.docs.rs]

elliptic-curve/src/secret_key.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use {
3232
FieldBytesSize,
3333
sec1::{EncodedPoint, ModulusSize, ValidatePublicKey},
3434
},
35-
sec1::der::{self, oid::AssociatedOid},
35+
sec1::der::{self, Decode, oid::AssociatedOid},
3636
};
3737

3838
#[cfg(all(feature = "alloc", feature = "arithmetic", feature = "sec1"))]
@@ -363,6 +363,36 @@ where
363363
}
364364
}
365365

366+
#[cfg(feature = "sec1")]
367+
impl<C> sec1::DecodeEcPrivateKey for SecretKey<C>
368+
where
369+
C: AssociatedOid + Curve + ValidatePublicKey,
370+
FieldBytesSize<C>: ModulusSize,
371+
{
372+
fn from_sec1_der(bytes: &[u8]) -> sec1::Result<Self> {
373+
Ok(sec1::EcPrivateKey::from_der(bytes)?.try_into()?)
374+
}
375+
}
376+
377+
#[cfg(all(feature = "alloc", feature = "arithmetic", feature = "sec1"))]
378+
impl<C> sec1::EncodeEcPrivateKey for SecretKey<C>
379+
where
380+
C: AssociatedOid + CurveArithmetic,
381+
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
382+
FieldBytesSize<C>: ModulusSize,
383+
{
384+
fn to_sec1_der(&self) -> sec1::Result<der::SecretDocument> {
385+
let private_key_bytes = Zeroizing::new(self.to_bytes());
386+
let public_key_bytes = self.public_key().to_encoded_point(false);
387+
388+
Ok(der::SecretDocument::encode_msg(&sec1::EcPrivateKey {
389+
private_key: &private_key_bytes,
390+
parameters: None,
391+
public_key: Some(public_key_bytes.as_bytes()),
392+
})?)
393+
}
394+
}
395+
366396
#[cfg(feature = "sec1")]
367397
impl<C> TryFrom<sec1::EcPrivateKey<'_>> for SecretKey<C>
368398
where

elliptic-curve/src/secret_key/pkcs8.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ use {
1818
},
1919
pkcs8::{
2020
EncodePrivateKey,
21-
der::{self, Encode, asn1::OctetStringRef},
21+
der::{self, asn1::OctetStringRef},
2222
},
23-
zeroize::Zeroizing,
2423
};
2524

2625
// Imports for actual PEM support
@@ -72,18 +71,7 @@ where
7271
parameters: Some((&C::OID).into()),
7372
};
7473

75-
let private_key_bytes = Zeroizing::new(self.to_bytes());
76-
let public_key_bytes = self.public_key().to_encoded_point(false);
77-
78-
// TODO(tarcieri): unify with `to_sec1_der()` by building an owned `EcPrivateKey`
79-
let ec_private_key = Zeroizing::new(
80-
EcPrivateKey {
81-
private_key: &private_key_bytes,
82-
parameters: None,
83-
public_key: Some(public_key_bytes.as_bytes()),
84-
}
85-
.to_der()?,
86-
);
74+
let ec_private_key = self.to_sec1_der()?;
8775

8876
let pkcs8_key = pkcs8::PrivateKeyInfoRef::new(
8977
algorithm_identifier,

0 commit comments

Comments
 (0)