diff options
-rw-r--r-- | src/lib/libcrypto/ec/ec_local.h | 15 | ||||
-rw-r--r-- | src/lib/libcrypto/ec/ecp_mont.c | 133 |
2 files changed, 62 insertions, 86 deletions
diff --git a/src/lib/libcrypto/ec/ec_local.h b/src/lib/libcrypto/ec/ec_local.h index 7ddf6240d2..1124108d57 100644 --- a/src/lib/libcrypto/ec/ec_local.h +++ b/src/lib/libcrypto/ec/ec_local.h | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: ec_local.h,v 1.6 2023/03/05 16:31:01 jsing Exp $ */ | 1 | /* $OpenBSD: ec_local.h,v 1.7 2023/03/07 05:41:18 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Originally written by Bodo Moeller for the OpenSSL project. | 3 | * Originally written by Bodo Moeller for the OpenSSL project. |
4 | */ | 4 | */ |
@@ -369,19 +369,6 @@ int ec_GFp_simple_mul_single_ct(const EC_GROUP *, EC_POINT *r, const BIGNUM *sca | |||
369 | int ec_GFp_simple_mul_double_nonct(const EC_GROUP *, EC_POINT *r, const BIGNUM *g_scalar, | 369 | int ec_GFp_simple_mul_double_nonct(const EC_GROUP *, EC_POINT *r, const BIGNUM *g_scalar, |
370 | const BIGNUM *p_scalar, const EC_POINT *point, BN_CTX *); | 370 | const BIGNUM *p_scalar, const EC_POINT *point, BN_CTX *); |
371 | 371 | ||
372 | |||
373 | /* method functions in ecp_mont.c */ | ||
374 | int ec_GFp_mont_group_init(EC_GROUP *); | ||
375 | int ec_GFp_mont_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *); | ||
376 | void ec_GFp_mont_group_finish(EC_GROUP *); | ||
377 | void ec_GFp_mont_group_clear_finish(EC_GROUP *); | ||
378 | int ec_GFp_mont_group_copy(EC_GROUP *, const EC_GROUP *); | ||
379 | int ec_GFp_mont_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *); | ||
380 | int ec_GFp_mont_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *); | ||
381 | int ec_GFp_mont_field_encode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *); | ||
382 | int ec_GFp_mont_field_decode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *); | ||
383 | int ec_GFp_mont_field_set_to_one(const EC_GROUP *, BIGNUM *r, BN_CTX *); | ||
384 | |||
385 | int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx); | 372 | int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx); |
386 | 373 | ||
387 | /* method functions in ecp_nist.c */ | 374 | /* method functions in ecp_nist.c */ |
diff --git a/src/lib/libcrypto/ec/ecp_mont.c b/src/lib/libcrypto/ec/ecp_mont.c index ed69d1f554..f26107c20b 100644 --- a/src/lib/libcrypto/ec/ecp_mont.c +++ b/src/lib/libcrypto/ec/ecp_mont.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: ecp_mont.c,v 1.24 2023/03/07 05:28:12 jsing Exp $ */ | 1 | /* $OpenBSD: ecp_mont.c,v 1.25 2023/03/07 05:41:18 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Originally written by Bodo Moeller for the OpenSSL project. | 3 | * Originally written by Bodo Moeller for the OpenSSL project. |
4 | */ | 4 | */ |
@@ -75,59 +75,7 @@ ec_GFp_mont_group_clear(EC_GROUP *group) | |||
75 | group->mont_one = NULL; | 75 | group->mont_one = NULL; |
76 | } | 76 | } |
77 | 77 | ||
78 | const EC_METHOD * | 78 | static int |
79 | EC_GFp_mont_method(void) | ||
80 | { | ||
81 | static const EC_METHOD ret = { | ||
82 | .flags = EC_FLAGS_DEFAULT_OCT, | ||
83 | .field_type = NID_X9_62_prime_field, | ||
84 | .group_init = ec_GFp_mont_group_init, | ||
85 | .group_finish = ec_GFp_mont_group_finish, | ||
86 | .group_clear_finish = ec_GFp_mont_group_clear_finish, | ||
87 | .group_copy = ec_GFp_mont_group_copy, | ||
88 | .group_set_curve = ec_GFp_mont_group_set_curve, | ||
89 | .group_get_curve = ec_GFp_simple_group_get_curve, | ||
90 | .group_get_degree = ec_GFp_simple_group_get_degree, | ||
91 | .group_order_bits = ec_group_simple_order_bits, | ||
92 | .group_check_discriminant = | ||
93 | ec_GFp_simple_group_check_discriminant, | ||
94 | .point_init = ec_GFp_simple_point_init, | ||
95 | .point_finish = ec_GFp_simple_point_finish, | ||
96 | .point_clear_finish = ec_GFp_simple_point_clear_finish, | ||
97 | .point_copy = ec_GFp_simple_point_copy, | ||
98 | .point_set_to_infinity = ec_GFp_simple_point_set_to_infinity, | ||
99 | .point_set_Jprojective_coordinates = | ||
100 | ec_GFp_simple_set_Jprojective_coordinates, | ||
101 | .point_get_Jprojective_coordinates = | ||
102 | ec_GFp_simple_get_Jprojective_coordinates, | ||
103 | .point_set_affine_coordinates = | ||
104 | ec_GFp_simple_point_set_affine_coordinates, | ||
105 | .point_get_affine_coordinates = | ||
106 | ec_GFp_simple_point_get_affine_coordinates, | ||
107 | .add = ec_GFp_simple_add, | ||
108 | .dbl = ec_GFp_simple_dbl, | ||
109 | .invert = ec_GFp_simple_invert, | ||
110 | .is_at_infinity = ec_GFp_simple_is_at_infinity, | ||
111 | .is_on_curve = ec_GFp_simple_is_on_curve, | ||
112 | .point_cmp = ec_GFp_simple_cmp, | ||
113 | .make_affine = ec_GFp_simple_make_affine, | ||
114 | .points_make_affine = ec_GFp_simple_points_make_affine, | ||
115 | .mul_generator_ct = ec_GFp_simple_mul_generator_ct, | ||
116 | .mul_single_ct = ec_GFp_simple_mul_single_ct, | ||
117 | .mul_double_nonct = ec_GFp_simple_mul_double_nonct, | ||
118 | .field_mul = ec_GFp_mont_field_mul, | ||
119 | .field_sqr = ec_GFp_mont_field_sqr, | ||
120 | .field_encode = ec_GFp_mont_field_encode, | ||
121 | .field_decode = ec_GFp_mont_field_decode, | ||
122 | .field_set_to_one = ec_GFp_mont_field_set_to_one, | ||
123 | .blind_coordinates = ec_GFp_simple_blind_coordinates, | ||
124 | }; | ||
125 | |||
126 | return &ret; | ||
127 | } | ||
128 | |||
129 | |||
130 | int | ||
131 | ec_GFp_mont_group_init(EC_GROUP *group) | 79 | ec_GFp_mont_group_init(EC_GROUP *group) |
132 | { | 80 | { |
133 | int ok; | 81 | int ok; |
@@ -138,24 +86,21 @@ ec_GFp_mont_group_init(EC_GROUP *group) | |||
138 | return ok; | 86 | return ok; |
139 | } | 87 | } |
140 | 88 | ||
141 | 89 | static void | |
142 | void | ||
143 | ec_GFp_mont_group_finish(EC_GROUP *group) | 90 | ec_GFp_mont_group_finish(EC_GROUP *group) |
144 | { | 91 | { |
145 | ec_GFp_mont_group_clear(group); | 92 | ec_GFp_mont_group_clear(group); |
146 | ec_GFp_simple_group_finish(group); | 93 | ec_GFp_simple_group_finish(group); |
147 | } | 94 | } |
148 | 95 | ||
149 | 96 | static void | |
150 | void | ||
151 | ec_GFp_mont_group_clear_finish(EC_GROUP *group) | 97 | ec_GFp_mont_group_clear_finish(EC_GROUP *group) |
152 | { | 98 | { |
153 | ec_GFp_mont_group_clear(group); | 99 | ec_GFp_mont_group_clear(group); |
154 | ec_GFp_simple_group_clear_finish(group); | 100 | ec_GFp_simple_group_clear_finish(group); |
155 | } | 101 | } |
156 | 102 | ||
157 | 103 | static int | |
158 | int | ||
159 | ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src) | 104 | ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src) |
160 | { | 105 | { |
161 | ec_GFp_mont_group_clear(dest); | 106 | ec_GFp_mont_group_clear(dest); |
@@ -185,8 +130,7 @@ ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src) | |||
185 | return 0; | 130 | return 0; |
186 | } | 131 | } |
187 | 132 | ||
188 | 133 | static int | |
189 | int | ||
190 | ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, | 134 | ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, |
191 | const BIGNUM *b, BN_CTX *ctx) | 135 | const BIGNUM *b, BN_CTX *ctx) |
192 | { | 136 | { |
@@ -232,8 +176,7 @@ ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, | |||
232 | return ret; | 176 | return ret; |
233 | } | 177 | } |
234 | 178 | ||
235 | 179 | static int | |
236 | int | ||
237 | ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, | 180 | ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
238 | const BIGNUM *b, BN_CTX *ctx) | 181 | const BIGNUM *b, BN_CTX *ctx) |
239 | { | 182 | { |
@@ -244,8 +187,7 @@ ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, | |||
244 | return BN_mod_mul_montgomery(r, a, b, group->mont_ctx, ctx); | 187 | return BN_mod_mul_montgomery(r, a, b, group->mont_ctx, ctx); |
245 | } | 188 | } |
246 | 189 | ||
247 | 190 | static int | |
248 | int | ||
249 | ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, | 191 | ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
250 | BN_CTX *ctx) | 192 | BN_CTX *ctx) |
251 | { | 193 | { |
@@ -256,8 +198,7 @@ ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, | |||
256 | return BN_mod_mul_montgomery(r, a, a, group->mont_ctx, ctx); | 198 | return BN_mod_mul_montgomery(r, a, a, group->mont_ctx, ctx); |
257 | } | 199 | } |
258 | 200 | ||
259 | 201 | static int | |
260 | int | ||
261 | ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, | 202 | ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
262 | BN_CTX *ctx) | 203 | BN_CTX *ctx) |
263 | { | 204 | { |
@@ -268,8 +209,7 @@ ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, | |||
268 | return BN_to_montgomery(r, a, group->mont_ctx, ctx); | 209 | return BN_to_montgomery(r, a, group->mont_ctx, ctx); |
269 | } | 210 | } |
270 | 211 | ||
271 | 212 | static int | |
272 | int | ||
273 | ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, | 213 | ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
274 | BN_CTX *ctx) | 214 | BN_CTX *ctx) |
275 | { | 215 | { |
@@ -280,8 +220,7 @@ ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, | |||
280 | return BN_from_montgomery(r, a, group->mont_ctx, ctx); | 220 | return BN_from_montgomery(r, a, group->mont_ctx, ctx); |
281 | } | 221 | } |
282 | 222 | ||
283 | 223 | static int | |
284 | int | ||
285 | ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r, BN_CTX *ctx) | 224 | ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r, BN_CTX *ctx) |
286 | { | 225 | { |
287 | if (group->mont_one == NULL) { | 226 | if (group->mont_one == NULL) { |
@@ -292,3 +231,53 @@ ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r, BN_CTX *ctx) | |||
292 | return 0; | 231 | return 0; |
293 | return 1; | 232 | return 1; |
294 | } | 233 | } |
234 | |||
235 | static const EC_METHOD ec_GFp_mont_method = { | ||
236 | .flags = EC_FLAGS_DEFAULT_OCT, | ||
237 | .field_type = NID_X9_62_prime_field, | ||
238 | .group_init = ec_GFp_mont_group_init, | ||
239 | .group_finish = ec_GFp_mont_group_finish, | ||
240 | .group_clear_finish = ec_GFp_mont_group_clear_finish, | ||
241 | .group_copy = ec_GFp_mont_group_copy, | ||
242 | .group_set_curve = ec_GFp_mont_group_set_curve, | ||
243 | .group_get_curve = ec_GFp_simple_group_get_curve, | ||
244 | .group_get_degree = ec_GFp_simple_group_get_degree, | ||
245 | .group_order_bits = ec_group_simple_order_bits, | ||
246 | .group_check_discriminant = ec_GFp_simple_group_check_discriminant, | ||
247 | .point_init = ec_GFp_simple_point_init, | ||
248 | .point_finish = ec_GFp_simple_point_finish, | ||
249 | .point_clear_finish = ec_GFp_simple_point_clear_finish, | ||
250 | .point_copy = ec_GFp_simple_point_copy, | ||
251 | .point_set_to_infinity = ec_GFp_simple_point_set_to_infinity, | ||
252 | .point_set_Jprojective_coordinates = | ||
253 | ec_GFp_simple_set_Jprojective_coordinates, | ||
254 | .point_get_Jprojective_coordinates = | ||
255 | ec_GFp_simple_get_Jprojective_coordinates, | ||
256 | .point_set_affine_coordinates = | ||
257 | ec_GFp_simple_point_set_affine_coordinates, | ||
258 | .point_get_affine_coordinates = | ||
259 | ec_GFp_simple_point_get_affine_coordinates, | ||
260 | .add = ec_GFp_simple_add, | ||
261 | .dbl = ec_GFp_simple_dbl, | ||
262 | .invert = ec_GFp_simple_invert, | ||
263 | .is_at_infinity = ec_GFp_simple_is_at_infinity, | ||
264 | .is_on_curve = ec_GFp_simple_is_on_curve, | ||
265 | .point_cmp = ec_GFp_simple_cmp, | ||
266 | .make_affine = ec_GFp_simple_make_affine, | ||
267 | .points_make_affine = ec_GFp_simple_points_make_affine, | ||
268 | .mul_generator_ct = ec_GFp_simple_mul_generator_ct, | ||
269 | .mul_single_ct = ec_GFp_simple_mul_single_ct, | ||
270 | .mul_double_nonct = ec_GFp_simple_mul_double_nonct, | ||
271 | .field_mul = ec_GFp_mont_field_mul, | ||
272 | .field_sqr = ec_GFp_mont_field_sqr, | ||
273 | .field_encode = ec_GFp_mont_field_encode, | ||
274 | .field_decode = ec_GFp_mont_field_decode, | ||
275 | .field_set_to_one = ec_GFp_mont_field_set_to_one, | ||
276 | .blind_coordinates = ec_GFp_simple_blind_coordinates, | ||
277 | }; | ||
278 | |||
279 | const EC_METHOD * | ||
280 | EC_GFp_mont_method(void) | ||
281 | { | ||
282 | return &ec_GFp_mont_method; | ||
283 | } | ||