Debian Codegraph @

mpi/mpih-const-time.c

raw
1 /* mpih-const-time.c - Constant-time MPI helper functions
2 * Copyright (C) 2020, 2025 g10 Code GmbH
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include "mpi-internal.h"
24 #include "g10lib.h"
25 #include "const-time.h"
26 #include "longlong.h"
27
28 #define A_LIMB_1 ((mpi_limb_t)1)
29
30
31 /*
32 * Return 1 if X > Y and otherwise return 0.
33 */
34 static inline mpi_limb_t
35 mpih_ct_limb_greater_than (mpi_limb_t x, mpi_limb_t y)
36 {
37 mpi_limb_t diff_hi, diff_lo;
38 sub_ddmmss (diff_hi, diff_lo, 0, y, 0, x);
39 (void)diff_lo;
40 return diff_hi >> (BITS_PER_MPI_LIMB - 1);
41 }
42
43
44 /*
45 * Return 1 if X < Y and otherwise return 0.
46 */
47 static inline mpi_limb_t
48 mpih_ct_limb_less_than (mpi_limb_t x, mpi_limb_t y)
49 {
50 return mpih_ct_limb_greater_than (y, x);
51 }
52
53
54 /*
55 * W = U when OP_ENABLED=1
56 * otherwise, W keeps old value
57 */
58 void
59 _gcry_mpih_set_cond (mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
60 unsigned long op_enable)
61 {
62 /* Note: dual mask with AND/OR used for EM leakage mitigation */
63 mpi_limb_t mask1 = ct_limb_gen_mask(op_enable);
64 mpi_limb_t mask2 = ct_limb_gen_inv_mask(op_enable);
65 mpi_size_t i;
66
67 for (i = 0; i < usize; i++)
68 {
69 wp[i] = (wp[i] & mask2) | (up[i] & mask1);
70 }
71 }
72
73
74 /*
75 * W = U + V when OP_ENABLED=1
76 * otherwise, W = U
77 */
78 mpi_limb_t
79 _gcry_mpih_add_n_cond (mpi_ptr_t wp, mpi_ptr_t up, mpi_ptr_t vp,
80 mpi_size_t usize, unsigned long op_enable)
81 {
82 /* Note: dual mask with AND/OR used for EM leakage mitigation */
83 mpi_limb_t mask1 = ct_limb_gen_mask(op_enable);
84 mpi_limb_t mask2 = ct_limb_gen_inv_mask(op_enable);
85 mpi_size_t i;
86 mpi_limb_t cy;
87
88 cy = 0;
89 for (i = 0; i < usize; i++)
90 {
91 mpi_limb_t u = up[i];
92 mpi_limb_t x = u + vp[i];
93 mpi_limb_t cy1 = mpih_ct_limb_less_than(x, u);
94 mpi_limb_t cy2;
95
96 x = x + cy;
97 cy2 = mpih_ct_limb_less_than(x, cy);
98 cy = cy1 | cy2;
99 wp[i] = (u & mask2) | (x & mask1);
100 }
101
102 return cy & mask1;
103 }
104
105
106 /*
107 * W = U - V when OP_ENABLED=1
108 * otherwise, W = U
109 */
110 mpi_limb_t
111 _gcry_mpih_sub_n_cond (mpi_ptr_t wp, mpi_ptr_t up, mpi_ptr_t vp,
112 mpi_size_t usize, unsigned long op_enable)
113 {
114 /* Note: dual mask with AND/OR used for EM leakage mitigation */
115 mpi_limb_t mask1 = ct_limb_gen_mask(op_enable);
116 mpi_limb_t mask2 = ct_limb_gen_inv_mask(op_enable);
117 mpi_size_t i;
118 mpi_limb_t cy;
119
120 cy = 0;
121 for (i = 0; i < usize; i++)
122 {
123 mpi_limb_t u = up[i];
124 mpi_limb_t x = u - vp[i];
125 mpi_limb_t cy1 = mpih_ct_limb_greater_than(x, u);
126 mpi_limb_t cy2;
127
128 cy2 = mpih_ct_limb_less_than(x, cy);
129 x = x - cy;
130 cy = cy1 | cy2;
131 wp[i] = (u & mask2) | (x & mask1);
132 }
133
134 return cy & mask1;
135 }
136
137
138 /*
139 * Swap value of U and V when OP_ENABLED=1
140 * otherwise, no change
141 */
142 void
143 _gcry_mpih_swap_cond (mpi_ptr_t up, mpi_ptr_t vp, mpi_size_t usize,
144 unsigned long op_enable)
145 {
146 /* Note: dual mask with AND/OR used for EM leakage mitigation */
147 mpi_limb_t mask1 = ct_limb_gen_mask(op_enable);
148 mpi_limb_t mask2 = ct_limb_gen_inv_mask(op_enable);
149 mpi_size_t i;
150
151 for (i = 0; i < usize; i++)
152 {
153 mpi_limb_t u = up[i];
154 mpi_limb_t v = vp[i];
155 up[i] = (u & mask2) | (v & mask1);
156 vp[i] = (u & mask1) | (v & mask2);
157 }
158 }
159
160
161 /*
162 * W = -U when OP_ENABLED=1
163 * otherwise, W = U
164 */
165 void
166 _gcry_mpih_abs_cond (mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
167 unsigned long op_enable)
168 {
169 /* Note: dual mask with AND/OR used for EM leakage mitigation */
170 mpi_limb_t mask1 = ct_limb_gen_mask(op_enable);
171 mpi_limb_t mask2 = ct_limb_gen_inv_mask(op_enable);
172 mpi_limb_t cy = op_enable;
173 mpi_size_t i;
174
175 for (i = 0; i < usize; i++)
176 {
177 mpi_limb_t u = up[i];
178 mpi_limb_t x = ~u + cy;
179
180 cy = mpih_ct_limb_less_than(x, ~u);
181 wp[i] = (u & mask2) | (x & mask1);
182 }
183 }
184
185
186 /*
187 * Allocating memory for W,
188 * compute W = V % U, then return W
189 */
190 mpi_ptr_t
191 _gcry_mpih_mod_lli (mpi_ptr_t vp, mpi_size_t vsize,
192 mpi_ptr_t up, mpi_size_t usize)
193 {
194 int secure;
195 mpi_ptr_t rp;
196 mpi_size_t i;
197
198 secure = _gcry_is_secure (vp);
199 rp = mpi_alloc_limb_space (usize, secure);
200 MPN_ZERO (rp, usize);
201
202 for (i = 0; i < vsize * BITS_PER_MPI_LIMB; i++)
203 {
204 unsigned int j = vsize * BITS_PER_MPI_LIMB - 1 - i;
205 unsigned int limbno = j / BITS_PER_MPI_LIMB;
206 unsigned int bitno = j % BITS_PER_MPI_LIMB;
207 mpi_limb_t limb = vp[limbno];
208 unsigned int the_bit = (limb >> bitno) & 1;
209 mpi_limb_t underflow;
210 mpi_limb_t overflow;
211
212 overflow = _gcry_mpih_lshift (rp, rp, usize, 1);
213 rp[0] |= the_bit;
214
215 underflow = _gcry_mpih_sub_n (rp, rp, up, usize);
216 mpih_add_n_cond (rp, rp, up, usize, overflow ^ underflow);
217 }
218
219 return rp;
220 }
221
222 int
223 _gcry_mpih_cmp_ui (mpi_ptr_t up, mpi_size_t usize, unsigned long v)
224 {
225 unsigned long is_all_zero = ct_ulong_gen_mask(1);
226 int cmp0;
227 mpi_size_t i;
228
229 cmp0 = -mpih_ct_limb_less_than (up[0], v);
230 cmp0 |= mpih_ct_limb_greater_than (up[0], v);
231
232 for (i = 1; i < usize; i++)
233 is_all_zero &= ct_ulong_gen_mask(mpih_limb_is_zero (up[i]));
234
235 return (int)((cmp0 & is_all_zero) | (~is_all_zero & 1));
236 }
237
238 /* Do same calculation as _gcry_mpih_cmp does, Least Leak Intended.
239 * Return 1 if U > V, 0 if they are equal, and -1 if U < V. */
240 int
241 _gcry_mpih_cmp_lli (mpi_ptr_t up, mpi_ptr_t vp, mpi_size_t size)
242 {
243 mpi_size_t i;
244 mpi_limb_t res_gt = 0;
245 mpi_limb_t res_lt = 0;
246
247 for (i = 0; i < size ; i++)
248 {
249 mpi_limb_t gt, lt, eq, neq;
250 gt = mpih_ct_limb_greater_than (up[i], vp[i]);
251 lt = mpih_ct_limb_less_than (up[i], vp[i]);
252 neq = ct_limb_gen_mask (gt | lt);
253 eq = ct_limb_gen_inv_mask (gt | lt);
254 res_gt = (eq & res_gt) | (neq & gt);
255 res_lt = (eq & res_lt) | (neq & lt);
256 }
257
258 return (int)(res_gt - res_lt); /* return 0 if U==V, 1 if U>V, -1 if U<V */
259 }
260
261
262 /*
263 * Lookup an MPI value from TABLE at IDX, and put into RP.
264 * The size of the MPI value is N limbs.
265 * TABLE has NENTS entries.
266 *
267 * Note: This is an implementation which accesses all the entries in
268 * the table, so that it can mitigate cache timing attacks. For some
269 * architectures, there may be possible optimization:
270 *
271 * - Access an entry only, with an instruction like
272 * __mm_stream_load_si128 (it makes sense when table is larger and
273 * read-only, and no timing difference)
274 *
275 */
276 void
277 _gcry_mpih_lookup_lli (mpi_ptr_t rp, const mpi_limb_t *table,
278 mpi_size_t n, mpi_size_t nents, mpi_size_t idx)
279 {
280 mpi_size_t i, k;
281 const mpi_limb_t *tp = table;
282
283 for (k = 0; k < nents; k++)
284 {
285 unsigned long idx_neq_k = ct_is_not_zero (idx ^ k);
286 for (i = 0; i < n; i++)
287 rp[i] = ct_limb_select (rp[i], tp[i], idx_neq_k);
288 tp += n;
289 }
290 }