#[non_exhaustive]pub enum Cipher {
None,
Aes128Cbc,
Aes192Cbc,
Aes256Cbc,
Aes128Ctr,
Aes192Ctr,
Aes256Ctr,
Aes128Gcm,
Aes256Gcm,
ChaCha20Poly1305,
TdesCbc,
}Expand description
Cipher algorithms.
A “cipher” within the scope of SSH was originally described in RFC4253 § 6.3 as a part of of the packet encryption protocol, where it refers to the combination of a symmetric block cipher, such as AES or 3DES, with a particular mode of operation, such as CBC or CTR.
This has been subsequently expanded by other standards documents, and now includes modern authenticated or “AEAD” modes such as AES-GCM and ChaCha20Poly1305, which we recommend and are marked with a ✅ in the table below.
Below is a table of the ciphers we support and what standards document defines them, along with which crate feature needs to be enabled to perform encryption with a given algorithm:
| Cipher name | Feature | AEAD | Algorithm | Standard |
|---|---|---|---|---|
3des-cbc | tdes | ⛔ | 3DES-CBC | RFC4253 § 6.3 |
aes128‑cbc | aes | ⛔ | AES-128-CBC | RFC4253 § 6.3 |
aes192‑cbc | aes | ⛔ | AES-192-CBC | RFC4253 § 6.3 |
aes256‑cbc | aes | ⛔ | AES-256-CBC | RFC4253 § 6.3 |
aes128‑ctr | aes | ⛔ | AES-128-CTR | RFC4344 |
aes192‑ctr | aes | ⛔ | AES-192-CTR | RFC4344 |
aes256‑ctr | aes | ⛔ | AES-256-CTR | RFC4344 |
aes128‑gcm@openssh.com | aes | ✅ | AES-128-GCM | RFC5647 |
aes256‑gcm@openssh.com | aes | ✅ | AES-256-GCM | RFC5647 |
chacha20‑poly1305@openssh.com | chacha20poly1305 | ✅ | ChaCha20Poly1305† | PROTOCOL.chacha20poly1305 |
† The construction called “ChaCha20Poly1305” as used by OpenSSH is different from other
constructions with that name including the one defined in RFC8439 and the one found in NaCl
variants like libsodium. See ChaCha20Poly1305 for more information.
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
None
none: no cipher.
Aes128Cbc
aes128-cbc: AES-128 in cipher block chaining (CBC) mode.
Aes192Cbc
aes192-cbc: AES-192 in cipher block chaining (CBC) mode.
Aes256Cbc
aes256-cbc: AES-256 in cipher block chaining (CBC) mode.
Aes128Ctr
aes128-ctr: AES-128 in counter (CTR) mode.
Aes192Ctr
aes192-ctr: AES-192 in counter (CTR) mode.
Aes256Ctr
aes256-ctr: AES-256 in counter (CTR) mode.
Aes128Gcm
aes128-gcm@openssh.com: AES-128 in Galois/Counter Mode (GCM).
Aes256Gcm
aes256-gcm@openssh.com: AES-256 in Galois/Counter Mode (GCM).
ChaCha20Poly1305
chacha20-poly1305@openssh.com: ChaCha20-Poly1305
TdesCbc
3des-cbc: TripleDES in block chaining (CBC) mode
Implementations§
Source§impl Cipher
impl Cipher
Sourcepub fn new(ciphername: &str) -> Result<Self, LabelError>
pub fn new(ciphername: &str) -> Result<Self, LabelError>
Decode cipher algorithm from the given ciphername.
§Supported cipher names
aes128-cbcaes192-cbcaes256-cbcaes128-ctraes192-ctraes256-ctraes128-gcm@openssh.comaes256-gcm@openssh.comchacha20-poly1305@openssh.com3des-cbc
§Errors
Returns LabelError if the provided ciphername is unknown.
Sourcepub fn as_str(self) -> &'static str
pub fn as_str(self) -> &'static str
Get the string identifier which corresponds to this algorithm.
Sourcepub fn key_and_iv_size(self) -> Option<(usize, usize)>
pub fn key_and_iv_size(self) -> Option<(usize, usize)>
Get the key and IV size for this cipher in bytes.
Sourcepub fn block_size(self) -> usize
pub fn block_size(self) -> usize
Get the block size for this cipher in bytes.
Sourcepub fn padding_len(self, input_size: usize) -> usize
pub fn padding_len(self, input_size: usize) -> usize
Compute the length of padding necessary to pad the given input to the block size.
Sourcepub fn has_tag(self) -> bool
pub fn has_tag(self) -> bool
Does this cipher have an authentication tag? (i.e. is it an AEAD mode?)
Sourcepub fn decrypt(
self,
key: &[u8],
iv: &[u8],
buffer: &mut [u8],
tag: Option<Tag>,
) -> Result<()>
pub fn decrypt( self, key: &[u8], iv: &[u8], buffer: &mut [u8], tag: Option<Tag>, ) -> Result<()>
Decrypt the ciphertext in the buffer in-place using this cipher.
§Errors
Returns Error::Length in the event that buffer is not a multiple of the cipher’s
block size.
Sourcepub fn decryptor<C>(self, key: &[u8], iv: &[u8]) -> Result<Decryptor<C>>where
C: BlockCipher,
Available on crate features aes or tdes only.
pub fn decryptor<C>(self, key: &[u8], iv: &[u8]) -> Result<Decryptor<C>>where
C: BlockCipher,
aes or tdes only.Get a stateful block_cipher::Decryptor for the given key and IV.
Only applicable to unauthenticated modes (e.g. AES-CBC, AES-CTR). Not usable with authenticated modes which are inherently one-shot (AES-GCM, ChaCha20Poly1305).
§Errors
Propagates errors from block_cipher::Decryptor::new.
Sourcepub fn encrypt(
self,
key: &[u8],
iv: &[u8],
buffer: &mut [u8],
) -> Result<Option<Tag>>
pub fn encrypt( self, key: &[u8], iv: &[u8], buffer: &mut [u8], ) -> Result<Option<Tag>>
Encrypt the ciphertext in the buffer in-place using this cipher.
§Errors
Returns Error::Length in the event that buffer is not a multiple of the cipher’s
block size.
Sourcepub fn encryptor<C>(self, key: &[u8], iv: &[u8]) -> Result<Encryptor<C>>where
C: BlockCipher,
Available on crate features aes or tdes only.
pub fn encryptor<C>(self, key: &[u8], iv: &[u8]) -> Result<Encryptor<C>>where
C: BlockCipher,
aes or tdes only.Get a stateful block_cipher::Encryptor for the given key and IV.
Only applicable to unauthenticated modes (e.g. AES-CBC, AES-CTR). Not usable with authenticated modes which are inherently one-shot (AES-GCM, ChaCha20Poly1305).
§Errors
Propagates errors from block_cipher::Encryptor::new.
Trait Implementations§
Source§impl Ord for Cipher
impl Ord for Cipher
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Cipher
impl PartialOrd for Cipher
impl Copy for Cipher
impl Eq for Cipher
impl Label for Cipher
impl StructuralPartialEq for Cipher
Auto Trait Implementations§
impl Freeze for Cipher
impl RefUnwindSafe for Cipher
impl Send for Cipher
impl Sync for Cipher
impl Unpin for Cipher
impl UnsafeUnpin for Cipher
impl UnwindSafe for Cipher
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Encode for Twhere
T: Label,
impl<T> Encode for Twhere
T: Label,
Source§fn encoded_len(&self) -> Result<usize, Error>
fn encoded_len(&self) -> Result<usize, Error>
Source§fn encoded_len_prefixed(&self) -> Result<usize, Error>
fn encoded_len_prefixed(&self) -> Result<usize, Error>
uint32 length prefix. Read more