diff options
| author | jsing <> | 2023-04-11 18:58:20 +0000 |
|---|---|---|
| committer | jsing <> | 2023-04-11 18:58:20 +0000 |
| commit | 28c1968b342ae3ee2e863c4a47a59d90c3d7da9c (patch) | |
| tree | 566c48c290ac86140f8df6c959b74661e1d596a7 /src/lib/libcrypto/ec/ec2_oct.c | |
| parent | d64e2f3567e88a542a225f4ab620c2851bd7f9e7 (diff) | |
| download | openbsd-28c1968b342ae3ee2e863c4a47a59d90c3d7da9c.tar.gz openbsd-28c1968b342ae3ee2e863c4a47a59d90c3d7da9c.tar.bz2 openbsd-28c1968b342ae3ee2e863c4a47a59d90c3d7da9c.zip | |
Handle BN_CTX at the EC API boundary.
The EC API allows callers to optionally pass in a BN_CTX, which means that
any code needing a BN_CTX has to check if one was provided, allocate one if
not, then free it again. Rather than doing this dance throughout the EC
code, handle the BN_CTX existance at the EC API boundary. This means that
lower level implementation code can simply assume that the BN_CTX is
available.
ok tb@
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libcrypto/ec/ec2_oct.c | 59 |
1 files changed, 20 insertions, 39 deletions
diff --git a/src/lib/libcrypto/ec/ec2_oct.c b/src/lib/libcrypto/ec/ec2_oct.c index d3fbc12749..6cb7259824 100644 --- a/src/lib/libcrypto/ec/ec2_oct.c +++ b/src/lib/libcrypto/ec/ec2_oct.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: ec2_oct.c,v 1.19 2022/11/26 16:08:52 tb Exp $ */ | 1 | /* $OpenBSD: ec2_oct.c,v 1.20 2023/04/11 18:58:20 jsing Exp $ */ |
| 2 | /* ==================================================================== | 2 | /* ==================================================================== |
| 3 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | 3 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 4 | * | 4 | * |
| @@ -94,21 +94,17 @@ int | |||
| 94 | ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point, | 94 | ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point, |
| 95 | const BIGNUM *x_, int y_bit, BN_CTX *ctx) | 95 | const BIGNUM *x_, int y_bit, BN_CTX *ctx) |
| 96 | { | 96 | { |
| 97 | BN_CTX *new_ctx = NULL; | ||
| 98 | BIGNUM *tmp, *x, *y, *z; | 97 | BIGNUM *tmp, *x, *y, *z; |
| 99 | int ret = 0, z0; | 98 | int z0; |
| 99 | int ret = 0; | ||
| 100 | 100 | ||
| 101 | /* clear error queue */ | 101 | /* clear error queue */ |
| 102 | ERR_clear_error(); | 102 | ERR_clear_error(); |
| 103 | 103 | ||
| 104 | if (ctx == NULL) { | ||
| 105 | ctx = new_ctx = BN_CTX_new(); | ||
| 106 | if (ctx == NULL) | ||
| 107 | return 0; | ||
| 108 | } | ||
| 109 | y_bit = (y_bit != 0) ? 1 : 0; | 104 | y_bit = (y_bit != 0) ? 1 : 0; |
| 110 | 105 | ||
| 111 | BN_CTX_start(ctx); | 106 | BN_CTX_start(ctx); |
| 107 | |||
| 112 | if ((tmp = BN_CTX_get(ctx)) == NULL) | 108 | if ((tmp = BN_CTX_get(ctx)) == NULL) |
| 113 | goto err; | 109 | goto err; |
| 114 | if ((x = BN_CTX_get(ctx)) == NULL) | 110 | if ((x = BN_CTX_get(ctx)) == NULL) |
| @@ -163,7 +159,7 @@ ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point | |||
| 163 | 159 | ||
| 164 | err: | 160 | err: |
| 165 | BN_CTX_end(ctx); | 161 | BN_CTX_end(ctx); |
| 166 | BN_CTX_free(new_ctx); | 162 | |
| 167 | return ret; | 163 | return ret; |
| 168 | } | 164 | } |
| 169 | 165 | ||
| @@ -177,18 +173,17 @@ ec_GF2m_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, | |||
| 177 | point_conversion_form_t form, | 173 | point_conversion_form_t form, |
| 178 | unsigned char *buf, size_t len, BN_CTX *ctx) | 174 | unsigned char *buf, size_t len, BN_CTX *ctx) |
| 179 | { | 175 | { |
| 180 | size_t ret; | ||
| 181 | BN_CTX *new_ctx = NULL; | ||
| 182 | int used_ctx = 0; | ||
| 183 | BIGNUM *x, *y, *yxi; | 176 | BIGNUM *x, *y, *yxi; |
| 184 | size_t field_len, i, skip; | 177 | size_t field_len, i, skip; |
| 178 | size_t ret; | ||
| 185 | 179 | ||
| 186 | if ((form != POINT_CONVERSION_COMPRESSED) | 180 | if (form != POINT_CONVERSION_COMPRESSED && |
| 187 | && (form != POINT_CONVERSION_UNCOMPRESSED) | 181 | form != POINT_CONVERSION_UNCOMPRESSED && |
| 188 | && (form != POINT_CONVERSION_HYBRID)) { | 182 | form != POINT_CONVERSION_HYBRID) { |
| 189 | ECerror(EC_R_INVALID_FORM); | 183 | ECerror(EC_R_INVALID_FORM); |
| 190 | goto err; | 184 | return 0; |
| 191 | } | 185 | } |
| 186 | |||
| 192 | if (EC_POINT_is_at_infinity(group, point) > 0) { | 187 | if (EC_POINT_is_at_infinity(group, point) > 0) { |
| 193 | /* encodes to a single 0 octet */ | 188 | /* encodes to a single 0 octet */ |
| 194 | if (buf != NULL) { | 189 | if (buf != NULL) { |
| @@ -200,6 +195,9 @@ ec_GF2m_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, | |||
| 200 | } | 195 | } |
| 201 | return 1; | 196 | return 1; |
| 202 | } | 197 | } |
| 198 | |||
| 199 | BN_CTX_start(ctx); | ||
| 200 | |||
| 203 | /* ret := required output buffer length */ | 201 | /* ret := required output buffer length */ |
| 204 | field_len = (EC_GROUP_get_degree(group) + 7) / 8; | 202 | field_len = (EC_GROUP_get_degree(group) + 7) / 8; |
| 205 | ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : | 203 | ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : |
| @@ -211,13 +209,7 @@ ec_GF2m_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, | |||
| 211 | ECerror(EC_R_BUFFER_TOO_SMALL); | 209 | ECerror(EC_R_BUFFER_TOO_SMALL); |
| 212 | goto err; | 210 | goto err; |
| 213 | } | 211 | } |
| 214 | if (ctx == NULL) { | 212 | |
| 215 | ctx = new_ctx = BN_CTX_new(); | ||
| 216 | if (ctx == NULL) | ||
| 217 | return 0; | ||
| 218 | } | ||
| 219 | BN_CTX_start(ctx); | ||
| 220 | used_ctx = 1; | ||
| 221 | if ((x = BN_CTX_get(ctx)) == NULL) | 213 | if ((x = BN_CTX_get(ctx)) == NULL) |
| 222 | goto err; | 214 | goto err; |
| 223 | if ((y = BN_CTX_get(ctx)) == NULL) | 215 | if ((y = BN_CTX_get(ctx)) == NULL) |
| @@ -271,18 +263,12 @@ ec_GF2m_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, | |||
| 271 | goto err; | 263 | goto err; |
| 272 | } | 264 | } |
| 273 | } | 265 | } |
| 274 | if (used_ctx) | ||
| 275 | BN_CTX_end(ctx); | ||
| 276 | BN_CTX_free(new_ctx); | ||
| 277 | return ret; | ||
| 278 | 266 | ||
| 279 | err: | 267 | err: |
| 280 | if (used_ctx) | 268 | BN_CTX_end(ctx); |
| 281 | BN_CTX_end(ctx); | ||
| 282 | BN_CTX_free(new_ctx); | ||
| 283 | return 0; | ||
| 284 | } | ||
| 285 | 269 | ||
| 270 | return ret; | ||
| 271 | } | ||
| 286 | 272 | ||
| 287 | /* | 273 | /* |
| 288 | * Converts an octet string representation to an EC_POINT. | 274 | * Converts an octet string representation to an EC_POINT. |
| @@ -294,7 +280,6 @@ ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point, | |||
| 294 | { | 280 | { |
| 295 | point_conversion_form_t form; | 281 | point_conversion_form_t form; |
| 296 | int y_bit; | 282 | int y_bit; |
| 297 | BN_CTX *new_ctx = NULL; | ||
| 298 | BIGNUM *x, *y, *yxi; | 283 | BIGNUM *x, *y, *yxi; |
| 299 | size_t field_len, enc_len; | 284 | size_t field_len, enc_len; |
| 300 | int ret = 0; | 285 | int ret = 0; |
| @@ -349,12 +334,8 @@ ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point, | |||
| 349 | return 0; | 334 | return 0; |
| 350 | } | 335 | } |
| 351 | 336 | ||
| 352 | if (ctx == NULL) { | ||
| 353 | ctx = new_ctx = BN_CTX_new(); | ||
| 354 | if (ctx == NULL) | ||
| 355 | return 0; | ||
| 356 | } | ||
| 357 | BN_CTX_start(ctx); | 337 | BN_CTX_start(ctx); |
| 338 | |||
| 358 | if ((x = BN_CTX_get(ctx)) == NULL) | 339 | if ((x = BN_CTX_get(ctx)) == NULL) |
| 359 | goto err; | 340 | goto err; |
| 360 | if ((y = BN_CTX_get(ctx)) == NULL) | 341 | if ((y = BN_CTX_get(ctx)) == NULL) |
| @@ -415,7 +396,7 @@ ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point, | |||
| 415 | 396 | ||
| 416 | err: | 397 | err: |
| 417 | BN_CTX_end(ctx); | 398 | BN_CTX_end(ctx); |
| 418 | BN_CTX_free(new_ctx); | 399 | |
| 419 | return ret; | 400 | return ret; |
| 420 | } | 401 | } |
| 421 | #endif | 402 | #endif |
