QSC Post Quantum Cryptographic Library 1.1.0.2 (B2)
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_512_KEY_SIZE 64U
97
102#define QSC_HMAC_256_KEY_SIZE 32U
103
108#define QSC_HMAC_512_KEY_SIZE 64U
109
114#define QSC_HMAC_256_MAC_SIZE 32U
115
120#define QSC_HMAC_512_MAC_SIZE 64U
121
126#define QSC_HMAC_256_RATE 64U
127
132#define QSC_HMAC_512_RATE 128U
133
138#define QSC_SHA2_256_HASH_SIZE 32U
139
144#define QSC_SHA2_384_HASH_SIZE 48U
145
150#define QSC_SHA2_512_HASH_SIZE 64U
151
156#define QSC_SHA2_256_RATE 64U
157
162#define QSC_SHA2_384_RATE 128U
163
168#define QSC_SHA2_512_RATE 128U
169
174#define QSC_SHA2_STATE_SIZE 8U
175
176/* SHA2-256 */
177
182QSC_EXPORT_API typedef struct
183{
186 uint64_t t;
187 size_t position;
189
197QSC_EXPORT_API void qsc_sha256_compute(uint8_t* output, const uint8_t* message, size_t msglen);
198
205
215QSC_EXPORT_API void qsc_sha256_finalize(qsc_sha256_state* ctx, uint8_t* output);
216
223
233QSC_EXPORT_API void qsc_sha256_permute(uint32_t* output, const uint8_t* input);
234
244QSC_EXPORT_API void qsc_sha256_update(qsc_sha256_state* ctx, const uint8_t* message, size_t msglen);
245
246/* SHA2-384 */
247
252QSC_EXPORT_API typedef struct
253{
255 uint64_t t[2U];
257 size_t position;
259
269QSC_EXPORT_API void qsc_sha384_compute(uint8_t* output, const uint8_t* message, size_t msglen);
270
277
287QSC_EXPORT_API void qsc_sha384_finalize(qsc_sha384_state* ctx, uint8_t* output);
288
295
305QSC_EXPORT_API void qsc_sha384_update(qsc_sha384_state* ctx, const uint8_t* message, size_t msglen);
306
307/* SHA2-512 */
308
313QSC_EXPORT_API typedef struct
314{
316 uint64_t t[2U];
318 size_t position;
320
330QSC_EXPORT_API void qsc_sha512_compute(uint8_t* output, const uint8_t* message, size_t msglen);
331
338
348QSC_EXPORT_API void qsc_sha512_finalize(qsc_sha512_state* ctx, uint8_t* output);
349
356
366QSC_EXPORT_API void qsc_sha512_permute(uint64_t* output, const uint8_t* input);
367
377QSC_EXPORT_API void qsc_sha512_update(qsc_sha512_state* ctx, const uint8_t* message, size_t msglen);
378
379/* HMAC-256 */
380
391
403QSC_EXPORT_API void qsc_hmac256_compute(uint8_t* output, const uint8_t* message, size_t msglen, const uint8_t* key, size_t keylen);
404
411
421QSC_EXPORT_API void qsc_hmac256_finalize(qsc_hmac256_state* ctx, uint8_t* output);
422
430QSC_EXPORT_API void qsc_hmac256_initialize(qsc_hmac256_state* ctx, const uint8_t* key, size_t keylen);
431
441QSC_EXPORT_API void qsc_hmac256_update(qsc_hmac256_state* ctx, const uint8_t* message, size_t msglen);
442
443/* HMAC-384 */
444
455
467QSC_EXPORT_API void qsc_hmac384_compute(uint8_t* output, const uint8_t* message, size_t msglen, const uint8_t* key, size_t keylen);
468
475
485QSC_EXPORT_API void qsc_hmac384_finalize(qsc_hmac384_state* ctx, uint8_t* output);
486
494QSC_EXPORT_API void qsc_hmac384_initialize(qsc_hmac384_state* ctx, const uint8_t* key, size_t keylen);
495
505QSC_EXPORT_API void qsc_hmac384_update(qsc_hmac384_state* ctx, const uint8_t* message, size_t msglen);
506
507/* HMAC-512 */
508
519
531QSC_EXPORT_API void qsc_hmac512_compute(uint8_t* output, const uint8_t* message, size_t msglen, const uint8_t* key, size_t keylen);
532
539
549QSC_EXPORT_API void qsc_hmac512_finalize(qsc_hmac512_state* ctx, uint8_t* output);
550
558QSC_EXPORT_API void qsc_hmac512_initialize(qsc_hmac512_state* ctx, const uint8_t* key, size_t keylen);
559
569QSC_EXPORT_API void qsc_hmac512_update(qsc_hmac512_state* ctx, const uint8_t* message, size_t msglen);
570
571/* HKDF */
572
583QSC_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);
584
595QSC_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);
596
607QSC_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);
608
619QSC_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);
620
621QSC_CPLUSPLUS_ENABLED_END
622
623#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:605
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:1464
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:1663
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:1611
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:1731
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:1494
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:168
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:1549
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:1441
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:1451
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:1561
#define QSC_SHA2_256_RATE
The SHA2-256 absorption rate in bytes.
Definition sha2.h:156
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:1579
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:1621
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:1690
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:162
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:1476
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:1536
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:1526
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:174
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:386
qsc_sha256_state pstate
Definition sha2.h:387
uint8_t ipad[QSC_SHA2_256_RATE]
Definition sha2.h:388
uint8_t opad[QSC_SHA2_256_RATE]
Definition sha2.h:389
The HMAC(SHA2-384) state array.
Definition sha2.h:450
qsc_sha384_state pstate
Definition sha2.h:451
uint8_t opad[QSC_SHA2_384_RATE]
Definition sha2.h:453
uint8_t ipad[QSC_SHA2_384_RATE]
Definition sha2.h:452
The HMAC(SHA2-512) state array.
Definition sha2.h:514
uint8_t opad[QSC_SHA2_512_RATE]
Definition sha2.h:517
uint8_t ipad[QSC_SHA2_512_RATE]
Definition sha2.h:516
qsc_sha512_state pstate
Definition sha2.h:515
The SHA2-256 digest state array.
Definition sha2.h:183
uint32_t state[QSC_SHA2_STATE_SIZE]
Definition sha2.h:184
uint64_t t
Definition sha2.h:186
size_t position
Definition sha2.h:187
uint8_t buffer[QSC_SHA2_256_RATE]
Definition sha2.h:185
The SHA2-384 digest state array.
Definition sha2.h:253
uint8_t buffer[QSC_SHA2_384_RATE]
Definition sha2.h:256
size_t position
Definition sha2.h:257
uint64_t state[QSC_SHA2_STATE_SIZE]
Definition sha2.h:254
uint64_t t[2U]
Definition sha2.h:255
The SHA2-512 digest state array.
Definition sha2.h:314
size_t position
Definition sha2.h:318
uint64_t state[QSC_SHA2_STATE_SIZE]
Definition sha2.h:315
uint64_t t[2U]
Definition sha2.h:316
uint8_t buffer[QSC_SHA2_512_RATE]
Definition sha2.h:317