Skip to content

Commit

Permalink
Add conversions From our keys
Browse files Browse the repository at this point in the history
  • Loading branch information
honzasp committed Jul 3, 2022
1 parent b5eec32 commit 63a915b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 30 deletions.
46 changes: 32 additions & 14 deletions src/pubkey/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pub static ECDSA_SHA2_NISTP384: PubkeyAlgo = PubkeyAlgo {
///
/// - `EcdsaPubkey<p256::NistP256>` is compatible with [`ECDSA_SHA2_NISTP256`].
/// - `EcdsaPubkey<p384::NistP384>` is compatible with [`ECDSA_SHA2_NISTP384`].
///
/// You can convert it to and from [`ecdsa::VerifyingKey<C>`] and [`elliptic_curve::PublicKey<C>`]
/// using `from()`/`into()`.
#[derive(Debug, Clone)]
pub struct EcdsaPubkey<C>
where C: ecdsa::PrimeCurve + elliptic_curve::ProjectiveArithmetic,
Expand All @@ -46,6 +49,9 @@ pub struct EcdsaPubkey<C>
///
/// - `EcdsaPrivkey<p256::NistP256>` is compatible with [`ECDSA_SHA2_NISTP256`].
/// - `EcdsaPrivkey<p384::NistP384>` is compatible with [`ECDSA_SHA2_NISTP384`].
///
/// You can convert it to and from [`ecdsa::SigningKey<C>`] and [`elliptic_curve::SecretKey<C>`]
/// using `from()`/`into()`.
#[derive(Clone)]
pub struct EcdsaPrivkey<C>
where C: ecdsa::PrimeCurve + elliptic_curve::ProjectiveArithmetic,
Expand Down Expand Up @@ -247,37 +253,49 @@ impl Curve for p384::NistP384 {
impl<C> From<ecdsa::VerifyingKey<C>> for EcdsaPubkey<C>
where C: ecdsa::PrimeCurve + elliptic_curve::ProjectiveArithmetic,
{
fn from(verifying: ecdsa::VerifyingKey<C>) -> Self {
Self { verifying }
}
fn from(verifying: ecdsa::VerifyingKey<C>) -> Self { Self { verifying } }
}

impl<C> From<elliptic_curve::PublicKey<C>> for EcdsaPubkey<C>
where C: ecdsa::PrimeCurve + elliptic_curve::ProjectiveArithmetic,
{
fn from(public: elliptic_curve::PublicKey<C>) -> Self { Self { verifying: public.into() } }
}

impl<C> From<EcdsaPubkey<C>> for ecdsa::VerifyingKey<C>
where C: ecdsa::PrimeCurve + elliptic_curve::ProjectiveArithmetic,
{
fn from(pubkey: EcdsaPubkey<C>) -> Self { pubkey.verifying }
}

impl<C> From<EcdsaPubkey<C>> for elliptic_curve::PublicKey<C>
where C: ecdsa::PrimeCurve + elliptic_curve::ProjectiveArithmetic,
{
fn from(pubkey: EcdsaPubkey<C>) -> Self { pubkey.verifying.into() }
}

impl<C> From<ecdsa::SigningKey<C>> for EcdsaPrivkey<C>
where C: ecdsa::PrimeCurve + elliptic_curve::ProjectiveArithmetic,
<C as elliptic_curve::ScalarArithmetic>::Scalar: ecdsa::hazmat::SignPrimitive<C>,
ecdsa::SignatureSize<C>: generic_array::ArrayLength<u8>,
{
fn from(signing: ecdsa::SigningKey<C>) -> Self {
Self { signing }
}
fn from(signing: ecdsa::SigningKey<C>) -> Self { Self { signing } }
}

impl<C> From<elliptic_curve::PublicKey<C>> for EcdsaPubkey<C>
impl<C> From<elliptic_curve::SecretKey<C>> for EcdsaPrivkey<C>
where C: ecdsa::PrimeCurve + elliptic_curve::ProjectiveArithmetic,
<C as elliptic_curve::ScalarArithmetic>::Scalar: ecdsa::hazmat::SignPrimitive<C>,
ecdsa::SignatureSize<C>: generic_array::ArrayLength<u8>,
{
fn from(public: elliptic_curve::PublicKey<C>) -> Self {
Self { verifying: public.into() }
}
fn from(secret: elliptic_curve::SecretKey<C>) -> Self { Self { signing: secret.into() } }
}

impl<C> From<elliptic_curve::SecretKey<C>> for EcdsaPrivkey<C>
impl<C> From<EcdsaPrivkey<C>> for ecdsa::SigningKey<C>
where C: ecdsa::PrimeCurve + elliptic_curve::ProjectiveArithmetic,
<C as elliptic_curve::ScalarArithmetic>::Scalar: ecdsa::hazmat::SignPrimitive<C>,
ecdsa::SignatureSize<C>: generic_array::ArrayLength<u8>,
{
fn from(secret: elliptic_curve::SecretKey<C>) -> Self {
Self { signing: secret.into() }
}
fn from(privkey: EcdsaPrivkey<C>) -> Self { privkey.signing }
}

impl fmt::Display for EcdsaPubkey<p256::NistP256> {
Expand Down
22 changes: 14 additions & 8 deletions src/pubkey/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ 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,
}

/// 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,
}
Expand Down Expand Up @@ -80,15 +82,19 @@ pub(super) fn decode(blob: &mut PacketDecode) -> Result<Ed25519Pubkey> {


impl From<ed25519_dalek::PublicKey> for Ed25519Pubkey {
fn from(pubkey: ed25519_dalek::PublicKey) -> Self {
Self { pubkey }
}
fn from(pubkey: ed25519_dalek::PublicKey) -> Self { Self { pubkey } }
}

impl From<Ed25519Pubkey> for ed25519_dalek::PublicKey {
fn from(pubkey: Ed25519Pubkey) -> Self { pubkey.pubkey }
}

impl From<ed25519_dalek::Keypair> for Ed25519Privkey {
fn from(keypair: ed25519_dalek::Keypair) -> Self {
Self { keypair }
}
fn from(keypair: ed25519_dalek::Keypair) -> Self { Self { keypair } }
}

impl From<Ed25519Privkey> for ed25519_dalek::Keypair {
fn from(privkey: Ed25519Privkey) -> Self { privkey.keypair }
}

impl fmt::Display for Ed25519Pubkey {
Expand Down
21 changes: 13 additions & 8 deletions src/pubkey/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ 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,
}

/// 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,
Expand Down Expand Up @@ -134,17 +136,20 @@ impl RsaHash for sha2::Sha512 {
}

impl From<rsa::RsaPublicKey> for RsaPubkey {
fn from(pubkey: rsa::RsaPublicKey) -> Self {
Self { pubkey }
}
fn from(pubkey: rsa::RsaPublicKey) -> Self { Self { pubkey } }
}

impl From<RsaPubkey> for rsa::RsaPublicKey {
fn from(pubkey: RsaPubkey) -> Self { pubkey.pubkey }
}

impl From<rsa::RsaPrivateKey> for RsaPrivkey {
fn from(privkey: rsa::RsaPrivateKey) -> Self {
Self { privkey }
}
fn from(privkey: rsa::RsaPrivateKey) -> Self { Self { privkey } }
}

impl From<RsaPrivkey> for rsa::RsaPrivateKey {
fn from(privkey: RsaPrivkey) -> Self { privkey.privkey }
}

impl fmt::Display for RsaPubkey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down

0 comments on commit 63a915b

Please sign in to comment.