Skip to content

Commit

Permalink
crypto: collapse 'der' feature into 'pem'
Browse files Browse the repository at this point in the history
Outputting some key types to 'der' format required their 'pem' feature to
be enabled, so instead of having a separate 'der' and 'pem' feature lets
just collapse them into a single feature to simplify things.
  • Loading branch information
bmwill committed Oct 27, 2024
1 parent 9b571b4 commit d617fe1
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 42 deletions.
12 changes: 10 additions & 2 deletions crates/sui-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,16 @@ zklogin = [
"dep:serde_json",
"signature/std",
]
der = ["dep:pkcs8", "ed25519-dalek?/pkcs8", "p256?/pkcs8", "k256?/pkcs8"]
pem = ["der", "dep:pem-rfc7468", "ed25519-dalek?/pem", "p256?/pem", "k256?/pem"]
pem = [
"dep:pkcs8",
"dep:pem-rfc7468",
"ed25519-dalek?/pkcs8",
"p256?/pkcs8",
"k256?/pkcs8",
"ed25519-dalek?/pem",
"p256?/pem",
"k256?/pem",
]

[dependencies]
signature = "2.2"
Expand Down
20 changes: 10 additions & 10 deletions crates/sui-crypto/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ impl Ed25519PrivateKey {
Self(buf.into())
}

#[cfg(feature = "der")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "der")))]
#[cfg(feature = "pem")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "pem")))]
/// Deserialize PKCS#8 private key from ASN.1 DER-encoded data (binary format).
pub fn from_der(bytes: &[u8]) -> Result<Self, SignatureError> {
ed25519_dalek::pkcs8::DecodePrivateKey::from_pkcs8_der(bytes)
.map(Self)
.map_err(SignatureError::from_source)
}

#[cfg(feature = "der")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "der")))]
#[cfg(feature = "pem")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "pem")))]
/// Serialize this private key as DER-encoded PKCS#8
pub fn to_der(&self) -> Result<Vec<u8>, SignatureError> {
use ed25519_dalek::pkcs8::EncodePrivateKey;
Expand Down Expand Up @@ -102,7 +102,7 @@ impl Ed25519PrivateKey {
.map(|pem| (*pem).to_owned())
}

#[cfg(feature = "der")]
#[cfg(feature = "pem")]
pub(crate) fn from_dalek(private_key: ed25519_dalek::SigningKey) -> Self {
Self(private_key)
}
Expand Down Expand Up @@ -145,17 +145,17 @@ impl Ed25519VerifyingKey {
Ed25519PublicKey::new(self.0.to_bytes())
}

#[cfg(feature = "der")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "der")))]
#[cfg(feature = "pem")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "pem")))]
/// Deserialize public key from ASN.1 DER-encoded data (binary format).
pub fn from_der(bytes: &[u8]) -> Result<Self, SignatureError> {
ed25519_dalek::pkcs8::DecodePublicKey::from_public_key_der(bytes)
.map(Self)
.map_err(SignatureError::from_source)
}

#[cfg(feature = "der")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "der")))]
#[cfg(feature = "pem")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "pem")))]
/// Serialize this public key as DER-encoded data
pub fn to_der(&self) -> Result<Vec<u8>, SignatureError> {
use pkcs8::EncodePublicKey;
Expand Down Expand Up @@ -186,7 +186,7 @@ impl Ed25519VerifyingKey {
.map_err(SignatureError::from_source)
}

#[cfg(feature = "der")]
#[cfg(feature = "pem")]
pub(crate) fn from_dalek(verifying_key: ed25519_dalek::VerifyingKey) -> Self {
Self(verifying_key)
}
Expand Down
20 changes: 10 additions & 10 deletions crates/sui-crypto/src/secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ impl Secp256k1PrivateKey {
Self(SigningKey::random(&mut rng))
}

#[cfg(feature = "der")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "der")))]
#[cfg(feature = "pem")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "pem")))]
/// Deserialize PKCS#8 private key from ASN.1 DER-encoded data (binary format).
pub fn from_der(bytes: &[u8]) -> Result<Self, SignatureError> {
k256::pkcs8::DecodePrivateKey::from_pkcs8_der(bytes)
.map(Self)
.map_err(SignatureError::from_source)
}

#[cfg(feature = "der")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "der")))]
#[cfg(feature = "pem")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "pem")))]
/// Serialize this private key as DER-encoded PKCS#8
pub fn to_der(&self) -> Result<Vec<u8>, SignatureError> {
use k256::pkcs8::EncodePrivateKey;
Expand Down Expand Up @@ -105,7 +105,7 @@ impl Secp256k1PrivateKey {
.map(|pem| (*pem).to_owned())
}

#[cfg(feature = "der")]
#[cfg(feature = "pem")]
pub(crate) fn from_k256(private_key: SigningKey) -> Self {
Self(private_key)
}
Expand Down Expand Up @@ -147,17 +147,17 @@ impl Secp256k1VerifyingKey {
Secp256k1PublicKey::new(self.0.as_ref().to_bytes().into())
}

#[cfg(feature = "der")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "der")))]
#[cfg(feature = "pem")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "pem")))]
/// Deserialize public key from ASN.1 DER-encoded data (binary format).
pub fn from_der(bytes: &[u8]) -> Result<Self, SignatureError> {
k256::pkcs8::DecodePublicKey::from_public_key_der(bytes)
.map(Self)
.map_err(SignatureError::from_source)
}

#[cfg(feature = "der")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "der")))]
#[cfg(feature = "pem")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "pem")))]
/// Serialize this public key as DER-encoded data
pub fn to_der(&self) -> Result<Vec<u8>, SignatureError> {
use pkcs8::EncodePublicKey;
Expand Down Expand Up @@ -188,7 +188,7 @@ impl Secp256k1VerifyingKey {
.map_err(SignatureError::from_source)
}

#[cfg(feature = "der")]
#[cfg(feature = "pem")]
pub(crate) fn from_k256(verifying_key: VerifyingKey) -> Self {
Self(verifying_key)
}
Expand Down
20 changes: 10 additions & 10 deletions crates/sui-crypto/src/secp256r1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ impl Secp256r1PrivateKey {
Self::new(buf)
}

#[cfg(feature = "der")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "der")))]
#[cfg(feature = "pem")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "pem")))]
/// Deserialize PKCS#8 private key from ASN.1 DER-encoded data (binary format).
pub fn from_der(bytes: &[u8]) -> Result<Self, SignatureError> {
p256::pkcs8::DecodePrivateKey::from_pkcs8_der(bytes)
.map(Self)
.map_err(SignatureError::from_source)
}

#[cfg(feature = "der")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "der")))]
#[cfg(feature = "pem")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "pem")))]
/// Serialize this private key as DER-encoded PKCS#8
pub fn to_der(&self) -> Result<Vec<u8>, SignatureError> {
use p256::pkcs8::EncodePrivateKey;
Expand Down Expand Up @@ -105,7 +105,7 @@ impl Secp256r1PrivateKey {
.map(|pem| (*pem).to_owned())
}

#[cfg(feature = "der")]
#[cfg(feature = "pem")]
pub(crate) fn from_p256(private_key: SigningKey) -> Self {
Self(private_key)
}
Expand Down Expand Up @@ -147,17 +147,17 @@ impl Secp256r1VerifyingKey {
Secp256r1PublicKey::new(self.0.as_ref().to_bytes().into())
}

#[cfg(feature = "der")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "der")))]
#[cfg(feature = "pem")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "pem")))]
/// Deserialize public key from ASN.1 DER-encoded data (binary format).
pub fn from_der(bytes: &[u8]) -> Result<Self, SignatureError> {
p256::pkcs8::DecodePublicKey::from_public_key_der(bytes)
.map(Self)
.map_err(SignatureError::from_source)
}

#[cfg(feature = "der")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "der")))]
#[cfg(feature = "pem")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "pem")))]
/// Serialize this public key as DER-encoded data
pub fn to_der(&self) -> Result<Vec<u8>, SignatureError> {
use pkcs8::EncodePublicKey;
Expand Down Expand Up @@ -188,7 +188,7 @@ impl Secp256r1VerifyingKey {
.map_err(SignatureError::from_source)
}

#[cfg(feature = "der")]
#[cfg(feature = "pem")]
pub(crate) fn from_p256(verifying_key: VerifyingKey) -> Self {
Self(verifying_key)
}
Expand Down
20 changes: 10 additions & 10 deletions crates/sui-crypto/src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ mod keypair {
self.verifying_key().public_key()
}

#[cfg(feature = "der")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "der")))]
#[cfg(feature = "pem")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "pem")))]
/// Deserialize PKCS#8 private key from ASN.1 DER-encoded data (binary format).
pub fn from_der(bytes: &[u8]) -> Result<Self, SignatureError> {
let private_key =
Expand Down Expand Up @@ -179,8 +179,8 @@ mod keypair {
.map(|inner| Self { inner })
}

#[cfg(feature = "der")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "der")))]
#[cfg(feature = "pem")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "pem")))]
/// Serialize this private key as DER-encoded PKCS#8
pub fn to_der(&self) -> Result<Vec<u8>, SignatureError> {
match &self.inner {
Expand All @@ -206,8 +206,8 @@ mod keypair {
Self::from_der(doc.as_bytes())
}

#[cfg(feature = "der")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "der")))]
#[cfg(feature = "pem")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "pem")))]
/// Serialize this private key as DER-encoded PKCS#8
pub fn to_pem(&self) -> Result<String, SignatureError> {
match &self.inner {
Expand Down Expand Up @@ -312,8 +312,8 @@ mod keypair {
}
}

#[cfg(feature = "der")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "der")))]
#[cfg(feature = "pem")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "pem")))]
/// Deserialize public key from ASN.1 DER-encoded data (binary format).
pub fn from_der(bytes: &[u8]) -> Result<Self, SignatureError> {
let public_key = pkcs8::SubjectPublicKeyInfoRef::try_from(bytes)
Expand Down Expand Up @@ -358,8 +358,8 @@ mod keypair {
.map(|inner| Self { inner })
}

#[cfg(feature = "der")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "der")))]
#[cfg(feature = "pem")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "pem")))]
/// Serialize this public key as DER-encoded data
pub fn to_der(&self) -> Result<Vec<u8>, SignatureError> {
match &self.inner {
Expand Down

0 comments on commit d617fe1

Please sign in to comment.