/src/openssl34/crypto/ec/ecp_oct.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2011-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
4 | | * |
5 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
6 | | * this file except in compliance with the License. You can obtain a copy |
7 | | * in the file LICENSE in the source distribution or at |
8 | | * https://www.openssl.org/source/license.html |
9 | | */ |
10 | | |
11 | | /* |
12 | | * ECDSA low level APIs are deprecated for public use, but still ok for |
13 | | * internal use. |
14 | | */ |
15 | | #include "internal/deprecated.h" |
16 | | |
17 | | #include <openssl/err.h> |
18 | | #include <openssl/symhacks.h> |
19 | | |
20 | | #include "ec_local.h" |
21 | | |
22 | | int ossl_ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group, |
23 | | EC_POINT *point, |
24 | | const BIGNUM *x_, int y_bit, |
25 | | BN_CTX *ctx) |
26 | 187k | { |
27 | 187k | BN_CTX *new_ctx = NULL; |
28 | 187k | BIGNUM *tmp1, *tmp2, *x, *y; |
29 | 187k | int ret = 0; |
30 | | |
31 | 187k | if (ctx == NULL) { |
32 | 0 | ctx = new_ctx = BN_CTX_new_ex(group->libctx); |
33 | 0 | if (ctx == NULL) |
34 | 0 | return 0; |
35 | 0 | } |
36 | | |
37 | 187k | y_bit = (y_bit != 0); |
38 | | |
39 | 187k | BN_CTX_start(ctx); |
40 | 187k | tmp1 = BN_CTX_get(ctx); |
41 | 187k | tmp2 = BN_CTX_get(ctx); |
42 | 187k | x = BN_CTX_get(ctx); |
43 | 187k | y = BN_CTX_get(ctx); |
44 | 187k | if (y == NULL) |
45 | 0 | goto err; |
46 | | |
47 | | /*- |
48 | | * Recover y. We have a Weierstrass equation |
49 | | * y^2 = x^3 + a*x + b, |
50 | | * so y is one of the square roots of x^3 + a*x + b. |
51 | | */ |
52 | | |
53 | | /* tmp1 := x^3 */ |
54 | 187k | if (!BN_nnmod(x, x_, group->field, ctx)) |
55 | 0 | goto err; |
56 | 187k | if (group->meth->field_decode == 0) { |
57 | | /* field_{sqr,mul} work on standard representation */ |
58 | 73.7k | if (!group->meth->field_sqr(group, tmp2, x_, ctx)) |
59 | 0 | goto err; |
60 | 73.7k | if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx)) |
61 | 0 | goto err; |
62 | 113k | } else { |
63 | 113k | if (!BN_mod_sqr(tmp2, x_, group->field, ctx)) |
64 | 0 | goto err; |
65 | 113k | if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx)) |
66 | 0 | goto err; |
67 | 113k | } |
68 | | |
69 | | /* tmp1 := tmp1 + a*x */ |
70 | 187k | if (group->a_is_minus3) { |
71 | 142k | if (!BN_mod_lshift1_quick(tmp2, x, group->field)) |
72 | 0 | goto err; |
73 | 142k | if (!BN_mod_add_quick(tmp2, tmp2, x, group->field)) |
74 | 0 | goto err; |
75 | 142k | if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field)) |
76 | 0 | goto err; |
77 | 142k | } else { |
78 | 45.3k | if (group->meth->field_decode) { |
79 | 45.3k | if (!group->meth->field_decode(group, tmp2, group->a, ctx)) |
80 | 0 | goto err; |
81 | 45.3k | if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx)) |
82 | 0 | goto err; |
83 | 45.3k | } else { |
84 | | /* field_mul works on standard representation */ |
85 | 0 | if (!group->meth->field_mul(group, tmp2, group->a, x, ctx)) |
86 | 0 | goto err; |
87 | 0 | } |
88 | | |
89 | 45.3k | if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field)) |
90 | 0 | goto err; |
91 | 45.3k | } |
92 | | |
93 | | /* tmp1 := tmp1 + b */ |
94 | 187k | if (group->meth->field_decode) { |
95 | 113k | if (!group->meth->field_decode(group, tmp2, group->b, ctx)) |
96 | 0 | goto err; |
97 | 113k | if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field)) |
98 | 0 | goto err; |
99 | 113k | } else { |
100 | 73.7k | if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field)) |
101 | 0 | goto err; |
102 | 73.7k | } |
103 | | |
104 | 187k | ERR_set_mark(); |
105 | 187k | if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) { |
106 | 62.8k | #ifndef FIPS_MODULE |
107 | 62.8k | unsigned long err = ERR_peek_last_error(); |
108 | | |
109 | 62.8k | if (ERR_GET_LIB(err) == ERR_LIB_BN |
110 | 62.8k | && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) { |
111 | 62.1k | ERR_pop_to_mark(); |
112 | 62.1k | ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSED_POINT); |
113 | 62.1k | } else |
114 | 749 | #endif |
115 | 749 | { |
116 | 749 | ERR_clear_last_mark(); |
117 | 749 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); |
118 | 749 | } |
119 | 62.8k | goto err; |
120 | 62.8k | } |
121 | 124k | ERR_clear_last_mark(); |
122 | | |
123 | 124k | if (y_bit != BN_is_odd(y)) { |
124 | 63.1k | if (BN_is_zero(y)) { |
125 | 194 | int kron; |
126 | | |
127 | 194 | kron = BN_kronecker(x, group->field, ctx); |
128 | 194 | if (kron == -2) |
129 | 0 | goto err; |
130 | | |
131 | 194 | if (kron == 1) |
132 | 194 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSION_BIT); |
133 | 194 | else |
134 | | /* |
135 | | * BN_mod_sqrt() should have caught this error (not a square) |
136 | | */ |
137 | 194 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSED_POINT); |
138 | 194 | goto err; |
139 | 194 | } |
140 | 62.9k | if (!BN_usub(y, group->field, y)) |
141 | 0 | goto err; |
142 | 62.9k | } |
143 | 124k | if (y_bit != BN_is_odd(y)) { |
144 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR); |
145 | 0 | goto err; |
146 | 0 | } |
147 | | |
148 | 124k | if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx)) |
149 | 0 | goto err; |
150 | | |
151 | 124k | ret = 1; |
152 | | |
153 | 187k | err: |
154 | 187k | BN_CTX_end(ctx); |
155 | 187k | BN_CTX_free(new_ctx); |
156 | 187k | return ret; |
157 | 124k | } |
158 | | |
159 | | size_t ossl_ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, |
160 | | point_conversion_form_t form, |
161 | | unsigned char *buf, size_t len, BN_CTX *ctx) |
162 | 238k | { |
163 | 238k | size_t ret; |
164 | 238k | BN_CTX *new_ctx = NULL; |
165 | 238k | int used_ctx = 0; |
166 | 238k | BIGNUM *x, *y; |
167 | 238k | size_t field_len, i, skip; |
168 | | |
169 | 238k | if ((form != POINT_CONVERSION_COMPRESSED) |
170 | 218k | && (form != POINT_CONVERSION_UNCOMPRESSED) |
171 | 627 | && (form != POINT_CONVERSION_HYBRID)) { |
172 | 329 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM); |
173 | 329 | goto err; |
174 | 329 | } |
175 | | |
176 | 237k | if (EC_POINT_is_at_infinity(group, point)) { |
177 | | /* encodes to a single 0 octet */ |
178 | 1.42k | if (buf != NULL) { |
179 | 710 | if (len < 1) { |
180 | 0 | ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL); |
181 | 0 | return 0; |
182 | 0 | } |
183 | 710 | buf[0] = 0; |
184 | 710 | } |
185 | 1.42k | return 1; |
186 | 1.42k | } |
187 | | |
188 | | /* ret := required output buffer length */ |
189 | 236k | field_len = BN_num_bytes(group->field); |
190 | 236k | ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len; |
191 | | |
192 | | /* if 'buf' is NULL, just return required length */ |
193 | 236k | if (buf != NULL) { |
194 | 118k | if (len < ret) { |
195 | 0 | ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL); |
196 | 0 | goto err; |
197 | 0 | } |
198 | | |
199 | 118k | if (ctx == NULL) { |
200 | 14.6k | ctx = new_ctx = BN_CTX_new_ex(group->libctx); |
201 | 14.6k | if (ctx == NULL) |
202 | 0 | return 0; |
203 | 14.6k | } |
204 | | |
205 | 118k | BN_CTX_start(ctx); |
206 | 118k | used_ctx = 1; |
207 | 118k | x = BN_CTX_get(ctx); |
208 | 118k | y = BN_CTX_get(ctx); |
209 | 118k | if (y == NULL) |
210 | 0 | goto err; |
211 | | |
212 | 118k | if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx)) |
213 | 0 | goto err; |
214 | | |
215 | 118k | if ((form == POINT_CONVERSION_COMPRESSED |
216 | 108k | || form == POINT_CONVERSION_HYBRID) |
217 | 9.87k | && BN_is_odd(y)) |
218 | 4.28k | buf[0] = form + 1; |
219 | 113k | else |
220 | 113k | buf[0] = form; |
221 | | |
222 | 118k | i = 1; |
223 | | |
224 | 118k | skip = field_len - BN_num_bytes(x); |
225 | 118k | if (skip > field_len) { |
226 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR); |
227 | 0 | goto err; |
228 | 0 | } |
229 | 188k | while (skip > 0) { |
230 | 69.7k | buf[i++] = 0; |
231 | 69.7k | skip--; |
232 | 69.7k | } |
233 | 118k | skip = BN_bn2bin(x, buf + i); |
234 | 118k | i += skip; |
235 | 118k | if (i != 1 + field_len) { |
236 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR); |
237 | 0 | goto err; |
238 | 0 | } |
239 | | |
240 | 118k | if (form == POINT_CONVERSION_UNCOMPRESSED |
241 | 108k | || form == POINT_CONVERSION_HYBRID) { |
242 | 108k | skip = field_len - BN_num_bytes(y); |
243 | 108k | if (skip > field_len) { |
244 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR); |
245 | 0 | goto err; |
246 | 0 | } |
247 | 117k | while (skip > 0) { |
248 | 8.72k | buf[i++] = 0; |
249 | 8.72k | skip--; |
250 | 8.72k | } |
251 | 108k | skip = BN_bn2bin(y, buf + i); |
252 | 108k | i += skip; |
253 | 108k | } |
254 | | |
255 | 118k | if (i != ret) { |
256 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR); |
257 | 0 | goto err; |
258 | 0 | } |
259 | 118k | } |
260 | | |
261 | 236k | if (used_ctx) |
262 | 118k | BN_CTX_end(ctx); |
263 | 236k | BN_CTX_free(new_ctx); |
264 | 236k | return ret; |
265 | | |
266 | 329 | err: |
267 | 329 | if (used_ctx) |
268 | 0 | BN_CTX_end(ctx); |
269 | 329 | BN_CTX_free(new_ctx); |
270 | 329 | return 0; |
271 | 236k | } |
272 | | |
273 | | int ossl_ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point, |
274 | | const unsigned char *buf, size_t len, |
275 | | BN_CTX *ctx) |
276 | 481k | { |
277 | 481k | point_conversion_form_t form; |
278 | 481k | int y_bit; |
279 | 481k | BN_CTX *new_ctx = NULL; |
280 | 481k | BIGNUM *x, *y; |
281 | 481k | size_t field_len, enc_len; |
282 | 481k | int ret = 0; |
283 | | |
284 | 481k | if (len == 0) { |
285 | 16.1k | ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL); |
286 | 16.1k | return 0; |
287 | 16.1k | } |
288 | 465k | form = buf[0]; |
289 | 465k | y_bit = form & 1; |
290 | 465k | form = form & ~1U; |
291 | 465k | if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED) |
292 | 241k | && (form != POINT_CONVERSION_UNCOMPRESSED) |
293 | 20.4k | && (form != POINT_CONVERSION_HYBRID)) { |
294 | 8.66k | ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
295 | 8.66k | return 0; |
296 | 8.66k | } |
297 | 456k | if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) { |
298 | 4.41k | ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
299 | 4.41k | return 0; |
300 | 4.41k | } |
301 | | |
302 | 452k | if (form == 0) { |
303 | 23.2k | if (len != 1) { |
304 | 6.46k | ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
305 | 6.46k | return 0; |
306 | 6.46k | } |
307 | | |
308 | 16.7k | return EC_POINT_set_to_infinity(group, point); |
309 | 23.2k | } |
310 | | |
311 | 428k | field_len = BN_num_bytes(group->field); |
312 | 428k | enc_len = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len; |
313 | | |
314 | 428k | if (len != enc_len) { |
315 | 9.31k | ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
316 | 9.31k | return 0; |
317 | 9.31k | } |
318 | | |
319 | 419k | if (ctx == NULL) { |
320 | 320k | ctx = new_ctx = BN_CTX_new_ex(group->libctx); |
321 | 320k | if (ctx == NULL) |
322 | 0 | return 0; |
323 | 320k | } |
324 | | |
325 | 419k | BN_CTX_start(ctx); |
326 | 419k | x = BN_CTX_get(ctx); |
327 | 419k | y = BN_CTX_get(ctx); |
328 | 419k | if (y == NULL) |
329 | 0 | goto err; |
330 | | |
331 | 419k | if (!BN_bin2bn(buf + 1, field_len, x)) |
332 | 0 | goto err; |
333 | 419k | if (BN_ucmp(x, group->field) >= 0) { |
334 | 4.73k | ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
335 | 4.73k | goto err; |
336 | 4.73k | } |
337 | | |
338 | 414k | if (form == POINT_CONVERSION_COMPRESSED) { |
339 | 187k | if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx)) |
340 | 63.0k | goto err; |
341 | 227k | } else { |
342 | 227k | if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) |
343 | 0 | goto err; |
344 | 227k | if (BN_ucmp(y, group->field) >= 0) { |
345 | 583 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
346 | 583 | goto err; |
347 | 583 | } |
348 | 226k | if (form == POINT_CONVERSION_HYBRID) { |
349 | 7.37k | if (y_bit != BN_is_odd(y)) { |
350 | 1.42k | ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
351 | 1.42k | goto err; |
352 | 1.42k | } |
353 | 7.37k | } |
354 | | |
355 | | /* |
356 | | * EC_POINT_set_affine_coordinates is responsible for checking that |
357 | | * the point is on the curve. |
358 | | */ |
359 | 225k | if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx)) |
360 | 24.9k | goto err; |
361 | 225k | } |
362 | | |
363 | 324k | ret = 1; |
364 | | |
365 | 419k | err: |
366 | 419k | BN_CTX_end(ctx); |
367 | 419k | BN_CTX_free(new_ctx); |
368 | 419k | return ret; |
369 | 324k | } |