diff options
Diffstat (limited to 'src/lib/libcrypto/ec')
| -rw-r--r-- | src/lib/libcrypto/ec/ec_convert.c | 62 |
1 files changed, 31 insertions, 31 deletions
diff --git a/src/lib/libcrypto/ec/ec_convert.c b/src/lib/libcrypto/ec/ec_convert.c index 3fdbaf3ce7..65636f27fa 100644 --- a/src/lib/libcrypto/ec/ec_convert.c +++ b/src/lib/libcrypto/ec/ec_convert.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: ec_convert.c,v 1.7 2024/10/31 15:37:53 tb Exp $ */ | 1 | /* $OpenBSD: ec_convert.c,v 1.8 2024/10/31 15:42:47 tb 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 | */ |
| @@ -91,13 +91,13 @@ | |||
| 91 | #define EC_OCT_POINT_CONVERSION_MASK 0x06 | 91 | #define EC_OCT_POINT_CONVERSION_MASK 0x06 |
| 92 | 92 | ||
| 93 | static int | 93 | static int |
| 94 | ec_oct_conversion_form_is_valid(uint8_t form) | 94 | ec_conversion_form_is_valid(uint8_t form) |
| 95 | { | 95 | { |
| 96 | return (form & EC_OCT_POINT_CONVERSION_MASK) == form; | 96 | return (form & EC_OCT_POINT_CONVERSION_MASK) == form; |
| 97 | } | 97 | } |
| 98 | 98 | ||
| 99 | static int | 99 | static int |
| 100 | ec_oct_check_hybrid_ybit_is_consistent(uint8_t form, int ybit, const BIGNUM *y) | 100 | ec_check_hybrid_ybit_is_consistent(uint8_t form, int ybit, const BIGNUM *y) |
| 101 | { | 101 | { |
| 102 | if (form == EC_OCT_POINT_HYBRID && ybit != BN_is_odd(y)) { | 102 | if (form == EC_OCT_POINT_HYBRID && ybit != BN_is_odd(y)) { |
| 103 | ECerror(EC_R_INVALID_ENCODING); | 103 | ECerror(EC_R_INVALID_ENCODING); |
| @@ -109,22 +109,22 @@ ec_oct_check_hybrid_ybit_is_consistent(uint8_t form, int ybit, const BIGNUM *y) | |||
| 109 | 109 | ||
| 110 | /* Nonzero y-bit only makes sense with compressed or hybrid encoding. */ | 110 | /* Nonzero y-bit only makes sense with compressed or hybrid encoding. */ |
| 111 | static int | 111 | static int |
| 112 | ec_oct_nonzero_ybit_allowed(uint8_t form) | 112 | ec_nonzero_ybit_allowed(uint8_t form) |
| 113 | { | 113 | { |
| 114 | return form == EC_OCT_POINT_COMPRESSED || form == EC_OCT_POINT_HYBRID; | 114 | return form == EC_OCT_POINT_COMPRESSED || form == EC_OCT_POINT_HYBRID; |
| 115 | } | 115 | } |
| 116 | 116 | ||
| 117 | static int | 117 | static int |
| 118 | ec_oct_add_leading_octet_cbb(CBB *cbb, uint8_t form, int ybit) | 118 | ec_add_leading_octet_cbb(CBB *cbb, uint8_t form, int ybit) |
| 119 | { | 119 | { |
| 120 | if (ec_oct_nonzero_ybit_allowed(form) && ybit != 0) | 120 | if (ec_nonzero_ybit_allowed(form) && ybit != 0) |
| 121 | form |= EC_OCT_YBIT; | 121 | form |= EC_OCT_YBIT; |
| 122 | 122 | ||
| 123 | return CBB_add_u8(cbb, form); | 123 | return CBB_add_u8(cbb, form); |
| 124 | } | 124 | } |
| 125 | 125 | ||
| 126 | static int | 126 | static int |
| 127 | ec_oct_get_leading_octet_cbs(CBS *cbs, uint8_t *out_form, int *out_ybit) | 127 | ec_get_leading_octet_cbs(CBS *cbs, uint8_t *out_form, int *out_ybit) |
| 128 | { | 128 | { |
| 129 | uint8_t octet; | 129 | uint8_t octet; |
| 130 | 130 | ||
| @@ -136,12 +136,12 @@ ec_oct_get_leading_octet_cbs(CBS *cbs, uint8_t *out_form, int *out_ybit) | |||
| 136 | *out_ybit = octet & EC_OCT_YBIT; | 136 | *out_ybit = octet & EC_OCT_YBIT; |
| 137 | *out_form = octet & ~EC_OCT_YBIT; | 137 | *out_form = octet & ~EC_OCT_YBIT; |
| 138 | 138 | ||
| 139 | if (!ec_oct_conversion_form_is_valid(*out_form)) { | 139 | if (!ec_conversion_form_is_valid(*out_form)) { |
| 140 | ECerror(EC_R_INVALID_ENCODING); | 140 | ECerror(EC_R_INVALID_ENCODING); |
| 141 | return 0; | 141 | return 0; |
| 142 | } | 142 | } |
| 143 | 143 | ||
| 144 | if (*out_ybit != 0 && !ec_oct_nonzero_ybit_allowed(*out_form)) { | 144 | if (*out_ybit != 0 && !ec_nonzero_ybit_allowed(*out_form)) { |
| 145 | ECerror(EC_R_INVALID_ENCODING); | 145 | ECerror(EC_R_INVALID_ENCODING); |
| 146 | return 0; | 146 | return 0; |
| 147 | } | 147 | } |
| @@ -150,7 +150,7 @@ ec_oct_get_leading_octet_cbs(CBS *cbs, uint8_t *out_form, int *out_ybit) | |||
| 150 | } | 150 | } |
| 151 | 151 | ||
| 152 | static int | 152 | static int |
| 153 | ec_oct_encoded_length(const EC_GROUP *group, uint8_t form, size_t *out_len) | 153 | ec_encoded_length(const EC_GROUP *group, uint8_t form, size_t *out_len) |
| 154 | { | 154 | { |
| 155 | switch (form) { | 155 | switch (form) { |
| 156 | case EC_OCT_POINT_AT_INFINITY: | 156 | case EC_OCT_POINT_AT_INFINITY: |
| @@ -169,19 +169,19 @@ ec_oct_encoded_length(const EC_GROUP *group, uint8_t form, size_t *out_len) | |||
| 169 | } | 169 | } |
| 170 | 170 | ||
| 171 | static int | 171 | static int |
| 172 | ec_oct_field_element_is_valid(const EC_GROUP *group, const BIGNUM *bn) | 172 | ec_field_element_is_valid(const EC_GROUP *group, const BIGNUM *bn) |
| 173 | { | 173 | { |
| 174 | /* Ensure bn is in the range [0, field). */ | 174 | /* Ensure bn is in the range [0, field). */ |
| 175 | return !BN_is_negative(bn) && BN_cmp(&group->field, bn) > 0; | 175 | return !BN_is_negative(bn) && BN_cmp(&group->field, bn) > 0; |
| 176 | } | 176 | } |
| 177 | 177 | ||
| 178 | static int | 178 | static int |
| 179 | ec_oct_add_field_element_cbb(CBB *cbb, const EC_GROUP *group, const BIGNUM *bn) | 179 | ec_add_field_element_cbb(CBB *cbb, const EC_GROUP *group, const BIGNUM *bn) |
| 180 | { | 180 | { |
| 181 | uint8_t *buf = NULL; | 181 | uint8_t *buf = NULL; |
| 182 | int buf_len = BN_num_bytes(&group->field); | 182 | int buf_len = BN_num_bytes(&group->field); |
| 183 | 183 | ||
| 184 | if (!ec_oct_field_element_is_valid(group, bn)) { | 184 | if (!ec_field_element_is_valid(group, bn)) { |
| 185 | ECerror(EC_R_BIGNUM_OUT_OF_RANGE); | 185 | ECerror(EC_R_BIGNUM_OUT_OF_RANGE); |
| 186 | return 0; | 186 | return 0; |
| 187 | } | 187 | } |
| @@ -198,7 +198,7 @@ ec_oct_add_field_element_cbb(CBB *cbb, const EC_GROUP *group, const BIGNUM *bn) | |||
| 198 | } | 198 | } |
| 199 | 199 | ||
| 200 | static int | 200 | static int |
| 201 | ec_oct_get_field_element_cbs(CBS *cbs, const EC_GROUP *group, BIGNUM *bn) | 201 | ec_get_field_element_cbs(CBS *cbs, const EC_GROUP *group, BIGNUM *bn) |
| 202 | { | 202 | { |
| 203 | CBS field_element; | 203 | CBS field_element; |
| 204 | 204 | ||
| @@ -210,7 +210,7 @@ ec_oct_get_field_element_cbs(CBS *cbs, const EC_GROUP *group, BIGNUM *bn) | |||
| 210 | ECerror(ERR_R_MALLOC_FAILURE); | 210 | ECerror(ERR_R_MALLOC_FAILURE); |
| 211 | return 0; | 211 | return 0; |
| 212 | } | 212 | } |
| 213 | if (!ec_oct_field_element_is_valid(group, bn)) { | 213 | if (!ec_field_element_is_valid(group, bn)) { |
| 214 | ECerror(EC_R_BIGNUM_OUT_OF_RANGE); | 214 | ECerror(EC_R_BIGNUM_OUT_OF_RANGE); |
| 215 | return 0; | 215 | return 0; |
| 216 | } | 216 | } |
| @@ -219,7 +219,7 @@ ec_oct_get_field_element_cbs(CBS *cbs, const EC_GROUP *group, BIGNUM *bn) | |||
| 219 | } | 219 | } |
| 220 | 220 | ||
| 221 | static size_t | 221 | static size_t |
| 222 | ec_oct_point2oct(const EC_GROUP *group, const EC_POINT *point, | 222 | ec_point2oct(const EC_GROUP *group, const EC_POINT *point, |
| 223 | point_conversion_form_t conversion_form, unsigned char *buf, size_t len, | 223 | point_conversion_form_t conversion_form, unsigned char *buf, size_t len, |
| 224 | BN_CTX *ctx) | 224 | BN_CTX *ctx) |
| 225 | { | 225 | { |
| @@ -240,7 +240,7 @@ ec_oct_point2oct(const EC_GROUP *group, const EC_POINT *point, | |||
| 240 | * Established behavior is to reject a request for the form 0 for the | 240 | * Established behavior is to reject a request for the form 0 for the |
| 241 | * point at infinity even if it is valid. | 241 | * point at infinity even if it is valid. |
| 242 | */ | 242 | */ |
| 243 | if (form == 0 || !ec_oct_conversion_form_is_valid(form)) { | 243 | if (form == 0 || !ec_conversion_form_is_valid(form)) { |
| 244 | ECerror(EC_R_INVALID_FORM); | 244 | ECerror(EC_R_INVALID_FORM); |
| 245 | return 0; | 245 | return 0; |
| 246 | } | 246 | } |
| @@ -248,7 +248,7 @@ ec_oct_point2oct(const EC_GROUP *group, const EC_POINT *point, | |||
| 248 | if (EC_POINT_is_at_infinity(group, point)) | 248 | if (EC_POINT_is_at_infinity(group, point)) |
| 249 | form = EC_OCT_POINT_AT_INFINITY; | 249 | form = EC_OCT_POINT_AT_INFINITY; |
| 250 | 250 | ||
| 251 | if (!ec_oct_encoded_length(group, form, &encoded_length)) { | 251 | if (!ec_encoded_length(group, form, &encoded_length)) { |
| 252 | ECerror(EC_R_INVALID_FORM); | 252 | ECerror(EC_R_INVALID_FORM); |
| 253 | return 0; | 253 | return 0; |
| 254 | } | 254 | } |
| @@ -266,7 +266,7 @@ ec_oct_point2oct(const EC_GROUP *group, const EC_POINT *point, | |||
| 266 | goto err; | 266 | goto err; |
| 267 | 267 | ||
| 268 | if (form == EC_OCT_POINT_AT_INFINITY) { | 268 | if (form == EC_OCT_POINT_AT_INFINITY) { |
| 269 | if (!ec_oct_add_leading_octet_cbb(&cbb, form, 0)) | 269 | if (!ec_add_leading_octet_cbb(&cbb, form, 0)) |
| 270 | goto err; | 270 | goto err; |
| 271 | 271 | ||
| 272 | goto done; | 272 | goto done; |
| @@ -279,16 +279,16 @@ ec_oct_point2oct(const EC_GROUP *group, const EC_POINT *point, | |||
| 279 | if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx)) | 279 | if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx)) |
| 280 | goto err; | 280 | goto err; |
| 281 | 281 | ||
| 282 | if (!ec_oct_add_leading_octet_cbb(&cbb, form, BN_is_odd(y))) | 282 | if (!ec_add_leading_octet_cbb(&cbb, form, BN_is_odd(y))) |
| 283 | goto err; | 283 | goto err; |
| 284 | 284 | ||
| 285 | if (form == EC_OCT_POINT_COMPRESSED) { | 285 | if (form == EC_OCT_POINT_COMPRESSED) { |
| 286 | if (!ec_oct_add_field_element_cbb(&cbb, group, x)) | 286 | if (!ec_add_field_element_cbb(&cbb, group, x)) |
| 287 | goto err; | 287 | goto err; |
| 288 | } else { | 288 | } else { |
| 289 | if (!ec_oct_add_field_element_cbb(&cbb, group, x)) | 289 | if (!ec_add_field_element_cbb(&cbb, group, x)) |
| 290 | goto err; | 290 | goto err; |
| 291 | if (!ec_oct_add_field_element_cbb(&cbb, group, y)) | 291 | if (!ec_add_field_element_cbb(&cbb, group, y)) |
| 292 | goto err; | 292 | goto err; |
| 293 | } | 293 | } |
| 294 | 294 | ||
| @@ -309,7 +309,7 @@ ec_oct_point2oct(const EC_GROUP *group, const EC_POINT *point, | |||
| 309 | } | 309 | } |
| 310 | 310 | ||
| 311 | static int | 311 | static int |
| 312 | ec_oct_oct2point(const EC_GROUP *group, EC_POINT *point, | 312 | ec_oct2point(const EC_GROUP *group, EC_POINT *point, |
| 313 | const unsigned char *buf, size_t len, BN_CTX *ctx) | 313 | const unsigned char *buf, size_t len, BN_CTX *ctx) |
| 314 | { | 314 | { |
| 315 | CBS cbs; | 315 | CBS cbs; |
| @@ -321,7 +321,7 @@ ec_oct_oct2point(const EC_GROUP *group, EC_POINT *point, | |||
| 321 | BN_CTX_start(ctx); | 321 | BN_CTX_start(ctx); |
| 322 | CBS_init(&cbs, buf, len); | 322 | CBS_init(&cbs, buf, len); |
| 323 | 323 | ||
| 324 | if (!ec_oct_get_leading_octet_cbs(&cbs, &form, &ybit)) | 324 | if (!ec_get_leading_octet_cbs(&cbs, &form, &ybit)) |
| 325 | goto err; | 325 | goto err; |
| 326 | 326 | ||
| 327 | if (form == EC_OCT_POINT_AT_INFINITY) { | 327 | if (form == EC_OCT_POINT_AT_INFINITY) { |
| @@ -337,16 +337,16 @@ ec_oct_oct2point(const EC_GROUP *group, EC_POINT *point, | |||
| 337 | goto err; | 337 | goto err; |
| 338 | 338 | ||
| 339 | if (form == EC_OCT_POINT_COMPRESSED) { | 339 | if (form == EC_OCT_POINT_COMPRESSED) { |
| 340 | if (!ec_oct_get_field_element_cbs(&cbs, group, x)) | 340 | if (!ec_get_field_element_cbs(&cbs, group, x)) |
| 341 | goto err; | 341 | goto err; |
| 342 | if (!EC_POINT_set_compressed_coordinates(group, point, x, ybit, ctx)) | 342 | if (!EC_POINT_set_compressed_coordinates(group, point, x, ybit, ctx)) |
| 343 | goto err; | 343 | goto err; |
| 344 | } else { | 344 | } else { |
| 345 | if (!ec_oct_get_field_element_cbs(&cbs, group, x)) | 345 | if (!ec_get_field_element_cbs(&cbs, group, x)) |
| 346 | goto err; | 346 | goto err; |
| 347 | if (!ec_oct_get_field_element_cbs(&cbs, group, y)) | 347 | if (!ec_get_field_element_cbs(&cbs, group, y)) |
| 348 | goto err; | 348 | goto err; |
| 349 | if (!ec_oct_check_hybrid_ybit_is_consistent(form, ybit, y)) | 349 | if (!ec_check_hybrid_ybit_is_consistent(form, ybit, y)) |
| 350 | goto err; | 350 | goto err; |
| 351 | if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx)) | 351 | if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx)) |
| 352 | goto err; | 352 | goto err; |
| @@ -451,7 +451,7 @@ EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point, | |||
| 451 | ECerror(EC_R_INCOMPATIBLE_OBJECTS); | 451 | ECerror(EC_R_INCOMPATIBLE_OBJECTS); |
| 452 | goto err; | 452 | goto err; |
| 453 | } | 453 | } |
| 454 | ret = ec_oct_point2oct(group, point, form, buf, len, ctx); | 454 | ret = ec_point2oct(group, point, form, buf, len, ctx); |
| 455 | 455 | ||
| 456 | err: | 456 | err: |
| 457 | if (ctx != ctx_in) | 457 | if (ctx != ctx_in) |
| @@ -477,7 +477,7 @@ EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point, | |||
| 477 | ECerror(EC_R_INCOMPATIBLE_OBJECTS); | 477 | ECerror(EC_R_INCOMPATIBLE_OBJECTS); |
| 478 | goto err; | 478 | goto err; |
| 479 | } | 479 | } |
| 480 | ret = ec_oct_oct2point(group, point, buf, len, ctx); | 480 | ret = ec_oct2point(group, point, buf, len, ctx); |
| 481 | 481 | ||
| 482 | err: | 482 | err: |
| 483 | if (ctx != ctx_in) | 483 | if (ctx != ctx_in) |
