2 releases
new 0.1.1 | May 15, 2025 |
---|---|
0.1.0 | May 15, 2025 |
#42 in Magic Beans
95 downloads per month
65KB
1.5K
SLoC
Rust bindings and API for CTAES (constant-time AES implementation from Bitcoin Core found at https://github.com/bitcoin-core/ctaes)
The CTAES Library provides a constant time implementation of the AES algorithm. For completeness this crate provides the interface to the AES-ECB methods, but they should not be used. Rather, use the AES-CBC methods.
The crate also provides a Padding utility implementation to help the user prepare, pad and unpad buffers. Zero Padding and PKCS7 padding implementations are provided
Examples
extern crate hex_conservative;
use hex_conservative::FromHex;
use ctaes_rs::{Padding, Pkcs7, AesCbcBlockCipher, Aes128Cbc};
let key = <[u8; 16]>::from_hex("2b7e151628aed2a6abf7158809cf4f3c").unwrap();
let iv = <[u8; 16]>::from_hex("000102030405060708090a0b0c0d0e0f").unwrap();
let message = <Vec<u8>>::from_hex("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710").unwrap();
let padded_buffer_length = Pkcs7::padded_buffer_length(message.len(), 16);
let mut plaintext = vec![0u8; padded_buffer_length];
plaintext[0..message.len()].copy_from_slice(message.as_slice());
Pkcs7::pad(plaintext.as_mut_slice(), message.len(), 16).unwrap();
let mut ciphertext = vec![0u8; padded_buffer_length];
let cipher = Aes128Cbc::new(key.as_slice(), iv.as_slice()).unwrap();
cipher.encrypt(plaintext.as_slice(), ciphertext.as_mut_slice()).unwrap();
let mut deciphered = vec![0u8; padded_buffer_length];
cipher.decrypt(ciphertext.as_slice(), deciphered.as_mut_slice()).unwrap();
let unpadded_result = Pkcs7::unpad(deciphered.as_slice()).unwrap();
assert_eq!(message.as_slice(), unpadded_result);
ctaes-rs
Rust bindings and API for CTAES (constant-time AES implementation from Bitcoin Core found at https://github.com/bitcoin-core/ctaes)
The CTAES Library provides a constant time implementation of the AES algorithm. For completeness this crate provides the interface to the AES-ECB methods, but they should not be used. Rather, use the AES-CBC methods.
The crate also provides a Padding utility implementation to help the user prepare, pad and unpad buffers. Zero Padding and PKCS7 padding implementations are provided
Run cargo doc --nodeps
to generate the documentation of the library.
Note
This is a low-level encryption library and should be employed in conjunction with a message authentication scheme to avoid chosen-ciphertext and chosen-plaintext attacks.
Compatibility
This crate requires Rust 1.63.0 or later.
Dependencies
~0.3–1MB
~21K SLoC