1 | /* |
2 | * Copyright 2011-2018 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 | #include <openssl/err.h> |
12 | |
13 | #include "ec_local.h" |
14 | |
15 | #ifndef OPENSSL_NO_EC2M |
16 | |
17 | /*- |
18 | * Calculates and sets the affine coordinates of an EC_POINT from the given |
19 | * compressed coordinates. Uses algorithm 2.3.4 of SEC 1. |
20 | * Note that the simple implementation only uses affine coordinates. |
21 | * |
22 | * The method is from the following publication: |
23 | * |
24 | * Harper, Menezes, Vanstone: |
25 | * "Public-Key Cryptosystems with Very Small Key Lengths", |
26 | * EUROCRYPT '92, Springer-Verlag LNCS 658, |
27 | * published February 1993 |
28 | * |
29 | * US Patents 6,141,420 and 6,618,483 (Vanstone, Mullin, Agnew) describe |
30 | * the same method, but claim no priority date earlier than July 29, 1994 |
31 | * (and additionally fail to cite the EUROCRYPT '92 publication as prior art). |
32 | */ |
33 | int ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *group, |
34 | EC_POINT *point, |
35 | const BIGNUM *x_, int y_bit, |
36 | BN_CTX *ctx) |
37 | { |
38 | BIGNUM *tmp, *x, *y, *z; |
39 | int ret = 0, z0; |
40 | #ifndef FIPS_MODE |
41 | BN_CTX *new_ctx = NULL; |
42 | |
43 | /* clear error queue */ |
44 | ERR_clear_error(); |
45 | |
46 | if (ctx == NULL) { |
47 | ctx = new_ctx = BN_CTX_new(); |
48 | if (ctx == NULL) |
49 | return 0; |
50 | } |
51 | #endif |
52 | |
53 | y_bit = (y_bit != 0) ? 1 : 0; |
54 | |
55 | BN_CTX_start(ctx); |
56 | tmp = BN_CTX_get(ctx); |
57 | x = BN_CTX_get(ctx); |
58 | y = BN_CTX_get(ctx); |
59 | z = BN_CTX_get(ctx); |
60 | if (z == NULL) |
61 | goto err; |
62 | |
63 | if (!BN_GF2m_mod_arr(x, x_, group->poly)) |
64 | goto err; |
65 | if (BN_is_zero(x)) { |
66 | if (!BN_GF2m_mod_sqrt_arr(y, group->b, group->poly, ctx)) |
67 | goto err; |
68 | } else { |
69 | if (!group->meth->field_sqr(group, tmp, x, ctx)) |
70 | goto err; |
71 | if (!group->meth->field_div(group, tmp, group->b, tmp, ctx)) |
72 | goto err; |
73 | if (!BN_GF2m_add(tmp, group->a, tmp)) |
74 | goto err; |
75 | if (!BN_GF2m_add(tmp, x, tmp)) |
76 | goto err; |
77 | if (!BN_GF2m_mod_solve_quad_arr(z, tmp, group->poly, ctx)) { |
78 | #ifndef FIPS_MODE |
79 | unsigned long err = ERR_peek_last_error(); |
80 | |
81 | if (ERR_GET_LIB(err) == ERR_LIB_BN |
82 | && ERR_GET_REASON(err) == BN_R_NO_SOLUTION) { |
83 | ERR_clear_error(); |
84 | ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES, |
85 | EC_R_INVALID_COMPRESSED_POINT); |
86 | } else |
87 | #endif |
88 | { |
89 | ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES, |
90 | ERR_R_BN_LIB); |
91 | } |
92 | goto err; |
93 | } |
94 | z0 = (BN_is_odd(z)) ? 1 : 0; |
95 | if (!group->meth->field_mul(group, y, x, z, ctx)) |
96 | goto err; |
97 | if (z0 != y_bit) { |
98 | if (!BN_GF2m_add(y, y, x)) |
99 | goto err; |
100 | } |
101 | } |
102 | |
103 | if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx)) |
104 | goto err; |
105 | |
106 | ret = 1; |
107 | |
108 | err: |
109 | BN_CTX_end(ctx); |
110 | #ifndef FIPS_MODE |
111 | BN_CTX_free(new_ctx); |
112 | #endif |
113 | return ret; |
114 | } |
115 | |
116 | /* |
117 | * Converts an EC_POINT to an octet string. If buf is NULL, the encoded |
118 | * length will be returned. If the length len of buf is smaller than required |
119 | * an error will be returned. |
120 | */ |
121 | size_t ec_GF2m_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, |
122 | point_conversion_form_t form, |
123 | unsigned char *buf, size_t len, BN_CTX *ctx) |
124 | { |
125 | size_t ret; |
126 | int used_ctx = 0; |
127 | BIGNUM *x, *y, *yxi; |
128 | size_t field_len, i, skip; |
129 | #ifndef FIPS_MODE |
130 | BN_CTX *new_ctx = NULL; |
131 | #endif |
132 | |
133 | if ((form != POINT_CONVERSION_COMPRESSED) |
134 | && (form != POINT_CONVERSION_UNCOMPRESSED) |
135 | && (form != POINT_CONVERSION_HYBRID)) { |
136 | ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_INVALID_FORM); |
137 | goto err; |
138 | } |
139 | |
140 | if (EC_POINT_is_at_infinity(group, point)) { |
141 | /* encodes to a single 0 octet */ |
142 | if (buf != NULL) { |
143 | if (len < 1) { |
144 | ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL); |
145 | return 0; |
146 | } |
147 | buf[0] = 0; |
148 | } |
149 | return 1; |
150 | } |
151 | |
152 | /* ret := required output buffer length */ |
153 | field_len = (EC_GROUP_get_degree(group) + 7) / 8; |
154 | ret = |
155 | (form == |
156 | POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len; |
157 | |
158 | /* if 'buf' is NULL, just return required length */ |
159 | if (buf != NULL) { |
160 | if (len < ret) { |
161 | ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL); |
162 | goto err; |
163 | } |
164 | |
165 | #ifndef FIPS_MODE |
166 | if (ctx == NULL) { |
167 | ctx = new_ctx = BN_CTX_new(); |
168 | if (ctx == NULL) |
169 | return 0; |
170 | } |
171 | #endif |
172 | |
173 | BN_CTX_start(ctx); |
174 | used_ctx = 1; |
175 | x = BN_CTX_get(ctx); |
176 | y = BN_CTX_get(ctx); |
177 | yxi = BN_CTX_get(ctx); |
178 | if (yxi == NULL) |
179 | goto err; |
180 | |
181 | if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx)) |
182 | goto err; |
183 | |
184 | buf[0] = form; |
185 | if ((form != POINT_CONVERSION_UNCOMPRESSED) && !BN_is_zero(x)) { |
186 | if (!group->meth->field_div(group, yxi, y, x, ctx)) |
187 | goto err; |
188 | if (BN_is_odd(yxi)) |
189 | buf[0]++; |
190 | } |
191 | |
192 | i = 1; |
193 | |
194 | skip = field_len - BN_num_bytes(x); |
195 | if (skip > field_len) { |
196 | ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR); |
197 | goto err; |
198 | } |
199 | while (skip > 0) { |
200 | buf[i++] = 0; |
201 | skip--; |
202 | } |
203 | skip = BN_bn2bin(x, buf + i); |
204 | i += skip; |
205 | if (i != 1 + field_len) { |
206 | ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR); |
207 | goto err; |
208 | } |
209 | |
210 | if (form == POINT_CONVERSION_UNCOMPRESSED |
211 | || form == POINT_CONVERSION_HYBRID) { |
212 | skip = field_len - BN_num_bytes(y); |
213 | if (skip > field_len) { |
214 | ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR); |
215 | goto err; |
216 | } |
217 | while (skip > 0) { |
218 | buf[i++] = 0; |
219 | skip--; |
220 | } |
221 | skip = BN_bn2bin(y, buf + i); |
222 | i += skip; |
223 | } |
224 | |
225 | if (i != ret) { |
226 | ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR); |
227 | goto err; |
228 | } |
229 | } |
230 | |
231 | if (used_ctx) |
232 | BN_CTX_end(ctx); |
233 | #ifndef FIPS_MODE |
234 | BN_CTX_free(new_ctx); |
235 | #endif |
236 | return ret; |
237 | |
238 | err: |
239 | if (used_ctx) |
240 | BN_CTX_end(ctx); |
241 | #ifndef FIPS_MODE |
242 | BN_CTX_free(new_ctx); |
243 | #endif |
244 | return 0; |
245 | } |
246 | |
247 | /* |
248 | * Converts an octet string representation to an EC_POINT. Note that the |
249 | * simple implementation only uses affine coordinates. |
250 | */ |
251 | int ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point, |
252 | const unsigned char *buf, size_t len, |
253 | BN_CTX *ctx) |
254 | { |
255 | point_conversion_form_t form; |
256 | int y_bit, m; |
257 | BIGNUM *x, *y, *yxi; |
258 | size_t field_len, enc_len; |
259 | int ret = 0; |
260 | #ifndef FIPS_MODE |
261 | BN_CTX *new_ctx = NULL; |
262 | #endif |
263 | |
264 | if (len == 0) { |
265 | ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL); |
266 | return 0; |
267 | } |
268 | form = buf[0]; |
269 | y_bit = form & 1; |
270 | form = form & ~1U; |
271 | if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED) |
272 | && (form != POINT_CONVERSION_UNCOMPRESSED) |
273 | && (form != POINT_CONVERSION_HYBRID)) { |
274 | ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING); |
275 | return 0; |
276 | } |
277 | if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) { |
278 | ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING); |
279 | return 0; |
280 | } |
281 | |
282 | if (form == 0) { |
283 | if (len != 1) { |
284 | ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING); |
285 | return 0; |
286 | } |
287 | |
288 | return EC_POINT_set_to_infinity(group, point); |
289 | } |
290 | |
291 | m = EC_GROUP_get_degree(group); |
292 | field_len = (m + 7) / 8; |
293 | enc_len = |
294 | (form == |
295 | POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len; |
296 | |
297 | if (len != enc_len) { |
298 | ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING); |
299 | return 0; |
300 | } |
301 | |
302 | #ifndef FIPS_MODE |
303 | if (ctx == NULL) { |
304 | ctx = new_ctx = BN_CTX_new(); |
305 | if (ctx == NULL) |
306 | return 0; |
307 | } |
308 | #endif |
309 | |
310 | BN_CTX_start(ctx); |
311 | x = BN_CTX_get(ctx); |
312 | y = BN_CTX_get(ctx); |
313 | yxi = BN_CTX_get(ctx); |
314 | if (yxi == NULL) |
315 | goto err; |
316 | |
317 | if (!BN_bin2bn(buf + 1, field_len, x)) |
318 | goto err; |
319 | if (BN_num_bits(x) > m) { |
320 | ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING); |
321 | goto err; |
322 | } |
323 | |
324 | if (form == POINT_CONVERSION_COMPRESSED) { |
325 | if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx)) |
326 | goto err; |
327 | } else { |
328 | if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) |
329 | goto err; |
330 | if (BN_num_bits(y) > m) { |
331 | ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING); |
332 | goto err; |
333 | } |
334 | if (form == POINT_CONVERSION_HYBRID) { |
335 | if (!group->meth->field_div(group, yxi, y, x, ctx)) |
336 | goto err; |
337 | if (y_bit != BN_is_odd(yxi)) { |
338 | ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING); |
339 | goto err; |
340 | } |
341 | } |
342 | |
343 | /* |
344 | * EC_POINT_set_affine_coordinates is responsible for checking that |
345 | * the point is on the curve. |
346 | */ |
347 | if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx)) |
348 | goto err; |
349 | } |
350 | |
351 | ret = 1; |
352 | |
353 | err: |
354 | BN_CTX_end(ctx); |
355 | #ifndef FIPS_MODE |
356 | BN_CTX_free(new_ctx); |
357 | #endif |
358 | return ret; |
359 | } |
360 | #endif |
361 | |