QSC Post Quantum Cryptographic Library 1.3.0.0 (C1)
A post quantum secure library written in Ansi C
Loading...
Searching...
No Matches
qsccommon.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_COMMON_H
53#define QSC_COMMON_H
54
55#if defined(__posix) || defined(__posix__) || defined(__USE_POSIX) || defined(_POSIX_VERSION) || defined(__MACH__) || \
56 defined(__linux) || defined(__linux__) || defined(__gnu_linux__) || defined(__unix) || defined(__unix__) || \
57 defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__) || defined(__DragonFly__)
58# if defined(__linux__)
59# if !defined(_DEFAULT_SOURCE)
60# define _DEFAULT_SOURCE
61# endif
62# if !defined(_XOPEN_SOURCE)
63# define _XOPEN_SOURCE 700
64# endif
65# if !defined(_GNU_SOURCE)
66# define _GNU_SOURCE
67# endif
68# elif defined(__APPLE__) && defined(__MACH__)
69# if !defined(_DARWIN_C_SOURCE)
70# define _DARWIN_C_SOURCE
71# endif
72# elif defined(__sun) && defined(__SVR4)
73# if !defined(__EXTENSIONS__)
74# define __EXTENSIONS__
75# endif
76# elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
77# if !defined(_BSD_SOURCE)
78# define _BSD_SOURCE
79# endif
80#endif
81
82# if !defined(_POSIX_C_SOURCE)
83# define _POSIX_C_SOURCE 200809L
84# endif
85#endif
86
87#include <assert.h>
88#include <errno.h>
89#include <limits.h>
90#include <stdbool.h>
91#include <stddef.h>
92#include <stdint.h>
93
94#if defined(__cplusplus)
95# define QSC_CPLUSPLUS_ENABLED_START extern "C" {
96# define QSC_CPLUSPLUS_ENABLED_END }
97#else
98# define QSC_CPLUSPLUS_ENABLED_START
99# define QSC_CPLUSPLUS_ENABLED_END
100#endif
101
102QSC_CPLUSPLUS_ENABLED_START
103
113
114/*==============================================================================
115 Compiler Identification Macros
116==============================================================================*/
117
118#if defined(_MSC_VER)
123# define QSC_SYSTEM_COMPILER_MSC
124#endif
125
126#if defined(__MINGW32__)
131# define QSC_SYSTEM_COMPILER_MINGW
136# define QSC_SYSTEM_COMPILER_GCC
137#endif
138
139#if defined(__CC_ARM)
144# define QSC_SYSTEM_COMPILER_ARM
145#endif
146
147#if defined(__BORLANDC__)
152# define QSC_SYSTEM_COMPILER_BORLAND
153#endif
154
155#if defined(__GNUC__) && !defined(__MINGW32__)
165# define QSC_SYSTEM_COMPILER_GCC
166#endif
167
168#if defined(__clang__)
176# define QSC_SYSTEM_COMPILER_CLANG
177#endif
178
179#if defined(__IBMC__) || defined(__IBMCPP__)
184# define QSC_SYSTEM_COMPILER_IBM
185#endif
186
187#if defined(__INTEL_COMPILER) || defined(__ICL)
192# define QSC_SYSTEM_COMPILER_INTEL
193#endif
194
195#if defined(__MWERKS__)
200# define QSC_SYSTEM_COMPILER_MWERKS
201#endif
202
203#if defined(__OPEN64__)
208# define QSC_SYSTEM_COMPILER_OPEN64
209#endif
210
211#if defined(__SUNPRO_C)
216# define QSC_SYSTEM_COMPILER_SUNPRO
217#endif
218
219#if defined(__TURBOC__)
224# define QSC_SYSTEM_COMPILER_TURBO
225#endif
226
227/*==============================================================================
228 Operating System Identification Macros
229==============================================================================*/
230
231#if defined(_WIN64) || defined(_WIN32) || defined(__WIN64__) || defined(__WIN32__)
236# if !defined(QSC_SYSTEM_OS_WINDOWS)
237# define QSC_SYSTEM_OS_WINDOWS
238# endif
239# if defined(_WIN64)
244# define QSC_SYSTEM_ISWIN64
245# elif defined(_WIN32)
250# define QSC_SYSTEM_ISWIN32
251# endif
252#else
253 typedef int32_t errno_t;
254#endif
255
256#if defined(__x86_64__) || defined(__i386__) || defined(_M_IX86) || defined(_M_X64)
257# define QSC_HAS_CPUID
258#endif
259
260#if defined(__ANDROID__)
265# define QSC_SYSTEM_OS_ANDROID
266#endif
267
268#if defined(__APPLE__) || defined(__MACH__)
269# if defined(__MACH__)
274# define QSC_SYSTEM_OS_MAC
275# endif
280# define QSC_SYSTEM_OS_BSD
281
282# if defined(TARGET_OS_IPHONE) && defined(TARGET_IPHONE_SIMULATOR)
287# define QSC_SYSTEM_ISIPHONESIM
288# elif defined(TARGET_OS_IPHONE)
293# define QSC_SYSTEM_ISIPHONE
294# else
299# define QSC_SYSTEM_ISOSX
300# endif
301#endif
302
303#if defined(__FreeBSD__)
308# define QSC_SYSTEM_OS_FREEBSD
309#endif
310
311#if defined(__OpenBSD__)
316# define QSC_SYSTEM_OS_OPENBSD
317#endif
318
319#if defined(__NetBSD__)
324# define QSC_SYSTEM_OS_NETBSD
325#endif
326
327#if defined(__DragonFly__)
332# define QSC_SYSTEM_OS_DRAGONFLY
333#endif
334
335#if defined(QSC_SYSTEM_OS_FREEBSD) || defined(QSC_SYSTEM_OS_OPENBSD) || defined(QSC_SYSTEM_OS_NETBSD) || defined(QSC_SYSTEM_OS_DRAGONFLY) || defined(__bsdi__) || defined(QSC_SYSTEM_ISOSX)
340# define QSC_SYSTEM_OS_BSD
341#endif
342
343#if defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
348# define QSC_SYSTEM_OS_LINUX
349#endif
350
351#if defined(__unix) || defined(__unix__)
356# define QSC_SYSTEM_OS_UNIX
357# if defined(__hpux) || defined(hpux)
362# define QSC_SYSTEM_OS_HPUX
363# endif
364# if defined(__sun__) || defined(__sun) || defined(sun)
369# define QSC_SYSTEM_OS_SUNUX
370# endif
371#endif
372
373#if defined(__posix) || defined(__posix__) || defined(__USE_POSIX) || defined(_POSIX_VERSION) || defined(QSC_SYSTEM_OS_UNIX) || defined(QSC_SYSTEM_OS_LINUX) || defined(QSC_SYSTEM_OS_BSD)
378# define QSC_SYSTEM_OS_POSIX
379
380#endif
381
382#if defined(QSC_SYSTEM_OS_WINDOWS) && defined(QSC_SYSTEM_COMPILER_MSC)
387# define QSC_WINDOWS_VSTUDIO_BUILD
388#endif
389
390#if defined(_OPENMP)
395# define QSC_SYSTEM_OPENMP
396#endif
397
398#if defined(DEBUG) || defined(_DEBUG) || defined(__DEBUG__) || (defined(__GNUC__) && !defined(__OPTIMIZE__))
403# define QSC_DEBUG_MODE
404#endif
405
406#ifdef QSC_DEBUG_MODE
411# define QSC_ASSERT(expr) assert(expr)
412#else
413# define QSC_ASSERT(expr) ((void)0)
414#endif
415
416/*==============================================================================
417 CPU Architecture Identification Macros
418==============================================================================*/
419
420#if defined(__OpenBSD__) || defined(__FreeBSD__) || \
421 defined(__NetBSD__) || defined(__APPLE__) || \
422 (defined(__GLIBC__) && (__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 25))
423# define QSC_HAVE_EXPLICIT_BZERO 1
424#endif
425
426#if defined(QSC_SYSTEM_COMPILER_MSC)
427# if defined(_M_X64) || defined(_M_AMD64)
432# define QSC_SYSTEM_ARCH_IX86_64
437# define QSC_SYSTEM_ARCH_IX86
438# if defined(_M_AMD64)
443# define QSC_SYSTEM_ARCH_AMD64
444# endif
445# elif defined(_M_IX86) || defined(_X86_)
450# define QSC_SYSTEM_ARCH_IX86_32
455# define QSC_SYSTEM_ARCH_IX86
456# elif defined(_M_ARM)
461# define QSC_SYSTEM_ARCH_ARM
462# if defined(_M_ARM_ARMV7VE)
467# define QSC_SYSTEM_ARCH_ARMV7VE
468# elif defined(_M_ARM_FP)
473# define QSC_SYSTEM_ARCH_ARMFP
474# elif defined(_M_ARM64)
479# define QSC_SYSTEM_ARCH_ARM64
480# endif
481# elif defined(_M_IA64)
486# define QSC_SYSTEM_ARCH_IA64
487# endif
488#elif defined(QSC_SYSTEM_COMPILER_GCC)
489# if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64)
494# define QSC_SYSTEM_ARCH_IX86_64
499# define QSC_SYSTEM_ARCH_IX86
500# if defined(__amd64__) || defined(__amd64)
505# define QSC_SYSTEM_ARCH_AMD64
506# endif
507# elif defined(i386) || defined(__i386) || defined(__i386__)
512# define QSC_SYSTEM_ARCH_IX86_32
517# define QSC_SYSTEM_ARCH_IX86
518# elif defined(__arm__) || defined(__aarch64__)
523# define QSC_SYSTEM_ARCH_ARM
524# if defined(__aarch64__)
529# define QSC_SYSTEM_ARCH_ARM64
530# endif
531# elif defined(__ia64) || defined(__ia64__) || defined(__itanium__)
536# define QSC_SYSTEM_ARCH_IA64
537# elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || defined(__64BIT__) || defined(_LP64) || defined(__LP64__)
542# define QSC_SYSTEM_ARCH_PPC
543# elif defined(__sparc) || defined(__sparc__)
548# define QSC_SYSTEM_ARCH_SPARC
549# if defined(__sparc64__)
554# define QSC_SYSTEM_ARCH_SPARC64
555# endif
556# endif
557#endif
558
559#if (defined(__x86_64__) || defined(_M_X64))
560# define QSC_SYSTEM_X86
561#endif
562
563/*==============================================================================
564 Sockets and Other System Macros
565==============================================================================*/
566
571#define QSC_PRAGMA_STR(x) _Pragma(#x)
572
577#if defined(_MSC_VER)
578# define QSC_NO_INLINE __declspec(noinline)
579#elif defined(__GNUC__) || defined(__clang__)
580# define QSC_NO_INLINE __attribute__((noinline))
581#else
582# define QSC_NO_INLINE
583#endif
584
585#if defined(_WIN64) || defined(_WIN32) || defined(__CYGWIN__)
590# define QSC_SYSTEM_SOCKETS_WINDOWS
591#else
596# define QSC_SYSTEM_SOCKETS_BERKELEY
597#endif
598
599#if defined(__GNUC__) || defined(__clang__)
600# define QSC_ATTRIBUTE __attribute__
601#else
602# define QSC_ATTRIBUTE(a)
603#endif
604
605#if defined(_DLL)
610# define QSC_DLL_API
611#endif
612
617#if defined(QSC_DLL_API)
618
619#if defined(QSC_SYSTEM_COMPILER_MSC)
620# if defined(QSC_DLL_IMPORT)
621# define QSC_EXPORT_API __declspec(dllimport)
622# else
623# define QSC_EXPORT_API __declspec(dllexport)
624# endif
625#elif defined(QSC_SYSTEM_COMPILER_GCC)
626# if defined(QSC_DLL_IMPORT)
627# define QSC_EXPORT_API QSC_ATTRIBUTE((dllimport))
628# else
629# define QSC_EXPORT_API QSC_ATTRIBUTE((dllexport))
630# endif
631#else
632# if defined(__SUNPRO_C)
633# if !defined(__GNU_C__)
634# define QSC_EXPORT_API QSC_ATTRIBUTE (visibility(__global))
635# else
636# define QSC_EXPORT_API QSC_ATTRIBUTE __global
637# endif
638# elif defined(_MSC_VER)
639# define QSC_EXPORT_API extern __declspec(dllexport)
640# else
641# define QSC_EXPORT_API QSC_ATTRIBUTE ((visibility ("default")))
642# endif
643#endif
644#else
645# define QSC_EXPORT_API
646#endif
647
652#if defined(__GNUC__)
653# define QSC_CACHE_ALIGNED QSC_ATTRIBUTE((aligned(64)))
654#elif defined(_MSC_VER)
655# define QSC_CACHE_ALIGNED __declspec(align(64U))
656#endif
657
658#if defined(QSC_SYSTEM_ARCH_IX86_64) || defined(QSC_SYSTEM_ARCH_ARM64) || defined(QSC_SYSTEM_ARCH_IA64) || defined(QSC_SYSTEM_ARCH_AMD64) || defined(QSC_SYSTEM_ARCH_SPARC64)
663# define QSC_SYSTEM_IS_X64
664#else
669# define QSC_SYSTEM_IS_X86
670#endif
671
672#if defined(QSC_SYSTEM_IS_X64)
677# define QSC_SIZE_MAX UINT64_MAX
678#else
683# define QSC_SIZE_MAX UINT32_MAX
684#endif
685
690#if !defined(__BIG_ENDIAN__)
691# if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
692# define QSC_SYSTEM_IS_LITTLE_ENDIAN 1U
693# elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
694# define QSC_SYSTEM_IS_LITTLE_ENDIAN 0U
695# elif defined(_WIN32) || defined(__LITTLE_ENDIAN__)
696# define QSC_SYSTEM_IS_LITTLE_ENDIAN 1U
697# elif defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || defined(__AARCH64EL__) || defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
698# define QSC_SYSTEM_IS_LITTLE_ENDIAN 1U
699# endif
700#endif
701
702#if (!defined(QSC_SYSTEM_IS_LITTLE_ENDIAN))
703# if defined(__sparc) || defined(__sparc__) || defined(__hppa__) || defined(__PPC__) || defined(__mips__) || (defined(__MWERKS__) && !defined(__INTEL__))
708# define QSC_SYSTEM_IS_BIG_ENDIAN
709# else
714# define QSC_SYSTEM_IS_LITTLE_ENDIAN
715# endif
716#endif
717
722#define QSC_SYSTEM_MAX_NAME 260ULL
723
728#define QSC_SYSTEM_MAX_PATH 260ULL
729
734#define QSC_SYSTEM_SECMEMALLOC_DEFAULT 4096ULL
735
740#define QSC_SYSTEM_SECMEMALLOC_MIN 16ULL
741
746#define QSC_SYSTEM_SECMEMALLOC_MAX 131072ULL
747
752#define QSC_SYSTEM_SECMEMALLOC_MAXKB 512ULL
753
754#if defined(_WIN32)
759# define QSC_SYSTEM_VIRTUAL_LOCK
760
765# define QSC_RTL_SECURE_MEMORY
766#endif
767
768#if defined(QSC_SYSTEM_OS_LINUX) || defined(QSC_SYSTEM_OS_MAC) || defined(QSC_SYSTEM_OS_BSD)
777# define QSC_SYSTEM_POSIX_MLOCK
778#endif
779
780#if defined(QSC_SYSTEM_VIRTUAL_LOCK) || defined(QSC_SYSTEM_POSIX_MLOCK)
785# define QSC_SYSTEM_SECURE_ALLOCATOR
786#endif
787
792#if defined(QSC_SYSTEM_COMPILER_MSC)
793# define QSC_SYSTEM_OPTIMIZE_IGNORE __pragma(optimize("", off))
794#elif defined(QSC_SYSTEM_COMPILER_GCC) || defined(QSC_SYSTEM_COMPILER_MINGW)
795# if defined(__clang__)
796# define QSC_SYSTEM_OPTIMIZE_IGNORE QSC_ATTRIBUTE((optnone))
797# else
798# define QSC_SYSTEM_OPTIMIZE_IGNORE QSC_ATTRIBUTE((optimize("O0")))
799# endif
800#elif defined(QSC_SYSTEM_COMPILER_INTEL)
801# define QSC_SYSTEM_OPTIMIZE_IGNORE _Pragma("optimize(\"\", off)")
802#else
803# define QSC_SYSTEM_OPTIMIZE_IGNORE
804#endif
805
810#if defined(QSC_SYSTEM_COMPILER_MSC)
811# define QSC_SYSTEM_OPTIMIZE_RESUME __pragma(optimize("", on))
812#elif defined(QSC_SYSTEM_COMPILER_GCC) || defined(QSC_SYSTEM_COMPILER_MINGW)
813# if defined(__clang__)
814# define QSC_SYSTEM_OPTIMIZE_RESUME
815# else
816# define QSC_SYSTEM_OPTIMIZE_RESUME
817# endif
818#elif defined(QSC_SYSTEM_COMPILER_INTEL)
819# define QSC_SYSTEM_OPTIMIZE_RESUME _Pragma("optimize(\"\", on)")
820#else
821# define QSC_SYSTEM_OPTIMIZE_RESUME
822#endif
823
828#if defined(QSC_SYSTEM_COMPILER_MSC)
829# define QSC_SYSTEM_CONDITION_IGNORE(x) __pragma(warning(disable : x))
830#elif defined(QSC_SYSTEM_COMPILER_GCC) || defined(QSC_SYSTEM_COMPILER_MINGW)
831# define QSC_SYSTEM_CONDITION_IGNORE(x)
832#elif defined(QSC_SYSTEM_COMPILER_INTEL)
833# define QSC_SYSTEM_CONDITION_IGNORE(x)
834#else
835# define QSC_SYSTEM_CONDITION_IGNORE(x)
836#endif
837
842#if defined(QSC_SYSTEM_COMPILER_MSC)
843# define QSC_SYSTEM_CONDITION_RESUME(x) __pragma(warning(default : x))
844#elif defined(QSC_SYSTEM_COMPILER_GCC) || defined(QSC_SYSTEM_COMPILER_MINGW)
845# define QSC_SYSTEM_CONDITION_RESUME(x)
846#elif defined(QSC_SYSTEM_COMPILER_INTEL)
847# define QSC_SYSTEM_CONDITION_RESUME(x)
848#else
849# define QSC_SYSTEM_CONDITION_RESUME(x)
850#endif
851
852#if defined(_MSC_VER) && (_MSC_VER >= 1600)
857# define QSC_WMMINTRIN_H 1
858#endif
859
860#if defined(_MSC_VER) && (_MSC_VER >= 1700) && defined(_M_X64)
865# define QSC_HAVE_AVX2INTRIN_H 1
866#endif
867
868/*==============================================================================
869 AVX512 Capabilities
870==============================================================================*/
871
872#if defined(QSC_SYSTEM_X86)
873
874# if defined(__AVX512F__) && (__AVX512F__ == 1)
879# include <immintrin.h>
880# if (!defined(__AVX512__))
881# define __AVX512__
882# endif
883# endif
884
885# if defined(__SSE2__)
890# define QSC_SYSTEM_HAS_SSE2
891# endif
892
893# if defined(__SSE3__)
898# define QSC_SYSTEM_HAS_SSE3
899# endif
900
901# if defined(__SSSE3__)
906# define QSC_SYSTEM_HAS_SSSE3
907# endif
908
909# if defined(__SSE4_1__)
914# define QSC_SYSTEM_HAS_SSE41
915# endif
916
917# if defined(__SSE4_2__)
922# define QSC_SYSTEM_HAS_SSE42
923# endif
924
925# if defined(__ARM_NEON) || defined(__ARM_NEON__)
930# define QSC_SYSTEM_HAS_ARM_NEON
931# endif
932
938# if defined(__ARM_FEATURE_SVE)
939# define QSC_SYSTEM_HAS_ARM_SVE
940# endif
941
947# if defined(__riscv_vector)
948# define QSC_SYSTEM_HAS_RVV
949# endif
950
951# if defined(__AVX__)
956# define QSC_SYSTEM_HAS_AVX
957# endif
958
959# if defined(__AVX2__)
964# define QSC_SYSTEM_HAS_AVX2
965# endif
966
967# if defined(__AVX512__)
972# define QSC_SYSTEM_HAS_AVX512
973# endif
974
975# if defined(__XOP__)
980# define QSC_SYSTEM_HAS_XOP
981#endif
982
983# if defined(QSC_SYSTEM_HAS_AVX) || defined(QSC_SYSTEM_HAS_AVX2) || defined(QSC_SYSTEM_HAS_AVX512)
988# define QSC_SYSTEM_AVX_INTRINSICS
989# endif
990#endif
991
992/*==============================================================================
993 Assembly and SIMD Alignment Macros
994==============================================================================*/
995
1000/*#define QSC_ASM_ENABLED */
1001
1006//#define QSC_MISRA_FULL_COMPLIANCE
1007
1008#if defined(QSC_SYSTEM_AVX_INTRINSICS) && defined(QSC_SYSTEM_COMPILER_GCC) && defined(QSC_ASM_ENABLED)
1009 // #define QSC_GCC_ASM_ENABLED /* Uncomment to enable GCC ASM processing */
1010#endif
1011
1016#if !defined(QSC_ALIGN)
1017 /* If compiling in C23 or later, use the built-in 'alignas' keyword. */
1018 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202311L)
1019 #define QSC_ALIGN(x) alignas(x)
1020# elif defined(_MSC_VER)
1021# define QSC_ALIGN(x) __declspec(align(x))
1022# elif defined(__GNUC__) || defined(__clang__)
1023# define QSC_ALIGN(x) __attribute__((aligned(x)))
1024# else
1025# define QSC_ALIGN(x)
1026# endif
1027#endif
1028
1033#if defined(QSC_SYSTEM_HAS_AVX512)
1034# define QSC_SIMD_ALIGNMENT 64
1035#elif defined(QSC_SYSTEM_HAS_AVX2)
1036# define QSC_SIMD_ALIGNMENT 32
1037#elif defined(QSC_SYSTEM_HAS_AVX)
1038# define QSC_SIMD_ALIGNMENT 16
1039#else
1040# define QSC_SIMD_ALIGNMENT 8
1041#endif
1042
1047#if defined(_MSC_VER)
1048# define QSC_SIMD_ALIGN __declspec(align(QSC_SIMD_ALIGNMENT))
1049#elif defined(__GNUC__) || defined(__clang__)
1050# define QSC_SIMD_ALIGN _Alignas(QSC_SIMD_ALIGNMENT)
1051#else
1052# define QSC_SIMD_ALIGN
1053#endif
1054
1055#if defined(QSC_SYSTEM_AVX_INTRINSICS)
1060# define QSC_RDRAND_COMPATIBLE
1061#endif
1062
1067#define QSC_STATUS_SUCCESS 0LL
1068
1073#define QSC_STATUS_FAILURE -1LL
1074
1075/*==============================================================================
1076 User Modifiable Values and Cryptographic Parameter Sets
1077==============================================================================*/
1078
1079#if !defined(QSC_SYSTEM_AESNI_ENABLED)
1080# if defined(QSC_SYSTEM_AVX_INTRINSICS)
1085# define QSC_SYSTEM_AESNI_ENABLED
1086# endif
1087#endif
1088
1105#if defined(QSC_SYSTEM_ARCH_IX86)
1106# if ((defined(__SHA__) || defined(__SHA256__)) && defined(__SSSE3__) && defined(__SSE4_1__)) || \
1107 (defined(__ISA_AVAILABLE_SHA) && defined(QSC_SYSTEM_AVX_INTRINSICS))
1108# define QSC_SHA2_SHANI_ENABLED
1109# endif
1110#endif
1111
1113// * \def QSC_KECCAK_UNROLLED_PERMUTATION
1114// * \brief Define to use the unrolled form of the Keccak permutation function.
1115// */
1116//#define QSC_KECCAK_UNROLLED_PERMUTATION
1117
1139#if !defined(QSC_TLS_NO_DEFAULT_SECURITY_CLASS) && !defined(QSC_TLS_SECURITY_CLASS_1) && !defined(QSC_TLS_SECURITY_CLASS_3) && !defined(QSC_TLS_SECURITY_CLASS_5)
1140# define QSC_TLS_SECURITY_CLASS_1
1141#endif
1142
1164//#define QSC_TLS_SECURITY_CLASS_3
1165
1193//#define QSC_TLS_SECURITY_CLASS_5
1194
1195#if defined(QSC_TLS_SECURITY_CLASS_1)
1196
1201# define QSC_EDDH_S1EC25519
1202
1207# define QSC_KYBER_S3K3P768
1208
1213# define QSC_DILITHIUM_S3P65
1214
1219# define QSC_ECDSA_S1P256
1220
1225# define QSC_EDDSA_S1EC25519
1226
1227#elif defined(QSC_TLS_SECURITY_CLASS_3)
1228
1233# define QSC_EDDH_S1EC25519
1234
1239# define QSC_KYBER_S3K3P768
1240
1245# define QSC_DILITHIUM_S3P65
1246
1251# define QSC_ECDSA_S1P256
1252
1257# define QSC_EDDSA_S1EC25519
1258
1259#elif defined(QSC_TLS_SECURITY_CLASS_5)
1260
1265# define QSC_KYBER_S5K4P1024
1266
1271# define QSC_DILITHIUM_S5P87
1272
1273#endif
1274
1275/*** Asymmetric Ciphers ***/
1276
1277/*** ECDH ***/
1278
1279#if !defined(QSC_ECDH_S1P256) && !defined(QSC_ECDH_S3P384) && !defined(QSC_ECDH_S5P521)
1284#define QSC_ECDH_S1P256
1285#endif
1286
1287#if !defined(QSC_ECDH_S1P256) && !defined(QSC_ECDH_S3P384) && !defined(QSC_ECDH_S5P521)
1292#define QSC_ECDH_S3P384
1293#endif
1294
1295#if !defined(QSC_ECDH_S1P256) && !defined(QSC_ECDH_S3P384) && !defined(QSC_ECDH_S5P521)
1300#define QSC_ECDH_S5P521
1301#endif
1302
1303/*** EDDH ***/
1304
1305#if !defined(QSC_EDDH_S1EC25519) && !defined(QSC_EDDH_S3EC448)
1310#define QSC_EDDH_S1EC25519
1311#endif
1312
1313#if !defined(QSC_EDDH_S1EC25519) && !defined(QSC_EDDH_S3EC448)
1318#define QSC_EDDH_S3EC448
1319#endif
1320
1321/*** ML-KEM Kyber ***/
1322
1323#if !defined(QSC_KYBER_S1K2P512) && !defined(QSC_KYBER_S3K3P768) && !defined(QSC_KYBER_S5K4P1024) && !defined(QSC_KYBER_S6K5P1280)
1328#define QSC_KYBER_S1K2P512
1329#endif
1330
1331#if !defined(QSC_KYBER_S1K2P512) && !defined(QSC_KYBER_S3K3P768) && !defined(QSC_KYBER_S5K4P1024) && !defined(QSC_KYBER_S6K5P1280)
1336#define QSC_KYBER_S3K3P768
1337#endif
1338
1339#if !defined(QSC_KYBER_S1K2P512) && !defined(QSC_KYBER_S3K3P768) && !defined(QSC_KYBER_S5K4P1024) && !defined(QSC_KYBER_S6K5P1280)
1344#define QSC_KYBER_S5K4P1024
1345#endif
1346
1347#if !defined(QSC_KYBER_S1K2P512) && !defined(QSC_KYBER_S3K3P768) && !defined(QSC_KYBER_S5K4P1024)
1352#define QSC_KYBER_S6K5P1280
1353#endif
1354
1355/*** HQC ***/
1356
1357#if !defined(QSC_HQC_S3N4602) && !defined(QSC_HQC_S5N7333)
1362#define QSC_HQC_S1N2321
1363#endif
1364
1365#if !defined(QSC_HQC_S1N2321) && !defined(QSC_HQC_S5N7333)
1370#define QSC_HQC_S3N4602
1371#endif
1372
1373#if !defined(QSC_HQC_S1N2321) && !defined(QSC_HQC_S3N4602)
1378#define QSC_HQC_S5N7333
1379#endif
1380
1381/*** McEliece ***/
1382
1383#if !defined(QSC_MCELIECE_S3N4608T96) && !defined(QSC_MCELIECE_S5N6688T128) && !defined(QSC_MCELIECE_S6N6960T119) && !defined(QSC_MCELIECE_S7N8192T128)
1388#define QSC_MCELIECE_S1N3488T64
1389#endif
1390
1391#if !defined(QSC_MCELIECE_S1N3488T64) && !defined(QSC_MCELIECE_S5N6688T128) && !defined(QSC_MCELIECE_S6N6960T119) && !defined(QSC_MCELIECE_S7N8192T128)
1396#define QSC_MCELIECE_S3N4608T96
1397#endif
1398
1399#if !defined(QSC_MCELIECE_S1N3488T64) && !defined(QSC_MCELIECE_S3N4608T96) && !defined(QSC_MCELIECE_S6N6960T119) && !defined(QSC_MCELIECE_S7N8192T128)
1404#define QSC_MCELIECE_S5N6688T128
1405#endif
1406
1407#if !defined(QSC_MCELIECE_S1N3488T64) && !defined(QSC_MCELIECE_S3N4608T96) && !defined(QSC_MCELIECE_S5N6688T128) && !defined(QSC_MCELIECE_S7N8192T128)
1412#define QSC_MCELIECE_S6N6960T119
1413#endif
1414
1415#if !defined(QSC_MCELIECE_S1N3488T64) && !defined(QSC_MCELIECE_S3N4608T96) && !defined(QSC_MCELIECE_S5N6688T128) && !defined(QSC_MCELIECE_S6N6960T119)
1420#define QSC_MCELIECE_S7N8192T128
1421#endif
1422
1423/*** Signature Schemes ***/
1424
1425#if !defined(QSC_DILITHIUM_S1P44) && !defined(QSC_DILITHIUM_S3P65) && !defined(QSC_DILITHIUM_S5P87)
1430#define QSC_DILITHIUM_S1P44
1431#endif
1432
1433#if !defined(QSC_DILITHIUM_S1P44) && !defined(QSC_DILITHIUM_S3P65) && !defined(QSC_DILITHIUM_S5P87)
1438#define QSC_DILITHIUM_S3P65
1439#endif
1440
1441#if !defined(QSC_DILITHIUM_S1P44) && !defined(QSC_DILITHIUM_S3P65) && !defined(QSC_DILITHIUM_S5P87)
1446#define QSC_DILITHIUM_S5P87
1447#endif
1448
1449 /*** ECDSA ***/
1450
1451#if !defined(QSC_ECDSA_S1P256) && !defined(QSC_ECDSA_S3P384) && !defined(QSC_ECDSA_S5P521)
1456#define QSC_ECDSA_S1P256
1457#endif
1458
1459#if !defined(QSC_ECDSA_S1P256) && !defined(QSC_ECDSA_S3P384) && !defined(QSC_ECDSA_S5P521)
1464#define QSC_ECDSA_S3P384
1465#endif
1466
1467#if !defined(QSC_ECDSA_S1P256) && !defined(QSC_ECDSA_S3P384) && !defined(QSC_ECDSA_S5P521)
1472#define QSC_ECDSA_S5P521
1473#endif
1474
1475/*** EDDSA ***/
1476
1477#if !defined(QSC_EDDSA_S1EC25519) && !defined(QSC_EDDSA_S3EC448)
1482#define QSC_EDDSA_S1EC25519
1483#endif
1484
1485#if !defined(QSC_EDDSA_S1EC25519) && !defined(QSC_EDDSA_S3EC448)
1490#define QSC_EDDSA_S3EC448
1491#endif
1492
1493 /*** Falcon ***/
1494
1495#if !defined(QSC_FALCON_S5SHAKE256F1024)
1500#define QSC_FALCON_S3SHAKE256F512
1501#endif
1502
1503#if !defined(QSC_FALCON_S3SHAKE256F512)
1508#define QSC_FALCON_S5SHAKE256F1024
1509#endif
1510
1511/*** SphincsPlus ***/
1512
1513#if !defined(QSC_SPHINCSPLUS_S3S192SHAKERS) && !defined(QSC_SPHINCSPLUS_S5S256SHAKERS) && !defined(QSC_SPHINCSPLUS_S6S512SHAKERS)
1518#define QSC_SPHINCSPLUS_S1S128SHAKERS
1519#endif
1520
1521#if !defined(QSC_SPHINCSPLUS_S1S128SHAKERS) && !defined(QSC_SPHINCSPLUS_S5S256SHAKERS) && !defined(QSC_SPHINCSPLUS_S6S512SHAKERS)
1526#define QSC_SPHINCSPLUS_S3S192SHAKERS
1527#endif
1528
1529#if !defined(QSC_SPHINCSPLUS_S1S128SHAKERS) && !defined(QSC_SPHINCSPLUS_S3S192SHAKERS) && !defined(QSC_SPHINCSPLUS_S6S512SHAKERS)
1534#define QSC_SPHINCSPLUS_S5S256SHAKERS
1535#endif
1536
1537#if !defined(QSC_SPHINCSPLUS_S1S128SHAKERS) && !defined(QSC_SPHINCSPLUS_S3S192SHAKERS) && !defined(QSC_SPHINCSPLUS_S5S256SHAKERS)
1542#define QSC_SPHINCSPLUS_S6S512SHAKERS
1543#endif
1544
1545QSC_CPLUSPLUS_ENABLED_END
1546
1547#endif