File: mpih-const-time.c

package info (click to toggle)
libgcrypt20 1.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 19,828 kB
  • sloc: ansic: 188,728; asm: 59,386; sh: 5,920; makefile: 924; sed: 37
file content (290 lines) | stat: -rw-r--r-- 7,502 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* mpih-const-time.c  -  Constant-time MPI helper functions
 *      Copyright (C) 2020, 2025  g10 Code GmbH
 *
 * This file is part of Libgcrypt.
 *
 * Libgcrypt is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * Libgcrypt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include "mpi-internal.h"
#include "g10lib.h"
#include "const-time.h"
#include "longlong.h"

#define A_LIMB_1 ((mpi_limb_t)1)


/*
 * Return 1 if X > Y and otherwise return 0.
 */
static inline mpi_limb_t
mpih_ct_limb_greater_than (mpi_limb_t x, mpi_limb_t y)
{
  mpi_limb_t diff_hi, diff_lo;
  sub_ddmmss (diff_hi, diff_lo, 0, y, 0, x);
  (void)diff_lo;
  return diff_hi >> (BITS_PER_MPI_LIMB - 1);
}


/*
 * Return 1 if X < Y and otherwise return 0.
 */
static inline mpi_limb_t
mpih_ct_limb_less_than (mpi_limb_t x, mpi_limb_t y)
{
  return mpih_ct_limb_greater_than (y, x);
}


/*
 *  W = U when OP_ENABLED=1
 *  otherwise, W keeps old value
 */
void
_gcry_mpih_set_cond (mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
                     unsigned long op_enable)
{
  /* Note: dual mask with AND/OR used for EM leakage mitigation */
  mpi_limb_t mask1 = ct_limb_gen_mask(op_enable);
  mpi_limb_t mask2 = ct_limb_gen_inv_mask(op_enable);
  mpi_size_t i;

  for (i = 0; i < usize; i++)
    {
      wp[i] = (wp[i] & mask2) | (up[i] & mask1);
    }
}


/*
 *  W = U + V when OP_ENABLED=1
 *  otherwise, W = U
 */
mpi_limb_t
_gcry_mpih_add_n_cond (mpi_ptr_t wp, mpi_ptr_t up, mpi_ptr_t vp,
                       mpi_size_t usize, unsigned long op_enable)
{
  /* Note: dual mask with AND/OR used for EM leakage mitigation */
  mpi_limb_t mask1 = ct_limb_gen_mask(op_enable);
  mpi_limb_t mask2 = ct_limb_gen_inv_mask(op_enable);
  mpi_size_t i;
  mpi_limb_t cy;

  cy = 0;
  for (i = 0; i < usize; i++)
    {
      mpi_limb_t u = up[i];
      mpi_limb_t x = u + vp[i];
      mpi_limb_t cy1 = mpih_ct_limb_less_than(x, u);
      mpi_limb_t cy2;

      x = x + cy;
      cy2 = mpih_ct_limb_less_than(x, cy);
      cy = cy1 | cy2;
      wp[i] = (u & mask2) | (x & mask1);
    }

  return cy & mask1;
}


/*
 *  W = U - V when OP_ENABLED=1
 *  otherwise, W = U
 */
mpi_limb_t
_gcry_mpih_sub_n_cond (mpi_ptr_t wp, mpi_ptr_t up, mpi_ptr_t vp,
                       mpi_size_t usize, unsigned long op_enable)
{
  /* Note: dual mask with AND/OR used for EM leakage mitigation */
  mpi_limb_t mask1 = ct_limb_gen_mask(op_enable);
  mpi_limb_t mask2 = ct_limb_gen_inv_mask(op_enable);
  mpi_size_t i;
  mpi_limb_t cy;

  cy = 0;
  for (i = 0; i < usize; i++)
    {
      mpi_limb_t u = up[i];
      mpi_limb_t x = u - vp[i];
      mpi_limb_t cy1 = mpih_ct_limb_greater_than(x, u);
      mpi_limb_t cy2;

      cy2 = mpih_ct_limb_less_than(x, cy);
      x = x - cy;
      cy = cy1 | cy2;
      wp[i] = (u & mask2) | (x & mask1);
    }

  return cy & mask1;
}


/*
 *  Swap value of U and V when OP_ENABLED=1
 *  otherwise, no change
 */
void
_gcry_mpih_swap_cond (mpi_ptr_t up, mpi_ptr_t vp, mpi_size_t usize,
                      unsigned long op_enable)
{
  /* Note: dual mask with AND/OR used for EM leakage mitigation */
  mpi_limb_t mask1 = ct_limb_gen_mask(op_enable);
  mpi_limb_t mask2 = ct_limb_gen_inv_mask(op_enable);
  mpi_size_t i;

  for (i = 0; i < usize; i++)
    {
      mpi_limb_t u = up[i];
      mpi_limb_t v = vp[i];
      up[i] = (u & mask2) | (v & mask1);
      vp[i] = (u & mask1) | (v & mask2);
    }
}


/*
 *  W = -U when OP_ENABLED=1
 *  otherwise, W = U
 */
void
_gcry_mpih_abs_cond (mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
                     unsigned long op_enable)
{
  /* Note: dual mask with AND/OR used for EM leakage mitigation */
  mpi_limb_t mask1 = ct_limb_gen_mask(op_enable);
  mpi_limb_t mask2 = ct_limb_gen_inv_mask(op_enable);
  mpi_limb_t cy = op_enable;
  mpi_size_t i;

  for (i = 0; i < usize; i++)
    {
      mpi_limb_t u = up[i];
      mpi_limb_t x = ~u + cy;

      cy = mpih_ct_limb_less_than(x, ~u);
      wp[i] = (u & mask2) | (x & mask1);
    }
}


/*
 * Allocating memory for W,
 * compute W = V % U, then return W
 */
mpi_ptr_t
_gcry_mpih_mod_lli (mpi_ptr_t vp, mpi_size_t vsize,
                    mpi_ptr_t up, mpi_size_t usize)
{
  int secure;
  mpi_ptr_t rp;
  mpi_size_t i;

  secure = _gcry_is_secure (vp);
  rp = mpi_alloc_limb_space (usize, secure);
  MPN_ZERO (rp, usize);

  for (i = 0; i < vsize * BITS_PER_MPI_LIMB; i++)
    {
      unsigned int j = vsize * BITS_PER_MPI_LIMB - 1 - i;
      unsigned int limbno = j / BITS_PER_MPI_LIMB;
      unsigned int bitno = j % BITS_PER_MPI_LIMB;
      mpi_limb_t limb = vp[limbno];
      unsigned int the_bit = (limb >> bitno) & 1;
      mpi_limb_t underflow;
      mpi_limb_t overflow;

      overflow = _gcry_mpih_lshift (rp, rp, usize, 1);
      rp[0] |= the_bit;

      underflow = _gcry_mpih_sub_n (rp, rp, up, usize);
      mpih_add_n_cond (rp, rp, up, usize, overflow ^ underflow);
    }

  return rp;
}

int
_gcry_mpih_cmp_ui (mpi_ptr_t up, mpi_size_t usize, unsigned long v)
{
  unsigned long is_all_zero = ct_ulong_gen_mask(1);
  int cmp0;
  mpi_size_t i;

  cmp0 = -mpih_ct_limb_less_than (up[0], v);
  cmp0 |= mpih_ct_limb_greater_than (up[0], v);

  for (i = 1; i < usize; i++)
    is_all_zero &= ct_ulong_gen_mask(mpih_limb_is_zero (up[i]));

  return (int)((cmp0 & is_all_zero) | (~is_all_zero & 1));
}

/* Do same calculation as _gcry_mpih_cmp does, Least Leak Intended.
 * Return 1 if U > V, 0 if they are equal, and -1 if U < V.  */
int
_gcry_mpih_cmp_lli (mpi_ptr_t up, mpi_ptr_t vp, mpi_size_t size)
{
  mpi_size_t i;
  mpi_limb_t res_gt = 0;
  mpi_limb_t res_lt = 0;

  for (i = 0; i < size ; i++)
    {
      mpi_limb_t gt, lt, eq, neq;
      gt = mpih_ct_limb_greater_than (up[i], vp[i]);
      lt = mpih_ct_limb_less_than (up[i], vp[i]);
      neq = ct_limb_gen_mask (gt | lt);
      eq = ct_limb_gen_inv_mask (gt | lt);
      res_gt = (eq & res_gt) | (neq & gt);
      res_lt = (eq & res_lt) | (neq & lt);
    }

  return (int)(res_gt - res_lt); /* return 0 if U==V, 1 if U>V, -1 if U<V */
}


/*
 * Lookup an MPI value from TABLE at IDX, and put into RP.
 * The size of the MPI value is N limbs.
 * TABLE has NENTS entries.
 *
 * Note: This is an implementation which accesses all the entries in
 * the table, so that it can mitigate cache timing attacks.  For some
 * architectures, there may be possible optimization:
 *
 *  - Access an entry only, with an instruction like
 *    __mm_stream_load_si128 (it makes sense when table is larger and
 *    read-only, and no timing difference)
 *
 */
void
_gcry_mpih_lookup_lli (mpi_ptr_t rp, const mpi_limb_t *table,
                       mpi_size_t n, mpi_size_t nents, mpi_size_t idx)
{
  mpi_size_t i, k;
  const mpi_limb_t *tp = table;

  for (k = 0; k < nents; k++)
    {
      unsigned long idx_neq_k = ct_is_not_zero (idx ^ k);
      for (i = 0; i < n; i++)
        rp[i] = ct_limb_select (rp[i], tp[i], idx_neq_k);
      tp += n;
    }
}