Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions elliptic-curve/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions elliptic-curve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,19 @@ members = ["."]

[dependencies]
crypto-bigint = { version = "0.3", default-features = false, features = ["rand_core", "generic-array", "zeroize"] }
der = { version = "=0.5.0-pre.1", default-features = false, features = ["oid"] }
der = { version = "0.5", default-features = false, features = ["oid"] }
generic-array = { version = "0.14", default-features = false }
rand_core = { version = "0.6", default-features = false }
subtle = { version = ">=2, <2.5", default-features = false }
zeroize = { version = ">=1, <1.5", default-features = false }
zeroize = { version = "1", default-features = false }

# optional dependencies
base64ct = { version = "1", optional = true, default-features = false }
ff = { version = "0.11", optional = true, default-features = false }
group = { version = "0.11", optional = true, default-features = false }
hex-literal = { version = "0.3", optional = true }
pem-rfc7468 = { version = "0.2", optional = true }
pkcs8 = { version = "0.8.0-pre", optional = true }
sec1 = { version = "0.2.0-pre", optional = true, features = ["subtle", "zeroize"] }
sec1 = { version = "=0.2.0-pre", optional = true, features = ["subtle", "zeroize"] }
serde = { version = "1", optional = true, default-features = false }
serde_json = { version = "1", optional = true, default-features = false, features = ["alloc"] }

Expand All @@ -46,11 +45,12 @@ default = ["arithmetic"]
alloc = ["der/alloc", "sec1/alloc", "zeroize/alloc"] # todo: use weak activation for `group`/`sec1` alloc when available
arithmetic = ["ff", "group"]
bits = ["arithmetic", "ff/bits"]
dev = ["arithmetic", "hex-literal", "pem"]
dev = ["arithmetic", "hex-literal", "pem", "pkcs8"]
ecdh = ["arithmetic"]
hazmat = []
jwk = ["alloc", "base64ct/alloc", "serde", "serde_json", "zeroize/alloc"]
pem = ["alloc", "arithmetic", "pem-rfc7468/alloc", "pkcs8/pem", "sec1/alloc"]
pem = ["alloc", "arithmetic", "pem-rfc7468/alloc", "pkcs8", "sec1/pem"]
pkcs8 = ["sec1/pkcs8"]
std = ["alloc", "rand_core/std"]

[package.metadata.docs.rs]
Expand Down
1 change: 1 addition & 0 deletions elliptic-curve/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
bigint::{Limb, U256},
error::{Error, Result},
ops::Reduce,
pkcs8,
rand_core::RngCore,
sec1::{FromEncodedPoint, ToEncodedPoint},
subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption},
Expand Down
3 changes: 3 additions & 0 deletions elliptic-curve/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

use core::fmt::{self, Display};

#[cfg(feature = "pkcs8")]
use crate::pkcs8;

/// Result type with the `elliptic-curve` crate's [`Error`] type.
pub type Result<T> = core::result::Result<T, Error>;

Expand Down
2 changes: 1 addition & 1 deletion elliptic-curve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub use crate::scalar::ScalarBits;
pub use crate::jwk::{JwkEcKey, JwkParameters};

#[cfg(feature = "pkcs8")]
pub use pkcs8;
pub use ::sec1::pkcs8;

use core::fmt::Debug;
use generic_array::GenericArray;
Expand Down
23 changes: 17 additions & 6 deletions elliptic-curve/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use group::{Curve as _, Group};
use crate::{JwkEcKey, JwkParameters};

#[cfg(all(feature = "sec1", feature = "pkcs8"))]
use {
crate::{AlgorithmParameters, ALGORITHM_OID},
pkcs8::DecodePublicKey,
use crate::{
pkcs8::{self, DecodePublicKey},
AlgorithmParameters, ALGORITHM_OID,
};

#[cfg(feature = "pem")]
Expand Down Expand Up @@ -266,20 +266,31 @@ where

#[cfg(all(feature = "pkcs8", feature = "sec1"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "pkcs8", feature = "sec1"))))]
impl<C> DecodePublicKey for PublicKey<C>
impl<C> TryFrom<pkcs8::SubjectPublicKeyInfo<'_>> for PublicKey<C>
where
C: Curve + AlgorithmParameters + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
{
fn from_spki(spki: pkcs8::SubjectPublicKeyInfo<'_>) -> pkcs8::spki::Result<Self> {
spki.algorithm.assert_oids(ALGORITHM_OID, C::OID)?;
type Error = pkcs8::spki::Error;

fn try_from(spki: pkcs8::SubjectPublicKeyInfo<'_>) -> pkcs8::spki::Result<Self> {
spki.algorithm.assert_oids(ALGORITHM_OID, C::OID)?;
Self::from_sec1_bytes(spki.subject_public_key)
.map_err(|_| der::Tag::BitString.value_error().into())
}
}

#[cfg(all(feature = "pkcs8", feature = "sec1"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "pkcs8", feature = "sec1"))))]
impl<C> DecodePublicKey for PublicKey<C>
where
C: Curve + AlgorithmParameters + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
{
}

#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
impl<C> EncodePublicKey for PublicKey<C>
Expand Down
18 changes: 13 additions & 5 deletions elliptic-curve/src/secret_key/pkcs8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

use super::SecretKey;
use crate::{
pkcs8::{self, DecodePrivateKey},
sec1::{ModulusSize, ValidatePublicKey},
AlgorithmParameters, Curve, FieldSize, ALGORITHM_OID,
};
use der::Decodable;
use pkcs8::DecodePrivateKey;
use sec1::EcPrivateKey;

// Imports for the `EncodePrivateKey` impl
Expand All @@ -28,14 +28,14 @@ use {
};

#[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))]
impl<C> DecodePrivateKey for SecretKey<C>
impl<C> TryFrom<pkcs8::PrivateKeyInfo<'_>> for SecretKey<C>
where
C: Curve + AlgorithmParameters + ValidatePublicKey,
FieldSize<C>: ModulusSize,
{
fn from_pkcs8_private_key_info(
private_key_info: pkcs8::PrivateKeyInfo<'_>,
) -> pkcs8::Result<Self> {
type Error = pkcs8::Error;

fn try_from(private_key_info: pkcs8::PrivateKeyInfo<'_>) -> pkcs8::Result<Self> {
private_key_info
.algorithm
.assert_oids(ALGORITHM_OID, C::OID)?;
Expand All @@ -45,6 +45,14 @@ where
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))]
impl<C> DecodePrivateKey for SecretKey<C>
where
C: Curve + AlgorithmParameters + ValidatePublicKey,
FieldSize<C>: ModulusSize,
{
}

// TODO(tarcieri): use weak activation of `pkcs8/alloc` for this when possible
// It doesn't strictly depend on `pkcs8/pem` but we can't easily activate `pkcs8/alloc`
// without adding a separate crate feature just for this functionality.
Expand Down
2 changes: 1 addition & 1 deletion elliptic-curve/tests/pkcs8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

use elliptic_curve::{
dev::{PublicKey, SecretKey},
pkcs8::{DecodePrivateKey, DecodePublicKey, EncodePrivateKey, PrivateKeyDocument},
sec1::ToEncodedPoint,
};
use hex_literal::hex;
use pkcs8::{DecodePrivateKey, DecodePublicKey, EncodePrivateKey, PrivateKeyDocument};

/// DER-encoded PKCS#8 public key
const PKCS8_PUBLIC_KEY_DER: &[u8; 91] = include_bytes!("examples/pkcs8-public-key.der");
Expand Down