| 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 |
/* References |
| 8 |
* ========== |
| 9 |
* |
| 10 |
* - [FIPS204] |
| 11 |
* FIPS 204 Module-Lattice-Based Digital Signature Standard |
| 12 |
* National Institute of Standards and Technology |
| 13 |
* https://csrc.nist.gov/pubs/fips/204/final |
| 14 |
* |
| 15 |
* - [libmceliece] |
| 16 |
* libmceliece implementation of Classic McEliece |
| 17 |
* Bernstein, Chou |
| 18 |
* https://lib.mceliece.org/ |
| 19 |
* |
| 20 |
* - [optblocker] |
| 21 |
* PQC forum post on opt-blockers using volatile globals |
| 22 |
* Daniel J. Bernstein |
| 23 |
* https://groups.google.com/a/list.nist.gov/g/pqc-forum/c/hqbtIGFKIpU/m/H14H0wOlBgAJ |
| 24 |
*/ |
| 25 |
|
| 26 |
#ifndef MLD_CT_H |
| 27 |
#define MLD_CT_H |
| 28 |
|
| 29 |
#include <stdint.h> |
| 30 |
#include "cbmc.h" |
| 31 |
#include "common.h" |
| 32 |
|
| 33 |
/* Constant-time comparisons and conditional operations |
| 34 |
|
| 35 |
We reduce the risk for compilation into variable-time code |
| 36 |
through the use of 'value barriers'. |
| 37 |
|
| 38 |
Functionally, a value barrier is a no-op. To the compiler, however, |
| 39 |
it constitutes an arbitrary modification of its input, and therefore |
| 40 |
harden's value propagation and range analysis. |
| 41 |
|
| 42 |
We consider two approaches to implement a value barrier: |
| 43 |
- An empty inline asm block which marks the target value as clobbered. |
| 44 |
- XOR'ing with the value of a volatile global that's set to 0; |
| 45 |
see @[optblocker] for a discussion of this idea, and |
| 46 |
@[libmceliece, inttypes/crypto_intN.h] for an implementation. |
| 47 |
|
| 48 |
The first approach is cheap because it only prevents the compiler |
| 49 |
from reasoning about the value of the variable past the barrier, |
| 50 |
but does not directly generate additional instructions. |
| 51 |
|
| 52 |
The second approach generates redundant loads and XOR operations |
| 53 |
and therefore comes at a higher runtime cost. However, it appears |
| 54 |
more robust towards optimization, as compilers should never drop |
| 55 |
a volatile load. |
| 56 |
|
| 57 |
We use the empty-ASM value barrier for GCC and clang, and fall |
| 58 |
back to the global volatile barrier otherwise. |
| 59 |
|
| 60 |
The global value barrier can be forced by setting |
| 61 |
MLD_CONFIG_NO_ASM_VALUE_BARRIER. |
| 62 |
|
| 63 |
*/ |
| 64 |
|
| 65 |
#if defined(MLD_HAVE_INLINE_ASM) && !defined(MLD_CONFIG_NO_ASM_VALUE_BARRIER) |
| 66 |
#define MLD_USE_ASM_VALUE_BARRIER |
| 67 |
#endif |
| 68 |
|
| 69 |
|
| 70 |
#if !defined(MLD_USE_ASM_VALUE_BARRIER) |
| 71 |
/* |
| 72 |
* Declaration of global volatile that the global value barrier |
| 73 |
* is loading from and masking with. |
| 74 |
*/ |
| 75 |
#define mld_ct_opt_blocker_u64 MLD_NAMESPACE(ct_opt_blocker_u64) |
| 76 |
extern volatile uint64_t mld_ct_opt_blocker_u64; |
| 77 |
|
| 78 |
|
| 79 |
/* Helper functions for obtaining global masks of various sizes */ |
| 80 |
|
| 81 |
/* This contract is not proved but treated as an axiom. |
| 82 |
* |
| 83 |
* Its validity relies on the assumption that the global opt-blocker |
| 84 |
* constant mld_ct_opt_blocker_u64 is not modified. |
| 85 |
*/ |
| 86 |
static MLD_INLINE uint64_t mld_ct_get_optblocker_u64(void) |
| 87 |
__contract__(ensures(return_value == 0)) { return mld_ct_opt_blocker_u64; } |
| 88 |
|
| 89 |
static MLD_INLINE int64_t mld_ct_get_optblocker_i64(void) |
| 90 |
__contract__(ensures(return_value == 0)) { return (int64_t)mld_ct_get_optblocker_u64(); } |
| 91 |
|
| 92 |
static MLD_INLINE uint32_t mld_ct_get_optblocker_u32(void) |
| 93 |
__contract__(ensures(return_value == 0)) { return (uint32_t)mld_ct_get_optblocker_u64(); } |
| 94 |
|
| 95 |
static MLD_INLINE uint8_t mld_ct_get_optblocker_u8(void) |
| 96 |
__contract__(ensures(return_value == 0)) { return (uint8_t)mld_ct_get_optblocker_u64(); } |
| 97 |
|
| 98 |
/* Opt-blocker based implementation of value barriers */ |
| 99 |
static MLD_INLINE int64_t mld_value_barrier_i64(int64_t b) |
| 100 |
__contract__(ensures(return_value == b)) { return (b ^ mld_ct_get_optblocker_i64()); } |
| 101 |
|
| 102 |
static MLD_INLINE uint32_t mld_value_barrier_u32(uint32_t b) |
| 103 |
__contract__(ensures(return_value == b)) { return (b ^ mld_ct_get_optblocker_u32()); } |
| 104 |
|
| 105 |
static MLD_INLINE uint8_t mld_value_barrier_u8(uint8_t b) |
| 106 |
__contract__(ensures(return_value == b)) { return (b ^ mld_ct_get_optblocker_u8()); } |
| 107 |
|
| 108 |
|
| 109 |
#else /* !MLD_USE_ASM_VALUE_BARRIER */ |
| 110 |
static MLD_INLINE int64_t mld_value_barrier_i64(int64_t b) |
| 111 |
__contract__(ensures(return_value == b)) |
| 112 |
{ |
| 113 |
__asm__ volatile("" : "+r"(b)); |
| 114 |
return b; |
| 115 |
} |
| 116 |
|
| 117 |
static MLD_INLINE uint32_t mld_value_barrier_u32(uint32_t b) |
| 118 |
__contract__(ensures(return_value == b)) |
| 119 |
{ |
| 120 |
__asm__ volatile("" : "+r"(b)); |
| 121 |
return b; |
| 122 |
} |
| 123 |
|
| 124 |
static MLD_INLINE uint8_t mld_value_barrier_u8(uint8_t b) |
| 125 |
__contract__(ensures(return_value == b)) |
| 126 |
{ |
| 127 |
__asm__ volatile("" : "+r"(b)); |
| 128 |
return b; |
| 129 |
} |
| 130 |
#endif /* MLD_USE_ASM_VALUE_BARRIER */ |
| 131 |
|
| 132 |
#ifdef CBMC |
| 133 |
#pragma CPROVER check push |
| 134 |
#pragma CPROVER check disable "conversion" |
| 135 |
#endif |
| 136 |
|
| 137 |
/************************************************* |
| 138 |
* Name: mld_cast_uint32_to_int32 |
| 139 |
* |
| 140 |
* Description: Cast uint32 value to int32 |
| 141 |
* |
| 142 |
* Returns: For uint32_t x, the unique y in int32_t |
| 143 |
* so that x == y mod 2^32. |
| 144 |
* |
| 145 |
* Concretely: |
| 146 |
* - x < 2^31: returns x |
| 147 |
* - x >= 2^31: returns x - 2^31 |
| 148 |
* |
| 149 |
**************************************************/ |
| 150 |
static MLD_ALWAYS_INLINE int32_t mld_cast_uint32_to_int32(uint32_t x) |
| 151 |
{ |
| 152 |
/* |
| 153 |
* PORTABILITY: This relies on uint32_t -> int32_t |
| 154 |
* being implemented as the inverse of int32_t -> uint32_t, |
| 155 |
* which is implementation-defined (C99 6.3.1.3 (3)) |
| 156 |
* CBMC (correctly) fails to prove this conversion is OK, |
| 157 |
* so we have to suppress that check here |
| 158 |
*/ |
| 159 |
return (int32_t)x; |
| 160 |
} |
| 161 |
|
| 162 |
#ifdef CBMC |
| 163 |
#pragma CPROVER check pop |
| 164 |
#endif |
| 165 |
|
| 166 |
|
| 167 |
/************************************************* |
| 168 |
* Name: mld_cast_int64_to_uint32 |
| 169 |
* |
| 170 |
* Description: Cast int64 value to uint32 as per C standard. |
| 171 |
* |
| 172 |
* Returns: For int64_t x, the unique y in uint32_t |
| 173 |
* so that x == y mod 2^32. |
| 174 |
**************************************************/ |
| 175 |
static MLD_ALWAYS_INLINE uint32_t mld_cast_int64_to_uint32(int64_t x) |
| 176 |
{ |
| 177 |
return (uint32_t)(x & (int64_t)UINT32_MAX); |
| 178 |
} |
| 179 |
|
| 180 |
/************************************************* |
| 181 |
* Name: mld_cast_int32_to_uint32 |
| 182 |
* |
| 183 |
* Description: Cast int32 value to uint32 as per C standard. |
| 184 |
* |
| 185 |
* Returns: For int32_t x, the unique y in uint32_t |
| 186 |
* so that x == y mod 2^32. |
| 187 |
**************************************************/ |
| 188 |
static MLD_ALWAYS_INLINE uint32_t mld_cast_int32_to_uint32(int32_t x) |
| 189 |
{ |
| 190 |
return mld_cast_int64_to_uint32((int64_t)x); |
| 191 |
} |
| 192 |
|
| 193 |
/************************************************* |
| 194 |
* Name: mld_ct_sel_int32 |
| 195 |
* |
| 196 |
* Description: Functionally equivalent to cond ? a : b, |
| 197 |
* but implemented with guards against |
| 198 |
* compiler-introduced branches. |
| 199 |
* |
| 200 |
* Arguments: int32_t a: First alternative |
| 201 |
* int32_t b: Second alternative |
| 202 |
* uint32_t cond: Condition variable. |
| 203 |
* |
| 204 |
* |
| 205 |
**************************************************/ |
| 206 |
static MLD_INLINE int32_t mld_ct_sel_int32(int32_t a, int32_t b, uint32_t cond) |
| 207 |
__contract__( |
| 208 |
requires(cond == 0x0 || cond == 0xFFFFFFFF) |
| 209 |
ensures(return_value == (cond ? a : b)) |
| 210 |
) |
| 211 |
{ |
| 212 |
uint32_t au = mld_cast_int32_to_uint32(a); |
| 213 |
uint32_t bu = mld_cast_int32_to_uint32(b); |
| 214 |
uint32_t res = bu ^ (mld_value_barrier_u32(cond) & (au ^ bu)); |
| 215 |
return mld_cast_uint32_to_int32(res); |
| 216 |
} |
| 217 |
|
| 218 |
/************************************************* |
| 219 |
* Name: mld_ct_cmask_nonzero_u32 |
| 220 |
* |
| 221 |
* Description: Return 0 if input is zero, and -1 otherwise. |
| 222 |
* |
| 223 |
* Arguments: uint32_t x: Value to be converted into a mask |
| 224 |
* |
| 225 |
**************************************************/ |
| 226 |
static MLD_INLINE uint32_t mld_ct_cmask_nonzero_u32(uint32_t x) |
| 227 |
__contract__(ensures(return_value == ((x == 0) ? 0 : 0xFFFFFFFF))) |
| 228 |
{ |
| 229 |
int64_t tmp = mld_value_barrier_i64(-((int64_t)x)); |
| 230 |
tmp >>= 32; |
| 231 |
return mld_cast_int64_to_uint32(tmp); |
| 232 |
} |
| 233 |
|
| 234 |
/************************************************* |
| 235 |
* Name: mld_ct_cmask_nonzero_u8 |
| 236 |
* |
| 237 |
* Description: Return 0 if input is zero, and -1 otherwise. |
| 238 |
* |
| 239 |
* Arguments: uint8_t x: Value to be converted into a mask |
| 240 |
* |
| 241 |
**************************************************/ |
| 242 |
static MLD_INLINE uint8_t mld_ct_cmask_nonzero_u8(uint8_t x) |
| 243 |
__contract__(ensures(return_value == ((x == 0) ? 0 : 0xFF))) |
| 244 |
{ |
| 245 |
uint32_t mask = mld_ct_cmask_nonzero_u32((uint32_t)x); |
| 246 |
return (uint8_t)(mask & 0xFF); |
| 247 |
} |
| 248 |
|
| 249 |
/************************************************* |
| 250 |
* Name: mld_ct_cmask_neg_i32 |
| 251 |
* |
| 252 |
* Description: Return 0 if input is non-negative, and -1 otherwise. |
| 253 |
* |
| 254 |
* Arguments: int32_t x: Value to be converted into a mask |
| 255 |
* |
| 256 |
**************************************************/ |
| 257 |
static MLD_INLINE uint32_t mld_ct_cmask_neg_i32(int32_t x) |
| 258 |
__contract__( |
| 259 |
ensures(return_value == ((x < 0) ? 0xFFFFFFFF : 0)) |
| 260 |
) |
| 261 |
{ |
| 262 |
int64_t tmp = mld_value_barrier_i64((int64_t)x); |
| 263 |
tmp >>= 31; |
| 264 |
return mld_cast_int64_to_uint32(tmp); |
| 265 |
} |
| 266 |
|
| 267 |
/************************************************* |
| 268 |
* Name: mld_ct_abs_i32 |
| 269 |
* |
| 270 |
* Description: Return -x if x<0, x otherwise |
| 271 |
* |
| 272 |
* Arguments: int32_t x: Input value |
| 273 |
* |
| 274 |
**************************************************/ |
| 275 |
static MLD_INLINE int32_t mld_ct_abs_i32(int32_t x) |
| 276 |
__contract__( |
| 277 |
requires(x >= -INT32_MAX) |
| 278 |
ensures(return_value == ((x < 0) ? -x : x)) |
| 279 |
) |
| 280 |
{ |
| 281 |
return mld_ct_sel_int32(-x, x, mld_ct_cmask_neg_i32(x)); |
| 282 |
} |
| 283 |
|
| 284 |
/************************************************* |
| 285 |
* Name: mld_ct_memcmp |
| 286 |
* |
| 287 |
* Description: Compare two arrays for equality in constant time. |
| 288 |
* |
| 289 |
* Arguments: const uint8_t *a: pointer to first byte array |
| 290 |
* const uint8_t *b: pointer to second byte array |
| 291 |
* size_t len: length of the byte arrays, upper-bounded |
| 292 |
* to UINT16_MAX to control proof complexity |
| 293 |
* only. |
| 294 |
* |
| 295 |
* Returns 0 if the byte arrays are equal, 0xFF otherwise. |
| 296 |
**************************************************/ |
| 297 |
static MLD_INLINE uint8_t mld_ct_memcmp(const uint8_t *a, const uint8_t *b, |
| 298 |
const size_t len) |
| 299 |
__contract__( |
| 300 |
requires(len <= UINT16_MAX) |
| 301 |
requires(memory_no_alias(a, len)) |
| 302 |
requires(memory_no_alias(b, len)) |
| 303 |
ensures((return_value == 0) || (return_value == 0xFF)) |
| 304 |
ensures((return_value == 0) == forall(i, 0, len, (a[i] == b[i])))) |
| 305 |
{ |
| 306 |
uint8_t r = 0, s = 0; |
| 307 |
unsigned i; |
| 308 |
|
| 309 |
for (i = 0; i < len; i++) |
| 310 |
__loop__( |
| 311 |
invariant(i <= len) |
| 312 |
invariant((r == 0) == (forall(k, 0, i, (a[k] == b[k]))))) |
| 313 |
{ |
| 314 |
r |= a[i] ^ b[i]; |
| 315 |
/* s is useless, but prevents the loop from being aborted once r=0xff. */ |
| 316 |
s ^= a[i] ^ b[i]; |
| 317 |
} |
| 318 |
|
| 319 |
/* |
| 320 |
* - Convert r into a mask; this may not be necessary, but is an additional |
| 321 |
* safeguard |
| 322 |
* towards leaking information about a and b. |
| 323 |
* - XOR twice with s, separated by a value barrier, to prevent the compile |
| 324 |
* from dropping the s computation in the loop. |
| 325 |
*/ |
| 326 |
return (mld_value_barrier_u8(mld_ct_cmask_nonzero_u8(r) ^ s) ^ s); |
| 327 |
} |
| 328 |
|
| 329 |
/************************************************* |
| 330 |
* Name: mld_zeroize |
| 331 |
* |
| 332 |
* Description: Force-zeroize a buffer. |
| 333 |
* @[FIPS204, Section 3.6.3] Destruction of intermediate |
| 334 |
* values. |
| 335 |
* |
| 336 |
* Arguments: void *ptr: pointer to buffer to be zeroed |
| 337 |
* size_t len: Amount of bytes to be zeroed |
| 338 |
**************************************************/ |
| 339 |
#if !defined(MLD_CONFIG_CUSTOM_ZEROIZE) |
| 340 |
#if defined(MLD_SYS_WINDOWS) |
| 341 |
#include <windows.h> |
| 342 |
static MLD_INLINE void mld_zeroize(void *ptr, size_t len) |
| 343 |
__contract__( |
| 344 |
requires(memory_no_alias(ptr, len)) |
| 345 |
assigns(memory_slice(ptr, len))) { SecureZeroMemory(ptr, len); } |
| 346 |
#elif defined(MLD_HAVE_INLINE_ASM) |
| 347 |
#include <string.h> |
| 348 |
static MLD_INLINE void mld_zeroize(void *ptr, size_t len) |
| 349 |
__contract__( |
| 350 |
requires(memory_no_alias(ptr, len)) |
| 351 |
assigns(memory_slice(ptr, len))) |
| 352 |
{ |
| 353 |
memset(ptr, 0, len); |
| 354 |
/* This follows OpenSSL and seems sufficient to prevent the compiler |
| 355 |
* from optimizing away the memset. |
| 356 |
* |
| 357 |
* If there was a reliable way to detect availability of memset_s(), |
| 358 |
* that would be preferred. */ |
| 359 |
__asm__ __volatile__("" : : "r"(ptr) : "memory"); |
| 360 |
} |
| 361 |
#else /* !MLD_SYS_WINDOWS && MLD_HAVE_INLINE_ASM */ |
| 362 |
#error No plausibly-secure implementation of mld_zeroize available. Please provide your own using MLD_CONFIG_CUSTOM_ZEROIZE. |
| 363 |
#endif /* !MLD_SYS_WINDOWS && !MLD_HAVE_INLINE_ASM */ |
| 364 |
#endif /* !MLD_CONFIG_CUSTOM_ZEROIZE */ |
| 365 |
|
| 366 |
#endif /* !MLD_CT_H */ |