Skip to content

Fixes #532 Add aes_decrypt_on_chain function for unified AES decryption #852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion fastcrypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ name = "hash"
harness = false

[features]
default = []
default = ["std"]
no_std = []

# Allow copying keys
copy_key = []
Expand Down
58 changes: 58 additions & 0 deletions fastcrypto/src/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,64 @@ where
}
}

/// Unified decryption interface for on-chain use.
///
/// # Parameters
/// - `key`: The AES key as a byte slice.
/// - `iv`: The initialization vector as a byte slice.
/// - `ciphertext`: The encrypted data.
/// - `mode`: The AES mode to use (e.g., "AES128_CTR", "AES256_CBC").
///
/// # Returns
/// - `Ok(Vec<u8>)`: The decrypted plaintext.
/// - `Err(FastCryptoError)`: If decryption fails.
///
/// # Example
/// ```
/// use fastcrypto::aes::aes_decrypt_on_chain;
///
/// let key = [0u8; 16]; // Example key
/// let iv = [0u8; 16]; // Example IV
/// let ciphertext = vec![0x1f, 0x2e, 0x3d]; // Example ciphertext
///
/// let plaintext = aes_decrypt_on_chain(&key, &iv, &ciphertext, "AES128_CTR").unwrap();
/// ```

pub fn aes_decrypt_on_chain(
key: &[u8],
iv: &[u8],
ciphertext: &[u8],
mode: &str,
) -> Result<Vec<u8>, FastCryptoError> {
match mode {
"AES128_CTR" => {
let aes_key = AesKey::<typenum::U16>::from_bytes(key)?;
let iv = InitializationVector::<typenum::U16>::from_bytes(iv)?;
let cipher = Aes128Ctr::new(aes_key);
cipher.decrypt(&iv, ciphertext)
}
"AES256_CTR" => {
let aes_key = AesKey::<typenum::U32>::from_bytes(key)?;
let iv = InitializationVector::<typenum::U16>::from_bytes(iv)?;
let cipher = Aes256Ctr::new(aes_key);
cipher.decrypt(&iv, ciphertext)
}
"AES128_CBC" => {
let aes_key = AesKey::<typenum::U16>::from_bytes(key)?;
let iv = InitializationVector::<typenum::U16>::from_bytes(iv)?;
let cipher = Aes128CbcPkcs7::new(aes_key);
cipher.decrypt(&iv, ciphertext)
}
"AES256_CBC" => {
let aes_key = AesKey::<typenum::U32>::from_bytes(key)?;
let iv = InitializationVector::<typenum::U16>::from_bytes(iv)?;
let cipher = Aes256CbcPkcs7::new(aes_key);
cipher.decrypt(&iv, ciphertext)
}
_ => Err(FastCryptoError::InvalidInput),
}
}

/// AES128 in CBC-mode using PKCS #7 padding.
pub type Aes128CbcPkcs7 = AesCbc<aes::Aes128, aes::cipher::block_padding::Pkcs7>;

Expand Down
8 changes: 8 additions & 0 deletions fastcrypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
rust_2021_compatibility
)]

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(not(feature = "std"))]
use alloc::{vec, vec::Vec};

#[cfg(test)]
#[path = "tests/ed25519_tests.rs"]
pub mod ed25519_tests;
Expand Down
31 changes: 31 additions & 0 deletions fastcrypto/src/tests/aes_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,34 @@ fn test_sk_zeroization_on_drop() {
}
}
}

#[test]
fn test_aes_decrypt_on_chain_valid() {
let plaintext = b"Hello, on-chain!";
let key = AesKey::<typenum::U32>::generate(&mut thread_rng());
let iv = InitializationVector::<typenum::U16>::generate(&mut thread_rng());

let cipher = Aes256Ctr::new(key.clone());
let ciphertext = cipher.encrypt(&iv, plaintext);

let decrypted = aes_decrypt_on_chain(
&key.as_bytes(),
&iv.as_bytes(),
&ciphertext,
"AES256_CTR",
)
.unwrap();

assert_eq!(decrypted, plaintext);
}

#[test]
fn test_aes_decrypt_on_chain_invalid_mode() {
let key = [0u8; 16];
let iv = [0u8; 16];
let ciphertext = vec![0x1f, 0x2e, 0x3d];

let result = aes_decrypt_on_chain(&key, &iv, &ciphertext, "INVALID_MODE");

assert!(result.is_err());
}