1109998Smarkm/* crypto/ec/ecp_nist.c */
2160814Ssimon/*
3160814Ssimon * Written by Nils Larsch for the OpenSSL project.
4160814Ssimon */
5109998Smarkm/* ====================================================================
6160814Ssimon * Copyright (c) 1998-2003 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
13280297Sjkim *    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
64160814Ssimon#include <limits.h>
65160814Ssimon
66160814Ssimon#include <openssl/err.h>
67160814Ssimon#include <openssl/obj_mac.h>
68109998Smarkm#include "ec_lcl.h"
69109998Smarkm
70238405Sjkim#ifdef OPENSSL_FIPS
71280297Sjkim# include <openssl/fips.h>
72238405Sjkim#endif
73238405Sjkim
74109998Smarkmconst EC_METHOD *EC_GFp_nist_method(void)
75280297Sjkim{
76280297Sjkim    static const EC_METHOD ret = {
77280297Sjkim        EC_FLAGS_DEFAULT_OCT,
78280297Sjkim        NID_X9_62_prime_field,
79280297Sjkim        ec_GFp_simple_group_init,
80280297Sjkim        ec_GFp_simple_group_finish,
81280297Sjkim        ec_GFp_simple_group_clear_finish,
82280297Sjkim        ec_GFp_nist_group_copy,
83280297Sjkim        ec_GFp_nist_group_set_curve,
84280297Sjkim        ec_GFp_simple_group_get_curve,
85280297Sjkim        ec_GFp_simple_group_get_degree,
86280297Sjkim        ec_GFp_simple_group_check_discriminant,
87280297Sjkim        ec_GFp_simple_point_init,
88280297Sjkim        ec_GFp_simple_point_finish,
89280297Sjkim        ec_GFp_simple_point_clear_finish,
90280297Sjkim        ec_GFp_simple_point_copy,
91280297Sjkim        ec_GFp_simple_point_set_to_infinity,
92280297Sjkim        ec_GFp_simple_set_Jprojective_coordinates_GFp,
93280297Sjkim        ec_GFp_simple_get_Jprojective_coordinates_GFp,
94280297Sjkim        ec_GFp_simple_point_set_affine_coordinates,
95280297Sjkim        ec_GFp_simple_point_get_affine_coordinates,
96280297Sjkim        0, 0, 0,
97280297Sjkim        ec_GFp_simple_add,
98280297Sjkim        ec_GFp_simple_dbl,
99280297Sjkim        ec_GFp_simple_invert,
100280297Sjkim        ec_GFp_simple_is_at_infinity,
101280297Sjkim        ec_GFp_simple_is_on_curve,
102280297Sjkim        ec_GFp_simple_cmp,
103280297Sjkim        ec_GFp_simple_make_affine,
104280297Sjkim        ec_GFp_simple_points_make_affine,
105280297Sjkim        0 /* mul */ ,
106280297Sjkim        0 /* precompute_mult */ ,
107280297Sjkim        0 /* have_precompute_mult */ ,
108280297Sjkim        ec_GFp_nist_field_mul,
109280297Sjkim        ec_GFp_nist_field_sqr,
110280297Sjkim        0 /* field_div */ ,
111280297Sjkim        0 /* field_encode */ ,
112280297Sjkim        0 /* field_decode */ ,
113280297Sjkim        0                       /* field_set_to_one */
114280297Sjkim    };
115109998Smarkm
116273144Sjkim#ifdef OPENSSL_FIPS
117280297Sjkim    if (FIPS_mode())
118280297Sjkim        return fips_ec_gfp_nist_method();
119273144Sjkim#endif
120273144Sjkim
121280297Sjkim    return &ret;
122280297Sjkim}
123160814Ssimon
124160814Ssimonint ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src)
125280297Sjkim{
126280297Sjkim    dest->field_mod_func = src->field_mod_func;
127109998Smarkm
128280297Sjkim    return ec_GFp_simple_group_copy(dest, src);
129280297Sjkim}
130109998Smarkm
131160814Ssimonint ec_GFp_nist_group_set_curve(EC_GROUP *group, const BIGNUM *p,
132280297Sjkim                                const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
133280297Sjkim{
134280297Sjkim    int ret = 0;
135280297Sjkim    BN_CTX *new_ctx = NULL;
136280297Sjkim    BIGNUM *tmp_bn;
137109998Smarkm
138280297Sjkim    if (ctx == NULL)
139280297Sjkim        if ((ctx = new_ctx = BN_CTX_new()) == NULL)
140280297Sjkim            return 0;
141109998Smarkm
142280297Sjkim    BN_CTX_start(ctx);
143280297Sjkim    if ((tmp_bn = BN_CTX_get(ctx)) == NULL)
144280297Sjkim        goto err;
145109998Smarkm
146280297Sjkim    if (BN_ucmp(BN_get0_nist_prime_192(), p) == 0)
147280297Sjkim        group->field_mod_func = BN_nist_mod_192;
148280297Sjkim    else if (BN_ucmp(BN_get0_nist_prime_224(), p) == 0)
149280297Sjkim        group->field_mod_func = BN_nist_mod_224;
150280297Sjkim    else if (BN_ucmp(BN_get0_nist_prime_256(), p) == 0)
151280297Sjkim        group->field_mod_func = BN_nist_mod_256;
152280297Sjkim    else if (BN_ucmp(BN_get0_nist_prime_384(), p) == 0)
153280297Sjkim        group->field_mod_func = BN_nist_mod_384;
154280297Sjkim    else if (BN_ucmp(BN_get0_nist_prime_521(), p) == 0)
155280297Sjkim        group->field_mod_func = BN_nist_mod_521;
156280297Sjkim    else {
157280297Sjkim        ECerr(EC_F_EC_GFP_NIST_GROUP_SET_CURVE, EC_R_NOT_A_NIST_PRIME);
158280297Sjkim        goto err;
159280297Sjkim    }
160109998Smarkm
161280297Sjkim    ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
162280297Sjkim
163160814Ssimon err:
164280297Sjkim    BN_CTX_end(ctx);
165280297Sjkim    if (new_ctx != NULL)
166280297Sjkim        BN_CTX_free(new_ctx);
167280297Sjkim    return ret;
168280297Sjkim}
169109998Smarkm
170160814Ssimonint ec_GFp_nist_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
171280297Sjkim                          const BIGNUM *b, BN_CTX *ctx)
172280297Sjkim{
173280297Sjkim    int ret = 0;
174280297Sjkim    BN_CTX *ctx_new = NULL;
175109998Smarkm
176280297Sjkim    if (!group || !r || !a || !b) {
177280297Sjkim        ECerr(EC_F_EC_GFP_NIST_FIELD_MUL, ERR_R_PASSED_NULL_PARAMETER);
178280297Sjkim        goto err;
179280297Sjkim    }
180280297Sjkim    if (!ctx)
181280297Sjkim        if ((ctx_new = ctx = BN_CTX_new()) == NULL)
182280297Sjkim            goto err;
183109998Smarkm
184280297Sjkim    if (!BN_mul(r, a, b, ctx))
185280297Sjkim        goto err;
186280297Sjkim    if (!group->field_mod_func(r, r, &group->field, ctx))
187280297Sjkim        goto err;
188109998Smarkm
189280297Sjkim    ret = 1;
190280297Sjkim err:
191280297Sjkim    if (ctx_new)
192280297Sjkim        BN_CTX_free(ctx_new);
193280297Sjkim    return ret;
194280297Sjkim}
195109998Smarkm
196160814Ssimonint ec_GFp_nist_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
197280297Sjkim                          BN_CTX *ctx)
198280297Sjkim{
199280297Sjkim    int ret = 0;
200280297Sjkim    BN_CTX *ctx_new = NULL;
201160814Ssimon
202280297Sjkim    if (!group || !r || !a) {
203280297Sjkim        ECerr(EC_F_EC_GFP_NIST_FIELD_SQR, EC_R_PASSED_NULL_PARAMETER);
204280297Sjkim        goto err;
205280297Sjkim    }
206280297Sjkim    if (!ctx)
207280297Sjkim        if ((ctx_new = ctx = BN_CTX_new()) == NULL)
208280297Sjkim            goto err;
209160814Ssimon
210280297Sjkim    if (!BN_sqr(r, a, ctx))
211280297Sjkim        goto err;
212280297Sjkim    if (!group->field_mod_func(r, r, &group->field, ctx))
213280297Sjkim        goto err;
214160814Ssimon
215280297Sjkim    ret = 1;
216280297Sjkim err:
217280297Sjkim    if (ctx_new)
218280297Sjkim        BN_CTX_free(ctx_new);
219280297Sjkim    return ret;
220280297Sjkim}
221