xref: /minix3/crypto/external/bsd/openssl/dist/crypto/ec/ec_oct.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* crypto/ec/ec_lib.c */
2ebfedea0SLionel Sambuc /*
3ebfedea0SLionel Sambuc  * Originally written by Bodo Moeller for the OpenSSL project.
4ebfedea0SLionel Sambuc  */
5ebfedea0SLionel Sambuc /* ====================================================================
6ebfedea0SLionel Sambuc  * Copyright (c) 1998-2003 The OpenSSL Project.  All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
17ebfedea0SLionel Sambuc  *    the documentation and/or other materials provided with the
18ebfedea0SLionel Sambuc  *    distribution.
19ebfedea0SLionel Sambuc  *
20ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this
21ebfedea0SLionel Sambuc  *    software must display the following acknowledgment:
22ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
23ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24ebfedea0SLionel Sambuc  *
25ebfedea0SLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26ebfedea0SLionel Sambuc  *    endorse or promote products derived from this software without
27ebfedea0SLionel Sambuc  *    prior written permission. For written permission, please contact
28ebfedea0SLionel Sambuc  *    openssl-core@openssl.org.
29ebfedea0SLionel Sambuc  *
30ebfedea0SLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
31ebfedea0SLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
32ebfedea0SLionel Sambuc  *    permission of the OpenSSL Project.
33ebfedea0SLionel Sambuc  *
34ebfedea0SLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
35ebfedea0SLionel Sambuc  *    acknowledgment:
36ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
37ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38ebfedea0SLionel Sambuc  *
39ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40ebfedea0SLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43ebfedea0SLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44ebfedea0SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46ebfedea0SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48ebfedea0SLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50ebfedea0SLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
51ebfedea0SLionel Sambuc  * ====================================================================
52ebfedea0SLionel Sambuc  *
53ebfedea0SLionel Sambuc  * This product includes cryptographic software written by Eric Young
54ebfedea0SLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
55ebfedea0SLionel Sambuc  * Hudson (tjh@cryptsoft.com).
56ebfedea0SLionel Sambuc  *
57ebfedea0SLionel Sambuc  */
58ebfedea0SLionel Sambuc /* ====================================================================
59ebfedea0SLionel Sambuc  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60ebfedea0SLionel Sambuc  * Binary polynomial ECC support in OpenSSL originally developed by
61ebfedea0SLionel Sambuc  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
62ebfedea0SLionel Sambuc  */
63ebfedea0SLionel Sambuc 
64ebfedea0SLionel Sambuc #include <string.h>
65ebfedea0SLionel Sambuc 
66ebfedea0SLionel Sambuc #include <openssl/err.h>
67ebfedea0SLionel Sambuc #include <openssl/opensslv.h>
68ebfedea0SLionel Sambuc 
69ebfedea0SLionel Sambuc #include "ec_lcl.h"
70ebfedea0SLionel Sambuc 
EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP * group,EC_POINT * point,const BIGNUM * x,int y_bit,BN_CTX * ctx)71*0a6a1f1dSLionel Sambuc int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
72*0a6a1f1dSLionel Sambuc                                             EC_POINT *point, const BIGNUM *x,
73*0a6a1f1dSLionel Sambuc                                             int y_bit, BN_CTX *ctx)
74ebfedea0SLionel Sambuc {
75ebfedea0SLionel Sambuc     if (group->meth->point_set_compressed_coordinates == 0
76*0a6a1f1dSLionel Sambuc         && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
77*0a6a1f1dSLionel Sambuc         ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
78*0a6a1f1dSLionel Sambuc               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
79ebfedea0SLionel Sambuc         return 0;
80ebfedea0SLionel Sambuc     }
81*0a6a1f1dSLionel Sambuc     if (group->meth != point->meth) {
82*0a6a1f1dSLionel Sambuc         ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
83*0a6a1f1dSLionel Sambuc               EC_R_INCOMPATIBLE_OBJECTS);
84ebfedea0SLionel Sambuc         return 0;
85ebfedea0SLionel Sambuc     }
86*0a6a1f1dSLionel Sambuc     if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
87ebfedea0SLionel Sambuc         if (group->meth->field_type == NID_X9_62_prime_field)
88*0a6a1f1dSLionel Sambuc             return ec_GFp_simple_set_compressed_coordinates(group, point, x,
89*0a6a1f1dSLionel Sambuc                                                             y_bit, ctx);
90ebfedea0SLionel Sambuc         else
91ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_EC2M
92ebfedea0SLionel Sambuc         {
93*0a6a1f1dSLionel Sambuc             ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
94*0a6a1f1dSLionel Sambuc                   EC_R_GF2M_NOT_SUPPORTED);
95ebfedea0SLionel Sambuc             return 0;
96ebfedea0SLionel Sambuc         }
97ebfedea0SLionel Sambuc #else
98*0a6a1f1dSLionel Sambuc             return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
99*0a6a1f1dSLionel Sambuc                                                              y_bit, ctx);
100ebfedea0SLionel Sambuc #endif
101ebfedea0SLionel Sambuc     }
102*0a6a1f1dSLionel Sambuc     return group->meth->point_set_compressed_coordinates(group, point, x,
103*0a6a1f1dSLionel Sambuc                                                          y_bit, ctx);
104ebfedea0SLionel Sambuc }
105ebfedea0SLionel Sambuc 
106ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_EC2M
EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP * group,EC_POINT * point,const BIGNUM * x,int y_bit,BN_CTX * ctx)107*0a6a1f1dSLionel Sambuc int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,
108*0a6a1f1dSLionel Sambuc                                              EC_POINT *point, const BIGNUM *x,
109*0a6a1f1dSLionel Sambuc                                              int y_bit, BN_CTX *ctx)
110ebfedea0SLionel Sambuc {
111ebfedea0SLionel Sambuc     if (group->meth->point_set_compressed_coordinates == 0
112*0a6a1f1dSLionel Sambuc         && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
113*0a6a1f1dSLionel Sambuc         ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,
114*0a6a1f1dSLionel Sambuc               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
115ebfedea0SLionel Sambuc         return 0;
116ebfedea0SLionel Sambuc     }
117*0a6a1f1dSLionel Sambuc     if (group->meth != point->meth) {
118*0a6a1f1dSLionel Sambuc         ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,
119*0a6a1f1dSLionel Sambuc               EC_R_INCOMPATIBLE_OBJECTS);
120ebfedea0SLionel Sambuc         return 0;
121ebfedea0SLionel Sambuc     }
122*0a6a1f1dSLionel Sambuc     if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
123ebfedea0SLionel Sambuc         if (group->meth->field_type == NID_X9_62_prime_field)
124*0a6a1f1dSLionel Sambuc             return ec_GFp_simple_set_compressed_coordinates(group, point, x,
125*0a6a1f1dSLionel Sambuc                                                             y_bit, ctx);
126ebfedea0SLionel Sambuc         else
127*0a6a1f1dSLionel Sambuc             return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
128*0a6a1f1dSLionel Sambuc                                                              y_bit, ctx);
129ebfedea0SLionel Sambuc     }
130*0a6a1f1dSLionel Sambuc     return group->meth->point_set_compressed_coordinates(group, point, x,
131*0a6a1f1dSLionel Sambuc                                                          y_bit, ctx);
132ebfedea0SLionel Sambuc }
133ebfedea0SLionel Sambuc #endif
134ebfedea0SLionel Sambuc 
EC_POINT_point2oct(const EC_GROUP * group,const EC_POINT * point,point_conversion_form_t form,unsigned char * buf,size_t len,BN_CTX * ctx)135*0a6a1f1dSLionel Sambuc size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
136*0a6a1f1dSLionel Sambuc                           point_conversion_form_t form, unsigned char *buf,
137*0a6a1f1dSLionel Sambuc                           size_t len, BN_CTX *ctx)
138ebfedea0SLionel Sambuc {
139ebfedea0SLionel Sambuc     if (group->meth->point2oct == 0
140*0a6a1f1dSLionel Sambuc         && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
141ebfedea0SLionel Sambuc         ECerr(EC_F_EC_POINT_POINT2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
142ebfedea0SLionel Sambuc         return 0;
143ebfedea0SLionel Sambuc     }
144*0a6a1f1dSLionel Sambuc     if (group->meth != point->meth) {
145ebfedea0SLionel Sambuc         ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_INCOMPATIBLE_OBJECTS);
146ebfedea0SLionel Sambuc         return 0;
147ebfedea0SLionel Sambuc     }
148*0a6a1f1dSLionel Sambuc     if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
149ebfedea0SLionel Sambuc         if (group->meth->field_type == NID_X9_62_prime_field)
150*0a6a1f1dSLionel Sambuc             return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx);
151ebfedea0SLionel Sambuc         else
152ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_EC2M
153ebfedea0SLionel Sambuc         {
154ebfedea0SLionel Sambuc             ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_GF2M_NOT_SUPPORTED);
155ebfedea0SLionel Sambuc             return 0;
156ebfedea0SLionel Sambuc         }
157ebfedea0SLionel Sambuc #else
158ebfedea0SLionel Sambuc             return ec_GF2m_simple_point2oct(group, point,
159ebfedea0SLionel Sambuc                                             form, buf, len, ctx);
160ebfedea0SLionel Sambuc #endif
161ebfedea0SLionel Sambuc     }
162ebfedea0SLionel Sambuc 
163ebfedea0SLionel Sambuc     return group->meth->point2oct(group, point, form, buf, len, ctx);
164ebfedea0SLionel Sambuc }
165ebfedea0SLionel Sambuc 
EC_POINT_oct2point(const EC_GROUP * group,EC_POINT * point,const unsigned char * buf,size_t len,BN_CTX * ctx)166ebfedea0SLionel Sambuc int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
167ebfedea0SLionel Sambuc                        const unsigned char *buf, size_t len, BN_CTX *ctx)
168ebfedea0SLionel Sambuc {
169ebfedea0SLionel Sambuc     if (group->meth->oct2point == 0
170*0a6a1f1dSLionel Sambuc         && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
171ebfedea0SLionel Sambuc         ECerr(EC_F_EC_POINT_OCT2POINT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
172ebfedea0SLionel Sambuc         return 0;
173ebfedea0SLionel Sambuc     }
174*0a6a1f1dSLionel Sambuc     if (group->meth != point->meth) {
175ebfedea0SLionel Sambuc         ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_INCOMPATIBLE_OBJECTS);
176ebfedea0SLionel Sambuc         return 0;
177ebfedea0SLionel Sambuc     }
178*0a6a1f1dSLionel Sambuc     if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
179ebfedea0SLionel Sambuc         if (group->meth->field_type == NID_X9_62_prime_field)
180*0a6a1f1dSLionel Sambuc             return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
181ebfedea0SLionel Sambuc         else
182ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_EC2M
183ebfedea0SLionel Sambuc         {
184ebfedea0SLionel Sambuc             ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_GF2M_NOT_SUPPORTED);
185ebfedea0SLionel Sambuc             return 0;
186ebfedea0SLionel Sambuc         }
187ebfedea0SLionel Sambuc #else
188*0a6a1f1dSLionel Sambuc             return ec_GF2m_simple_oct2point(group, point, buf, len, ctx);
189ebfedea0SLionel Sambuc #endif
190ebfedea0SLionel Sambuc     }
191ebfedea0SLionel Sambuc     return group->meth->oct2point(group, point, buf, len, ctx);
192ebfedea0SLionel Sambuc }
193