From 63a915b5b5e1b46effc599ed4b85476249e7eefd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0pa=C4=8Dek?= Date: Sun, 3 Jul 2022 21:58:37 +0200 Subject: [PATCH] Add conversions From our keys --- src/pubkey/ecdsa.rs | 46 ++++++++++++++++++++++++++++++------------- src/pubkey/ed25519.rs | 22 +++++++++++++-------- src/pubkey/rsa.rs | 21 ++++++++++++-------- 3 files changed, 59 insertions(+), 30 deletions(-) diff --git a/src/pubkey/ecdsa.rs b/src/pubkey/ecdsa.rs index c798d59..f5a165b 100644 --- a/src/pubkey/ecdsa.rs +++ b/src/pubkey/ecdsa.rs @@ -35,6 +35,9 @@ pub static ECDSA_SHA2_NISTP384: PubkeyAlgo = PubkeyAlgo { /// /// - `EcdsaPubkey` is compatible with [`ECDSA_SHA2_NISTP256`]. /// - `EcdsaPubkey` is compatible with [`ECDSA_SHA2_NISTP384`]. +/// +/// You can convert it to and from [`ecdsa::VerifyingKey`] and [`elliptic_curve::PublicKey`] +/// using `from()`/`into()`. #[derive(Debug, Clone)] pub struct EcdsaPubkey where C: ecdsa::PrimeCurve + elliptic_curve::ProjectiveArithmetic, @@ -46,6 +49,9 @@ pub struct EcdsaPubkey /// /// - `EcdsaPrivkey` is compatible with [`ECDSA_SHA2_NISTP256`]. /// - `EcdsaPrivkey` is compatible with [`ECDSA_SHA2_NISTP384`]. +/// +/// You can convert it to and from [`ecdsa::SigningKey`] and [`elliptic_curve::SecretKey`] +/// using `from()`/`into()`. #[derive(Clone)] pub struct EcdsaPrivkey where C: ecdsa::PrimeCurve + elliptic_curve::ProjectiveArithmetic, @@ -247,9 +253,25 @@ impl Curve for p384::NistP384 { impl From> for EcdsaPubkey where C: ecdsa::PrimeCurve + elliptic_curve::ProjectiveArithmetic, { - fn from(verifying: ecdsa::VerifyingKey) -> Self { - Self { verifying } - } + fn from(verifying: ecdsa::VerifyingKey) -> Self { Self { verifying } } +} + +impl From> for EcdsaPubkey + where C: ecdsa::PrimeCurve + elliptic_curve::ProjectiveArithmetic, +{ + fn from(public: elliptic_curve::PublicKey) -> Self { Self { verifying: public.into() } } +} + +impl From> for ecdsa::VerifyingKey + where C: ecdsa::PrimeCurve + elliptic_curve::ProjectiveArithmetic, +{ + fn from(pubkey: EcdsaPubkey) -> Self { pubkey.verifying } +} + +impl From> for elliptic_curve::PublicKey + where C: ecdsa::PrimeCurve + elliptic_curve::ProjectiveArithmetic, +{ + fn from(pubkey: EcdsaPubkey) -> Self { pubkey.verifying.into() } } impl From> for EcdsaPrivkey @@ -257,27 +279,23 @@ impl From> for EcdsaPrivkey ::Scalar: ecdsa::hazmat::SignPrimitive, ecdsa::SignatureSize: generic_array::ArrayLength, { - fn from(signing: ecdsa::SigningKey) -> Self { - Self { signing } - } + fn from(signing: ecdsa::SigningKey) -> Self { Self { signing } } } -impl From> for EcdsaPubkey +impl From> for EcdsaPrivkey where C: ecdsa::PrimeCurve + elliptic_curve::ProjectiveArithmetic, + ::Scalar: ecdsa::hazmat::SignPrimitive, + ecdsa::SignatureSize: generic_array::ArrayLength, { - fn from(public: elliptic_curve::PublicKey) -> Self { - Self { verifying: public.into() } - } + fn from(secret: elliptic_curve::SecretKey) -> Self { Self { signing: secret.into() } } } -impl From> for EcdsaPrivkey +impl From> for ecdsa::SigningKey where C: ecdsa::PrimeCurve + elliptic_curve::ProjectiveArithmetic, ::Scalar: ecdsa::hazmat::SignPrimitive, ecdsa::SignatureSize: generic_array::ArrayLength, { - fn from(secret: elliptic_curve::SecretKey) -> Self { - Self { signing: secret.into() } - } + fn from(privkey: EcdsaPrivkey) -> Self { privkey.signing } } impl fmt::Display for EcdsaPubkey { diff --git a/src/pubkey/ed25519.rs b/src/pubkey/ed25519.rs index 31a2a5f..ffa4dfc 100644 --- a/src/pubkey/ed25519.rs +++ b/src/pubkey/ed25519.rs @@ -16,7 +16,8 @@ pub static SSH_ED25519: PubkeyAlgo = PubkeyAlgo { /// Ed25519 public key from RFC 8032. /// -/// This key is compatible with [`SSH_ED25519`]. +/// This key is compatible with [`SSH_ED25519`]. You can convert it to and from +/// [`ed25519_dalek::PublicKey`] using `from()`/`into()`. #[derive(Debug, Clone)] pub struct Ed25519Pubkey { pub(crate) pubkey: ed25519_dalek::PublicKey, @@ -24,7 +25,8 @@ pub struct Ed25519Pubkey { /// Ed25519 keypair from RFC 8032. /// -/// This key is compatible with [`SSH_ED25519`]. +/// This key is compatible with [`SSH_ED25519`]. You can convert it to and from +/// [`ed25519_dalek::Keypair`] using `from()`/`into()`. pub struct Ed25519Privkey { pub(crate) keypair: ed25519_dalek::Keypair, } @@ -80,15 +82,19 @@ pub(super) fn decode(blob: &mut PacketDecode) -> Result { impl From for Ed25519Pubkey { - fn from(pubkey: ed25519_dalek::PublicKey) -> Self { - Self { pubkey } - } + fn from(pubkey: ed25519_dalek::PublicKey) -> Self { Self { pubkey } } +} + +impl From for ed25519_dalek::PublicKey { + fn from(pubkey: Ed25519Pubkey) -> Self { pubkey.pubkey } } impl From for Ed25519Privkey { - fn from(keypair: ed25519_dalek::Keypair) -> Self { - Self { keypair } - } + fn from(keypair: ed25519_dalek::Keypair) -> Self { Self { keypair } } +} + +impl From for ed25519_dalek::Keypair { + fn from(privkey: Ed25519Privkey) -> Self { privkey.keypair } } impl fmt::Display for Ed25519Pubkey { diff --git a/src/pubkey/rsa.rs b/src/pubkey/rsa.rs index 021d593..c2304d6 100644 --- a/src/pubkey/rsa.rs +++ b/src/pubkey/rsa.rs @@ -37,7 +37,8 @@ pub static RSA_SHA2_512: PubkeyAlgo = PubkeyAlgo { /// RSA public key. /// -/// This key is compatible with [`SSH_RSA_SHA1`], [`RSA_SHA2_256`] and [`RSA_SHA2_512`]. +/// This key is compatible with [`SSH_RSA_SHA1`], [`RSA_SHA2_256`] and [`RSA_SHA2_512`]. You can +/// convert it to and from [`rsa::RsaPublicKey`] using `from()`/`into()`. #[derive(Debug, Clone)] pub struct RsaPubkey { pub(crate) pubkey: rsa::RsaPublicKey, @@ -45,7 +46,8 @@ pub struct RsaPubkey { /// RSA whole key (private and public parts). /// -/// This key is compatible with [`SSH_RSA_SHA1`], [`RSA_SHA2_256`] and [`RSA_SHA2_512`]. +/// This key is compatible with [`SSH_RSA_SHA1`], [`RSA_SHA2_256`] and [`RSA_SHA2_512`]. You can +/// convert it to and from [`rsa::RsaPrivateKey`] using `from()`/`into()`. #[derive(Clone)] pub struct RsaPrivkey { pub(crate) privkey: rsa::RsaPrivateKey, @@ -134,17 +136,20 @@ impl RsaHash for sha2::Sha512 { } impl From for RsaPubkey { - fn from(pubkey: rsa::RsaPublicKey) -> Self { - Self { pubkey } - } + fn from(pubkey: rsa::RsaPublicKey) -> Self { Self { pubkey } } +} + +impl From for rsa::RsaPublicKey { + fn from(pubkey: RsaPubkey) -> Self { pubkey.pubkey } } impl From for RsaPrivkey { - fn from(privkey: rsa::RsaPrivateKey) -> Self { - Self { privkey } - } + fn from(privkey: rsa::RsaPrivateKey) -> Self { Self { privkey } } } +impl From for rsa::RsaPrivateKey { + fn from(privkey: RsaPrivkey) -> Self { privkey.privkey } +} impl fmt::Display for RsaPubkey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {