summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ec/ecp_oct.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/ec/ecp_oct.c')
-rw-r--r--src/lib/libcrypto/ec/ecp_oct.c62
1 files changed, 21 insertions, 41 deletions
diff --git a/src/lib/libcrypto/ec/ecp_oct.c b/src/lib/libcrypto/ec/ecp_oct.c
index f2f1929e84..8cdf544bf1 100644
--- a/src/lib/libcrypto/ec/ecp_oct.c
+++ b/src/lib/libcrypto/ec/ecp_oct.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ecp_oct.c,v 1.19 2022/11/26 16:08:52 tb Exp $ */ 1/* $OpenBSD: ecp_oct.c,v 1.20 2023/04/11 18:58:20 jsing Exp $ */
2/* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de> 2/* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
3 * for the OpenSSL project. 3 * for the OpenSSL project.
4 * Includes code written by Bodo Moeller for the OpenSSL project. 4 * Includes code written by Bodo Moeller for the OpenSSL project.
@@ -70,21 +70,16 @@ int
70ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group, 70ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,
71 EC_POINT *point, const BIGNUM *x_, int y_bit, BN_CTX *ctx) 71 EC_POINT *point, const BIGNUM *x_, int y_bit, BN_CTX *ctx)
72{ 72{
73 BN_CTX *new_ctx = NULL;
74 BIGNUM *tmp1, *tmp2, *x, *y; 73 BIGNUM *tmp1, *tmp2, *x, *y;
75 int ret = 0; 74 int ret = 0;
76 75
77 /* clear error queue */ 76 /* clear error queue */
78 ERR_clear_error(); 77 ERR_clear_error();
79 78
80 if (ctx == NULL) {
81 ctx = new_ctx = BN_CTX_new();
82 if (ctx == NULL)
83 return 0;
84 }
85 y_bit = (y_bit != 0); 79 y_bit = (y_bit != 0);
86 80
87 BN_CTX_start(ctx); 81 BN_CTX_start(ctx);
82
88 if ((tmp1 = BN_CTX_get(ctx)) == NULL) 83 if ((tmp1 = BN_CTX_get(ctx)) == NULL)
89 goto err; 84 goto err;
90 if ((tmp2 = BN_CTX_get(ctx)) == NULL) 85 if ((tmp2 = BN_CTX_get(ctx)) == NULL)
@@ -179,27 +174,25 @@ ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,
179 174
180 err: 175 err:
181 BN_CTX_end(ctx); 176 BN_CTX_end(ctx);
182 BN_CTX_free(new_ctx); 177
183 return ret; 178 return ret;
184} 179}
185 180
186
187size_t 181size_t
188ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form, 182ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
189 unsigned char *buf, size_t len, BN_CTX *ctx) 183 point_conversion_form_t form, unsigned char *buf, size_t len, BN_CTX *ctx)
190{ 184{
191 size_t ret;
192 BN_CTX *new_ctx = NULL;
193 int used_ctx = 0;
194 BIGNUM *x, *y; 185 BIGNUM *x, *y;
195 size_t field_len, i, skip; 186 size_t field_len, i, skip;
187 size_t ret = 0;
196 188
197 if ((form != POINT_CONVERSION_COMPRESSED) 189 if (form != POINT_CONVERSION_COMPRESSED &&
198 && (form != POINT_CONVERSION_UNCOMPRESSED) 190 form != POINT_CONVERSION_UNCOMPRESSED &&
199 && (form != POINT_CONVERSION_HYBRID)) { 191 form != POINT_CONVERSION_HYBRID) {
200 ECerror(EC_R_INVALID_FORM); 192 ECerror(EC_R_INVALID_FORM);
201 goto err; 193 return 0;
202 } 194 }
195
203 if (EC_POINT_is_at_infinity(group, point) > 0) { 196 if (EC_POINT_is_at_infinity(group, point) > 0) {
204 /* encodes to a single 0 octet */ 197 /* encodes to a single 0 octet */
205 if (buf != NULL) { 198 if (buf != NULL) {
@@ -211,23 +204,20 @@ ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conv
211 } 204 }
212 return 1; 205 return 1;
213 } 206 }
207
214 /* ret := required output buffer length */ 208 /* ret := required output buffer length */
215 field_len = BN_num_bytes(&group->field); 209 field_len = BN_num_bytes(&group->field);
216 ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len; 210 ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
217 211
212 BN_CTX_start(ctx);
213
218 /* if 'buf' is NULL, just return required length */ 214 /* if 'buf' is NULL, just return required length */
219 if (buf != NULL) { 215 if (buf != NULL) {
220 if (len < ret) { 216 if (len < ret) {
221 ECerror(EC_R_BUFFER_TOO_SMALL); 217 ECerror(EC_R_BUFFER_TOO_SMALL);
222 goto err; 218 goto err;
223 } 219 }
224 if (ctx == NULL) { 220
225 ctx = new_ctx = BN_CTX_new();
226 if (ctx == NULL)
227 return 0;
228 }
229 BN_CTX_start(ctx);
230 used_ctx = 1;
231 if ((x = BN_CTX_get(ctx)) == NULL) 221 if ((x = BN_CTX_get(ctx)) == NULL)
232 goto err; 222 goto err;
233 if ((y = BN_CTX_get(ctx)) == NULL) 223 if ((y = BN_CTX_get(ctx)) == NULL)
@@ -276,18 +266,12 @@ ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conv
276 goto err; 266 goto err;
277 } 267 }
278 } 268 }
279 if (used_ctx)
280 BN_CTX_end(ctx);
281 BN_CTX_free(new_ctx);
282 return ret;
283 269
284 err: 270 err:
285 if (used_ctx) 271 BN_CTX_end(ctx);
286 BN_CTX_end(ctx);
287 BN_CTX_free(new_ctx);
288 return 0;
289}
290 272
273 return ret;
274}
291 275
292int 276int
293ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point, 277ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
@@ -295,7 +279,6 @@ ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
295{ 279{
296 point_conversion_form_t form; 280 point_conversion_form_t form;
297 int y_bit; 281 int y_bit;
298 BN_CTX *new_ctx = NULL;
299 BIGNUM *x, *y; 282 BIGNUM *x, *y;
300 size_t field_len, enc_len; 283 size_t field_len, enc_len;
301 int ret = 0; 284 int ret = 0;
@@ -331,12 +314,9 @@ ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
331 ECerror(EC_R_INVALID_ENCODING); 314 ECerror(EC_R_INVALID_ENCODING);
332 return 0; 315 return 0;
333 } 316 }
334 if (ctx == NULL) { 317
335 ctx = new_ctx = BN_CTX_new();
336 if (ctx == NULL)
337 return 0;
338 }
339 BN_CTX_start(ctx); 318 BN_CTX_start(ctx);
319
340 if ((x = BN_CTX_get(ctx)) == NULL) 320 if ((x = BN_CTX_get(ctx)) == NULL)
341 goto err; 321 goto err;
342 if ((y = BN_CTX_get(ctx)) == NULL) 322 if ((y = BN_CTX_get(ctx)) == NULL)
@@ -380,6 +360,6 @@ ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
380 360
381 err: 361 err:
382 BN_CTX_end(ctx); 362 BN_CTX_end(ctx);
383 BN_CTX_free(new_ctx); 363
384 return ret; 364 return ret;
385} 365}