blob: d8dfe5f4f3140651d94078010c67f48cafb4a051 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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 Langleyf4e42722015-06-04 17:45:09 -070061#include <openssl/err.h>
62
Adam Langleyd9e397b2015-01-22 14:27:53 -080063#include "internal.h"
64
65
66int 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 Langleyf4e42722015-06-04 17:45:09 -070071 if (n < 0) {
Kenny Rootb8494592015-09-25 02:29:14 +000072 OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
Adam Langleyf4e42722015-06-04 17:45:09 -070073 return 0;
74 }
75
Adam Langleyd9e397b2015-01-22 14:27:53 -080076 r->neg = a->neg;
77 nw = n / BN_BITS2;
Robert Sloanab8b8882018-03-26 11:39:51 -070078 if (!bn_wexpand(r, a->width + nw + 1)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -080079 return 0;
80 }
81 lb = n % BN_BITS2;
82 rb = BN_BITS2 - lb;
83 f = a->d;
84 t = r->d;
Robert Sloanab8b8882018-03-26 11:39:51 -070085 t[a->width + nw] = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -080086 if (lb == 0) {
Robert Sloanab8b8882018-03-26 11:39:51 -070087 for (i = a->width - 1; i >= 0; i--) {
Adam Langleyd9e397b2015-01-22 14:27:53 -080088 t[nw + i] = f[i];
89 }
90 } else {
Robert Sloanab8b8882018-03-26 11:39:51 -070091 for (i = a->width - 1; i >= 0; i--) {
Adam Langleyd9e397b2015-01-22 14:27:53 -080092 l = f[i];
Robert Sloan29c1d2c2017-10-30 14:10:28 -070093 t[nw + i + 1] |= l >> rb;
94 t[nw + i] = l << lb;
Adam Langleyd9e397b2015-01-22 14:27:53 -080095 }
96 }
Robert Sloan69939df2017-01-09 10:53:07 -080097 OPENSSL_memset(t, 0, nw * sizeof(t[0]));
Robert Sloanab8b8882018-03-26 11:39:51 -070098 r->width = a->width + nw + 1;
99 bn_set_minimal_width(r);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800100
101 return 1;
102}
103
104int 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 Sloanab8b8882018-03-26 11:39:51 -0700110 if (!bn_wexpand(r, a->width + 1)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800111 return 0;
112 }
Robert Sloanab8b8882018-03-26 11:39:51 -0700113 r->width = a->width;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800114 } else {
Robert Sloanab8b8882018-03-26 11:39:51 -0700115 if (!bn_wexpand(r, a->width + 1)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800116 return 0;
117 }
118 }
119 ap = a->d;
120 rp = r->d;
121 c = 0;
Robert Sloanab8b8882018-03-26 11:39:51 -0700122 for (i = 0; i < a->width; i++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800123 t = *(ap++);
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700124 *(rp++) = (t << 1) | c;
Robert Sloand5c22152017-11-13 09:22:12 -0800125 c = t >> (BN_BITS2 - 1);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800126 }
127 if (c) {
128 *rp = 1;
Robert Sloanab8b8882018-03-26 11:39:51 -0700129 r->width++;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800130 }
131
132 return 1;
133}
134
135int 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 Langleyf4e42722015-06-04 17:45:09 -0700140 if (n < 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000141 OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
Adam Langleyf4e42722015-06-04 17:45:09 -0700142 return 0;
143 }
144
Robert Sloanab8b8882018-03-26 11:39:51 -0700145 int a_width = bn_minimal_width(a);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800146 nw = n / BN_BITS2;
147 rb = n % BN_BITS2;
148 lb = BN_BITS2 - rb;
Robert Sloanab8b8882018-03-26 11:39:51 -0700149 if (nw >= a_width || a_width == 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800150 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 Sloan9254e682017-04-24 09:42:06 -0700156 if (!bn_wexpand(r, i)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800157 return 0;
158 }
159 } else {
160 if (n == 0) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700161 return 1; // or the copying loop will go berserk
Adam Langleyd9e397b2015-01-22 14:27:53 -0800162 }
163 }
164
165 f = &(a->d[nw]);
166 t = r->d;
Robert Sloanab8b8882018-03-26 11:39:51 -0700167 j = a_width - nw;
168 r->width = i;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800169
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 Sloan29c1d2c2017-10-30 14:10:28 -0700177 tmp = l >> rb;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800178 l = *(f++);
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700179 *(t++) = tmp | (l << lb);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800180 }
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700181 l >>= rb;
182 if (l) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800183 *(t) = l;
184 }
185 }
186
Robert Sloanab8b8882018-03-26 11:39:51 -0700187 if (r->width == 0) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500188 r->neg = 0;
189 }
190
Adam Langleyd9e397b2015-01-22 14:27:53 -0800191 return 1;
192}
193
194int 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 Sloanab8b8882018-03-26 11:39:51 -0700202 i = bn_minimal_width(a);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800203 ap = a->d;
204 j = i - (ap[i - 1] == 1);
205 if (a != r) {
Robert Sloan9254e682017-04-24 09:42:06 -0700206 if (!bn_wexpand(r, j)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800207 return 0;
208 }
209 r->neg = a->neg;
210 }
211 rp = r->d;
212 t = ap[--i];
Robert Sloand5c22152017-11-13 09:22:12 -0800213 c = t << (BN_BITS2 - 1);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800214 if (t >>= 1) {
215 rp[i] = t;
216 }
217 while (i > 0) {
218 t = ap[--i];
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700219 rp[i] = (t >> 1) | c;
Robert Sloand5c22152017-11-13 09:22:12 -0800220 c = t << (BN_BITS2 - 1);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800221 }
Robert Sloanab8b8882018-03-26 11:39:51 -0700222 r->width = j;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800223
Robert Sloanab8b8882018-03-26 11:39:51 -0700224 if (r->width == 0) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500225 r->neg = 0;
226 }
227
Adam Langleyd9e397b2015-01-22 14:27:53 -0800228 return 1;
229}
230
231int BN_set_bit(BIGNUM *a, int n) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800232 if (n < 0) {
233 return 0;
234 }
235
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700236 int i = n / BN_BITS2;
237 int j = n % BN_BITS2;
Robert Sloanab8b8882018-03-26 11:39:51 -0700238 if (a->width <= i) {
Robert Sloan9254e682017-04-24 09:42:06 -0700239 if (!bn_wexpand(a, i + 1)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800240 return 0;
241 }
Robert Sloanab8b8882018-03-26 11:39:51 -0700242 for (int k = a->width; k < i + 1; k++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800243 a->d[k] = 0;
244 }
Robert Sloanab8b8882018-03-26 11:39:51 -0700245 a->width = i + 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800246 }
247
248 a->d[i] |= (((BN_ULONG)1) << j);
249
250 return 1;
251}
252
253int 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 Sloanab8b8882018-03-26 11:39:51 -0700262 if (a->width <= i) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800263 return 0;
264 }
265
266 a->d[i] &= (~(((BN_ULONG)1) << j));
Robert Sloanab8b8882018-03-26 11:39:51 -0700267 bn_set_minimal_width(a);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800268 return 1;
269}
270
Robert Sloan99319a12017-11-27 10:32:46 -0800271int 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 Langleyd9e397b2015-01-22 14:27:53 -0800280int BN_is_bit_set(const BIGNUM *a, int n) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800281 if (n < 0) {
282 return 0;
283 }
Robert Sloanab8b8882018-03-26 11:39:51 -0700284 return bn_is_bit_set_words(a->d, a->width, n);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800285}
286
287int BN_mask_bits(BIGNUM *a, int n) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800288 if (n < 0) {
289 return 0;
290 }
291
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700292 int w = n / BN_BITS2;
293 int b = n % BN_BITS2;
Robert Sloanab8b8882018-03-26 11:39:51 -0700294 if (w >= a->width) {
295 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800296 }
297 if (b == 0) {
Robert Sloanab8b8882018-03-26 11:39:51 -0700298 a->width = w;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800299 } else {
Robert Sloanab8b8882018-03-26 11:39:51 -0700300 a->width = w + 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800301 a->d[w] &= ~(BN_MASK2 << b);
302 }
303
Robert Sloanab8b8882018-03-26 11:39:51 -0700304 bn_set_minimal_width(a);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800305 return 1;
306}
Robert Sloanab8b8882018-03-26 11:39:51 -0700307
308int 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}