| 1 |
/* |
| 2 |
* Copyright (c) The mlkem-native project authors |
| 3 |
* Copyright (c) The mldsa-native project authors |
| 4 |
* SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#ifndef MLD_SYS_H |
| 8 |
#define MLD_SYS_H |
| 9 |
|
| 10 |
#if !defined(MLD_CONFIG_NO_ASM) && (defined(__GNUC__) || defined(__clang__)) |
| 11 |
#define MLD_HAVE_INLINE_ASM |
| 12 |
#endif |
| 13 |
|
| 14 |
/* Try to find endianness, if not forced through CFLAGS already */ |
| 15 |
#if !defined(MLD_SYS_LITTLE_ENDIAN) && !defined(MLD_SYS_BIG_ENDIAN) |
| 16 |
#if defined(__BYTE_ORDER__) |
| 17 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 18 |
#define MLD_SYS_LITTLE_ENDIAN |
| 19 |
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 20 |
#define MLD_SYS_BIG_ENDIAN |
| 21 |
#else |
| 22 |
#error "__BYTE_ORDER__ defined, but don't recognize value." |
| 23 |
#endif |
| 24 |
#endif /* __BYTE_ORDER__ */ |
| 25 |
|
| 26 |
/* MSVC does not define __BYTE_ORDER__. However, MSVC only supports |
| 27 |
* little endian x86, x86_64, and AArch64. It is, hence, safe to assume |
| 28 |
* little endian. */ |
| 29 |
#if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_AMD64) || \ |
| 30 |
defined(_M_IX86) || defined(_M_ARM64)) |
| 31 |
#define MLD_SYS_LITTLE_ENDIAN |
| 32 |
#endif |
| 33 |
|
| 34 |
#endif /* !MLD_SYS_LITTLE_ENDIAN && !MLD_SYS_BIG_ENDIAN */ |
| 35 |
|
| 36 |
/* Check if we're running on an AArch64 little endian system. _M_ARM64 is set by |
| 37 |
* MSVC. */ |
| 38 |
#if defined(__AARCH64EL__) || defined(_M_ARM64) |
| 39 |
#define MLD_SYS_AARCH64 |
| 40 |
#endif |
| 41 |
|
| 42 |
/* Check if we're running on an AArch64 big endian system. */ |
| 43 |
#if defined(__AARCH64EB__) |
| 44 |
#define MLD_SYS_AARCH64_EB |
| 45 |
#endif |
| 46 |
|
| 47 |
#if defined(__x86_64__) |
| 48 |
#define MLD_SYS_X86_64 |
| 49 |
#if defined(__AVX2__) |
| 50 |
#define MLD_SYS_X86_64_AVX2 |
| 51 |
#endif |
| 52 |
#endif /* __x86_64__ */ |
| 53 |
|
| 54 |
#if defined(MLD_SYS_LITTLE_ENDIAN) && defined(__powerpc64__) |
| 55 |
#define MLD_SYS_PPC64LE |
| 56 |
#endif |
| 57 |
|
| 58 |
#if defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64 |
| 59 |
#define MLD_SYS_RISCV64 |
| 60 |
#endif |
| 61 |
|
| 62 |
#if defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 32 |
| 63 |
#define MLD_SYS_RISCV32 |
| 64 |
#endif |
| 65 |
|
| 66 |
#if defined(_WIN64) || defined(_WIN32) |
| 67 |
#define MLD_SYS_WINDOWS |
| 68 |
#endif |
| 69 |
|
| 70 |
#if defined(__linux__) |
| 71 |
#define MLD_SYS_LINUX |
| 72 |
#endif |
| 73 |
|
| 74 |
#if defined(__APPLE__) |
| 75 |
#define MLD_SYS_APPLE |
| 76 |
#endif |
| 77 |
|
| 78 |
/* If MLD_FORCE_AARCH64 is set, assert that we're indeed on an AArch64 system. |
| 79 |
*/ |
| 80 |
#if defined(MLD_FORCE_AARCH64) && !defined(MLD_SYS_AARCH64) |
| 81 |
#error "MLD_FORCE_AARCH64 is set, but we don't seem to be on an AArch64 system." |
| 82 |
#endif |
| 83 |
|
| 84 |
/* If MLD_FORCE_AARCH64_EB is set, assert that we're indeed on a big endian |
| 85 |
* AArch64 system. */ |
| 86 |
#if defined(MLD_FORCE_AARCH64_EB) && !defined(MLD_SYS_AARCH64_EB) |
| 87 |
#error \ |
| 88 |
"MLD_FORCE_AARCH64_EB is set, but we don't seem to be on an AArch64 system." |
| 89 |
#endif |
| 90 |
|
| 91 |
/* If MLD_FORCE_X86_64 is set, assert that we're indeed on an X86_64 system. */ |
| 92 |
#if defined(MLD_FORCE_X86_64) && !defined(MLD_SYS_X86_64) |
| 93 |
#error "MLD_FORCE_X86_64 is set, but we don't seem to be on an X86_64 system." |
| 94 |
#endif |
| 95 |
|
| 96 |
#if defined(MLD_FORCE_PPC64LE) && !defined(MLD_SYS_PPC64LE) |
| 97 |
#error "MLD_FORCE_PPC64LE is set, but we don't seem to be on a PPC64LE system." |
| 98 |
#endif |
| 99 |
|
| 100 |
#if defined(MLD_FORCE_RISCV64) && !defined(MLD_SYS_RISCV64) |
| 101 |
#error "MLD_FORCE_RISCV64 is set, but we don't seem to be on a RISCV64 system." |
| 102 |
#endif |
| 103 |
|
| 104 |
#if defined(MLD_FORCE_RISCV32) && !defined(MLD_SYS_RISCV32) |
| 105 |
#error "MLD_FORCE_RISCV32 is set, but we don't seem to be on a RISCV32 system." |
| 106 |
#endif |
| 107 |
|
| 108 |
/* |
| 109 |
* MLD_INLINE: Hint for inlining. |
| 110 |
* - MSVC: __inline |
| 111 |
* - C99+: inline |
| 112 |
* - GCC/Clang C90: __attribute__((unused)) to silence warnings |
| 113 |
* - Other C90: empty |
| 114 |
*/ |
| 115 |
#if !defined(MLD_INLINE) |
| 116 |
#if defined(_MSC_VER) |
| 117 |
#define MLD_INLINE __inline |
| 118 |
#elif defined(inline) || \ |
| 119 |
(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) |
| 120 |
#define MLD_INLINE inline |
| 121 |
#elif defined(__GNUC__) || defined(__clang__) |
| 122 |
#define MLD_INLINE __attribute__((unused)) |
| 123 |
#else |
| 124 |
#define MLD_INLINE |
| 125 |
#endif |
| 126 |
#endif /* !MLD_INLINE */ |
| 127 |
|
| 128 |
/* |
| 129 |
* MLD_ALWAYS_INLINE: Force inlining. |
| 130 |
* - MSVC: __forceinline |
| 131 |
* - GCC/Clang C99+: MLD_INLINE __attribute__((always_inline)) |
| 132 |
* - Other: MLD_INLINE (no forced inlining) |
| 133 |
*/ |
| 134 |
#if !defined(MLD_ALWAYS_INLINE) |
| 135 |
#if defined(_MSC_VER) |
| 136 |
#define MLD_ALWAYS_INLINE __forceinline |
| 137 |
#elif (defined(__GNUC__) || defined(__clang__)) && \ |
| 138 |
(defined(inline) || \ |
| 139 |
(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)) |
| 140 |
#define MLD_ALWAYS_INLINE MLD_INLINE __attribute__((always_inline)) |
| 141 |
#else |
| 142 |
#define MLD_ALWAYS_INLINE MLD_INLINE |
| 143 |
#endif |
| 144 |
#endif /* !MLD_ALWAYS_INLINE */ |
| 145 |
|
| 146 |
#ifndef MLD_STATIC_TESTABLE |
| 147 |
#define MLD_STATIC_TESTABLE static |
| 148 |
#endif |
| 149 |
|
| 150 |
/* |
| 151 |
* C90 does not have the restrict compiler directive yet. |
| 152 |
* We don't use it in C90 builds. |
| 153 |
*/ |
| 154 |
#if !defined(restrict) |
| 155 |
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L |
| 156 |
#define MLD_RESTRICT restrict |
| 157 |
#else |
| 158 |
#define MLD_RESTRICT |
| 159 |
#endif |
| 160 |
|
| 161 |
#else /* !restrict */ |
| 162 |
|
| 163 |
#define MLD_RESTRICT restrict |
| 164 |
#endif /* restrict */ |
| 165 |
|
| 166 |
#define MLD_DEFAULT_ALIGN 32 |
| 167 |
#define MLD_ALIGN_UP(N) \ |
| 168 |
((((N) + (MLD_DEFAULT_ALIGN - 1)) / MLD_DEFAULT_ALIGN) * MLD_DEFAULT_ALIGN) |
| 169 |
#if defined(__GNUC__) |
| 170 |
#define MLD_ALIGN __attribute__((aligned(MLD_DEFAULT_ALIGN))) |
| 171 |
#elif defined(_MSC_VER) |
| 172 |
#define MLD_ALIGN __declspec(align(MLD_DEFAULT_ALIGN)) |
| 173 |
#else |
| 174 |
#define MLD_ALIGN /* No known support for alignment constraints */ |
| 175 |
#endif |
| 176 |
|
| 177 |
|
| 178 |
/* New X86_64 CPUs support Conflow-flow protection using the CET instructions. |
| 179 |
* When enabled (through -fcf-protection=), all compilation units (including |
| 180 |
* empty ones) need to support CET for this to work. |
| 181 |
* For assembly, this means that source files need to signal support for |
| 182 |
* CET by setting the appropriate note.gnu.property section. |
| 183 |
* This can be achieved by including the <cet.h> header in all assembly file. |
| 184 |
* This file also provides the _CET_ENDBR macro which needs to be placed at |
| 185 |
* every potential target of an indirect branch. |
| 186 |
* If CET is enabled _CET_ENDBR maps to the endbr64 instruction, otherwise |
| 187 |
* it is empty. |
| 188 |
* In case the compiler does not support CET (e.g., <gcc8, <clang11), |
| 189 |
* the __CET__ macro is not set and we default to nothing. |
| 190 |
* Note that we only issue _CET_ENDBR instructions through the MLD_ASM_FN_SYMBOL |
| 191 |
* macro as the global symbols are the only possible targets of indirect |
| 192 |
* branches in our code. |
| 193 |
*/ |
| 194 |
#if defined(MLD_SYS_X86_64) |
| 195 |
#if defined(__CET__) |
| 196 |
#include <cet.h> |
| 197 |
#define MLD_CET_ENDBR _CET_ENDBR |
| 198 |
#else |
| 199 |
#define MLD_CET_ENDBR |
| 200 |
#endif |
| 201 |
#endif /* MLD_SYS_X86_64 */ |
| 202 |
|
| 203 |
#if defined(MLD_CONFIG_CT_TESTING_ENABLED) && !defined(__ASSEMBLER__) |
| 204 |
#include <valgrind/memcheck.h> |
| 205 |
#define MLD_CT_TESTING_SECRET(ptr, len) \ |
| 206 |
VALGRIND_MAKE_MEM_UNDEFINED((ptr), (len)) |
| 207 |
#define MLD_CT_TESTING_DECLASSIFY(ptr, len) \ |
| 208 |
VALGRIND_MAKE_MEM_DEFINED((ptr), (len)) |
| 209 |
#else /* MLD_CONFIG_CT_TESTING_ENABLED && !__ASSEMBLER__ */ |
| 210 |
#define MLD_CT_TESTING_SECRET(ptr, len) \ |
| 211 |
do \ |
| 212 |
{ \ |
| 213 |
} while (0) |
| 214 |
#define MLD_CT_TESTING_DECLASSIFY(ptr, len) \ |
| 215 |
do \ |
| 216 |
{ \ |
| 217 |
} while (0) |
| 218 |
#endif /* !(MLD_CONFIG_CT_TESTING_ENABLED && !__ASSEMBLER__) */ |
| 219 |
|
| 220 |
#if defined(__GNUC__) || defined(__clang__) |
| 221 |
#define MLD_MUST_CHECK_RETURN_VALUE __attribute__((warn_unused_result)) |
| 222 |
#else |
| 223 |
#define MLD_MUST_CHECK_RETURN_VALUE |
| 224 |
#endif |
| 225 |
|
| 226 |
|
| 227 |
#if !defined(__ASSEMBLER__) |
| 228 |
/* System capability enumeration */ |
| 229 |
typedef enum |
| 230 |
{ |
| 231 |
/* x86_64 */ |
| 232 |
MLD_SYS_CAP_AVX2, |
| 233 |
/* AArch64 */ |
| 234 |
MLD_SYS_CAP_SHA3 |
| 235 |
} mld_sys_cap; |
| 236 |
|
| 237 |
#if !defined(MLD_CONFIG_CUSTOM_CAPABILITY_FUNC) |
| 238 |
#include "cbmc.h" |
| 239 |
|
| 240 |
static MLD_INLINE int mld_sys_check_capability(mld_sys_cap cap) |
| 241 |
__contract__( |
| 242 |
ensures(return_value == 0 || return_value == 1) |
| 243 |
) |
| 244 |
{ |
| 245 |
/* By default, we rely on compile-time feature detection/specification: |
| 246 |
* If a feature is enabled at compile-time, we assume it is supported by |
| 247 |
* the host that the resulting library/binary will be built on. |
| 248 |
* If this assumption is not true, you MUST overwrite this function. |
| 249 |
* See the documentation of MLD_CONFIG_CUSTOM_CAPABILITY_FUNC in |
| 250 |
* mldsa_native_config.h for more information. */ |
| 251 |
(void)cap; |
| 252 |
return 1; |
| 253 |
} |
| 254 |
#endif /* !MLD_CONFIG_CUSTOM_CAPABILITY_FUNC */ |
| 255 |
#endif /* !__ASSEMBLER__ */ |
| 256 |
|
| 257 |
#endif /* !MLD_SYS_H */ |