xts-mode
XTS block mode implementation in Rust.
Currently this implementation supports only ciphers with 128-bit (16-byte) block size (distinct from key size). Note that AES-256 uses 128-bit blocks, so it is supported by this crate. If you require other block sizes, feel free to open an issue, but notice that only XTS-AES has been standardized and the AES standard supports only 128-bit blocks.
Security warnings
This crate has never been independently audited, use at your own risk.
All of the usual caveats of XTS apply to this crate, and its only intended usage is disk (sector-based storage) encryption. Some of these caveats:
- It has no authentication tags, so an adversary with write access may be able to randomize blocks.
- An adversary with read-write access may be able to reset blocks to a previous value they have seen.
- It's deterministic, so passive observers may be able to infer when each block has changed.
I recommend reading more about XTS weaknesses before using it.
Examples:
Encrypting and decrypting multiple sectors at a time:
use Aes128;
use ;
use ;
// Load encryption key
let key: = Array;
let plaintext = ;
// Load data to be encrypted
let mut buffer = plaintext.to_owned;
let = key.;
let cipher_1 = new;
let cipher_2 = new;
let xts = new;
let sector_size = 0x200;
let first_sector_index = 0;
// Encrypt data in the buffer
xts.encrypt_area;
// Decrypt data in the buffer
xts.decrypt_area;
assert_eq!;
AES-256 works too:
use Aes256;
use ;
use ;
// Load the encryption key
let key: = Array;
let plaintext = ;
// Load the data to be encrypted
let mut buffer = plaintext.to_owned;
let = key.;
let cipher_1 = new;
let cipher_2 = new;
let xts = new;
let sector_size = 0x200;
let first_sector_index = 0;
xts.encrypt_area;
xts.decrypt_area;
assert_eq!;
Encrypting and decrypting a single sector:
use Aes128;
use ;
use ;
// Load encryption key
let key: = Array;
let plaintext = ;
// Load data to be encrypted
let mut buffer = plaintext.to_owned;
let = key.;
let cipher_1 = new;
let cipher_2 = new;
let xts = new;
let tweak = get_tweak_default; // 0 is the sector index
// Encrypt data in the buffer
xts.encrypt_sector;
// Decrypt data in the buffer
xts.decrypt_sector;
assert_eq!;
Decrypting a NCA (nintendo content archive) header:
use Aes128;
use ;
use Xts128;
// Load header key
let header_key: = Array;
// Read header to be decrypted into buffer
let mut buffer = vec!;
let = header_key.;
let cipher_1 = new;
let cipher_2 = new;
let xts = new;
// Decrypt the first 0x400 bytes of the header in 0x200 sections
xts.decrypt_area;
let magic = &buffer;
assert_eq!; // In older NCA versions the section index used in header encryption was different
// Decrypt the rest of the header
xts.decrypt_area;