/src/libressl/crypto/ec/ecp_oct.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: ecp_oct.c,v 1.14 2021/04/20 17:32:57 tb Exp $ */ |
2 | | /* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de> |
3 | | * for the OpenSSL project. |
4 | | * Includes code written by Bodo Moeller for the OpenSSL project. |
5 | | */ |
6 | | /* ==================================================================== |
7 | | * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. |
8 | | * |
9 | | * Redistribution and use in source and binary forms, with or without |
10 | | * modification, are permitted provided that the following conditions |
11 | | * are met: |
12 | | * |
13 | | * 1. Redistributions of source code must retain the above copyright |
14 | | * notice, this list of conditions and the following disclaimer. |
15 | | * |
16 | | * 2. Redistributions in binary form must reproduce the above copyright |
17 | | * notice, this list of conditions and the following disclaimer in |
18 | | * the documentation and/or other materials provided with the |
19 | | * distribution. |
20 | | * |
21 | | * 3. All advertising materials mentioning features or use of this |
22 | | * software must display the following acknowledgment: |
23 | | * "This product includes software developed by the OpenSSL Project |
24 | | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
25 | | * |
26 | | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
27 | | * endorse or promote products derived from this software without |
28 | | * prior written permission. For written permission, please contact |
29 | | * openssl-core@openssl.org. |
30 | | * |
31 | | * 5. Products derived from this software may not be called "OpenSSL" |
32 | | * nor may "OpenSSL" appear in their names without prior written |
33 | | * permission of the OpenSSL Project. |
34 | | * |
35 | | * 6. Redistributions of any form whatsoever must retain the following |
36 | | * acknowledgment: |
37 | | * "This product includes software developed by the OpenSSL Project |
38 | | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
39 | | * |
40 | | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
41 | | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
42 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
43 | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
44 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
45 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
46 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
47 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
48 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
49 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
50 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
51 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
52 | | * ==================================================================== |
53 | | * |
54 | | * This product includes cryptographic software written by Eric Young |
55 | | * (eay@cryptsoft.com). This product includes software written by Tim |
56 | | * Hudson (tjh@cryptsoft.com). |
57 | | * |
58 | | */ |
59 | | /* ==================================================================== |
60 | | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
61 | | * Portions of this software developed by SUN MICROSYSTEMS, INC., |
62 | | * and contributed to the OpenSSL project. |
63 | | */ |
64 | | |
65 | | #include <openssl/err.h> |
66 | | |
67 | | #include "ec_lcl.h" |
68 | | |
69 | | int |
70 | | ec_GFp_simple_set_compressed_coordinates(const EC_GROUP * group, |
71 | | EC_POINT * point, const BIGNUM * x_, int y_bit, BN_CTX * ctx) |
72 | 0 | { |
73 | 0 | BN_CTX *new_ctx = NULL; |
74 | 0 | BIGNUM *tmp1, *tmp2, *x, *y; |
75 | 0 | int ret = 0; |
76 | | |
77 | | /* clear error queue */ |
78 | 0 | ERR_clear_error(); |
79 | |
|
80 | 0 | if (ctx == NULL) { |
81 | 0 | ctx = new_ctx = BN_CTX_new(); |
82 | 0 | if (ctx == NULL) |
83 | 0 | return 0; |
84 | 0 | } |
85 | 0 | y_bit = (y_bit != 0); |
86 | |
|
87 | 0 | BN_CTX_start(ctx); |
88 | 0 | if ((tmp1 = BN_CTX_get(ctx)) == NULL) |
89 | 0 | goto err; |
90 | 0 | if ((tmp2 = BN_CTX_get(ctx)) == NULL) |
91 | 0 | goto err; |
92 | 0 | if ((x = BN_CTX_get(ctx)) == NULL) |
93 | 0 | goto err; |
94 | 0 | if ((y = BN_CTX_get(ctx)) == NULL) |
95 | 0 | goto err; |
96 | | |
97 | | /* |
98 | | * Recover y. We have a Weierstrass equation y^2 = x^3 + a*x + b, so |
99 | | * y is one of the square roots of x^3 + a*x + b. |
100 | | */ |
101 | | |
102 | | /* tmp1 := x^3 */ |
103 | 0 | if (!BN_nnmod(x, x_, &group->field, ctx)) |
104 | 0 | goto err; |
105 | 0 | if (group->meth->field_decode == 0) { |
106 | | /* field_{sqr,mul} work on standard representation */ |
107 | 0 | if (!group->meth->field_sqr(group, tmp2, x_, ctx)) |
108 | 0 | goto err; |
109 | 0 | if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx)) |
110 | 0 | goto err; |
111 | 0 | } else { |
112 | 0 | if (!BN_mod_sqr(tmp2, x_, &group->field, ctx)) |
113 | 0 | goto err; |
114 | 0 | if (!BN_mod_mul(tmp1, tmp2, x_, &group->field, ctx)) |
115 | 0 | goto err; |
116 | 0 | } |
117 | | |
118 | | /* tmp1 := tmp1 + a*x */ |
119 | 0 | if (group->a_is_minus3) { |
120 | 0 | if (!BN_mod_lshift1_quick(tmp2, x, &group->field)) |
121 | 0 | goto err; |
122 | 0 | if (!BN_mod_add_quick(tmp2, tmp2, x, &group->field)) |
123 | 0 | goto err; |
124 | 0 | if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, &group->field)) |
125 | 0 | goto err; |
126 | 0 | } else { |
127 | 0 | if (group->meth->field_decode) { |
128 | 0 | if (!group->meth->field_decode(group, tmp2, &group->a, ctx)) |
129 | 0 | goto err; |
130 | 0 | if (!BN_mod_mul(tmp2, tmp2, x, &group->field, ctx)) |
131 | 0 | goto err; |
132 | 0 | } else { |
133 | | /* field_mul works on standard representation */ |
134 | 0 | if (!group->meth->field_mul(group, tmp2, &group->a, x, ctx)) |
135 | 0 | goto err; |
136 | 0 | } |
137 | | |
138 | 0 | if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) |
139 | 0 | goto err; |
140 | 0 | } |
141 | | |
142 | | /* tmp1 := tmp1 + b */ |
143 | 0 | if (group->meth->field_decode) { |
144 | 0 | if (!group->meth->field_decode(group, tmp2, &group->b, ctx)) |
145 | 0 | goto err; |
146 | 0 | if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) |
147 | 0 | goto err; |
148 | 0 | } else { |
149 | 0 | if (!BN_mod_add_quick(tmp1, tmp1, &group->b, &group->field)) |
150 | 0 | goto err; |
151 | 0 | } |
152 | | |
153 | 0 | if (!BN_mod_sqrt(y, tmp1, &group->field, ctx)) { |
154 | 0 | unsigned long err = ERR_peek_last_error(); |
155 | |
|
156 | 0 | if (ERR_GET_LIB(err) == ERR_LIB_BN && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) { |
157 | 0 | ERR_clear_error(); |
158 | 0 | ECerror(EC_R_INVALID_COMPRESSED_POINT); |
159 | 0 | } else |
160 | 0 | ECerror(ERR_R_BN_LIB); |
161 | 0 | goto err; |
162 | 0 | } |
163 | 0 | if (y_bit != BN_is_odd(y)) { |
164 | 0 | if (BN_is_zero(y)) { |
165 | 0 | int kron; |
166 | |
|
167 | 0 | kron = BN_kronecker(x, &group->field, ctx); |
168 | 0 | if (kron == -2) |
169 | 0 | goto err; |
170 | | |
171 | 0 | if (kron == 1) |
172 | 0 | ECerror(EC_R_INVALID_COMPRESSION_BIT); |
173 | 0 | else |
174 | | /* |
175 | | * BN_mod_sqrt() should have cought this |
176 | | * error (not a square) |
177 | | */ |
178 | 0 | ECerror(EC_R_INVALID_COMPRESSED_POINT); |
179 | 0 | goto err; |
180 | 0 | } |
181 | 0 | if (!BN_usub(y, &group->field, y)) |
182 | 0 | goto err; |
183 | 0 | } |
184 | 0 | if (y_bit != BN_is_odd(y)) { |
185 | 0 | ECerror(ERR_R_INTERNAL_ERROR); |
186 | 0 | goto err; |
187 | 0 | } |
188 | 0 | if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx)) |
189 | 0 | goto err; |
190 | | |
191 | 0 | ret = 1; |
192 | |
|
193 | 0 | err: |
194 | 0 | BN_CTX_end(ctx); |
195 | 0 | BN_CTX_free(new_ctx); |
196 | 0 | return ret; |
197 | 0 | } |
198 | | |
199 | | |
200 | | size_t |
201 | | ec_GFp_simple_point2oct(const EC_GROUP * group, const EC_POINT * point, point_conversion_form_t form, |
202 | | unsigned char *buf, size_t len, BN_CTX * ctx) |
203 | 0 | { |
204 | 0 | size_t ret; |
205 | 0 | BN_CTX *new_ctx = NULL; |
206 | 0 | int used_ctx = 0; |
207 | 0 | BIGNUM *x, *y; |
208 | 0 | size_t field_len, i, skip; |
209 | |
|
210 | 0 | if ((form != POINT_CONVERSION_COMPRESSED) |
211 | 0 | && (form != POINT_CONVERSION_UNCOMPRESSED) |
212 | 0 | && (form != POINT_CONVERSION_HYBRID)) { |
213 | 0 | ECerror(EC_R_INVALID_FORM); |
214 | 0 | goto err; |
215 | 0 | } |
216 | 0 | if (EC_POINT_is_at_infinity(group, point) > 0) { |
217 | | /* encodes to a single 0 octet */ |
218 | 0 | if (buf != NULL) { |
219 | 0 | if (len < 1) { |
220 | 0 | ECerror(EC_R_BUFFER_TOO_SMALL); |
221 | 0 | return 0; |
222 | 0 | } |
223 | 0 | buf[0] = 0; |
224 | 0 | } |
225 | 0 | return 1; |
226 | 0 | } |
227 | | /* ret := required output buffer length */ |
228 | 0 | field_len = BN_num_bytes(&group->field); |
229 | 0 | ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len; |
230 | | |
231 | | /* if 'buf' is NULL, just return required length */ |
232 | 0 | if (buf != NULL) { |
233 | 0 | if (len < ret) { |
234 | 0 | ECerror(EC_R_BUFFER_TOO_SMALL); |
235 | 0 | goto err; |
236 | 0 | } |
237 | 0 | if (ctx == NULL) { |
238 | 0 | ctx = new_ctx = BN_CTX_new(); |
239 | 0 | if (ctx == NULL) |
240 | 0 | return 0; |
241 | 0 | } |
242 | 0 | BN_CTX_start(ctx); |
243 | 0 | used_ctx = 1; |
244 | 0 | if ((x = BN_CTX_get(ctx)) == NULL) |
245 | 0 | goto err; |
246 | 0 | if ((y = BN_CTX_get(ctx)) == NULL) |
247 | 0 | goto err; |
248 | | |
249 | 0 | if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx)) |
250 | 0 | goto err; |
251 | | |
252 | 0 | if ((form == POINT_CONVERSION_COMPRESSED || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y)) |
253 | 0 | buf[0] = form + 1; |
254 | 0 | else |
255 | 0 | buf[0] = form; |
256 | |
|
257 | 0 | i = 1; |
258 | |
|
259 | 0 | skip = field_len - BN_num_bytes(x); |
260 | 0 | if (skip > field_len) { |
261 | 0 | ECerror(ERR_R_INTERNAL_ERROR); |
262 | 0 | goto err; |
263 | 0 | } |
264 | 0 | while (skip > 0) { |
265 | 0 | buf[i++] = 0; |
266 | 0 | skip--; |
267 | 0 | } |
268 | 0 | skip = BN_bn2bin(x, buf + i); |
269 | 0 | i += skip; |
270 | 0 | if (i != 1 + field_len) { |
271 | 0 | ECerror(ERR_R_INTERNAL_ERROR); |
272 | 0 | goto err; |
273 | 0 | } |
274 | 0 | if (form == POINT_CONVERSION_UNCOMPRESSED || form == POINT_CONVERSION_HYBRID) { |
275 | 0 | skip = field_len - BN_num_bytes(y); |
276 | 0 | if (skip > field_len) { |
277 | 0 | ECerror(ERR_R_INTERNAL_ERROR); |
278 | 0 | goto err; |
279 | 0 | } |
280 | 0 | while (skip > 0) { |
281 | 0 | buf[i++] = 0; |
282 | 0 | skip--; |
283 | 0 | } |
284 | 0 | skip = BN_bn2bin(y, buf + i); |
285 | 0 | i += skip; |
286 | 0 | } |
287 | 0 | if (i != ret) { |
288 | 0 | ECerror(ERR_R_INTERNAL_ERROR); |
289 | 0 | goto err; |
290 | 0 | } |
291 | 0 | } |
292 | 0 | if (used_ctx) |
293 | 0 | BN_CTX_end(ctx); |
294 | 0 | BN_CTX_free(new_ctx); |
295 | 0 | return ret; |
296 | | |
297 | 0 | err: |
298 | 0 | if (used_ctx) |
299 | 0 | BN_CTX_end(ctx); |
300 | 0 | BN_CTX_free(new_ctx); |
301 | 0 | return 0; |
302 | 0 | } |
303 | | |
304 | | |
305 | | int |
306 | | ec_GFp_simple_oct2point(const EC_GROUP * group, EC_POINT * point, |
307 | | const unsigned char *buf, size_t len, BN_CTX * ctx) |
308 | 0 | { |
309 | 0 | point_conversion_form_t form; |
310 | 0 | int y_bit; |
311 | 0 | BN_CTX *new_ctx = NULL; |
312 | 0 | BIGNUM *x, *y; |
313 | 0 | size_t field_len, enc_len; |
314 | 0 | int ret = 0; |
315 | |
|
316 | 0 | if (len == 0) { |
317 | 0 | ECerror(EC_R_BUFFER_TOO_SMALL); |
318 | 0 | return 0; |
319 | 0 | } |
320 | 0 | form = buf[0]; |
321 | 0 | y_bit = form & 1; |
322 | 0 | form = form & ~1U; |
323 | 0 | if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED) |
324 | 0 | && (form != POINT_CONVERSION_UNCOMPRESSED) |
325 | 0 | && (form != POINT_CONVERSION_HYBRID)) { |
326 | 0 | ECerror(EC_R_INVALID_ENCODING); |
327 | 0 | return 0; |
328 | 0 | } |
329 | 0 | if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) { |
330 | 0 | ECerror(EC_R_INVALID_ENCODING); |
331 | 0 | return 0; |
332 | 0 | } |
333 | 0 | if (form == 0) { |
334 | 0 | if (len != 1) { |
335 | 0 | ECerror(EC_R_INVALID_ENCODING); |
336 | 0 | return 0; |
337 | 0 | } |
338 | 0 | return EC_POINT_set_to_infinity(group, point); |
339 | 0 | } |
340 | 0 | field_len = BN_num_bytes(&group->field); |
341 | 0 | enc_len = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len; |
342 | |
|
343 | 0 | if (len != enc_len) { |
344 | 0 | ECerror(EC_R_INVALID_ENCODING); |
345 | 0 | return 0; |
346 | 0 | } |
347 | 0 | if (ctx == NULL) { |
348 | 0 | ctx = new_ctx = BN_CTX_new(); |
349 | 0 | if (ctx == NULL) |
350 | 0 | return 0; |
351 | 0 | } |
352 | 0 | BN_CTX_start(ctx); |
353 | 0 | if ((x = BN_CTX_get(ctx)) == NULL) |
354 | 0 | goto err; |
355 | 0 | if ((y = BN_CTX_get(ctx)) == NULL) |
356 | 0 | goto err; |
357 | | |
358 | 0 | if (!BN_bin2bn(buf + 1, field_len, x)) |
359 | 0 | goto err; |
360 | 0 | if (BN_ucmp(x, &group->field) >= 0) { |
361 | 0 | ECerror(EC_R_INVALID_ENCODING); |
362 | 0 | goto err; |
363 | 0 | } |
364 | 0 | if (form == POINT_CONVERSION_COMPRESSED) { |
365 | | /* |
366 | | * EC_POINT_set_compressed_coordinates checks that the point |
367 | | * is on the curve as required by X9.62. |
368 | | */ |
369 | 0 | if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx)) |
370 | 0 | goto err; |
371 | 0 | } else { |
372 | 0 | if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) |
373 | 0 | goto err; |
374 | 0 | if (BN_ucmp(y, &group->field) >= 0) { |
375 | 0 | ECerror(EC_R_INVALID_ENCODING); |
376 | 0 | goto err; |
377 | 0 | } |
378 | 0 | if (form == POINT_CONVERSION_HYBRID) { |
379 | 0 | if (y_bit != BN_is_odd(y)) { |
380 | 0 | ECerror(EC_R_INVALID_ENCODING); |
381 | 0 | goto err; |
382 | 0 | } |
383 | 0 | } |
384 | | /* |
385 | | * EC_POINT_set_affine_coordinates checks that the point is |
386 | | * on the curve as required by X9.62. |
387 | | */ |
388 | 0 | if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx)) |
389 | 0 | goto err; |
390 | 0 | } |
391 | | |
392 | 0 | ret = 1; |
393 | |
|
394 | 0 | err: |
395 | 0 | BN_CTX_end(ctx); |
396 | 0 | BN_CTX_free(new_ctx); |
397 | 0 | return ret; |
398 | 0 | } |