QSC Post Quantum Cryptographic Library 1.3.0.0 (C1)
A post quantum secure library written in Ansi C
Loading...
Searching...
No Matches
sha2.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_SHA2_H
53#define QSC_SHA2_H
54
55#include "qsccommon.h"
56
57QSC_CPLUSPLUS_ENABLED_START
58
85
90#define QSC_HKDF_256_KEY_SIZE 32U
91
96#define QSC_HKDF_384_KEY_SIZE 48U
97
102#define QSC_HKDF_512_KEY_SIZE 64U
103
108#define QSC_HMAC_256_KEY_SIZE 32U
109
114#define QSC_HMAC_384_KEY_SIZE 48U
115
120#define QSC_HMAC_512_KEY_SIZE 64U
121
126#define QSC_HMAC_256_MAC_SIZE 32U
127
132#define QSC_HMAC_384_MAC_SIZE 48U
133
138#define QSC_HMAC_512_MAC_SIZE 64U
139
144#define QSC_HMAC_256_RATE 64U
145
150#define QSC_HMAC_384_RATE 96U
151
156#define QSC_HMAC_512_RATE 128U
157
162#define QSC_SHA2_256_HASH_SIZE 32U
163
168#define QSC_SHA2_384_HASH_SIZE 48U
169
174#define QSC_SHA2_512_HASH_SIZE 64U
175
180#define QSC_SHA2_256_RATE 64U
181
186#define QSC_SHA2_384_RATE 128U
187
192#define QSC_SHA2_512_RATE 128U
193
198#define QSC_SHA2_STATE_SIZE 8U
199
200/* SHA2-256 */
201
206QSC_EXPORT_API typedef struct
207{
210 uint64_t t;
211 size_t position;
213
221QSC_EXPORT_API void qsc_sha256_compute(uint8_t* output, const uint8_t* message, size_t msglen);
222
229
239QSC_EXPORT_API void qsc_sha256_finalize(qsc_sha256_state* ctx, uint8_t* output);
240
247
257QSC_EXPORT_API void qsc_sha256_permute(uint32_t* output, const uint8_t* input);
258
268QSC_EXPORT_API void qsc_sha256_update(qsc_sha256_state* ctx, const uint8_t* message, size_t msglen);
269
270/* SHA2-384 */
271
276QSC_EXPORT_API typedef struct
277{
279 uint64_t t[2U];
281 size_t position;
283
293QSC_EXPORT_API void qsc_sha384_compute(uint8_t* output, const uint8_t* message, size_t msglen);
294
301
311QSC_EXPORT_API void qsc_sha384_finalize(qsc_sha384_state* ctx, uint8_t* output);
312
319
329QSC_EXPORT_API void qsc_sha384_update(qsc_sha384_state* ctx, const uint8_t* message, size_t msglen);
330
331/* SHA2-512 */
332
337QSC_EXPORT_API typedef struct
338{
340 uint64_t t[2U];
342 size_t position;
344
354QSC_EXPORT_API void qsc_sha512_compute(uint8_t* output, const uint8_t* message, size_t msglen);
355
362
372QSC_EXPORT_API void qsc_sha512_finalize(qsc_sha512_state* ctx, uint8_t* output);
373
380
390QSC_EXPORT_API void qsc_sha512_permute(uint64_t* output, const uint8_t* input);
391
401QSC_EXPORT_API void qsc_sha512_update(qsc_sha512_state* ctx, const uint8_t* message, size_t msglen);
402
403/* HMAC-256 */
404
415
427QSC_EXPORT_API void qsc_hmac256_compute(uint8_t* output, const uint8_t* message, size_t msglen, const uint8_t* key, size_t keylen);
428
435
445QSC_EXPORT_API void qsc_hmac256_finalize(qsc_hmac256_state* ctx, uint8_t* output);
446
454QSC_EXPORT_API void qsc_hmac256_initialize(qsc_hmac256_state* ctx, const uint8_t* key, size_t keylen);
455
465QSC_EXPORT_API void qsc_hmac256_update(qsc_hmac256_state* ctx, const uint8_t* message, size_t msglen);
466
467/* HMAC-384 */
468
479
491QSC_EXPORT_API void qsc_hmac384_compute(uint8_t* output, const uint8_t* message, size_t msglen, const uint8_t* key, size_t keylen);
492
499
509QSC_EXPORT_API void qsc_hmac384_finalize(qsc_hmac384_state* ctx, uint8_t* output);
510
518QSC_EXPORT_API void qsc_hmac384_initialize(qsc_hmac384_state* ctx, const uint8_t* key, size_t keylen);
519
529QSC_EXPORT_API void qsc_hmac384_update(qsc_hmac384_state* ctx, const uint8_t* message, size_t msglen);
530
531/* HMAC-512 */
532
543
555QSC_EXPORT_API void qsc_hmac512_compute(uint8_t* output, const uint8_t* message, size_t msglen, const uint8_t* key, size_t keylen);
556
563
573QSC_EXPORT_API void qsc_hmac512_finalize(qsc_hmac512_state* ctx, uint8_t* output);
574
582QSC_EXPORT_API void qsc_hmac512_initialize(qsc_hmac512_state* ctx, const uint8_t* key, size_t keylen);
583
593QSC_EXPORT_API void qsc_hmac512_update(qsc_hmac512_state* ctx, const uint8_t* message, size_t msglen);
594
595/* HKDF */
596
607QSC_EXPORT_API void qsc_hkdf256_expand(uint8_t* output, size_t otplen, const uint8_t* key, size_t keylen, const uint8_t* info, size_t infolen);
608
619QSC_EXPORT_API void qsc_hkdf256_extract(uint8_t* output, size_t otplen, const uint8_t* key, size_t keylen, const uint8_t* salt, size_t saltlen);
620
631QSC_EXPORT_API void qsc_hkdf384_expand(uint8_t* output, size_t otplen, const uint8_t* key, size_t keylen, const uint8_t* info, size_t infolen);
632
643QSC_EXPORT_API void qsc_hkdf384_extract(uint8_t* output, size_t otplen, const uint8_t* key, size_t keylen, const uint8_t* salt, size_t saltlen);
644
655QSC_EXPORT_API void qsc_hkdf512_expand(uint8_t* output, size_t otplen, const uint8_t* key, size_t keylen, const uint8_t* info, size_t infolen);
656
667QSC_EXPORT_API void qsc_hkdf512_extract(uint8_t* output, size_t otplen, const uint8_t* key, size_t keylen, const uint8_t* salt, size_t saltlen);
668
669QSC_CPLUSPLUS_ENABLED_END
670
671#endif
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
QSC_EXPORT_API void qsc_sha256_finalize(qsc_sha256_state *ctx, uint8_t *output)
Finalize the message state and return the hash value in the output array.
Definition sha2.c:56
QSC_EXPORT_API void qsc_sha384_update(qsc_sha384_state *ctx, const uint8_t *message, size_t msglen)
Update SHA2-384 with message input.
Definition sha2.c:782
QSC_EXPORT_API void qsc_sha512_initialize(qsc_sha512_state *ctx)
Initialize a SHA2-512 state structure.
Definition sha2.c:930
QSC_EXPORT_API void qsc_hmac384_dispose(qsc_hmac384_state *ctx)
Dispose of the HMAC-384 state.
Definition sha2.c:1474
QSC_EXPORT_API void qsc_hkdf256_extract(uint8_t *output, size_t otplen, const uint8_t *key, size_t keylen, const uint8_t *salt, size_t saltlen)
Extract a key from a combined key and salt input using HMAC(SHA2-256).
Definition sha2.c:1693
QSC_EXPORT_API void qsc_sha384_finalize(qsc_sha384_state *ctx, uint8_t *output)
Finalize the SHA2-384 state and return the hash value in the output array.
Definition sha2.c:714
QSC_EXPORT_API void qsc_sha384_dispose(qsc_sha384_state *ctx)
Dispose of the SHA2-384 state.
Definition sha2.c:700
QSC_EXPORT_API void qsc_hmac256_initialize(qsc_hmac256_state *ctx, const uint8_t *key, size_t keylen)
Initialize an HMAC-256 state structure with a key.
Definition sha2.c:1409
QSC_EXPORT_API void qsc_hmac512_update(qsc_hmac512_state *ctx, const uint8_t *message, size_t msglen)
Update HMAC-512 with message input.
Definition sha2.c:1638
QSC_EXPORT_API void qsc_hkdf512_extract(uint8_t *output, size_t otplen, const uint8_t *key, size_t keylen, const uint8_t *salt, size_t saltlen)
Extract a key from a combined key and salt input using HMAC(SHA2-512).
Definition sha2.c:1845
QSC_EXPORT_API void qsc_hmac384_initialize(qsc_hmac384_state *ctx, const uint8_t *key, size_t keylen)
Initialize an HMAC-384 state structure with a key.
Definition sha2.c:1504
QSC_EXPORT_API void qsc_sha256_initialize(qsc_sha256_state *ctx)
Initialize a SHA2-256 state structure.
Definition sha2.c:110
QSC_EXPORT_API void qsc_sha256_update(qsc_sha256_state *ctx, const uint8_t *message, size_t msglen)
Update SHA2-256 with message input.
Definition sha2.c:617
#define QSC_SHA2_512_RATE
The SHA2-512 absorption rate in bytes.
Definition sha2.h:192
QSC_EXPORT_API void qsc_sha512_compute(uint8_t *output, const uint8_t *message, size_t msglen)
Process a message with SHA2-512 and return the hash code in the output byte array.
Definition sha2.c:850
QSC_EXPORT_API void qsc_hmac512_dispose(qsc_hmac512_state *ctx)
Dispose of the HMAC-512 state.
Definition sha2.c:1569
QSC_EXPORT_API void qsc_hkdf384_expand(uint8_t *output, size_t otplen, const uint8_t *key, size_t keylen, const uint8_t *info, size_t infolen)
Initialize an instance of HKDF(HMAC(SHA2-384)) and generate pseudo-random output.
Definition sha2.c:1728
QSC_EXPORT_API void qsc_hmac256_update(qsc_hmac256_state *ctx, const uint8_t *message, size_t msglen)
Update HMAC-256 with message input.
Definition sha2.c:1448
QSC_EXPORT_API void qsc_hmac384_compute(uint8_t *output, const uint8_t *message, size_t msglen, const uint8_t *key, size_t keylen)
Process a message with HMAC(SHA2-384) and return the MAC code in the output byte array.
Definition sha2.c:1461
QSC_EXPORT_API void qsc_hmac256_finalize(qsc_hmac256_state *ctx, uint8_t *output)
Finalize the HMAC-256 state and return the MAC code in the output byte array.
Definition sha2.c:1391
QSC_EXPORT_API void qsc_sha384_initialize(qsc_sha384_state *ctx)
Initialize a SHA2-384 state structure.
Definition sha2.c:768
QSC_EXPORT_API void qsc_hmac512_finalize(qsc_hmac512_state *ctx, uint8_t *output)
Finalize the HMAC-512 state and return the MAC code in the output byte array.
Definition sha2.c:1581
#define QSC_SHA2_256_RATE
The SHA2-256 absorption rate in bytes.
Definition sha2.h:180
QSC_EXPORT_API void qsc_hmac512_initialize(qsc_hmac512_state *ctx, const uint8_t *key, size_t keylen)
Initialize an HMAC-512 state structure with a key.
Definition sha2.c:1599
QSC_EXPORT_API void qsc_hmac256_compute(uint8_t *output, const uint8_t *message, size_t msglen, const uint8_t *key, size_t keylen)
Process a message with HMAC(SHA2-256) and return the MAC code in the output byte array.
Definition sha2.c:1366
QSC_EXPORT_API void qsc_hkdf256_expand(uint8_t *output, size_t otplen, const uint8_t *key, size_t keylen, const uint8_t *info, size_t infolen)
Initialize an instance of HKDF(HMAC(SHA2-256)) and generate pseudo-random output.
Definition sha2.c:1651
QSC_EXPORT_API void qsc_hkdf512_expand(uint8_t *output, size_t otplen, const uint8_t *key, size_t keylen, const uint8_t *info, size_t infolen)
Initialize an instance of HKDF(HMAC(SHA2-512)) and generate pseudo-random output.
Definition sha2.c:1804
QSC_EXPORT_API void qsc_sha256_compute(uint8_t *output, const uint8_t *message, size_t msglen)
Process a message with SHA2-256 and return the hash code in the output byte array.
Definition sha2.c:44
QSC_EXPORT_API void qsc_sha256_dispose(qsc_sha256_state *ctx)
Dispose of the SHA2-256 state.
Definition sha2.c:31
QSC_EXPORT_API void qsc_sha512_permute(uint64_t *output, const uint8_t *input)
The SHA2-512 permutation function.
Definition sha2.c:944
QSC_EXPORT_API void qsc_hmac256_dispose(qsc_hmac256_state *ctx)
Dispose of the HMAC-256 state.
Definition sha2.c:1379
#define QSC_SHA2_384_RATE
The SHA2-384 absorption rate in bytes.
Definition sha2.h:186
QSC_EXPORT_API void qsc_sha512_finalize(qsc_sha512_state *ctx, uint8_t *output)
Finalize the SHA2-512 state and return the hash value in the output array.
Definition sha2.c:876
QSC_EXPORT_API void qsc_sha256_permute(uint32_t *output, const uint8_t *input)
The SHA2-256 permutation function.
Definition sha2.c:302
QSC_EXPORT_API void qsc_hmac384_finalize(qsc_hmac384_state *ctx, uint8_t *output)
Finalize the HMAC-384 state and return the MAC code in the output byte array.
Definition sha2.c:1486
QSC_EXPORT_API void qsc_hmac512_compute(uint8_t *output, const uint8_t *message, size_t msglen, const uint8_t *key, size_t keylen)
Process a message with HMAC(SHA2-512) and return the MAC code in the output byte array.
Definition sha2.c:1556
QSC_EXPORT_API void qsc_hkdf384_extract(uint8_t *output, size_t otplen, const uint8_t *key, size_t keylen, const uint8_t *salt, size_t saltlen)
Extract a key from a combined key and salt input using HMAC(SHA2-384).
Definition sha2.c:1769
QSC_EXPORT_API void qsc_hmac384_update(qsc_hmac384_state *ctx, const uint8_t *message, size_t msglen)
Update HMAC-384 with message input.
Definition sha2.c:1543
QSC_EXPORT_API void qsc_sha512_update(qsc_sha512_state *ctx, const uint8_t *message, size_t msglen)
Update SHA2-512 with message input.
Definition sha2.c:1323
QSC_EXPORT_API void qsc_sha512_dispose(qsc_sha512_state *ctx)
Dispose of the SHA2-512 state.
Definition sha2.c:862
#define QSC_SHA2_STATE_SIZE
The SHA2 state array size.
Definition sha2.h:198
QSC_EXPORT_API void qsc_sha384_compute(uint8_t *output, const uint8_t *message, size_t msglen)
Process a message with SHA2-384 and return the hash code in the output byte array.
Definition sha2.c:688
The HMAC(SHA2-256) state array.
Definition sha2.h:410
qsc_sha256_state pstate
Definition sha2.h:411
uint8_t ipad[QSC_SHA2_256_RATE]
Definition sha2.h:412
uint8_t opad[QSC_SHA2_256_RATE]
Definition sha2.h:413
The HMAC(SHA2-384) state array.
Definition sha2.h:474
qsc_sha384_state pstate
Definition sha2.h:475
uint8_t opad[QSC_SHA2_384_RATE]
Definition sha2.h:477
uint8_t ipad[QSC_SHA2_384_RATE]
Definition sha2.h:476
The HMAC(SHA2-512) state array.
Definition sha2.h:538
uint8_t opad[QSC_SHA2_512_RATE]
Definition sha2.h:541
uint8_t ipad[QSC_SHA2_512_RATE]
Definition sha2.h:540
qsc_sha512_state pstate
Definition sha2.h:539
The SHA2-256 digest state array.
Definition sha2.h:207
uint32_t state[QSC_SHA2_STATE_SIZE]
Definition sha2.h:208
uint64_t t
Definition sha2.h:210
size_t position
Definition sha2.h:211
uint8_t buffer[QSC_SHA2_256_RATE]
Definition sha2.h:209
The SHA2-384 digest state array.
Definition sha2.h:277
uint8_t buffer[QSC_SHA2_384_RATE]
Definition sha2.h:280
size_t position
Definition sha2.h:281
uint64_t state[QSC_SHA2_STATE_SIZE]
Definition sha2.h:278
uint64_t t[2U]
Definition sha2.h:279
The SHA2-512 digest state array.
Definition sha2.h:338
size_t position
Definition sha2.h:342
uint64_t state[QSC_SHA2_STATE_SIZE]
Definition sha2.h:339
uint64_t t[2U]
Definition sha2.h:340
uint8_t buffer[QSC_SHA2_512_RATE]
Definition sha2.h:341