QSC Post Quantum Cryptographic Library 1.0.0.6c (A6)
A post quantum secure library written in Ansi C
 
Loading...
Searching...
No Matches
sha2.h
Go to the documentation of this file.
1/* 2025 Quantum Resistant Cryptographic Solutions Corporation
2 * All Rights Reserved.
3 *
4 * NOTICE: This software and all accompanying materials are the exclusive
5 * property of Quantum Resistant Cryptographic Solutions Corporation (QRCS).
6 * The intellectual and technical concepts contained within this implementation
7 * are proprietary to QRCS and its authorized licensors and are protected under
8 * applicable U.S. and international copyright, patent, and trade secret laws.
9 *
10 * CRYPTOGRAPHIC STANDARDS:
11 * - This software includes implementations of cryptographic algorithms such as
12 * SHA3, AES, and others. These algorithms are public domain or standardized
13 * by organizations such as NIST and are NOT the property of QRCS.
14 * - However, all source code, optimizations, and implementations in this library
15 * are original works of QRCS and are protected under this license.
16 *
17 * RESTRICTIONS:
18 * - Redistribution, modification, or unauthorized distribution of this software,
19 * in whole or in part, is strictly prohibited.
20 * - This software is provided for non-commercial, educational, and research
21 * purposes only. Commercial use in any form is expressly forbidden.
22 * - Licensing and authorized distribution are solely at the discretion of QRCS.
23 * - Any use of this software implies acceptance of these restrictions.
24 *
25 * DISCLAIMER:
26 * This software is provided "as is," without warranty of any kind, express or
27 * implied, including but not limited to warranties of merchantability or fitness
28 * for a particular purpose. QRCS disclaims all liability for any direct, indirect,
29 * incidental, or consequential damages resulting from the use or misuse of this software.
30 *
31 * FULL LICENSE:
32 * This software is subject to the **Quantum Resistant Cryptographic Solutions
33 * Proprietary License (QRCS-PL)**. The complete license terms are included
34 * in the LICENSE.txt file distributed with this software.
35 *
36 * Written by: John Underhill
37 * Contact: john.underhill@protonmail.com
38 */
39
40#ifndef QSC_SHA2_H
41#define QSC_SHA2_H
42
43#include "common.h"
44
45QSC_CPLUSPLUS_ENABLED_START
46
73
74#if !defined(QSC_SHA2_SHANI_ENABLED)
75//#define QSC_SHA2_SHANI_ENABLED
76#endif
77
82#define QSC_HKDF_256_KEY_SIZE 32ULL
83
88#define QSC_HKDF_512_KEY_SIZE 64ULL
89
94#define QSC_HMAC_256_KEY_SIZE 32ULL
95
100#define QSC_HMAC_512_KEY_SIZE 64ULL
101
106#define QSC_HMAC_256_MAC_SIZE 32ULL
107
112#define QSC_HMAC_512_MAC_SIZE 64ULL
113
118#define QSC_HMAC_256_RATE 64ULL
119
124#define QSC_HMAC_512_RATE 128ULL
125
130#define QSC_SHA2_256_HASH_SIZE 32ULL
131
136#define QSC_SHA2_384_HASH_SIZE 48ULL
137
142#define QSC_SHA2_512_HASH_SIZE 64ULL
143
148#define QSC_SHA2_256_RATE 64ULL
149
154#define QSC_SHA2_384_RATE 128ULL
155
160#define QSC_SHA2_512_RATE 128ULL
161
166#define QSC_SHA2_STATE_SIZE 8ULL
167
168/* SHA2-256 */
169
174QSC_EXPORT_API typedef struct
175{
178 uint64_t t;
179 size_t position;
181
189QSC_EXPORT_API void qsc_sha256_compute(uint8_t* output, const uint8_t* message, size_t msglen);
190
197
207QSC_EXPORT_API void qsc_sha256_finalize(qsc_sha256_state* ctx, uint8_t* output);
208
215
225QSC_EXPORT_API void qsc_sha256_permute(uint32_t* output, const uint8_t* input);
226
236QSC_EXPORT_API void qsc_sha256_update(qsc_sha256_state* ctx, const uint8_t* message, size_t msglen);
237
238/* SHA2-384 */
239
244QSC_EXPORT_API typedef struct
245{
247 uint64_t t[2];
249 size_t position;
251
261QSC_EXPORT_API void qsc_sha384_compute(uint8_t* output, const uint8_t* message, size_t msglen);
262
269
279QSC_EXPORT_API void qsc_sha384_finalize(qsc_sha384_state* ctx, uint8_t* output);
280
287
297QSC_EXPORT_API void qsc_sha384_update(qsc_sha384_state* ctx, const uint8_t* message, size_t msglen);
298
299/* SHA2-512 */
300
305QSC_EXPORT_API typedef struct
306{
308 uint64_t t[2];
310 size_t position;
312
322QSC_EXPORT_API void qsc_sha512_compute(uint8_t* output, const uint8_t* message, size_t msglen);
323
330
340QSC_EXPORT_API void qsc_sha512_finalize(qsc_sha512_state* ctx, uint8_t* output);
341
348
358QSC_EXPORT_API void qsc_sha512_permute(uint64_t* output, const uint8_t* input);
359
369QSC_EXPORT_API void qsc_sha512_update(qsc_sha512_state* ctx, const uint8_t* message, size_t msglen);
370
371/* HMAC-256 */
372
383
395QSC_EXPORT_API void qsc_hmac256_compute(uint8_t* output, const uint8_t* message, size_t msglen, const uint8_t* key, size_t keylen);
396
403
413QSC_EXPORT_API void qsc_hmac256_finalize(qsc_hmac256_state* ctx, uint8_t* output);
414
422QSC_EXPORT_API void qsc_hmac256_initialize(qsc_hmac256_state* ctx, const uint8_t* key, size_t keylen);
423
433QSC_EXPORT_API void qsc_hmac256_update(qsc_hmac256_state* ctx, const uint8_t* message, size_t msglen);
434
435/* HMAC-512 */
436
447
459QSC_EXPORT_API void qsc_hmac512_compute(uint8_t* output, const uint8_t* message, size_t msglen, const uint8_t* key, size_t keylen);
460
467
477QSC_EXPORT_API void qsc_hmac512_finalize(qsc_hmac512_state* ctx, uint8_t* output);
478
486QSC_EXPORT_API void qsc_hmac512_initialize(qsc_hmac512_state* ctx, const uint8_t* key, size_t keylen);
487
497QSC_EXPORT_API void qsc_hmac512_update(qsc_hmac512_state* ctx, const uint8_t* message, size_t msglen);
498
499/* HKDF */
500
511QSC_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);
512
523QSC_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);
524
535QSC_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);
536
547QSC_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);
548
549QSC_CPLUSPLUS_ENABLED_END
550
551#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 common.h:520
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:58
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:766
QSC_EXPORT_API void qsc_sha512_initialize(qsc_sha512_state *ctx)
Initialize a SHA2-512 state structure.
Definition sha2.c:912
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:1539
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:705
QSC_EXPORT_API void qsc_sha384_dispose(qsc_sha384_state *ctx)
Dispose of the SHA2-384 state.
Definition sha2.c:690
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:1384
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:1494
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:1600
QSC_EXPORT_API void qsc_sha256_initialize(qsc_sha256_state *ctx)
Initialize a SHA2-256 state structure.
Definition sha2.c:108
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:609
#define QSC_SHA2_512_RATE
The SHA2-512 absorption rate in bytes.
Definition sha2.h:160
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:834
QSC_EXPORT_API void qsc_hmac512_dispose(qsc_hmac512_state *ctx)
Dispose of the HMAC-512 state.
Definition sha2.c:1437
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:1413
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:1369
QSC_EXPORT_API void qsc_sha384_initialize(qsc_sha384_state *ctx)
Initialize a SHA2-384 state structure.
Definition sha2.c:755
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:1450
#define QSC_SHA2_256_RATE
The SHA2-256 absorption rate in bytes.
Definition sha2.h:148
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:1465
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:1342
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:1504
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:1565
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:46
QSC_EXPORT_API void qsc_sha256_dispose(qsc_sha256_state *ctx)
Dispose of the SHA2-256 state.
Definition sha2.c:33
QSC_EXPORT_API void qsc_sha512_permute(uint64_t *output, const uint8_t *input)
The SHA2-512 permutation function.
Definition sha2.c:923
QSC_EXPORT_API void qsc_hmac256_dispose(qsc_hmac256_state *ctx)
Dispose of the HMAC-256 state.
Definition sha2.c:1356
#define QSC_SHA2_384_RATE
The SHA2-384 absorption rate in bytes.
Definition sha2.h:154
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:862
QSC_EXPORT_API void qsc_sha256_permute(uint32_t *output, const uint8_t *input)
The SHA2-256 permutation function.
Definition sha2.c:297
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:1423
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:1299
QSC_EXPORT_API void qsc_sha512_dispose(qsc_sha512_state *ctx)
Dispose of the SHA2-512 state.
Definition sha2.c:847
#define QSC_SHA2_STATE_SIZE
The SHA2 state array size.
Definition sha2.h:166
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:677
The HMAC(SHA2-256) state array.
Definition sha2.h:378
qsc_sha256_state pstate
Definition sha2.h:379
uint8_t ipad[QSC_SHA2_256_RATE]
Definition sha2.h:380
uint8_t opad[QSC_SHA2_256_RATE]
Definition sha2.h:381
The HMAC(SHA2-512) state array.
Definition sha2.h:442
uint8_t opad[QSC_SHA2_512_RATE]
Definition sha2.h:445
uint8_t ipad[QSC_SHA2_512_RATE]
Definition sha2.h:444
qsc_sha512_state pstate
Definition sha2.h:443
The SHA2-256 digest state array.
Definition sha2.h:175
uint32_t state[QSC_SHA2_STATE_SIZE]
Definition sha2.h:176
uint64_t t
Definition sha2.h:178
size_t position
Definition sha2.h:179
uint8_t buffer[QSC_SHA2_256_RATE]
Definition sha2.h:177
The SHA2-384 digest state array.
Definition sha2.h:245
uint8_t buffer[QSC_SHA2_384_RATE]
Definition sha2.h:248
uint64_t t[2]
Definition sha2.h:247
size_t position
Definition sha2.h:249
uint64_t state[QSC_SHA2_STATE_SIZE]
Definition sha2.h:246
The SHA2-512 digest state array.
Definition sha2.h:306
uint64_t t[2]
Definition sha2.h:308
size_t position
Definition sha2.h:310
uint64_t state[QSC_SHA2_STATE_SIZE]
Definition sha2.h:307
uint8_t buffer[QSC_SHA2_512_RATE]
Definition sha2.h:309