Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 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 <string.h> |
| 60 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 61 | #include <openssl/err.h> |
| 62 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 63 | #include "internal.h" |
| 64 | |
| 65 | |
| 66 | int BN_lshift(BIGNUM *r, const BIGNUM *a, int n) { |
| 67 | int i, nw, lb, rb; |
| 68 | BN_ULONG *t, *f; |
| 69 | BN_ULONG l; |
| 70 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 71 | if (n < 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 72 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 76 | r->neg = a->neg; |
| 77 | nw = n / BN_BITS2; |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 78 | if (!bn_wexpand(r, a->width + nw + 1)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | lb = n % BN_BITS2; |
| 82 | rb = BN_BITS2 - lb; |
| 83 | f = a->d; |
| 84 | t = r->d; |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 85 | t[a->width + nw] = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 86 | if (lb == 0) { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 87 | for (i = a->width - 1; i >= 0; i--) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 88 | t[nw + i] = f[i]; |
| 89 | } |
| 90 | } else { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 91 | for (i = a->width - 1; i >= 0; i--) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 92 | l = f[i]; |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 93 | t[nw + i + 1] |= l >> rb; |
| 94 | t[nw + i] = l << lb; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 97 | OPENSSL_memset(t, 0, nw * sizeof(t[0])); |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 98 | r->width = a->width + nw + 1; |
| 99 | bn_set_minimal_width(r); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 100 | |
| 101 | return 1; |
| 102 | } |
| 103 | |
| 104 | int BN_lshift1(BIGNUM *r, const BIGNUM *a) { |
| 105 | BN_ULONG *ap, *rp, t, c; |
| 106 | int i; |
| 107 | |
| 108 | if (r != a) { |
| 109 | r->neg = a->neg; |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 110 | if (!bn_wexpand(r, a->width + 1)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 111 | return 0; |
| 112 | } |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 113 | r->width = a->width; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 114 | } else { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 115 | if (!bn_wexpand(r, a->width + 1)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | } |
| 119 | ap = a->d; |
| 120 | rp = r->d; |
| 121 | c = 0; |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 122 | for (i = 0; i < a->width; i++) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 123 | t = *(ap++); |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 124 | *(rp++) = (t << 1) | c; |
Robert Sloan | d5c2215 | 2017-11-13 09:22:12 -0800 | [diff] [blame] | 125 | c = t >> (BN_BITS2 - 1); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 126 | } |
| 127 | if (c) { |
| 128 | *rp = 1; |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 129 | r->width++; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | return 1; |
| 133 | } |
| 134 | |
| 135 | int BN_rshift(BIGNUM *r, const BIGNUM *a, int n) { |
| 136 | int i, j, nw, lb, rb; |
| 137 | BN_ULONG *t, *f; |
| 138 | BN_ULONG l, tmp; |
| 139 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 140 | if (n < 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 141 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 145 | int a_width = bn_minimal_width(a); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 146 | nw = n / BN_BITS2; |
| 147 | rb = n % BN_BITS2; |
| 148 | lb = BN_BITS2 - rb; |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 149 | if (nw >= a_width || a_width == 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 150 | BN_zero(r); |
| 151 | return 1; |
| 152 | } |
| 153 | i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2; |
| 154 | if (r != a) { |
| 155 | r->neg = a->neg; |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 156 | if (!bn_wexpand(r, i)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | } else { |
| 160 | if (n == 0) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 161 | return 1; // or the copying loop will go berserk |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | f = &(a->d[nw]); |
| 166 | t = r->d; |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 167 | j = a_width - nw; |
| 168 | r->width = i; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 169 | |
| 170 | if (rb == 0) { |
| 171 | for (i = j; i != 0; i--) { |
| 172 | *(t++) = *(f++); |
| 173 | } |
| 174 | } else { |
| 175 | l = *(f++); |
| 176 | for (i = j - 1; i != 0; i--) { |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 177 | tmp = l >> rb; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 178 | l = *(f++); |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 179 | *(t++) = tmp | (l << lb); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 180 | } |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 181 | l >>= rb; |
| 182 | if (l) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 183 | *(t) = l; |
| 184 | } |
| 185 | } |
| 186 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 187 | if (r->width == 0) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 188 | r->neg = 0; |
| 189 | } |
| 190 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 191 | return 1; |
| 192 | } |
| 193 | |
| 194 | int BN_rshift1(BIGNUM *r, const BIGNUM *a) { |
| 195 | BN_ULONG *ap, *rp, t, c; |
| 196 | int i, j; |
| 197 | |
| 198 | if (BN_is_zero(a)) { |
| 199 | BN_zero(r); |
| 200 | return 1; |
| 201 | } |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 202 | i = bn_minimal_width(a); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 203 | ap = a->d; |
| 204 | j = i - (ap[i - 1] == 1); |
| 205 | if (a != r) { |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 206 | if (!bn_wexpand(r, j)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | r->neg = a->neg; |
| 210 | } |
| 211 | rp = r->d; |
| 212 | t = ap[--i]; |
Robert Sloan | d5c2215 | 2017-11-13 09:22:12 -0800 | [diff] [blame] | 213 | c = t << (BN_BITS2 - 1); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 214 | if (t >>= 1) { |
| 215 | rp[i] = t; |
| 216 | } |
| 217 | while (i > 0) { |
| 218 | t = ap[--i]; |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 219 | rp[i] = (t >> 1) | c; |
Robert Sloan | d5c2215 | 2017-11-13 09:22:12 -0800 | [diff] [blame] | 220 | c = t << (BN_BITS2 - 1); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 221 | } |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 222 | r->width = j; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 223 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 224 | if (r->width == 0) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 225 | r->neg = 0; |
| 226 | } |
| 227 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 228 | return 1; |
| 229 | } |
| 230 | |
| 231 | int BN_set_bit(BIGNUM *a, int n) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 232 | if (n < 0) { |
| 233 | return 0; |
| 234 | } |
| 235 | |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 236 | int i = n / BN_BITS2; |
| 237 | int j = n % BN_BITS2; |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 238 | if (a->width <= i) { |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 239 | if (!bn_wexpand(a, i + 1)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 240 | return 0; |
| 241 | } |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 242 | for (int k = a->width; k < i + 1; k++) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 243 | a->d[k] = 0; |
| 244 | } |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 245 | a->width = i + 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | a->d[i] |= (((BN_ULONG)1) << j); |
| 249 | |
| 250 | return 1; |
| 251 | } |
| 252 | |
| 253 | int BN_clear_bit(BIGNUM *a, int n) { |
| 254 | int i, j; |
| 255 | |
| 256 | if (n < 0) { |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | i = n / BN_BITS2; |
| 261 | j = n % BN_BITS2; |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 262 | if (a->width <= i) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | a->d[i] &= (~(((BN_ULONG)1) << j)); |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 267 | bn_set_minimal_width(a); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 268 | return 1; |
| 269 | } |
| 270 | |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 271 | int bn_is_bit_set_words(const BN_ULONG *a, size_t num, unsigned bit) { |
| 272 | unsigned i = bit / BN_BITS2; |
| 273 | unsigned j = bit % BN_BITS2; |
| 274 | if (i >= num) { |
| 275 | return 0; |
| 276 | } |
| 277 | return (a[i] >> j) & 1; |
| 278 | } |
| 279 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 280 | int BN_is_bit_set(const BIGNUM *a, int n) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 281 | if (n < 0) { |
| 282 | return 0; |
| 283 | } |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 284 | return bn_is_bit_set_words(a->d, a->width, n); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | int BN_mask_bits(BIGNUM *a, int n) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 288 | if (n < 0) { |
| 289 | return 0; |
| 290 | } |
| 291 | |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 292 | int w = n / BN_BITS2; |
| 293 | int b = n % BN_BITS2; |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 294 | if (w >= a->width) { |
| 295 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 296 | } |
| 297 | if (b == 0) { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 298 | a->width = w; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 299 | } else { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 300 | a->width = w + 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 301 | a->d[w] &= ~(BN_MASK2 << b); |
| 302 | } |
| 303 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 304 | bn_set_minimal_width(a); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 305 | return 1; |
| 306 | } |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 307 | |
| 308 | int BN_count_low_zero_bits(const BIGNUM *bn) { |
| 309 | for (int i = 0; i < bn->width; i++) { |
| 310 | if (bn->d[i] != 0) { |
| 311 | int bits = 0; |
| 312 | for (BN_ULONG w = bn->d[i]; (w & 1) == 0; w >>= 1) { |
| 313 | bits++; |
| 314 | } |
| 315 | return i * BN_BITS2 + bits; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | // We got to the end of |bn| and saw no non-zero words. |bn| is zero, so |
| 320 | // return zero. |
| 321 | return 0; |
| 322 | } |