xts-mode
XTS block mode implementation in rust. Currently only 128-bit (16-byte) algorithms are supported, if you require other sizes, please open an issue.
For better AES performance, it is recommended to use the aes crate and enable the aes feature in
the compiler (see reference
and aesni).
Examples:
Encrypting and decrypting multiple sectors at a time:
use Aes128;
use NewBlockCipher;
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_varkey.unwrap;
let cipher_2 = new_varkey.unwrap;
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!;
Encrypting and decrypting a single sector:
use Aes128;
use NewBlockCipher;
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_varkey.unwrap;
let cipher_2 = new_varkey.unwrap;
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 NewBlockCipher;
use Xts128;
// Load the header key
let header_key = &;
// Read into buffer header to be decrypted
let mut buffer = vec!;
let cipher_1 = new_varkey.unwrap;
let cipher_2 = new_varkey.unwrap;
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;