summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ec/ecp_oct.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/ec/ecp_oct.c')
-rw-r--r--src/lib/libcrypto/ec/ecp_oct.c432
1 files changed, 432 insertions, 0 deletions
diff --git a/src/lib/libcrypto/ec/ecp_oct.c b/src/lib/libcrypto/ec/ecp_oct.c
new file mode 100644
index 0000000000..a06abbc8cd
--- /dev/null
+++ b/src/lib/libcrypto/ec/ecp_oct.c
@@ -0,0 +1,432 @@
1/* crypto/ec/ecp_oct.c */
2/* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
3 * for the OpenSSL project.
4 * Includes code written by Bodo Moeller for the OpenSSL project.
5*/
6/* ====================================================================
7 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25 *
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * openssl-core@openssl.org.
30 *
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 * acknowledgment:
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
57 *
58 */
59/* ====================================================================
60 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
61 * Portions of this software developed by SUN MICROSYSTEMS, INC.,
62 * and contributed to the OpenSSL project.
63 */
64
65#include <openssl/err.h>
66
67#include "ec_lcl.h"
68
69int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point,
70 const BIGNUM *x_, int y_bit, BN_CTX *ctx)
71 {
72 BN_CTX *new_ctx = NULL;
73 BIGNUM *tmp1, *tmp2, *x, *y;
74 int ret = 0;
75
76 /* clear error queue*/
77 ERR_clear_error();
78
79 if (ctx == NULL)
80 {
81 ctx = new_ctx = BN_CTX_new();
82 if (ctx == NULL)
83 return 0;
84 }
85
86 y_bit = (y_bit != 0);
87
88 BN_CTX_start(ctx);
89 tmp1 = BN_CTX_get(ctx);
90 tmp2 = BN_CTX_get(ctx);
91 x = BN_CTX_get(ctx);
92 y = BN_CTX_get(ctx);
93 if (y == NULL) goto err;
94
95 /* Recover y. We have a Weierstrass equation
96 * y^2 = x^3 + a*x + b,
97 * so y is one of the square roots of x^3 + a*x + b.
98 */
99
100 /* tmp1 := x^3 */
101 if (!BN_nnmod(x, x_, &group->field,ctx)) goto err;
102 if (group->meth->field_decode == 0)
103 {
104 /* field_{sqr,mul} work on standard representation */
105 if (!group->meth->field_sqr(group, tmp2, x_, ctx)) goto err;
106 if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx)) goto err;
107 }
108 else
109 {
110 if (!BN_mod_sqr(tmp2, x_, &group->field, ctx)) goto err;
111 if (!BN_mod_mul(tmp1, tmp2, x_, &group->field, ctx)) goto err;
112 }
113
114 /* tmp1 := tmp1 + a*x */
115 if (group->a_is_minus3)
116 {
117 if (!BN_mod_lshift1_quick(tmp2, x, &group->field)) goto err;
118 if (!BN_mod_add_quick(tmp2, tmp2, x, &group->field)) goto err;
119 if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
120 }
121 else
122 {
123 if (group->meth->field_decode)
124 {
125 if (!group->meth->field_decode(group, tmp2, &group->a, ctx)) goto err;
126 if (!BN_mod_mul(tmp2, tmp2, x, &group->field, ctx)) goto err;
127 }
128 else
129 {
130 /* field_mul works on standard representation */
131 if (!group->meth->field_mul(group, tmp2, &group->a, x, ctx)) goto err;
132 }
133
134 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
135 }
136
137 /* tmp1 := tmp1 + b */
138 if (group->meth->field_decode)
139 {
140 if (!group->meth->field_decode(group, tmp2, &group->b, ctx)) goto err;
141 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) goto err;
142 }
143 else
144 {
145 if (!BN_mod_add_quick(tmp1, tmp1, &group->b, &group->field)) goto err;
146 }
147
148 if (!BN_mod_sqrt(y, tmp1, &group->field, ctx))
149 {
150 unsigned long err = ERR_peek_last_error();
151
152 if (ERR_GET_LIB(err) == ERR_LIB_BN && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE)
153 {
154 ERR_clear_error();
155 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSED_POINT);
156 }
157 else
158 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, ERR_R_BN_LIB);
159 goto err;
160 }
161
162 if (y_bit != BN_is_odd(y))
163 {
164 if (BN_is_zero(y))
165 {
166 int kron;
167
168 kron = BN_kronecker(x, &group->field, ctx);
169 if (kron == -2) goto err;
170
171 if (kron == 1)
172 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSION_BIT);
173 else
174 /* BN_mod_sqrt() should have cought this error (not a square) */
175 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSED_POINT);
176 goto err;
177 }
178 if (!BN_usub(y, &group->field, y)) goto err;
179 }
180 if (y_bit != BN_is_odd(y))
181 {
182 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, ERR_R_INTERNAL_ERROR);
183 goto err;
184 }
185
186 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
187
188 ret = 1;
189
190 err:
191 BN_CTX_end(ctx);
192 if (new_ctx != NULL)
193 BN_CTX_free(new_ctx);
194 return ret;
195 }
196
197
198size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form,
199 unsigned char *buf, size_t len, BN_CTX *ctx)
200 {
201 size_t ret;
202 BN_CTX *new_ctx = NULL;
203 int used_ctx = 0;
204 BIGNUM *x, *y;
205 size_t field_len, i, skip;
206
207 if ((form != POINT_CONVERSION_COMPRESSED)
208 && (form != POINT_CONVERSION_UNCOMPRESSED)
209 && (form != POINT_CONVERSION_HYBRID))
210 {
211 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
212 goto err;
213 }
214
215 if (EC_POINT_is_at_infinity(group, point))
216 {
217 /* encodes to a single 0 octet */
218 if (buf != NULL)
219 {
220 if (len < 1)
221 {
222 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
223 return 0;
224 }
225 buf[0] = 0;
226 }
227 return 1;
228 }
229
230
231 /* ret := required output buffer length */
232 field_len = BN_num_bytes(&group->field);
233 ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
234
235 /* if 'buf' is NULL, just return required length */
236 if (buf != NULL)
237 {
238 if (len < ret)
239 {
240 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
241 goto err;
242 }
243
244 if (ctx == NULL)
245 {
246 ctx = new_ctx = BN_CTX_new();
247 if (ctx == NULL)
248 return 0;
249 }
250
251 BN_CTX_start(ctx);
252 used_ctx = 1;
253 x = BN_CTX_get(ctx);
254 y = BN_CTX_get(ctx);
255 if (y == NULL) goto err;
256
257 if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
258
259 if ((form == POINT_CONVERSION_COMPRESSED || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))
260 buf[0] = form + 1;
261 else
262 buf[0] = form;
263
264 i = 1;
265
266 skip = field_len - BN_num_bytes(x);
267 if (skip > field_len)
268 {
269 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
270 goto err;
271 }
272 while (skip > 0)
273 {
274 buf[i++] = 0;
275 skip--;
276 }
277 skip = BN_bn2bin(x, buf + i);
278 i += skip;
279 if (i != 1 + field_len)
280 {
281 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
282 goto err;
283 }
284
285 if (form == POINT_CONVERSION_UNCOMPRESSED || form == POINT_CONVERSION_HYBRID)
286 {
287 skip = field_len - BN_num_bytes(y);
288 if (skip > field_len)
289 {
290 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
291 goto err;
292 }
293 while (skip > 0)
294 {
295 buf[i++] = 0;
296 skip--;
297 }
298 skip = BN_bn2bin(y, buf + i);
299 i += skip;
300 }
301
302 if (i != ret)
303 {
304 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
305 goto err;
306 }
307 }
308
309 if (used_ctx)
310 BN_CTX_end(ctx);
311 if (new_ctx != NULL)
312 BN_CTX_free(new_ctx);
313 return ret;
314
315 err:
316 if (used_ctx)
317 BN_CTX_end(ctx);
318 if (new_ctx != NULL)
319 BN_CTX_free(new_ctx);
320 return 0;
321 }
322
323
324int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
325 const unsigned char *buf, size_t len, BN_CTX *ctx)
326 {
327 point_conversion_form_t form;
328 int y_bit;
329 BN_CTX *new_ctx = NULL;
330 BIGNUM *x, *y;
331 size_t field_len, enc_len;
332 int ret = 0;
333
334 if (len == 0)
335 {
336 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
337 return 0;
338 }
339 form = buf[0];
340 y_bit = form & 1;
341 form = form & ~1U;
342 if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
343 && (form != POINT_CONVERSION_UNCOMPRESSED)
344 && (form != POINT_CONVERSION_HYBRID))
345 {
346 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
347 return 0;
348 }
349 if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit)
350 {
351 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
352 return 0;
353 }
354
355 if (form == 0)
356 {
357 if (len != 1)
358 {
359 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
360 return 0;
361 }
362
363 return EC_POINT_set_to_infinity(group, point);
364 }
365
366 field_len = BN_num_bytes(&group->field);
367 enc_len = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
368
369 if (len != enc_len)
370 {
371 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
372 return 0;
373 }
374
375 if (ctx == NULL)
376 {
377 ctx = new_ctx = BN_CTX_new();
378 if (ctx == NULL)
379 return 0;
380 }
381
382 BN_CTX_start(ctx);
383 x = BN_CTX_get(ctx);
384 y = BN_CTX_get(ctx);
385 if (y == NULL) goto err;
386
387 if (!BN_bin2bn(buf + 1, field_len, x)) goto err;
388 if (BN_ucmp(x, &group->field) >= 0)
389 {
390 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
391 goto err;
392 }
393
394 if (form == POINT_CONVERSION_COMPRESSED)
395 {
396 if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) goto err;
397 }
398 else
399 {
400 if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) goto err;
401 if (BN_ucmp(y, &group->field) >= 0)
402 {
403 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
404 goto err;
405 }
406 if (form == POINT_CONVERSION_HYBRID)
407 {
408 if (y_bit != BN_is_odd(y))
409 {
410 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
411 goto err;
412 }
413 }
414
415 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
416 }
417
418 if (!EC_POINT_is_on_curve(group, point, ctx)) /* test required by X9.62 */
419 {
420 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);
421 goto err;
422 }
423
424 ret = 1;
425
426 err:
427 BN_CTX_end(ctx);
428 if (new_ctx != NULL)
429 BN_CTX_free(new_ctx);
430 return ret;
431 }
432