/src/openssl/crypto/ec/ecp_mont.c
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
17 | 0 | static const EC_METHOD ret = { |
18 | 0 | EC_FLAGS_DEFAULT_OCT, |
19 | 0 | NID_X9_62_prime_field, |
20 | 0 | ec_GFp_mont_group_init, |
21 | 0 | ec_GFp_mont_group_finish, |
22 | 0 | ec_GFp_mont_group_clear_finish, |
23 | 0 | ec_GFp_mont_group_copy, |
24 | 0 | ec_GFp_mont_group_set_curve, |
25 | 0 | ec_GFp_simple_group_get_curve, |
26 | 0 | ec_GFp_simple_group_get_degree, |
27 | 0 | ec_group_simple_order_bits, |
28 | 0 | ec_GFp_simple_group_check_discriminant, |
29 | 0 | ec_GFp_simple_point_init, |
30 | 0 | ec_GFp_simple_point_finish, |
31 | 0 | ec_GFp_simple_point_clear_finish, |
32 | 0 | ec_GFp_simple_point_copy, |
33 | 0 | ec_GFp_simple_point_set_to_infinity, |
34 | 0 | ec_GFp_simple_set_Jprojective_coordinates_GFp, |
35 | 0 | ec_GFp_simple_get_Jprojective_coordinates_GFp, |
36 | 0 | ec_GFp_simple_point_set_affine_coordinates, |
37 | 0 | ec_GFp_simple_point_get_affine_coordinates, |
38 | 0 | 0, 0, 0, |
39 | 0 | ec_GFp_simple_add, |
40 | 0 | ec_GFp_simple_dbl, |
41 | 0 | ec_GFp_simple_invert, |
42 | 0 | ec_GFp_simple_is_at_infinity, |
43 | 0 | ec_GFp_simple_is_on_curve, |
44 | 0 | ec_GFp_simple_cmp, |
45 | 0 | ec_GFp_simple_make_affine, |
46 | 0 | ec_GFp_simple_points_make_affine, |
47 | 0 | 0 /* mul */ , |
48 | 0 | 0 /* precompute_mult */ , |
49 | 0 | 0 /* have_precompute_mult */ , |
50 | 0 | ec_GFp_mont_field_mul, |
51 | 0 | ec_GFp_mont_field_sqr, |
52 | 0 | 0 /* field_div */ , |
53 | 0 | ec_GFp_mont_field_encode, |
54 | 0 | ec_GFp_mont_field_decode, |
55 | 0 | ec_GFp_mont_field_set_to_one, |
56 | 0 | ec_key_simple_priv2oct, |
57 | 0 | ec_key_simple_oct2priv, |
58 | 0 | 0, /* set private */ |
59 | 0 | ec_key_simple_generate_key, |
60 | 0 | ec_key_simple_check_key, |
61 | 0 | ec_key_simple_generate_public_key, |
62 | 0 | 0, /* keycopy */ |
63 | 0 | 0, /* keyfinish */ |
64 | 0 | ecdh_simple_compute_key, |
65 | 0 | 0, /* field_inverse_mod_ord */ |
66 | 0 | ec_GFp_simple_blind_coordinates, |
67 | 0 | ec_GFp_simple_ladder_pre, |
68 | 0 | ec_GFp_simple_ladder_step, |
69 | 0 | ec_GFp_simple_ladder_post |
70 | 0 | }; |
71 | 0 |
|
72 | 0 | return &ret; |
73 | 0 | } |
74 | | |
75 | | int ec_GFp_mont_group_init(EC_GROUP *group) |
76 | 0 | { |
77 | 0 | int ok; |
78 | 0 |
|
79 | 0 | ok = ec_GFp_simple_group_init(group); |
80 | 0 | group->field_data1 = NULL; |
81 | 0 | group->field_data2 = NULL; |
82 | 0 | return ok; |
83 | 0 | } |
84 | | |
85 | | void ec_GFp_mont_group_finish(EC_GROUP *group) |
86 | 0 | { |
87 | 0 | BN_MONT_CTX_free(group->field_data1); |
88 | 0 | group->field_data1 = NULL; |
89 | 0 | BN_free(group->field_data2); |
90 | 0 | group->field_data2 = NULL; |
91 | 0 | ec_GFp_simple_group_finish(group); |
92 | 0 | } |
93 | | |
94 | | void ec_GFp_mont_group_clear_finish(EC_GROUP *group) |
95 | 0 | { |
96 | 0 | BN_MONT_CTX_free(group->field_data1); |
97 | 0 | group->field_data1 = NULL; |
98 | 0 | BN_clear_free(group->field_data2); |
99 | 0 | group->field_data2 = NULL; |
100 | 0 | ec_GFp_simple_group_clear_finish(group); |
101 | 0 | } |
102 | | |
103 | | int ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src) |
104 | 0 | { |
105 | 0 | BN_MONT_CTX_free(dest->field_data1); |
106 | 0 | dest->field_data1 = NULL; |
107 | 0 | BN_clear_free(dest->field_data2); |
108 | 0 | dest->field_data2 = NULL; |
109 | 0 |
|
110 | 0 | if (!ec_GFp_simple_group_copy(dest, src)) |
111 | 0 | return 0; |
112 | 0 | |
113 | 0 | if (src->field_data1 != NULL) { |
114 | 0 | dest->field_data1 = BN_MONT_CTX_new(); |
115 | 0 | if (dest->field_data1 == NULL) |
116 | 0 | return 0; |
117 | 0 | if (!BN_MONT_CTX_copy(dest->field_data1, src->field_data1)) |
118 | 0 | goto err; |
119 | 0 | } |
120 | 0 | if (src->field_data2 != NULL) { |
121 | 0 | dest->field_data2 = BN_dup(src->field_data2); |
122 | 0 | if (dest->field_data2 == NULL) |
123 | 0 | goto err; |
124 | 0 | } |
125 | 0 | |
126 | 0 | return 1; |
127 | 0 | |
128 | 0 | err: |
129 | 0 | BN_MONT_CTX_free(dest->field_data1); |
130 | 0 | dest->field_data1 = NULL; |
131 | 0 | return 0; |
132 | 0 | } |
133 | | |
134 | | int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p, |
135 | | const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) |
136 | 0 | { |
137 | 0 | BN_CTX *new_ctx = NULL; |
138 | 0 | BN_MONT_CTX *mont = NULL; |
139 | 0 | BIGNUM *one = NULL; |
140 | 0 | int ret = 0; |
141 | 0 |
|
142 | 0 | BN_MONT_CTX_free(group->field_data1); |
143 | 0 | group->field_data1 = NULL; |
144 | 0 | BN_free(group->field_data2); |
145 | 0 | group->field_data2 = NULL; |
146 | 0 |
|
147 | 0 | if (ctx == NULL) { |
148 | 0 | ctx = new_ctx = BN_CTX_new(); |
149 | 0 | if (ctx == NULL) |
150 | 0 | return 0; |
151 | 0 | } |
152 | 0 | |
153 | 0 | mont = BN_MONT_CTX_new(); |
154 | 0 | if (mont == NULL) |
155 | 0 | goto err; |
156 | 0 | if (!BN_MONT_CTX_set(mont, p, ctx)) { |
157 | 0 | ECerr(EC_F_EC_GFP_MONT_GROUP_SET_CURVE, ERR_R_BN_LIB); |
158 | 0 | goto err; |
159 | 0 | } |
160 | 0 | one = BN_new(); |
161 | 0 | if (one == NULL) |
162 | 0 | goto err; |
163 | 0 | if (!BN_to_montgomery(one, BN_value_one(), mont, ctx)) |
164 | 0 | goto err; |
165 | 0 | |
166 | 0 | group->field_data1 = mont; |
167 | 0 | mont = NULL; |
168 | 0 | group->field_data2 = one; |
169 | 0 | one = NULL; |
170 | 0 |
|
171 | 0 | ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx); |
172 | 0 |
|
173 | 0 | if (!ret) { |
174 | 0 | BN_MONT_CTX_free(group->field_data1); |
175 | 0 | group->field_data1 = NULL; |
176 | 0 | BN_free(group->field_data2); |
177 | 0 | group->field_data2 = NULL; |
178 | 0 | } |
179 | 0 |
|
180 | 0 | err: |
181 | 0 | BN_free(one); |
182 | 0 | BN_CTX_free(new_ctx); |
183 | 0 | BN_MONT_CTX_free(mont); |
184 | 0 | return ret; |
185 | 0 | } |
186 | | |
187 | | int ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
188 | | const BIGNUM *b, BN_CTX *ctx) |
189 | 0 | { |
190 | 0 | if (group->field_data1 == NULL) { |
191 | 0 | ECerr(EC_F_EC_GFP_MONT_FIELD_MUL, EC_R_NOT_INITIALIZED); |
192 | 0 | return 0; |
193 | 0 | } |
194 | 0 |
|
195 | 0 | return BN_mod_mul_montgomery(r, a, b, group->field_data1, ctx); |
196 | 0 | } |
197 | | |
198 | | int ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
199 | | BN_CTX *ctx) |
200 | 0 | { |
201 | 0 | if (group->field_data1 == NULL) { |
202 | 0 | ECerr(EC_F_EC_GFP_MONT_FIELD_SQR, EC_R_NOT_INITIALIZED); |
203 | 0 | return 0; |
204 | 0 | } |
205 | 0 |
|
206 | 0 | return BN_mod_mul_montgomery(r, a, a, group->field_data1, ctx); |
207 | 0 | } |
208 | | |
209 | | int ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r, |
210 | | const BIGNUM *a, BN_CTX *ctx) |
211 | 0 | { |
212 | 0 | if (group->field_data1 == NULL) { |
213 | 0 | ECerr(EC_F_EC_GFP_MONT_FIELD_ENCODE, EC_R_NOT_INITIALIZED); |
214 | 0 | return 0; |
215 | 0 | } |
216 | 0 |
|
217 | 0 | return BN_to_montgomery(r, a, (BN_MONT_CTX *)group->field_data1, ctx); |
218 | 0 | } |
219 | | |
220 | | int ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r, |
221 | | const BIGNUM *a, BN_CTX *ctx) |
222 | 0 | { |
223 | 0 | if (group->field_data1 == NULL) { |
224 | 0 | ECerr(EC_F_EC_GFP_MONT_FIELD_DECODE, EC_R_NOT_INITIALIZED); |
225 | 0 | return 0; |
226 | 0 | } |
227 | 0 |
|
228 | 0 | return BN_from_montgomery(r, a, group->field_data1, ctx); |
229 | 0 | } |
230 | | |
231 | | int ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r, |
232 | | BN_CTX *ctx) |
233 | 0 | { |
234 | 0 | if (group->field_data2 == NULL) { |
235 | 0 | ECerr(EC_F_EC_GFP_MONT_FIELD_SET_TO_ONE, EC_R_NOT_INITIALIZED); |
236 | 0 | return 0; |
237 | 0 | } |
238 | 0 |
|
239 | 0 | if (!BN_copy(r, group->field_data2)) |
240 | 0 | return 0; |
241 | 0 | return 1; |
242 | 0 | } |