Skip to content

Commit

Permalink
Rename the encrypt_with_iv method so it's clear that it also can de…
Browse files Browse the repository at this point in the history
…crypt
  • Loading branch information
poljar committed Oct 10, 2023
1 parent 372e0d3 commit c2bb760
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions crates/matrix-sdk-crypto/src/ciphers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,23 +149,26 @@ impl AesHmacSha2Key {
/// create a authentication tag.
pub(crate) fn encrypt(&self, plaintext: Vec<u8>) -> (Vec<u8>, [u8; IV_SIZE]) {
let initialization_vector = Self::generate_iv();
let ciphertext = self.encrypt_with_iv(plaintext, &initialization_vector);
let ciphertext = self.apply_keystream(plaintext, &initialization_vector);

(ciphertext, initialization_vector)
}

/// Encrypt the given plaintext using the specified initialization vector
/// and return the ciphertext.
/// Apply the keystream to the data stream, producing either the plaintext
/// or the ciphertext depending on whether the data stream is the ciphertext
/// or the plaintext, respectively.
///
/// ⚠️ This method is a low-level cryptographic primitive.
///
/// You *must* ensure that the initialization vector is unique across all
/// calls to this method for a given key.
/// If this method is encrypting a plaintext, you *must* ensure that the
/// initialization vector is unique across all calls to this method for
/// a given key.
///
/// This method does not provide authenticity. You *must* call the
/// [`AesHmacSha2Key::create_mac_tag()`] method after the encryption step to
/// create a authentication tag.
pub(crate) fn encrypt_with_iv(
/// create a authentication tag or the [`AesHmacSha2Key::verify_mac()`]
/// method before decrypting.
pub(crate) fn apply_keystream(
&self,
mut plaintext: Vec<u8>,
initialization_vector: &[u8; IV_SIZE],
Expand Down Expand Up @@ -224,7 +227,7 @@ impl AesHmacSha2Key {
ciphertext: Vec<u8>,
initialization_vector: &[u8; IV_SIZE],
) -> Vec<u8> {
self.encrypt_with_iv(ciphertext, initialization_vector)
self.apply_keystream(ciphertext, initialization_vector)
}

fn split_keys(
Expand Down

0 comments on commit c2bb760

Please sign in to comment.