Coverage Report

Created: 2024-11-21 06:47

/src/boringssl/crypto/fipsmodule/bn/shift.c.inc
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2
 * All rights reserved.
3
 *
4
 * This package is an SSL implementation written
5
 * by Eric Young (eay@cryptsoft.com).
6
 * The implementation was written so as to conform with Netscapes SSL.
7
 *
8
 * This library is free for commercial and non-commercial use as long as
9
 * the following conditions are aheared to.  The following conditions
10
 * apply to all code found in this distribution, be it the RC4, RSA,
11
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12
 * included with this distribution is covered by the same copyright terms
13
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14
 *
15
 * Copyright remains Eric Young's, and as such any Copyright notices in
16
 * the code are not to be removed.
17
 * If this package is used in a product, Eric Young should be given attribution
18
 * as the author of the parts of the library used.
19
 * This can be in the form of a textual message at program startup or
20
 * in documentation (online or textual) provided with the package.
21
 *
22
 * Redistribution and use in source and binary forms, with or without
23
 * modification, are permitted provided that the following conditions
24
 * are met:
25
 * 1. Redistributions of source code must retain the copyright
26
 *    notice, this list of conditions and the following disclaimer.
27
 * 2. Redistributions in binary form must reproduce the above copyright
28
 *    notice, this list of conditions and the following disclaimer in the
29
 *    documentation and/or other materials provided with the distribution.
30
 * 3. All advertising materials mentioning features or use of this software
31
 *    must display the following acknowledgement:
32
 *    "This product includes cryptographic software written by
33
 *     Eric Young (eay@cryptsoft.com)"
34
 *    The word 'cryptographic' can be left out if the rouines from the library
35
 *    being used are not cryptographic related :-).
36
 * 4. If you include any Windows specific code (or a derivative thereof) from
37
 *    the apps directory (application code) you must include an acknowledgement:
38
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
 * SUCH DAMAGE.
51
 *
52
 * The licence and distribution terms for any publically available version or
53
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
54
 * copied and put under another distribution licence
55
 * [including the GNU Public Licence.] */
56
57
#include <openssl/bn.h>
58
59
#include <assert.h>
60
#include <string.h>
61
62
#include <openssl/err.h>
63
64
#include "internal.h"
65
66
67
3.27M
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n) {
68
3.27M
  int i, nw, lb, rb;
69
3.27M
  BN_ULONG *t, *f;
70
3.27M
  BN_ULONG l;
71
72
3.27M
  if (n < 0) {
73
0
    OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
74
0
    return 0;
75
0
  }
76
77
3.27M
  r->neg = a->neg;
78
3.27M
  nw = n / BN_BITS2;
79
3.27M
  if (!bn_wexpand(r, a->width + nw + 1)) {
80
0
    return 0;
81
0
  }
82
3.27M
  lb = n % BN_BITS2;
83
3.27M
  rb = BN_BITS2 - lb;
84
3.27M
  f = a->d;
85
3.27M
  t = r->d;
86
3.27M
  t[a->width + nw] = 0;
87
3.27M
  if (lb == 0) {
88
14.5M
    for (i = a->width - 1; i >= 0; i--) {
89
11.3M
      t[nw + i] = f[i];
90
11.3M
    }
91
3.23M
  } else {
92
231k
    for (i = a->width - 1; i >= 0; i--) {
93
190k
      l = f[i];
94
190k
      t[nw + i + 1] |= l >> rb;
95
190k
      t[nw + i] = l << lb;
96
190k
    }
97
41.8k
  }
98
3.27M
  OPENSSL_memset(t, 0, nw * sizeof(t[0]));
99
3.27M
  r->width = a->width + nw + 1;
100
3.27M
  bn_set_minimal_width(r);
101
102
3.27M
  return 1;
103
3.27M
}
104
105
1.51k
int BN_lshift1(BIGNUM *r, const BIGNUM *a) {
106
1.51k
  BN_ULONG *ap, *rp, t, c;
107
1.51k
  int i;
108
109
1.51k
  if (r != a) {
110
1.51k
    r->neg = a->neg;
111
1.51k
    if (!bn_wexpand(r, a->width + 1)) {
112
0
      return 0;
113
0
    }
114
1.51k
    r->width = a->width;
115
1.51k
  } else {
116
0
    if (!bn_wexpand(r, a->width + 1)) {
117
0
      return 0;
118
0
    }
119
0
  }
120
1.51k
  ap = a->d;
121
1.51k
  rp = r->d;
122
1.51k
  c = 0;
123
7.58k
  for (i = 0; i < a->width; i++) {
124
6.07k
    t = *(ap++);
125
6.07k
    *(rp++) = (t << 1) | c;
126
6.07k
    c = t >> (BN_BITS2 - 1);
127
6.07k
  }
128
1.51k
  if (c) {
129
403
    *rp = 1;
130
403
    r->width++;
131
403
  }
132
133
1.51k
  return 1;
134
1.51k
}
135
136
void bn_rshift_words(BN_ULONG *r, const BN_ULONG *a, unsigned shift,
137
879k
                     size_t num) {
138
879k
  unsigned shift_bits = shift % BN_BITS2;
139
879k
  size_t shift_words = shift / BN_BITS2;
140
879k
  if (shift_words >= num) {
141
8.97k
    OPENSSL_memset(r, 0, num * sizeof(BN_ULONG));
142
8.97k
    return;
143
8.97k
  }
144
870k
  if (shift_bits == 0) {
145
254k
    OPENSSL_memmove(r, a + shift_words, (num - shift_words) * sizeof(BN_ULONG));
146
616k
  } else {
147
2.79M
    for (size_t i = shift_words; i < num - 1; i++) {
148
2.17M
      r[i - shift_words] =
149
2.17M
          (a[i] >> shift_bits) | (a[i + 1] << (BN_BITS2 - shift_bits));
150
2.17M
    }
151
616k
    r[num - 1 - shift_words] = a[num - 1] >> shift_bits;
152
616k
  }
153
870k
  OPENSSL_memset(r + num - shift_words, 0, shift_words * sizeof(BN_ULONG));
154
870k
}
155
156
863k
int BN_rshift(BIGNUM *r, const BIGNUM *a, int n) {
157
863k
  if (n < 0) {
158
0
    OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
159
0
    return 0;
160
0
  }
161
162
863k
  if (!bn_wexpand(r, a->width)) {
163
0
    return 0;
164
0
  }
165
863k
  bn_rshift_words(r->d, a->d, n, a->width);
166
863k
  r->neg = a->neg;
167
863k
  r->width = a->width;
168
863k
  bn_set_minimal_width(r);
169
863k
  return 1;
170
863k
}
171
172
int bn_rshift_secret_shift(BIGNUM *r, const BIGNUM *a, unsigned n,
173
1.90k
                           BN_CTX *ctx) {
174
1.90k
  int ret = 0;
175
1.90k
  BN_CTX_start(ctx);
176
1.90k
  BIGNUM *tmp = BN_CTX_get(ctx);
177
1.90k
  if (tmp == NULL ||
178
1.90k
      !BN_copy(r, a) ||
179
1.90k
      !bn_wexpand(tmp, r->width)) {
180
0
    goto err;
181
0
  }
182
183
  // Shift conditionally by powers of two.
184
1.90k
  unsigned max_bits = BN_BITS2 * r->width;
185
17.8k
  for (unsigned i = 0; (max_bits >> i) != 0; i++) {
186
15.9k
    BN_ULONG mask = (n >> i) & 1;
187
15.9k
    mask = 0 - mask;
188
15.9k
    bn_rshift_words(tmp->d, r->d, 1u << i, r->width);
189
15.9k
    bn_select_words(r->d, mask, tmp->d /* apply shift */,
190
15.9k
                    r->d /* ignore shift */, r->width);
191
15.9k
  }
192
193
1.90k
  ret = 1;
194
195
1.90k
err:
196
1.90k
  BN_CTX_end(ctx);
197
1.90k
  return ret;
198
1.90k
}
199
200
6.26M
void bn_rshift1_words(BN_ULONG *r, const BN_ULONG *a, size_t num) {
201
6.26M
  if (num == 0) {
202
28
    return;
203
28
  }
204
32.2M
  for (size_t i = 0; i < num - 1; i++) {
205
26.0M
    r[i] = (a[i] >> 1) | (a[i + 1] << (BN_BITS2 - 1));
206
26.0M
  }
207
6.26M
  r[num - 1] = a[num - 1] >> 1;
208
6.26M
}
209
210
182k
int BN_rshift1(BIGNUM *r, const BIGNUM *a) {
211
182k
  if (!bn_wexpand(r, a->width)) {
212
0
    return 0;
213
0
  }
214
182k
  bn_rshift1_words(r->d, a->d, a->width);
215
182k
  r->width = a->width;
216
182k
  r->neg = a->neg;
217
182k
  bn_set_minimal_width(r);
218
182k
  return 1;
219
182k
}
220
221
5.86k
int BN_set_bit(BIGNUM *a, int n) {
222
5.86k
  if (n < 0) {
223
0
    return 0;
224
0
  }
225
226
5.86k
  int i = n / BN_BITS2;
227
5.86k
  int j = n % BN_BITS2;
228
5.86k
  if (a->width <= i) {
229
5.86k
    if (!bn_wexpand(a, i + 1)) {
230
0
      return 0;
231
0
    }
232
44.1k
    for (int k = a->width; k < i + 1; k++) {
233
38.3k
      a->d[k] = 0;
234
38.3k
    }
235
5.86k
    a->width = i + 1;
236
5.86k
  }
237
238
5.86k
  a->d[i] |= (((BN_ULONG)1) << j);
239
240
5.86k
  return 1;
241
5.86k
}
242
243
0
int BN_clear_bit(BIGNUM *a, int n) {
244
0
  int i, j;
245
246
0
  if (n < 0) {
247
0
    return 0;
248
0
  }
249
250
0
  i = n / BN_BITS2;
251
0
  j = n % BN_BITS2;
252
0
  if (a->width <= i) {
253
0
    return 0;
254
0
  }
255
256
0
  a->d[i] &= (~(((BN_ULONG)1) << j));
257
0
  bn_set_minimal_width(a);
258
0
  return 1;
259
0
}
260
261
3.88M
int bn_is_bit_set_words(const BN_ULONG *a, size_t num, size_t bit) {
262
3.88M
  size_t i = bit / BN_BITS2;
263
3.88M
  size_t j = bit % BN_BITS2;
264
3.88M
  if (i >= num) {
265
0
    return 0;
266
0
  }
267
3.88M
  return (a[i] >> j) & 1;
268
3.88M
}
269
270
3.88M
int BN_is_bit_set(const BIGNUM *a, int n) {
271
3.88M
  if (n < 0) {
272
0
    return 0;
273
0
  }
274
3.88M
  return bn_is_bit_set_words(a->d, a->width, n);
275
3.88M
}
276
277
0
int BN_mask_bits(BIGNUM *a, int n) {
278
0
  if (n < 0) {
279
0
    return 0;
280
0
  }
281
282
0
  int w = n / BN_BITS2;
283
0
  int b = n % BN_BITS2;
284
0
  if (w >= a->width) {
285
0
    return 1;
286
0
  }
287
0
  if (b == 0) {
288
0
    a->width = w;
289
0
  } else {
290
0
    a->width = w + 1;
291
0
    a->d[w] &= ~(BN_MASK2 << b);
292
0
  }
293
294
0
  bn_set_minimal_width(a);
295
0
  return 1;
296
0
}
297
298
7.10k
static int bn_count_low_zero_bits_word(BN_ULONG l) {
299
7.10k
  static_assert(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
300
7.10k
                "crypto_word_t is too small");
301
7.10k
  static_assert(sizeof(int) <= sizeof(crypto_word_t),
302
7.10k
                "crypto_word_t is too small");
303
7.10k
  static_assert(BN_BITS2 == sizeof(BN_ULONG) * 8, "BN_ULONG has padding bits");
304
  // C has very bizarre rules for types smaller than an int.
305
7.10k
  static_assert(sizeof(BN_ULONG) >= sizeof(int),
306
7.10k
                "BN_ULONG gets promoted to int");
307
308
7.10k
  crypto_word_t mask;
309
7.10k
  int bits = 0;
310
311
7.10k
#if BN_BITS2 > 32
312
  // Check if the lower half of |x| are all zero.
313
7.10k
  mask = constant_time_is_zero_w(l << (BN_BITS2 - 32));
314
  // If the lower half is all zeros, it is included in the bit count and we
315
  // count the upper half. Otherwise, we count the lower half.
316
7.10k
  bits += 32 & mask;
317
7.10k
  l = constant_time_select_w(mask, l >> 32, l);
318
7.10k
#endif
319
320
  // The remaining blocks are analogous iterations at lower powers of two.
321
7.10k
  mask = constant_time_is_zero_w(l << (BN_BITS2 - 16));
322
7.10k
  bits += 16 & mask;
323
7.10k
  l = constant_time_select_w(mask, l >> 16, l);
324
325
7.10k
  mask = constant_time_is_zero_w(l << (BN_BITS2 - 8));
326
7.10k
  bits += 8 & mask;
327
7.10k
  l = constant_time_select_w(mask, l >> 8, l);
328
329
7.10k
  mask = constant_time_is_zero_w(l << (BN_BITS2 - 4));
330
7.10k
  bits += 4 & mask;
331
7.10k
  l = constant_time_select_w(mask, l >> 4, l);
332
333
7.10k
  mask = constant_time_is_zero_w(l << (BN_BITS2 - 2));
334
7.10k
  bits += 2 & mask;
335
7.10k
  l = constant_time_select_w(mask, l >> 2, l);
336
337
7.10k
  mask = constant_time_is_zero_w(l << (BN_BITS2 - 1));
338
7.10k
  bits += 1 & mask;
339
340
7.10k
  return bits;
341
7.10k
}
342
343
1.90k
int BN_count_low_zero_bits(const BIGNUM *bn) {
344
1.90k
  static_assert(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
345
1.90k
                "crypto_word_t is too small");
346
1.90k
  static_assert(sizeof(int) <= sizeof(crypto_word_t),
347
1.90k
                "crypto_word_t is too small");
348
349
1.90k
  int ret = 0;
350
1.90k
  crypto_word_t saw_nonzero = 0;
351
9.01k
  for (int i = 0; i < bn->width; i++) {
352
7.10k
    crypto_word_t nonzero = ~constant_time_is_zero_w(bn->d[i]);
353
7.10k
    crypto_word_t first_nonzero = ~saw_nonzero & nonzero;
354
7.10k
    saw_nonzero |= nonzero;
355
356
7.10k
    int bits = bn_count_low_zero_bits_word(bn->d[i]);
357
7.10k
    ret |= first_nonzero & (i * BN_BITS2 + bits);
358
7.10k
  }
359
360
  // If got to the end of |bn| and saw no non-zero words, |bn| is zero. |ret|
361
  // will then remain zero.
362
1.90k
  return ret;
363
1.90k
}