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
1 change: 1 addition & 0 deletions .github/workflows/ssh-key.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
paths:
- ".github/workflows/ssh-key.yml"
- "ssh-cipher/**"
- "ssh-derive/**"
- "ssh-encoding/**"
- "ssh-key/**"
- "Cargo.*"
Expand Down
6 changes: 5 additions & 1 deletion ssh-key/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ readme = "README.md"
edition = "2024"
rust-version = "1.87"

[dependencies.encoding]
package = "ssh-encoding"
version = "0.4.0-rc.2"
features = ["base64", "derive", "digest", "pem", "ctutils", "zeroize"]

[dependencies]
cipher = { package = "ssh-cipher", version = "0.4.0-rc.0", features = ["encoding", "zeroize"] }
ctutils = { version = "0.4.8", default-features = false }
encoding = { package = "ssh-encoding", version = "0.4.0-rc.2", features = ["base64", "digest", "pem", "ctutils", "zeroize"] }
signature = { version = "3", default-features = false }
zeroize = { version = "1.9", default-features = false }

Expand Down
15 changes: 2 additions & 13 deletions ssh-key/src/certificate/cert_type.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! OpenSSH certificate types.

use crate::{Error, Result};
use encoding::{Decode, Decoder, Encode, Reader, Writer};
use encoding::{Decode, Decoder, Encode, Reader};

/// Types of OpenSSH certificates: user or host.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[derive(Copy, Clone, Debug, Encode, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[repr(u32)]
#[derive(Default)]
pub enum CertType {
Expand Down Expand Up @@ -38,17 +38,6 @@ impl Decode for CertType {
}
}

impl Encode for CertType {
fn encoded_len(&self) -> encoding::Result<usize> {
Ok(4)
}

fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
u32::from(*self).encode(writer)?;
Ok(())
}
}

impl From<CertType> for u32 {
#[allow(clippy::as_conversions, reason = "repr(u32) enum")]
fn from(cert_type: CertType) -> u32 {
Expand Down
13 changes: 1 addition & 12 deletions ssh-key/src/composite/mldsa87_p384/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl fmt::Debug for MlDsa87P384PrivateKey {
}

/// ML-DSA-87 and ECDSA P-384 composite keypair.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Encode)]
pub struct MlDsa87P384Keypair {
public: MlDsa87P384PublicKey,
private: MlDsa87P384PrivateKey,
Expand Down Expand Up @@ -249,14 +249,3 @@ impl Decode for MlDsa87P384Keypair {
Ok(Self { public, private })
}
}

impl Encode for MlDsa87P384Keypair {
fn encoded_len(&self) -> encoding::Result<usize> {
[self.public.encoded_len()?, self.private.encoded_len()?].checked_sum()
}

fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
self.public.encode(writer)?;
self.private.encode(writer)
}
}
3 changes: 3 additions & 0 deletions ssh-key/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

// The derive macros refer to the encoding crate as `::ssh_encoding`.
extern crate encoding as ssh_encoding;

pub mod authorized_keys;
pub mod private;
pub mod public;
Expand Down
27 changes: 3 additions & 24 deletions ssh-key/src/private/dsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{Error, Mpint, Result, public::DsaPublicKey};
use core::fmt;
use ctutils::{Choice, CtEq};
use encoding::{CheckedSum, Decode, Decoder, Encode, Reader, Writer};
use encoding::{Decode, Decoder, Encode, Reader};
use zeroize::Zeroize;

#[cfg(feature = "dsa")]
Expand All @@ -17,7 +17,7 @@ use rand_core::CryptoRng;
/// range `[1, q–1]`.
///
/// Described in [FIPS 186-4 § 4.1](https://csrc.nist.gov/publications/detail/fips/186/4/final).
#[derive(Clone)]
#[derive(Clone, Encode)]
pub struct DsaPrivateKey {
/// Integer representing a DSA private key.
inner: Mpint,
Expand Down Expand Up @@ -84,16 +84,6 @@ impl Decode for DsaPrivateKey {
}
}

impl Encode for DsaPrivateKey {
fn encoded_len(&self) -> encoding::Result<usize> {
self.inner.encoded_len()
}

fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
self.inner.encode(writer)
}
}

impl fmt::Debug for DsaPrivateKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DsaPrivateKey").finish_non_exhaustive()
Expand Down Expand Up @@ -139,7 +129,7 @@ impl From<&DsaPrivateKey> for Uint {
}

/// Digital Signature Algorithm (DSA) private/public keypair.
#[derive(Clone)]
#[derive(Clone, Encode)]
pub struct DsaKeypair {
/// Public key.
public: DsaPublicKey,
Expand Down Expand Up @@ -210,17 +200,6 @@ impl Decode for DsaKeypair {
}
}

impl Encode for DsaKeypair {
fn encoded_len(&self) -> encoding::Result<usize> {
[self.public.encoded_len()?, self.private.encoded_len()?].checked_sum()
}

fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
self.public.encode(writer)?;
self.private.encode(writer)
}
}

impl From<DsaKeypair> for DsaPublicKey {
fn from(keypair: DsaKeypair) -> DsaPublicKey {
keypair.public
Expand Down
12 changes: 1 addition & 11 deletions ssh-key/src/private/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,11 @@ impl Decode for OpaqueKeypairBytes {
}
}

impl Encode for OpaquePrivateKeyBytes {
fn encoded_len(&self) -> encoding::Result<usize> {
self.0.encoded_len()
}

fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
self.0.encode(writer)
}
}

/// An opaque private key.
///
/// The encoded representation of an `OpaquePrivateKeyBytes` consists of a 4-byte length prefix,
/// followed by its byte representation.
#[derive(Clone)]
#[derive(Clone, Encode)]
pub struct OpaquePrivateKeyBytes(Vec<u8>);

impl AsRef<[u8]> for OpaquePrivateKeyBytes {
Expand Down
22 changes: 1 addition & 21 deletions ssh-key/src/private/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use {
};

/// RSA private key.
#[derive(Clone)]
#[derive(Clone, Encode)]
pub struct RsaPrivateKey {
/// RSA private exponent.
d: Mpint,
Expand Down Expand Up @@ -108,26 +108,6 @@ impl Decode for RsaPrivateKey {
}
}

impl Encode for RsaPrivateKey {
fn encoded_len(&self) -> encoding::Result<usize> {
[
self.d.encoded_len()?,
self.iqmp.encoded_len()?,
self.p.encoded_len()?,
self.q.encoded_len()?,
]
.checked_sum()
}

fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
self.d.encode(writer)?;
self.iqmp.encode(writer)?;
self.p.encode(writer)?;
self.q.encode(writer)?;
Ok(())
}
}

impl Eq for RsaPrivateKey {}
impl PartialEq for RsaPrivateKey {
fn eq(&self, other: &Self) -> bool {
Expand Down
47 changes: 3 additions & 44 deletions ssh-key/src/private/sk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

use crate::{Error, Result, public};
use alloc::vec::Vec;
use encoding::{CheckedSum, Decode, Decoder, Encode, Reader, Writer};
use encoding::{Decode, Decoder, Encode, Reader};

/// Security Key (FIDO/U2F) ECDSA/NIST P-256 private key as specified in
/// [PROTOCOL.u2f](https://cvsweb.openbsd.org/src/usr.bin/ssh/PROTOCOL.u2f?annotate=HEAD).
#[cfg(all(feature = "alloc", feature = "ecdsa"))]
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[derive(Clone, Debug, Encode, Eq, PartialEq, PartialOrd, Ord)]
pub struct SkEcdsaSha2NistP256 {
/// Public key.
public: public::SkEcdsaSha2NistP256,
Expand Down Expand Up @@ -83,31 +83,10 @@ impl Decode for SkEcdsaSha2NistP256 {
}
}

#[cfg(feature = "ecdsa")]
impl Encode for SkEcdsaSha2NistP256 {
fn encoded_len(&self) -> encoding::Result<usize> {
[
self.public.encoded_len()?,
self.flags.encoded_len()?,
self.key_handle.encoded_len()?,
self.reserved.encoded_len()?,
]
.checked_sum()
}

fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
self.public.encode(writer)?;
self.flags.encode(writer)?;
self.key_handle.encode(writer)?;
self.reserved.encode(writer)?;
Ok(())
}
}

/// Security Key (FIDO/U2F) Ed25519 private key as specified in
/// [PROTOCOL.u2f](https://cvsweb.openbsd.org/src/usr.bin/ssh/PROTOCOL.u2f?annotate=HEAD).
#[cfg(feature = "alloc")]
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[derive(Clone, Debug, Encode, Eq, PartialEq, PartialOrd, Ord)]
pub struct SkEd25519 {
/// Public key.
public: public::SkEd25519,
Expand Down Expand Up @@ -178,23 +157,3 @@ impl Decode for SkEd25519 {
})
}
}

impl Encode for SkEd25519 {
fn encoded_len(&self) -> encoding::Result<usize> {
[
self.public.encoded_len()?,
self.flags.encoded_len()?,
self.key_handle.encoded_len()?,
self.reserved.encoded_len()?,
]
.checked_sum()
}

fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
self.public.encode(writer)?;
self.flags.encode(writer)?;
self.key_handle.encode(writer)?;
self.reserved.encode(writer)?;
Ok(())
}
}
23 changes: 2 additions & 21 deletions ssh-key/src/public/dsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

use crate::{Error, Mpint, Result};
use core::hash::{Hash, Hasher};
use encoding::{CheckedSum, Decode, Decoder, Encode, Reader, Writer};
use encoding::{Decode, Decoder, Encode, Reader};

#[cfg(feature = "dsa")]
use encoding::Uint;

/// Digital Signature Algorithm (DSA) public key.
///
/// Described in [FIPS 186-4 § 4.1](https://csrc.nist.gov/publications/detail/fips/186/4/final).
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[derive(Clone, Debug, Encode, Eq, Ord, PartialEq, PartialOrd)]
pub struct DsaPublicKey {
/// Prime modulus.
p: Mpint,
Expand Down Expand Up @@ -82,25 +82,6 @@ impl Decode for DsaPublicKey {
}
}

impl Encode for DsaPublicKey {
fn encoded_len(&self) -> encoding::Result<usize> {
[
self.p.encoded_len()?,
self.q.encoded_len()?,
self.g.encoded_len()?,
self.y.encoded_len()?,
]
.checked_sum()
}

fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
self.p.encode(writer)?;
self.q.encode(writer)?;
self.g.encode(writer)?;
self.y.encode(writer)
}
}

impl Hash for DsaPublicKey {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
Expand Down
16 changes: 3 additions & 13 deletions ssh-key/src/public/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use crate::{Error, Result};
use core::fmt;
use encoding::{CheckedSum, Decode, Decoder, Encode, Reader, Writer};
use encoding::{Decode, Decoder, Encode, Reader};

/// Ed25519 public key.
///
Expand All @@ -19,7 +19,8 @@ use encoding::{CheckedSum, Decode, Decoder, Encode, Reader, Writer};
/// > Here, 'key' is the 32-octet public key described in RFC8032
///
/// [RFC8709 § 4]: https://datatracker.ietf.org/doc/html/rfc8709#section-4
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[derive(Copy, Clone, Debug, Encode, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[ssh(length_prefixed)]
pub struct Ed25519PublicKey(pub [u8; Self::BYTE_SIZE]);

impl Ed25519PublicKey {
Expand All @@ -43,17 +44,6 @@ impl Decode for Ed25519PublicKey {
}
}

impl Encode for Ed25519PublicKey {
fn encoded_len(&self) -> encoding::Result<usize> {
[4, Self::BYTE_SIZE].checked_sum()
}

fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
self.0.as_slice().encode(writer)?;
Ok(())
}
}

impl TryFrom<&[u8]> for Ed25519PublicKey {
type Error = Error;

Expand Down
12 changes: 1 addition & 11 deletions ssh-key/src/public/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Encode for OpaquePublicKey {
///
/// The encoded representation of an `OpaquePublicKeyBytes` consists of a 4-byte length prefix,
/// followed by its byte representation.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[derive(Clone, Debug, Encode, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct OpaquePublicKeyBytes(Vec<u8>);

impl Decode for OpaquePublicKeyBytes {
Expand All @@ -83,16 +83,6 @@ impl Decode for OpaquePublicKeyBytes {
}
}

impl Encode for OpaquePublicKeyBytes {
fn encoded_len(&self) -> encoding::Result<usize> {
self.0.encoded_len()
}

fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
self.0.encode(writer)
}
}

impl AsRef<[u8]> for OpaquePublicKeyBytes {
fn as_ref(&self) -> &[u8] {
&self.0
Expand Down