QSC Post Quantum Cryptographic Library 1.0.0.6c (A6)
A post quantum secure library written in Ansi C
 
Loading...
Searching...
No Matches
aes.h
Go to the documentation of this file.
1/*
2 * ================= LICENSE INFORMATION =================
3 * 2025 Quantum Resistant Cryptographic Solutions Corporation
4 * All Rights Reserved.
5 *
6 * NOTICE: This software and all accompanying materials are the exclusive
7 * property of Quantum Resistant Cryptographic Solutions Corporation (QRCS).
8 * The intellectual and technical concepts contained within this implementation
9 * are proprietary to QRCS and its authorized licensors and are protected under
10 * applicable U.S. and international copyright, patent, and trade secret laws.
11 *
12 * CRYPTOGRAPHIC STANDARDS:
13 * - This software includes implementations of cryptographic algorithms such as
14 * SHA3, AES, and others. These algorithms are public domain or standardized
15 * by organizations such as NIST and are NOT the property of QRCS.
16 * - However, all source code, optimizations, and implementations in this library
17 * are original works of QRCS and are protected under this license.
18 *
19 * RESTRICTIONS:
20 * - Redistribution, modification, or unauthorized distribution of this software,
21 * in whole or in part, is strictly prohibited.
22 * - This software is provided for non-commercial, educational, and research
23 * purposes only. Commercial use in any form is expressly forbidden.
24 * - Licensing and authorized distribution are solely at the discretion of QRCS.
25 * - Any use of this software implies acceptance of these restrictions.
26 *
27 * DISCLAIMER:
28 * This software is provided "as is," without warranty of any kind, express or
29 * implied, including but not limited to warranties of merchantability or fitness
30 * for a particular purpose. QRCS disclaims all liability for any direct, indirect,
31 * incidental, or consequential damages resulting from the use or misuse of this software.
32 *
33 * FULL LICENSE:
34 * This software is subject to the **Quantum Resistant Cryptographic Solutions
35 * Proprietary License (QRCS-PL)**. The complete license terms are included
36 * in the LICENSE.txt file distributed with this software.
37 *
38 * Written by: John G. Underhill
39 * Contact: john.underhill@protonmail.com
40 */
41
42#ifndef QSC_AES_H
43#define QSC_AES_H
44
45#include "common.h"
46#include "intrinsics.h"
47
53#define QSC_HBA_KMAC_EXTENSION
54
60#define QSC_HBA_HKDF_EXTENSION
61
62#if defined(QSC_HBA_KMAC_EXTENSION)
63# include "sha3.h"
64#else
65# include "sha2.h"
66#endif
67
110
111QSC_CPLUSPLUS_ENABLED_START
112
124
138
139/***********************************
140* AES CONSTANTS AND SIZES *
141***********************************/
142
149#define QSC_AES_BLOCK_SIZE 16ULL
150
157#define QSC_AES_IV_SIZE 16ULL
158
163#define QSC_AES128_KEY_SIZE 16ULL
164
169#define QSC_AES256_KEY_SIZE 32ULL
170
175#define QSC_GCM256_MAC_SIZE 16ULL
176
181#define QSC_GCM_MAXAAD_SIZE 256ULL
182
187#define QSC_GCM_NONCE_SIZE 12ULL
188
193#define QSC_HBA256_MAC_SIZE 32ULL
194
199#define QSC_HBA_MAXAAD_SIZE 256ULL
200
205#define QSC_HBA_MAXINFO_SIZE 256ULL
206
213#if defined(QSC_HBA_KMAC_EXTENSION)
214# define QSC_HBA_KMAC_AUTH
215#endif
216
226QSC_EXPORT_API typedef struct
227{
228 const uint8_t* key;
229 size_t keylen;
230 uint8_t* nonce;
231 size_t noncelen;
232 const uint8_t* info;
233 size_t infolen;
235
244QSC_EXPORT_API typedef struct
245{
246#if defined(QSC_SYSTEM_AESNI_ENABLED)
247 __m128i roundkeys[31];
248# if defined(QSC_SYSTEM_HAS_AVX512)
249 __m512i roundkeysw[31];
250# endif
251#else
252 uint32_t roundkeys[124];
253#endif
254 size_t roundkeylen;
255 size_t rounds;
256 uint8_t* nonce;
258
259/* Function Declarations */
260
271
287QSC_EXPORT_API void qsc_aes_initialize(qsc_aes_state* ctx, const qsc_aes_keyparams* keyparams, bool encryption, qsc_aes_cipher_type ctype);
288
289/* CBC Mode */
290
306QSC_EXPORT_API void qsc_aes_cbc_decrypt(qsc_aes_state* ctx, uint8_t* output, size_t* outputlen, const uint8_t* input, size_t length);
307
322QSC_EXPORT_API void qsc_aes_cbc_encrypt(qsc_aes_state* ctx, uint8_t* output, const uint8_t* input, size_t length);
323
335QSC_EXPORT_API void qsc_aes_cbc_decrypt_block(qsc_aes_state* ctx, uint8_t* output, const uint8_t* input);
336
348QSC_EXPORT_API void qsc_aes_cbc_encrypt_block(qsc_aes_state* ctx, uint8_t* output, const uint8_t* input);
349
350/* PKCS#7 Padding */
351
362QSC_EXPORT_API void qsc_pkcs7_add_padding(uint8_t* input, size_t length);
363
375QSC_EXPORT_API size_t qsc_pkcs7_padding_length(const uint8_t* input);
376
377/* CTR Mode */
378
393QSC_EXPORT_API void qsc_aes_ctrbe_transform(qsc_aes_state* ctx, uint8_t* output, const uint8_t* input, size_t length);
394
409QSC_EXPORT_API void qsc_aes_ctrle_transform(qsc_aes_state* ctx, uint8_t* output, const uint8_t* input, size_t length);
410
411/* ECB Mode */
412
424QSC_EXPORT_API void qsc_aes_ecb_decrypt_block(const qsc_aes_state* ctx, uint8_t* output, const uint8_t* input);
425
435QSC_EXPORT_API void qsc_aes_ecb_encrypt_block(const qsc_aes_state* ctx, uint8_t* output, const uint8_t* input);
436
437/* HBA-256 Authenticated Encryption */
438
447QSC_EXPORT_API typedef struct
448{
449#if defined(QSC_HBA_KMAC_EXTENSION)
451#else
453#endif
455 uint64_t counter;
458 size_t custlen;
459 bool encrypt;
461
474
488QSC_EXPORT_API void qsc_aes_hba256_initialize(qsc_aes_hba256_state* ctx, const qsc_aes_keyparams* keyparams, bool encrypt);
489
502QSC_EXPORT_API void qsc_aes_hba256_set_associated(qsc_aes_hba256_state* ctx, const uint8_t* data, size_t datalen);
503
520QSC_EXPORT_API bool qsc_aes_hba256_transform(qsc_aes_hba256_state* ctx, uint8_t* output, const uint8_t* input, size_t length);
521
522/* GCM-AES-256 RFC 5288/5116 */
523
543
559QSC_EXPORT_API bool qsc_aes_gcm256_decrypt(qsc_aes_gcm256_state* ctx, uint8_t* output, const uint8_t* input, size_t length);
560
573
587QSC_EXPORT_API void qsc_aes_gcm256_encrypt(qsc_aes_gcm256_state* ctx, uint8_t* output, const uint8_t* input, size_t length);
588
602
615QSC_EXPORT_API void qsc_aes_gcm256_set_associated(qsc_aes_gcm256_state* ctx, const uint8_t* data, size_t datalen);
616
617QSC_CPLUSPLUS_ENABLED_END
618
619#endif
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:1599
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:1124
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.
Definition aes.c:1965
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:1186
QSC_EXPORT_API void qsc_aes_hba256_dispose(qsc_aes_hba256_state *ctx)
Dispose of an HBA-256 ctx.
Definition aes.c:1521
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:1581
QSC_EXPORT_API void qsc_aes_gcm256_dispose(qsc_aes_gcm256_state *ctx)
Dispose of an GCM-256 ctx.
Definition aes.c:1903
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:1919
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:1370
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:1294
#define QSC_HBA256_MAC_SIZE
Size in bytes of the MAC code for HBA-256.
Definition aes.h:193
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.
Definition aes.c:1835
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:1812
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:1337
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:1543
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:1217
qsc_aes_cipher_type
Pre-defined cipher key sizes for AES.
Definition aes.h:120
@ qsc_aes_cipher_128
Definition aes.h:121
@ qsc_aes_cipher_256
Definition aes.h:122
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:1388
QSC_EXPORT_API void qsc_aes_dispose(qsc_aes_state *ctx)
Erase and dispose of the AES ctx.
Definition aes.c:1355
#define QSC_AES_BLOCK_SIZE
Internal AES block size in bytes.
Definition aes.h:149
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:1346
#define QSC_HBA_MAXINFO_SIZE
Maximum allowed size (in bytes) for key information tweaks in HBA.
Definition aes.h:205
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:1253
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:1236
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:1161
qsc_aes_cipher_mode
Pre-defined AES cipher mode implementations.
Definition aes.h:133
@ qsc_aes_mode_ecb
Definition aes.h:136
@ qsc_aes_mode_cbc
Definition aes.h:134
@ qsc_aes_mode_ctr
Definition aes.h:135
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 common.h:520
The SHA2 family of hash functions.
SHA3 family of hash functions.
State structure for AES-based Galois Counter Mode (GCM-256).
Definition aes.h:533
uint8_t J0[QSC_AES_BLOCK_SIZE]
Definition aes.h:537
uint64_t ctlen
Definition aes.h:540
qsc_aes_state cstate
Definition aes.h:534
uint8_t H[QSC_AES_BLOCK_SIZE]
Definition aes.h:536
uint64_t aadlen
Definition aes.h:539
uint8_t C[QSC_AES_BLOCK_SIZE]
Definition aes.h:535
uint8_t S[QSC_AES_BLOCK_SIZE]
Definition aes.h:538
State structure for AES-based Hash Based Authentication (HBA-256).
Definition aes.h:448
size_t custlen
Definition aes.h:458
uint64_t counter
Definition aes.h:455
qsc_aes_state cstate
Definition aes.h:454
qsc_keccak_state kstate
Definition aes.h:450
uint8_t cust[QSC_HBA_MAXINFO_SIZE]
Definition aes.h:457
uint8_t mkey[QSC_HBA256_MAC_SIZE]
Definition aes.h:456
bool encrypt
Definition aes.h:459
Structure for AES key parameters.
Definition aes.h:227
uint8_t * nonce
Definition aes.h:230
const uint8_t * key
Definition aes.h:228
size_t noncelen
Definition aes.h:231
size_t infolen
Definition aes.h:233
size_t keylen
Definition aes.h:229
const uint8_t * info
Definition aes.h:232
AES cipher ctx structure.
Definition aes.h:245
uint32_t roundkeys[124]
Definition aes.h:252
uint8_t * nonce
Definition aes.h:256
size_t roundkeylen
Definition aes.h:254
size_t rounds
Definition aes.h:255
The HMAC(SHA2-256) state array.
Definition sha2.h:378
The Keccak state array; state array must be initialized by the caller.
Definition sha3.h:240