Skip to content

Commit

Permalink
algorithm names QoL changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugeny committed Jul 9, 2024
1 parent 1615319 commit 77cc2f7
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 38 deletions.
21 changes: 21 additions & 0 deletions russh-keys/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ pub const NONE: Name = Name("none");

pub const SSH_RSA: Name = Name("ssh-rsa");

pub static ALL_KEY_TYPES: &[&Name] = &[
&NONE,
&SSH_RSA,
&RSA_SHA2_256,
&RSA_SHA2_512,
&ECDSA_SHA2_NISTP256,
&ECDSA_SHA2_NISTP384,
&ECDSA_SHA2_NISTP521,
];

impl Name {
/// Base name of the private key file for a key name.
pub fn identity_file(&self) -> &'static str {
Expand All @@ -69,6 +79,17 @@ impl Name {
}
}

impl TryFrom<&str> for Name {
type Error = ();
fn try_from(s: &str) -> Result<Name, ()> {
ALL_KEY_TYPES
.iter()
.find(|x| x.0 == s)
.map(|x| **x)
.ok_or(())
}
}

#[doc(hidden)]
pub trait Verify {
fn verify_client_auth(&self, buffer: &[u8], sig: &[u8]) -> bool;
Expand Down
29 changes: 29 additions & 0 deletions russh/src/cipher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

//!
//! This module exports cipher names for use with [Preferred].
use std::borrow::Borrow;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::fmt::Debug;
use std::marker::PhantomData;
use std::num::Wrapping;
Expand Down Expand Up @@ -97,6 +99,19 @@ static _AES_192_CBC: SshBlockCipher<CbcWrapper<Aes192>> = SshBlockCipher(Phantom
static _AES_256_CBC: SshBlockCipher<CbcWrapper<Aes256>> = SshBlockCipher(PhantomData);
static _CHACHA20_POLY1305: SshChacha20Poly1305Cipher = SshChacha20Poly1305Cipher {};

pub static ALL_CIPHERS: &[&Name] = &[
&CLEAR,
&NONE,
&AES_128_CTR,
&AES_192_CTR,
&AES_256_CTR,
&AES_256_GCM,
&AES_128_CBC,
&AES_192_CBC,
&AES_256_CBC,
&CHACHA20_POLY1305,
];

pub(crate) static CIPHERS: Lazy<HashMap<&'static Name, &(dyn Cipher + Send + Sync)>> =
Lazy::new(|| {
let mut h: HashMap<&'static Name, &(dyn Cipher + Send + Sync)> = HashMap::new();
Expand All @@ -110,6 +125,7 @@ pub(crate) static CIPHERS: Lazy<HashMap<&'static Name, &(dyn Cipher + Send + Syn
h.insert(&AES_192_CBC, &_AES_192_CBC);
h.insert(&AES_256_CBC, &_AES_256_CBC);
h.insert(&CHACHA20_POLY1305, &_CHACHA20_POLY1305);
assert_eq!(h.len(), ALL_CIPHERS.len());
h
});

Expand All @@ -121,6 +137,19 @@ impl AsRef<str> for Name {
}
}

impl Borrow<str> for &Name {
fn borrow(&self) -> &str {
self.0
}
}

impl TryFrom<&str> for Name {
type Error = ();
fn try_from(s: &str) -> Result<Name, ()> {
CIPHERS.keys().find(|x| x.0 == s).map(|x| **x).ok_or(())
}
}

pub(crate) struct CipherPair {
pub local_to_remote: Box<dyn SealingKey + Send>,
pub remote_to_local: Box<dyn OpeningKey + Send>,
Expand Down
41 changes: 38 additions & 3 deletions russh/src/compression.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::convert::TryFrom;

#[derive(Debug, Clone)]
pub enum Compression {
None,
Expand All @@ -19,10 +21,43 @@ pub enum Decompress {
Zlib(flate2::Decompress),
}

#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
pub struct Name(&'static str);
impl AsRef<str> for Name {
fn as_ref(&self) -> &str {
self.0
}
}

impl TryFrom<&str> for Name {
type Error = ();
fn try_from(s: &str) -> Result<Name, ()> {
ALL_COMPRESSION_ALGORITHMS
.iter()
.find(|x| x.0 == s)
.map(|x| **x)
.ok_or(())
}
}

pub const NONE: Name = Name("none");
#[cfg(feature = "flate2")]
pub const ZLIB: Name = Name("zlib");
#[cfg(feature = "flate2")]
pub const ZLIB_LEGACY: Name = Name("[email protected]");

pub const ALL_COMPRESSION_ALGORITHMS: &[&Name] = &[
&NONE,
#[cfg(feature = "flate2")]
&ZLIB,
#[cfg(feature = "flate2")]
&ZLIB_LEGACY,
];

#[cfg(feature = "flate2")]
impl Compression {
pub fn from_string(s: &str) -> Self {
if s == "zlib" || s == "[email protected]" {
pub fn new(name: &Name) -> Self {
if name == &ZLIB || name == &ZLIB_LEGACY {
Compression::Zlib
} else {
Compression::None
Expand Down Expand Up @@ -56,7 +91,7 @@ impl Compression {

#[cfg(not(feature = "flate2"))]
impl Compression {
pub fn from_string(_: &str) -> Self {
pub fn new(_name: &Name) -> Self {
Compression::None
}

Expand Down
22 changes: 22 additions & 0 deletions russh/src/kex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod ecdh_nistp;
mod none;
use std::cell::RefCell;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::fmt::Debug;

use curve25519::Curve25519KexType;
Expand Down Expand Up @@ -87,6 +88,13 @@ impl AsRef<str> for Name {
}
}

impl TryFrom<&str> for Name {
type Error = ();
fn try_from(s: &str) -> Result<Name, ()> {
KEXES.keys().find(|x| x.0 == s).map(|x| **x).ok_or(())
}
}

/// `curve25519-sha256`
pub const CURVE25519: Name = Name("curve25519-sha256");
/// `[email protected]`
Expand Down Expand Up @@ -126,6 +134,19 @@ const _ECDH_SHA2_NISTP384: EcdhNistP384KexType = EcdhNistP384KexType {};
const _ECDH_SHA2_NISTP521: EcdhNistP521KexType = EcdhNistP521KexType {};
const _NONE: none::NoneKexType = none::NoneKexType {};

pub const ALL_KEX_ALGORITHMS: &[&Name] = &[
&CURVE25519,
&CURVE25519_PRE_RFC_8731,
&DH_G1_SHA1,
&DH_G14_SHA1,
&DH_G14_SHA256,
&DH_G16_SHA512,
&ECDH_SHA2_NISTP256,
&ECDH_SHA2_NISTP384,
&ECDH_SHA2_NISTP521,
&NONE,
];

pub(crate) static KEXES: Lazy<HashMap<&'static Name, &(dyn KexType + Send + Sync)>> =
Lazy::new(|| {
let mut h: HashMap<&'static Name, &(dyn KexType + Send + Sync)> = HashMap::new();
Expand All @@ -139,6 +160,7 @@ pub(crate) static KEXES: Lazy<HashMap<&'static Name, &(dyn KexType + Send + Sync
h.insert(&ECDH_SHA2_NISTP384, &_ECDH_SHA2_NISTP384);
h.insert(&ECDH_SHA2_NISTP521, &_ECDH_SHA2_NISTP521);
h.insert(&NONE, &_NONE);
assert_eq!(ALL_KEX_ALGORITHMS.len(), h.len());
h
});

Expand Down
3 changes: 2 additions & 1 deletion russh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ pub mod cipher;
pub mod kex;
/// MAC algorithm names
pub mod mac;
/// Compression algorithm names
pub mod compression;

mod cert;
mod compression;
mod key;
mod msg;
mod negotiation;
Expand Down
19 changes: 19 additions & 0 deletions russh/src/mac/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//!
//! This module exports cipher names for use with [Preferred].
use std::collections::HashMap;
use std::convert::TryFrom;
use std::marker::PhantomData;

use digest::typenum::{U20, U32, U64};
Expand Down Expand Up @@ -52,6 +53,13 @@ impl AsRef<str> for Name {
}
}

impl TryFrom<&str> for Name {
type Error = ();
fn try_from(s: &str) -> Result<Name, ()> {
MACS.keys().find(|x| x.0 == s).map(|x| **x).ok_or(())
}
}

/// `none`
pub const NONE: Name = Name("none");
/// `hmac-sha1`
Expand Down Expand Up @@ -81,6 +89,16 @@ static _HMAC_SHA256_ETM: CryptoEtmMacAlgorithm<Hmac<Sha256>, U32> =
static _HMAC_SHA512_ETM: CryptoEtmMacAlgorithm<Hmac<Sha512>, U64> =
CryptoEtmMacAlgorithm(PhantomData, PhantomData);

pub const ALL_MAC_ALGORITHMS: &[&Name] = &[
&NONE,
&HMAC_SHA1,
&HMAC_SHA256,
&HMAC_SHA512,
&HMAC_SHA1_ETM,
&HMAC_SHA256_ETM,
&HMAC_SHA512_ETM,
];

pub(crate) static MACS: Lazy<HashMap<&'static Name, &(dyn MacAlgorithm + Send + Sync)>> =
Lazy::new(|| {
let mut h: HashMap<&'static Name, &(dyn MacAlgorithm + Send + Sync)> = HashMap::new();
Expand All @@ -91,5 +109,6 @@ pub(crate) static MACS: Lazy<HashMap<&'static Name, &(dyn MacAlgorithm + Send +
h.insert(&HMAC_SHA1_ETM, &_HMAC_SHA1_ETM);
h.insert(&HMAC_SHA256_ETM, &_HMAC_SHA256_ETM);
h.insert(&HMAC_SHA512_ETM, &_HMAC_SHA512_ETM);
assert_eq!(h.len(), ALL_MAC_ALGORITHMS.len());
h
});
Loading

0 comments on commit 77cc2f7

Please sign in to comment.