| // You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 |
| 16 | #include <openssl/ec.h> |
| 17 |
| 18 | #include <openssl/bn.h> |
| 19 | #include <openssl/err.h> |
| 20 |
| 21 | #include "internal.h" |
| 22 |
| 23 |
| 24 | size_t ec_point_byte_len(const EC_GROUP *group, point_conversion_form_t form) { |
| 25 | if (form != POINT_CONVERSION_COMPRESSED && |
| 26 | form != POINT_CONVERSION_UNCOMPRESSED) { |
| 27 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FORM); |
| 28 | return 0; |
| 29 | } |
| 30 |
| 31 | const size_t field_len = BN_num_bytes(&group->field.N); |
| 32 | size_t output_len = 1 /* type byte */ + field_len; |
| 33 | if (form == POINT_CONVERSION_UNCOMPRESSED) { |
| 34 | // Uncompressed points have a second coordinate. |
| 35 | output_len += field_len; |
| 36 | } |
| 37 | return output_len; |
| 38 | } |
| 39 |
| 40 | size_t ec_point_to_bytes(const EC_GROUP *group, const EC_AFFINE *point, |
| 41 | point_conversion_form_t form, uint8_t *buf, |
| 42 | size_t max_out) { |
| 43 | size_t output_len = ec_point_byte_len(group, form); |
| 44 | if (max_out < output_len) { |
| 45 | OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL); |
| 46 | return 0; |
| 47 | } |
| 48 |
| 49 | size_t field_len; |
| 50 | ec_felem_to_bytes(group, buf + 1, &field_len, &point->X); |
| 51 | assert(field_len == BN_num_bytes(&group->field.N)); |
| 52 |
| 53 | if (form == POINT_CONVERSION_UNCOMPRESSED) { |
| 54 | ec_felem_to_bytes(group, buf + 1 + field_len, &field_len, &point->Y); |
| 55 | assert(field_len == BN_num_bytes(&group->field.N)); |
| 56 | buf[0] = form; |
| 57 | } else { |
| 58 | uint8_t y_buf[EC_MAX_BYTES]; |
| 59 | ec_felem_to_bytes(group, y_buf, &field_len, &point->Y); |
| 60 | buf[0] = form + (y_buf[field_len - 1] & 1); |
| 61 | } |
| 62 |
| 63 | return output_len; |
| 64 | } |
| 65 |
| 66 | int ec_point_from_uncompressed(const EC_GROUP *group, EC_AFFINE *out, |
| 67 | const uint8_t *in, size_t len) { |
| 68 | const size_t field_len = BN_num_bytes(&group->field.N); |
| 69 | if (len != 1 + 2 * field_len || in[0] != POINT_CONVERSION_UNCOMPRESSED) { |
| 70 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
| 71 | return 0; |
| 72 | } |
| 73 |
| 74 | EC_FELEM x, y; |
| 75 | if (!ec_felem_from_bytes(group, &x, in + 1, field_len) || |
| 76 | !ec_felem_from_bytes(group, &y, in + 1 + field_len, field_len) || |
| 77 | !ec_point_set_affine_coordinates(group, out, &x, &y)) { |
| 78 | return 0; |
| 79 | } |
| 80 |
| 81 | return 1; |
| 82 | } |
| 83 |
| 84 | static int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point, |
| 85 | const uint8_t *buf, size_t len, |
| 86 | BN_CTX *ctx) { |
| 87 | if (len == 0) { |
| 88 | OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL); |
| 89 | return 0; |
| 90 | } |
| 91 |
| 92 | uint8_t form = buf[0]; |
| 93 | if (form == static_cast<uint8_t>(POINT_CONVERSION_UNCOMPRESSED)) { |
| 94 | EC_AFFINE affine; |
| 95 | if (!ec_point_from_uncompressed(group, &affine, buf, len)) { |
| 96 | // In the event of an error, defend against the caller not checking the |
| 97 | // return value by setting a known safe value. |
| 98 | ec_set_to_safe_point(group, &point->raw); |
| 99 | return 0; |
| 100 | } |
| 101 | ec_affine_to_jacobian(group, &point->raw, &affine); |
| 102 | return 1; |
| 103 | } |
| 104 |
| 105 | const int y_bit = form & 1; |
| 106 | const size_t field_len = BN_num_bytes(&group->field.N); |
| 107 | form = form & ~1u; |
| 108 | if (form != static_cast<uint8_t>(POINT_CONVERSION_COMPRESSED) || |
| 109 | len != 1 /* type byte */ + field_len) { |
| 110 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
| 111 | return 0; |
| 112 | } |
| 113 |
| 114 | // TODO(davidben): Integrate compressed coordinates with the lower-level EC |
| 115 | // abstractions. This requires a way to compute square roots, which is tricky |
| 116 | // for primes which are not 3 (mod 4), namely P-224 and custom curves. P-224's |
| 117 | // prime is particularly inconvenient for compressed coordinates. See |
| 118 | // https://cr.yp.to/papers/sqroot.pdf |
| 119 | bssl::UniquePtr<BN_CTX> new_ctx; |
| 120 | if (ctx == nullptr) { |
| 121 | new_ctx.reset(BN_CTX_new()); |
| 122 | if (new_ctx == nullptr) { |
| 123 | return 0; |
| 124 | } |
| 125 | ctx = new_ctx.get(); |
| 126 | } |
| 127 |
| 128 | bssl::BN_CTXScope scope(ctx); |
| 129 | BIGNUM *x = BN_CTX_get(ctx); |
| 130 | if (x == nullptr || !BN_bin2bn(buf + 1, field_len, x)) { |
| 131 | return 0; |
| 132 | } |
| 133 | if (BN_ucmp(x, &group->field.N) >= 0) { |
| 134 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
| 135 | return 0; |
| 136 | } |
| 137 |
| 138 | if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) { |
| 139 | return 0; |
| 140 | } |
| 141 |
| 142 | return 1; |
| 143 | } |
| 144 |
| 145 | int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point, |
| 146 | const uint8_t *buf, size_t len, BN_CTX *ctx) { |
| 147 | if (EC_GROUP_cmp(group, point->group, NULL) != 0) { |
| 148 | OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
| 149 | return 0; |
| 150 | } |
| 151 | return ec_GFp_simple_oct2point(group, point, buf, len, ctx); |
| 152 | } |
| 153 |
| 154 | size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point, |
| 155 | point_conversion_form_t form, uint8_t *buf, |
| 156 | size_t max_out, BN_CTX *ctx) { |
| 157 | if (EC_GROUP_cmp(group, point->group, NULL) != 0) { |
| 158 | OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
| 159 | return 0; |
| 160 | } |
| 161 | if (buf == NULL) { |
| 162 | // When |buf| is NULL, just return the number of bytes that would be |
| 163 | // written, without doing an expensive Jacobian-to-affine conversion. |
| 164 | if (ec_GFp_simple_is_at_infinity(group, &point->raw)) { |
| 165 | OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY); |
| 166 | return 0; |
| 167 | } |
| 168 | return ec_point_byte_len(group, form); |
| 169 | } |
| 170 | EC_AFFINE affine; |
| 171 | if (!ec_jacobian_to_affine(group, &affine, &point->raw)) { |
| 172 | return 0; |
| 173 | } |
| 174 | return ec_point_to_bytes(group, &affine, form, buf, max_out); |
| 175 | } |
| 176 |
| 177 | size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point, |
| 178 | point_conversion_form_t form, uint8_t **out_buf, |
| 179 | BN_CTX *ctx) { |
| 180 | *out_buf = NULL; |
| 181 | size_t len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx); |
| 182 | if (len == 0) { |
| 183 | return 0; |
| 184 | } |
| 185 | uint8_t *buf = reinterpret_cast<uint8_t *>(OPENSSL_malloc(len)); |
| 186 | if (buf == NULL) { |
| 187 | return 0; |
| 188 | } |
| 189 | len = EC_POINT_point2oct(group, point, form, buf, len, ctx); |
| 190 | if (len == 0) { |
| 191 | OPENSSL_free(buf); |
| 192 | return 0; |
| 193 | } |
| 194 | *out_buf = buf; |
| 195 | return len; |
| 196 | } |
| 197 |
| 198 | int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group, |
| 199 | EC_POINT *point, const BIGNUM *x, |
| 200 | int y_bit, BN_CTX *ctx) { |
| 201 | if (EC_GROUP_cmp(group, point->group, NULL) != 0) { |
| 202 | OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
| 203 | return 0; |
| 204 | } |
| 205 |
| 206 | const BIGNUM *field = &group->field.N; |
| 207 | if (BN_is_negative(x) || BN_cmp(x, field) >= 0) { |
| 208 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT); |
| 209 | return 0; |
| 210 | } |
| 211 |
| 212 | ERR_clear_error(); |
| 213 |
| 214 | bssl::UniquePtr<BN_CTX> new_ctx; |
| 215 | if (ctx == nullptr) { |
| 216 | new_ctx.reset(BN_CTX_new()); |
| 217 | if (new_ctx == nullptr) { |
| 218 | return 0; |
| 219 | } |
| 220 | ctx = new_ctx.get(); |
| 221 | } |
| 222 |
| 223 | y_bit = (y_bit != 0); |
| 224 |
| 225 | bssl::BN_CTXScope scope(ctx); |
| 226 | BIGNUM *tmp1 = BN_CTX_get(ctx); |
| 227 | BIGNUM *tmp2 = BN_CTX_get(ctx); |
| 228 | BIGNUM *a = BN_CTX_get(ctx); |
| 229 | BIGNUM *b = BN_CTX_get(ctx); |
| 230 | BIGNUM *y = BN_CTX_get(ctx); |
| 231 | if (y == NULL || !EC_GROUP_get_curve_GFp(group, NULL, a, b, ctx)) { |
| 232 | return 0; |
| 233 | } |
| 234 |
| 235 | // Recover y. We have a Weierstrass equation |
| 236 | // y^2 = x^3 + a*x + b, |
| 237 | // so y is one of the square roots of x^3 + a*x + b. |
| 238 |
| 239 | // tmp1 := x^3 |
| 240 | if (!BN_mod_sqr(tmp2, x, field, ctx) || |
| 241 | !BN_mod_mul(tmp1, tmp2, x, field, ctx)) { |
| 242 | return 0; |
| 243 | } |
| 244 |
| 245 | // tmp1 := tmp1 + a*x |
| 246 | if (group->a_is_minus3) { |
| 247 | if (!bn_mod_lshift1_consttime(tmp2, x, field, ctx) || |
| 248 | !bn_mod_add_consttime(tmp2, tmp2, x, field, ctx) || |
| 249 | !bn_mod_sub_consttime(tmp1, tmp1, tmp2, field, ctx)) { |
| 250 | return 0; |
| 251 | } |
| 252 | } else { |
| 253 | if (!BN_mod_mul(tmp2, a, x, field, ctx) || |
| 254 | !bn_mod_add_consttime(tmp1, tmp1, tmp2, field, ctx)) { |
| 255 | return 0; |
| 256 | } |
| 257 | } |
| 258 |
| 259 | // tmp1 := tmp1 + b |
| 260 | if (!bn_mod_add_consttime(tmp1, tmp1, b, field, ctx)) { |
| 261 | return 0; |
| 262 | } |
| 263 |
| 264 | if (!BN_mod_sqrt(y, tmp1, field, ctx)) { |
| 265 | uint32_t err = ERR_peek_last_error(); |
| 266 | if (ERR_GET_LIB(err) == ERR_LIB_BN && |
| 267 | ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) { |
| 268 | ERR_clear_error(); |
| 269 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT); |
| 270 | } else { |
| 271 | OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB); |
| 272 | } |
| 273 | return 0; |
| 274 | } |
| 275 |
| 276 | if (y_bit != BN_is_odd(y)) { |
| 277 | if (BN_is_zero(y)) { |
| 278 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSION_BIT); |
| 279 | return 0; |
| 280 | } |
| 281 | if (!BN_usub(y, field, y)) { |
| 282 | return 0; |
| 283 | } |
| 284 | } |
| 285 | if (y_bit != BN_is_odd(y)) { |
| 286 | OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
| 287 | return 0; |
| 288 | } |
| 289 |
| 290 | if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) { |
| 291 | return 0; |
| 292 | } |
| 293 |
| 294 | return 1; |
| 295 | } |
| 296 |