2 /* crypto/ec/ecp_mont2.c */
3 /* ====================================================================
4 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * 3. All advertising materials mentioning features or use of this
19 * software must display the following acknowledgment:
20 * "This product includes software developed by the OpenSSL Project
21 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
23 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
24 * endorse or promote products derived from this software without
25 * prior written permission. For written permission, please contact
26 * openssl-core@openssl.org.
28 * 5. Products derived from this software may not be called "OpenSSL"
29 * nor may "OpenSSL" appear in their names without prior written
30 * permission of the OpenSSL Project.
32 * 6. Redistributions of any form whatsoever must retain the following
34 * "This product includes software developed by the OpenSSL Project
35 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
37 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
38 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
40 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
43 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
46 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
48 * OF THE POSSIBILITY OF SUCH DAMAGE.
49 * ====================================================================
51 * This product includes cryptographic software written by Eric Young
52 * (eay@cryptsoft.com). This product includes software written by Tim
53 * Hudson (tjh@cryptsoft.com).
59 #include <openssl/err.h>
63 #include "../bn/bn_mont2.c"
65 int ec_GFp_mont2_group_init(EC_GROUP *);
66 int ec_GFp_mont2_group_set_curve_GFp(EC_GROUP *, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *);
67 void ec_GFp_mont2_group_finish(EC_GROUP *);
68 void ec_GFp_mont2_group_clear_finish(EC_GROUP *);
69 /* int ec_GFp_mont2_group_copy(EC_GROUP *, const EC_GROUP *); */
70 int ec_GFp_mont2_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *);
71 int ec_GFp_mont2_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
72 int ec_GFp_mont2_field_encode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
73 int ec_GFp_mont2_field_decode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
74 int ec_GFp_mont2_field_set_to_one(const EC_GROUP *, BIGNUM *r, BN_CTX *);
76 const EC_METHOD *EC_GFp_mont2_method(void)
78 static const EC_METHOD ret = {
79 ec_GFp_mont2_group_init,
80 ec_GFp_mont2_group_finish,
81 ec_GFp_mont2_group_clear_finish,
82 0 /* ec_GFp_mont2_group_copy */,
83 ec_GFp_mont2_group_set_curve_GFp,
84 ec_GFp_simple_group_get_curve_GFp,
85 ec_GFp_simple_group_set_generator,
86 ec_GFp_simple_group_get0_generator,
87 ec_GFp_simple_group_get_order,
88 ec_GFp_simple_group_get_cofactor,
89 ec_GFp_simple_point_init,
90 ec_GFp_simple_point_finish,
91 ec_GFp_simple_point_clear_finish,
92 ec_GFp_simple_point_copy,
93 ec_GFp_simple_point_set_to_infinity,
94 ec_GFp_simple_set_Jprojective_coordinates_GFp,
95 ec_GFp_simple_get_Jprojective_coordinates_GFp,
96 ec_GFp_simple_point_set_affine_coordinates_GFp,
97 ec_GFp_simple_point_get_affine_coordinates_GFp,
98 ec_GFp_simple_set_compressed_coordinates_GFp,
99 ec_GFp_simple_point2oct,
100 ec_GFp_simple_oct2point,
103 ec_GFp_simple_invert,
104 ec_GFp_simple_is_at_infinity,
105 ec_GFp_simple_is_on_curve,
107 ec_GFp_simple_make_affine,
108 ec_GFp_simple_points_make_affine,
109 ec_GFp_mont2_field_mul,
110 ec_GFp_mont2_field_sqr,
111 ec_GFp_mont2_field_encode,
112 ec_GFp_mont2_field_decode,
113 ec_GFp_mont2_field_set_to_one };
119 int ec_GFp_mont2_group_init(EC_GROUP *group)
123 ok = ec_GFp_simple_group_init(group);
124 group->field_data1 = NULL;
125 group->field_data2 = NULL;
130 int ec_GFp_mont2_group_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
132 BN_CTX *new_ctx = NULL;
133 BN_MONTGOMERY *mont = NULL;
137 if (group->field_data1 != NULL)
139 BN_mont_clear_free(group->field_data1);
140 group->field_data1 = NULL;
142 if (group->field_data2 != NULL)
144 BN_free(group->field_data2);
145 group->field_data2 = NULL;
150 ctx = new_ctx = BN_CTX_new();
155 mont = BN_mont_new();
156 if (mont == NULL) goto err;
157 if (!BN_mont_set(p, mont, ctx))
159 ECerr(EC_F_GFP_MONT2_GROUP_SET_CURVE_GFP, ERR_R_BN_LIB);
163 if (one == NULL) goto err;
164 if (!BN_one(one)) goto err;
165 if (!BN_to_mont(one, mont, ctx)) goto err;
167 group->field_data1 = mont;
169 group->field_data2 = one;
172 ret = ec_GFp_simple_group_set_curve_GFp(group, p, a, b, ctx);
176 BN_mont_clear_free(group->field_data1);
177 group->field_data1 = NULL;
178 BN_free(group->field_data2);
179 group->field_data2 = NULL;
184 BN_CTX_free(new_ctx);
186 BN_mont_clear_free(mont);
191 void ec_GFp_mont2_group_finish(EC_GROUP *group)
193 if (group->field_data1 != NULL)
195 BN_mont_clear_free(group->field_data1);
196 group->field_data1 = NULL;
198 if (group->field_data2 != NULL)
200 BN_free(group->field_data2);
201 group->field_data2 = NULL;
203 ec_GFp_simple_group_finish(group);
207 void ec_GFp_mont2_group_clear_finish(EC_GROUP *group)
209 if (group->field_data1 != NULL)
211 BN_mont_clear_free(group->field_data1);
212 group->field_data1 = NULL;
214 if (group->field_data2 != NULL)
216 BN_clear_free(group->field_data2);
217 group->field_data2 = NULL;
219 ec_GFp_simple_group_clear_finish(group);
223 int ec_GFp_mont2_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
225 if (group->field_data1 == NULL)
227 ECerr(EC_F_EC_GFP_MONT2_FIELD_MUL, EC_R_NOT_INITIALIZED);
231 return BN_mont_mod_mul(r, a, b, group->field_data1, ctx);
235 int ec_GFp_mont2_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
237 if (group->field_data1 == NULL)
239 ECerr(EC_F_EC_GFP_MONT2_FIELD_SQR, EC_R_NOT_INITIALIZED);
243 return BN_mont_mod_mul(r, a, a, group->field_data1, ctx);
247 int ec_GFp_mont2_field_encode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
249 if (group->field_data1 == NULL)
251 ECerr(EC_F_EC_GFP_MONT2_FIELD_ENCODE, EC_R_NOT_INITIALIZED);
255 if (!BN_copy(r, a)) return 0;
256 return BN_to_mont(r, (BN_MONTGOMERY *)group->field_data1, ctx);
260 int ec_GFp_mont2_field_decode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
262 if (group->field_data1 == NULL)
264 ECerr(EC_F_EC_GFP_MONT2_FIELD_DECODE, EC_R_NOT_INITIALIZED);
268 if (!BN_copy(r, a)) return 0;
269 return BN_mont_red(r, (BN_MONTGOMERY *)group->field_data1);
273 int ec_GFp_mont2_field_set_to_one(const EC_GROUP *group, BIGNUM *r, BN_CTX *ctx)
275 if (group->field_data2 == NULL)
277 ECerr(EC_F_EC_GFP_MONT2_FIELD_DECODE, EC_R_NOT_INITIALIZED);
281 if (!BN_copy(r, group->field_data2)) return 0;