Skip to content

Commit

Permalink
Add aead_encrypt and aead_decrypt convenience functions (#155)
Browse files Browse the repository at this point in the history
* aead_encrypt and aead_decrypt

* Accept key slice as parameter

* Add .change file

* Add key length checks

* Add functions docs

* Fmt
  • Loading branch information
thibault-martinez authored Aug 19, 2022
1 parent e48a0e9 commit 1910a54
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changes/aead-helpers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"iota-crypto": minor
---

Add aead_encrypt and aead_decrypt convenience functions
68 changes: 66 additions & 2 deletions src/ciphers/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ macro_rules! impl_aead {
($impl:ident, $name:expr, $key_len:ident, $nonce_len:ident, $tag_len:ident) => {
impl $crate::ciphers::traits::Aead for $impl {
type KeyLength = $key_len;

type NonceLength = $nonce_len;

type TagLength = $tag_len;

const NAME: &'static str = $name;
Expand Down Expand Up @@ -71,5 +69,71 @@ macro_rules! impl_aead {
Ok(ciphertext.len())
}
}

/// A helper function to encrypt `plaintext` with `key`.
/// The return value is arbitrarily chosen as `nonce || tag || ciphertext` for historic reasons, mainly
/// compatibility with out wallet libraries. The nonce is randomly chosen.
pub fn aead_encrypt(key: &[u8], plaintext: &[u8]) -> crate::Result<Vec<u8>> {
if key.len() != <$impl as $crate::ciphers::traits::Aead>::KEY_LENGTH {
return Err($crate::Error::BufferSize {
name: "key",
needs: <$impl as $crate::ciphers::traits::Aead>::KEY_LENGTH,
has: key.len(),
});
}

let mut nonce = [0; <$impl as $crate::ciphers::traits::Aead>::NONCE_LENGTH];
let mut tag = vec![0; <$impl as $crate::ciphers::traits::Aead>::TAG_LENGTH];
let mut ciphertext = vec![0; plaintext.len()];

crate::utils::rand::fill(&mut nonce)?;

<$impl as $crate::ciphers::traits::Aead>::encrypt(
$crate::ciphers::traits::Key::<$impl>::from_slice(&key),
$crate::ciphers::traits::Nonce::<$impl>::from_slice(&nonce),
&[],
plaintext,
&mut ciphertext,
$crate::ciphers::traits::Tag::<$impl>::from_mut_slice(&mut tag),
)?;

let mut ret = nonce.to_vec();
ret.append(&mut tag);
ret.append(&mut ciphertext);

Ok(ret)
}

/// A helper function to decrypt `ciphertext` with `key`.
/// The input value is assumed to be `nonce || tag || ciphertext` for historic reason, mainly compatibility with
/// out wallet libraries.
pub fn aead_decrypt(key: &[u8], ciphertext: &[u8]) -> crate::Result<Vec<u8>> {
if key.len() != <$impl as $crate::ciphers::traits::Aead>::KEY_LENGTH {
return Err($crate::Error::BufferSize {
name: "key",
needs: <$impl as $crate::ciphers::traits::Aead>::KEY_LENGTH,
has: key.len(),
});
}

let nonce = &ciphertext[..<$impl as $crate::ciphers::traits::Aead>::NONCE_LENGTH];
let tag = &ciphertext[<$impl as $crate::ciphers::traits::Aead>::NONCE_LENGTH
..<$impl as $crate::ciphers::traits::Aead>::NONCE_LENGTH
+ <$impl as $crate::ciphers::traits::Aead>::TAG_LENGTH];
let ciphertext = &ciphertext[<$impl as $crate::ciphers::traits::Aead>::NONCE_LENGTH
+ <$impl as $crate::ciphers::traits::Aead>::TAG_LENGTH..];
let mut plaintext = vec![0u8; ciphertext.len()];

<$impl as $crate::ciphers::traits::Aead>::decrypt(
$crate::ciphers::traits::Key::<$impl>::from_slice(&key),
$crate::ciphers::traits::Nonce::<$impl>::from_slice(&nonce),
&[],
&mut plaintext,
ciphertext,
$crate::ciphers::traits::Tag::<$impl>::from_slice(&tag),
)?;

Ok(plaintext)
}
};
}

0 comments on commit 1910a54

Please sign in to comment.