Deprecate the ECDSA and EV_KEY_METHOD functions.
[openssl.git] / crypto / ec / ecp_mont.c
1 /*
2  * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4  *
5  * Licensed under the Apache License 2.0 (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  */
10
11 /*
12  * ECDSA low level APIs are deprecated for public use, but still ok for
13  * internal use.
14  */
15 #include "internal/deprecated.h"
16
17 #include <openssl/err.h>
18
19 #include "ec_local.h"
20
21 const EC_METHOD *EC_GFp_mont_method(void)
22 {
23     static const EC_METHOD ret = {
24         EC_FLAGS_DEFAULT_OCT,
25         NID_X9_62_prime_field,
26         ec_GFp_mont_group_init,
27         ec_GFp_mont_group_finish,
28         ec_GFp_mont_group_clear_finish,
29         ec_GFp_mont_group_copy,
30         ec_GFp_mont_group_set_curve,
31         ec_GFp_simple_group_get_curve,
32         ec_GFp_simple_group_get_degree,
33         ec_group_simple_order_bits,
34         ec_GFp_simple_group_check_discriminant,
35         ec_GFp_simple_point_init,
36         ec_GFp_simple_point_finish,
37         ec_GFp_simple_point_clear_finish,
38         ec_GFp_simple_point_copy,
39         ec_GFp_simple_point_set_to_infinity,
40         ec_GFp_simple_set_Jprojective_coordinates_GFp,
41         ec_GFp_simple_get_Jprojective_coordinates_GFp,
42         ec_GFp_simple_point_set_affine_coordinates,
43         ec_GFp_simple_point_get_affine_coordinates,
44         0, 0, 0,
45         ec_GFp_simple_add,
46         ec_GFp_simple_dbl,
47         ec_GFp_simple_invert,
48         ec_GFp_simple_is_at_infinity,
49         ec_GFp_simple_is_on_curve,
50         ec_GFp_simple_cmp,
51         ec_GFp_simple_make_affine,
52         ec_GFp_simple_points_make_affine,
53         0 /* mul */ ,
54         0 /* precompute_mult */ ,
55         0 /* have_precompute_mult */ ,
56         ec_GFp_mont_field_mul,
57         ec_GFp_mont_field_sqr,
58         0 /* field_div */ ,
59         ec_GFp_mont_field_inv,
60         ec_GFp_mont_field_encode,
61         ec_GFp_mont_field_decode,
62         ec_GFp_mont_field_set_to_one,
63         ec_key_simple_priv2oct,
64         ec_key_simple_oct2priv,
65         0, /* set private */
66         ec_key_simple_generate_key,
67         ec_key_simple_check_key,
68         ec_key_simple_generate_public_key,
69         0, /* keycopy */
70         0, /* keyfinish */
71         ecdh_simple_compute_key,
72         ecdsa_simple_sign_setup,
73         ecdsa_simple_sign_sig,
74         ecdsa_simple_verify_sig,
75         0, /* field_inverse_mod_ord */
76         ec_GFp_simple_blind_coordinates,
77         ec_GFp_simple_ladder_pre,
78         ec_GFp_simple_ladder_step,
79         ec_GFp_simple_ladder_post
80     };
81
82     return &ret;
83 }
84
85 int ec_GFp_mont_group_init(EC_GROUP *group)
86 {
87     int ok;
88
89     ok = ec_GFp_simple_group_init(group);
90     group->field_data1 = NULL;
91     group->field_data2 = NULL;
92     return ok;
93 }
94
95 void ec_GFp_mont_group_finish(EC_GROUP *group)
96 {
97     BN_MONT_CTX_free(group->field_data1);
98     group->field_data1 = NULL;
99     BN_free(group->field_data2);
100     group->field_data2 = NULL;
101     ec_GFp_simple_group_finish(group);
102 }
103
104 void ec_GFp_mont_group_clear_finish(EC_GROUP *group)
105 {
106     BN_MONT_CTX_free(group->field_data1);
107     group->field_data1 = NULL;
108     BN_clear_free(group->field_data2);
109     group->field_data2 = NULL;
110     ec_GFp_simple_group_clear_finish(group);
111 }
112
113 int ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src)
114 {
115     BN_MONT_CTX_free(dest->field_data1);
116     dest->field_data1 = NULL;
117     BN_clear_free(dest->field_data2);
118     dest->field_data2 = NULL;
119
120     if (!ec_GFp_simple_group_copy(dest, src))
121         return 0;
122
123     if (src->field_data1 != NULL) {
124         dest->field_data1 = BN_MONT_CTX_new();
125         if (dest->field_data1 == NULL)
126             return 0;
127         if (!BN_MONT_CTX_copy(dest->field_data1, src->field_data1))
128             goto err;
129     }
130     if (src->field_data2 != NULL) {
131         dest->field_data2 = BN_dup(src->field_data2);
132         if (dest->field_data2 == NULL)
133             goto err;
134     }
135
136     return 1;
137
138  err:
139     BN_MONT_CTX_free(dest->field_data1);
140     dest->field_data1 = NULL;
141     return 0;
142 }
143
144 int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
145                                 const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
146 {
147     BN_CTX *new_ctx = NULL;
148     BN_MONT_CTX *mont = NULL;
149     BIGNUM *one = NULL;
150     int ret = 0;
151
152     BN_MONT_CTX_free(group->field_data1);
153     group->field_data1 = NULL;
154     BN_free(group->field_data2);
155     group->field_data2 = NULL;
156
157     if (ctx == NULL) {
158         ctx = new_ctx = BN_CTX_new_ex(group->libctx);
159         if (ctx == NULL)
160             return 0;
161     }
162
163     mont = BN_MONT_CTX_new();
164     if (mont == NULL)
165         goto err;
166     if (!BN_MONT_CTX_set(mont, p, ctx)) {
167         ECerr(EC_F_EC_GFP_MONT_GROUP_SET_CURVE, ERR_R_BN_LIB);
168         goto err;
169     }
170     one = BN_new();
171     if (one == NULL)
172         goto err;
173     if (!BN_to_montgomery(one, BN_value_one(), mont, ctx))
174         goto err;
175
176     group->field_data1 = mont;
177     mont = NULL;
178     group->field_data2 = one;
179     one = NULL;
180
181     ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
182
183     if (!ret) {
184         BN_MONT_CTX_free(group->field_data1);
185         group->field_data1 = NULL;
186         BN_free(group->field_data2);
187         group->field_data2 = NULL;
188     }
189
190  err:
191     BN_free(one);
192     BN_CTX_free(new_ctx);
193     BN_MONT_CTX_free(mont);
194     return ret;
195 }
196
197 int ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
198                           const BIGNUM *b, BN_CTX *ctx)
199 {
200     if (group->field_data1 == NULL) {
201         ECerr(EC_F_EC_GFP_MONT_FIELD_MUL, EC_R_NOT_INITIALIZED);
202         return 0;
203     }
204
205     return BN_mod_mul_montgomery(r, a, b, group->field_data1, ctx);
206 }
207
208 int ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
209                           BN_CTX *ctx)
210 {
211     if (group->field_data1 == NULL) {
212         ECerr(EC_F_EC_GFP_MONT_FIELD_SQR, EC_R_NOT_INITIALIZED);
213         return 0;
214     }
215
216     return BN_mod_mul_montgomery(r, a, a, group->field_data1, ctx);
217 }
218
219 /*-
220  * Computes the multiplicative inverse of a in GF(p), storing the result in r.
221  * If a is zero (or equivalent), you'll get a EC_R_CANNOT_INVERT error.
222  * We have a Mont structure, so SCA hardening is FLT inversion.
223  */
224 int ec_GFp_mont_field_inv(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
225                             BN_CTX *ctx)
226 {
227     BIGNUM *e = NULL;
228     BN_CTX *new_ctx = NULL;
229     int ret = 0;
230
231     if (group->field_data1 == NULL)
232         return 0;
233
234     if (ctx == NULL
235             && (ctx = new_ctx = BN_CTX_secure_new_ex(group->libctx)) == NULL)
236         return 0;
237
238     BN_CTX_start(ctx);
239     if ((e = BN_CTX_get(ctx)) == NULL)
240         goto err;
241
242     /* Inverse in constant time with Fermats Little Theorem */
243     if (!BN_set_word(e, 2))
244         goto err;
245     if (!BN_sub(e, group->field, e))
246         goto err;
247     /*-
248      * Exponent e is public.
249      * No need for scatter-gather or BN_FLG_CONSTTIME.
250      */
251     if (!BN_mod_exp_mont(r, a, e, group->field, ctx, group->field_data1))
252         goto err;
253
254     /* throw an error on zero */
255     if (BN_is_zero(r)) {
256         ECerr(EC_F_EC_GFP_MONT_FIELD_INV, EC_R_CANNOT_INVERT);
257         goto err;
258     }
259
260     ret = 1;
261
262   err:
263     BN_CTX_end(ctx);
264     BN_CTX_free(new_ctx);
265     return ret;
266 }
267
268 int ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r,
269                              const BIGNUM *a, BN_CTX *ctx)
270 {
271     if (group->field_data1 == NULL) {
272         ECerr(EC_F_EC_GFP_MONT_FIELD_ENCODE, EC_R_NOT_INITIALIZED);
273         return 0;
274     }
275
276     return BN_to_montgomery(r, a, (BN_MONT_CTX *)group->field_data1, ctx);
277 }
278
279 int ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r,
280                              const BIGNUM *a, BN_CTX *ctx)
281 {
282     if (group->field_data1 == NULL) {
283         ECerr(EC_F_EC_GFP_MONT_FIELD_DECODE, EC_R_NOT_INITIALIZED);
284         return 0;
285     }
286
287     return BN_from_montgomery(r, a, group->field_data1, ctx);
288 }
289
290 int ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r,
291                                  BN_CTX *ctx)
292 {
293     if (group->field_data2 == NULL) {
294         ECerr(EC_F_EC_GFP_MONT_FIELD_SET_TO_ONE, EC_R_NOT_INITIALIZED);
295         return 0;
296     }
297
298     if (!BN_copy(r, group->field_data2))
299         return 0;
300     return 1;
301 }