Skip to main content

Crate ctaes_rs

Crate ctaes_rs 

Source
Expand description

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);

Structs§

Aes128
128-bit AES-ECB cipher
Aes192
192-bit AES-ECB cipher
Aes256
256-bit AES-ECB cipher
Aes128Cbc
128-bit AES-CBC cipher
Aes192Cbc
192-bit AES-CBC cipher
Aes256Cbc
256-bit AES-CBC cipher

Enums§

Error
The errors that can be encountered using this crate
Pkcs7
Implementation of the PKCS7 padding scheme
ZeroPadding
Implementation of basic Zero Padding. May not be reversible if the original data ends with one or more zero bytes. Does not add an extra block of padding if the data length is already a multiple of the block size

Constants§

AES128_KEY_LENGTH
AES192_KEY_LENGTH
AES256_KEY_LENGTH
AES_BLOCK_SIZE

Traits§

AesBlockCipher
Trait that implements the common encrypt and decrypt methods for all AES ciphers
AesCbcBlockCipher
Trait that implements the common encrypt and decrypt methods for all AES-CBC ciphers
Padding
Trait defining interface for a Padding implementation