An implementation of the AES symmetric cipher along with modes and an AEAD scheme. More...
Go to the source code of this file.
Data Structures | |
struct | qsc_aes_keyparams |
Structure for AES key parameters. More... | |
struct | qsc_aes_state |
AES cipher ctx structure. More... | |
struct | qsc_aes_hba256_state |
State structure for AES-based Hash Based Authentication (HBA-256). More... | |
struct | qsc_aes_gcm256_state |
State structure for AES-based Galois Counter Mode (GCM-256). More... | |
Macros | |
#define | QSC_HBA_KMAC_EXTENSION |
Enables the cSHAKE/KMAC extensions for the HBA cipher mode. | |
#define | QSC_HBA_HKDF_EXTENSION |
Enables the HKDF extensions for the HBA cipher mode as an alternative to the cSHAKE mode. | |
#define | QSC_AES_BLOCK_SIZE 16ULL |
Internal AES block size in bytes. | |
#define | QSC_AES_IV_SIZE 16ULL |
Initialization vector (IV) size in bytes. | |
#define | QSC_AES128_KEY_SIZE 16ULL |
Key size in bytes for AES-128. | |
#define | QSC_AES256_KEY_SIZE 32ULL |
Key size in bytes for AES-256. | |
#define | QSC_GCM256_MAC_SIZE 16ULL |
Size in bytes of the MAC code for GCM-AES-256. | |
#define | QSC_GCM_MAXAAD_SIZE 256ULL |
Maximum allowed size (in bytes) for Associated Additional Data (AAD) in GCM. | |
#define | QSC_GCM_NONCE_SIZE 12ULL |
The standard nonce size used by GCM-AES-256. | |
#define | QSC_HBA256_MAC_SIZE 32ULL |
Size in bytes of the MAC code for HBA-256. | |
#define | QSC_HBA_MAXAAD_SIZE 256ULL |
Maximum allowed size (in bytes) for Associated Additional Data (AAD) in HBA. | |
#define | QSC_HBA_MAXINFO_SIZE 256ULL |
Maximum allowed size (in bytes) for key information tweaks in HBA. | |
#define | QSC_HBA_KMAC_AUTH |
When defined, enables the use of KMAC for authenticating HBA. | |
Typedefs | |
typedef QSC_EXPORT_API struct qsc_aes_gcm256_state | qsc_aes_gcm256_state |
Enumerations | |
enum | qsc_aes_cipher_type { qsc_aes_cipher_128 = 0x01U , qsc_aes_cipher_256 = 0x02U } |
Pre-defined cipher key sizes for AES. More... | |
enum | qsc_aes_cipher_mode { qsc_aes_mode_cbc = 0x01U , qsc_aes_mode_ctr = 0x02U , qsc_aes_mode_ecb = 0x03U } |
Pre-defined AES cipher mode implementations. More... | |
Functions | |
QSC_EXPORT_API void | qsc_aes_dispose (qsc_aes_state *ctx) |
Erase and dispose of the AES ctx. | |
QSC_EXPORT_API void | qsc_aes_initialize (qsc_aes_state *ctx, const qsc_aes_keyparams *keyparams, bool encryption, qsc_aes_cipher_type ctype) |
Initialize the AES ctx with the given key parameters. | |
QSC_EXPORT_API void | qsc_aes_cbc_decrypt (qsc_aes_state *ctx, uint8_t *output, size_t *outputlen, const uint8_t *input, size_t length) |
Decrypt ciphertext using AES in Cipher Block Chaining (CBC) mode. | |
QSC_EXPORT_API void | qsc_aes_cbc_encrypt (qsc_aes_state *ctx, uint8_t *output, const uint8_t *input, size_t length) |
Encrypt plaintext using AES in Cipher Block Chaining (CBC) mode. | |
QSC_EXPORT_API void | qsc_aes_cbc_decrypt_block (qsc_aes_state *ctx, uint8_t *output, const uint8_t *input) |
Decrypt a single 16-byte block using AES in CBC mode. | |
QSC_EXPORT_API void | qsc_aes_cbc_encrypt_block (qsc_aes_state *ctx, uint8_t *output, const uint8_t *input) |
Encrypt a single 16-byte block using AES in CBC mode. | |
QSC_EXPORT_API void | qsc_pkcs7_add_padding (uint8_t *input, size_t length) |
Add PKCS#7 padding to a plaintext block. | |
QSC_EXPORT_API size_t | qsc_pkcs7_padding_length (const uint8_t *input) |
Determine the length of PKCS#7 padding in a decrypted block. | |
QSC_EXPORT_API void | qsc_aes_ctrbe_transform (qsc_aes_state *ctx, uint8_t *output, const uint8_t *input, size_t length) |
Transform data using AES in Counter (CTR) mode with Big Endian counter incrementation. | |
QSC_EXPORT_API void | qsc_aes_ctrle_transform (qsc_aes_state *ctx, uint8_t *output, const uint8_t *input, size_t length) |
Transform data using AES in Counter (CTR) mode with Little Endian counter incrementation. | |
QSC_EXPORT_API void | qsc_aes_ecb_decrypt_block (const qsc_aes_state *ctx, uint8_t *output, const uint8_t *input) |
Decrypt a single 16-byte block using AES in Electronic CodeBook (ECB) mode. | |
QSC_EXPORT_API void | qsc_aes_ecb_encrypt_block (const qsc_aes_state *ctx, uint8_t *output, const uint8_t *input) |
Encrypt a single 16-byte block using AES in Electronic CodeBook (ECB) mode. | |
QSC_EXPORT_API void | qsc_aes_hba256_dispose (qsc_aes_hba256_state *ctx) |
Dispose of an HBA-256 ctx. | |
QSC_EXPORT_API void | qsc_aes_hba256_initialize (qsc_aes_hba256_state *ctx, const qsc_aes_keyparams *keyparams, bool encrypt) |
Initialize the HBA-256 ctx for authenticated encryption or decryption. | |
QSC_EXPORT_API void | qsc_aes_hba256_set_associated (qsc_aes_hba256_state *ctx, const uint8_t *data, size_t datalen) |
Set the associated data (AAD) for HBA-256 authenticated encryption. | |
QSC_EXPORT_API bool | qsc_aes_hba256_transform (qsc_aes_hba256_state *ctx, uint8_t *output, const uint8_t *input, size_t length) |
Transform data using the HBA-256 authenticated encryption mode. | |
QSC_EXPORT_API bool | qsc_aes_gcm256_decrypt (qsc_aes_gcm256_state *ctx, uint8_t *output, const uint8_t *input, size_t length) |
Decrypt data using the GCM-AES-256 authenticated encryption mode. | |
QSC_EXPORT_API void | qsc_aes_gcm256_dispose (qsc_aes_gcm256_state *ctx) |
Dispose of an GCM-256 ctx. | |
QSC_EXPORT_API void | qsc_aes_gcm256_encrypt (qsc_aes_gcm256_state *ctx, uint8_t *output, const uint8_t *input, size_t length) |
Encrypt data using the GCM-AES-256 authenticated encryption mode. | |
QSC_EXPORT_API void | qsc_aes_gcm256_initialize (qsc_aes_gcm256_state *ctx, const qsc_aes_keyparams *keyparams) |
Initialize the GCM-256 ctx for authenticated encryption or decryption. | |
QSC_EXPORT_API void | qsc_aes_gcm256_set_associated (qsc_aes_gcm256_state *ctx, const uint8_t *data, size_t datalen) |
Set the associated data (AAD) for GCM-256 authenticated encryption. | |
An implementation of the AES symmetric cipher along with modes and an AEAD scheme.
This header provides the interface for an implementation of the AES block cipher. Supported features include:
The HBA-256 mode supports both cSHAKE-based (via KMAC) and HKDF-based authentication. See QSC_HBA_KMAC_EXTENSION and QSC_HBA_HKDF_EXTENSION for details on enabling each mode.
#define QSC_AES_BLOCK_SIZE 16ULL |
Internal AES block size in bytes.
All AES operations use a fixed block size of 16 bytes.
#define QSC_AES_IV_SIZE 16ULL |
Initialization vector (IV) size in bytes.
The IV (or nonce) size is equal to the block size.
#define QSC_HBA_HKDF_EXTENSION |
Enables the HKDF extensions for the HBA cipher mode as an alternative to the cSHAKE mode.
When defined (and if QSC_HBA_KMAC_EXTENSION is not defined), HMAC(SHA2) is used by default.
#define QSC_HBA_KMAC_AUTH |
When defined, enables the use of KMAC for authenticating HBA.
If QSC_HBA_KMAC_EXTENSION is disabled, HMAC (SHA2) is used by default.
#define QSC_HBA_KMAC_EXTENSION |
Enables the cSHAKE/KMAC extensions for the HBA cipher mode.
When defined, the HBA-256 mode uses cSHAKE/KMAC for message authentication.
enum qsc_aes_cipher_mode |
Pre-defined AES cipher mode implementations.
Modes include CBC (Cipher Block Chaining), CTR (Counter mode), and ECB (Electronic Code Book). Note: ECB mode is considered insecure and should only be used for testing or as a building block.
Enumerator | |
---|---|
qsc_aes_mode_cbc | Cipher Block Chaining (CBC) mode |
qsc_aes_mode_ctr | Counter (CTR) mode using a segmented integer counter |
qsc_aes_mode_ecb | Electronic CodeBook (ECB) mode (insecure) |
enum qsc_aes_cipher_type |
QSC_EXPORT_API void qsc_aes_cbc_decrypt | ( | qsc_aes_state * | ctx, |
uint8_t * | output, | ||
size_t * | outputlen, | ||
const uint8_t * | input, | ||
size_t | length ) |
Decrypt ciphertext using AES in Cipher Block Chaining (CBC) mode.
Decrypts the input data in CBC mode and removes PKCS#7 padding.
ctx | [struct] Pointer to an initialized qsc_aes_state structure. |
output | [uint8_t*] Pointer to the output buffer where plaintext will be stored. |
outputlen | [size_t*] Pointer to a size_t that receives the length of the decrypted data. |
input | [const uint8_t*] Pointer to the input ciphertext. |
length | [size_t] Number of bytes of input ciphertext. |
QSC_EXPORT_API void qsc_aes_cbc_decrypt_block | ( | qsc_aes_state * | ctx, |
uint8_t * | output, | ||
const uint8_t * | input ) |
Decrypt a single 16-byte block using AES in CBC mode.
Decrypts one block of ciphertext and performs the XOR with the previous ciphertext block (or IV).
ctx | [struct] Pointer to an initialized qsc_aes_state structure. |
output | [uint8_t*] Pointer to a 16-byte buffer to receive the decrypted block. |
input | [const uint8_t*] Pointer to a 16-byte block of ciphertext. |
QSC_EXPORT_API void qsc_aes_cbc_encrypt | ( | qsc_aes_state * | ctx, |
uint8_t * | output, | ||
const uint8_t * | input, | ||
size_t | length ) |
Encrypt plaintext using AES in Cipher Block Chaining (CBC) mode.
Encrypts the input data in CBC mode, automatically applying PKCS#7 padding to the final block.
ctx | [struct] Pointer to an initialized qsc_aes_state structure. |
output | [uint8_t*] Pointer to the output buffer where ciphertext will be stored. |
input | [const uint8_t*] Pointer to the input plaintext. |
length | [size_t] Number of bytes of input plaintext. |
QSC_EXPORT_API void qsc_aes_cbc_encrypt_block | ( | qsc_aes_state * | ctx, |
uint8_t * | output, | ||
const uint8_t * | input ) |
Encrypt a single 16-byte block using AES in CBC mode.
Encrypts one block of plaintext by XOR-ing with the previous ciphertext block (or IV) and applying AES encryption.
ctx | [struct] Pointer to an initialized qsc_aes_state structure. |
output | [uint8_t*] Pointer to a 16-byte buffer to receive the ciphertext block. |
input | [const uint8_t*] Pointer to a 16-byte block of plaintext. |
QSC_EXPORT_API void qsc_aes_ctrbe_transform | ( | qsc_aes_state * | ctx, |
uint8_t * | output, | ||
const uint8_t * | input, | ||
size_t | length ) |
Transform data using AES in Counter (CTR) mode with Big Endian counter incrementation.
Encrypts or decrypts data in CTR mode. The same function is used for both operations.
ctx | [struct] Pointer to an initialized qsc_aes_state structure. |
output | [uint8_t*] Pointer to the buffer where the transformed data will be stored. |
input | [const uint8_t*] Pointer to the input data. |
length | [size_t] Number of bytes to process. |
QSC_EXPORT_API void qsc_aes_ctrle_transform | ( | qsc_aes_state * | ctx, |
uint8_t * | output, | ||
const uint8_t * | input, | ||
size_t | length ) |
Transform data using AES in Counter (CTR) mode with Little Endian counter incrementation.
Encrypts or decrypts data in CTR mode using a little endian counter.
ctx | [struct] Pointer to an initialized qsc_aes_state structure. |
output | [uint8_t*] Pointer to the buffer where the transformed data will be stored. |
input | [const uint8_t*] Pointer to the input data. |
length | [size_t] Number of bytes to process. |
QSC_EXPORT_API void qsc_aes_dispose | ( | qsc_aes_state * | ctx | ) |
Erase and dispose of the AES ctx.
Securely clears the round-key array and resets the key length.
ctx | [struct] Pointer to a qsc_aes_state structure. |
QSC_EXPORT_API void qsc_aes_ecb_decrypt_block | ( | const qsc_aes_state * | ctx, |
uint8_t * | output, | ||
const uint8_t * | input ) |
Decrypt a single 16-byte block using AES in Electronic CodeBook (ECB) mode.
ECB mode should only be used for testing or as a building block due to its inherent insecurity.
ctx | [const struct] Pointer to an initialized qsc_aes_state structure. |
output | [uint8_t*] Pointer to a 16-byte buffer to receive the decrypted plaintext. |
input | [const uint8_t*] Pointer to a 16-byte ciphertext block. |
QSC_EXPORT_API void qsc_aes_ecb_encrypt_block | ( | const qsc_aes_state * | ctx, |
uint8_t * | output, | ||
const uint8_t * | input ) |
Encrypt a single 16-byte block using AES in Electronic CodeBook (ECB) mode.
ctx | [const struct] Pointer to an initialized qsc_aes_state structure. |
output | [uint8_t*] Pointer to a 16-byte buffer to receive the ciphertext. |
input | [const uint8_t*] Pointer to a 16-byte plaintext block. |
QSC_EXPORT_API bool qsc_aes_gcm256_decrypt | ( | qsc_aes_gcm256_state * | ctx, |
uint8_t * | output, | ||
const uint8_t * | input, | ||
size_t | length ) |
Decrypt data using the GCM-AES-256 authenticated encryption mode.
This function decrypts the plaintext using AES-256 in GCM-CTR mode, computes a MAC over the nonce and ciphertext, and writes the MAC to the tag parameter.
ctx | [struct] Pointer to an initialized qsc_aes_gcm256_state structure. |
output | [uint8_t*] Pointer to the plaintext buffer. |
input | [const uint8_t*] Pointer to the ciphertext data with the appended MAC. |
length | [size_t] Length of the input ciphertext in bytes. |
true
if the transformation and MAC verification was successful; otherwise, false
.QSC_EXPORT_API void qsc_aes_gcm256_dispose | ( | qsc_aes_gcm256_state * | ctx | ) |
Dispose of an GCM-256 ctx.
Securely clears all internal ctx and keys used by the GCM-256 authenticated encryption mode.
ctx | [struct] Pointer to a qsc_aes_gcm256_state structure. |
QSC_EXPORT_API void qsc_aes_gcm256_encrypt | ( | qsc_aes_gcm256_state * | ctx, |
uint8_t * | output, | ||
const uint8_t * | input, | ||
size_t | length ) |
Encrypt data using the GCM-AES-256 authenticated encryption mode.
In encryption mode, this function encrypts the plaintext using AES-256 in CTR mode, computes a MAC over the nonce and ciphertext, and appends the MAC to the output.
ctx | [struct] Pointer to an initialized qsc_aes_gcm256_state structure. |
output | [uint8_t*] Pointer to the output buffer; must be large enough to hold ciphertext plus MAC. |
input | [const uint8_t*] Pointer to the input data; ciphertext with appended MAC. |
length | [size_t] Length of the input data in bytes (excluding the MAC for decryption). |
QSC_EXPORT_API void qsc_aes_gcm256_initialize | ( | qsc_aes_gcm256_state * | ctx, |
const qsc_aes_keyparams * | keyparams ) |
Initialize the GCM-256 ctx for authenticated encryption or decryption.
Generates the cipher key and MAC key from the provided key parameters and sets up the internal states.
ctx | [struct] Pointer to a qsc_aes_gcm256_state structure to initialize. |
keyparams | [const struct] Pointer to a constant qsc_aes_keyparams structure that provides the key, nonce, and optional info. |
QSC_EXPORT_API void qsc_aes_gcm256_set_associated | ( | qsc_aes_gcm256_state * | ctx, |
const uint8_t * | data, | ||
size_t | datalen ) |
Set the associated data (AAD) for GCM-256 authenticated encryption.
The associated data is used to authenticate additional information (such as headers) that is not encrypted. It must be set after initialization and before each call to qsc_aes_gcm256_encrypt
ctx | [struct] Pointer to the qsc_aes_gcm256_state structure. |
data | [const uint8_t*] Pointer to the associated data. |
datalen | [size_t] Length of the associated data in bytes. |
QSC_EXPORT_API void qsc_aes_hba256_dispose | ( | qsc_aes_hba256_state * | ctx | ) |
Dispose of an HBA-256 ctx.
Securely clears all internal ctx and keys used by the HBA-256 authenticated encryption mode.
ctx | [struct] Pointer to a qsc_aes_hba256_state structure. |
QSC_EXPORT_API void qsc_aes_hba256_initialize | ( | qsc_aes_hba256_state * | ctx, |
const qsc_aes_keyparams * | keyparams, | ||
bool | encrypt ) |
Initialize the HBA-256 ctx for authenticated encryption or decryption.
Generates the cipher key and MAC key from the provided key parameters and sets up the internal states.
ctx | [struct] Pointer to a qsc_aes_hba256_state structure to initialize. |
keyparams | [const struct] Pointer to a constant qsc_aes_keyparams structure that provides the key, nonce, and optional info. |
encrypt | [bool] Set to true for encryption mode, or false for decryption mode. |
QSC_EXPORT_API void qsc_aes_hba256_set_associated | ( | qsc_aes_hba256_state * | ctx, |
const uint8_t * | data, | ||
size_t | datalen ) |
Set the associated data (AAD) for HBA-256 authenticated encryption.
The associated data is used to authenticate additional information (such as headers) that is not encrypted. It must be set after initialization and before each call to qsc_aes_hba256_transform.
ctx | [struct] Pointer to the qsc_aes_hba256_state structure. |
data | [const uint8_t*] Pointer to the associated data. |
datalen | [size_t] Length of the associated data in bytes. |
QSC_EXPORT_API bool qsc_aes_hba256_transform | ( | qsc_aes_hba256_state * | ctx, |
uint8_t * | output, | ||
const uint8_t * | input, | ||
size_t | length ) |
Transform data using the HBA-256 authenticated encryption mode.
In encryption mode, this function encrypts the plaintext using AES-256 in CTR mode, computes a MAC over the nonce and ciphertext, and appends the MAC to the output. In decryption mode, it first verifies the MAC before decrypting the ciphertext.
ctx | [struct] Pointer to an initialized qsc_aes_hba256_state structure. |
output | [uint8_t*] Pointer to the output buffer (must be large enough to hold ciphertext plus MAC in encryption mode). |
input | [const uint8_t*] Pointer to the input data (ciphertext with appended MAC in decryption mode, plaintext in encryption mode). |
length | [size_t] Length of the input data in bytes (excluding the MAC for decryption). |
true
if the transformation (and MAC verification in decryption mode) was successful; otherwise, false
.QSC_EXPORT_API void qsc_aes_initialize | ( | qsc_aes_state * | ctx, |
const qsc_aes_keyparams * | keyparams, | ||
bool | encryption, | ||
qsc_aes_cipher_type | ctype ) |
Initialize the AES ctx with the given key parameters.
Expands the input cipher key into a round-key array for encryption or decryption. Note that for CTR mode the cipher is always initialized for encryption.
ctx | [struct] Pointer to the qsc_aes_state structure to initialize. |
keyparams | [const struct] Pointer to a constant qsc_aes_keyparams structure containing key, nonce, and optional info. |
encryption | [bool] Set to true to initialize for encryption; false for decryption. |
ctype | [enum] Specifies the AES cipher type (qsc_aes_cipher_128 or qsc_aes_cipher_256). |
ctx->roundkeys
is cleared and its size set before calling.QSC_EXPORT_API void qsc_pkcs7_add_padding | ( | uint8_t * | input, |
size_t | length ) |
Add PKCS#7 padding to a plaintext block.
Pads the input block with a padding byte equal to the number of padded bytes.
input | [uint8_t*] Pointer to the plaintext block (will be modified in-place). |
length | [size_t] Number of bytes that are less than the block size (i.e. QSC_AES_BLOCK_SIZE - actual data length). |
QSC_EXPORT_API size_t qsc_pkcs7_padding_length | ( | const uint8_t * | input | ) |
Determine the length of PKCS#7 padding in a decrypted block.
Analyzes a block of decrypted data and returns the number of padding bytes.
input | [const uint8_t*] Pointer to a decrypted block of plaintext. |