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 works with this crate. If you require other cipher block sizes, please open an issue.
Examples:
Encrypting and decrypting multiple sectors at a time:
use ;
use ;
// Load the encryption key
let key = ;
let plaintext = ;
// Load the data to be encrypted
let mut buffer = plaintext.to_owned;
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 ;
use ;
// Load the encryption key
let key = ;
let plaintext = ;
// Load the data to be encrypted
let mut buffer = plaintext.to_owned;
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 ;
use ;
// Load the encryption key
let key = ;
let plaintext = ;
// Load the data to be encrypted
let mut buffer = plaintext.to_owned;
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 ;
use ;
// Load the header key
let header_key = &;
// Read into buffer header to be decrypted
let mut buffer = vec!;
let cipher_1 = new;
let cipher_2 = new;
let mut 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;