Repository: google/OpenSK
File: libraries/crypto/src/cbc.rs (Rust)

Code before:
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::util::{xor_block_16, Block16};
use crate::aes256::{DecryptionKey, EncryptionKey};
use core::convert::TryInto;

pub fn cbc_encrypt(key: &EncryptionKey, mut iv: Block16, blocks: &mut [u8]) {
    for block in blocks.chunks_mut(16) {
        let block: &mut Block16 = block.try_into().unwrap();
        xor_block_16(block, &iv);
        key.encrypt_block(block);
        iv = *block;
    }
}

pub fn cbc_decrypt(key: &DecryptionKey, mut iv: Block16, blocks: &mut [u8]) {
    for block in blocks.chunks_mut(16) {
        let block: &mut Block16 = block.try_into().unwrap();
        let tmp = *block;
        key.decrypt_block(block);
        xor_block_16(block, &iv);
        iv = tmp;
    }
}

#[cfg(test)]
mod test {
    use super::super::aes256;
    use super::*;

    #[test]
    fn test_cbc_encrypt_decrypt() {

Review comment (security):
Nit: Given that the input doesn't take a slice of blocks anymore but a slice of bytes, it might make sense to rename the `blocks` parameter as something like `bytes`, as the new API doesn't guarantee the constraint that this parameter is a multiple of 16-byte blocks.

It would also be worth documenting what the behavior should be if the input is not a multiple of 16 bytes (i.e. the "remainder" is left as is rather than encrypted, which might be surprising at best, and at worst could lead to a security issue).

Code after:
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::util::{xor_block_16, Block16};
use crate::aes256::{DecryptionKey, EncryptionKey};
use core::convert::TryInto;

/// Encrypts a byte slice.
///
/// # Panics
///
/// Panics if the byte slice is not a multiple of the block size (16 bytes).
pub fn cbc_encrypt(key: &EncryptionKey, mut iv: Block16, bytes: &mut [u8]) {
    for block in bytes.chunks_mut(16) {
        let block: &mut Block16 = block.try_into().unwrap();
        xor_block_16(block, &iv);
        key.encrypt_block(block);
        iv = *block;
    }
}

/// Decrypts a byte slice.
///
/// # Panics
///
/// Panics if the byte slice is not a multiple of the block size (16 bytes).
pub fn cbc_decrypt(key: &DecryptionKey, mut iv: Block16, bytes: &mut [u8]) {
    for block in bytes.chunks_mut(16) {
        let block: &mut Block16 = block.try_into().unwrap();
        let tmp = *block;
        key.decrypt_block(block);
        xor_block_16(block, &iv);
        iv = tmp;