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,304 @@
1
+ /* Originally written by Bodo Moeller and Nils Larsch for the OpenSSL project.
2
+ * ====================================================================
3
+ * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions
7
+ * are met:
8
+ *
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ *
12
+ * 2. Redistributions in binary form must reproduce the above copyright
13
+ * notice, this list of conditions and the following disclaimer in
14
+ * the documentation and/or other materials provided with the
15
+ * distribution.
16
+ *
17
+ * 3. All advertising materials mentioning features or use of this
18
+ * software must display the following acknowledgment:
19
+ * "This product includes software developed by the OpenSSL Project
20
+ * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21
+ *
22
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23
+ * endorse or promote products derived from this software without
24
+ * prior written permission. For written permission, please contact
25
+ * openssl-core@openssl.org.
26
+ *
27
+ * 5. Products derived from this software may not be called "OpenSSL"
28
+ * nor may "OpenSSL" appear in their names without prior written
29
+ * permission of the OpenSSL Project.
30
+ *
31
+ * 6. Redistributions of any form whatsoever must retain the following
32
+ * acknowledgment:
33
+ * "This product includes software developed by the OpenSSL Project
34
+ * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35
+ *
36
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
48
+ * ====================================================================
49
+ *
50
+ * This product includes cryptographic software written by Eric Young
51
+ * (eay@cryptsoft.com). This product includes software written by Tim
52
+ * Hudson (tjh@cryptsoft.com).
53
+ *
54
+ */
55
+ /* ====================================================================
56
+ * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
57
+ *
58
+ * Portions of the attached software ("Contribution") are developed by
59
+ * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
60
+ *
61
+ * The Contribution is licensed pursuant to the OpenSSL open source
62
+ * license provided above.
63
+ *
64
+ * The elliptic curve binary polynomial software is originally written by
65
+ * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
66
+ * Laboratories. */
67
+
68
+ #include <openssl/ec.h>
69
+
70
+ #include <openssl/bn.h>
71
+ #include <openssl/err.h>
72
+ #include <openssl/mem.h>
73
+
74
+ #include "../bn/internal.h"
75
+ #include "../delocate.h"
76
+ #include "internal.h"
77
+
78
+
79
+ int ec_GFp_mont_group_init(EC_GROUP *group) {
80
+ int ok;
81
+
82
+ ok = ec_GFp_simple_group_init(group);
83
+ group->mont = NULL;
84
+ return ok;
85
+ }
86
+
87
+ void ec_GFp_mont_group_finish(EC_GROUP *group) {
88
+ BN_MONT_CTX_free(group->mont);
89
+ group->mont = NULL;
90
+ ec_GFp_simple_group_finish(group);
91
+ }
92
+
93
+ int ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src) {
94
+ BN_MONT_CTX_free(dest->mont);
95
+ dest->mont = NULL;
96
+
97
+ if (!ec_GFp_simple_group_copy(dest, src)) {
98
+ return 0;
99
+ }
100
+
101
+ if (src->mont != NULL) {
102
+ dest->mont = BN_MONT_CTX_new();
103
+ if (dest->mont == NULL) {
104
+ return 0;
105
+ }
106
+ if (!BN_MONT_CTX_copy(dest->mont, src->mont)) {
107
+ goto err;
108
+ }
109
+ }
110
+
111
+ return 1;
112
+
113
+ err:
114
+ BN_MONT_CTX_free(dest->mont);
115
+ dest->mont = NULL;
116
+ return 0;
117
+ }
118
+
119
+ int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
120
+ const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
121
+ BN_CTX *new_ctx = NULL;
122
+ BN_MONT_CTX *mont = NULL;
123
+ int ret = 0;
124
+
125
+ BN_MONT_CTX_free(group->mont);
126
+ group->mont = NULL;
127
+
128
+ if (ctx == NULL) {
129
+ ctx = new_ctx = BN_CTX_new();
130
+ if (ctx == NULL) {
131
+ return 0;
132
+ }
133
+ }
134
+
135
+ mont = BN_MONT_CTX_new();
136
+ if (mont == NULL) {
137
+ goto err;
138
+ }
139
+ if (!BN_MONT_CTX_set(mont, p, ctx)) {
140
+ OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
141
+ goto err;
142
+ }
143
+
144
+ group->mont = mont;
145
+ mont = NULL;
146
+
147
+ ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
148
+
149
+ if (!ret) {
150
+ BN_MONT_CTX_free(group->mont);
151
+ group->mont = NULL;
152
+ }
153
+
154
+ err:
155
+ BN_CTX_free(new_ctx);
156
+ BN_MONT_CTX_free(mont);
157
+ return ret;
158
+ }
159
+
160
+ int ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
161
+ const BIGNUM *b, BN_CTX *ctx) {
162
+ if (group->mont == NULL) {
163
+ OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
164
+ return 0;
165
+ }
166
+
167
+ return BN_mod_mul_montgomery(r, a, b, group->mont, ctx);
168
+ }
169
+
170
+ int ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
171
+ BN_CTX *ctx) {
172
+ if (group->mont == NULL) {
173
+ OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
174
+ return 0;
175
+ }
176
+
177
+ return BN_mod_mul_montgomery(r, a, a, group->mont, ctx);
178
+ }
179
+
180
+ int ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
181
+ BN_CTX *ctx) {
182
+ if (group->mont == NULL) {
183
+ OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
184
+ return 0;
185
+ }
186
+
187
+ return BN_to_montgomery(r, a, group->mont, ctx);
188
+ }
189
+
190
+ int ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
191
+ BN_CTX *ctx) {
192
+ if (group->mont == NULL) {
193
+ OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
194
+ return 0;
195
+ }
196
+
197
+ return BN_from_montgomery(r, a, group->mont, ctx);
198
+ }
199
+
200
+ static int ec_GFp_mont_point_get_affine_coordinates(const EC_GROUP *group,
201
+ const EC_POINT *point,
202
+ BIGNUM *x, BIGNUM *y,
203
+ BN_CTX *ctx) {
204
+ if (EC_POINT_is_at_infinity(group, point)) {
205
+ OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
206
+ return 0;
207
+ }
208
+
209
+ BN_CTX *new_ctx = NULL;
210
+ if (ctx == NULL) {
211
+ ctx = new_ctx = BN_CTX_new();
212
+ if (ctx == NULL) {
213
+ return 0;
214
+ }
215
+ }
216
+
217
+ int ret = 0;
218
+
219
+ BN_CTX_start(ctx);
220
+
221
+ if (BN_cmp(&point->Z, &group->one) == 0) {
222
+ // |point| is already affine.
223
+ if (x != NULL && !BN_from_montgomery(x, &point->X, group->mont, ctx)) {
224
+ goto err;
225
+ }
226
+ if (y != NULL && !BN_from_montgomery(y, &point->Y, group->mont, ctx)) {
227
+ goto err;
228
+ }
229
+ } else {
230
+ // transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3)
231
+
232
+ BIGNUM *Z_1 = BN_CTX_get(ctx);
233
+ BIGNUM *Z_2 = BN_CTX_get(ctx);
234
+ BIGNUM *Z_3 = BN_CTX_get(ctx);
235
+ if (Z_1 == NULL ||
236
+ Z_2 == NULL ||
237
+ Z_3 == NULL) {
238
+ goto err;
239
+ }
240
+
241
+ // The straightforward way to calculate the inverse of a Montgomery-encoded
242
+ // value where the result is Montgomery-encoded is:
243
+ //
244
+ // |BN_from_montgomery| + invert + |BN_to_montgomery|.
245
+ //
246
+ // This is equivalent, but more efficient, because |BN_from_montgomery|
247
+ // is more efficient (at least in theory) than |BN_to_montgomery|, since it
248
+ // doesn't have to do the multiplication before the reduction.
249
+ //
250
+ // Use Fermat's Little Theorem instead of |BN_mod_inverse_odd| since this
251
+ // inversion may be done as the final step of private key operations.
252
+ // Unfortunately, this is suboptimal for ECDSA verification.
253
+ if (!BN_from_montgomery(Z_1, &point->Z, group->mont, ctx) ||
254
+ !BN_from_montgomery(Z_1, Z_1, group->mont, ctx) ||
255
+ !bn_mod_inverse_prime(Z_1, Z_1, &group->field, ctx, group->mont)) {
256
+ goto err;
257
+ }
258
+
259
+ if (!BN_mod_mul_montgomery(Z_2, Z_1, Z_1, group->mont, ctx)) {
260
+ goto err;
261
+ }
262
+
263
+ // Instead of using |BN_from_montgomery| to convert the |x| coordinate
264
+ // and then calling |BN_from_montgomery| again to convert the |y|
265
+ // coordinate below, convert the common factor |Z_2| once now, saving one
266
+ // reduction.
267
+ if (!BN_from_montgomery(Z_2, Z_2, group->mont, ctx)) {
268
+ goto err;
269
+ }
270
+
271
+ if (x != NULL) {
272
+ if (!BN_mod_mul_montgomery(x, &point->X, Z_2, group->mont, ctx)) {
273
+ goto err;
274
+ }
275
+ }
276
+
277
+ if (y != NULL) {
278
+ if (!BN_mod_mul_montgomery(Z_3, Z_2, Z_1, group->mont, ctx) ||
279
+ !BN_mod_mul_montgomery(y, &point->Y, Z_3, group->mont, ctx)) {
280
+ goto err;
281
+ }
282
+ }
283
+ }
284
+
285
+ ret = 1;
286
+
287
+ err:
288
+ BN_CTX_end(ctx);
289
+ BN_CTX_free(new_ctx);
290
+ return ret;
291
+ }
292
+
293
+ DEFINE_METHOD_FUNCTION(EC_METHOD, EC_GFp_mont_method) {
294
+ out->group_init = ec_GFp_mont_group_init;
295
+ out->group_finish = ec_GFp_mont_group_finish;
296
+ out->group_copy = ec_GFp_mont_group_copy;
297
+ out->group_set_curve = ec_GFp_mont_group_set_curve;
298
+ out->point_get_affine_coordinates = ec_GFp_mont_point_get_affine_coordinates;
299
+ out->mul = ec_wNAF_mul /* XXX: Not constant time. */;
300
+ out->field_mul = ec_GFp_mont_field_mul;
301
+ out->field_sqr = ec_GFp_mont_field_sqr;
302
+ out->field_encode = ec_GFp_mont_field_encode;
303
+ out->field_decode = ec_GFp_mont_field_decode;
304
+ }
@@ -0,0 +1,404 @@
1
+ /* Originally written by Bodo Moeller for the OpenSSL project.
2
+ * ====================================================================
3
+ * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions
7
+ * are met:
8
+ *
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ *
12
+ * 2. Redistributions in binary form must reproduce the above copyright
13
+ * notice, this list of conditions and the following disclaimer in
14
+ * the documentation and/or other materials provided with the
15
+ * distribution.
16
+ *
17
+ * 3. All advertising materials mentioning features or use of this
18
+ * software must display the following acknowledgment:
19
+ * "This product includes software developed by the OpenSSL Project
20
+ * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21
+ *
22
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23
+ * endorse or promote products derived from this software without
24
+ * prior written permission. For written permission, please contact
25
+ * openssl-core@openssl.org.
26
+ *
27
+ * 5. Products derived from this software may not be called "OpenSSL"
28
+ * nor may "OpenSSL" appear in their names without prior written
29
+ * permission of the OpenSSL Project.
30
+ *
31
+ * 6. Redistributions of any form whatsoever must retain the following
32
+ * acknowledgment:
33
+ * "This product includes software developed by the OpenSSL Project
34
+ * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35
+ *
36
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
48
+ * ====================================================================
49
+ *
50
+ * This product includes cryptographic software written by Eric Young
51
+ * (eay@cryptsoft.com). This product includes software written by Tim
52
+ * Hudson (tjh@cryptsoft.com).
53
+ *
54
+ */
55
+ /* ====================================================================
56
+ * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
57
+ *
58
+ * Portions of the attached software ("Contribution") are developed by
59
+ * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
60
+ *
61
+ * The Contribution is licensed pursuant to the OpenSSL open source
62
+ * license provided above.
63
+ *
64
+ * The elliptic curve binary polynomial software is originally written by
65
+ * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
66
+ * Laboratories. */
67
+
68
+ #include <openssl/ec.h>
69
+
70
+ #include <openssl/bn.h>
71
+ #include <openssl/err.h>
72
+
73
+ #include "internal.h"
74
+
75
+
76
+ static size_t ec_GFp_simple_point2oct(const EC_GROUP *group,
77
+ const EC_POINT *point,
78
+ point_conversion_form_t form,
79
+ uint8_t *buf, size_t len, BN_CTX *ctx) {
80
+ size_t ret;
81
+ BN_CTX *new_ctx = NULL;
82
+ int used_ctx = 0;
83
+ BIGNUM *x, *y;
84
+ size_t field_len, i;
85
+
86
+ if ((form != POINT_CONVERSION_COMPRESSED) &&
87
+ (form != POINT_CONVERSION_UNCOMPRESSED)) {
88
+ OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FORM);
89
+ goto err;
90
+ }
91
+
92
+ if (EC_POINT_is_at_infinity(group, point)) {
93
+ OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
94
+ goto err;
95
+ }
96
+
97
+ // ret := required output buffer length
98
+ field_len = BN_num_bytes(&group->field);
99
+ ret =
100
+ (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
101
+
102
+ // if 'buf' is NULL, just return required length
103
+ if (buf != NULL) {
104
+ if (len < ret) {
105
+ OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL);
106
+ goto err;
107
+ }
108
+
109
+ if (ctx == NULL) {
110
+ ctx = new_ctx = BN_CTX_new();
111
+ if (ctx == NULL) {
112
+ goto err;
113
+ }
114
+ }
115
+
116
+ BN_CTX_start(ctx);
117
+ used_ctx = 1;
118
+ x = BN_CTX_get(ctx);
119
+ y = BN_CTX_get(ctx);
120
+ if (y == NULL) {
121
+ goto err;
122
+ }
123
+
124
+ if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) {
125
+ goto err;
126
+ }
127
+
128
+ if ((form == POINT_CONVERSION_COMPRESSED) &&
129
+ BN_is_odd(y)) {
130
+ buf[0] = form + 1;
131
+ } else {
132
+ buf[0] = form;
133
+ }
134
+ i = 1;
135
+
136
+ if (!BN_bn2bin_padded(buf + i, field_len, x)) {
137
+ OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
138
+ goto err;
139
+ }
140
+ i += field_len;
141
+
142
+ if (form == POINT_CONVERSION_UNCOMPRESSED) {
143
+ if (!BN_bn2bin_padded(buf + i, field_len, y)) {
144
+ OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
145
+ goto err;
146
+ }
147
+ i += field_len;
148
+ }
149
+
150
+ if (i != ret) {
151
+ OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
152
+ goto err;
153
+ }
154
+ }
155
+
156
+ if (used_ctx) {
157
+ BN_CTX_end(ctx);
158
+ }
159
+ BN_CTX_free(new_ctx);
160
+ return ret;
161
+
162
+ err:
163
+ if (used_ctx) {
164
+ BN_CTX_end(ctx);
165
+ }
166
+ BN_CTX_free(new_ctx);
167
+ return 0;
168
+ }
169
+
170
+
171
+ static int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
172
+ const uint8_t *buf, size_t len,
173
+ BN_CTX *ctx) {
174
+ point_conversion_form_t form;
175
+ int y_bit;
176
+ BN_CTX *new_ctx = NULL;
177
+ BIGNUM *x, *y;
178
+ size_t field_len, enc_len;
179
+ int ret = 0;
180
+
181
+ if (len == 0) {
182
+ OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL);
183
+ return 0;
184
+ }
185
+ form = buf[0];
186
+ y_bit = form & 1;
187
+ form = form & ~1U;
188
+ if ((form != POINT_CONVERSION_COMPRESSED &&
189
+ form != POINT_CONVERSION_UNCOMPRESSED) ||
190
+ (form == POINT_CONVERSION_UNCOMPRESSED && y_bit)) {
191
+ OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
192
+ return 0;
193
+ }
194
+
195
+ field_len = BN_num_bytes(&group->field);
196
+ enc_len =
197
+ (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
198
+
199
+ if (len != enc_len) {
200
+ OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
201
+ return 0;
202
+ }
203
+
204
+ if (ctx == NULL) {
205
+ ctx = new_ctx = BN_CTX_new();
206
+ if (ctx == NULL) {
207
+ return 0;
208
+ }
209
+ }
210
+
211
+ BN_CTX_start(ctx);
212
+ x = BN_CTX_get(ctx);
213
+ y = BN_CTX_get(ctx);
214
+ if (x == NULL || y == NULL) {
215
+ goto err;
216
+ }
217
+
218
+ if (!BN_bin2bn(buf + 1, field_len, x)) {
219
+ goto err;
220
+ }
221
+ if (BN_ucmp(x, &group->field) >= 0) {
222
+ OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
223
+ goto err;
224
+ }
225
+
226
+ if (form == POINT_CONVERSION_COMPRESSED) {
227
+ if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) {
228
+ goto err;
229
+ }
230
+ } else {
231
+ if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) {
232
+ goto err;
233
+ }
234
+ if (BN_ucmp(y, &group->field) >= 0) {
235
+ OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
236
+ goto err;
237
+ }
238
+
239
+ if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
240
+ goto err;
241
+ }
242
+ }
243
+
244
+ ret = 1;
245
+
246
+ err:
247
+ BN_CTX_end(ctx);
248
+ BN_CTX_free(new_ctx);
249
+ return ret;
250
+ }
251
+
252
+ int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
253
+ const uint8_t *buf, size_t len, BN_CTX *ctx) {
254
+ if (group->meth != point->meth) {
255
+ OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
256
+ return 0;
257
+ }
258
+ return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
259
+ }
260
+
261
+ size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
262
+ point_conversion_form_t form, uint8_t *buf,
263
+ size_t len, BN_CTX *ctx) {
264
+ if (group->meth != point->meth) {
265
+ OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
266
+ return 0;
267
+ }
268
+ return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx);
269
+ }
270
+
271
+ int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,
272
+ EC_POINT *point, const BIGNUM *x,
273
+ int y_bit, BN_CTX *ctx) {
274
+ if (BN_is_negative(x) || BN_cmp(x, &group->field) >= 0) {
275
+ OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT);
276
+ return 0;
277
+ }
278
+
279
+ BN_CTX *new_ctx = NULL;
280
+ BIGNUM *tmp1, *tmp2, *y;
281
+ int ret = 0;
282
+
283
+ ERR_clear_error();
284
+
285
+ if (ctx == NULL) {
286
+ ctx = new_ctx = BN_CTX_new();
287
+ if (ctx == NULL) {
288
+ return 0;
289
+ }
290
+ }
291
+
292
+ y_bit = (y_bit != 0);
293
+
294
+ BN_CTX_start(ctx);
295
+ tmp1 = BN_CTX_get(ctx);
296
+ tmp2 = BN_CTX_get(ctx);
297
+ y = BN_CTX_get(ctx);
298
+ if (y == NULL) {
299
+ goto err;
300
+ }
301
+
302
+ // Recover y. We have a Weierstrass equation
303
+ // y^2 = x^3 + a*x + b,
304
+ // so y is one of the square roots of x^3 + a*x + b.
305
+
306
+ // tmp1 := x^3
307
+ if (group->meth->field_decode == 0) {
308
+ // field_{sqr,mul} work on standard representation
309
+ if (!group->meth->field_sqr(group, tmp2, x, ctx) ||
310
+ !group->meth->field_mul(group, tmp1, tmp2, x, ctx)) {
311
+ goto err;
312
+ }
313
+ } else {
314
+ if (!BN_mod_sqr(tmp2, x, &group->field, ctx) ||
315
+ !BN_mod_mul(tmp1, tmp2, x, &group->field, ctx)) {
316
+ goto err;
317
+ }
318
+ }
319
+
320
+ // tmp1 := tmp1 + a*x
321
+ if (group->a_is_minus3) {
322
+ if (!BN_mod_lshift1_quick(tmp2, x, &group->field) ||
323
+ !BN_mod_add_quick(tmp2, tmp2, x, &group->field) ||
324
+ !BN_mod_sub_quick(tmp1, tmp1, tmp2, &group->field)) {
325
+ goto err;
326
+ }
327
+ } else {
328
+ if (group->meth->field_decode) {
329
+ if (!group->meth->field_decode(group, tmp2, &group->a, ctx) ||
330
+ !BN_mod_mul(tmp2, tmp2, x, &group->field, ctx)) {
331
+ goto err;
332
+ }
333
+ } else {
334
+ // field_mul works on standard representation
335
+ if (!group->meth->field_mul(group, tmp2, &group->a, x, ctx)) {
336
+ goto err;
337
+ }
338
+ }
339
+
340
+ if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) {
341
+ goto err;
342
+ }
343
+ }
344
+
345
+ // tmp1 := tmp1 + b
346
+ if (group->meth->field_decode) {
347
+ if (!group->meth->field_decode(group, tmp2, &group->b, ctx) ||
348
+ !BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) {
349
+ goto err;
350
+ }
351
+ } else {
352
+ if (!BN_mod_add_quick(tmp1, tmp1, &group->b, &group->field)) {
353
+ goto err;
354
+ }
355
+ }
356
+
357
+ if (!BN_mod_sqrt(y, tmp1, &group->field, ctx)) {
358
+ unsigned long err = ERR_peek_last_error();
359
+
360
+ if (ERR_GET_LIB(err) == ERR_LIB_BN &&
361
+ ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
362
+ ERR_clear_error();
363
+ OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT);
364
+ } else {
365
+ OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
366
+ }
367
+ goto err;
368
+ }
369
+
370
+ if (y_bit != BN_is_odd(y)) {
371
+ if (BN_is_zero(y)) {
372
+ OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSION_BIT);
373
+ goto err;
374
+ }
375
+ if (!BN_usub(y, &group->field, y)) {
376
+ goto err;
377
+ }
378
+ }
379
+ if (y_bit != BN_is_odd(y)) {
380
+ OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
381
+ goto err;
382
+ }
383
+
384
+ if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
385
+ goto err;
386
+ }
387
+
388
+ ret = 1;
389
+
390
+ err:
391
+ BN_CTX_end(ctx);
392
+ BN_CTX_free(new_ctx);
393
+ return ret;
394
+ }
395
+
396
+ int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
397
+ EC_POINT *point, const BIGNUM *x,
398
+ int y_bit, BN_CTX *ctx) {
399
+ if (group->meth != point->meth) {
400
+ OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
401
+ return 0;
402
+ }
403
+ return ec_GFp_simple_set_compressed_coordinates(group, point, x, y_bit, ctx);
404
+ }