|
| 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_CT_H |
| 8 | +#define MLD_CT_H |
| 9 | + |
| 10 | +/* References |
| 11 | + * ========== |
| 12 | + * |
| 13 | + * - [libmceliece] |
| 14 | + * libmceliece implementation of Classic McEliece |
| 15 | + * Bernstein, Chou |
| 16 | + * https://lib.mceliece.org/ |
| 17 | + * |
| 18 | + * - [optblocker] |
| 19 | + * PQC forum post on opt-blockers using volatile globals |
| 20 | + * Daniel J. Bernstein |
| 21 | + * https://groups.google.com/a/list.nist.gov/g/pqc-forum/c/hqbtIGFKIpU/m/H14H0wOlBgAJ |
| 22 | + */ |
| 23 | + |
| 24 | +#include <stdint.h> |
| 25 | +#include "cbmc.h" |
| 26 | +#include "common.h" |
| 27 | + |
| 28 | +/* Constant-time comparisons and conditional operations |
| 29 | +
|
| 30 | + We reduce the risk for compilation into variable-time code |
| 31 | + through the use of 'value barriers'. |
| 32 | +
|
| 33 | + Functionally, a value barrier is a no-op. To the compiler, however, |
| 34 | + it constitutes an arbitrary modification of its input, and therefore |
| 35 | + harden's value propagation and range analysis. |
| 36 | +
|
| 37 | + We consider two approaches to implement a value barrier: |
| 38 | + - An empty inline asm block which marks the target value as clobbered. |
| 39 | + - XOR'ing with the value of a volatile global that's set to 0; |
| 40 | + see @[optblocker] for a discussion of this idea, and |
| 41 | + @[libmceliece, inttypes/crypto_intN.h] for an implementation. |
| 42 | +
|
| 43 | + The first approach is cheap because it only prevents the compiler |
| 44 | + from reasoning about the value of the variable past the barrier, |
| 45 | + but does not directly generate additional instructions. |
| 46 | +
|
| 47 | + The second approach generates redundant loads and XOR operations |
| 48 | + and therefore comes at a higher runtime cost. However, it appears |
| 49 | + more robust towards optimization, as compilers should never drop |
| 50 | + a volatile load. |
| 51 | +
|
| 52 | + We use the empty-ASM value barrier for GCC and clang, and fall |
| 53 | + back to the global volatile barrier otherwise. |
| 54 | +
|
| 55 | + The global value barrier can be forced by setting |
| 56 | + MLD_CONFIG_NO_ASM_VALUE_BARRIER. |
| 57 | +
|
| 58 | +*/ |
| 59 | + |
| 60 | +#if defined(MLD_HAVE_INLINE_ASM) && !defined(MLD_CONFIG_NO_ASM_VALUE_BARRIER) |
| 61 | +#define MLD_USE_ASM_VALUE_BARRIER |
| 62 | +#endif |
| 63 | + |
| 64 | + |
| 65 | +#if !defined(MLD_USE_ASM_VALUE_BARRIER) |
| 66 | +/* |
| 67 | + * Declaration of global volatile that the global value barrier |
| 68 | + * is loading from and masking with. |
| 69 | + */ |
| 70 | +#define mld_ct_opt_blocker_u64 MLD_NAMESPACE(ct_opt_blocker_u64) |
| 71 | +extern volatile uint64_t mld_ct_opt_blocker_u64; |
| 72 | + |
| 73 | + |
| 74 | +/* Helper functions for obtaining global masks of various sizes */ |
| 75 | + |
| 76 | +/* This contract is not proved but treated as an axiom. |
| 77 | + * |
| 78 | + * Its validity relies on the assumption that the global opt-blocker |
| 79 | + * constant mld_ct_opt_blocker_u64 is not modified. |
| 80 | + */ |
| 81 | +static MLD_INLINE uint64_t mld_ct_get_optblocker_u64(void) |
| 82 | +__contract__(ensures(return_value == 0)) { return mld_ct_opt_blocker_u64; } |
| 83 | + |
| 84 | +static MLD_INLINE int64_t mld_ct_get_optblocker_i64(void) |
| 85 | +__contract__(ensures(return_value == 0)) { return (int64_t)mld_ct_get_optblocker_u64(); } |
| 86 | + |
| 87 | +static MLD_INLINE uint32_t mld_ct_get_optblocker_u32(void) |
| 88 | +__contract__(ensures(return_value == 0)) { return (uint32_t)mld_ct_get_optblocker_u64(); } |
| 89 | + |
| 90 | +/* Opt-blocker based implementation of value barriers */ |
| 91 | +static MLD_INLINE int64_t mld_value_barrier_i64(int64_t b) |
| 92 | +__contract__(ensures(return_value == b)) { return (b ^ mld_ct_get_optblocker_i64()); } |
| 93 | + |
| 94 | +static MLD_INLINE uint32_t mld_value_barrier_u32(uint32_t b) |
| 95 | +__contract__(ensures(return_value == b)) { return (b ^ mld_ct_get_optblocker_u32()); } |
| 96 | + |
| 97 | + |
| 98 | +#else /* !MLD_USE_ASM_VALUE_BARRIER */ |
| 99 | +static MLD_INLINE int64_t mld_value_barrier_i64(int64_t b) |
| 100 | +__contract__(ensures(return_value == b)) |
| 101 | +{ |
| 102 | + __asm__("" : "+r"(b)); |
| 103 | + return b; |
| 104 | +} |
| 105 | + |
| 106 | +static MLD_INLINE uint32_t mld_value_barrier_u32(uint32_t b) |
| 107 | +__contract__(ensures(return_value == b)) |
| 108 | +{ |
| 109 | + __asm__("" : "+r"(b)); |
| 110 | + return b; |
| 111 | +} |
| 112 | +#endif /* MLD_USE_ASM_VALUE_BARRIER */ |
| 113 | + |
| 114 | + |
| 115 | +/* |
| 116 | + * The mld_ct_sel_int32 function below makes deliberate use of unsigned |
| 117 | + * to signed integer conversion, which is implementation-defined |
| 118 | + * behaviour. Here, we assume that uint16_t -> int16_t is inverse |
| 119 | + * to int16_t -> uint16_t. |
| 120 | + */ |
| 121 | +#ifdef CBMC |
| 122 | +#pragma CPROVER check push |
| 123 | +#pragma CPROVER check disable "conversion" |
| 124 | +#endif |
| 125 | + |
| 126 | +/************************************************* |
| 127 | + * Name: mld_ct_sel_int32 |
| 128 | + * |
| 129 | + * Description: Functionally equivalent to cond ? a : b, |
| 130 | + * but implemented with guards against |
| 131 | + * compiler-introduced branches. |
| 132 | + * |
| 133 | + * Arguments: int32_t a: First alternative |
| 134 | + * int32_t b: Second alternative |
| 135 | + * uint32_t cond: Condition variable. |
| 136 | + * |
| 137 | + * |
| 138 | + **************************************************/ |
| 139 | +static MLD_INLINE int32_t mld_ct_sel_int32(int32_t a, int32_t b, uint32_t cond) |
| 140 | +/* TODO: proof */ |
| 141 | +__contract__( |
| 142 | + requires(cond == 0x0 || cond == 0xFFFFFFFF) |
| 143 | + ensures(return_value == (cond ? a : b)) |
| 144 | +) |
| 145 | +{ |
| 146 | + uint32_t au = a, bu = b; |
| 147 | + uint32_t res = bu ^ (mld_value_barrier_u32(cond) & (au ^ bu)); |
| 148 | + return (int32_t)res; |
| 149 | +} |
| 150 | + |
| 151 | +/* Put unsigned-to-signed warnings in CBMC back into scope */ |
| 152 | +#ifdef CBMC |
| 153 | +#pragma CPROVER check pop |
| 154 | +#endif |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | +/* |
| 159 | + * The mld_ct_cmask_neg_i32 function below makes deliberate use of |
| 160 | + * signed to unsigned integer conversion, which is fully defined |
| 161 | + * behaviour in C. It is thus safe to disable this warning. |
| 162 | + */ |
| 163 | +#ifdef CBMC |
| 164 | +#pragma CPROVER check push |
| 165 | +#pragma CPROVER check disable "conversion" |
| 166 | +#endif |
| 167 | + |
| 168 | + |
| 169 | +/************************************************* |
| 170 | + * Name: mld_ct_cmask_neg_i32 |
| 171 | + * |
| 172 | + * Description: Return 0 if input is non-negative, and -1 otherwise. |
| 173 | + * |
| 174 | + * Arguments: int32_t x: Value to be converted into a mask |
| 175 | + * |
| 176 | + **************************************************/ |
| 177 | +/* TODO: proof */ |
| 178 | +static MLD_INLINE uint32_t mld_ct_cmask_neg_i32(int32_t x) |
| 179 | +__contract__(ensures(return_value == ((x < 0) ? 0xFFFFFFFF : 0))) |
| 180 | +{ |
| 181 | + int64_t tmp = mld_value_barrier_i64((int64_t)x); |
| 182 | + tmp >>= 31; |
| 183 | + return (uint32_t)tmp; |
| 184 | +} |
| 185 | + |
| 186 | +/* Put signed-to-unsigned warnings in CBMC back into scope */ |
| 187 | +#ifdef CBMC |
| 188 | +#pragma CPROVER check pop |
| 189 | +#endif |
| 190 | + |
| 191 | +/************************************************* |
| 192 | + * Name: mld_ct_abs_i32 |
| 193 | + * |
| 194 | + * Description: Return -x if x<0, x otherwise |
| 195 | + * |
| 196 | + * Arguments: int32_t x: Input value |
| 197 | + * |
| 198 | + **************************************************/ |
| 199 | +/* TODO: proof */ |
| 200 | +static MLD_INLINE uint32_t mld_ct_abs_i32(int32_t x) |
| 201 | +__contract__( |
| 202 | + requires(x >= -INT32_MAX) |
| 203 | + ensures(return_value == ((x < 0) ? -x : x)) |
| 204 | +) |
| 205 | +{ |
| 206 | + return mld_ct_sel_int32(-x, x, mld_ct_cmask_neg_i32(x)); |
| 207 | +} |
| 208 | + |
| 209 | + |
| 210 | +#endif /* !MLD_CT_H */ |
0 commit comments