cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
ec_lib.c
Go to the documentation of this file.
1 /* crypto/ec/ec_lib.c */
2 /*
3  * Originally written by Bodo Moeller for the OpenSSL project.
4  */
5 /* ====================================================================
6  * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  * software must display the following acknowledgment:
22  * "This product includes software developed by the OpenSSL Project
23  * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  * endorse or promote products derived from this software without
27  * prior written permission. For written permission, please contact
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  * nor may "OpenSSL" appear in their names without prior written
32  * permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  * acknowledgment:
36  * "This product includes software developed by the OpenSSL Project
37  * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * ([email protected]). This product includes software written by Tim
55  * Hudson ([email protected]).
56  *
57  */
58 /* ====================================================================
59  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60  * Binary polynomial ECC support in OpenSSL originally developed by
61  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
62  */
63 
64 #if defined( INC_ALL )
65  #include "ec_lcl.h"
66 #else
67  #include "bn/ec_lcl.h"
68 #endif /* Compiler-specific includes */
69 
70 #if defined( USE_ECDH ) || defined( USE_ECDSA )
71 
72 /* functions for EC_GROUP objects */
73 
74 EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
75  {
76  EC_GROUP *ret;
77 
78  if (meth == NULL)
79  {
80  ECerr(EC_F_EC_GROUP_NEW, ERR_R_PASSED_NULL_PARAMETER);
81  return NULL;
82  }
83  if (meth->group_init == 0)
84  {
85  ECerr(EC_F_EC_GROUP_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
86  return NULL;
87  }
88 
89  ret = clBnAlloc( "EC_GROUP_new", sizeof *ret);
90  if (ret == NULL)
91  {
92  ECerr(EC_F_EC_GROUP_NEW, ERR_R_MALLOC_FAILURE);
93  return NULL;
94  }
95 
96  ret->meth = meth;
97 
98  ret->extra_data = NULL;
99 
100  ret->generator = NULL;
101  BN_init(&ret->order);
102  BN_init(&ret->cofactor);
103 
104  ret->curve_name = 0;
105  ret->asn1_flag = 0;
107 
108  ret->seed = NULL;
109  ret->seed_len = 0;
110 
111  if (!meth->group_init(ret))
112  {
113  OPENSSL_free(ret);
114  return NULL;
115  }
116 
117  return ret;
118  }
119 
120 
121 void EC_GROUP_free(EC_GROUP *group)
122  {
123  if (!group) return;
124 
125  if (group->meth->group_finish != 0)
126  group->meth->group_finish(group);
127 
129 
130  if (group->generator != NULL)
131  EC_POINT_free(group->generator);
132  BN_free(&group->order);
133  BN_free(&group->cofactor);
134 
135  if (group->seed)
136  OPENSSL_free(group->seed);
137 
138  OPENSSL_free(group);
139  }
140 
141 
142 void EC_GROUP_clear_free(EC_GROUP *group)
143  {
144  if (!group) return;
145 
146  if (group->meth->group_clear_finish != 0)
147  group->meth->group_clear_finish(group);
148  else if (group->meth->group_finish != 0)
149  group->meth->group_finish(group);
150 
152 
153  if (group->generator != NULL)
155  BN_clear_free(&group->order);
156  BN_clear_free(&group->cofactor);
157 
158  if (group->seed)
159  {
160  OPENSSL_cleanse(group->seed, group->seed_len);
161  OPENSSL_free(group->seed);
162  }
163 
164  OPENSSL_cleanse(group, sizeof *group);
165  OPENSSL_free(group);
166  }
167 
168 
169 int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
170  {
171  EC_EXTRA_DATA *d;
172 
173  if (dest->meth->group_copy == 0)
174  {
175  ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
176  return 0;
177  }
178  if (dest->meth != src->meth)
179  {
181  return 0;
182  }
183  if (dest == src)
184  return 1;
185 
187 
188  for (d = src->extra_data; d != NULL; d = d->next)
189  {
190  void *t = d->dup_func(d->data);
191 
192  if (t == NULL)
193  return 0;
195  return 0;
196  }
197 
198  if (src->generator != NULL)
199  {
200  if (dest->generator == NULL)
201  {
202  dest->generator = EC_POINT_new(dest);
203  if (dest->generator == NULL) return 0;
204  }
205  if (!EC_POINT_copy(dest->generator, src->generator)) return 0;
206  }
207  else
208  {
209  /* src->generator == NULL */
210  if (dest->generator != NULL)
211  {
213  dest->generator = NULL;
214  }
215  }
216 
217  if (!BN_copy(&dest->order, &src->order)) return 0;
218  if (!BN_copy(&dest->cofactor, &src->cofactor)) return 0;
219 
220  dest->curve_name = src->curve_name;
221  dest->asn1_flag = src->asn1_flag;
222  dest->asn1_form = src->asn1_form;
223 
224  if (src->seed)
225  {
226  if (dest->seed)
227  OPENSSL_free(dest->seed);
228  dest->seed = clBnAlloc( "EC_GROUP_copy", src->seed_len);
229  if (dest->seed == NULL)
230  return 0;
231  if (!memcpy(dest->seed, src->seed, src->seed_len))
232  return 0;
233  dest->seed_len = src->seed_len;
234  }
235  else
236  {
237  if (dest->seed)
238  OPENSSL_free(dest->seed);
239  dest->seed = NULL;
240  dest->seed_len = 0;
241  }
242 
243 
244  return dest->meth->group_copy(dest, src);
245  }
246 
247 
248 EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
249  {
250  EC_GROUP *t = NULL;
251  int ok = 0;
252 
253  if (a == NULL) return NULL;
254 
255  if ((t = EC_GROUP_new(a->meth)) == NULL) return(NULL);
256  if (!EC_GROUP_copy(t, a)) goto err;
257 
258  ok = 1;
259 
260  err:
261  if (!ok)
262  {
263  if (t) EC_GROUP_free(t);
264  return NULL;
265  }
266  else return t;
267  }
268 
269 
270 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
271  {
272  return group->meth;
273  }
274 
275 
276 int EC_METHOD_get_field_type(const EC_METHOD *meth)
277  {
278  return meth->field_type;
279  }
280 
281 
282 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator, const BIGNUM *order, const BIGNUM *cofactor)
283  {
284  if (generator == NULL)
285  {
286  ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
287  return 0 ;
288  }
289 
290  if (group->generator == NULL)
291  {
292  group->generator = EC_POINT_new(group);
293  if (group->generator == NULL) return 0;
294  }
295  if (!EC_POINT_copy(group->generator, generator)) return 0;
296 
297  if (order != NULL)
298  { if (!BN_copy(&group->order, order)) return 0; }
299  else
300  BN_zero(&group->order);
301 
302  if (cofactor != NULL)
303  { if (!BN_copy(&group->cofactor, cofactor)) return 0; }
304  else
305  BN_zero(&group->cofactor);
306 
307  return 1;
308  }
309 
310 
311 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
312  {
313  return group->generator;
314  }
315 
316 
317 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
318  {
319  if (!BN_copy(order, &group->order))
320  return 0;
321 
322  return !BN_is_zero(order);
323  }
324 
325 
326 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, BN_CTX *ctx)
327  {
328  if (!BN_copy(cofactor, &group->cofactor))
329  return 0;
330 
331  return !BN_is_zero(&group->cofactor);
332  }
333 
334 
335 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
336  {
337  group->curve_name = nid;
338  }
339 
340 
341 int EC_GROUP_get_curve_name(const EC_GROUP *group)
342  {
343  return group->curve_name;
344  }
345 
346 
347 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
348  {
349  group->asn1_flag = flag;
350  }
351 
352 
353 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
354  {
355  return group->asn1_flag;
356  }
357 
358 
361  {
362  group->asn1_form = form;
363  }
364 
365 
367  {
368  return group->asn1_form;
369  }
370 
371 
372 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
373  {
374  if (group->seed)
375  {
376  OPENSSL_free(group->seed);
377  group->seed = NULL;
378  group->seed_len = 0;
379  }
380 
381  if (!len || !p)
382  return 1;
383 
384  if ((group->seed = clBnAlloc( "EC_GROUP_set_seed", len)) == NULL)
385  return 0;
386  memcpy(group->seed, p, len);
387  group->seed_len = len;
388 
389  return len;
390  }
391 
392 
393 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
394  {
395  return group->seed;
396  }
397 
398 
399 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
400  {
401  return group->seed_len;
402  }
403 
404 
405 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
406  {
407  if (group->meth->group_set_curve == 0)
408  {
409  ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
410  return 0;
411  }
412  return group->meth->group_set_curve(group, p, a, b, ctx);
413  }
414 
415 
416 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
417  {
418  if (group->meth->group_get_curve == 0)
419  {
420  ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
421  return 0;
422  }
423  return group->meth->group_get_curve(group, p, a, b, ctx);
424  }
425 
426 
427 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
428  {
429  if (group->meth->group_set_curve == 0)
430  {
431  ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
432  return 0;
433  }
434  return group->meth->group_set_curve(group, p, a, b, ctx);
435  }
436 
437 
438 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
439  {
440  if (group->meth->group_get_curve == 0)
441  {
442  ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
443  return 0;
444  }
445  return group->meth->group_get_curve(group, p, a, b, ctx);
446  }
447 
448 
449 int EC_GROUP_get_degree(const EC_GROUP *group)
450  {
451  if (group->meth->group_get_degree == 0)
452  {
453  ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
454  return 0;
455  }
456  return group->meth->group_get_degree(group);
457  }
458 
459 
460 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
461  {
462  if (group->meth->group_check_discriminant == 0)
463  {
464  ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
465  return 0;
466  }
467  return group->meth->group_check_discriminant(group, ctx);
468  }
469 
470 
471 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
472  {
473  int r = 0;
474  BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
475  BN_CTX *ctx_new = NULL;
476 
477  /* compare the field types*/
480  return 1;
481  /* compare the curve name (if present) */
484  return 0;
485 
486  if (!ctx)
487  ctx_new = ctx = BN_CTX_new();
488  if (!ctx)
489  return -1;
490 
491  BN_CTX_start(ctx);
492  a1 = BN_CTX_get(ctx);
493  a2 = BN_CTX_get(ctx);
494  a3 = BN_CTX_get(ctx);
495  b1 = BN_CTX_get(ctx);
496  b2 = BN_CTX_get(ctx);
497  b3 = BN_CTX_get(ctx);
498  if (!b3)
499  {
500  BN_CTX_end(ctx);
501  if (ctx_new)
502  BN_CTX_free(ctx);
503  return -1;
504  }
505 
506  /* XXX This approach assumes that the external representation
507  * of curves over the same field type is the same.
508  */
509  if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
510  !b->meth->group_get_curve(b, b1, b2, b3, ctx))
511  r = 1;
512 
513  if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
514  r = 1;
515 
516  /* XXX EC_POINT_cmp() assumes that the methods are equal */
517  if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
518  EC_GROUP_get0_generator(b), ctx))
519  r = 1;
520 
521  if (!r)
522  {
523  /* compare the order and cofactor */
524  if (!EC_GROUP_get_order(a, a1, ctx) ||
525  !EC_GROUP_get_order(b, b1, ctx) ||
526  !EC_GROUP_get_cofactor(a, a2, ctx) ||
527  !EC_GROUP_get_cofactor(b, b2, ctx))
528  {
529  BN_CTX_end(ctx);
530  if (ctx_new)
531  BN_CTX_free(ctx);
532  return -1;
533  }
534  if (BN_cmp(a1, b1) || BN_cmp(a2, b2))
535  r = 1;
536  }
537 
538  BN_CTX_end(ctx);
539  if (ctx_new)
540  BN_CTX_free(ctx);
541 
542  return r;
543  }
544 
545 
546 /* this has 'package' visibility */
547 int EC_EX_DATA_set_data(EC_EXTRA_DATA **ex_data, void *data,
548  void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *))
549  {
550  EC_EXTRA_DATA *d;
551 
552  if (ex_data == NULL)
553  return 0;
554 
555  for (d = *ex_data; d != NULL; d = d->next)
556  {
557  if (d->dup_func == dup_func && d->free_func == free_func && d->clear_free_func == clear_free_func)
558  {
560  return 0;
561  }
562  }
563 
564  if (data == NULL)
565  /* no explicit entry needed */
566  return 1;
567 
568  d = clBnAlloc( "EC_EX_DATA_set_data", sizeof *d);
569  if (d == NULL)
570  return 0;
571 
572  d->data = data;
573  d->dup_func = dup_func;
574  d->free_func = free_func;
575  d->clear_free_func = clear_free_func;
576 
577  d->next = *ex_data;
578  *ex_data = d;
579 
580  return 1;
581  }
582 
583 /* this has 'package' visibility */
584 void *EC_EX_DATA_get_data(const EC_EXTRA_DATA *ex_data,
585  void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *))
586  {
587  const EC_EXTRA_DATA *d;
588 
589  for (d = ex_data; d != NULL; d = d->next)
590  {
591  if (d->dup_func == dup_func && d->free_func == free_func && d->clear_free_func == clear_free_func)
592  return d->data;
593  }
594 
595  return NULL;
596  }
597 
598 /* this has 'package' visibility */
599 void EC_EX_DATA_free_data(EC_EXTRA_DATA **ex_data,
600  void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *))
601  {
602  EC_EXTRA_DATA **p;
603 
604  if (ex_data == NULL)
605  return;
606 
607  for (p = ex_data; *p != NULL; p = &((*p)->next))
608  {
609  if ((*p)->dup_func == dup_func && (*p)->free_func == free_func && (*p)->clear_free_func == clear_free_func)
610  {
611  EC_EXTRA_DATA *next = (*p)->next;
612 
613  (*p)->free_func((*p)->data);
614  OPENSSL_free(*p);
615 
616  *p = next;
617  return;
618  }
619  }
620  }
621 
622 /* this has 'package' visibility */
624  void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *))
625  {
626  EC_EXTRA_DATA **p;
627 
628  if (ex_data == NULL)
629  return;
630 
631  for (p = ex_data; *p != NULL; p = &((*p)->next))
632  {
633  if ((*p)->dup_func == dup_func && (*p)->free_func == free_func && (*p)->clear_free_func == clear_free_func)
634  {
635  EC_EXTRA_DATA *next = (*p)->next;
636 
637  (*p)->clear_free_func((*p)->data);
638  OPENSSL_free(*p);
639 
640  *p = next;
641  return;
642  }
643  }
644  }
645 
646 /* this has 'package' visibility */
648  {
649  EC_EXTRA_DATA *d;
650 
651  if (ex_data == NULL)
652  return;
653 
654  d = *ex_data;
655  while (d)
656  {
657  EC_EXTRA_DATA *next = d->next;
658 
659  d->free_func(d->data);
660  OPENSSL_free(d);
661 
662  d = next;
663  }
664  *ex_data = NULL;
665  }
666 
667 /* this has 'package' visibility */
669  {
670  EC_EXTRA_DATA *d;
671 
672  if (ex_data == NULL)
673  return;
674 
675  d = *ex_data;
676  while (d)
677  {
678  EC_EXTRA_DATA *next = d->next;
679 
680  d->clear_free_func(d->data);
681  OPENSSL_free(d);
682 
683  d = next;
684  }
685  *ex_data = NULL;
686  }
687 
688 
689 /* functions for EC_POINT objects */
690 
691 EC_POINT *EC_POINT_new(const EC_GROUP *group)
692  {
693  EC_POINT *ret;
694 
695  if (group == NULL)
696  {
697  ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
698  return NULL;
699  }
700  if (group->meth->point_init == 0)
701  {
702  ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
703  return NULL;
704  }
705 
706  ret = clBnAlloc( "EC_POINT_new", sizeof *ret);
707  if (ret == NULL)
708  {
709  ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
710  return NULL;
711  }
712 
713  ret->meth = group->meth;
714 
715  if (!ret->meth->point_init(ret))
716  {
717  OPENSSL_free(ret);
718  return NULL;
719  }
720 
721  return ret;
722  }
723 
724 
725 void EC_POINT_free(EC_POINT *point)
726  {
727  if (!point) return;
728 
729  if (point->meth->point_finish != 0)
730  point->meth->point_finish(point);
731  OPENSSL_free(point);
732  }
733 
734 
735 void EC_POINT_clear_free(EC_POINT *point)
736  {
737  if (!point) return;
738 
739  if (point->meth->point_clear_finish != 0)
740  point->meth->point_clear_finish(point);
741  else if (point->meth != NULL && point->meth->point_finish != 0)
742  point->meth->point_finish(point);
743  OPENSSL_cleanse(point, sizeof *point);
744  OPENSSL_free(point);
745  }
746 
747 
748 int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
749  {
750  if (dest->meth->point_copy == 0)
751  {
752  ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
753  return 0;
754  }
755  if (dest->meth != src->meth)
756  {
758  return 0;
759  }
760  if (dest == src)
761  return 1;
762  return dest->meth->point_copy(dest, src);
763  }
764 
765 
766 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
767  {
768  EC_POINT *t;
769  int r;
770 
771  if (a == NULL) return NULL;
772 
773  t = EC_POINT_new(group);
774  if (t == NULL) return(NULL);
775  r = EC_POINT_copy(t, a);
776  if (!r)
777  {
778  EC_POINT_free(t);
779  return NULL;
780  }
781  else return t;
782  }
783 
784 
785 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
786  {
787  return point->meth;
788  }
789 
790 
791 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
792  {
793  if (group->meth->point_set_to_infinity == 0)
794  {
795  ECerr(EC_F_EC_POINT_SET_TO_INFINITY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
796  return 0;
797  }
798  if (group->meth != point->meth)
799  {
801  return 0;
802  }
803  return group->meth->point_set_to_infinity(group, point);
804  }
805 
806 
808  const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *ctx)
809  {
811  {
812  ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
813  return 0;
814  }
815  if (group->meth != point->meth)
816  {
818  return 0;
819  }
820  return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
821  }
822 
823 
824 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
825  BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *ctx)
826  {
828  {
829  ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
830  return 0;
831  }
832  if (group->meth != point->meth)
833  {
835  return 0;
836  }
837  return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
838  }
839 
840 
841 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
842  const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
843  {
844  if (group->meth->point_set_affine_coordinates == 0)
845  {
846  ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
847  return 0;
848  }
849  if (group->meth != point->meth)
850  {
852  return 0;
853  }
854  return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
855  }
856 
857 
859  const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
860  {
861  if (group->meth->point_set_affine_coordinates == 0)
862  {
863  ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
864  return 0;
865  }
866  if (group->meth != point->meth)
867  {
869  return 0;
870  }
871  return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
872  }
873 
874 
875 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
876  BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
877  {
878  if (group->meth->point_get_affine_coordinates == 0)
879  {
880  ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
881  return 0;
882  }
883  if (group->meth != point->meth)
884  {
886  return 0;
887  }
888  return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
889  }
890 
891 
892 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group, const EC_POINT *point,
893  BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
894  {
895  if (group->meth->point_get_affine_coordinates == 0)
896  {
897  ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
898  return 0;
899  }
900  if (group->meth != point->meth)
901  {
903  return 0;
904  }
905  return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
906  }
907 
908 
910  const BIGNUM *x, int y_bit, BN_CTX *ctx)
911  {
912  if (group->meth->point_set_compressed_coordinates == 0)
913  {
914  ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
915  return 0;
916  }
917  if (group->meth != point->meth)
918  {
920  return 0;
921  }
922  return group->meth->point_set_compressed_coordinates(group, point, x, y_bit, ctx);
923  }
924 
925 
927  const BIGNUM *x, int y_bit, BN_CTX *ctx)
928  {
929  if (group->meth->point_set_compressed_coordinates == 0)
930  {
931  ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
932  return 0;
933  }
934  if (group->meth != point->meth)
935  {
937  return 0;
938  }
939  return group->meth->point_set_compressed_coordinates(group, point, x, y_bit, ctx);
940  }
941 
942 
943 size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form,
944  unsigned char *buf, size_t len, BN_CTX *ctx)
945  {
946  if (group->meth->point2oct == 0)
947  {
948  ECerr(EC_F_EC_POINT_POINT2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
949  return 0;
950  }
951  if (group->meth != point->meth)
952  {
954  return 0;
955  }
956  return group->meth->point2oct(group, point, form, buf, len, ctx);
957  }
958 
959 
960 int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
961  const unsigned char *buf, size_t len, BN_CTX *ctx)
962  {
963  if (group->meth->oct2point == 0)
964  {
965  ECerr(EC_F_EC_POINT_OCT2POINT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
966  return 0;
967  }
968  if (group->meth != point->meth)
969  {
971  return 0;
972  }
973  return group->meth->oct2point(group, point, buf, len, ctx);
974  }
975 
976 
977 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
978  {
979  if (group->meth->add == 0)
980  {
981  ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
982  return 0;
983  }
984  if ((group->meth != r->meth) || (r->meth != a->meth) || (a->meth != b->meth))
985  {
987  return 0;
988  }
989  return group->meth->add(group, r, a, b, ctx);
990  }
991 
992 
993 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx)
994  {
995  if (group->meth->dbl == 0)
996  {
997  ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
998  return 0;
999  }
1000  if ((group->meth != r->meth) || (r->meth != a->meth))
1001  {
1003  return 0;
1004  }
1005  return group->meth->dbl(group, r, a, ctx);
1006  }
1007 
1008 
1009 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
1010  {
1011  if (group->meth->dbl == 0)
1012  {
1013  ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1014  return 0;
1015  }
1016  if (group->meth != a->meth)
1017  {
1019  return 0;
1020  }
1021  return group->meth->invert(group, a, ctx);
1022  }
1023 
1024 
1025 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
1026  {
1027  if (group->meth->is_at_infinity == 0)
1028  {
1029  ECerr(EC_F_EC_POINT_IS_AT_INFINITY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1030  return 0;
1031  }
1032  if (group->meth != point->meth)
1033  {
1035  return 0;
1036  }
1037  return group->meth->is_at_infinity(group, point);
1038  }
1039 
1040 
1041 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx)
1042  {
1043  if (group->meth->is_on_curve == 0)
1044  {
1045  ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1046  return 0;
1047  }
1048  if (group->meth != point->meth)
1049  {
1051  return 0;
1052  }
1053  return group->meth->is_on_curve(group, point, ctx);
1054  }
1055 
1056 
1057 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
1058  {
1059  if (group->meth->point_cmp == 0)
1060  {
1061  ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1062  return 0;
1063  }
1064  if ((group->meth != a->meth) || (a->meth != b->meth))
1065  {
1067  return 0;
1068  }
1069  return group->meth->point_cmp(group, a, b, ctx);
1070  }
1071 
1072 
1073 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1074  {
1075  if (group->meth->make_affine == 0)
1076  {
1077  ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1078  return 0;
1079  }
1080  if (group->meth != point->meth)
1081  {
1083  return 0;
1084  }
1085  return group->meth->make_affine(group, point, ctx);
1086  }
1087 
1088 
1089 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num, EC_POINT *points[], BN_CTX *ctx)
1090  {
1091  size_t i;
1092 
1093  if (group->meth->points_make_affine == 0)
1094  {
1095  ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1096  return 0;
1097  }
1098  for (i = 0; i < num; i++)
1099  {
1100  if (group->meth != points[i]->meth)
1101  {
1103  return 0;
1104  }
1105  }
1106  return group->meth->points_make_affine(group, num, points, ctx);
1107  }
1108 
1109 
1110 /* Functions for point multiplication.
1111  *
1112  * If group->meth->mul is 0, we use the wNAF-based implementations in ec_mult.c;
1113  * otherwise we dispatch through methods.
1114  */
1115 
1116 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1117  size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx)
1118  {
1119  if (group->meth->mul == 0)
1120  /* use default */
1121  return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1122 
1123  return group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1124  }
1125 
1126 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1127  const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1128  {
1129  /* just a convenient interface to EC_POINTs_mul() */
1130 
1131  const EC_POINT *points[1];
1132  const BIGNUM *scalars[1];
1133 
1134  points[0] = point;
1135  scalars[0] = p_scalar;
1136 
1137  return EC_POINTs_mul(group, r, g_scalar, (point != NULL && p_scalar != NULL), points, scalars, ctx);
1138  }
1139 
1140 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1141  {
1142  if (group->meth->mul == 0)
1143  /* use default */
1144  return ec_wNAF_precompute_mult(group, ctx);
1145 
1146  if (group->meth->precompute_mult != 0)
1147  return group->meth->precompute_mult(group, ctx);
1148  else
1149  return 1; /* nothing to do, so report success */
1150  }
1151 
1152 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
1153  {
1154  if (group->meth->mul == 0)
1155  /* use default */
1156  return ec_wNAF_have_precompute_mult(group);
1157 
1158  if (group->meth->have_precompute_mult != 0)
1159  return group->meth->have_precompute_mult(group);
1160  else
1161  return 0; /* cannot tell whether precomputation has been performed */
1162  }
1163 
1164 #endif /* USE_ECDH || USE_ECDSA */