summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ec/ec2_oct.c
diff options
context:
space:
mode:
authortb <>2023-04-25 19:53:30 +0000
committertb <>2023-04-25 19:53:30 +0000
commit82b040aef9cef17610a89204220ee3cb1012fb20 (patch)
treee9be44ea96c7294efcc800d9cb419edbab4fe999 /src/lib/libcrypto/ec/ec2_oct.c
parentaa0643f4294a31c69cf4097f866cd5cb11e48c1e (diff)
downloadopenbsd-82b040aef9cef17610a89204220ee3cb1012fb20.tar.gz
openbsd-82b040aef9cef17610a89204220ee3cb1012fb20.tar.bz2
openbsd-82b040aef9cef17610a89204220ee3cb1012fb20.zip
GF2m bites the dust. It won't be missed.
Diffstat (limited to 'src/lib/libcrypto/ec/ec2_oct.c')
-rw-r--r--src/lib/libcrypto/ec/ec2_oct.c402
1 files changed, 0 insertions, 402 deletions
diff --git a/src/lib/libcrypto/ec/ec2_oct.c b/src/lib/libcrypto/ec/ec2_oct.c
deleted file mode 100644
index 6cb7259824..0000000000
--- a/src/lib/libcrypto/ec/ec2_oct.c
+++ /dev/null
@@ -1,402 +0,0 @@
1/* $OpenBSD: ec2_oct.c,v 1.20 2023/04/11 18:58:20 jsing Exp $ */
2/* ====================================================================
3 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4 *
5 * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
6 * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
7 * to the OpenSSL project.
8 *
9 * The ECC Code is licensed pursuant to the OpenSSL open source
10 * license provided below.
11 *
12 * The software is originally written by Sheueling Chang Shantz and
13 * Douglas Stebila of Sun Microsystems Laboratories.
14 *
15 */
16/* ====================================================================
17 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 *
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 *
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
29 * distribution.
30 *
31 * 3. All advertising materials mentioning features or use of this
32 * software must display the following acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
35 *
36 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
37 * endorse or promote products derived from this software without
38 * prior written permission. For written permission, please contact
39 * openssl-core@openssl.org.
40 *
41 * 5. Products derived from this software may not be called "OpenSSL"
42 * nor may "OpenSSL" appear in their names without prior written
43 * permission of the OpenSSL Project.
44 *
45 * 6. Redistributions of any form whatsoever must retain the following
46 * acknowledgment:
47 * "This product includes software developed by the OpenSSL Project
48 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
51 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
54 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
61 * OF THE POSSIBILITY OF SUCH DAMAGE.
62 * ====================================================================
63 *
64 * This product includes cryptographic software written by Eric Young
65 * (eay@cryptsoft.com). This product includes software written by Tim
66 * Hudson (tjh@cryptsoft.com).
67 *
68 */
69
70#include <openssl/opensslconf.h>
71
72#include <openssl/err.h>
73
74#include "ec_local.h"
75
76#ifndef OPENSSL_NO_EC2M
77
78/* Calculates and sets the affine coordinates of an EC_POINT from the given
79 * compressed coordinates. Uses algorithm 2.3.4 of SEC 1.
80 * Note that the simple implementation only uses affine coordinates.
81 *
82 * The method is from the following publication:
83 *
84 * Harper, Menezes, Vanstone:
85 * "Public-Key Cryptosystems with Very Small Key Lengths",
86 * EUROCRYPT '92, Springer-Verlag LNCS 658,
87 * published February 1993
88 *
89 * US Patents 6,141,420 and 6,618,483 (Vanstone, Mullin, Agnew) describe
90 * the same method, but claim no priority date earlier than July 29, 1994
91 * (and additionally fail to cite the EUROCRYPT '92 publication as prior art).
92 */
93int
94ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point,
95 const BIGNUM *x_, int y_bit, BN_CTX *ctx)
96{
97 BIGNUM *tmp, *x, *y, *z;
98 int z0;
99 int ret = 0;
100
101 /* clear error queue */
102 ERR_clear_error();
103
104 y_bit = (y_bit != 0) ? 1 : 0;
105
106 BN_CTX_start(ctx);
107
108 if ((tmp = BN_CTX_get(ctx)) == NULL)
109 goto err;
110 if ((x = BN_CTX_get(ctx)) == NULL)
111 goto err;
112 if ((y = BN_CTX_get(ctx)) == NULL)
113 goto err;
114 if ((z = BN_CTX_get(ctx)) == NULL)
115 goto err;
116
117 if (!BN_GF2m_mod_arr(x, x_, group->poly))
118 goto err;
119 if (BN_is_zero(x)) {
120 if (y_bit != 0) {
121 ECerror(EC_R_INVALID_COMPRESSED_POINT);
122 goto err;
123 }
124 if (!BN_GF2m_mod_sqrt_arr(y, &group->b, group->poly, ctx))
125 goto err;
126 } else {
127 if (!group->meth->field_sqr(group, tmp, x, ctx))
128 goto err;
129 if (!group->meth->field_div(group, tmp, &group->b, tmp, ctx))
130 goto err;
131 if (!BN_GF2m_add(tmp, &group->a, tmp))
132 goto err;
133 if (!BN_GF2m_add(tmp, x, tmp))
134 goto err;
135 if (!BN_GF2m_mod_solve_quad_arr(z, tmp, group->poly, ctx)) {
136 unsigned long err = ERR_peek_last_error();
137
138 if (ERR_GET_LIB(err) == ERR_LIB_BN &&
139 ERR_GET_REASON(err) == BN_R_NO_SOLUTION) {
140 ERR_clear_error();
141 ECerror(EC_R_INVALID_COMPRESSED_POINT);
142 } else
143 ECerror(ERR_R_BN_LIB);
144 goto err;
145 }
146 z0 = (BN_is_odd(z)) ? 1 : 0;
147 if (!group->meth->field_mul(group, y, x, z, ctx))
148 goto err;
149 if (z0 != y_bit) {
150 if (!BN_GF2m_add(y, y, x))
151 goto err;
152 }
153 }
154
155 if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
156 goto err;
157
158 ret = 1;
159
160 err:
161 BN_CTX_end(ctx);
162
163 return ret;
164}
165
166
167/* Converts an EC_POINT to an octet string.
168 * If buf is NULL, the encoded length will be returned.
169 * If the length len of buf is smaller than required an error will be returned.
170 */
171size_t
172ec_GF2m_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
173 point_conversion_form_t form,
174 unsigned char *buf, size_t len, BN_CTX *ctx)
175{
176 BIGNUM *x, *y, *yxi;
177 size_t field_len, i, skip;
178 size_t ret;
179
180 if (form != POINT_CONVERSION_COMPRESSED &&
181 form != POINT_CONVERSION_UNCOMPRESSED &&
182 form != POINT_CONVERSION_HYBRID) {
183 ECerror(EC_R_INVALID_FORM);
184 return 0;
185 }
186
187 if (EC_POINT_is_at_infinity(group, point) > 0) {
188 /* encodes to a single 0 octet */
189 if (buf != NULL) {
190 if (len < 1) {
191 ECerror(EC_R_BUFFER_TOO_SMALL);
192 return 0;
193 }
194 buf[0] = 0;
195 }
196 return 1;
197 }
198
199 BN_CTX_start(ctx);
200
201 /* ret := required output buffer length */
202 field_len = (EC_GROUP_get_degree(group) + 7) / 8;
203 ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len :
204 1 + 2 * field_len;
205
206 /* if 'buf' is NULL, just return required length */
207 if (buf != NULL) {
208 if (len < ret) {
209 ECerror(EC_R_BUFFER_TOO_SMALL);
210 goto err;
211 }
212
213 if ((x = BN_CTX_get(ctx)) == NULL)
214 goto err;
215 if ((y = BN_CTX_get(ctx)) == NULL)
216 goto err;
217 if ((yxi = BN_CTX_get(ctx)) == NULL)
218 goto err;
219
220 if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))
221 goto err;
222
223 buf[0] = form;
224 if ((form != POINT_CONVERSION_UNCOMPRESSED) && !BN_is_zero(x)) {
225 if (!group->meth->field_div(group, yxi, y, x, ctx))
226 goto err;
227 if (BN_is_odd(yxi))
228 buf[0]++;
229 }
230 i = 1;
231
232 skip = field_len - BN_num_bytes(x);
233 if (skip > field_len) {
234 ECerror(ERR_R_INTERNAL_ERROR);
235 goto err;
236 }
237 while (skip > 0) {
238 buf[i++] = 0;
239 skip--;
240 }
241 skip = BN_bn2bin(x, buf + i);
242 i += skip;
243 if (i != 1 + field_len) {
244 ECerror(ERR_R_INTERNAL_ERROR);
245 goto err;
246 }
247 if (form == POINT_CONVERSION_UNCOMPRESSED ||
248 form == POINT_CONVERSION_HYBRID) {
249 skip = field_len - BN_num_bytes(y);
250 if (skip > field_len) {
251 ECerror(ERR_R_INTERNAL_ERROR);
252 goto err;
253 }
254 while (skip > 0) {
255 buf[i++] = 0;
256 skip--;
257 }
258 skip = BN_bn2bin(y, buf + i);
259 i += skip;
260 }
261 if (i != ret) {
262 ECerror(ERR_R_INTERNAL_ERROR);
263 goto err;
264 }
265 }
266
267 err:
268 BN_CTX_end(ctx);
269
270 return ret;
271}
272
273/*
274 * Converts an octet string representation to an EC_POINT.
275 * Note that the simple implementation only uses affine coordinates.
276 */
277int
278ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
279 const unsigned char *buf, size_t len, BN_CTX *ctx)
280{
281 point_conversion_form_t form;
282 int y_bit;
283 BIGNUM *x, *y, *yxi;
284 size_t field_len, enc_len;
285 int ret = 0;
286
287 if (len == 0) {
288 ECerror(EC_R_BUFFER_TOO_SMALL);
289 return 0;
290 }
291
292 /*
293 * The first octet is the point conversion octet PC, see X9.62, page 4
294 * and section 4.4.2. It must be:
295 * 0x00 for the point at infinity
296 * 0x02 or 0x03 for compressed form
297 * 0x04 for uncompressed form
298 * 0x06 or 0x07 for hybrid form.
299 * For compressed or hybrid forms, we store the last bit of buf[0] as
300 * y_bit and clear it from buf[0] so as to obtain a POINT_CONVERSION_*.
301 * We error if buf[0] contains any but the above values.
302 */
303 y_bit = buf[0] & 1;
304 form = buf[0] & ~1U;
305
306 if (form != 0 && form != POINT_CONVERSION_COMPRESSED &&
307 form != POINT_CONVERSION_UNCOMPRESSED &&
308 form != POINT_CONVERSION_HYBRID) {
309 ECerror(EC_R_INVALID_ENCODING);
310 return 0;
311 }
312 if (form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) {
313 if (y_bit != 0) {
314 ECerror(EC_R_INVALID_ENCODING);
315 return 0;
316 }
317 }
318
319 /* The point at infinity is represented by a single zero octet. */
320 if (form == 0) {
321 if (len != 1) {
322 ECerror(EC_R_INVALID_ENCODING);
323 return 0;
324 }
325 return EC_POINT_set_to_infinity(group, point);
326 }
327
328 field_len = (EC_GROUP_get_degree(group) + 7) / 8;
329 enc_len = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len :
330 1 + 2 * field_len;
331
332 if (len != enc_len) {
333 ECerror(EC_R_INVALID_ENCODING);
334 return 0;
335 }
336
337 BN_CTX_start(ctx);
338
339 if ((x = BN_CTX_get(ctx)) == NULL)
340 goto err;
341 if ((y = BN_CTX_get(ctx)) == NULL)
342 goto err;
343 if ((yxi = BN_CTX_get(ctx)) == NULL)
344 goto err;
345
346 if (!BN_bin2bn(buf + 1, field_len, x))
347 goto err;
348 if (BN_ucmp(x, &group->field) >= 0) {
349 ECerror(EC_R_INVALID_ENCODING);
350 goto err;
351 }
352 if (form == POINT_CONVERSION_COMPRESSED) {
353 /*
354 * EC_POINT_set_compressed_coordinates checks that the
355 * point is on the curve as required by X9.62.
356 */
357 if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx))
358 goto err;
359 } else {
360 if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
361 goto err;
362 if (BN_ucmp(y, &group->field) >= 0) {
363 ECerror(EC_R_INVALID_ENCODING);
364 goto err;
365 }
366 if (form == POINT_CONVERSION_HYBRID) {
367 /*
368 * Check that the form in the encoding was set
369 * correctly according to X9.62 4.4.2.a, 4(c),
370 * see also first paragraph of X9.62 4.4.1.b.
371 */
372 if (BN_is_zero(x)) {
373 if (y_bit != 0) {
374 ECerror(EC_R_INVALID_ENCODING);
375 goto err;
376 }
377 } else {
378 if (!group->meth->field_div(group, yxi, y, x,
379 ctx))
380 goto err;
381 if (y_bit != BN_is_odd(yxi)) {
382 ECerror(EC_R_INVALID_ENCODING);
383 goto err;
384 }
385 }
386 }
387 /*
388 * EC_POINT_set_affine_coordinates checks that the
389 * point is on the curve as required by X9.62.
390 */
391 if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
392 goto err;
393 }
394
395 ret = 1;
396
397 err:
398 BN_CTX_end(ctx);
399
400 return ret;
401}
402#endif