QSC Post Quantum Cryptographic Library 1.1.0.2 (B2)
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__)
160# define QSC_SYSTEM_COMPILER_GCC
161#endif
162
163#if defined(__clang__)
168# define QSC_SYSTEM_COMPILER_CLANG
169#endif
170
171#if defined(__IBMC__) || defined(__IBMCPP__)
176# define QSC_SYSTEM_COMPILER_IBM
177#endif
178
179#if defined(__INTEL_COMPILER) || defined(__ICL)
184# define QSC_SYSTEM_COMPILER_INTEL
185#endif
186
187#if defined(__MWERKS__)
192# define QSC_SYSTEM_COMPILER_MWERKS
193#endif
194
195#if defined(__OPEN64__)
200# define QSC_SYSTEM_COMPILER_OPEN64
201#endif
202
203#if defined(__SUNPRO_C)
208# define QSC_SYSTEM_COMPILER_SUNPRO
209#endif
210
211#if defined(__TURBOC__)
216# define QSC_SYSTEM_COMPILER_TURBO
217#endif
218
219/*==============================================================================
220 Operating System Identification Macros
221==============================================================================*/
222
223#if defined(_WIN64) || defined(_WIN32) || defined(__WIN64__) || defined(__WIN32__)
228# if !defined(QSC_SYSTEM_OS_WINDOWS)
229# define QSC_SYSTEM_OS_WINDOWS
230# endif
231# if defined(_WIN64)
236# define QSC_SYSTEM_ISWIN64
237# elif defined(_WIN32)
242# define QSC_SYSTEM_ISWIN32
243# endif
244#else
245 typedef int errno_t;
246#endif
247
248#if defined(__x86_64__) || defined(__i386__) || defined(_M_IX86) || defined(_M_X64)
249# define QSC_HAS_CPUID
250#endif
251
252#if defined(__ANDROID__)
257# define QSC_SYSTEM_OS_ANDROID
258#endif
259
260#if defined(__APPLE__) || defined(__MACH__)
261# if defined(__MACH__)
266# define QSC_SYSTEM_OS_MAC
267# endif
272# define QSC_SYSTEM_OS_BSD
273
274# if defined(TARGET_OS_IPHONE) && defined(TARGET_IPHONE_SIMULATOR)
279# define QSC_SYSTEM_ISIPHONESIM
280# elif defined(TARGET_OS_IPHONE)
285# define QSC_SYSTEM_ISIPHONE
286# else
291# define QSC_SYSTEM_ISOSX
292# endif
293#endif
294
295#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__) || defined(__DragonFly__) || defined(QSC_SYSTEM_ISOSX)
300# define QSC_SYSTEM_OS_BSD
301#endif
302
303#if defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
308# define QSC_SYSTEM_OS_LINUX
309#endif
310
311#if defined(__unix) || defined(__unix__)
316# define QSC_SYSTEM_OS_UNIX
317# if defined(__hpux) || defined(hpux)
322# define QSC_SYSTEM_OS_HPUX
323# endif
324# if defined(__sun__) || defined(__sun) || defined(sun)
329# define QSC_SYSTEM_OS_SUNUX
330# endif
331#endif
332
333#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)
338# define QSC_SYSTEM_OS_POSIX
339
340#endif
341
342#if defined(QSC_SYSTEM_OS_WINDOWS) && defined(QSC_SYSTEM_COMPILER_MSC)
347# define QSC_WINDOWS_VSTUDIO_BUILD
348#endif
349
350#if defined(_OPENMP)
355# define QSC_SYSTEM_OPENMP
356#endif
357
358#if defined(DEBUG) || defined(_DEBUG) || defined(__DEBUG__) || (defined(__GNUC__) && !defined(__OPTIMIZE__))
363# define QSC_DEBUG_MODE
364#endif
365
366#ifdef QSC_DEBUG_MODE
371# define QSC_ASSERT(expr) assert(expr)
372#else
373# define QSC_ASSERT(expr) ((void)0)
374#endif
375
376/*==============================================================================
377 CPU Architecture Identification Macros
378==============================================================================*/
379
380#if defined(__OpenBSD__) || defined(__FreeBSD__) || \
381 defined(__NetBSD__) || defined(__APPLE__) || \
382 (defined(__GLIBC__) && (__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 25))
383# define QSC_HAVE_EXPLICIT_BZERO 1
384#endif
385
386#if defined(QSC_SYSTEM_COMPILER_MSC)
387# if defined(_M_X64) || defined(_M_AMD64)
392# define QSC_SYSTEM_ARCH_IX86_64
397# define QSC_SYSTEM_ARCH_IX86
398# if defined(_M_AMD64)
403# define QSC_SYSTEM_ARCH_AMD64
404# endif
405# elif defined(_M_IX86) || defined(_X86_)
410# define QSC_SYSTEM_ARCH_IX86_32
415# define QSC_SYSTEM_ARCH_IX86
416# elif defined(_M_ARM)
421# define QSC_SYSTEM_ARCH_ARM
422# if defined(_M_ARM_ARMV7VE)
427# define QSC_SYSTEM_ARCH_ARMV7VE
428# elif defined(_M_ARM_FP)
433# define QSC_SYSTEM_ARCH_ARMFP
434# elif defined(_M_ARM64)
439# define QSC_SYSTEM_ARCH_ARM64
440# endif
441# elif defined(_M_IA64)
446# define QSC_SYSTEM_ARCH_IA64
447# endif
448#elif defined(QSC_SYSTEM_COMPILER_GCC)
449# if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64)
454# define QSC_SYSTEM_ARCH_IX86_64
459# define QSC_SYSTEM_ARCH_IX86
460# if defined(__amd64__) || defined(__amd64)
465# define QSC_SYSTEM_ARCH_AMD64
466# endif
467# elif defined(i386) || defined(__i386) || defined(__i386__)
472# define QSC_SYSTEM_ARCH_IX86_32
477# define QSC_SYSTEM_ARCH_IX86
478# elif defined(__arm__) || defined(__aarch64__)
483# define QSC_SYSTEM_ARCH_ARM
484# if defined(__aarch64__)
489# define QSC_SYSTEM_ARCH_ARM64
490# endif
491# elif defined(__ia64) || defined(__ia64__) || defined(__itanium__)
496# define QSC_SYSTEM_ARCH_IA64
497# elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || defined(__64BIT__) || defined(_LP64) || defined(__LP64__)
502# define QSC_SYSTEM_ARCH_PPC
503# elif defined(__sparc) || defined(__sparc__)
508# define QSC_SYSTEM_ARCH_SPARC
509# if defined(__sparc64__)
514# define QSC_SYSTEM_ARCH_SPARC64
515# endif
516# endif
517#endif
518
519#if (defined(__x86_64__) || defined(_M_X64))
520# define QSC_SYSTEM_X86
521#endif
522
523/*==============================================================================
524 Sockets and Other System Macros
525==============================================================================*/
526
531#define QSC_PRAGMA_STR(x) _Pragma(#x)
532
537#if defined(_MSC_VER)
538# define QSC_NO_INLINE __declspec(noinline)
539#elif defined(__GNUC__) || defined(__clang__)
540# define QSC_NO_INLINE __attribute__((noinline))
541#else
542# define QSC_NO_INLINE
543#endif
544
545#if defined(_WIN64) || defined(_WIN32) || defined(__CYGWIN__)
550# define QSC_SYSTEM_SOCKETS_WINDOWS
551#else
556# define QSC_SYSTEM_SOCKETS_BERKELEY
557#endif
558
559#if defined(__GNUC__) || defined(__clang__)
560# define QSC_ATTRIBUTE __attribute__
561#else
562# define QSC_ATTRIBUTE(a)
563#endif
564
565#if defined(_DLL)
570# define QSC_DLL_API
571#endif
572
577#if defined(QSC_DLL_API)
578
579#if defined(QSC_SYSTEM_COMPILER_MSC)
580# if defined(QSC_DLL_IMPORT)
581# define QSC_EXPORT_API __declspec(dllimport)
582# else
583# define QSC_EXPORT_API __declspec(dllexport)
584# endif
585#elif defined(QSC_SYSTEM_COMPILER_GCC)
586# if defined(QSC_DLL_IMPORT)
587# define QSC_EXPORT_API QSC_ATTRIBUTE((dllimport))
588# else
589# define QSC_EXPORT_API QSC_ATTRIBUTE((dllexport))
590# endif
591#else
592# if defined(__SUNPRO_C)
593# if !defined(__GNU_C__)
594# define QSC_EXPORT_API QSC_ATTRIBUTE (visibility(__global))
595# else
596# define QSC_EXPORT_API QSC_ATTRIBUTE __global
597# endif
598# elif defined(_MSC_VER)
599# define QSC_EXPORT_API extern __declspec(dllexport)
600# else
601# define QSC_EXPORT_API QSC_ATTRIBUTE ((visibility ("default")))
602# endif
603#endif
604#else
605# define QSC_EXPORT_API
606#endif
607
612#if defined(__GNUC__)
613# define QSC_CACHE_ALIGNED QSC_ATTRIBUTE((aligned(64)))
614#elif defined(_MSC_VER)
615# define QSC_CACHE_ALIGNED __declspec(align(64U))
616#endif
617
618#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)
623# define QSC_SYSTEM_IS_X64
624#else
629# define QSC_SYSTEM_IS_X86
630#endif
631
632#if defined(QSC_SYSTEM_IS_X64)
637# define QSC_SIZE_MAX UINT64_MAX
638#else
643# define QSC_SIZE_MAX UINT32_MAX
644#endif
645
650#if !defined(__BIG_ENDIAN__)
651# if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
652# define QSC_SYSTEM_IS_LITTLE_ENDIAN 1U
653# elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
654# define QSC_SYSTEM_IS_LITTLE_ENDIAN 0U
655# elif defined(_WIN32) || defined(__LITTLE_ENDIAN__)
656# define QSC_SYSTEM_IS_LITTLE_ENDIAN 1U
657# endif
658#endif
659
660#if (!defined(QSC_SYSTEM_IS_LITTLE_ENDIAN))
661# if defined(__sparc) || defined(__sparc__) || defined(__hppa__) || defined(__PPC__) || defined(__mips__) || (defined(__MWERKS__) && !defined(__INTEL__))
666# define QSC_SYSTEM_IS_BIG_ENDIAN
667# else
672# define QSC_SYSTEM_IS_LITTLE_ENDIAN
673# endif
674#endif
675
676#if defined(__SIZEOF_INT128__) && defined(QSC_SYSTEM_IS_X64) && !defined(__xlc__) && !defined(uint128_t)
681# define QSC_SYSTEM_NATIVE_UINT128
682# if defined(__GNUC__)
687 typedef unsigned int uint128_t QSC_ATTRIBUTE((mode(TI)));
688# else
689 typedef __int128 uint128_t;
690# endif
691#endif
692
697#if defined(QSC_SYSTEM_NATIVE_UINT128)
698# define QSC_SYSTEM_FAST_64X64_MUL(X,Y,Low,High) \
699 do { \
700 const uint128_t r = (uint128_t)(X) * (Y); \
701 *(High) = (r >> 64) & 0xFFFFFFFFFFFFFFFFULL; \
702 *(Low) = (r) & 0xFFFFFFFFFFFFFFFFULL; \
703 } while(0U)
704#elif defined(QSC_SYSTEM_COMPILER_MSC) && defined(QSC_SYSTEM_IS_X64)
705# include <intrin.h>
706# pragma intrinsic(_umul128)
707# define QSC_SYSTEM_FAST_64X64_MUL(X,Y,Low,High) \
708 do { \
709 *(Low) = _umul128((X), (Y), (High)); \
710 } while(0U)
711#elif defined(QSC_SYSTEM_COMPILER_GCC)
712# if defined(QSC_SYSTEM_ARCH_IX86)
713# define QSC_SYSTEM_FAST_64X64_MUL(X,Y,Low,High) \
714 do { \
715 asm("mulq %3" : "=d" (*(High)), "=a" (*(Low)) : "a" (X), "rm" (Y) : "cc"); \
716 } while(0U)
717# elif defined(QSC_SYSTEM_ARCH_ALPHA)
718# define QSC_SYSTEM_FAST_64X64_MUL(X,Y,Low,High) \
719 do { \
720 asm("umulh %1,%2,%0" : "=r" (*(High)) : "r" (X), "r" (Y)); \
721 *(Low) = (X) * (Y); \
722 } while(0U)
723# elif defined(QSC_SYSTEM_ARCH_IA64)
724# define QSC_SYSTEM_FAST_64X64_MUL(X,Y,Low,High) \
725 do { \
726 asm("xmpy.hu %0=%1,%2" : "=f" (*(High)) : "f" (X), "f" (Y)); \
727 *(Low) = (X) * (Y); \
728 } while(0U)
729# elif defined(QSC_SYSTEM_ARCH_PPC)
730# define QSC_SYSTEM_FAST_64X64_MUL(X,Y,Low,High) \
731 do { \
732 asm("mulhdu %0,%1,%2" : "=r" (*(High)) : "r" (X), "r" (Y) : "cc"); \
733 *(Low) = (X) * (Y); \
734 } while(0U)
735# endif
736#endif
737
742#define QSC_SYSTEM_MAX_NAME 260ULL
743
748#define QSC_SYSTEM_MAX_PATH 260ULL
749
754#define QSC_SYSTEM_SECMEMALLOC_DEFAULT 4096ULL
755
760#define QSC_SYSTEM_SECMEMALLOC_MIN 16ULL
761
766#define QSC_SYSTEM_SECMEMALLOC_MAX 131072ULL
767
772#define QSC_SYSTEM_SECMEMALLOC_MAXKB 512ULL
773
774#if defined(_WIN32)
779# define QSC_SYSTEM_VIRTUAL_LOCK
780
785# define QSC_RTL_SECURE_MEMORY
786#endif
787
788#if defined(_POSIX_MEMLOCK_RANGE)
793# define QSC_SYSTEM_POSIX_MLOCK
794#endif
795
796#if defined(QSC_SYSTEM_VIRTUAL_LOCK) || defined(QSC_SYSTEM_POSIX_MLOCK)
801# define QSC_SYSTEM_SECURE_ALLOCATOR
802#endif
803
808#if defined(QSC_SYSTEM_COMPILER_MSC)
809# define QSC_SYSTEM_OPTIMIZE_IGNORE __pragma(optimize("", off))
810#elif defined(QSC_SYSTEM_COMPILER_GCC) || defined(QSC_SYSTEM_COMPILER_MINGW)
811# if defined(__clang__)
812# define QSC_SYSTEM_OPTIMIZE_IGNORE QSC_ATTRIBUTE((optnone))
813# else
814# define QSC_SYSTEM_OPTIMIZE_IGNORE QSC_ATTRIBUTE((optimize("O0")))
815# endif
816#elif defined(QSC_SYSTEM_COMPILER_INTEL)
817# define QSC_SYSTEM_OPTIMIZE_IGNORE _Pragma("optimize(\"\", off)")
818#else
819# define QSC_SYSTEM_OPTIMIZE_IGNORE
820#endif
821
826#if defined(QSC_SYSTEM_COMPILER_MSC)
827# define QSC_SYSTEM_OPTIMIZE_RESUME __pragma(optimize("", on))
828#elif defined(QSC_SYSTEM_COMPILER_GCC) || defined(QSC_SYSTEM_COMPILER_MINGW)
829# if defined(__clang__)
830# define QSC_SYSTEM_OPTIMIZE_RESUME
831# else
832# define QSC_SYSTEM_OPTIMIZE_RESUME
833# endif
834#elif defined(QSC_SYSTEM_COMPILER_INTEL)
835# define QSC_SYSTEM_OPTIMIZE_RESUME _Pragma("optimize(\"\", on)")
836#else
837# define QSC_SYSTEM_OPTIMIZE_RESUME
838#endif
839
844#if defined(QSC_SYSTEM_COMPILER_MSC)
845# define QSC_SYSTEM_CONDITION_IGNORE(x) __pragma(warning(disable : x))
846#elif defined(QSC_SYSTEM_COMPILER_GCC) || defined(QSC_SYSTEM_COMPILER_MINGW)
847# define QSC_SYSTEM_CONDITION_IGNORE(x)
848#elif defined(QSC_SYSTEM_COMPILER_INTEL)
849# define QSC_SYSTEM_CONDITION_IGNORE(x)
850#else
851# define QSC_SYSTEM_CONDITION_IGNORE(x)
852#endif
853
858#if defined(QSC_SYSTEM_COMPILER_MSC)
859# define QSC_SYSTEM_CONDITION_RESUME(x) __pragma(warning(default : x))
860#elif defined(QSC_SYSTEM_COMPILER_GCC) || defined(QSC_SYSTEM_COMPILER_MINGW)
861# define QSC_SYSTEM_CONDITION_RESUME(x)
862#elif defined(QSC_SYSTEM_COMPILER_INTEL)
863# define QSC_SYSTEM_CONDITION_RESUME(x)
864#else
865# define QSC_SYSTEM_CONDITION_RESUME(x)
866#endif
867
868#if defined(_MSC_VER) && (_MSC_VER >= 1600)
873# define QSC_WMMINTRIN_H 1
874#endif
875
876#if defined(_MSC_VER) && (_MSC_VER >= 1700) && defined(_M_X64)
881# define QSC_HAVE_AVX2INTRIN_H 1
882#endif
883
884/*==============================================================================
885 AVX512 Capabilities
886==============================================================================*/
887
888#if defined(QSC_SYSTEM_X86)
889
890# if defined(__AVX512F__) && (__AVX512F__ == 1)
895# include <immintrin.h>
896# if (!defined(__AVX512__))
897# define __AVX512__
898# endif
899# endif
900
901# if defined(__SSE2__)
906# define QSC_SYSTEM_HAS_SSE2
907# endif
908
909# if defined(__SSE3__)
914# define QSC_SYSTEM_HAS_SSE3
915# endif
916
917# if defined(__SSSE3__)
922# define QSC_SYSTEM_HAS_SSSE3
923# endif
924
925# if defined(__SSE4_1__)
930# define QSC_SYSTEM_HAS_SSE41
931# endif
932
933# if defined(__SSE4_2__)
938# define QSC_SYSTEM_HAS_SSE42
939# endif
940
941# if defined(__ARM_NEON) || defined(__ARM_NEON__)
946# define QSC_SYSTEM_HAS_ARM_NEON
947# endif
948
954# if defined(__ARM_FEATURE_SVE)
955# define QSC_SYSTEM_HAS_ARM_SVE
956# endif
957
963# if defined(__riscv_vector)
964# define QSC_SYSTEM_HAS_RVV
965# endif
966
967# if defined(__AVX__)
972# define QSC_SYSTEM_HAS_AVX
973# endif
974
975# if defined(__AVX2__)
980# define QSC_SYSTEM_HAS_AVX2
981# endif
982
983# if defined(__AVX512__)
988# define QSC_SYSTEM_HAS_AVX512
989# endif
990
991# if defined(__XOP__)
996# define QSC_SYSTEM_HAS_XOP
997#endif
998
999# if defined(QSC_SYSTEM_HAS_AVX) || defined(QSC_SYSTEM_HAS_AVX2) || defined(QSC_SYSTEM_HAS_AVX512)
1004# define QSC_SYSTEM_AVX_INTRINSICS
1005# endif
1006#endif
1007
1008/*==============================================================================
1009 Assembly and SIMD Alignment Macros
1010==============================================================================*/
1011
1016/*#define QSC_ASM_ENABLED */
1017
1022//#define QSC_MISRA_FULL_COMPLIANCE
1023
1024#if defined(QSC_SYSTEM_AVX_INTRINSICS) && defined(QSC_SYSTEM_COMPILER_GCC) && defined(QSC_ASM_ENABLED)
1025 // #define QSC_GCC_ASM_ENABLED /* Uncomment to enable GCC ASM processing */
1026#endif
1027
1032#if !defined(QSC_ALIGN)
1033 /* If compiling in C23 or later, use the built-in 'alignas' keyword. */
1034 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202311L)
1035 #define QSC_ALIGN(x) alignas(x)
1036# elif defined(_MSC_VER)
1037# define QSC_ALIGN(x) __declspec(align(x))
1038# elif defined(__GNUC__) || defined(__clang__)
1039# define QSC_ALIGN(x) __attribute__((aligned(x)))
1040# else
1041# define QSC_ALIGN(x)
1042# endif
1043#endif
1044
1049#if defined(QSC_SYSTEM_HAS_AVX512)
1050# define QSC_SIMD_ALIGNMENT 64
1051#elif defined(QSC_SYSTEM_HAS_AVX2)
1052# define QSC_SIMD_ALIGNMENT 32
1053#elif defined(QSC_SYSTEM_HAS_AVX)
1054# define QSC_SIMD_ALIGNMENT 16
1055#else
1056# define QSC_SIMD_ALIGNMENT 8
1057#endif
1058
1063#if defined(_MSC_VER)
1064# define QSC_SIMD_ALIGN __declspec(align(QSC_SIMD_ALIGNMENT))
1065#elif defined(__GNUC__) || defined(__clang__)
1066# define QSC_SIMD_ALIGN _Alignas(QSC_SIMD_ALIGNMENT)
1067#else
1068# define QSC_SIMD_ALIGN
1069#endif
1070
1071#if defined(QSC_SYSTEM_AVX_INTRINSICS)
1076# define QSC_RDRAND_COMPATIBLE
1077#endif
1078
1083#define QSC_STATUS_SUCCESS 0LL
1084
1089#define QSC_STATUS_FAILURE -1LL
1090
1091/*==============================================================================
1092 User Modifiable Values and Cryptographic Parameter Sets
1093==============================================================================*/
1094
1095#if !defined(QSC_SYSTEM_AESNI_ENABLED)
1096# if defined(QSC_SYSTEM_AVX_INTRINSICS)
1101# define QSC_SYSTEM_AESNI_ENABLED
1102# endif
1103#endif
1104
1109#if defined(__SHA__) || defined(__SHA256__) || defined(__SHA512__) || defined(__ISA_AVAILABLE_SHA)
1110# define QSC_SHA2_SHANI_ENABLED
1111#endif
1112
1114// * \def QSC_KECCAK_UNROLLED_PERMUTATION
1115// * \brief Define to use the unrolled form of the Keccak permutation function.
1116// */
1117//#define QSC_KECCAK_UNROLLED_PERMUTATION
1118
1119/*** Asymmetric Ciphers ***/
1120
1121/*** EDDH ***/
1122
1150#define QSC_EDDH_RFC_7748_COMPLIANT
1151
1156#define QSC_EDDH_S1EC25519
1157
1158/*** ML-KEM Kyber ***/
1159
1161// * \def QSC_KYBER_S1K2P512
1162// * \brief Enable the Kyber S1K2P512 parameter set.
1163// */
1164//#define QSC_KYBER_S1K2P512
1165
1167// * \def QSC_KYBER_S3K3P768
1168// * \brief Enable the Kyber S3K3P768 parameter set.
1169// */
1170//#define QSC_KYBER_S3K3P768
1171
1176#define QSC_KYBER_S5K4P1024
1177
1179// * \def QSC_KYBER_S6K5P1280
1180// * \brief Enable the Kyber S6K5P1280 parameter set (experimental).
1181// */
1182//#define QSC_KYBER_S6K5P1280
1183
1184/*** HQC ***/
1185
1187// * \def QSC_HQC_S1N2321
1188// * \brief Enable the HQC S1N17669 parameter set.
1189// */
1190//#define QSC_HQC_S1N2321
1191
1193// * \def QSC_HQC_S3N4602
1194// * \brief Enable the HQCS3N35851 parameter set.
1195// */
1196//#define QSC_HQC_S3N4602
1197
1202#define QSC_HQC_S5N7333
1203
1204/*** McEliece ***/
1205
1207// * \def QSC_MCELIECE_S1N3488T64
1208// * \brief Enable the McEliece S1-N3488T64 parameter set.
1209// */
1210//#define QSC_MCELIECE_S1N3488T64
1211
1213// * \def QSC_MCELIECE_S3N4608T96
1214// * \brief Enable the McEliece S3-N4608T96 parameter set.
1215// */
1216//#define QSC_MCELIECE_S3N4608T96
1217
1222#define QSC_MCELIECE_S5N6688T128
1223
1225// * \def QSC_MCELIECE_S6N6960T119
1226// * \brief Enable the McEliece S6-N6960T119 parameter set.
1227// */
1228//#define QSC_MCELIECE_S6N6960T119
1229
1231// * \def QSC_MCELIECE_S7N8192T128
1232// * \brief Enable the McEliece S7-N8192T128 parameter set.
1233// */
1234//#define QSC_MCELIECE_S7N8192T128
1235
1236/*** Signature Schemes ***/
1237
1239// * \def QSC_DILITHIUM_S1P44
1240// * \brief Enable the Dilithium S1P44 parameter set.
1241// */
1242//#define QSC_DILITHIUM_S1P44
1243
1245// * \def QSC_DILITHIUM_S3P65
1246// * \brief Enable the Dilithium S3P65 parameter set.
1247// */
1248//#define QSC_DILITHIUM_S3P65
1249
1254#define QSC_DILITHIUM_S5P87
1255
1256 /*** ECDSA ***/
1257
1259// * \def QSC_ECDSA_S1P256
1260// * \brief Enable the ECDSA S1EC256 (NIST P-256) parameter set.
1261// */
1262//#define QSC_ECDSA_S1P256
1263
1265// * \def QSC_ECDSA_S3P384
1266// * \brief Enable the ECDSA S1P384 (NIST P-384) parameter set.
1267// */
1268//#define QSC_ECDSA_S3P384
1269
1274#define QSC_ECDSA_S5P521
1275
1276/*** EDDSA ***/
1277
1282#define QSC_EDDSA_S1EC25519
1283
1284 /*** Falcon ***/
1285
1287// * \def QSC_FALCON_S3SHAKE256F512
1288// * \brief Enable the Falcon S3SHAKE256F512 parameter set.
1289// */
1290//#define QSC_FALCON_S3SHAKE256F512
1291
1296#define QSC_FALCON_S5SHAKE256F1024
1297
1298/*** SphincsPlus ***/
1299
1301// * \def QSC_SPHINCSPLUS_S1S128SHAKERS
1302// * \brief Enable the SphincsPlus S1S128SHAKERS robust small parameter set.
1303// */
1304//#define QSC_SPHINCSPLUS_S1S128SHAKERS
1305
1307// * \def QSC_SPHINCSPLUS_S3S192SHAKERS
1308// * \brief Enable the SphincsPlus S3S192SHAKERS robust small parameter set.
1309// */
1310//#define QSC_SPHINCSPLUS_S3S192SHAKERS
1311
1316#define QSC_SPHINCSPLUS_S5S256SHAKERS
1317
1319// * \def QSC_SPHINCSPLUS_S6S512SHAKERS
1320// * \brief Enable the SphincsPlus S6S512SHAKERS robust small parameter set.
1321// */
1322//#define QSC_SPHINCSPLUS_S6S512SHAKERS
1323
1324QSC_CPLUSPLUS_ENABLED_END
1325
1326#endif