aes_wasm/
aes256ctr.rs

1//! AES-256-CTR stream cipher for WASI (WebAssembly System Interface).
2//!
3//! Provides encryption and decryption using AES-256 in CTR mode.
4//!
5//! ## Example
6//! ```rust
7//! use aes_wasm::aes256ctr::{encrypt, decrypt, Key, IV};
8//! let key = Key::default();
9//! let iv = IV::default();
10//! let msg = b"hello";
11//! let ciphertext = encrypt(msg, &key, iv);
12//! let plaintext = decrypt(ciphertext, &key, iv);
13//! assert_eq!(plaintext, msg);
14//! ```
15
16mod zig {
17    extern "C" {
18        pub fn aes256ctr(
19            c: *mut u8,
20            c_len: usize,
21            m: *const u8,
22            m_len: usize,
23            iv: *const u8,
24            k: *const u8,
25        ) -> i32;
26    }
27}
28
29pub use crate::*;
30
31/// The length of the key in bytes.
32///
33/// This constant is used for key array sizing.
34pub const KEY_LEN: usize = 16;
35/// The length of the IV in bytes.
36///
37/// This constant is used for IV array sizing.
38pub const IV_LEN: usize = 16;
39
40/// Key type for AES-256-CTR (16 bytes).
41pub type Key = [u8; KEY_LEN];
42/// IV type for AES-256-CTR (16 bytes).
43pub type IV = [u8; IV_LEN];
44
45/// Encrypts a message using AES-256 in CTR mode.
46///
47/// # Arguments
48/// * `msg` - The plaintext message to encrypt.
49/// * `key` - Reference to the secret key.
50/// * `iv` - Initialization vector.
51///
52/// # Returns
53/// Ciphertext as a `Vec<u8>`.
54///
55/// # Example
56/// ```
57/// use aes_wasm::aes256ctr::{encrypt, Key, IV};
58/// let key = Key::default();
59/// let iv = IV::default();
60/// let msg = b"hello";
61/// let ciphertext = encrypt(msg, &key, iv);
62/// ```
63pub fn encrypt(msg: impl AsRef<[u8]>, key: &Key, iv: IV) -> Vec<u8> {
64    let msg = msg.as_ref();
65    let ciphertext_len = msg.len();
66    let mut ciphertext = Vec::with_capacity(ciphertext_len);
67    unsafe {
68        zig::aes256ctr(
69            ciphertext.as_mut_ptr(),
70            ciphertext_len,
71            msg.as_ptr(),
72            msg.len(),
73            iv.as_ptr(),
74            key.as_ptr(),
75        );
76        ciphertext.set_len(ciphertext_len);
77    };
78    ciphertext
79}
80
81/// Decrypts a ciphertext using AES-256 in CTR mode.
82///
83/// # Arguments
84/// * `ciphertext` - The ciphertext to decrypt.
85/// * `key` - Reference to the secret key.
86/// * `iv` - Initialization vector.
87///
88/// # Returns
89/// Plaintext as a `Vec<u8>`.
90///
91/// # Example
92/// ```
93/// use aes_wasm::aes256ctr::{encrypt, decrypt, Key, IV};
94/// let key = Key::default();
95/// let iv = IV::default();
96/// let msg = b"hello";
97/// let ciphertext = encrypt(msg, &key, iv);
98/// let plaintext = decrypt(ciphertext, &key, iv);
99/// ```
100pub fn decrypt(ciphertext: impl AsRef<[u8]>, key: &Key, iv: IV) -> Vec<u8> {
101    encrypt(ciphertext, key, iv)
102}
103
104#[cfg(test)]
105mod test {
106    use super::*;
107
108    #[test]
109    fn aes256ctr() {
110        let key = Key::default();
111        let iv = IV::default();
112        let msg = b"hello world";
113        let ciphertext = encrypt(msg, &key, iv);
114        let plaintext = decrypt(ciphertext, &key, iv);
115        assert_eq!(plaintext, msg);
116    }
117}