grpc 1.10.0.pre1 → 1.10.0.pre2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +3 -3
  3. data/src/core/ext/filters/max_age/max_age_filter.cc +3 -0
  4. data/src/core/lib/surface/version.cc +1 -1
  5. data/src/ruby/lib/grpc/version.rb +1 -1
  6. data/third_party/boringssl/crypto/fipsmodule/aes/aes.c +1100 -0
  7. data/third_party/boringssl/crypto/fipsmodule/aes/key_wrap.c +138 -0
  8. data/third_party/boringssl/crypto/fipsmodule/aes/mode_wrappers.c +112 -0
  9. data/third_party/boringssl/crypto/fipsmodule/bn/add.c +375 -0
  10. data/third_party/boringssl/crypto/fipsmodule/bn/asm/x86_64-gcc.c +537 -0
  11. data/third_party/boringssl/crypto/fipsmodule/bn/bn.c +370 -0
  12. data/third_party/boringssl/crypto/fipsmodule/bn/bytes.c +269 -0
  13. data/third_party/boringssl/crypto/fipsmodule/bn/cmp.c +239 -0
  14. data/third_party/boringssl/crypto/fipsmodule/bn/ctx.c +303 -0
  15. data/third_party/boringssl/crypto/fipsmodule/bn/div.c +726 -0
  16. data/third_party/boringssl/crypto/fipsmodule/bn/exponentiation.c +1233 -0
  17. data/third_party/boringssl/crypto/fipsmodule/bn/gcd.c +627 -0
  18. data/third_party/boringssl/crypto/fipsmodule/bn/generic.c +715 -0
  19. data/third_party/boringssl/crypto/fipsmodule/bn/jacobi.c +146 -0
  20. data/third_party/boringssl/crypto/fipsmodule/bn/montgomery.c +430 -0
  21. data/third_party/boringssl/crypto/fipsmodule/bn/montgomery_inv.c +207 -0
  22. data/third_party/boringssl/crypto/fipsmodule/bn/mul.c +869 -0
  23. data/third_party/boringssl/crypto/fipsmodule/bn/prime.c +894 -0
  24. data/third_party/boringssl/crypto/fipsmodule/bn/random.c +283 -0
  25. data/third_party/boringssl/crypto/fipsmodule/bn/rsaz_exp.c +254 -0
  26. data/third_party/boringssl/crypto/fipsmodule/bn/shift.c +307 -0
  27. data/third_party/boringssl/crypto/fipsmodule/bn/sqrt.c +502 -0
  28. data/third_party/boringssl/crypto/fipsmodule/cipher/aead.c +284 -0
  29. data/third_party/boringssl/crypto/fipsmodule/cipher/cipher.c +613 -0
  30. data/third_party/boringssl/crypto/fipsmodule/cipher/e_aes.c +1437 -0
  31. data/third_party/boringssl/crypto/fipsmodule/cipher/e_des.c +233 -0
  32. data/third_party/boringssl/crypto/fipsmodule/des/des.c +785 -0
  33. data/third_party/boringssl/crypto/fipsmodule/digest/digest.c +256 -0
  34. data/third_party/boringssl/crypto/fipsmodule/digest/digests.c +280 -0
  35. data/third_party/boringssl/crypto/fipsmodule/ec/ec.c +842 -0
  36. data/third_party/boringssl/crypto/fipsmodule/ec/ec_key.c +517 -0
  37. data/third_party/boringssl/crypto/fipsmodule/ec/ec_montgomery.c +304 -0
  38. data/third_party/boringssl/crypto/fipsmodule/ec/oct.c +404 -0
  39. data/third_party/boringssl/crypto/fipsmodule/ec/p224-64.c +1165 -0
  40. data/third_party/boringssl/crypto/fipsmodule/ec/p256-64.c +1708 -0
  41. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64.c +561 -0
  42. data/third_party/boringssl/crypto/fipsmodule/ec/simple.c +1113 -0
  43. data/third_party/boringssl/crypto/fipsmodule/ec/util-64.c +109 -0
  44. data/third_party/boringssl/crypto/fipsmodule/ec/wnaf.c +456 -0
  45. data/third_party/boringssl/crypto/fipsmodule/ecdsa/ecdsa.c +444 -0
  46. data/third_party/boringssl/crypto/fipsmodule/hmac/hmac.c +228 -0
  47. data/third_party/boringssl/crypto/fipsmodule/md4/md4.c +254 -0
  48. data/third_party/boringssl/crypto/fipsmodule/md5/md5.c +298 -0
  49. data/third_party/boringssl/crypto/fipsmodule/modes/cbc.c +212 -0
  50. data/third_party/boringssl/crypto/fipsmodule/modes/cfb.c +230 -0
  51. data/third_party/boringssl/crypto/fipsmodule/modes/ctr.c +219 -0
  52. data/third_party/boringssl/crypto/fipsmodule/modes/gcm.c +1074 -0
  53. data/third_party/boringssl/crypto/fipsmodule/modes/ofb.c +95 -0
  54. data/third_party/boringssl/crypto/fipsmodule/modes/polyval.c +91 -0
  55. data/third_party/boringssl/crypto/fipsmodule/rand/ctrdrbg.c +200 -0
  56. data/third_party/boringssl/crypto/fipsmodule/rand/rand.c +358 -0
  57. data/third_party/boringssl/crypto/fipsmodule/rand/urandom.c +302 -0
  58. data/third_party/boringssl/crypto/fipsmodule/rsa/blinding.c +263 -0
  59. data/third_party/boringssl/crypto/fipsmodule/rsa/padding.c +692 -0
  60. data/third_party/boringssl/crypto/fipsmodule/rsa/rsa.c +855 -0
  61. data/third_party/boringssl/crypto/fipsmodule/rsa/rsa_impl.c +1061 -0
  62. data/third_party/boringssl/crypto/fipsmodule/sha/sha1-altivec.c +361 -0
  63. data/third_party/boringssl/crypto/fipsmodule/sha/sha1.c +375 -0
  64. data/third_party/boringssl/crypto/fipsmodule/sha/sha256.c +337 -0
  65. data/third_party/boringssl/crypto/fipsmodule/sha/sha512.c +608 -0
  66. metadata +62 -2
@@ -0,0 +1,307 @@
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
+
61
+ #include <openssl/err.h>
62
+
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
+
71
+ if (n < 0) {
72
+ OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
73
+ return 0;
74
+ }
75
+
76
+ r->neg = a->neg;
77
+ nw = n / BN_BITS2;
78
+ if (!bn_wexpand(r, a->top + nw + 1)) {
79
+ return 0;
80
+ }
81
+ lb = n % BN_BITS2;
82
+ rb = BN_BITS2 - lb;
83
+ f = a->d;
84
+ t = r->d;
85
+ t[a->top + nw] = 0;
86
+ if (lb == 0) {
87
+ for (i = a->top - 1; i >= 0; i--) {
88
+ t[nw + i] = f[i];
89
+ }
90
+ } else {
91
+ for (i = a->top - 1; i >= 0; i--) {
92
+ l = f[i];
93
+ t[nw + i + 1] |= (l >> rb) & BN_MASK2;
94
+ t[nw + i] = (l << lb) & BN_MASK2;
95
+ }
96
+ }
97
+ OPENSSL_memset(t, 0, nw * sizeof(t[0]));
98
+ r->top = a->top + nw + 1;
99
+ bn_correct_top(r);
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;
110
+ if (!bn_wexpand(r, a->top + 1)) {
111
+ return 0;
112
+ }
113
+ r->top = a->top;
114
+ } else {
115
+ if (!bn_wexpand(r, a->top + 1)) {
116
+ return 0;
117
+ }
118
+ }
119
+ ap = a->d;
120
+ rp = r->d;
121
+ c = 0;
122
+ for (i = 0; i < a->top; i++) {
123
+ t = *(ap++);
124
+ *(rp++) = ((t << 1) | c) & BN_MASK2;
125
+ c = (t & BN_TBIT) ? 1 : 0;
126
+ }
127
+ if (c) {
128
+ *rp = 1;
129
+ r->top++;
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
+
140
+ if (n < 0) {
141
+ OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
142
+ return 0;
143
+ }
144
+
145
+ nw = n / BN_BITS2;
146
+ rb = n % BN_BITS2;
147
+ lb = BN_BITS2 - rb;
148
+ if (nw >= a->top || a->top == 0) {
149
+ BN_zero(r);
150
+ return 1;
151
+ }
152
+ i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2;
153
+ if (r != a) {
154
+ r->neg = a->neg;
155
+ if (!bn_wexpand(r, i)) {
156
+ return 0;
157
+ }
158
+ } else {
159
+ if (n == 0) {
160
+ return 1; // or the copying loop will go berserk
161
+ }
162
+ }
163
+
164
+ f = &(a->d[nw]);
165
+ t = r->d;
166
+ j = a->top - nw;
167
+ r->top = i;
168
+
169
+ if (rb == 0) {
170
+ for (i = j; i != 0; i--) {
171
+ *(t++) = *(f++);
172
+ }
173
+ } else {
174
+ l = *(f++);
175
+ for (i = j - 1; i != 0; i--) {
176
+ tmp = (l >> rb) & BN_MASK2;
177
+ l = *(f++);
178
+ *(t++) = (tmp | (l << lb)) & BN_MASK2;
179
+ }
180
+ if ((l = (l >> rb) & BN_MASK2)) {
181
+ *(t) = l;
182
+ }
183
+ }
184
+
185
+ if (r->top == 0) {
186
+ r->neg = 0;
187
+ }
188
+
189
+ return 1;
190
+ }
191
+
192
+ int BN_rshift1(BIGNUM *r, const BIGNUM *a) {
193
+ BN_ULONG *ap, *rp, t, c;
194
+ int i, j;
195
+
196
+ if (BN_is_zero(a)) {
197
+ BN_zero(r);
198
+ return 1;
199
+ }
200
+ i = a->top;
201
+ ap = a->d;
202
+ j = i - (ap[i - 1] == 1);
203
+ if (a != r) {
204
+ if (!bn_wexpand(r, j)) {
205
+ return 0;
206
+ }
207
+ r->neg = a->neg;
208
+ }
209
+ rp = r->d;
210
+ t = ap[--i];
211
+ c = (t & 1) ? BN_TBIT : 0;
212
+ if (t >>= 1) {
213
+ rp[i] = t;
214
+ }
215
+ while (i > 0) {
216
+ t = ap[--i];
217
+ rp[i] = ((t >> 1) & BN_MASK2) | c;
218
+ c = (t & 1) ? BN_TBIT : 0;
219
+ }
220
+ r->top = j;
221
+
222
+ if (r->top == 0) {
223
+ r->neg = 0;
224
+ }
225
+
226
+ return 1;
227
+ }
228
+
229
+ int BN_set_bit(BIGNUM *a, int n) {
230
+ int i, j, k;
231
+
232
+ if (n < 0) {
233
+ return 0;
234
+ }
235
+
236
+ i = n / BN_BITS2;
237
+ j = n % BN_BITS2;
238
+ if (a->top <= i) {
239
+ if (!bn_wexpand(a, i + 1)) {
240
+ return 0;
241
+ }
242
+ for (k = a->top; k < i + 1; k++) {
243
+ a->d[k] = 0;
244
+ }
245
+ a->top = i + 1;
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;
262
+ if (a->top <= i) {
263
+ return 0;
264
+ }
265
+
266
+ a->d[i] &= (~(((BN_ULONG)1) << j));
267
+ bn_correct_top(a);
268
+ return 1;
269
+ }
270
+
271
+ int BN_is_bit_set(const BIGNUM *a, int n) {
272
+ int i, j;
273
+
274
+ if (n < 0) {
275
+ return 0;
276
+ }
277
+ i = n / BN_BITS2;
278
+ j = n % BN_BITS2;
279
+ if (a->top <= i) {
280
+ return 0;
281
+ }
282
+
283
+ return (a->d[i]>>j)&1;
284
+ }
285
+
286
+ int BN_mask_bits(BIGNUM *a, int n) {
287
+ int b, w;
288
+
289
+ if (n < 0) {
290
+ return 0;
291
+ }
292
+
293
+ w = n / BN_BITS2;
294
+ b = n % BN_BITS2;
295
+ if (w >= a->top) {
296
+ return 0;
297
+ }
298
+ if (b == 0) {
299
+ a->top = w;
300
+ } else {
301
+ a->top = w + 1;
302
+ a->d[w] &= ~(BN_MASK2 << b);
303
+ }
304
+
305
+ bn_correct_top(a);
306
+ return 1;
307
+ }
@@ -0,0 +1,502 @@
1
+ /* Written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
2
+ * and Bodo Moeller for the OpenSSL project. */
3
+ /* ====================================================================
4
+ * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions
8
+ * are met:
9
+ *
10
+ * 1. Redistributions of source code must retain the above copyright
11
+ * notice, this list of conditions and the following disclaimer.
12
+ *
13
+ * 2. Redistributions in binary form must reproduce the above copyright
14
+ * notice, this list of conditions and the following disclaimer in
15
+ * the documentation and/or other materials provided with the
16
+ * distribution.
17
+ *
18
+ * 3. All advertising materials mentioning features or use of this
19
+ * software must display the following acknowledgment:
20
+ * "This product includes software developed by the OpenSSL Project
21
+ * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22
+ *
23
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
24
+ * endorse or promote products derived from this software without
25
+ * prior written permission. For written permission, please contact
26
+ * openssl-core@openssl.org.
27
+ *
28
+ * 5. Products derived from this software may not be called "OpenSSL"
29
+ * nor may "OpenSSL" appear in their names without prior written
30
+ * permission of the OpenSSL Project.
31
+ *
32
+ * 6. Redistributions of any form whatsoever must retain the following
33
+ * acknowledgment:
34
+ * "This product includes software developed by the OpenSSL Project
35
+ * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36
+ *
37
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
38
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
40
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
41
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
43
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
46
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
48
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
49
+ * ====================================================================
50
+ *
51
+ * This product includes cryptographic software written by Eric Young
52
+ * (eay@cryptsoft.com). This product includes software written by Tim
53
+ * Hudson (tjh@cryptsoft.com). */
54
+
55
+ #include <openssl/bn.h>
56
+
57
+ #include <openssl/err.h>
58
+
59
+ #include "internal.h"
60
+
61
+
62
+ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) {
63
+ // Compute a square root of |a| mod |p| using the Tonelli/Shanks algorithm
64
+ // (cf. Henri Cohen, "A Course in Algebraic Computational Number Theory",
65
+ // algorithm 1.5.1). |p| is assumed to be a prime.
66
+
67
+ BIGNUM *ret = in;
68
+ int err = 1;
69
+ int r;
70
+ BIGNUM *A, *b, *q, *t, *x, *y;
71
+ int e, i, j;
72
+
73
+ if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) {
74
+ if (BN_abs_is_word(p, 2)) {
75
+ if (ret == NULL) {
76
+ ret = BN_new();
77
+ }
78
+ if (ret == NULL) {
79
+ goto end;
80
+ }
81
+ if (!BN_set_word(ret, BN_is_bit_set(a, 0))) {
82
+ if (ret != in) {
83
+ BN_free(ret);
84
+ }
85
+ return NULL;
86
+ }
87
+ return ret;
88
+ }
89
+
90
+ OPENSSL_PUT_ERROR(BN, BN_R_P_IS_NOT_PRIME);
91
+ return (NULL);
92
+ }
93
+
94
+ if (BN_is_zero(a) || BN_is_one(a)) {
95
+ if (ret == NULL) {
96
+ ret = BN_new();
97
+ }
98
+ if (ret == NULL) {
99
+ goto end;
100
+ }
101
+ if (!BN_set_word(ret, BN_is_one(a))) {
102
+ if (ret != in) {
103
+ BN_free(ret);
104
+ }
105
+ return NULL;
106
+ }
107
+ return ret;
108
+ }
109
+
110
+ BN_CTX_start(ctx);
111
+ A = BN_CTX_get(ctx);
112
+ b = BN_CTX_get(ctx);
113
+ q = BN_CTX_get(ctx);
114
+ t = BN_CTX_get(ctx);
115
+ x = BN_CTX_get(ctx);
116
+ y = BN_CTX_get(ctx);
117
+ if (y == NULL) {
118
+ goto end;
119
+ }
120
+
121
+ if (ret == NULL) {
122
+ ret = BN_new();
123
+ }
124
+ if (ret == NULL) {
125
+ goto end;
126
+ }
127
+
128
+ // A = a mod p
129
+ if (!BN_nnmod(A, a, p, ctx)) {
130
+ goto end;
131
+ }
132
+
133
+ // now write |p| - 1 as 2^e*q where q is odd
134
+ e = 1;
135
+ while (!BN_is_bit_set(p, e)) {
136
+ e++;
137
+ }
138
+ // we'll set q later (if needed)
139
+
140
+ if (e == 1) {
141
+ // The easy case: (|p|-1)/2 is odd, so 2 has an inverse
142
+ // modulo (|p|-1)/2, and square roots can be computed
143
+ // directly by modular exponentiation.
144
+ // We have
145
+ // 2 * (|p|+1)/4 == 1 (mod (|p|-1)/2),
146
+ // so we can use exponent (|p|+1)/4, i.e. (|p|-3)/4 + 1.
147
+ if (!BN_rshift(q, p, 2)) {
148
+ goto end;
149
+ }
150
+ q->neg = 0;
151
+ if (!BN_add_word(q, 1) ||
152
+ !BN_mod_exp_mont(ret, A, q, p, ctx, NULL)) {
153
+ goto end;
154
+ }
155
+ err = 0;
156
+ goto vrfy;
157
+ }
158
+
159
+ if (e == 2) {
160
+ // |p| == 5 (mod 8)
161
+ //
162
+ // In this case 2 is always a non-square since
163
+ // Legendre(2,p) = (-1)^((p^2-1)/8) for any odd prime.
164
+ // So if a really is a square, then 2*a is a non-square.
165
+ // Thus for
166
+ // b := (2*a)^((|p|-5)/8),
167
+ // i := (2*a)*b^2
168
+ // we have
169
+ // i^2 = (2*a)^((1 + (|p|-5)/4)*2)
170
+ // = (2*a)^((p-1)/2)
171
+ // = -1;
172
+ // so if we set
173
+ // x := a*b*(i-1),
174
+ // then
175
+ // x^2 = a^2 * b^2 * (i^2 - 2*i + 1)
176
+ // = a^2 * b^2 * (-2*i)
177
+ // = a*(-i)*(2*a*b^2)
178
+ // = a*(-i)*i
179
+ // = a.
180
+ //
181
+ // (This is due to A.O.L. Atkin,
182
+ // <URL:
183
+ //http://listserv.nodak.edu/scripts/wa.exe?A2=ind9211&L=nmbrthry&O=T&P=562>,
184
+ // November 1992.)
185
+
186
+ // t := 2*a
187
+ if (!BN_mod_lshift1_quick(t, A, p)) {
188
+ goto end;
189
+ }
190
+
191
+ // b := (2*a)^((|p|-5)/8)
192
+ if (!BN_rshift(q, p, 3)) {
193
+ goto end;
194
+ }
195
+ q->neg = 0;
196
+ if (!BN_mod_exp_mont(b, t, q, p, ctx, NULL)) {
197
+ goto end;
198
+ }
199
+
200
+ // y := b^2
201
+ if (!BN_mod_sqr(y, b, p, ctx)) {
202
+ goto end;
203
+ }
204
+
205
+ // t := (2*a)*b^2 - 1
206
+ if (!BN_mod_mul(t, t, y, p, ctx) ||
207
+ !BN_sub_word(t, 1)) {
208
+ goto end;
209
+ }
210
+
211
+ // x = a*b*t
212
+ if (!BN_mod_mul(x, A, b, p, ctx) ||
213
+ !BN_mod_mul(x, x, t, p, ctx)) {
214
+ goto end;
215
+ }
216
+
217
+ if (!BN_copy(ret, x)) {
218
+ goto end;
219
+ }
220
+ err = 0;
221
+ goto vrfy;
222
+ }
223
+
224
+ // e > 2, so we really have to use the Tonelli/Shanks algorithm.
225
+ // First, find some y that is not a square.
226
+ if (!BN_copy(q, p)) {
227
+ goto end; // use 'q' as temp
228
+ }
229
+ q->neg = 0;
230
+ i = 2;
231
+ do {
232
+ // For efficiency, try small numbers first;
233
+ // if this fails, try random numbers.
234
+ if (i < 22) {
235
+ if (!BN_set_word(y, i)) {
236
+ goto end;
237
+ }
238
+ } else {
239
+ if (!BN_pseudo_rand(y, BN_num_bits(p), 0, 0)) {
240
+ goto end;
241
+ }
242
+ if (BN_ucmp(y, p) >= 0) {
243
+ if (!(p->neg ? BN_add : BN_sub)(y, y, p)) {
244
+ goto end;
245
+ }
246
+ }
247
+ // now 0 <= y < |p|
248
+ if (BN_is_zero(y)) {
249
+ if (!BN_set_word(y, i)) {
250
+ goto end;
251
+ }
252
+ }
253
+ }
254
+
255
+ r = bn_jacobi(y, q, ctx); // here 'q' is |p|
256
+ if (r < -1) {
257
+ goto end;
258
+ }
259
+ if (r == 0) {
260
+ // m divides p
261
+ OPENSSL_PUT_ERROR(BN, BN_R_P_IS_NOT_PRIME);
262
+ goto end;
263
+ }
264
+ } while (r == 1 && ++i < 82);
265
+
266
+ if (r != -1) {
267
+ // Many rounds and still no non-square -- this is more likely
268
+ // a bug than just bad luck.
269
+ // Even if p is not prime, we should have found some y
270
+ // such that r == -1.
271
+ OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_ITERATIONS);
272
+ goto end;
273
+ }
274
+
275
+ // Here's our actual 'q':
276
+ if (!BN_rshift(q, q, e)) {
277
+ goto end;
278
+ }
279
+
280
+ // Now that we have some non-square, we can find an element
281
+ // of order 2^e by computing its q'th power.
282
+ if (!BN_mod_exp_mont(y, y, q, p, ctx, NULL)) {
283
+ goto end;
284
+ }
285
+ if (BN_is_one(y)) {
286
+ OPENSSL_PUT_ERROR(BN, BN_R_P_IS_NOT_PRIME);
287
+ goto end;
288
+ }
289
+
290
+ // Now we know that (if p is indeed prime) there is an integer
291
+ // k, 0 <= k < 2^e, such that
292
+ //
293
+ // a^q * y^k == 1 (mod p).
294
+ //
295
+ // As a^q is a square and y is not, k must be even.
296
+ // q+1 is even, too, so there is an element
297
+ //
298
+ // X := a^((q+1)/2) * y^(k/2),
299
+ //
300
+ // and it satisfies
301
+ //
302
+ // X^2 = a^q * a * y^k
303
+ // = a,
304
+ //
305
+ // so it is the square root that we are looking for.
306
+
307
+ // t := (q-1)/2 (note that q is odd)
308
+ if (!BN_rshift1(t, q)) {
309
+ goto end;
310
+ }
311
+
312
+ // x := a^((q-1)/2)
313
+ if (BN_is_zero(t)) // special case: p = 2^e + 1
314
+ {
315
+ if (!BN_nnmod(t, A, p, ctx)) {
316
+ goto end;
317
+ }
318
+ if (BN_is_zero(t)) {
319
+ // special case: a == 0 (mod p)
320
+ BN_zero(ret);
321
+ err = 0;
322
+ goto end;
323
+ } else if (!BN_one(x)) {
324
+ goto end;
325
+ }
326
+ } else {
327
+ if (!BN_mod_exp_mont(x, A, t, p, ctx, NULL)) {
328
+ goto end;
329
+ }
330
+ if (BN_is_zero(x)) {
331
+ // special case: a == 0 (mod p)
332
+ BN_zero(ret);
333
+ err = 0;
334
+ goto end;
335
+ }
336
+ }
337
+
338
+ // b := a*x^2 (= a^q)
339
+ if (!BN_mod_sqr(b, x, p, ctx) ||
340
+ !BN_mod_mul(b, b, A, p, ctx)) {
341
+ goto end;
342
+ }
343
+
344
+ // x := a*x (= a^((q+1)/2))
345
+ if (!BN_mod_mul(x, x, A, p, ctx)) {
346
+ goto end;
347
+ }
348
+
349
+ while (1) {
350
+ // Now b is a^q * y^k for some even k (0 <= k < 2^E
351
+ // where E refers to the original value of e, which we
352
+ // don't keep in a variable), and x is a^((q+1)/2) * y^(k/2).
353
+ //
354
+ // We have a*b = x^2,
355
+ // y^2^(e-1) = -1,
356
+ // b^2^(e-1) = 1.
357
+
358
+ if (BN_is_one(b)) {
359
+ if (!BN_copy(ret, x)) {
360
+ goto end;
361
+ }
362
+ err = 0;
363
+ goto vrfy;
364
+ }
365
+
366
+
367
+ // find smallest i such that b^(2^i) = 1
368
+ i = 1;
369
+ if (!BN_mod_sqr(t, b, p, ctx)) {
370
+ goto end;
371
+ }
372
+ while (!BN_is_one(t)) {
373
+ i++;
374
+ if (i == e) {
375
+ OPENSSL_PUT_ERROR(BN, BN_R_NOT_A_SQUARE);
376
+ goto end;
377
+ }
378
+ if (!BN_mod_mul(t, t, t, p, ctx)) {
379
+ goto end;
380
+ }
381
+ }
382
+
383
+
384
+ // t := y^2^(e - i - 1)
385
+ if (!BN_copy(t, y)) {
386
+ goto end;
387
+ }
388
+ for (j = e - i - 1; j > 0; j--) {
389
+ if (!BN_mod_sqr(t, t, p, ctx)) {
390
+ goto end;
391
+ }
392
+ }
393
+ if (!BN_mod_mul(y, t, t, p, ctx) ||
394
+ !BN_mod_mul(x, x, t, p, ctx) ||
395
+ !BN_mod_mul(b, b, y, p, ctx)) {
396
+ goto end;
397
+ }
398
+ e = i;
399
+ }
400
+
401
+ vrfy:
402
+ if (!err) {
403
+ // verify the result -- the input might have been not a square
404
+ // (test added in 0.9.8)
405
+
406
+ if (!BN_mod_sqr(x, ret, p, ctx)) {
407
+ err = 1;
408
+ }
409
+
410
+ if (!err && 0 != BN_cmp(x, A)) {
411
+ OPENSSL_PUT_ERROR(BN, BN_R_NOT_A_SQUARE);
412
+ err = 1;
413
+ }
414
+ }
415
+
416
+ end:
417
+ if (err) {
418
+ if (ret != in) {
419
+ BN_clear_free(ret);
420
+ }
421
+ ret = NULL;
422
+ }
423
+ BN_CTX_end(ctx);
424
+ return ret;
425
+ }
426
+
427
+ int BN_sqrt(BIGNUM *out_sqrt, const BIGNUM *in, BN_CTX *ctx) {
428
+ BIGNUM *estimate, *tmp, *delta, *last_delta, *tmp2;
429
+ int ok = 0, last_delta_valid = 0;
430
+
431
+ if (in->neg) {
432
+ OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
433
+ return 0;
434
+ }
435
+ if (BN_is_zero(in)) {
436
+ BN_zero(out_sqrt);
437
+ return 1;
438
+ }
439
+
440
+ BN_CTX_start(ctx);
441
+ if (out_sqrt == in) {
442
+ estimate = BN_CTX_get(ctx);
443
+ } else {
444
+ estimate = out_sqrt;
445
+ }
446
+ tmp = BN_CTX_get(ctx);
447
+ last_delta = BN_CTX_get(ctx);
448
+ delta = BN_CTX_get(ctx);
449
+ if (estimate == NULL || tmp == NULL || last_delta == NULL || delta == NULL) {
450
+ OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
451
+ goto err;
452
+ }
453
+
454
+ // We estimate that the square root of an n-bit number is 2^{n/2}.
455
+ if (!BN_lshift(estimate, BN_value_one(), BN_num_bits(in)/2)) {
456
+ goto err;
457
+ }
458
+
459
+ // This is Newton's method for finding a root of the equation |estimate|^2 -
460
+ // |in| = 0.
461
+ for (;;) {
462
+ // |estimate| = 1/2 * (|estimate| + |in|/|estimate|)
463
+ if (!BN_div(tmp, NULL, in, estimate, ctx) ||
464
+ !BN_add(tmp, tmp, estimate) ||
465
+ !BN_rshift1(estimate, tmp) ||
466
+ // |tmp| = |estimate|^2
467
+ !BN_sqr(tmp, estimate, ctx) ||
468
+ // |delta| = |in| - |tmp|
469
+ !BN_sub(delta, in, tmp)) {
470
+ OPENSSL_PUT_ERROR(BN, ERR_R_BN_LIB);
471
+ goto err;
472
+ }
473
+
474
+ delta->neg = 0;
475
+ // The difference between |in| and |estimate| squared is required to always
476
+ // decrease. This ensures that the loop always terminates, but I don't have
477
+ // a proof that it always finds the square root for a given square.
478
+ if (last_delta_valid && BN_cmp(delta, last_delta) >= 0) {
479
+ break;
480
+ }
481
+
482
+ last_delta_valid = 1;
483
+
484
+ tmp2 = last_delta;
485
+ last_delta = delta;
486
+ delta = tmp2;
487
+ }
488
+
489
+ if (BN_cmp(tmp, in) != 0) {
490
+ OPENSSL_PUT_ERROR(BN, BN_R_NOT_A_SQUARE);
491
+ goto err;
492
+ }
493
+
494
+ ok = 1;
495
+
496
+ err:
497
+ if (ok && out_sqrt == in && !BN_copy(out_sqrt, estimate)) {
498
+ ok = 0;
499
+ }
500
+ BN_CTX_end(ctx);
501
+ return ok;
502
+ }