Skip to content

Commit 77cc2f7

Browse files
committed
algorithm names QoL changes
1 parent 1615319 commit 77cc2f7

File tree

7 files changed

+175
-38
lines changed

7 files changed

+175
-38
lines changed

russh-keys/src/key.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ pub const NONE: Name = Name("none");
5656

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

59+
pub static ALL_KEY_TYPES: &[&Name] = &[
60+
&NONE,
61+
&SSH_RSA,
62+
&RSA_SHA2_256,
63+
&RSA_SHA2_512,
64+
&ECDSA_SHA2_NISTP256,
65+
&ECDSA_SHA2_NISTP384,
66+
&ECDSA_SHA2_NISTP521,
67+
];
68+
5969
impl Name {
6070
/// Base name of the private key file for a key name.
6171
pub fn identity_file(&self) -> &'static str {
@@ -69,6 +79,17 @@ impl Name {
6979
}
7080
}
7181

82+
impl TryFrom<&str> for Name {
83+
type Error = ();
84+
fn try_from(s: &str) -> Result<Name, ()> {
85+
ALL_KEY_TYPES
86+
.iter()
87+
.find(|x| x.0 == s)
88+
.map(|x| **x)
89+
.ok_or(())
90+
}
91+
}
92+
7293
#[doc(hidden)]
7394
pub trait Verify {
7495
fn verify_client_auth(&self, buffer: &[u8], sig: &[u8]) -> bool;

russh/src/cipher/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

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

102+
pub static ALL_CIPHERS: &[&Name] = &[
103+
&CLEAR,
104+
&NONE,
105+
&AES_128_CTR,
106+
&AES_192_CTR,
107+
&AES_256_CTR,
108+
&AES_256_GCM,
109+
&AES_128_CBC,
110+
&AES_192_CBC,
111+
&AES_256_CBC,
112+
&CHACHA20_POLY1305,
113+
];
114+
100115
pub(crate) static CIPHERS: Lazy<HashMap<&'static Name, &(dyn Cipher + Send + Sync)>> =
101116
Lazy::new(|| {
102117
let mut h: HashMap<&'static Name, &(dyn Cipher + Send + Sync)> = HashMap::new();
@@ -110,6 +125,7 @@ pub(crate) static CIPHERS: Lazy<HashMap<&'static Name, &(dyn Cipher + Send + Syn
110125
h.insert(&AES_192_CBC, &_AES_192_CBC);
111126
h.insert(&AES_256_CBC, &_AES_256_CBC);
112127
h.insert(&CHACHA20_POLY1305, &_CHACHA20_POLY1305);
128+
assert_eq!(h.len(), ALL_CIPHERS.len());
113129
h
114130
});
115131

@@ -121,6 +137,19 @@ impl AsRef<str> for Name {
121137
}
122138
}
123139

140+
impl Borrow<str> for &Name {
141+
fn borrow(&self) -> &str {
142+
self.0
143+
}
144+
}
145+
146+
impl TryFrom<&str> for Name {
147+
type Error = ();
148+
fn try_from(s: &str) -> Result<Name, ()> {
149+
CIPHERS.keys().find(|x| x.0 == s).map(|x| **x).ok_or(())
150+
}
151+
}
152+
124153
pub(crate) struct CipherPair {
125154
pub local_to_remote: Box<dyn SealingKey + Send>,
126155
pub remote_to_local: Box<dyn OpeningKey + Send>,

russh/src/compression.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::convert::TryFrom;
2+
13
#[derive(Debug, Clone)]
24
pub enum Compression {
35
None,
@@ -19,10 +21,43 @@ pub enum Decompress {
1921
Zlib(flate2::Decompress),
2022
}
2123

24+
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
25+
pub struct Name(&'static str);
26+
impl AsRef<str> for Name {
27+
fn as_ref(&self) -> &str {
28+
self.0
29+
}
30+
}
31+
32+
impl TryFrom<&str> for Name {
33+
type Error = ();
34+
fn try_from(s: &str) -> Result<Name, ()> {
35+
ALL_COMPRESSION_ALGORITHMS
36+
.iter()
37+
.find(|x| x.0 == s)
38+
.map(|x| **x)
39+
.ok_or(())
40+
}
41+
}
42+
43+
pub const NONE: Name = Name("none");
44+
#[cfg(feature = "flate2")]
45+
pub const ZLIB: Name = Name("zlib");
46+
#[cfg(feature = "flate2")]
47+
pub const ZLIB_LEGACY: Name = Name("zlib@openssh.com");
48+
49+
pub const ALL_COMPRESSION_ALGORITHMS: &[&Name] = &[
50+
&NONE,
51+
#[cfg(feature = "flate2")]
52+
&ZLIB,
53+
#[cfg(feature = "flate2")]
54+
&ZLIB_LEGACY,
55+
];
56+
2257
#[cfg(feature = "flate2")]
2358
impl Compression {
24-
pub fn from_string(s: &str) -> Self {
25-
if s == "zlib" || s == "zlib@openssh.com" {
59+
pub fn new(name: &Name) -> Self {
60+
if name == &ZLIB || name == &ZLIB_LEGACY {
2661
Compression::Zlib
2762
} else {
2863
Compression::None
@@ -56,7 +91,7 @@ impl Compression {
5691

5792
#[cfg(not(feature = "flate2"))]
5893
impl Compression {
59-
pub fn from_string(_: &str) -> Self {
94+
pub fn new(_name: &Name) -> Self {
6095
Compression::None
6196
}
6297

russh/src/kex/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ mod ecdh_nistp;
2121
mod none;
2222
use std::cell::RefCell;
2323
use std::collections::HashMap;
24+
use std::convert::TryFrom;
2425
use std::fmt::Debug;
2526

2627
use curve25519::Curve25519KexType;
@@ -87,6 +88,13 @@ impl AsRef<str> for Name {
8788
}
8889
}
8990

91+
impl TryFrom<&str> for Name {
92+
type Error = ();
93+
fn try_from(s: &str) -> Result<Name, ()> {
94+
KEXES.keys().find(|x| x.0 == s).map(|x| **x).ok_or(())
95+
}
96+
}
97+
9098
/// `curve25519-sha256`
9199
pub const CURVE25519: Name = Name("curve25519-sha256");
92100
/// `curve25519-sha256@libssh.org`
@@ -126,6 +134,19 @@ const _ECDH_SHA2_NISTP384: EcdhNistP384KexType = EcdhNistP384KexType {};
126134
const _ECDH_SHA2_NISTP521: EcdhNistP521KexType = EcdhNistP521KexType {};
127135
const _NONE: none::NoneKexType = none::NoneKexType {};
128136

137+
pub const ALL_KEX_ALGORITHMS: &[&Name] = &[
138+
&CURVE25519,
139+
&CURVE25519_PRE_RFC_8731,
140+
&DH_G1_SHA1,
141+
&DH_G14_SHA1,
142+
&DH_G14_SHA256,
143+
&DH_G16_SHA512,
144+
&ECDH_SHA2_NISTP256,
145+
&ECDH_SHA2_NISTP384,
146+
&ECDH_SHA2_NISTP521,
147+
&NONE,
148+
];
149+
129150
pub(crate) static KEXES: Lazy<HashMap<&'static Name, &(dyn KexType + Send + Sync)>> =
130151
Lazy::new(|| {
131152
let mut h: HashMap<&'static Name, &(dyn KexType + Send + Sync)> = HashMap::new();
@@ -139,6 +160,7 @@ pub(crate) static KEXES: Lazy<HashMap<&'static Name, &(dyn KexType + Send + Sync
139160
h.insert(&ECDH_SHA2_NISTP384, &_ECDH_SHA2_NISTP384);
140161
h.insert(&ECDH_SHA2_NISTP521, &_ECDH_SHA2_NISTP521);
141162
h.insert(&NONE, &_NONE);
163+
assert_eq!(ALL_KEX_ALGORITHMS.len(), h.len());
142164
h
143165
});
144166

russh/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ pub mod cipher;
108108
pub mod kex;
109109
/// MAC algorithm names
110110
pub mod mac;
111+
/// Compression algorithm names
112+
pub mod compression;
111113

112114
mod cert;
113-
mod compression;
114115
mod key;
115116
mod msg;
116117
mod negotiation;

russh/src/mac/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//!
1515
//! This module exports cipher names for use with [Preferred].
1616
use std::collections::HashMap;
17+
use std::convert::TryFrom;
1718
use std::marker::PhantomData;
1819

1920
use digest::typenum::{U20, U32, U64};
@@ -52,6 +53,13 @@ impl AsRef<str> for Name {
5253
}
5354
}
5455

56+
impl TryFrom<&str> for Name {
57+
type Error = ();
58+
fn try_from(s: &str) -> Result<Name, ()> {
59+
MACS.keys().find(|x| x.0 == s).map(|x| **x).ok_or(())
60+
}
61+
}
62+
5563
/// `none`
5664
pub const NONE: Name = Name("none");
5765
/// `hmac-sha1`
@@ -81,6 +89,16 @@ static _HMAC_SHA256_ETM: CryptoEtmMacAlgorithm<Hmac<Sha256>, U32> =
8189
static _HMAC_SHA512_ETM: CryptoEtmMacAlgorithm<Hmac<Sha512>, U64> =
8290
CryptoEtmMacAlgorithm(PhantomData, PhantomData);
8391

92+
pub const ALL_MAC_ALGORITHMS: &[&Name] = &[
93+
&NONE,
94+
&HMAC_SHA1,
95+
&HMAC_SHA256,
96+
&HMAC_SHA512,
97+
&HMAC_SHA1_ETM,
98+
&HMAC_SHA256_ETM,
99+
&HMAC_SHA512_ETM,
100+
];
101+
84102
pub(crate) static MACS: Lazy<HashMap<&'static Name, &(dyn MacAlgorithm + Send + Sync)>> =
85103
Lazy::new(|| {
86104
let mut h: HashMap<&'static Name, &(dyn MacAlgorithm + Send + Sync)> = HashMap::new();
@@ -91,5 +109,6 @@ pub(crate) static MACS: Lazy<HashMap<&'static Name, &(dyn MacAlgorithm + Send +
91109
h.insert(&HMAC_SHA1_ETM, &_HMAC_SHA1_ETM);
92110
h.insert(&HMAC_SHA256_ETM, &_HMAC_SHA256_ETM);
93111
h.insert(&HMAC_SHA512_ETM, &_HMAC_SHA512_ETM);
112+
assert_eq!(h.len(), ALL_MAC_ALGORITHMS.len());
94113
h
95114
});

0 commit comments

Comments
 (0)