-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ssh-cipher: length handling improvements
Adds an `Error::Length` variant and returns it whenever the input buffer for an encryption/decryption operation is not properly aligned to the cipher's block size. Also adds a private `Cipher::check_key_and_iv` method to validate keys/IVs are the expected size and return appropriate erros if they are not.
- Loading branch information
Showing
5 changed files
with
76 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,7 +92,7 @@ pub type Nonce = [u8; 12]; | |
/// `[email protected]`. | ||
pub type Tag = [u8; 16]; | ||
|
||
/// Counter mode with a 32-bit big endian counter. | ||
/// Counter mode with a 128-bit big endian counter. | ||
#[cfg(feature = "aes-ctr")] | ||
type Ctr128BE<Cipher> = ctr::CtrCore<Cipher, ctr::flavors::Ctr128BE>; | ||
|
||
|
@@ -221,6 +221,9 @@ impl Cipher { | |
} | ||
|
||
/// Decrypt the ciphertext in the `buffer` in-place using this cipher. | ||
/// | ||
/// Returns [`Error::Length`] in the event that `buffer` is not a multiple of the cipher's | ||
/// block size. | ||
#[cfg_attr( | ||
not(any(feature = "aes-cbc", feature = "aes-ctr", feature = "tdes")), | ||
allow(unused_variables) | ||
|
@@ -279,6 +282,9 @@ impl Cipher { | |
} | ||
|
||
/// Encrypt the ciphertext in the `buffer` in-place using this cipher. | ||
/// | ||
/// Returns [`Error::Length`] in the event that `buffer` is not a multiple of the cipher's | ||
/// block size. | ||
#[cfg_attr( | ||
not(any(feature = "aes-cbc", feature = "aes-ctr", feature = "tdes")), | ||
allow(unused_variables) | ||
|
@@ -330,6 +336,24 @@ impl Cipher { | |
Encryptor::new(self, key, iv) | ||
} | ||
|
||
/// Check that the key and IV are the expected length for this cipher. | ||
#[cfg(any(feature = "aes-cbc", feature = "aes-ctr", feature = "tdes"))] | ||
fn check_key_and_iv(self, key: &[u8], iv: &[u8]) -> Result<()> { | ||
let (key_size, iv_size) = self | ||
.key_and_iv_size() | ||
.ok_or(Error::UnsupportedCipher(self))?; | ||
|
||
if key.len() != key_size { | ||
return Err(Error::KeySize); | ||
} | ||
|
||
if iv.len() != iv_size { | ||
return Err(Error::IvSize); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Create an unsupported cipher error. | ||
fn unsupported(self) -> Error { | ||
Error::UnsupportedCipher(self) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,12 +115,16 @@ impl Kdf { | |
self.derive(password, &mut okm)?; | ||
let mut iv = okm.split_off(key_size); | ||
|
||
// For whatever reason `[email protected]` uses a slightly different key/nonce | ||
// derivation than the other algorithms. | ||
if cipher == Cipher::ChaCha20Poly1305 { | ||
// Only use the first ChaCha20 key. | ||
// Only use the first ChaCha20 key. As noted in the comment above, the derived key is | ||
// partitioned into `K_1` and `K_2`, and for the purposes of private key encryption | ||
// we only use `K_2` (which, perhaps confusingly, is the first of the two keys). | ||
okm.truncate(32); | ||
|
||
// Use an all-zero nonce (with a key derived from password + salt providing uniqueness) | ||
iv.extend_from_slice(&cipher::Nonce::default()); | ||
iv = cipher::Nonce::default().to_vec(); | ||
} | ||
|
||
Ok((okm, iv)) | ||
|