Coverage Report

Created: 2023-06-07 07:13

/src/boringssl/crypto/fipsmodule/bn/cmp.c
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
61
#include <openssl/mem.h>
62
63
#include "internal.h"
64
#include "../../internal.h"
65
66
67
static int bn_cmp_words_consttime(const BN_ULONG *a, size_t a_len,
68
2.54M
                                  const BN_ULONG *b, size_t b_len) {
69
2.54M
  static_assert(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
70
2.54M
                "crypto_word_t is too small");
71
2.54M
  int ret = 0;
72
  // Process the common words in little-endian order.
73
2.54M
  size_t min = a_len < b_len ? a_len : b_len;
74
40.9M
  for (size_t i = 0; i < min; i++) {
75
38.3M
    crypto_word_t eq = constant_time_eq_w(a[i], b[i]);
76
38.3M
    crypto_word_t lt = constant_time_lt_w(a[i], b[i]);
77
38.3M
    ret =
78
38.3M
        constant_time_select_int(eq, ret, constant_time_select_int(lt, -1, 1));
79
38.3M
  }
80
81
  // If |a| or |b| has non-zero words beyond |min|, they take precedence.
82
2.54M
  if (a_len < b_len) {
83
156k
    crypto_word_t mask = 0;
84
2.28M
    for (size_t i = a_len; i < b_len; i++) {
85
2.13M
      mask |= b[i];
86
2.13M
    }
87
156k
    ret = constant_time_select_int(constant_time_is_zero_w(mask), ret, -1);
88
2.39M
  } else if (b_len < a_len) {
89
256k
    crypto_word_t mask = 0;
90
5.05M
    for (size_t i = b_len; i < a_len; i++) {
91
4.79M
      mask |= a[i];
92
4.79M
    }
93
256k
    ret = constant_time_select_int(constant_time_is_zero_w(mask), ret, 1);
94
256k
  }
95
96
2.54M
  return ret;
97
2.54M
}
98
99
2.33M
int BN_ucmp(const BIGNUM *a, const BIGNUM *b) {
100
2.33M
  return bn_cmp_words_consttime(a->d, a->width, b->d, b->width);
101
2.33M
}
102
103
133k
int BN_cmp(const BIGNUM *a, const BIGNUM *b) {
104
133k
  if ((a == NULL) || (b == NULL)) {
105
0
    if (a != NULL) {
106
0
      return -1;
107
0
    } else if (b != NULL) {
108
0
      return 1;
109
0
    } else {
110
0
      return 0;
111
0
    }
112
0
  }
113
114
  // We do not attempt to process the sign bit in constant time. Negative
115
  // |BIGNUM|s should never occur in crypto, only calculators.
116
133k
  if (a->neg != b->neg) {
117
0
    if (a->neg) {
118
0
      return -1;
119
0
    }
120
0
    return 1;
121
0
  }
122
123
133k
  int ret = BN_ucmp(a, b);
124
133k
  return a->neg ? -ret : ret;
125
133k
}
126
127
214k
int bn_less_than_words(const BN_ULONG *a, const BN_ULONG *b, size_t len) {
128
214k
  return bn_cmp_words_consttime(a, len, b, len) < 0;
129
214k
}
130
131
4.31M
int BN_abs_is_word(const BIGNUM *bn, BN_ULONG w) {
132
4.31M
  if (bn->width == 0) {
133
0
    return w == 0;
134
0
  }
135
4.31M
  BN_ULONG mask = bn->d[0] ^ w;
136
16.9M
  for (int i = 1; i < bn->width; i++) {
137
12.6M
    mask |= bn->d[i];
138
12.6M
  }
139
4.31M
  return mask == 0;
140
4.31M
}
141
142
0
int BN_cmp_word(const BIGNUM *a, BN_ULONG b) {
143
0
  BIGNUM b_bn;
144
0
  BN_init(&b_bn);
145
146
0
  b_bn.d = &b;
147
0
  b_bn.width = b > 0;
148
0
  b_bn.dmax = 1;
149
0
  b_bn.flags = BN_FLG_STATIC_DATA;
150
0
  return BN_cmp(a, &b_bn);
151
0
}
152
153
12.8M
int BN_is_zero(const BIGNUM *bn) {
154
12.8M
  return bn_fits_in_words(bn, 0);
155
12.8M
}
156
157
4.31M
int BN_is_one(const BIGNUM *bn) {
158
4.31M
  return bn->neg == 0 && BN_abs_is_word(bn, 1);
159
4.31M
}
160
161
0
int BN_is_word(const BIGNUM *bn, BN_ULONG w) {
162
0
  return BN_abs_is_word(bn, w) && (w == 0 || bn->neg == 0);
163
0
}
164
165
4.04M
int BN_is_odd(const BIGNUM *bn) {
166
4.04M
  return bn->width > 0 && (bn->d[0] & 1) == 1;
167
4.04M
}
168
169
0
int BN_is_pow2(const BIGNUM *bn) {
170
0
  int width = bn_minimal_width(bn);
171
0
  if (width == 0 || bn->neg) {
172
0
    return 0;
173
0
  }
174
175
0
  for (int i = 0; i < width - 1; i++) {
176
0
    if (bn->d[i] != 0) {
177
0
      return 0;
178
0
    }
179
0
  }
180
181
0
  return 0 == (bn->d[width-1] & (bn->d[width-1] - 1));
182
0
}
183
184
33.1k
int BN_equal_consttime(const BIGNUM *a, const BIGNUM *b) {
185
33.1k
  BN_ULONG mask = 0;
186
  // If |a| or |b| has more words than the other, all those words must be zero.
187
33.1k
  for (int i = a->width; i < b->width; i++) {
188
0
    mask |= b->d[i];
189
0
  }
190
33.1k
  for (int i = b->width; i < a->width; i++) {
191
0
    mask |= a->d[i];
192
0
  }
193
  // Common words must match.
194
33.1k
  int min = a->width < b->width ? a->width : b->width;
195
1.09M
  for (int i = 0; i < min; i++) {
196
1.06M
    mask |= (a->d[i] ^ b->d[i]);
197
1.06M
  }
198
  // The sign bit must match.
199
33.1k
  mask |= (a->neg ^ b->neg);
200
33.1k
  return mask == 0;
201
33.1k
}