QSC Post Quantum Cryptographic Library 1.3.0.0 (C1)
A post quantum secure library written in Ansi C
Loading...
Searching...
No Matches
aes.h
Go to the documentation of this file.
1/* 2020-2026 Quantum Resistant Cryptographic Solutions Corporation
2 * All Rights Reserved.
3 *
4 * NOTICE:
5 * This software and all accompanying materials are the exclusive property of
6 * Quantum Resistant Cryptographic Solutions Corporation (QRCS). The intellectual
7 * and technical concepts contained herein are proprietary to QRCS and are
8 * protected under applicable Canadian, U.S., and international copyright,
9 * patent, and trade secret laws.
10 *
11 * CRYPTOGRAPHIC ALGORITHMS AND IMPLEMENTATIONS:
12 * - This software includes implementations of cryptographic primitives and
13 * algorithms that are standardized or in the public domain, such as AES
14 * and SHA-3, which are not proprietary to QRCS.
15 * - This software also includes cryptographic primitives, constructions, and
16 * algorithms designed by QRCS, including but not limited to RCS, SCB, CSX, QMAC, and
17 * related components, which are proprietary to QRCS.
18 * - All source code, implementations, protocol compositions, optimizations,
19 * parameter selections, and engineering work contained in this software are
20 * original works of QRCS and are protected under this license.
21 *
22 * LICENSE AND USE RESTRICTIONS:
23 * - This software is licensed under the Quantum Resistant Cryptographic Solutions
24 * Public Research and Evaluation License (QRCS-PREL), 2025-2026.
25 * - Permission is granted solely for non-commercial evaluation, academic research,
26 * cryptographic analysis, interoperability testing, and feasibility assessment.
27 * - Commercial use, production deployment, commercial redistribution, or
28 * integration into products or services is strictly prohibited without a
29 * separate written license agreement executed with QRCS.
30 * - Licensing and authorized distribution are solely at the discretion of QRCS.
31 *
32 * EXPERIMENTAL CRYPTOGRAPHY NOTICE:
33 * Portions of this software may include experimental, novel, or evolving
34 * cryptographic designs. Use of this software is entirely at the user's risk.
35 *
36 * DISCLAIMER:
37 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38 * IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS
39 * FOR A PARTICULAR PURPOSE, SECURITY, OR NON-INFRINGEMENT. QRCS DISCLAIMS ALL
40 * LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
41 * ARISING FROM THE USE OR MISUSE OF THIS SOFTWARE.
42 *
43 * FULL LICENSE:
44 * This software is subject to the Quantum Resistant Cryptographic Solutions
45 * Public Research and Evaluation License (QRCS-PREL), 2025-2026. The complete license terms
46 * are provided in the accompanying LICENSE file or at https://www.qrcscorp.ca.
47 *
48 * Written by: John G. Underhill
49 * Contact: contact@qrcscorp.ca
50 */
51
52#ifndef QSC_AES_H
53#define QSC_AES_H
54
55#include "qsccommon.h"
56#include "intrinsics.h"
57
63#define QSC_HBA_KMAC_EXTENSION
64
70#define QSC_HBA_HKDF_EXTENSION
71
72#if defined(QSC_HBA_KMAC_EXTENSION)
73# include "sha3.h"
74#else
75# include "sha2.h"
76#endif
77
120
121QSC_CPLUSPLUS_ENABLED_START
122
134
148
149/***********************************
150* AES CONSTANTS AND SIZES *
151***********************************/
152
159#define QSC_AES_BLOCK_SIZE 16U
160
167#define QSC_AES_IV_SIZE 16U
168
173#define QSC_AES128_KEY_SIZE 16U
174
179#define QSC_AES256_KEY_SIZE 32U
180
185#define QSC_GCM128_MAC_SIZE 16U
186
191#define QSC_GCM256_MAC_SIZE 16U
192
197#define QSC_GCM_MAXAAD_SIZE 65536U
198
203#define QSC_GCM_NONCE_SIZE 12U
204
211#define QSC_GCM_MAX_NONCE_SIZE 32U
212
217#define QSC_HBA256_MAC_SIZE 32U
218
223#define QSC_HBA_MAXAAD_SIZE 256U
224
229#define QSC_HBA_MAXINFO_SIZE 256U
230
237#if defined(QSC_HBA_KMAC_EXTENSION)
238# define QSC_HBA_KMAC_AUTH
239#endif
240
250QSC_EXPORT_API typedef struct
251{
252 const uint8_t* key;
253 size_t keylen;
254 uint8_t* nonce;
255 size_t noncelen;
256 const uint8_t* info;
257 size_t infolen;
259
268QSC_EXPORT_API typedef struct
269{
270#if defined(QSC_SYSTEM_AESNI_ENABLED)
271 __m128i roundkeys[31U];
272# if defined(QSC_SYSTEM_HAS_AVX512)
273 __m512i roundkeysw[31U];
274# endif
275#else
276 uint32_t roundkeys[124U];
277#endif
278 size_t roundkeylen;
279 size_t rounds;
280 uint8_t* nonce;
282
283/* Function Declarations */
284
295
314QSC_EXPORT_API void qsc_aes_initialize(qsc_aes_state* ctx, const qsc_aes_keyparams* keyparams, bool encryption, qsc_aes_cipher_type ctype);
315
316/* CBC Mode */
317
333QSC_EXPORT_API void qsc_aes_cbc_decrypt(qsc_aes_state* ctx, uint8_t* output, size_t* outputlen, const uint8_t* input, size_t length);
334
349QSC_EXPORT_API void qsc_aes_cbc_encrypt(qsc_aes_state* ctx, uint8_t* output, const uint8_t* input, size_t length);
350
362QSC_EXPORT_API void qsc_aes_cbc_decrypt_block(qsc_aes_state* ctx, uint8_t* output, const uint8_t* input);
363
375QSC_EXPORT_API void qsc_aes_cbc_encrypt_block(qsc_aes_state* ctx, uint8_t* output, const uint8_t* input);
376
377/* PKCS#7 Padding */
378
389QSC_EXPORT_API void qsc_pkcs7_add_padding(uint8_t* input, size_t length);
390
402QSC_EXPORT_API size_t qsc_pkcs7_padding_length(const uint8_t* input);
403
404/* CTR Mode */
405
420QSC_EXPORT_API void qsc_aes_ctrbe_transform(qsc_aes_state* ctx, uint8_t* output, const uint8_t* input, size_t length);
421
436QSC_EXPORT_API void qsc_aes_ctrle_transform(qsc_aes_state* ctx, uint8_t* output, const uint8_t* input, size_t length);
437
438/* ECB Mode */
439
451QSC_EXPORT_API void qsc_aes_ecb_decrypt_block(const qsc_aes_state* ctx, uint8_t* output, const uint8_t* input);
452
462QSC_EXPORT_API void qsc_aes_ecb_encrypt_block(const qsc_aes_state* ctx, uint8_t* output, const uint8_t* input);
463
464/* HBA-256 Authenticated Encryption */
465
474QSC_EXPORT_API typedef struct
475{
476#if defined(QSC_HBA_KMAC_EXTENSION)
478#else
480#endif
482 uint64_t counter;
485 size_t custlen;
486 bool encrypt;
488
501
515QSC_EXPORT_API void qsc_aes_hba256_initialize(qsc_aes_hba256_state* ctx, const qsc_aes_keyparams* keyparams, bool encrypt);
516
529QSC_EXPORT_API void qsc_aes_hba256_set_associated(qsc_aes_hba256_state* ctx, const uint8_t* data, size_t datalen);
530
547QSC_EXPORT_API bool qsc_aes_hba256_transform(qsc_aes_hba256_state* ctx, uint8_t* output, const uint8_t* input, size_t length);
548
549/* GCM-AES-128 RFC 5288/5116 */
550
574
602QSC_EXPORT_API bool qsc_aes_gcm128_decrypt(qsc_aes_gcm128_state* ctx, uint8_t* output, const uint8_t* input, size_t length);
603
619
641QSC_EXPORT_API void qsc_aes_gcm128_encrypt(qsc_aes_gcm128_state* ctx, uint8_t* output, const uint8_t* input, size_t length);
642
667QSC_EXPORT_API void qsc_aes_gcm128_initialize(qsc_aes_gcm128_state* ctx, const qsc_aes_keyparams* keyparams, bool encryption);
668
687QSC_EXPORT_API void qsc_aes_gcm128_set_associated(qsc_aes_gcm128_state* ctx, const uint8_t* data, size_t datalen);
688
716QSC_EXPORT_API bool qsc_aes_gcm128_transform(qsc_aes_gcm128_state* ctx, uint8_t* output, const uint8_t* input, size_t length);
717
718/* GCM-AES-256 RFC 5288/5116 */
719
739
754QSC_EXPORT_API bool qsc_aes_gcm256_decrypt(qsc_aes_gcm256_state* ctx, uint8_t* output, const uint8_t* input, size_t length);
755
768
782QSC_EXPORT_API void qsc_aes_gcm256_encrypt(qsc_aes_gcm256_state* ctx, uint8_t* output, const uint8_t* input, size_t length);
783
797QSC_EXPORT_API void qsc_aes_gcm256_initialize(qsc_aes_gcm256_state* ctx, const qsc_aes_keyparams* keyparams, bool encryption);
798
811QSC_EXPORT_API void qsc_aes_gcm256_set_associated(qsc_aes_gcm256_state* ctx, const uint8_t* data, size_t datalen);
812
834QSC_EXPORT_API bool qsc_aes_gcm256_transform(qsc_aes_gcm256_state* ctx, uint8_t* output, const uint8_t* input, size_t length);
835
836QSC_CPLUSPLUS_ENABLED_END
837
838#endif
QSC_EXPORT_API void qsc_aes_gcm128_set_associated(qsc_aes_gcm128_state *ctx, const uint8_t *data, size_t datalen)
Supply associated additional data (AAD) to GCM-128.
Definition aes.c:2522
QSC_EXPORT_API void qsc_aes_gcm128_encrypt(qsc_aes_gcm128_state *ctx, uint8_t *output, const uint8_t *input, size_t length)
Encrypt plaintext and append an authentication tag using GCM-AES-128.
Definition aes.c:2401
QSC_EXPORT_API bool qsc_aes_gcm128_decrypt(qsc_aes_gcm128_state *ctx, uint8_t *output, const uint8_t *input, size_t length)
Decrypt ciphertext and verify the authentication tag using GCM-AES-128.
Definition aes.c:2318
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.
Definition aes.c:2106
QSC_EXPORT_API bool qsc_aes_gcm256_transform(qsc_aes_gcm256_state *ctx, uint8_t *output, const uint8_t *input, size_t length)
Transform an array of bytes.
Definition aes.c:2825
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.
Definition aes.c:1590
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.
Definition aes.c:1657
QSC_EXPORT_API void qsc_aes_hba256_dispose(qsc_aes_hba256_state *ctx)
Dispose of an HBA-256 ctx.
Definition aes.c:2024
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.
Definition aes.c:2088
QSC_EXPORT_API void qsc_aes_gcm256_dispose(qsc_aes_gcm256_state *ctx)
Dispose of an GCM-256 ctx.
Definition aes.c:2665
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.
Definition aes.c:2681
QSC_EXPORT_API void qsc_aes_gcm128_initialize(qsc_aes_gcm128_state *ctx, const qsc_aes_keyparams *keyparams, bool encryption)
Initialize the GCM-128 context for authenticated encryption or decryption.
Definition aes.c:2454
QSC_EXPORT_API void qsc_pkcs7_add_padding(uint8_t *input, size_t length)
Add PKCS#7 padding to a plaintext block.
Definition aes.c:1867
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.
Definition aes.c:1781
#define QSC_HBA256_MAC_SIZE
Size in bytes of the MAC code for HBA-256.
Definition aes.h:217
QSC_EXPORT_API bool qsc_aes_gcm256_decrypt(qsc_aes_gcm256_state *ctx, uint8_t *output, const uint8_t *input, size_t length)
Decrypt ciphertext and verify the authentication tag using GCM-AES-256.
Definition aes.c:2600
QSC_EXPORT_API void qsc_aes_gcm256_initialize(qsc_aes_gcm256_state *ctx, const qsc_aes_keyparams *keyparams, bool encryption)
Initialize the GCM-256 ctx for authenticated encryption or decryption.
Definition aes.c:2732
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.
Definition aes.c:2799
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.
Definition aes.c:1828
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.
Definition aes.c:2046
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.
Definition aes.c:1692
qsc_aes_cipher_type
Pre-defined cipher key sizes for AES.
Definition aes.h:130
@ qsc_aes_cipher_128
Definition aes.h:131
@ qsc_aes_cipher_256
Definition aes.h:132
QSC_EXPORT_API bool qsc_aes_gcm128_transform(qsc_aes_gcm128_state *ctx, uint8_t *output, const uint8_t *input, size_t length)
Unified GCM-128 encrypt-or-decrypt transform.
Definition aes.c:2550
QSC_EXPORT_API size_t qsc_pkcs7_padding_length(const uint8_t *input)
Determine the length of PKCS#7 padding in a decrypted block.
Definition aes.c:1889
QSC_EXPORT_API void qsc_aes_gcm128_dispose(qsc_aes_gcm128_state *ctx)
Dispose of a GCM-128 context.
Definition aes.c:2385
QSC_EXPORT_API void qsc_aes_dispose(qsc_aes_state *ctx)
Erase and dispose of the AES ctx.
Definition aes.c:1852
#define QSC_AES_BLOCK_SIZE
Internal AES block size in bytes.
Definition aes.h:159
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.
Definition aes.c:1840
#define QSC_HBA_MAXINFO_SIZE
Maximum allowed size (in bytes) for key information tweaks in HBA.
Definition aes.h:229
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.
Definition aes.c:1734
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.
Definition aes.c:1714
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.
Definition aes.c:1628
qsc_aes_cipher_mode
Pre-defined AES cipher mode implementations.
Definition aes.h:143
@ qsc_aes_mode_ecb
Definition aes.h:146
@ qsc_aes_mode_cbc
Definition aes.h:144
@ qsc_aes_mode_ctr
Definition aes.h:145
Contains common definitions for the Quantum Secure Cryptographic (QSC) library.
#define QSC_EXPORT_API
API export macro for Microsoft compilers when importing from a DLL.
Definition qsccommon.h:645
The SHA2 family of hash functions.
SHA3 family of hash functions.
State structure for AES-based Galois Counter Mode (GCM-128).
Definition aes.h:564
uint8_t J0[QSC_AES_BLOCK_SIZE]
Definition aes.h:568
uint64_t ctlen
Definition aes.h:571
qsc_aes_state cstate
Definition aes.h:565
uint8_t H[QSC_AES_BLOCK_SIZE]
Definition aes.h:567
uint64_t aadlen
Definition aes.h:570
uint8_t C[QSC_AES_BLOCK_SIZE]
Definition aes.h:566
uint8_t S[QSC_AES_BLOCK_SIZE]
Definition aes.h:569
bool encrypt
Definition aes.h:572
State structure for AES-based Galois Counter Mode (GCM-256).
Definition aes.h:729
uint8_t J0[QSC_AES_BLOCK_SIZE]
Definition aes.h:733
uint64_t ctlen
Definition aes.h:736
qsc_aes_state cstate
Definition aes.h:730
uint8_t H[QSC_AES_BLOCK_SIZE]
Definition aes.h:732
uint64_t aadlen
Definition aes.h:735
uint8_t C[QSC_AES_BLOCK_SIZE]
Definition aes.h:731
uint8_t S[QSC_AES_BLOCK_SIZE]
Definition aes.h:734
bool encrypt
Definition aes.h:737
State structure for AES-based Hash Based Authentication (HBA-256).
Definition aes.h:475
size_t custlen
Definition aes.h:485
uint64_t counter
Definition aes.h:482
qsc_aes_state cstate
Definition aes.h:481
qsc_keccak_state kstate
Definition aes.h:477
uint8_t cust[QSC_HBA_MAXINFO_SIZE]
Definition aes.h:484
uint8_t mkey[QSC_HBA256_MAC_SIZE]
Definition aes.h:483
bool encrypt
Definition aes.h:486
Structure for AES key parameters.
Definition aes.h:251
uint8_t * nonce
Definition aes.h:254
const uint8_t * key
Definition aes.h:252
size_t noncelen
Definition aes.h:255
size_t infolen
Definition aes.h:257
size_t keylen
Definition aes.h:253
const uint8_t * info
Definition aes.h:256
AES cipher ctx structure.
Definition aes.h:269
uint8_t * nonce
Definition aes.h:280
size_t roundkeylen
Definition aes.h:278
size_t rounds
Definition aes.h:279
uint32_t roundkeys[124U]
Definition aes.h:276
The HMAC(SHA2-256) state array.
Definition sha2.h:410
The Keccak state array; state array must be initialized by the caller.
Definition sha3.h:256