Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/: Introduce rsa feature flag to avoid ring dependency #2860

Merged
merged 11 commits into from Sep 7, 2022
6 changes: 6 additions & 0 deletions core/CHANGELOG.md
@@ -1,3 +1,9 @@
# 0.35.2
GamePad64 marked this conversation as resolved.
Show resolved Hide resolved

- Make `ring` dependency optional. See [PR 2860].

[PR 2860]: https://github.com/libp2p/rust-libp2p/pull/2860/

# 0.35.1

- Update to `p256` `v0.11.0`. See [PR 2636].
Expand Down
5 changes: 3 additions & 2 deletions core/Cargo.toml
Expand Up @@ -40,7 +40,7 @@ zeroize = "1"
_serde = { package = "serde", version = "1", optional = true, features = ["derive"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ring = { version = "0.16.9", features = ["alloc", "std"], default-features = false }
ring = { version = "0.16.9", features = ["alloc", "std"], default-features = false, optional = true}

[dev-dependencies]
async-std = { version = "1.6.2", features = ["attributes"] }
Expand All @@ -59,9 +59,10 @@ serde_json = "1.0"
prost-build = "0.11"

[features]
default = [ "secp256k1", "ecdsa" ]
default = [ "secp256k1", "ecdsa", "rsa" ]
GamePad64 marked this conversation as resolved.
Show resolved Hide resolved
secp256k1 = [ "libsecp256k1" ]
ecdsa = [ "p256" ]
rsa = [ "ring" ]
GamePad64 marked this conversation as resolved.
Show resolved Hide resolved
serde = ["multihash/serde-codec", "_serde"]

[[bench]]
Expand Down
22 changes: 11 additions & 11 deletions core/src/identity.rs
Expand Up @@ -35,7 +35,7 @@
#[cfg(feature = "ecdsa")]
pub mod ecdsa;
pub mod ed25519;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
pub mod rsa;
#[cfg(feature = "secp256k1")]
pub mod secp256k1;
Expand Down Expand Up @@ -68,7 +68,7 @@ use std::convert::{TryFrom, TryInto};
pub enum Keypair {
/// An Ed25519 keypair.
Ed25519(ed25519::Keypair),
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
/// An RSA keypair.
Rsa(rsa::Keypair),
/// A Secp256k1 keypair.
Expand Down Expand Up @@ -101,7 +101,7 @@ impl Keypair {
/// format (i.e. unencrypted) as defined in [RFC5208].
///
/// [RFC5208]: https://tools.ietf.org/html/rfc5208#section-5
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
pub fn rsa_from_pkcs8(pkcs8_der: &mut [u8]) -> Result<Keypair, DecodingError> {
rsa::Keypair::from_pkcs8(pkcs8_der).map(Keypair::Rsa)
}
Expand All @@ -122,7 +122,7 @@ impl Keypair {
use Keypair::*;
match self {
Ed25519(ref pair) => Ok(pair.sign(msg)),
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
Rsa(ref pair) => pair.sign(msg),
#[cfg(feature = "secp256k1")]
Secp256k1(ref pair) => pair.secret().sign(msg),
Expand All @@ -136,7 +136,7 @@ impl Keypair {
use Keypair::*;
match self {
Ed25519(pair) => PublicKey::Ed25519(pair.public()),
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
Rsa(pair) => PublicKey::Rsa(pair.public()),
#[cfg(feature = "secp256k1")]
Secp256k1(pair) => PublicKey::Secp256k1(pair.public().clone()),
Expand All @@ -154,7 +154,7 @@ impl Keypair {
r#type: keys_proto::KeyType::Ed25519.into(),
data: data.encode().into(),
},
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
Self::Rsa(_) => {
return Err(DecodingError::new(
"Encoding RSA key into Protobuf is unsupported",
Expand Down Expand Up @@ -218,7 +218,7 @@ impl zeroize::Zeroize for keys_proto::PrivateKey {
pub enum PublicKey {
/// A public Ed25519 key.
Ed25519(ed25519::PublicKey),
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
/// A public RSA key.
Rsa(rsa::PublicKey),
#[cfg(feature = "secp256k1")]
Expand All @@ -239,7 +239,7 @@ impl PublicKey {
use PublicKey::*;
match self {
Ed25519(pk) => pk.verify(msg, sig),
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
Rsa(pk) => pk.verify(msg, sig),
#[cfg(feature = "secp256k1")]
Secp256k1(pk) => pk.verify(msg, sig),
Expand Down Expand Up @@ -286,7 +286,7 @@ impl From<&PublicKey> for keys_proto::PublicKey {
r#type: keys_proto::KeyType::Ed25519 as i32,
data: key.encode().to_vec(),
},
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
PublicKey::Rsa(key) => keys_proto::PublicKey {
r#type: keys_proto::KeyType::Rsa as i32,
data: key.encode_x509(),
Expand Down Expand Up @@ -316,11 +316,11 @@ impl TryFrom<keys_proto::PublicKey> for PublicKey {
keys_proto::KeyType::Ed25519 => {
ed25519::PublicKey::decode(&pubkey.data).map(PublicKey::Ed25519)
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
keys_proto::KeyType::Rsa => {
rsa::PublicKey::decode_x509(&pubkey.data).map(PublicKey::Rsa)
}
#[cfg(target_arch = "wasm32")]
#[cfg(any(not(feature = "rsa"), target_arch = "wasm32"))]
keys_proto::KeyType::Rsa => {
log::debug!("support for RSA was disabled at compile-time");
Err(DecodingError::new("Unsupported"))
Expand Down
1 change: 1 addition & 0 deletions core/src/identity/error.rs
Expand Up @@ -74,6 +74,7 @@ impl SigningError {
}
}

#[allow(dead_code)]
pub(crate) fn source(self, source: impl Error + Send + Sync + 'static) -> Self {
Self {
source: Some(Box::new(source)),
Expand Down