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
13296341Sdelphij *    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
66238405Sjkim#ifdef OPENSSL_FIPS
67296341Sdelphij# include <openssl/fips.h>
68238405Sjkim#endif
69238405Sjkim
70109998Smarkm#include "ec_lcl.h"
71109998Smarkm
72109998Smarkmconst EC_METHOD *EC_GFp_mont_method(void)
73296341Sdelphij{
74296341Sdelphij    static const EC_METHOD ret = {
75296341Sdelphij        EC_FLAGS_DEFAULT_OCT,
76296341Sdelphij        NID_X9_62_prime_field,
77296341Sdelphij        ec_GFp_mont_group_init,
78296341Sdelphij        ec_GFp_mont_group_finish,
79296341Sdelphij        ec_GFp_mont_group_clear_finish,
80296341Sdelphij        ec_GFp_mont_group_copy,
81296341Sdelphij        ec_GFp_mont_group_set_curve,
82296341Sdelphij        ec_GFp_simple_group_get_curve,
83296341Sdelphij        ec_GFp_simple_group_get_degree,
84296341Sdelphij        ec_GFp_simple_group_check_discriminant,
85296341Sdelphij        ec_GFp_simple_point_init,
86296341Sdelphij        ec_GFp_simple_point_finish,
87296341Sdelphij        ec_GFp_simple_point_clear_finish,
88296341Sdelphij        ec_GFp_simple_point_copy,
89296341Sdelphij        ec_GFp_simple_point_set_to_infinity,
90296341Sdelphij        ec_GFp_simple_set_Jprojective_coordinates_GFp,
91296341Sdelphij        ec_GFp_simple_get_Jprojective_coordinates_GFp,
92296341Sdelphij        ec_GFp_simple_point_set_affine_coordinates,
93296341Sdelphij        ec_GFp_simple_point_get_affine_coordinates,
94296341Sdelphij        0, 0, 0,
95296341Sdelphij        ec_GFp_simple_add,
96296341Sdelphij        ec_GFp_simple_dbl,
97296341Sdelphij        ec_GFp_simple_invert,
98296341Sdelphij        ec_GFp_simple_is_at_infinity,
99296341Sdelphij        ec_GFp_simple_is_on_curve,
100296341Sdelphij        ec_GFp_simple_cmp,
101296341Sdelphij        ec_GFp_simple_make_affine,
102296341Sdelphij        ec_GFp_simple_points_make_affine,
103296341Sdelphij        0 /* mul */ ,
104296341Sdelphij        0 /* precompute_mult */ ,
105296341Sdelphij        0 /* have_precompute_mult */ ,
106296341Sdelphij        ec_GFp_mont_field_mul,
107296341Sdelphij        ec_GFp_mont_field_sqr,
108296341Sdelphij        0 /* field_div */ ,
109296341Sdelphij        ec_GFp_mont_field_encode,
110296341Sdelphij        ec_GFp_mont_field_decode,
111296341Sdelphij        ec_GFp_mont_field_set_to_one
112296341Sdelphij    };
113109998Smarkm
114273399Sdelphij#ifdef OPENSSL_FIPS
115296341Sdelphij    if (FIPS_mode())
116296341Sdelphij        return fips_ec_gfp_mont_method();
117273399Sdelphij#endif
118273399Sdelphij
119296341Sdelphij    return &ret;
120296341Sdelphij}
121109998Smarkm
122109998Smarkmint ec_GFp_mont_group_init(EC_GROUP *group)
123296341Sdelphij{
124296341Sdelphij    int ok;
125109998Smarkm
126296341Sdelphij    ok = ec_GFp_simple_group_init(group);
127296341Sdelphij    group->field_data1 = NULL;
128296341Sdelphij    group->field_data2 = NULL;
129296341Sdelphij    return ok;
130296341Sdelphij}
131109998Smarkm
132109998Smarkmvoid ec_GFp_mont_group_finish(EC_GROUP *group)
133296341Sdelphij{
134296341Sdelphij    if (group->field_data1 != NULL) {
135296341Sdelphij        BN_MONT_CTX_free(group->field_data1);
136296341Sdelphij        group->field_data1 = NULL;
137296341Sdelphij    }
138296341Sdelphij    if (group->field_data2 != NULL) {
139296341Sdelphij        BN_free(group->field_data2);
140296341Sdelphij        group->field_data2 = NULL;
141296341Sdelphij    }
142296341Sdelphij    ec_GFp_simple_group_finish(group);
143296341Sdelphij}
144109998Smarkm
145109998Smarkmvoid ec_GFp_mont_group_clear_finish(EC_GROUP *group)
146296341Sdelphij{
147296341Sdelphij    if (group->field_data1 != NULL) {
148296341Sdelphij        BN_MONT_CTX_free(group->field_data1);
149296341Sdelphij        group->field_data1 = NULL;
150296341Sdelphij    }
151296341Sdelphij    if (group->field_data2 != NULL) {
152296341Sdelphij        BN_clear_free(group->field_data2);
153296341Sdelphij        group->field_data2 = NULL;
154296341Sdelphij    }
155296341Sdelphij    ec_GFp_simple_group_clear_finish(group);
156296341Sdelphij}
157109998Smarkm
158109998Smarkmint ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src)
159296341Sdelphij{
160296341Sdelphij    if (dest->field_data1 != NULL) {
161296341Sdelphij        BN_MONT_CTX_free(dest->field_data1);
162296341Sdelphij        dest->field_data1 = NULL;
163296341Sdelphij    }
164296341Sdelphij    if (dest->field_data2 != NULL) {
165296341Sdelphij        BN_clear_free(dest->field_data2);
166296341Sdelphij        dest->field_data2 = NULL;
167296341Sdelphij    }
168109998Smarkm
169296341Sdelphij    if (!ec_GFp_simple_group_copy(dest, src))
170296341Sdelphij        return 0;
171109998Smarkm
172296341Sdelphij    if (src->field_data1 != NULL) {
173296341Sdelphij        dest->field_data1 = BN_MONT_CTX_new();
174296341Sdelphij        if (dest->field_data1 == NULL)
175296341Sdelphij            return 0;
176296341Sdelphij        if (!BN_MONT_CTX_copy(dest->field_data1, src->field_data1))
177296341Sdelphij            goto err;
178296341Sdelphij    }
179296341Sdelphij    if (src->field_data2 != NULL) {
180296341Sdelphij        dest->field_data2 = BN_dup(src->field_data2);
181296341Sdelphij        if (dest->field_data2 == NULL)
182296341Sdelphij            goto err;
183296341Sdelphij    }
184109998Smarkm
185296341Sdelphij    return 1;
186109998Smarkm
187109998Smarkm err:
188296341Sdelphij    if (dest->field_data1 != NULL) {
189296341Sdelphij        BN_MONT_CTX_free(dest->field_data1);
190296341Sdelphij        dest->field_data1 = NULL;
191296341Sdelphij    }
192296341Sdelphij    return 0;
193296341Sdelphij}
194109998Smarkm
195296341Sdelphijint ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
196296341Sdelphij                                const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
197296341Sdelphij{
198296341Sdelphij    BN_CTX *new_ctx = NULL;
199296341Sdelphij    BN_MONT_CTX *mont = NULL;
200296341Sdelphij    BIGNUM *one = NULL;
201296341Sdelphij    int ret = 0;
202109998Smarkm
203296341Sdelphij    if (group->field_data1 != NULL) {
204296341Sdelphij        BN_MONT_CTX_free(group->field_data1);
205296341Sdelphij        group->field_data1 = NULL;
206296341Sdelphij    }
207296341Sdelphij    if (group->field_data2 != NULL) {
208296341Sdelphij        BN_free(group->field_data2);
209296341Sdelphij        group->field_data2 = NULL;
210296341Sdelphij    }
211160814Ssimon
212296341Sdelphij    if (ctx == NULL) {
213296341Sdelphij        ctx = new_ctx = BN_CTX_new();
214296341Sdelphij        if (ctx == NULL)
215296341Sdelphij            return 0;
216296341Sdelphij    }
217160814Ssimon
218296341Sdelphij    mont = BN_MONT_CTX_new();
219296341Sdelphij    if (mont == NULL)
220296341Sdelphij        goto err;
221296341Sdelphij    if (!BN_MONT_CTX_set(mont, p, ctx)) {
222296341Sdelphij        ECerr(EC_F_EC_GFP_MONT_GROUP_SET_CURVE, ERR_R_BN_LIB);
223296341Sdelphij        goto err;
224296341Sdelphij    }
225296341Sdelphij    one = BN_new();
226296341Sdelphij    if (one == NULL)
227296341Sdelphij        goto err;
228296341Sdelphij    if (!BN_to_montgomery(one, BN_value_one(), mont, ctx))
229296341Sdelphij        goto err;
230160814Ssimon
231296341Sdelphij    group->field_data1 = mont;
232296341Sdelphij    mont = NULL;
233296341Sdelphij    group->field_data2 = one;
234296341Sdelphij    one = NULL;
235160814Ssimon
236296341Sdelphij    ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
237160814Ssimon
238296341Sdelphij    if (!ret) {
239296341Sdelphij        BN_MONT_CTX_free(group->field_data1);
240296341Sdelphij        group->field_data1 = NULL;
241296341Sdelphij        BN_free(group->field_data2);
242296341Sdelphij        group->field_data2 = NULL;
243296341Sdelphij    }
244160814Ssimon
245160814Ssimon err:
246296341Sdelphij    if (new_ctx != NULL)
247296341Sdelphij        BN_CTX_free(new_ctx);
248296341Sdelphij    if (mont != NULL)
249296341Sdelphij        BN_MONT_CTX_free(mont);
250296341Sdelphij    return ret;
251296341Sdelphij}
252160814Ssimon
253296341Sdelphijint ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
254296341Sdelphij                          const BIGNUM *b, BN_CTX *ctx)
255296341Sdelphij{
256296341Sdelphij    if (group->field_data1 == NULL) {
257296341Sdelphij        ECerr(EC_F_EC_GFP_MONT_FIELD_MUL, EC_R_NOT_INITIALIZED);
258296341Sdelphij        return 0;
259296341Sdelphij    }
260160814Ssimon
261296341Sdelphij    return BN_mod_mul_montgomery(r, a, b, group->field_data1, ctx);
262296341Sdelphij}
263109998Smarkm
264296341Sdelphijint ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
265296341Sdelphij                          BN_CTX *ctx)
266296341Sdelphij{
267296341Sdelphij    if (group->field_data1 == NULL) {
268296341Sdelphij        ECerr(EC_F_EC_GFP_MONT_FIELD_SQR, EC_R_NOT_INITIALIZED);
269296341Sdelphij        return 0;
270296341Sdelphij    }
271109998Smarkm
272296341Sdelphij    return BN_mod_mul_montgomery(r, a, a, group->field_data1, ctx);
273296341Sdelphij}
274109998Smarkm
275296341Sdelphijint ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r,
276296341Sdelphij                             const BIGNUM *a, BN_CTX *ctx)
277296341Sdelphij{
278296341Sdelphij    if (group->field_data1 == NULL) {
279296341Sdelphij        ECerr(EC_F_EC_GFP_MONT_FIELD_ENCODE, EC_R_NOT_INITIALIZED);
280296341Sdelphij        return 0;
281296341Sdelphij    }
282109998Smarkm
283296341Sdelphij    return BN_to_montgomery(r, a, (BN_MONT_CTX *)group->field_data1, ctx);
284296341Sdelphij}
285109998Smarkm
286296341Sdelphijint ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r,
287296341Sdelphij                             const BIGNUM *a, BN_CTX *ctx)
288296341Sdelphij{
289296341Sdelphij    if (group->field_data1 == NULL) {
290296341Sdelphij        ECerr(EC_F_EC_GFP_MONT_FIELD_DECODE, EC_R_NOT_INITIALIZED);
291296341Sdelphij        return 0;
292296341Sdelphij    }
293109998Smarkm
294296341Sdelphij    return BN_from_montgomery(r, a, group->field_data1, ctx);
295296341Sdelphij}
296109998Smarkm
297296341Sdelphijint ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r,
298296341Sdelphij                                 BN_CTX *ctx)
299296341Sdelphij{
300296341Sdelphij    if (group->field_data2 == NULL) {
301296341Sdelphij        ECerr(EC_F_EC_GFP_MONT_FIELD_SET_TO_ONE, EC_R_NOT_INITIALIZED);
302296341Sdelphij        return 0;
303296341Sdelphij    }
304109998Smarkm
305296341Sdelphij    if (!BN_copy(r, group->field_data2))
306296341Sdelphij        return 0;
307296341Sdelphij    return 1;
308296341Sdelphij}
309