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