aes_wasm/
lib.rs

1//! # aes-wasm
2//!
3//! High-performance AEAD, stream cipher, and MAC primitives for WebAssembly/WASI.
4//!
5//! This crate provides a simple, dependency-free API for cryptography in WASI environments.
6//!
7//! ## Example: AES-128-GCM
8//! ```rust
9//! use aes_wasm::aes128gcm::{encrypt, decrypt, Key, Nonce};
10//! let key = Key::default();
11//! let nonce = Nonce::default();
12//! let msg = b"hello world";
13//! let ad = b"extra data";
14//! let ciphertext = encrypt(msg, ad, &key, nonce);
15//! let plaintext = decrypt(ciphertext, ad, &key, nonce).unwrap();
16//! assert_eq!(plaintext, msg);
17//! ```
18//!
19//! AEAD ciphers for WebAssembly, including AEGIS, AES-GCM, AES-OCB, AES-CBC, AES-CTR, and CMAC.
20//!
21//! This crate provides high-performance AEAD and MAC primitives for use in WebAssembly environments.
22//! It exposes a simple API for encryption, decryption, and authentication using modern ciphers.
23//!
24//! # Example
25//! ```
26//! use aes_wasm::aes128gcm::{encrypt, decrypt, Key, Nonce};
27//! let key = Key::default();
28//! let nonce = Nonce::default();
29//! let msg = b"hello";
30//! let ad = b"ad";
31//! let ciphertext = encrypt(msg, ad, &key, nonce);
32//! let plaintext = decrypt(ciphertext, ad, &key, nonce).unwrap();
33//! assert_eq!(plaintext, msg);
34//! ```
35
36use core::fmt::{self, Display};
37
38/// Error type for AEAD operations.
39///
40/// This error is returned when authentication fails during decryption.
41///
42/// # Example
43/// ```
44/// use aes_wasm::aes128gcm::{decrypt, Key, Nonce};
45/// use aes_wasm::Error;
46/// let key = Key::default();
47/// let nonce = Nonce::default();
48/// let ad = b"ad";
49/// // Intentionally use invalid ciphertext
50/// let ciphertext = b"invalid";
51/// let result = decrypt(ciphertext, ad, &key, nonce);
52/// assert_eq!(result, Err(Error::VerificationFailed));
53/// ```
54#[derive(Debug, Copy, Clone, Eq, PartialEq)]
55pub enum Error {
56    /// Ciphertext verification failed.
57    VerificationFailed,
58}
59
60impl std::error::Error for Error {}
61
62impl Display for Error {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        match self {
65            Error::VerificationFailed => write!(f, "Verification failed"),
66        }
67    }
68}
69
70pub mod aegis128l;
71pub mod aegis128x2;
72pub mod aegis128x4;
73pub mod aegis256;
74pub mod aegis256x2;
75pub mod aegis256x4;
76pub mod aes128cbc;
77pub mod aes128ctr;
78pub mod aes128gcm;
79pub mod aes128ocb;
80pub mod aes256cbc;
81pub mod aes256ctr;
82pub mod aes256gcm;
83pub mod aes256ocb;
84pub mod cmac_aes128;