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