linux/include/crypto/aes-cbc.h
Eric Biggers 3cbcf6a2d1 lib/crypto: aes: Add CBC and CBC-CTS support
Add support for AES-CBC and AES-CBC-CTS to the crypto library.

These will be used to provide streamlined implementations of the
"cbc(aes)" and "cts(cbc(aes))" crypto_skcipher algorithms.  Most users
of these crypto_skcipher algorithms will also be able to switch to the
library, which as usual will be simpler and faster, e.g.:

    - block/blk-crypto-fallback.c (for AES-128-CBC-ESSIV)
    - fs/crypto/crypto.c (for AES-128-CBC-ESSIV)
    - fs/crypto/fname.c (for AES-256-CTS and AES-128-CBC)
    - kernel/bpf/crypto.c
    - net/ceph/crypto.c
    - security/keys/encrypted-keys/encrypted.c

As usual, the architecture-optimized AES-CBC and AES-CBC-CTS code will
be migrated into the library as well (using the hooks provided in this
commit), eliminating lots of repetitive boilerplate code.

Initial test coverage is provided by the crypto_skcipher support added
in a later commit.  I'm planning a KUnit test suite as well.

Reviewed-by: Thomas Huth <thuth@redhat.com>
Link: https://patch.msgid.link/20260715221153.246410-4-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
2026-07-19 17:34:16 -07:00

78 lines
2.9 KiB
C

/* SPDX-License-Identifier: GPL-2.0 */
/*
* AES-CBC and AES-CBC-CTS unauthenticated encryption and decryption
*
* Copyright 2026 Google LLC
*/
#ifndef _CRYPTO_AES_CBC_H
#define _CRYPTO_AES_CBC_H
#include <crypto/aes.h>
/**
* aes_cbc_encrypt() - Encrypt data using AES-CBC
* @dst: The destination buffer. Can be in-place or out-of-place. For other
* overlaps the behavior is unspecified.
* @src: The source data
* @len: Number of bytes to encrypt. Must be a multiple of AES_BLOCK_SIZE.
* @iv: The initialization vector. It is updated with the next value, i.e. the
* last ciphertext block (or left unchanged if @len == 0).
* @key: The key, already prepared using aes_preparekey() or aes_prepareenckey()
*
* This supports incremental encryption. The length of each chunk must be a
* multiple of AES_BLOCK_SIZE, and the updated @iv must be passed in each time.
*
* Context: Any context.
*/
void aes_cbc_encrypt(u8 *dst, const u8 *src, size_t len,
u8 iv[at_least AES_BLOCK_SIZE], aes_encrypt_arg key);
/**
* aes_cbc_decrypt() - Decrypt data using AES-CBC
* @dst: The destination buffer. Can be in-place or out-of-place. For other
* overlaps the behavior is unspecified.
* @src: The source data
* @len: Number of bytes to decrypt. Must be a multiple of AES_BLOCK_SIZE.
* @iv: The initialization vector. It is updated with the next value, i.e. the
* last ciphertext block (or left unchanged if @len == 0).
* @key: The key, already prepared using aes_preparekey()
*
* This supports incremental decryption. The length of each chunk must be a
* multiple of AES_BLOCK_SIZE, and the updated @iv must be passed in each time.
*
* Context: Any context.
*/
void aes_cbc_decrypt(u8 *dst, const u8 *src, size_t len,
u8 iv[at_least AES_BLOCK_SIZE], const struct aes_key *key);
/**
* aes_cbc_cts_encrypt() - Encrypt data using AES-CBC-CTS (CS3 variant)
* @dst: The destination buffer. Can be in-place or out-of-place. For other
* overlaps the behavior is unspecified.
* @src: The source data
* @len: Number of bytes to encrypt, at least AES_BLOCK_SIZE
* @iv: The initialization vector, clobbered by this function
* @key: The key, already prepared using aes_preparekey() or aes_prepareenckey()
*
* Context: Any context.
*/
void aes_cbc_cts_encrypt(u8 *dst, const u8 *src, size_t len,
u8 iv[at_least AES_BLOCK_SIZE], aes_encrypt_arg key);
/**
* aes_cbc_cts_decrypt() - Decrypt data using AES-CBC-CTS (CS3 variant)
* @dst: The destination buffer. Can be in-place or out-of-place. For other
* overlaps the behavior is unspecified.
* @src: The source data
* @len: Number of bytes to decrypt, at least AES_BLOCK_SIZE
* @iv: The initialization vector, clobbered by this function
* @key: The key, already prepared using aes_preparekey()
*
* Context: Any context.
*/
void aes_cbc_cts_decrypt(u8 *dst, const u8 *src, size_t len,
u8 iv[at_least AES_BLOCK_SIZE],
const struct aes_key *key);
#endif /* _CRYPTO_AES_CBC_H */