/src/openssl32/crypto/ec/ecp_oct.c
Line | Count | Source (jump to first uncovered line) |
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 | 116k | { |
27 | 116k | BN_CTX *new_ctx = NULL; |
28 | 116k | BIGNUM *tmp1, *tmp2, *x, *y; |
29 | 116k | int ret = 0; |
30 | | |
31 | 116k | 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 | 116k | y_bit = (y_bit != 0); |
38 | | |
39 | 116k | BN_CTX_start(ctx); |
40 | 116k | tmp1 = BN_CTX_get(ctx); |
41 | 116k | tmp2 = BN_CTX_get(ctx); |
42 | 116k | x = BN_CTX_get(ctx); |
43 | 116k | y = BN_CTX_get(ctx); |
44 | 116k | 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 | 116k | if (!BN_nnmod(x, x_, group->field, ctx)) |
55 | 0 | goto err; |
56 | 116k | if (group->meth->field_decode == 0) { |
57 | | /* field_{sqr,mul} work on standard representation */ |
58 | 43.1k | if (!group->meth->field_sqr(group, tmp2, x_, ctx)) |
59 | 0 | goto err; |
60 | 43.1k | if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx)) |
61 | 0 | goto err; |
62 | 73.6k | } else { |
63 | 73.6k | if (!BN_mod_sqr(tmp2, x_, group->field, ctx)) |
64 | 0 | goto err; |
65 | 73.6k | if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx)) |
66 | 0 | goto err; |
67 | 73.6k | } |
68 | | |
69 | | /* tmp1 := tmp1 + a*x */ |
70 | 116k | if (group->a_is_minus3) { |
71 | 87.7k | if (!BN_mod_lshift1_quick(tmp2, x, group->field)) |
72 | 0 | goto err; |
73 | 87.7k | if (!BN_mod_add_quick(tmp2, tmp2, x, group->field)) |
74 | 0 | goto err; |
75 | 87.7k | if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field)) |
76 | 0 | goto err; |
77 | 87.7k | } else { |
78 | 29.0k | if (group->meth->field_decode) { |
79 | 29.0k | if (!group->meth->field_decode(group, tmp2, group->a, ctx)) |
80 | 0 | goto err; |
81 | 29.0k | if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx)) |
82 | 0 | goto err; |
83 | 29.0k | } 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 | 29.0k | if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field)) |
90 | 0 | goto err; |
91 | 29.0k | } |
92 | | |
93 | | /* tmp1 := tmp1 + b */ |
94 | 116k | if (group->meth->field_decode) { |
95 | 73.6k | if (!group->meth->field_decode(group, tmp2, group->b, ctx)) |
96 | 0 | goto err; |
97 | 73.6k | if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field)) |
98 | 0 | goto err; |
99 | 73.6k | } else { |
100 | 43.1k | if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field)) |
101 | 0 | goto err; |
102 | 43.1k | } |
103 | | |
104 | 116k | ERR_set_mark(); |
105 | 116k | if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) { |
106 | 32.3k | #ifndef FIPS_MODULE |
107 | 32.3k | unsigned long err = ERR_peek_last_error(); |
108 | | |
109 | 32.3k | if (ERR_GET_LIB(err) == ERR_LIB_BN |
110 | 32.3k | && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) { |
111 | 31.9k | ERR_pop_to_mark(); |
112 | 31.9k | ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSED_POINT); |
113 | 31.9k | } else |
114 | 479 | #endif |
115 | 479 | { |
116 | 479 | ERR_clear_last_mark(); |
117 | 479 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); |
118 | 479 | } |
119 | 32.3k | goto err; |
120 | 32.3k | } |
121 | 84.4k | ERR_clear_last_mark(); |
122 | | |
123 | 84.4k | if (y_bit != BN_is_odd(y)) { |
124 | 41.1k | if (BN_is_zero(y)) { |
125 | 95 | int kron; |
126 | | |
127 | 95 | kron = BN_kronecker(x, group->field, ctx); |
128 | 95 | if (kron == -2) |
129 | 0 | goto err; |
130 | | |
131 | 95 | if (kron == 1) |
132 | 95 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSION_BIT); |
133 | 95 | else |
134 | | /* |
135 | | * BN_mod_sqrt() should have caught this error (not a square) |
136 | | */ |
137 | 95 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSED_POINT); |
138 | 95 | goto err; |
139 | 95 | } |
140 | 41.0k | if (!BN_usub(y, group->field, y)) |
141 | 0 | goto err; |
142 | 41.0k | } |
143 | 84.3k | 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 | 84.3k | if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx)) |
149 | 0 | goto err; |
150 | | |
151 | 84.3k | ret = 1; |
152 | | |
153 | 116k | err: |
154 | 116k | BN_CTX_end(ctx); |
155 | 116k | BN_CTX_free(new_ctx); |
156 | 116k | return ret; |
157 | 84.3k | } |
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 | 127k | { |
163 | 127k | size_t ret; |
164 | 127k | BN_CTX *new_ctx = NULL; |
165 | 127k | int used_ctx = 0; |
166 | 127k | BIGNUM *x, *y; |
167 | 127k | size_t field_len, i, skip; |
168 | | |
169 | 127k | if ((form != POINT_CONVERSION_COMPRESSED) |
170 | 127k | && (form != POINT_CONVERSION_UNCOMPRESSED) |
171 | 127k | && (form != POINT_CONVERSION_HYBRID)) { |
172 | 220 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM); |
173 | 220 | goto err; |
174 | 220 | } |
175 | | |
176 | 127k | if (EC_POINT_is_at_infinity(group, point)) { |
177 | | /* encodes to a single 0 octet */ |
178 | 866 | if (buf != NULL) { |
179 | 433 | if (len < 1) { |
180 | 0 | ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL); |
181 | 0 | return 0; |
182 | 0 | } |
183 | 433 | buf[0] = 0; |
184 | 433 | } |
185 | 866 | return 1; |
186 | 866 | } |
187 | | |
188 | | /* ret := required output buffer length */ |
189 | 126k | field_len = BN_num_bytes(group->field); |
190 | 126k | ret = |
191 | 126k | (form == |
192 | 126k | POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len; |
193 | | |
194 | | /* if 'buf' is NULL, just return required length */ |
195 | 126k | if (buf != NULL) { |
196 | 63.4k | if (len < ret) { |
197 | 0 | ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL); |
198 | 0 | goto err; |
199 | 0 | } |
200 | | |
201 | 63.4k | if (ctx == NULL) { |
202 | 7.16k | ctx = new_ctx = BN_CTX_new_ex(group->libctx); |
203 | 7.16k | if (ctx == NULL) |
204 | 0 | return 0; |
205 | 7.16k | } |
206 | | |
207 | 63.4k | BN_CTX_start(ctx); |
208 | 63.4k | used_ctx = 1; |
209 | 63.4k | x = BN_CTX_get(ctx); |
210 | 63.4k | y = BN_CTX_get(ctx); |
211 | 63.4k | if (y == NULL) |
212 | 0 | goto err; |
213 | | |
214 | 63.4k | if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx)) |
215 | 0 | goto err; |
216 | | |
217 | 63.4k | if ((form == POINT_CONVERSION_COMPRESSED |
218 | 63.4k | || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y)) |
219 | 1.64k | buf[0] = form + 1; |
220 | 61.8k | else |
221 | 61.8k | buf[0] = form; |
222 | | |
223 | 63.4k | i = 1; |
224 | | |
225 | 63.4k | skip = field_len - BN_num_bytes(x); |
226 | 63.4k | if (skip > field_len) { |
227 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR); |
228 | 0 | goto err; |
229 | 0 | } |
230 | 98.1k | while (skip > 0) { |
231 | 34.6k | buf[i++] = 0; |
232 | 34.6k | skip--; |
233 | 34.6k | } |
234 | 63.4k | skip = BN_bn2bin(x, buf + i); |
235 | 63.4k | i += skip; |
236 | 63.4k | if (i != 1 + field_len) { |
237 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR); |
238 | 0 | goto err; |
239 | 0 | } |
240 | | |
241 | 63.4k | if (form == POINT_CONVERSION_UNCOMPRESSED |
242 | 63.4k | || form == POINT_CONVERSION_HYBRID) { |
243 | 58.3k | skip = field_len - BN_num_bytes(y); |
244 | 58.3k | if (skip > field_len) { |
245 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR); |
246 | 0 | goto err; |
247 | 0 | } |
248 | 64.4k | while (skip > 0) { |
249 | 6.06k | buf[i++] = 0; |
250 | 6.06k | skip--; |
251 | 6.06k | } |
252 | 58.3k | skip = BN_bn2bin(y, buf + i); |
253 | 58.3k | i += skip; |
254 | 58.3k | } |
255 | | |
256 | 63.4k | if (i != ret) { |
257 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR); |
258 | 0 | goto err; |
259 | 0 | } |
260 | 63.4k | } |
261 | | |
262 | 126k | if (used_ctx) |
263 | 63.4k | BN_CTX_end(ctx); |
264 | 126k | BN_CTX_free(new_ctx); |
265 | 126k | return ret; |
266 | | |
267 | 220 | err: |
268 | 220 | if (used_ctx) |
269 | 0 | BN_CTX_end(ctx); |
270 | 220 | BN_CTX_free(new_ctx); |
271 | 220 | return 0; |
272 | 126k | } |
273 | | |
274 | | int ossl_ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point, |
275 | | const unsigned char *buf, size_t len, |
276 | | BN_CTX *ctx) |
277 | 291k | { |
278 | 291k | point_conversion_form_t form; |
279 | 291k | int y_bit; |
280 | 291k | BN_CTX *new_ctx = NULL; |
281 | 291k | BIGNUM *x, *y; |
282 | 291k | size_t field_len, enc_len; |
283 | 291k | int ret = 0; |
284 | | |
285 | 291k | if (len == 0) { |
286 | 8.18k | ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL); |
287 | 8.18k | return 0; |
288 | 8.18k | } |
289 | 283k | form = buf[0]; |
290 | 283k | y_bit = form & 1; |
291 | 283k | form = form & ~1U; |
292 | 283k | if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED) |
293 | 283k | && (form != POINT_CONVERSION_UNCOMPRESSED) |
294 | 283k | && (form != POINT_CONVERSION_HYBRID)) { |
295 | 4.48k | ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
296 | 4.48k | return 0; |
297 | 4.48k | } |
298 | 279k | if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) { |
299 | 2.00k | ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
300 | 2.00k | return 0; |
301 | 2.00k | } |
302 | | |
303 | 277k | if (form == 0) { |
304 | 19.2k | if (len != 1) { |
305 | 3.10k | ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
306 | 3.10k | return 0; |
307 | 3.10k | } |
308 | | |
309 | 16.1k | return EC_POINT_set_to_infinity(group, point); |
310 | 19.2k | } |
311 | | |
312 | 257k | field_len = BN_num_bytes(group->field); |
313 | 257k | enc_len = |
314 | 257k | (form == |
315 | 257k | POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len; |
316 | | |
317 | 257k | if (len != enc_len) { |
318 | 5.63k | ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
319 | 5.63k | return 0; |
320 | 5.63k | } |
321 | | |
322 | 252k | if (ctx == NULL) { |
323 | 197k | ctx = new_ctx = BN_CTX_new_ex(group->libctx); |
324 | 197k | if (ctx == NULL) |
325 | 0 | return 0; |
326 | 197k | } |
327 | | |
328 | 252k | BN_CTX_start(ctx); |
329 | 252k | x = BN_CTX_get(ctx); |
330 | 252k | y = BN_CTX_get(ctx); |
331 | 252k | if (y == NULL) |
332 | 0 | goto err; |
333 | | |
334 | 252k | if (!BN_bin2bn(buf + 1, field_len, x)) |
335 | 0 | goto err; |
336 | 252k | if (BN_ucmp(x, group->field) >= 0) { |
337 | 1.19k | ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
338 | 1.19k | goto err; |
339 | 1.19k | } |
340 | | |
341 | 251k | if (form == POINT_CONVERSION_COMPRESSED) { |
342 | 116k | if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx)) |
343 | 32.4k | goto err; |
344 | 134k | } else { |
345 | 134k | if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) |
346 | 0 | goto err; |
347 | 134k | if (BN_ucmp(y, group->field) >= 0) { |
348 | 621 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
349 | 621 | goto err; |
350 | 621 | } |
351 | 133k | if (form == POINT_CONVERSION_HYBRID) { |
352 | 5.65k | if (y_bit != BN_is_odd(y)) { |
353 | 1.09k | ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
354 | 1.09k | goto err; |
355 | 1.09k | } |
356 | 5.65k | } |
357 | | |
358 | | /* |
359 | | * EC_POINT_set_affine_coordinates is responsible for checking that |
360 | | * the point is on the curve. |
361 | | */ |
362 | 132k | if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx)) |
363 | 13.8k | goto err; |
364 | 132k | } |
365 | | |
366 | 203k | ret = 1; |
367 | | |
368 | 252k | err: |
369 | 252k | BN_CTX_end(ctx); |
370 | 252k | BN_CTX_free(new_ctx); |
371 | 252k | return ret; |
372 | 203k | } |