]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ec/ecp_mont.c
Update copyright year
[thirdparty/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 OpenSSL license (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_lcl.h"
14
15 const 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_encode,
54 ec_GFp_mont_field_decode,
55 ec_GFp_mont_field_set_to_one,
56 ec_key_simple_priv2oct,
57 ec_key_simple_oct2priv,
58 0, /* set private */
59 ec_key_simple_generate_key,
60 ec_key_simple_check_key,
61 ec_key_simple_generate_public_key,
62 0, /* keycopy */
63 0, /* keyfinish */
64 ecdh_simple_compute_key,
65 0, /* field_inverse_mod_ord */
66 ec_GFp_simple_blind_coordinates
67 };
68
69 return &ret;
70 }
71
72 int ec_GFp_mont_group_init(EC_GROUP *group)
73 {
74 int ok;
75
76 ok = ec_GFp_simple_group_init(group);
77 group->field_data1 = NULL;
78 group->field_data2 = NULL;
79 return ok;
80 }
81
82 void ec_GFp_mont_group_finish(EC_GROUP *group)
83 {
84 BN_MONT_CTX_free(group->field_data1);
85 group->field_data1 = NULL;
86 BN_free(group->field_data2);
87 group->field_data2 = NULL;
88 ec_GFp_simple_group_finish(group);
89 }
90
91 void ec_GFp_mont_group_clear_finish(EC_GROUP *group)
92 {
93 BN_MONT_CTX_free(group->field_data1);
94 group->field_data1 = NULL;
95 BN_clear_free(group->field_data2);
96 group->field_data2 = NULL;
97 ec_GFp_simple_group_clear_finish(group);
98 }
99
100 int ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src)
101 {
102 BN_MONT_CTX_free(dest->field_data1);
103 dest->field_data1 = NULL;
104 BN_clear_free(dest->field_data2);
105 dest->field_data2 = NULL;
106
107 if (!ec_GFp_simple_group_copy(dest, src))
108 return 0;
109
110 if (src->field_data1 != NULL) {
111 dest->field_data1 = BN_MONT_CTX_new();
112 if (dest->field_data1 == NULL)
113 return 0;
114 if (!BN_MONT_CTX_copy(dest->field_data1, src->field_data1))
115 goto err;
116 }
117 if (src->field_data2 != NULL) {
118 dest->field_data2 = BN_dup(src->field_data2);
119 if (dest->field_data2 == NULL)
120 goto err;
121 }
122
123 return 1;
124
125 err:
126 BN_MONT_CTX_free(dest->field_data1);
127 dest->field_data1 = NULL;
128 return 0;
129 }
130
131 int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
132 const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
133 {
134 BN_CTX *new_ctx = NULL;
135 BN_MONT_CTX *mont = NULL;
136 BIGNUM *one = NULL;
137 int ret = 0;
138
139 BN_MONT_CTX_free(group->field_data1);
140 group->field_data1 = NULL;
141 BN_free(group->field_data2);
142 group->field_data2 = NULL;
143
144 if (ctx == NULL) {
145 ctx = new_ctx = BN_CTX_new();
146 if (ctx == NULL)
147 return 0;
148 }
149
150 mont = BN_MONT_CTX_new();
151 if (mont == NULL)
152 goto err;
153 if (!BN_MONT_CTX_set(mont, p, ctx)) {
154 ECerr(EC_F_EC_GFP_MONT_GROUP_SET_CURVE, ERR_R_BN_LIB);
155 goto err;
156 }
157 one = BN_new();
158 if (one == NULL)
159 goto err;
160 if (!BN_to_montgomery(one, BN_value_one(), mont, ctx))
161 goto err;
162
163 group->field_data1 = mont;
164 mont = NULL;
165 group->field_data2 = one;
166 one = NULL;
167
168 ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
169
170 if (!ret) {
171 BN_MONT_CTX_free(group->field_data1);
172 group->field_data1 = NULL;
173 BN_free(group->field_data2);
174 group->field_data2 = NULL;
175 }
176
177 err:
178 BN_free(one);
179 BN_CTX_free(new_ctx);
180 BN_MONT_CTX_free(mont);
181 return ret;
182 }
183
184 int ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
185 const BIGNUM *b, BN_CTX *ctx)
186 {
187 if (group->field_data1 == NULL) {
188 ECerr(EC_F_EC_GFP_MONT_FIELD_MUL, EC_R_NOT_INITIALIZED);
189 return 0;
190 }
191
192 return BN_mod_mul_montgomery(r, a, b, group->field_data1, ctx);
193 }
194
195 int ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
196 BN_CTX *ctx)
197 {
198 if (group->field_data1 == NULL) {
199 ECerr(EC_F_EC_GFP_MONT_FIELD_SQR, EC_R_NOT_INITIALIZED);
200 return 0;
201 }
202
203 return BN_mod_mul_montgomery(r, a, a, group->field_data1, ctx);
204 }
205
206 int ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r,
207 const BIGNUM *a, BN_CTX *ctx)
208 {
209 if (group->field_data1 == NULL) {
210 ECerr(EC_F_EC_GFP_MONT_FIELD_ENCODE, EC_R_NOT_INITIALIZED);
211 return 0;
212 }
213
214 return BN_to_montgomery(r, a, (BN_MONT_CTX *)group->field_data1, ctx);
215 }
216
217 int ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r,
218 const BIGNUM *a, BN_CTX *ctx)
219 {
220 if (group->field_data1 == NULL) {
221 ECerr(EC_F_EC_GFP_MONT_FIELD_DECODE, EC_R_NOT_INITIALIZED);
222 return 0;
223 }
224
225 return BN_from_montgomery(r, a, group->field_data1, ctx);
226 }
227
228 int ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r,
229 BN_CTX *ctx)
230 {
231 if (group->field_data2 == NULL) {
232 ECerr(EC_F_EC_GFP_MONT_FIELD_SET_TO_ONE, EC_R_NOT_INITIALIZED);
233 return 0;
234 }
235
236 if (!BN_copy(r, group->field_data2))
237 return 0;
238 return 1;
239 }