ecp_mont.c revision 160814
1109998Smarkm/* crypto/ec/ecp_mont.c */
2160814Ssimon/*
3160814Ssimon * Originally written by Bodo Moeller for the OpenSSL project.
4160814Ssimon */
5109998Smarkm/* ====================================================================
6109998Smarkm * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
7109998Smarkm *
8109998Smarkm * Redistribution and use in source and binary forms, with or without
9109998Smarkm * modification, are permitted provided that the following conditions
10109998Smarkm * are met:
11109998Smarkm *
12109998Smarkm * 1. Redistributions of source code must retain the above copyright
13109998Smarkm *    notice, this list of conditions and the following disclaimer.
14109998Smarkm *
15109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright
16109998Smarkm *    notice, this list of conditions and the following disclaimer in
17109998Smarkm *    the documentation and/or other materials provided with the
18109998Smarkm *    distribution.
19109998Smarkm *
20109998Smarkm * 3. All advertising materials mentioning features or use of this
21109998Smarkm *    software must display the following acknowledgment:
22109998Smarkm *    "This product includes software developed by the OpenSSL Project
23109998Smarkm *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24109998Smarkm *
25109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26109998Smarkm *    endorse or promote products derived from this software without
27109998Smarkm *    prior written permission. For written permission, please contact
28109998Smarkm *    openssl-core@openssl.org.
29109998Smarkm *
30109998Smarkm * 5. Products derived from this software may not be called "OpenSSL"
31109998Smarkm *    nor may "OpenSSL" appear in their names without prior written
32109998Smarkm *    permission of the OpenSSL Project.
33109998Smarkm *
34109998Smarkm * 6. Redistributions of any form whatsoever must retain the following
35109998Smarkm *    acknowledgment:
36109998Smarkm *    "This product includes software developed by the OpenSSL Project
37109998Smarkm *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38109998Smarkm *
39109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42109998Smarkm * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE.
51109998Smarkm * ====================================================================
52109998Smarkm *
53109998Smarkm * This product includes cryptographic software written by Eric Young
54109998Smarkm * (eay@cryptsoft.com).  This product includes software written by Tim
55109998Smarkm * Hudson (tjh@cryptsoft.com).
56109998Smarkm *
57109998Smarkm */
58160814Ssimon/* ====================================================================
59160814Ssimon * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60160814Ssimon * Portions of this software developed by SUN MICROSYSTEMS, INC.,
61160814Ssimon * and contributed to the OpenSSL project.
62160814Ssimon */
63109998Smarkm
64109998Smarkm#include <openssl/err.h>
65109998Smarkm
66109998Smarkm#include "ec_lcl.h"
67109998Smarkm
68109998Smarkm
69109998Smarkmconst EC_METHOD *EC_GFp_mont_method(void)
70109998Smarkm	{
71109998Smarkm	static const EC_METHOD ret = {
72160814Ssimon		NID_X9_62_prime_field,
73109998Smarkm		ec_GFp_mont_group_init,
74109998Smarkm		ec_GFp_mont_group_finish,
75109998Smarkm		ec_GFp_mont_group_clear_finish,
76109998Smarkm		ec_GFp_mont_group_copy,
77160814Ssimon		ec_GFp_mont_group_set_curve,
78160814Ssimon		ec_GFp_simple_group_get_curve,
79160814Ssimon		ec_GFp_simple_group_get_degree,
80160814Ssimon		ec_GFp_simple_group_check_discriminant,
81109998Smarkm		ec_GFp_simple_point_init,
82109998Smarkm		ec_GFp_simple_point_finish,
83109998Smarkm		ec_GFp_simple_point_clear_finish,
84109998Smarkm		ec_GFp_simple_point_copy,
85109998Smarkm		ec_GFp_simple_point_set_to_infinity,
86109998Smarkm		ec_GFp_simple_set_Jprojective_coordinates_GFp,
87109998Smarkm		ec_GFp_simple_get_Jprojective_coordinates_GFp,
88160814Ssimon		ec_GFp_simple_point_set_affine_coordinates,
89160814Ssimon		ec_GFp_simple_point_get_affine_coordinates,
90160814Ssimon		ec_GFp_simple_set_compressed_coordinates,
91109998Smarkm		ec_GFp_simple_point2oct,
92109998Smarkm		ec_GFp_simple_oct2point,
93109998Smarkm		ec_GFp_simple_add,
94109998Smarkm		ec_GFp_simple_dbl,
95109998Smarkm		ec_GFp_simple_invert,
96109998Smarkm		ec_GFp_simple_is_at_infinity,
97109998Smarkm		ec_GFp_simple_is_on_curve,
98109998Smarkm		ec_GFp_simple_cmp,
99109998Smarkm		ec_GFp_simple_make_affine,
100109998Smarkm		ec_GFp_simple_points_make_affine,
101160814Ssimon		0 /* mul */,
102160814Ssimon		0 /* precompute_mult */,
103160814Ssimon		0 /* have_precompute_mult */,
104109998Smarkm		ec_GFp_mont_field_mul,
105109998Smarkm		ec_GFp_mont_field_sqr,
106160814Ssimon		0 /* field_div */,
107109998Smarkm		ec_GFp_mont_field_encode,
108109998Smarkm		ec_GFp_mont_field_decode,
109109998Smarkm		ec_GFp_mont_field_set_to_one };
110109998Smarkm
111109998Smarkm	return &ret;
112109998Smarkm	}
113109998Smarkm
114109998Smarkm
115109998Smarkmint ec_GFp_mont_group_init(EC_GROUP *group)
116109998Smarkm	{
117109998Smarkm	int ok;
118109998Smarkm
119109998Smarkm	ok = ec_GFp_simple_group_init(group);
120109998Smarkm	group->field_data1 = NULL;
121109998Smarkm	group->field_data2 = NULL;
122109998Smarkm	return ok;
123109998Smarkm	}
124109998Smarkm
125109998Smarkm
126109998Smarkmvoid ec_GFp_mont_group_finish(EC_GROUP *group)
127109998Smarkm	{
128109998Smarkm	if (group->field_data1 != NULL)
129109998Smarkm		{
130109998Smarkm		BN_MONT_CTX_free(group->field_data1);
131109998Smarkm		group->field_data1 = NULL;
132109998Smarkm		}
133109998Smarkm	if (group->field_data2 != NULL)
134109998Smarkm		{
135109998Smarkm		BN_free(group->field_data2);
136109998Smarkm		group->field_data2 = NULL;
137109998Smarkm		}
138109998Smarkm	ec_GFp_simple_group_finish(group);
139109998Smarkm	}
140109998Smarkm
141109998Smarkm
142109998Smarkmvoid ec_GFp_mont_group_clear_finish(EC_GROUP *group)
143109998Smarkm	{
144109998Smarkm	if (group->field_data1 != NULL)
145109998Smarkm		{
146109998Smarkm		BN_MONT_CTX_free(group->field_data1);
147109998Smarkm		group->field_data1 = NULL;
148109998Smarkm		}
149109998Smarkm	if (group->field_data2 != NULL)
150109998Smarkm		{
151109998Smarkm		BN_clear_free(group->field_data2);
152109998Smarkm		group->field_data2 = NULL;
153109998Smarkm		}
154109998Smarkm	ec_GFp_simple_group_clear_finish(group);
155109998Smarkm	}
156109998Smarkm
157109998Smarkm
158109998Smarkmint ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src)
159109998Smarkm	{
160109998Smarkm	if (dest->field_data1 != NULL)
161109998Smarkm		{
162109998Smarkm		BN_MONT_CTX_free(dest->field_data1);
163109998Smarkm		dest->field_data1 = NULL;
164109998Smarkm		}
165109998Smarkm	if (dest->field_data2 != NULL)
166109998Smarkm		{
167109998Smarkm		BN_clear_free(dest->field_data2);
168109998Smarkm		dest->field_data2 = NULL;
169109998Smarkm		}
170109998Smarkm
171109998Smarkm	if (!ec_GFp_simple_group_copy(dest, src)) return 0;
172109998Smarkm
173109998Smarkm	if (src->field_data1 != NULL)
174109998Smarkm		{
175109998Smarkm		dest->field_data1 = BN_MONT_CTX_new();
176109998Smarkm		if (dest->field_data1 == NULL) return 0;
177109998Smarkm		if (!BN_MONT_CTX_copy(dest->field_data1, src->field_data1)) goto err;
178109998Smarkm		}
179109998Smarkm	if (src->field_data2 != NULL)
180109998Smarkm		{
181109998Smarkm		dest->field_data2 = BN_dup(src->field_data2);
182109998Smarkm		if (dest->field_data2 == NULL) goto err;
183109998Smarkm		}
184109998Smarkm
185109998Smarkm	return 1;
186109998Smarkm
187109998Smarkm err:
188109998Smarkm	if (dest->field_data1 != NULL)
189109998Smarkm		{
190109998Smarkm		BN_MONT_CTX_free(dest->field_data1);
191109998Smarkm		dest->field_data1 = NULL;
192109998Smarkm		}
193109998Smarkm	return 0;
194109998Smarkm	}
195109998Smarkm
196109998Smarkm
197160814Ssimonint ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
198160814Ssimon	{
199160814Ssimon	BN_CTX *new_ctx = NULL;
200160814Ssimon	BN_MONT_CTX *mont = NULL;
201160814Ssimon	BIGNUM *one = NULL;
202160814Ssimon	int ret = 0;
203160814Ssimon
204160814Ssimon	if (group->field_data1 != NULL)
205160814Ssimon		{
206160814Ssimon		BN_MONT_CTX_free(group->field_data1);
207160814Ssimon		group->field_data1 = NULL;
208160814Ssimon		}
209160814Ssimon	if (group->field_data2 != NULL)
210160814Ssimon		{
211160814Ssimon		BN_free(group->field_data2);
212160814Ssimon		group->field_data2 = NULL;
213160814Ssimon		}
214160814Ssimon
215160814Ssimon	if (ctx == NULL)
216160814Ssimon		{
217160814Ssimon		ctx = new_ctx = BN_CTX_new();
218160814Ssimon		if (ctx == NULL)
219160814Ssimon			return 0;
220160814Ssimon		}
221160814Ssimon
222160814Ssimon	mont = BN_MONT_CTX_new();
223160814Ssimon	if (mont == NULL) goto err;
224160814Ssimon	if (!BN_MONT_CTX_set(mont, p, ctx))
225160814Ssimon		{
226160814Ssimon		ECerr(EC_F_EC_GFP_MONT_GROUP_SET_CURVE, ERR_R_BN_LIB);
227160814Ssimon		goto err;
228160814Ssimon		}
229160814Ssimon	one = BN_new();
230160814Ssimon	if (one == NULL) goto err;
231160814Ssimon	if (!BN_to_montgomery(one, BN_value_one(), mont, ctx)) goto err;
232160814Ssimon
233160814Ssimon	group->field_data1 = mont;
234160814Ssimon	mont = NULL;
235160814Ssimon	group->field_data2 = one;
236160814Ssimon	one = NULL;
237160814Ssimon
238160814Ssimon	ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
239160814Ssimon
240160814Ssimon	if (!ret)
241160814Ssimon		{
242160814Ssimon		BN_MONT_CTX_free(group->field_data1);
243160814Ssimon		group->field_data1 = NULL;
244160814Ssimon		BN_free(group->field_data2);
245160814Ssimon		group->field_data2 = NULL;
246160814Ssimon		}
247160814Ssimon
248160814Ssimon err:
249160814Ssimon	if (new_ctx != NULL)
250160814Ssimon		BN_CTX_free(new_ctx);
251160814Ssimon	if (mont != NULL)
252160814Ssimon		BN_MONT_CTX_free(mont);
253160814Ssimon	return ret;
254160814Ssimon	}
255160814Ssimon
256160814Ssimon
257109998Smarkmint ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
258109998Smarkm	{
259109998Smarkm	if (group->field_data1 == NULL)
260109998Smarkm		{
261109998Smarkm		ECerr(EC_F_EC_GFP_MONT_FIELD_MUL, EC_R_NOT_INITIALIZED);
262109998Smarkm		return 0;
263109998Smarkm		}
264109998Smarkm
265109998Smarkm	return BN_mod_mul_montgomery(r, a, b, group->field_data1, ctx);
266109998Smarkm	}
267109998Smarkm
268109998Smarkm
269109998Smarkmint ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
270109998Smarkm	{
271109998Smarkm	if (group->field_data1 == NULL)
272109998Smarkm		{
273109998Smarkm		ECerr(EC_F_EC_GFP_MONT_FIELD_SQR, EC_R_NOT_INITIALIZED);
274109998Smarkm		return 0;
275109998Smarkm		}
276109998Smarkm
277109998Smarkm	return BN_mod_mul_montgomery(r, a, a, group->field_data1, ctx);
278109998Smarkm	}
279109998Smarkm
280109998Smarkm
281109998Smarkmint ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
282109998Smarkm	{
283109998Smarkm	if (group->field_data1 == NULL)
284109998Smarkm		{
285109998Smarkm		ECerr(EC_F_EC_GFP_MONT_FIELD_ENCODE, EC_R_NOT_INITIALIZED);
286109998Smarkm		return 0;
287109998Smarkm		}
288109998Smarkm
289109998Smarkm	return BN_to_montgomery(r, a, (BN_MONT_CTX *)group->field_data1, ctx);
290109998Smarkm	}
291109998Smarkm
292109998Smarkm
293109998Smarkmint ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
294109998Smarkm	{
295109998Smarkm	if (group->field_data1 == NULL)
296109998Smarkm		{
297109998Smarkm		ECerr(EC_F_EC_GFP_MONT_FIELD_DECODE, EC_R_NOT_INITIALIZED);
298109998Smarkm		return 0;
299109998Smarkm		}
300109998Smarkm
301109998Smarkm	return BN_from_montgomery(r, a, group->field_data1, ctx);
302109998Smarkm	}
303109998Smarkm
304109998Smarkm
305109998Smarkmint ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r, BN_CTX *ctx)
306109998Smarkm	{
307109998Smarkm	if (group->field_data2 == NULL)
308109998Smarkm		{
309160814Ssimon		ECerr(EC_F_EC_GFP_MONT_FIELD_SET_TO_ONE, EC_R_NOT_INITIALIZED);
310109998Smarkm		return 0;
311109998Smarkm		}
312109998Smarkm
313109998Smarkm	if (!BN_copy(r, group->field_data2)) return 0;
314109998Smarkm	return 1;
315109998Smarkm	}
316