| // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/bn.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <string.h> |
| 19 |
| 20 | #include <openssl/err.h> |
| 21 |
| 22 | #include "internal.h" |
| 23 |
| 24 |
| 25 | int BN_lshift(BIGNUM *r, const BIGNUM *a, int n) { |
| 26 | int i, nw, lb, rb; |
| 27 | BN_ULONG *t, *f; |
| 28 | BN_ULONG l; |
| 29 |
| 30 | if (n < 0) { |
| 31 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
| 32 | return 0; |
| 33 | } |
| 34 |
| 35 | r->neg = a->neg; |
| 36 | nw = n / BN_BITS2; |
| 37 | if (!bn_wexpand(r, a->width + nw + 1)) { |
| 38 | return 0; |
| 39 | } |
| 40 | lb = n % BN_BITS2; |
| 41 | rb = BN_BITS2 - lb; |
| 42 | f = a->d; |
| 43 | t = r->d; |
| 44 | t[a->width + nw] = 0; |
| 45 | if (lb == 0) { |
| 46 | for (i = a->width - 1; i >= 0; i--) { |
| 47 | t[nw + i] = f[i]; |
| 48 | } |
| 49 | } else { |
| 50 | for (i = a->width - 1; i >= 0; i--) { |
| 51 | l = f[i]; |
| 52 | t[nw + i + 1] |= l >> rb; |
| 53 | t[nw + i] = l << lb; |
| 54 | } |
| 55 | } |
| 56 | OPENSSL_memset(t, 0, nw * sizeof(t[0])); |
| 57 | r->width = a->width + nw + 1; |
| 58 | bn_set_minimal_width(r); |
| 59 |
| 60 | return 1; |
| 61 | } |
| 62 |
| 63 | int BN_lshift1(BIGNUM *r, const BIGNUM *a) { |
| 64 | BN_ULONG *ap, *rp, t, c; |
| 65 | int i; |
| 66 |
| 67 | if (r != a) { |
| 68 | r->neg = a->neg; |
| 69 | if (!bn_wexpand(r, a->width + 1)) { |
| 70 | return 0; |
| 71 | } |
| 72 | r->width = a->width; |
| 73 | } else { |
| 74 | if (!bn_wexpand(r, a->width + 1)) { |
| 75 | return 0; |
| 76 | } |
| 77 | } |
| 78 | ap = a->d; |
| 79 | rp = r->d; |
| 80 | c = 0; |
| 81 | for (i = 0; i < a->width; i++) { |
| 82 | t = *(ap++); |
| 83 | *(rp++) = (t << 1) | c; |
| 84 | c = t >> (BN_BITS2 - 1); |
| 85 | } |
| 86 | if (c) { |
| 87 | *rp = 1; |
| 88 | r->width++; |
| 89 | } |
| 90 |
| 91 | return 1; |
| 92 | } |
| 93 |
| 94 | void bn_rshift_words(BN_ULONG *r, const BN_ULONG *a, unsigned shift, |
| 95 | size_t num) { |
| 96 | unsigned shift_bits = shift % BN_BITS2; |
| 97 | size_t shift_words = shift / BN_BITS2; |
| 98 | if (shift_words >= num) { |
| 99 | OPENSSL_memset(r, 0, num * sizeof(BN_ULONG)); |
| 100 | return; |
| 101 | } |
| 102 | if (shift_bits == 0) { |
| 103 | OPENSSL_memmove(r, a + shift_words, (num - shift_words) * sizeof(BN_ULONG)); |
| 104 | } else { |
| 105 | for (size_t i = shift_words; i < num - 1; i++) { |
| 106 | r[i - shift_words] = |
| 107 | (a[i] >> shift_bits) | (a[i + 1] << (BN_BITS2 - shift_bits)); |
| 108 | } |
| 109 | r[num - 1 - shift_words] = a[num - 1] >> shift_bits; |
| 110 | } |
| 111 | OPENSSL_memset(r + num - shift_words, 0, shift_words * sizeof(BN_ULONG)); |
| 112 | } |
| 113 |
| 114 | int BN_rshift(BIGNUM *r, const BIGNUM *a, int n) { |
| 115 | if (n < 0) { |
| 116 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
| 117 | return 0; |
| 118 | } |
| 119 |
| 120 | if (!bn_wexpand(r, a->width)) { |
| 121 | return 0; |
| 122 | } |
| 123 | bn_rshift_words(r->d, a->d, n, a->width); |
| 124 | r->neg = a->neg; |
| 125 | r->width = a->width; |
| 126 | bn_set_minimal_width(r); |
| 127 | return 1; |
| 128 | } |
| 129 |
| 130 | int bn_rshift_secret_shift(BIGNUM *r, const BIGNUM *a, unsigned n, |
| 131 | BN_CTX *ctx) { |
| 132 | int ret = 0; |
| 133 | BN_CTX_start(ctx); |
| 134 | BIGNUM *tmp = BN_CTX_get(ctx); |
| 135 | unsigned max_bits; |
| 136 | if (tmp == NULL || !BN_copy(r, a) || !bn_wexpand(tmp, r->width)) { |
| 137 | goto err; |
| 138 | } |
| 139 |
| 140 | // Shift conditionally by powers of two. |
| 141 | max_bits = BN_BITS2 * r->width; |
| 142 | for (unsigned i = 0; (max_bits >> i) != 0; i++) { |
| 143 | BN_ULONG mask = (n >> i) & 1; |
| 144 | mask = 0 - mask; |
| 145 | bn_rshift_words(tmp->d, r->d, 1u << i, r->width); |
| 146 | bn_select_words(r->d, mask, tmp->d /* apply shift */, |
| 147 | r->d /* ignore shift */, r->width); |
| 148 | } |
| 149 |
| 150 | ret = 1; |
| 151 |
| 152 | err: |
| 153 | BN_CTX_end(ctx); |
| 154 | return ret; |
| 155 | } |
| 156 |
| 157 | void bn_rshift1_words(BN_ULONG *r, const BN_ULONG *a, size_t num) { |
| 158 | if (num == 0) { |
| 159 | return; |
| 160 | } |
| 161 | for (size_t i = 0; i < num - 1; i++) { |
| 162 | r[i] = (a[i] >> 1) | (a[i + 1] << (BN_BITS2 - 1)); |
| 163 | } |
| 164 | r[num - 1] = a[num - 1] >> 1; |
| 165 | } |
| 166 |
| 167 | int BN_rshift1(BIGNUM *r, const BIGNUM *a) { |
| 168 | if (!bn_wexpand(r, a->width)) { |
| 169 | return 0; |
| 170 | } |
| 171 | bn_rshift1_words(r->d, a->d, a->width); |
| 172 | r->width = a->width; |
| 173 | r->neg = a->neg; |
| 174 | bn_set_minimal_width(r); |
| 175 | return 1; |
| 176 | } |
| 177 |
| 178 | int BN_set_bit(BIGNUM *a, int n) { |
| 179 | if (n < 0) { |
| 180 | return 0; |
| 181 | } |
| 182 |
| 183 | int i = n / BN_BITS2; |
| 184 | int j = n % BN_BITS2; |
| 185 | if (a->width <= i) { |
| 186 | if (!bn_wexpand(a, i + 1)) { |
| 187 | return 0; |
| 188 | } |
| 189 | for (int k = a->width; k < i + 1; k++) { |
| 190 | a->d[k] = 0; |
| 191 | } |
| 192 | a->width = i + 1; |
| 193 | } |
| 194 |
| 195 | a->d[i] |= (((BN_ULONG)1) << j); |
| 196 |
| 197 | return 1; |
| 198 | } |
| 199 |
| 200 | int BN_clear_bit(BIGNUM *a, int n) { |
| 201 | int i, j; |
| 202 |
| 203 | if (n < 0) { |
| 204 | return 0; |
| 205 | } |
| 206 |
| 207 | i = n / BN_BITS2; |
| 208 | j = n % BN_BITS2; |
| 209 | if (a->width <= i) { |
| 210 | return 0; |
| 211 | } |
| 212 |
| 213 | a->d[i] &= (~(((BN_ULONG)1) << j)); |
| 214 | bn_set_minimal_width(a); |
| 215 | return 1; |
| 216 | } |
| 217 |
| 218 | int bn_is_bit_set_words(const BN_ULONG *a, size_t num, size_t bit) { |
| 219 | size_t i = bit / BN_BITS2; |
| 220 | size_t j = bit % BN_BITS2; |
| 221 | if (i >= num) { |
| 222 | return 0; |
| 223 | } |
| 224 | return (a[i] >> j) & 1; |
| 225 | } |
| 226 |
| 227 | int BN_is_bit_set(const BIGNUM *a, int n) { |
| 228 | if (n < 0) { |
| 229 | return 0; |
| 230 | } |
| 231 | return bn_is_bit_set_words(a->d, a->width, n); |
| 232 | } |
| 233 |
| 234 | int BN_mask_bits(BIGNUM *a, int n) { |
| 235 | if (n < 0) { |
| 236 | return 0; |
| 237 | } |
| 238 |
| 239 | int w = n / BN_BITS2; |
| 240 | int b = n % BN_BITS2; |
| 241 | if (w >= a->width) { |
| 242 | return 1; |
| 243 | } |
| 244 | if (b == 0) { |
| 245 | a->width = w; |
| 246 | } else { |
| 247 | a->width = w + 1; |
| 248 | a->d[w] &= ~(BN_MASK2 << b); |
| 249 | } |
| 250 |
| 251 | bn_set_minimal_width(a); |
| 252 | return 1; |
| 253 | } |
| 254 |
| 255 | static int bn_count_low_zero_bits_word(BN_ULONG l) { |
| 256 | static_assert(sizeof(BN_ULONG) <= sizeof(crypto_word_t), |
| 257 | "crypto_word_t is too small"); |
| 258 | static_assert(sizeof(int) <= sizeof(crypto_word_t), |
| 259 | "crypto_word_t is too small"); |
| 260 | static_assert(BN_BITS2 == sizeof(BN_ULONG) * 8, "BN_ULONG has padding bits"); |
| 261 | // C has very bizarre rules for types smaller than an int. |
| 262 | static_assert(sizeof(BN_ULONG) >= sizeof(int), |
| 263 | "BN_ULONG gets promoted to int"); |
| 264 |
| 265 | crypto_word_t mask; |
| 266 | int bits = 0; |
| 267 |
| 268 | #if BN_BITS2 > 32 |
| 269 | // Check if the lower half of |x| are all zero. |
| 270 | mask = constant_time_is_zero_w(l << (BN_BITS2 - 32)); |
| 271 | // If the lower half is all zeros, it is included in the bit count and we |
| 272 | // count the upper half. Otherwise, we count the lower half. |
| 273 | bits += 32 & mask; |
| 274 | l = constant_time_select_w(mask, l >> 32, l); |
| 275 | #endif |
| 276 |
| 277 | // The remaining blocks are analogous iterations at lower powers of two. |
| 278 | mask = constant_time_is_zero_w(l << (BN_BITS2 - 16)); |
| 279 | bits += 16 & mask; |
| 280 | l = constant_time_select_w(mask, l >> 16, l); |
| 281 |
| 282 | mask = constant_time_is_zero_w(l << (BN_BITS2 - 8)); |
| 283 | bits += 8 & mask; |
| 284 | l = constant_time_select_w(mask, l >> 8, l); |
| 285 |
| 286 | mask = constant_time_is_zero_w(l << (BN_BITS2 - 4)); |
| 287 | bits += 4 & mask; |
| 288 | l = constant_time_select_w(mask, l >> 4, l); |
| 289 |
| 290 | mask = constant_time_is_zero_w(l << (BN_BITS2 - 2)); |
| 291 | bits += 2 & mask; |
| 292 | l = constant_time_select_w(mask, l >> 2, l); |
| 293 |
| 294 | mask = constant_time_is_zero_w(l << (BN_BITS2 - 1)); |
| 295 | bits += 1 & mask; |
| 296 |
| 297 | return bits; |
| 298 | } |
| 299 |
| 300 | int BN_count_low_zero_bits(const BIGNUM *bn) { |
| 301 | static_assert(sizeof(BN_ULONG) <= sizeof(crypto_word_t), |
| 302 | "crypto_word_t is too small"); |
| 303 | static_assert(sizeof(int) <= sizeof(crypto_word_t), |
| 304 | "crypto_word_t is too small"); |
| 305 |
| 306 | int ret = 0; |
| 307 | crypto_word_t saw_nonzero = 0; |
| 308 | for (int i = 0; i < bn->width; i++) { |
| 309 | crypto_word_t nonzero = ~constant_time_is_zero_w(bn->d[i]); |
| 310 | crypto_word_t first_nonzero = ~saw_nonzero & nonzero; |
| 311 | saw_nonzero |= nonzero; |
| 312 |
| 313 | int bits = bn_count_low_zero_bits_word(bn->d[i]); |
| 314 | ret |= first_nonzero & (i * BN_BITS2 + bits); |
| 315 | } |
| 316 |
| 317 | // If got to the end of |bn| and saw no non-zero words, |bn| is zero. |ret| |
| 318 | // will then remain zero. |
| 319 | return ret; |
| 320 | } |
| 321 |