QSC Post Quantum Cryptographic Library
1.2.0.0 (B1)
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
102
QSC_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
int32_t 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
# elif defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || defined(__AARCH64EL__) || defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
658
# define QSC_SYSTEM_IS_LITTLE_ENDIAN 1U
659
# endif
660
#endif
661
662
#if (!defined(QSC_SYSTEM_IS_LITTLE_ENDIAN))
663
# if defined(__sparc) || defined(__sparc__) || defined(__hppa__) || defined(__PPC__) || defined(__mips__) || (defined(__MWERKS__) && !defined(__INTEL__))
668
# define QSC_SYSTEM_IS_BIG_ENDIAN
669
# else
674
# define QSC_SYSTEM_IS_LITTLE_ENDIAN
675
# endif
676
#endif
677
682
#define QSC_SYSTEM_MAX_NAME 260ULL
683
688
#define QSC_SYSTEM_MAX_PATH 260ULL
689
694
#define QSC_SYSTEM_SECMEMALLOC_DEFAULT 4096ULL
695
700
#define QSC_SYSTEM_SECMEMALLOC_MIN 16ULL
701
706
#define QSC_SYSTEM_SECMEMALLOC_MAX 131072ULL
707
712
#define QSC_SYSTEM_SECMEMALLOC_MAXKB 512ULL
713
714
#if defined(_WIN32)
719
# define QSC_SYSTEM_VIRTUAL_LOCK
720
725
# define QSC_RTL_SECURE_MEMORY
726
#endif
727
728
#if defined(_POSIX_MEMLOCK_RANGE)
733
# define QSC_SYSTEM_POSIX_MLOCK
734
#endif
735
736
#if defined(QSC_SYSTEM_VIRTUAL_LOCK) || defined(QSC_SYSTEM_POSIX_MLOCK)
741
# define QSC_SYSTEM_SECURE_ALLOCATOR
742
#endif
743
748
#if defined(QSC_SYSTEM_COMPILER_MSC)
749
# define QSC_SYSTEM_OPTIMIZE_IGNORE __pragma(optimize("", off))
750
#elif defined(QSC_SYSTEM_COMPILER_GCC) || defined(QSC_SYSTEM_COMPILER_MINGW)
751
# if defined(__clang__)
752
# define QSC_SYSTEM_OPTIMIZE_IGNORE QSC_ATTRIBUTE((optnone))
753
# else
754
# define QSC_SYSTEM_OPTIMIZE_IGNORE QSC_ATTRIBUTE((optimize("O0")))
755
# endif
756
#elif defined(QSC_SYSTEM_COMPILER_INTEL)
757
# define QSC_SYSTEM_OPTIMIZE_IGNORE _Pragma("optimize(\"\", off)")
758
#else
759
# define QSC_SYSTEM_OPTIMIZE_IGNORE
760
#endif
761
766
#if defined(QSC_SYSTEM_COMPILER_MSC)
767
# define QSC_SYSTEM_OPTIMIZE_RESUME __pragma(optimize("", on))
768
#elif defined(QSC_SYSTEM_COMPILER_GCC) || defined(QSC_SYSTEM_COMPILER_MINGW)
769
# if defined(__clang__)
770
# define QSC_SYSTEM_OPTIMIZE_RESUME
771
# else
772
# define QSC_SYSTEM_OPTIMIZE_RESUME
773
# endif
774
#elif defined(QSC_SYSTEM_COMPILER_INTEL)
775
# define QSC_SYSTEM_OPTIMIZE_RESUME _Pragma("optimize(\"\", on)")
776
#else
777
# define QSC_SYSTEM_OPTIMIZE_RESUME
778
#endif
779
784
#if defined(QSC_SYSTEM_COMPILER_MSC)
785
# define QSC_SYSTEM_CONDITION_IGNORE(x) __pragma(warning(disable : x))
786
#elif defined(QSC_SYSTEM_COMPILER_GCC) || defined(QSC_SYSTEM_COMPILER_MINGW)
787
# define QSC_SYSTEM_CONDITION_IGNORE(x)
788
#elif defined(QSC_SYSTEM_COMPILER_INTEL)
789
# define QSC_SYSTEM_CONDITION_IGNORE(x)
790
#else
791
# define QSC_SYSTEM_CONDITION_IGNORE(x)
792
#endif
793
798
#if defined(QSC_SYSTEM_COMPILER_MSC)
799
# define QSC_SYSTEM_CONDITION_RESUME(x) __pragma(warning(default : x))
800
#elif defined(QSC_SYSTEM_COMPILER_GCC) || defined(QSC_SYSTEM_COMPILER_MINGW)
801
# define QSC_SYSTEM_CONDITION_RESUME(x)
802
#elif defined(QSC_SYSTEM_COMPILER_INTEL)
803
# define QSC_SYSTEM_CONDITION_RESUME(x)
804
#else
805
# define QSC_SYSTEM_CONDITION_RESUME(x)
806
#endif
807
808
#if defined(_MSC_VER) && (_MSC_VER >= 1600)
813
# define QSC_WMMINTRIN_H 1
814
#endif
815
816
#if defined(_MSC_VER) && (_MSC_VER >= 1700) && defined(_M_X64)
821
# define QSC_HAVE_AVX2INTRIN_H 1
822
#endif
823
824
/*==============================================================================
825
AVX512 Capabilities
826
==============================================================================*/
827
828
#if defined(QSC_SYSTEM_X86)
829
830
# if defined(__AVX512F__) && (__AVX512F__ == 1)
835
# include <immintrin.h>
836
# if (!defined(__AVX512__))
837
# define __AVX512__
838
# endif
839
# endif
840
841
# if defined(__SSE2__)
846
# define QSC_SYSTEM_HAS_SSE2
847
# endif
848
849
# if defined(__SSE3__)
854
# define QSC_SYSTEM_HAS_SSE3
855
# endif
856
857
# if defined(__SSSE3__)
862
# define QSC_SYSTEM_HAS_SSSE3
863
# endif
864
865
# if defined(__SSE4_1__)
870
# define QSC_SYSTEM_HAS_SSE41
871
# endif
872
873
# if defined(__SSE4_2__)
878
# define QSC_SYSTEM_HAS_SSE42
879
# endif
880
881
# if defined(__ARM_NEON) || defined(__ARM_NEON__)
886
# define QSC_SYSTEM_HAS_ARM_NEON
887
# endif
888
894
# if defined(__ARM_FEATURE_SVE)
895
# define QSC_SYSTEM_HAS_ARM_SVE
896
# endif
897
903
# if defined(__riscv_vector)
904
# define QSC_SYSTEM_HAS_RVV
905
# endif
906
907
# if defined(__AVX__)
912
# define QSC_SYSTEM_HAS_AVX
913
# endif
914
915
# if defined(__AVX2__)
920
# define QSC_SYSTEM_HAS_AVX2
921
# endif
922
923
# if defined(__AVX512__)
928
# define QSC_SYSTEM_HAS_AVX512
929
# endif
930
931
# if defined(__XOP__)
936
# define QSC_SYSTEM_HAS_XOP
937
#endif
938
939
# if defined(QSC_SYSTEM_HAS_AVX) || defined(QSC_SYSTEM_HAS_AVX2) || defined(QSC_SYSTEM_HAS_AVX512)
944
# define QSC_SYSTEM_AVX_INTRINSICS
945
# endif
946
#endif
947
948
/*==============================================================================
949
Assembly and SIMD Alignment Macros
950
==============================================================================*/
951
956
/*#define QSC_ASM_ENABLED */
957
962
//#define QSC_MISRA_FULL_COMPLIANCE
963
964
#if defined(QSC_SYSTEM_AVX_INTRINSICS) && defined(QSC_SYSTEM_COMPILER_GCC) && defined(QSC_ASM_ENABLED)
965
// #define QSC_GCC_ASM_ENABLED /* Uncomment to enable GCC ASM processing */
966
#endif
967
972
#if !defined(QSC_ALIGN)
973
/* If compiling in C23 or later, use the built-in 'alignas' keyword. */
974
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202311L)
975
#define QSC_ALIGN(x) alignas(x)
976
# elif defined(_MSC_VER)
977
# define QSC_ALIGN(x) __declspec(align(x))
978
# elif defined(__GNUC__) || defined(__clang__)
979
# define QSC_ALIGN(x) __attribute__((aligned(x)))
980
# else
981
# define QSC_ALIGN(x)
982
# endif
983
#endif
984
989
#if defined(QSC_SYSTEM_HAS_AVX512)
990
# define QSC_SIMD_ALIGNMENT 64
991
#elif defined(QSC_SYSTEM_HAS_AVX2)
992
# define QSC_SIMD_ALIGNMENT 32
993
#elif defined(QSC_SYSTEM_HAS_AVX)
994
# define QSC_SIMD_ALIGNMENT 16
995
#else
996
# define QSC_SIMD_ALIGNMENT 8
997
#endif
998
1003
#if defined(_MSC_VER)
1004
# define QSC_SIMD_ALIGN __declspec(align(QSC_SIMD_ALIGNMENT))
1005
#elif defined(__GNUC__) || defined(__clang__)
1006
# define QSC_SIMD_ALIGN _Alignas(QSC_SIMD_ALIGNMENT)
1007
#else
1008
# define QSC_SIMD_ALIGN
1009
#endif
1010
1011
#if defined(QSC_SYSTEM_AVX_INTRINSICS)
1016
# define QSC_RDRAND_COMPATIBLE
1017
#endif
1018
1023
#define QSC_STATUS_SUCCESS 0LL
1024
1029
#define QSC_STATUS_FAILURE -1LL
1030
1031
/*==============================================================================
1032
User Modifiable Values and Cryptographic Parameter Sets
1033
==============================================================================*/
1034
1035
#if !defined(QSC_SYSTEM_AESNI_ENABLED)
1036
# if defined(QSC_SYSTEM_AVX_INTRINSICS)
1041
# define QSC_SYSTEM_AESNI_ENABLED
1042
# endif
1043
#endif
1044
1049
#if defined(__SHA__) || defined(__SHA256__) || defined(__SHA512__) || defined(__ISA_AVAILABLE_SHA)
1050
# define QSC_SHA2_SHANI_ENABLED
1051
#endif
1052
1054
// * \def QSC_KECCAK_UNROLLED_PERMUTATION
1055
// * \brief Define to use the unrolled form of the Keccak permutation function.
1056
// */
1057
//#define QSC_KECCAK_UNROLLED_PERMUTATION
1058
1080
#define QSC_TLS_SECURITY_CLASS_1
1081
1103
//#define QSC_TLS_SECURITY_CLASS_3
1104
1132
//#define QSC_TLS_SECURITY_CLASS_5
1133
1134
#if defined(QSC_TLS_SECURITY_CLASS_1)
1135
1140
# define QSC_EDDH_S1EC25519
1141
1146
# define QSC_KYBER_S3K3P768
1147
1152
# define QSC_DILITHIUM_S3P65
1153
1158
# define QSC_ECDSA_S1P256
1159
1164
# define QSC_EDDSA_S1EC25519
1165
1166
#elif defined(QSC_TLS_SECURITY_CLASS_3)
1167
1172
# define QSC_EDDH_S1EC25519
1173
1178
# define QSC_KYBER_S3K3P768
1179
1184
# define QSC_DILITHIUM_S3P65
1185
1190
# define QSC_ECDSA_S1P256
1191
1196
# define QSC_EDDSA_S1EC25519
1197
1198
#elif defined(QSC_TLS_SECURITY_CLASS_5)
1199
1204
# define QSC_KYBER_S5K4P1024
1205
1210
# define QSC_DILITHIUM_S5P87
1211
1212
#endif
1213
1214
/*** Asymmetric Ciphers ***/
1215
1216
/*** ECDH ***/
1217
1218
#if !defined(QSC_ECDH_S1P256) && !defined(QSC_ECDH_S3P384) && !defined(QSC_ECDH_S5P521)
1223
#define QSC_ECDH_S1P256
1224
#endif
1225
1226
#if !defined(QSC_ECDH_S1P256) && !defined(QSC_ECDH_S3P384) && !defined(QSC_ECDH_S5P521)
1231
#define QSC_ECDH_S3P384
1232
#endif
1233
1234
#if !defined(QSC_ECDH_S1P256) && !defined(QSC_ECDH_S3P384) && !defined(QSC_ECDH_S5P521)
1239
#define QSC_ECDH_S5P521
1240
#endif
1241
1242
/*** EDDH ***/
1243
1244
#if !defined(QSC_EDDH_S1EC25519) && !defined(QSC_EDDH_S3EC448)
1249
#define QSC_EDDH_S1EC25519
1250
#endif
1251
1252
#if !defined(QSC_EDDH_S1EC25519) && !defined(QSC_EDDH_S3EC448)
1257
#define QSC_EDDH_S3EC448
1258
#endif
1259
1260
/*** ML-KEM Kyber ***/
1261
1262
#if !defined(QSC_KYBER_S1K2P512) && !defined(QSC_KYBER_S3K3P768) && !defined(QSC_KYBER_S5K4P1024) && !defined(QSC_KYBER_S6K5P1280)
1267
#define QSC_KYBER_S1K2P512
1268
#endif
1269
1270
#if !defined(QSC_KYBER_S1K2P512) && !defined(QSC_KYBER_S3K3P768) && !defined(QSC_KYBER_S5K4P1024) && !defined(QSC_KYBER_S6K5P1280)
1275
#define QSC_KYBER_S3K3P768
1276
#endif
1277
1278
#if !defined(QSC_KYBER_S1K2P512) && !defined(QSC_KYBER_S3K3P768) && !defined(QSC_KYBER_S5K4P1024) && !defined(QSC_KYBER_S6K5P1280)
1283
#define QSC_KYBER_S5K4P1024
1284
#endif
1285
1286
#if !defined(QSC_KYBER_S1K2P512) && !defined(QSC_KYBER_S3K3P768) && !defined(QSC_KYBER_S5K4P1024)
1291
#define QSC_KYBER_S6K5P1280
1292
#endif
1293
1294
/*** HQC ***/
1295
1296
#if !defined(QSC_HQC_S3N4602) && !defined(QSC_HQC_S5N7333)
1301
#define QSC_HQC_S1N2321
1302
#endif
1303
1304
#if !defined(QSC_HQC_S1N2321) && !defined(QSC_HQC_S5N7333)
1309
#define QSC_HQC_S3N4602
1310
#endif
1311
1312
#if !defined(QSC_HQC_S1N2321) && !defined(QSC_HQC_S3N4602)
1317
#define QSC_HQC_S5N7333
1318
#endif
1319
1320
/*** McEliece ***/
1321
1322
#if !defined(QSC_MCELIECE_S3N4608T96) && !defined(QSC_MCELIECE_S5N6688T128) && !defined(QSC_MCELIECE_S6N6960T119) && !defined(QSC_MCELIECE_S7N8192T128)
1327
#define QSC_MCELIECE_S1N3488T64
1328
#endif
1329
1330
#if !defined(QSC_MCELIECE_S1N3488T64) && !defined(QSC_MCELIECE_S5N6688T128) && !defined(QSC_MCELIECE_S6N6960T119) && !defined(QSC_MCELIECE_S7N8192T128)
1335
#define QSC_MCELIECE_S3N4608T96
1336
#endif
1337
1338
#if !defined(QSC_MCELIECE_S1N3488T64) && !defined(QSC_MCELIECE_S3N4608T96) && !defined(QSC_MCELIECE_S6N6960T119) && !defined(QSC_MCELIECE_S7N8192T128)
1343
#define QSC_MCELIECE_S5N6688T128
1344
#endif
1345
1346
#if !defined(QSC_MCELIECE_S1N3488T64) && !defined(QSC_MCELIECE_S3N4608T96) && !defined(QSC_MCELIECE_S5N6688T128) && !defined(QSC_MCELIECE_S7N8192T128)
1351
#define QSC_MCELIECE_S6N6960T119
1352
#endif
1353
1354
#if !defined(QSC_MCELIECE_S1N3488T64) && !defined(QSC_MCELIECE_S3N4608T96) && !defined(QSC_MCELIECE_S5N6688T128) && !defined(QSC_MCELIECE_S6N6960T119)
1359
#define QSC_MCELIECE_S7N8192T128
1360
#endif
1361
1362
/*** Signature Schemes ***/
1363
1364
#if !defined(QSC_DILITHIUM_S1P44) && !defined(QSC_DILITHIUM_S3P65) && !defined(QSC_DILITHIUM_S5P87)
1369
#define QSC_DILITHIUM_S1P44
1370
#endif
1371
1372
#if !defined(QSC_DILITHIUM_S1P44) && !defined(QSC_DILITHIUM_S3P65) && !defined(QSC_DILITHIUM_S5P87)
1377
#define QSC_DILITHIUM_S3P65
1378
#endif
1379
1380
#if !defined(QSC_DILITHIUM_S1P44) && !defined(QSC_DILITHIUM_S3P65) && !defined(QSC_DILITHIUM_S5P87)
1385
#define QSC_DILITHIUM_S5P87
1386
#endif
1387
1388
/*** ECDSA ***/
1389
1390
#if !defined(QSC_ECDSA_S1P256) && !defined(QSC_ECDSA_S3P384) && !defined(QSC_ECDSA_S5P521)
1395
#define QSC_ECDSA_S1P256
1396
#endif
1397
1398
#if !defined(QSC_ECDSA_S1P256) && !defined(QSC_ECDSA_S3P384) && !defined(QSC_ECDSA_S5P521)
1403
#define QSC_ECDSA_S3P384
1404
#endif
1405
1406
#if !defined(QSC_ECDSA_S1P256) && !defined(QSC_ECDSA_S3P384) && !defined(QSC_ECDSA_S5P521)
1411
#define QSC_ECDSA_S5P521
1412
#endif
1413
1414
/*** EDDSA ***/
1415
1416
#if !defined(QSC_EDDSA_S1EC25519) && !defined(QSC_EDDSA_S3EC448)
1421
#define QSC_EDDSA_S1EC25519
1422
#endif
1423
1424
#if !defined(QSC_EDDSA_S1EC25519) && !defined(QSC_EDDSA_S3EC448)
1429
#define QSC_EDDSA_S3EC448
1430
#endif
1431
1432
/*** Falcon ***/
1433
1434
#if !defined(QSC_FALCON_S5SHAKE256F1024)
1439
#define QSC_FALCON_S3SHAKE256F512
1440
#endif
1441
1442
#if !defined(QSC_FALCON_S3SHAKE256F512)
1447
#define QSC_FALCON_S5SHAKE256F1024
1448
#endif
1449
1450
/*** SphincsPlus ***/
1451
1452
#if !defined(QSC_SPHINCSPLUS_S3S192SHAKERS) && !defined(QSC_SPHINCSPLUS_S5S256SHAKERS) && !defined(QSC_SPHINCSPLUS_S6S512SHAKERS)
1457
#define QSC_SPHINCSPLUS_S1S128SHAKERS
1458
#endif
1459
1460
#if !defined(QSC_SPHINCSPLUS_S1S128SHAKERS) && !defined(QSC_SPHINCSPLUS_S5S256SHAKERS) && !defined(QSC_SPHINCSPLUS_S6S512SHAKERS)
1465
#define QSC_SPHINCSPLUS_S3S192SHAKERS
1466
#endif
1467
1468
#if !defined(QSC_SPHINCSPLUS_S1S128SHAKERS) && !defined(QSC_SPHINCSPLUS_S3S192SHAKERS) && !defined(QSC_SPHINCSPLUS_S6S512SHAKERS)
1473
#define QSC_SPHINCSPLUS_S5S256SHAKERS
1474
#endif
1475
1476
#if !defined(QSC_SPHINCSPLUS_S1S128SHAKERS) && !defined(QSC_SPHINCSPLUS_S3S192SHAKERS) && !defined(QSC_SPHINCSPLUS_S5S256SHAKERS)
1481
#define QSC_SPHINCSPLUS_S6S512SHAKERS
1482
#endif
1483
1484
QSC_CPLUSPLUS_ENABLED_END
1485
1486
#endif
QSC
qsccommon.h
Generated by
1.14.0