pub fn encrypt_sector(&self, sector: &mut [u8], mut tweak: [u8; 16]) {
        assert_eq!(
            <C as BlockCipher>::BlockSize::to_usize(),
            128 / 8,
            "Wrong block size"
        );

        assert!(
            sector.len() >= 16,
            "AES-XTS needs at least two blocks to perform stealing, or a single complete block"
        );

        let block_count = sector.len() / 16;
        let need_stealing = sector.len() % 16 != 0;

        // Compute tweak
        self.cipher_2
            .encrypt_block(GenericArray::from_mut_slice(&mut tweak));

        let nosteal_block_count = if need_stealing {
            block_count - 1
        } else {
            block_count
        };

        for i in (0..sector.len()).step_by(16).take(nosteal_block_count) {
            let block = &mut sector[i..i + 16];

            xor(block, &tweak);
            self.cipher_1
                .encrypt_block(GenericArray::from_mut_slice(block));
            xor(block, &tweak);

            tweak = galois_field_128_mul_le(tweak);
        }

        if need_stealing {
            let next_to_last_tweak = tweak;
            let last_tweak = galois_field_128_mul_le(tweak);

            let remaining = sector.len() % 16;
            let mut block: [u8; 16] = sector[16 * (block_count - 1)..16 * block_count]
                .try_into()
                .unwrap();

            xor(&mut block, &next_to_last_tweak);
            self.cipher_1
                .encrypt_block(GenericArray::from_mut_slice(&mut block));
            xor(&mut block, &next_to_last_tweak);

            let mut last_block = [0u8; 16];
            last_block[..remaining].copy_from_slice(&sector[16 * block_count..]);
            last_block[remaining..].copy_from_slice(&block[remaining..]);

            xor(&mut last_block, &last_tweak);
            self.cipher_1
                .encrypt_block(GenericArray::from_mut_slice(&mut last_block));
            xor(&mut last_block, &last_tweak);

            sector[16 * (block_count - 1)..16 * block_count].copy_from_slice(&last_block);
            sector[16 * block_count..].copy_from_slice(&block[..remaining]);
        }
    }
/// Encrypts a single sector in place using the given tweak.
///
/// # Panics
/// - If the block size is not 16 bytes.
/// - If there's less than a single block in the sector.
Encrypts a single sector in place using the given tweak.
Encrypts a single sector in place using the given tweak .
pub fn encrypt_sector(&self, sector: &mut [u8], mut tweak: [u8; 16]) {
        assert_eq!(
            <C as BlockCipher>::BlockSize::to_usize(),
            128 / 8,
            "Wrong block size"
        );
        assert!(
            sector.len() >= 16,
            "AES-XTS needs at least two blocks to perform stealing, or a single complete block"
        );
        let block_count = sector.len() / 16;
        let need_stealing = sector.len() % 16 != 0;
        // Compute tweak
        self.cipher_2
            .encrypt_block(GenericArray::from_mut_slice(&mut tweak));
        let nosteal_block_count = if need_stealing {
            block_count - 1
        } else {
            block_count
        };
        for i in (0..sector.len()).step_by(16).take(nosteal_block_count) {
            let block = &mut sector[i..i + 16];
            xor(block, &tweak);
            self.cipher_1
                .encrypt_block(GenericArray::from_mut_slice(block));
            xor(block, &tweak);
            tweak = galois_field_128_mul_le(tweak);
        }
        if need_stealing {
            let next_to_last_tweak = tweak;
            let last_tweak = galois_field_128_mul_le(tweak);
            let remaining = sector.len() % 16;
            let mut block: [u8; 16] = sector[16 * (block_count - 1)..16 * block_count]
                .try_into()
                .unwrap();
            xor(&mut block, &next_to_last_tweak);
            self.cipher_1
                .encrypt_block(GenericArray::from_mut_slice(&mut block));
            xor(&mut block, &next_to_last_tweak);
            let mut last_block = [0u8; 16];
            last_block[..remaining].copy_from_slice(&sector[16 * block_count..]);
            last_block[remaining..].copy_from_slice(&block[remaining..]);
            xor(&mut last_block, &last_tweak);
            self.cipher_1
                .encrypt_block(GenericArray::from_mut_slice(&mut last_block));
            xor(&mut last_block, &last_tweak);
            sector[16 * (block_count - 1)..16 * block_count].copy_from_slice(&last_block);
            sector[16 * block_count..].copy_from_slice(&block[..remaining]);
        }
    }
pub fn encrypt_sector ( & self , sector : & mut [ u8 ] , mut tweak : [ u8 ; 16 ] ) { assert_eq ! ( C as BlockCipher BlockSize to_usize ( ) 128 8 " " ) ; assert ! ( sector len ( ) 16 " " ) ; let block_count = sector . len ( ) / 16 ; let need_stealing = sector . len ( ) % 16 != 0 ; // Compute tweak self . cipher_2 . encrypt_block ( GenericArray :: from_mut_slice ( & mut tweak ) ) ; let nosteal_block_count = if need_stealing { block_count - 1 } else { block_count } ; for i in ( 0 .. sector . len ( ) ) . step_by ( 16 ) . take ( nosteal_block_count ) { let block = & mut sector [ i .. i + 16 ] ; xor ( block , & tweak ) ; self . cipher_1 . encrypt_block ( GenericArray :: from_mut_slice ( block ) ) ; xor ( block , & tweak ) ; tweak = galois_field_128_mul_le ( tweak ) ; } if need_stealing { let next_to_last_tweak = tweak ; let last_tweak = galois_field_128_mul_le ( tweak ) ; let remaining = sector . len ( ) % 16 ; let mut block : [ u8 ; 16 ] = sector [ 16 * ( block_count - 1 ) .. 16 * block_count ] . try_into ( ) . unwrap ( ) ; xor ( & mut block , & next_to_last_tweak ) ; self . cipher_1 . encrypt_block ( GenericArray :: from_mut_slice ( & mut block ) ) ; xor ( & mut block , & next_to_last_tweak ) ; let mut last_block = [ 0u8 ; 16 ] ; last_block [ .. remaining ] . copy_from_slice ( & sector [ 16 * block_count .. ] ) ; last_block [ remaining .. ] . copy_from_slice ( & block [ remaining .. ] ) ; xor ( & mut last_block , & last_tweak ) ; self . cipher_1 . encrypt_block ( GenericArray :: from_mut_slice ( & mut last_block ) ) ; xor ( & mut last_block , & last_tweak ) ; sector [ 16 * ( block_count - 1 ) .. 16 * block_count ] . copy_from_slice ( & last_block ) ; sector [ 16 * block_count .. ] . copy_from_slice ( & block [ .. remaining ] ) ; } }
Encrypts a single sector in place using the given tweak.
Encrypts a single sector in place using the given tweak .
&self
sector
&mut [u8]
tweak
[u8; 16]
&self
sector
&mut [u8]
tweak
[u8; 16]
panics
If the block size is not 16 bytes.
If there's less than a single block in the sector.
If the block size is not 16 bytes . If there ' s less than a single block in the sector .