1414
1515//!
1616//! This module exports cipher names for use with [Preferred].
17+ use std:: borrow:: Borrow ;
1718use std:: collections:: HashMap ;
19+ use std:: convert:: TryFrom ;
1820use std:: fmt:: Debug ;
1921use std:: marker:: PhantomData ;
2022use std:: num:: Wrapping ;
@@ -97,6 +99,19 @@ static _AES_192_CBC: SshBlockCipher<CbcWrapper<Aes192>> = SshBlockCipher(Phantom
9799static _AES_256_CBC: SshBlockCipher < CbcWrapper < Aes256 > > = SshBlockCipher ( PhantomData ) ;
98100static _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+
100115pub ( 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+
124153pub ( crate ) struct CipherPair {
125154 pub local_to_remote : Box < dyn SealingKey + Send > ,
126155 pub remote_to_local : Box < dyn OpeningKey + Send > ,
0 commit comments