/src/openssl/crypto/ec/ec2_oct.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
4 | | * |
5 | | * Licensed under the OpenSSL license (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_lcl.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 | 0 | { |
38 | 0 | BN_CTX *new_ctx = NULL; |
39 | 0 | BIGNUM *tmp, *x, *y, *z; |
40 | 0 | int ret = 0, z0; |
41 | 0 |
|
42 | 0 | /* clear error queue */ |
43 | 0 | ERR_clear_error(); |
44 | 0 |
|
45 | 0 | if (ctx == NULL) { |
46 | 0 | ctx = new_ctx = BN_CTX_new(); |
47 | 0 | if (ctx == NULL) |
48 | 0 | return 0; |
49 | 0 | } |
50 | 0 | |
51 | 0 | y_bit = (y_bit != 0) ? 1 : 0; |
52 | 0 |
|
53 | 0 | BN_CTX_start(ctx); |
54 | 0 | tmp = BN_CTX_get(ctx); |
55 | 0 | x = BN_CTX_get(ctx); |
56 | 0 | y = BN_CTX_get(ctx); |
57 | 0 | z = BN_CTX_get(ctx); |
58 | 0 | if (z == NULL) |
59 | 0 | goto err; |
60 | 0 | |
61 | 0 | if (!BN_GF2m_mod_arr(x, x_, group->poly)) |
62 | 0 | goto err; |
63 | 0 | if (BN_is_zero(x)) { |
64 | 0 | if (!BN_GF2m_mod_sqrt_arr(y, group->b, group->poly, ctx)) |
65 | 0 | goto err; |
66 | 0 | } else { |
67 | 0 | if (!group->meth->field_sqr(group, tmp, x, ctx)) |
68 | 0 | goto err; |
69 | 0 | if (!group->meth->field_div(group, tmp, group->b, tmp, ctx)) |
70 | 0 | goto err; |
71 | 0 | if (!BN_GF2m_add(tmp, group->a, tmp)) |
72 | 0 | goto err; |
73 | 0 | if (!BN_GF2m_add(tmp, x, tmp)) |
74 | 0 | goto err; |
75 | 0 | if (!BN_GF2m_mod_solve_quad_arr(z, tmp, group->poly, ctx)) { |
76 | 0 | unsigned long err = ERR_peek_last_error(); |
77 | 0 |
|
78 | 0 | if (ERR_GET_LIB(err) == ERR_LIB_BN |
79 | 0 | && ERR_GET_REASON(err) == BN_R_NO_SOLUTION) { |
80 | 0 | ERR_clear_error(); |
81 | 0 | ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES, |
82 | 0 | EC_R_INVALID_COMPRESSED_POINT); |
83 | 0 | } else |
84 | 0 | ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES, |
85 | 0 | ERR_R_BN_LIB); |
86 | 0 | goto err; |
87 | 0 | } |
88 | 0 | z0 = (BN_is_odd(z)) ? 1 : 0; |
89 | 0 | if (!group->meth->field_mul(group, y, x, z, ctx)) |
90 | 0 | goto err; |
91 | 0 | if (z0 != y_bit) { |
92 | 0 | if (!BN_GF2m_add(y, y, x)) |
93 | 0 | goto err; |
94 | 0 | } |
95 | 0 | } |
96 | 0 | |
97 | 0 | if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx)) |
98 | 0 | goto err; |
99 | 0 | |
100 | 0 | ret = 1; |
101 | 0 |
|
102 | 0 | err: |
103 | 0 | BN_CTX_end(ctx); |
104 | 0 | BN_CTX_free(new_ctx); |
105 | 0 | return ret; |
106 | 0 | } |
107 | | |
108 | | /* |
109 | | * Converts an EC_POINT to an octet string. If buf is NULL, the encoded |
110 | | * length will be returned. If the length len of buf is smaller than required |
111 | | * an error will be returned. |
112 | | */ |
113 | | size_t ec_GF2m_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, |
114 | | point_conversion_form_t form, |
115 | | unsigned char *buf, size_t len, BN_CTX *ctx) |
116 | 0 | { |
117 | 0 | size_t ret; |
118 | 0 | BN_CTX *new_ctx = NULL; |
119 | 0 | int used_ctx = 0; |
120 | 0 | BIGNUM *x, *y, *yxi; |
121 | 0 | size_t field_len, i, skip; |
122 | 0 |
|
123 | 0 | if ((form != POINT_CONVERSION_COMPRESSED) |
124 | 0 | && (form != POINT_CONVERSION_UNCOMPRESSED) |
125 | 0 | && (form != POINT_CONVERSION_HYBRID)) { |
126 | 0 | ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_INVALID_FORM); |
127 | 0 | goto err; |
128 | 0 | } |
129 | 0 |
|
130 | 0 | if (EC_POINT_is_at_infinity(group, point)) { |
131 | 0 | /* encodes to a single 0 octet */ |
132 | 0 | if (buf != NULL) { |
133 | 0 | if (len < 1) { |
134 | 0 | ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL); |
135 | 0 | return 0; |
136 | 0 | } |
137 | 0 | buf[0] = 0; |
138 | 0 | } |
139 | 0 | return 1; |
140 | 0 | } |
141 | 0 | |
142 | 0 | /* ret := required output buffer length */ |
143 | 0 | field_len = (EC_GROUP_get_degree(group) + 7) / 8; |
144 | 0 | ret = |
145 | 0 | (form == |
146 | 0 | POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len; |
147 | 0 |
|
148 | 0 | /* if 'buf' is NULL, just return required length */ |
149 | 0 | if (buf != NULL) { |
150 | 0 | if (len < ret) { |
151 | 0 | ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL); |
152 | 0 | goto err; |
153 | 0 | } |
154 | 0 |
|
155 | 0 | if (ctx == NULL) { |
156 | 0 | ctx = new_ctx = BN_CTX_new(); |
157 | 0 | if (ctx == NULL) |
158 | 0 | return 0; |
159 | 0 | } |
160 | 0 | |
161 | 0 | BN_CTX_start(ctx); |
162 | 0 | used_ctx = 1; |
163 | 0 | x = BN_CTX_get(ctx); |
164 | 0 | y = BN_CTX_get(ctx); |
165 | 0 | yxi = BN_CTX_get(ctx); |
166 | 0 | if (yxi == NULL) |
167 | 0 | goto err; |
168 | 0 | |
169 | 0 | if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx)) |
170 | 0 | goto err; |
171 | 0 | |
172 | 0 | buf[0] = form; |
173 | 0 | if ((form != POINT_CONVERSION_UNCOMPRESSED) && !BN_is_zero(x)) { |
174 | 0 | if (!group->meth->field_div(group, yxi, y, x, ctx)) |
175 | 0 | goto err; |
176 | 0 | if (BN_is_odd(yxi)) |
177 | 0 | buf[0]++; |
178 | 0 | } |
179 | 0 |
|
180 | 0 | i = 1; |
181 | 0 |
|
182 | 0 | skip = field_len - BN_num_bytes(x); |
183 | 0 | if (skip > field_len) { |
184 | 0 | ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR); |
185 | 0 | goto err; |
186 | 0 | } |
187 | 0 | while (skip > 0) { |
188 | 0 | buf[i++] = 0; |
189 | 0 | skip--; |
190 | 0 | } |
191 | 0 | skip = BN_bn2bin(x, buf + i); |
192 | 0 | i += skip; |
193 | 0 | if (i != 1 + field_len) { |
194 | 0 | ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR); |
195 | 0 | goto err; |
196 | 0 | } |
197 | 0 |
|
198 | 0 | if (form == POINT_CONVERSION_UNCOMPRESSED |
199 | 0 | || form == POINT_CONVERSION_HYBRID) { |
200 | 0 | skip = field_len - BN_num_bytes(y); |
201 | 0 | if (skip > field_len) { |
202 | 0 | ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR); |
203 | 0 | goto err; |
204 | 0 | } |
205 | 0 | while (skip > 0) { |
206 | 0 | buf[i++] = 0; |
207 | 0 | skip--; |
208 | 0 | } |
209 | 0 | skip = BN_bn2bin(y, buf + i); |
210 | 0 | i += skip; |
211 | 0 | } |
212 | 0 |
|
213 | 0 | if (i != ret) { |
214 | 0 | ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR); |
215 | 0 | goto err; |
216 | 0 | } |
217 | 0 | } |
218 | 0 |
|
219 | 0 | if (used_ctx) |
220 | 0 | BN_CTX_end(ctx); |
221 | 0 | BN_CTX_free(new_ctx); |
222 | 0 | return ret; |
223 | 0 |
|
224 | 0 | err: |
225 | 0 | if (used_ctx) |
226 | 0 | BN_CTX_end(ctx); |
227 | 0 | BN_CTX_free(new_ctx); |
228 | 0 | return 0; |
229 | 0 | } |
230 | | |
231 | | /* |
232 | | * Converts an octet string representation to an EC_POINT. Note that the |
233 | | * simple implementation only uses affine coordinates. |
234 | | */ |
235 | | int ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point, |
236 | | const unsigned char *buf, size_t len, |
237 | | BN_CTX *ctx) |
238 | 0 | { |
239 | 0 | point_conversion_form_t form; |
240 | 0 | int y_bit; |
241 | 0 | BN_CTX *new_ctx = NULL; |
242 | 0 | BIGNUM *x, *y, *yxi; |
243 | 0 | size_t field_len, enc_len; |
244 | 0 | int ret = 0; |
245 | 0 |
|
246 | 0 | if (len == 0) { |
247 | 0 | ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL); |
248 | 0 | return 0; |
249 | 0 | } |
250 | 0 | form = buf[0]; |
251 | 0 | y_bit = form & 1; |
252 | 0 | form = form & ~1U; |
253 | 0 | if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED) |
254 | 0 | && (form != POINT_CONVERSION_UNCOMPRESSED) |
255 | 0 | && (form != POINT_CONVERSION_HYBRID)) { |
256 | 0 | ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING); |
257 | 0 | return 0; |
258 | 0 | } |
259 | 0 | if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) { |
260 | 0 | ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING); |
261 | 0 | return 0; |
262 | 0 | } |
263 | 0 |
|
264 | 0 | if (form == 0) { |
265 | 0 | if (len != 1) { |
266 | 0 | ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING); |
267 | 0 | return 0; |
268 | 0 | } |
269 | 0 |
|
270 | 0 | return EC_POINT_set_to_infinity(group, point); |
271 | 0 | } |
272 | 0 | |
273 | 0 | field_len = (EC_GROUP_get_degree(group) + 7) / 8; |
274 | 0 | enc_len = |
275 | 0 | (form == |
276 | 0 | POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len; |
277 | 0 |
|
278 | 0 | if (len != enc_len) { |
279 | 0 | ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING); |
280 | 0 | return 0; |
281 | 0 | } |
282 | 0 |
|
283 | 0 | if (ctx == NULL) { |
284 | 0 | ctx = new_ctx = BN_CTX_new(); |
285 | 0 | if (ctx == NULL) |
286 | 0 | return 0; |
287 | 0 | } |
288 | 0 | |
289 | 0 | BN_CTX_start(ctx); |
290 | 0 | x = BN_CTX_get(ctx); |
291 | 0 | y = BN_CTX_get(ctx); |
292 | 0 | yxi = BN_CTX_get(ctx); |
293 | 0 | if (yxi == NULL) |
294 | 0 | goto err; |
295 | 0 | |
296 | 0 | if (!BN_bin2bn(buf + 1, field_len, x)) |
297 | 0 | goto err; |
298 | 0 | if (BN_ucmp(x, group->field) >= 0) { |
299 | 0 | ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING); |
300 | 0 | goto err; |
301 | 0 | } |
302 | 0 |
|
303 | 0 | if (form == POINT_CONVERSION_COMPRESSED) { |
304 | 0 | if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx)) |
305 | 0 | goto err; |
306 | 0 | } else { |
307 | 0 | if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) |
308 | 0 | goto err; |
309 | 0 | if (BN_ucmp(y, group->field) >= 0) { |
310 | 0 | ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING); |
311 | 0 | goto err; |
312 | 0 | } |
313 | 0 | if (form == POINT_CONVERSION_HYBRID) { |
314 | 0 | if (!group->meth->field_div(group, yxi, y, x, ctx)) |
315 | 0 | goto err; |
316 | 0 | if (y_bit != BN_is_odd(yxi)) { |
317 | 0 | ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING); |
318 | 0 | goto err; |
319 | 0 | } |
320 | 0 | } |
321 | 0 |
|
322 | 0 | /* |
323 | 0 | * EC_POINT_set_affine_coordinates is responsible for checking that |
324 | 0 | * the point is on the curve. |
325 | 0 | */ |
326 | 0 | if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx)) |
327 | 0 | goto err; |
328 | 0 | } |
329 | 0 | |
330 | 0 | ret = 1; |
331 | 0 |
|
332 | 0 | err: |
333 | 0 | BN_CTX_end(ctx); |
334 | 0 | BN_CTX_free(new_ctx); |
335 | 0 | return ret; |
336 | 0 | } |
337 | | #endif |