Skip to content

Commit

Permalink
Merge pull request #13 from ported-pw/decrypt-without-hmac
Browse files Browse the repository at this point in the history
Allow symmetric decrypt without hmac
  • Loading branch information
icewind1991 authored Sep 22, 2024
2 parents a6fb924 + 11f2d84 commit 4e81f90
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,27 @@ pub fn symmetric_encrypt(input: BytesMut, key: &[u8; 32]) -> BytesMut {
symmetric_encrypt_with_iv_buffer(BytesMut::from(&[0; 16][..]), input, key)
}

/// Decrypt the IV stored in the first 16 bytes of `input`
/// and use it to decrypt the remaining bytes.
pub fn symmetric_decrypt(mut input: BytesMut, key: &[u8; 32]) -> Result<BytesMut> {
fn symmetric_decrypt_impl(mut input: BytesMut, key: &[u8; 32]) -> Result<([u8; 16], BytesMut)> {
let message = input.split_off(16);
let encrypted_iv = input.as_ref().try_into().unwrap();
let plain_iv = decrypt_iv(encrypted_iv, key);

let message = decrypt_message(message, key, &plain_iv)?;

Ok((plain_iv, message))
}

/// Decrypt the IV stored in the first 16 bytes of `input`
/// and use it to decrypt the remaining bytes, skipping HMAC validation.
pub fn symmetric_decrypt_without_hmac(input: BytesMut, key: &[u8; 32]) -> Result<BytesMut> {
let (_, message) = symmetric_decrypt_impl(input, key)?;
Ok(message)
}

/// Decrypt the IV stored in the first 16 bytes of `input`
/// and use it to decrypt the remaining bytes.
pub fn symmetric_decrypt(input: BytesMut, key: &[u8; 32]) -> Result<BytesMut> {
let (plain_iv, message) = symmetric_decrypt_impl(input, key)?;
// let padding = *message.last().unwrap();
// message.resize(message.len() - padding as usize, 0);

Expand All @@ -240,7 +253,6 @@ pub fn symmetric_decrypt(mut input: BytesMut, key: &[u8; 32]) -> Result<BytesMut
if hmac[0..13] != plain_iv[0..13] {
return Err(CryptError::InvalidHmac);
}

Ok(message)
}

Expand Down

0 comments on commit 4e81f90

Please sign in to comment.