| 1 |
// Originally written by Bodo Moeller for the OpenSSL project. |
| 2 |
// Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. |
| 3 |
// Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 4 |
// |
| 5 |
// The elliptic curve binary polynomial software is originally written by |
| 6 |
// Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems |
| 7 |
// Laboratories. |
| 8 |
// |
| 9 |
// SPDX-License-Identifier: Apache-2.0 |
| 10 |
|
| 11 |
#include <openssl/ec.h> |
| 12 |
|
| 13 |
#include <openssl/bn.h> |
| 14 |
#include <openssl/err.h> |
| 15 |
|
| 16 |
#include "internal.h" |
| 17 |
|
| 18 |
static int is_point_conversion_form_hybrid(int form_bit) { |
| 19 |
return POINT_CONVERSION_HYBRID == (form_bit & ~1u); |
| 20 |
} |
| 21 |
|
| 22 |
static int is_hybrid_bytes_consistent(const uint8_t *in, size_t field_len) { |
| 23 |
// Check that the encoded solution in the first byte aligns with the computed |
| 24 |
// point's, i.e., that they have the same parity. |
| 25 |
return ((in[0] & 1) == (in[1 + field_len * 2 - 1] & 1)); |
| 26 |
} |
| 27 |
|
| 28 |
size_t ec_point_byte_len(const EC_GROUP *group, point_conversion_form_t form) { |
| 29 |
if (form != POINT_CONVERSION_COMPRESSED && |
| 30 |
form != POINT_CONVERSION_UNCOMPRESSED && |
| 31 |
form != POINT_CONVERSION_HYBRID) { |
| 32 |
OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FORM); |
| 33 |
return 0; |
| 34 |
} |
| 35 |
|
| 36 |
const size_t field_len = BN_num_bytes(&group->field.N); |
| 37 |
size_t output_len = 1 /* type byte */ + field_len; |
| 38 |
if (form == POINT_CONVERSION_UNCOMPRESSED || |
| 39 |
form == POINT_CONVERSION_HYBRID) { |
| 40 |
// Uncompressed and Hybrid points have a second coordinate. |
| 41 |
output_len += field_len; |
| 42 |
} |
| 43 |
return output_len; |
| 44 |
} |
| 45 |
|
| 46 |
size_t ec_point_to_bytes(const EC_GROUP *group, const EC_AFFINE *point, |
| 47 |
point_conversion_form_t form, uint8_t *buf, |
| 48 |
size_t len) { |
| 49 |
size_t output_len = ec_point_byte_len(group, form); |
| 50 |
if (len < output_len) { |
| 51 |
OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL); |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
size_t field_len; |
| 56 |
ec_felem_to_bytes(group, buf + 1, &field_len, &point->X); |
| 57 |
assert(field_len == BN_num_bytes(&group->field.N)); |
| 58 |
|
| 59 |
if (form == POINT_CONVERSION_UNCOMPRESSED) { |
| 60 |
ec_felem_to_bytes(group, buf + 1 + field_len, &field_len, &point->Y); |
| 61 |
assert(field_len == BN_num_bytes(&group->field.N)); |
| 62 |
buf[0] = form; |
| 63 |
} else { |
| 64 |
uint8_t y_buf[EC_MAX_BYTES]; |
| 65 |
ec_felem_to_bytes(group, y_buf, &field_len, &point->Y); |
| 66 |
buf[0] = form + (y_buf[field_len - 1] & 1); |
| 67 |
if (form == POINT_CONVERSION_HYBRID) { |
| 68 |
// |POINT_CONVERSION_HYBRID| specifies y's solution of the quadratic |
| 69 |
// equation, but also encodes the y coordinate along with it. |
| 70 |
OPENSSL_memcpy(buf + 1 + field_len, y_buf, field_len); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return output_len; |
| 75 |
} |
| 76 |
|
| 77 |
int ec_point_from_uncompressed(const EC_GROUP *group, EC_AFFINE *out, |
| 78 |
const uint8_t *in, size_t len) { |
| 79 |
const size_t field_len = BN_num_bytes(&group->field.N); |
| 80 |
if (len != 1 + 2 * field_len || in[0] != POINT_CONVERSION_UNCOMPRESSED) { |
| 81 |
OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
EC_FELEM x, y; |
| 86 |
if (!ec_felem_from_bytes(group, &x, in + 1, field_len) || |
| 87 |
!ec_felem_from_bytes(group, &y, in + 1 + field_len, field_len) || |
| 88 |
!ec_point_set_affine_coordinates(group, out, &x, &y)) { |
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
return 1; |
| 93 |
} |
| 94 |
|
| 95 |
static int ec_point_from_hybrid(const EC_GROUP *group, EC_AFFINE *out, |
| 96 |
const uint8_t *in, size_t len) { |
| 97 |
const size_t field_len = BN_num_bytes(&group->field.N); |
| 98 |
// |POINT_CONVERSION_HYBRID| has the solution of y encoded in the first byte |
| 99 |
// as well. |
| 100 |
if (len != 1 + 2 * field_len || !is_point_conversion_form_hybrid(in[0]) || |
| 101 |
!is_hybrid_bytes_consistent(in, field_len)) { |
| 102 |
OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
EC_FELEM x, y; |
| 107 |
if (!ec_felem_from_bytes(group, &x, in + 1, field_len) || |
| 108 |
!ec_felem_from_bytes(group, &y, in + 1 + field_len, field_len) || |
| 109 |
!ec_point_set_affine_coordinates(group, out, &x, &y)) { |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
return 1; |
| 114 |
} |
| 115 |
|
| 116 |
static int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point, |
| 117 |
const uint8_t *buf, size_t len, |
| 118 |
BN_CTX *ctx) { |
| 119 |
if (len == 0) { |
| 120 |
OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL); |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
point_conversion_form_t form = buf[0]; |
| 125 |
|
| 126 |
// OpenSSL supports decoding infinity. |
| 127 |
if (form == 0) { |
| 128 |
if (len != 1) { |
| 129 |
OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
| 130 |
return 0; |
| 131 |
} |
| 132 |
ec_GFp_simple_point_set_to_infinity(group, &point->raw); |
| 133 |
return 1; |
| 134 |
} |
| 135 |
|
| 136 |
const int y_bit = form & 1; |
| 137 |
form = form & ~1u; |
| 138 |
|
| 139 |
if (form == POINT_CONVERSION_UNCOMPRESSED) { |
| 140 |
EC_AFFINE affine; |
| 141 |
if (!ec_point_from_uncompressed(group, &affine, buf, len)) { |
| 142 |
// In the event of an error, defend against the caller not checking the |
| 143 |
// return value by setting a known safe value. |
| 144 |
ec_set_to_safe_point(group, &point->raw); |
| 145 |
return 0; |
| 146 |
} |
| 147 |
ec_affine_to_jacobian(group, &point->raw, &affine); |
| 148 |
return 1; |
| 149 |
} |
| 150 |
|
| 151 |
if (form == POINT_CONVERSION_HYBRID) { |
| 152 |
EC_AFFINE affine; |
| 153 |
if (!ec_point_from_hybrid(group, &affine, buf, len)) { |
| 154 |
ec_set_to_safe_point(group, &point->raw); |
| 155 |
return 0; |
| 156 |
} |
| 157 |
ec_affine_to_jacobian(group, &point->raw, &affine); |
| 158 |
return 1; |
| 159 |
} |
| 160 |
|
| 161 |
const size_t field_len = BN_num_bytes(&group->field.N); |
| 162 |
if (form != POINT_CONVERSION_COMPRESSED || |
| 163 |
len != 1 /* type byte */ + field_len) { |
| 164 |
OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
| 165 |
return 0; |
| 166 |
} |
| 167 |
|
| 168 |
// TODO(davidben): Integrate compressed coordinates with the lower-level EC |
| 169 |
// abstractions. This requires a way to compute square roots, which is tricky |
| 170 |
// for primes which are not 3 (mod 4), namely P-224 and custom curves. P-224's |
| 171 |
// prime is particularly inconvenient for compressed coordinates. See |
| 172 |
// https://cr.yp.to/papers/sqroot.pdf |
| 173 |
BN_CTX *new_ctx = NULL; |
| 174 |
if (ctx == NULL) { |
| 175 |
ctx = new_ctx = BN_CTX_new(); |
| 176 |
if (ctx == NULL) { |
| 177 |
return 0; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
int ret = 0; |
| 182 |
BN_CTX_start(ctx); |
| 183 |
BIGNUM *x = BN_CTX_get(ctx); |
| 184 |
if (x == NULL || !BN_bin2bn(buf + 1, field_len, x)) { |
| 185 |
goto err; |
| 186 |
} |
| 187 |
if (BN_ucmp(x, &group->field.N) >= 0) { |
| 188 |
OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
| 189 |
goto err; |
| 190 |
} |
| 191 |
|
| 192 |
if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) { |
| 193 |
goto err; |
| 194 |
} |
| 195 |
|
| 196 |
ret = 1; |
| 197 |
|
| 198 |
err: |
| 199 |
BN_CTX_end(ctx); |
| 200 |
BN_CTX_free(new_ctx); |
| 201 |
return ret; |
| 202 |
} |
| 203 |
|
| 204 |
int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point, |
| 205 |
const uint8_t *buf, size_t len, BN_CTX *ctx) { |
| 206 |
if (EC_GROUP_cmp(group, point->group, NULL) != 0) { |
| 207 |
OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
| 208 |
return 0; |
| 209 |
} |
| 210 |
return ec_GFp_simple_oct2point(group, point, buf, len, ctx); |
| 211 |
} |
| 212 |
|
| 213 |
size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point, |
| 214 |
point_conversion_form_t form, uint8_t *buf, |
| 215 |
size_t len, BN_CTX *ctx) { |
| 216 |
if (EC_GROUP_cmp(group, point->group, NULL) != 0) { |
| 217 |
OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
| 218 |
return 0; |
| 219 |
} |
| 220 |
|
| 221 |
// OpenSSL encodes infinity to a single 0 octet. |
| 222 |
if (ec_GFp_simple_is_at_infinity(group, &point->raw)) { |
| 223 |
if(buf != NULL) { |
| 224 |
if (len < 1) { |
| 225 |
OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL); |
| 226 |
return 0; |
| 227 |
} |
| 228 |
buf[0] = 0; |
| 229 |
} |
| 230 |
return 1; |
| 231 |
} |
| 232 |
|
| 233 |
if (buf == NULL) { |
| 234 |
// When |buf| is NULL, just return the number of bytes that would be |
| 235 |
// written, without doing an expensive Jacobian-to-affine conversion. |
| 236 |
return ec_point_byte_len(group, form); |
| 237 |
} |
| 238 |
EC_AFFINE affine; |
| 239 |
if (!ec_jacobian_to_affine(group, &affine, &point->raw)) { |
| 240 |
return 0; |
| 241 |
} |
| 242 |
return ec_point_to_bytes(group, &affine, form, buf, len); |
| 243 |
} |
| 244 |
|
| 245 |
int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group, |
| 246 |
EC_POINT *point, const BIGNUM *x, |
| 247 |
int y_bit, BN_CTX *ctx) { |
| 248 |
if (EC_GROUP_cmp(group, point->group, NULL) != 0) { |
| 249 |
OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
| 250 |
return 0; |
| 251 |
} |
| 252 |
|
| 253 |
const BIGNUM *field = &group->field.N; |
| 254 |
if (BN_is_negative(x) || BN_cmp(x, field) >= 0) { |
| 255 |
OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT); |
| 256 |
return 0; |
| 257 |
} |
| 258 |
|
| 259 |
BN_CTX *new_ctx = NULL; |
| 260 |
int ret = 0; |
| 261 |
|
| 262 |
ERR_clear_error(); |
| 263 |
|
| 264 |
if (ctx == NULL) { |
| 265 |
ctx = new_ctx = BN_CTX_new(); |
| 266 |
if (ctx == NULL) { |
| 267 |
return 0; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
y_bit = (y_bit != 0); |
| 272 |
|
| 273 |
BN_CTX_start(ctx); |
| 274 |
BIGNUM *tmp1 = BN_CTX_get(ctx); |
| 275 |
BIGNUM *tmp2 = BN_CTX_get(ctx); |
| 276 |
BIGNUM *a = BN_CTX_get(ctx); |
| 277 |
BIGNUM *b = BN_CTX_get(ctx); |
| 278 |
BIGNUM *y = BN_CTX_get(ctx); |
| 279 |
if (y == NULL || |
| 280 |
!EC_GROUP_get_curve_GFp(group, NULL, a, b, ctx)) { |
| 281 |
goto err; |
| 282 |
} |
| 283 |
|
| 284 |
// Recover y. We have a Weierstrass equation |
| 285 |
// y^2 = x^3 + a*x + b, |
| 286 |
// so y is one of the square roots of x^3 + a*x + b. |
| 287 |
|
| 288 |
// tmp1 := x^3 |
| 289 |
if (!BN_mod_sqr(tmp2, x, field, ctx) || |
| 290 |
!BN_mod_mul(tmp1, tmp2, x, field, ctx)) { |
| 291 |
goto err; |
| 292 |
} |
| 293 |
|
| 294 |
// tmp1 := tmp1 + a*x |
| 295 |
if (group->a_is_minus3) { |
| 296 |
if (!bn_mod_lshift1_consttime(tmp2, x, field, ctx) || |
| 297 |
!bn_mod_add_consttime(tmp2, tmp2, x, field, ctx) || |
| 298 |
!bn_mod_sub_consttime(tmp1, tmp1, tmp2, field, ctx)) { |
| 299 |
goto err; |
| 300 |
} |
| 301 |
} else { |
| 302 |
if (!BN_mod_mul(tmp2, a, x, field, ctx) || |
| 303 |
!bn_mod_add_consttime(tmp1, tmp1, tmp2, field, ctx)) { |
| 304 |
goto err; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
// tmp1 := tmp1 + b |
| 309 |
if (!bn_mod_add_consttime(tmp1, tmp1, b, field, ctx)) { |
| 310 |
goto err; |
| 311 |
} |
| 312 |
|
| 313 |
if (!BN_mod_sqrt(y, tmp1, field, ctx)) { |
| 314 |
uint32_t err = ERR_peek_last_error(); |
| 315 |
if (ERR_GET_LIB(err) == ERR_LIB_BN && |
| 316 |
ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) { |
| 317 |
ERR_clear_error(); |
| 318 |
OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT); |
| 319 |
} else { |
| 320 |
OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB); |
| 321 |
} |
| 322 |
goto err; |
| 323 |
} |
| 324 |
|
| 325 |
if (y_bit != BN_is_odd(y)) { |
| 326 |
if (BN_is_zero(y)) { |
| 327 |
OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSION_BIT); |
| 328 |
goto err; |
| 329 |
} |
| 330 |
if (!BN_usub(y, field, y)) { |
| 331 |
goto err; |
| 332 |
} |
| 333 |
} |
| 334 |
if (y_bit != BN_is_odd(y)) { |
| 335 |
OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
| 336 |
goto err; |
| 337 |
} |
| 338 |
|
| 339 |
if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) { |
| 340 |
goto err; |
| 341 |
} |
| 342 |
|
| 343 |
ret = 1; |
| 344 |
|
| 345 |
err: |
| 346 |
BN_CTX_end(ctx); |
| 347 |
BN_CTX_free(new_ctx); |
| 348 |
return ret; |
| 349 |
} |