1/* 2 * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved. 3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. 4 * 5 * Licensed under the OpenSSL license (the "License"). You may not use 6 * this file except in compliance with the License. You can obtain a copy 7 * in the file LICENSE in the source distribution or at 8 * https://www.openssl.org/source/license.html 9 */ 10 11#include <openssl/ec.h> 12 13#include <openssl/bn.h> 14#include <openssl/err.h> 15 16#include "internal.h" 17 18 19size_t ec_point_byte_len(const EC_GROUP *group, point_conversion_form_t form) { 20 if (form != POINT_CONVERSION_COMPRESSED && 21 form != POINT_CONVERSION_UNCOMPRESSED) { 22 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FORM); 23 return 0; 24 } 25 26 const size_t field_len = BN_num_bytes(&group->field.N); 27 size_t output_len = 1 /* type byte */ + field_len; 28 if (form == POINT_CONVERSION_UNCOMPRESSED) { 29 // Uncompressed points have a second coordinate. 30 output_len += field_len; 31 } 32 return output_len; 33} 34 35size_t ec_point_to_bytes(const EC_GROUP *group, const EC_AFFINE *point, 36 point_conversion_form_t form, uint8_t *buf, 37 size_t max_out) { 38 size_t output_len = ec_point_byte_len(group, form); 39 if (max_out < output_len) { 40 OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL); 41 return 0; 42 } 43 44 size_t field_len; 45 ec_felem_to_bytes(group, buf + 1, &field_len, &point->X); 46 assert(field_len == BN_num_bytes(&group->field.N)); 47 48 if (form == POINT_CONVERSION_UNCOMPRESSED) { 49 ec_felem_to_bytes(group, buf + 1 + field_len, &field_len, &point->Y); 50 assert(field_len == BN_num_bytes(&group->field.N)); 51 buf[0] = form; 52 } else { 53 uint8_t y_buf[EC_MAX_BYTES]; 54 ec_felem_to_bytes(group, y_buf, &field_len, &point->Y); 55 buf[0] = form + (y_buf[field_len - 1] & 1); 56 } 57 58 return output_len; 59} 60 61int ec_point_from_uncompressed(const EC_GROUP *group, EC_AFFINE *out, 62 const uint8_t *in, size_t len) { 63 const size_t field_len = BN_num_bytes(&group->field.N); 64 if (len != 1 + 2 * field_len || in[0] != POINT_CONVERSION_UNCOMPRESSED) { 65 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); 66 return 0; 67 } 68 69 EC_FELEM x, y; 70 if (!ec_felem_from_bytes(group, &x, in + 1, field_len) || 71 !ec_felem_from_bytes(group, &y, in + 1 + field_len, field_len) || 72 !ec_point_set_affine_coordinates(group, out, &x, &y)) { 73 return 0; 74 } 75 76 return 1; 77} 78 79static int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point, 80 const uint8_t *buf, size_t len, 81 BN_CTX *ctx) { 82 if (len == 0) { 83 OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL); 84 return 0; 85 } 86 87 uint8_t form = buf[0]; 88 if (form == static_cast<uint8_t>(POINT_CONVERSION_UNCOMPRESSED)) { 89 EC_AFFINE affine; 90 if (!ec_point_from_uncompressed(group, &affine, buf, len)) { 91 // In the event of an error, defend against the caller not checking the 92 // return value by setting a known safe value. 93 ec_set_to_safe_point(group, &point->raw); 94 return 0; 95 } 96 ec_affine_to_jacobian(group, &point->raw, &affine); 97 return 1; 98 } 99 100 const int y_bit = form & 1; 101 const size_t field_len = BN_num_bytes(&group->field.N); 102 form = form & ~1u; 103 if (form != static_cast<uint8_t>(POINT_CONVERSION_COMPRESSED) || 104 len != 1 /* type byte */ + field_len) { 105 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); 106 return 0; 107 } 108 109 // TODO(davidben): Integrate compressed coordinates with the lower-level EC 110 // abstractions. This requires a way to compute square roots, which is tricky 111 // for primes which are not 3 (mod 4), namely P-224 and custom curves. P-224's 112 // prime is particularly inconvenient for compressed coordinates. See 113 // https://cr.yp.to/papers/sqroot.pdf 114 BN_CTX *new_ctx = NULL; 115 if (ctx == NULL) { 116 ctx = new_ctx = BN_CTX_new(); 117 if (ctx == NULL) { 118 return 0; 119 } 120 } 121 122 int ret = 0; 123 BN_CTX_start(ctx); 124 BIGNUM *x = BN_CTX_get(ctx); 125 if (x == NULL || !BN_bin2bn(buf + 1, field_len, x)) { 126 goto err; 127 } 128 if (BN_ucmp(x, &group->field.N) >= 0) { 129 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); 130 goto err; 131 } 132 133 if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) { 134 goto err; 135 } 136 137 ret = 1; 138 139err: 140 BN_CTX_end(ctx); 141 BN_CTX_free(new_ctx); 142 return ret; 143} 144 145int 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 154size_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 177size_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 198int 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 BN_CTX *new_ctx = NULL; 213 int ret = 0; 214 215 ERR_clear_error(); 216 217 if (ctx == NULL) { 218 ctx = new_ctx = BN_CTX_new(); 219 if (ctx == NULL) { 220 return 0; 221 } 222 } 223 224 y_bit = (y_bit != 0); 225 226 BN_CTX_start(ctx); 227 BIGNUM *tmp1 = BN_CTX_get(ctx); 228 BIGNUM *tmp2 = BN_CTX_get(ctx); 229 BIGNUM *a = BN_CTX_get(ctx); 230 BIGNUM *b = BN_CTX_get(ctx); 231 BIGNUM *y = BN_CTX_get(ctx); 232 if (y == NULL || !EC_GROUP_get_curve_GFp(group, NULL, a, b, ctx)) { 233 goto err; 234 } 235 236 // Recover y. We have a Weierstrass equation 237 // y^2 = x^3 + a*x + b, 238 // so y is one of the square roots of x^3 + a*x + b. 239 240 // tmp1 := x^3 241 if (!BN_mod_sqr(tmp2, x, field, ctx) || 242 !BN_mod_mul(tmp1, tmp2, x, field, ctx)) { 243 goto err; 244 } 245 246 // tmp1 := tmp1 + a*x 247 if (group->a_is_minus3) { 248 if (!bn_mod_lshift1_consttime(tmp2, x, field, ctx) || 249 !bn_mod_add_consttime(tmp2, tmp2, x, field, ctx) || 250 !bn_mod_sub_consttime(tmp1, tmp1, tmp2, field, ctx)) { 251 goto err; 252 } 253 } else { 254 if (!BN_mod_mul(tmp2, a, x, field, ctx) || 255 !bn_mod_add_consttime(tmp1, tmp1, tmp2, field, ctx)) { 256 goto err; 257 } 258 } 259 260 // tmp1 := tmp1 + b 261 if (!bn_mod_add_consttime(tmp1, tmp1, b, field, ctx)) { 262 goto err; 263 } 264 265 if (!BN_mod_sqrt(y, tmp1, field, ctx)) { 266 uint32_t err = ERR_peek_last_error(); 267 if (ERR_GET_LIB(err) == ERR_LIB_BN && 268 ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) { 269 ERR_clear_error(); 270 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT); 271 } else { 272 OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB); 273 } 274 goto err; 275 } 276 277 if (y_bit != BN_is_odd(y)) { 278 if (BN_is_zero(y)) { 279 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSION_BIT); 280 goto err; 281 } 282 if (!BN_usub(y, field, y)) { 283 goto err; 284 } 285 } 286 if (y_bit != BN_is_odd(y)) { 287 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); 288 goto err; 289 } 290 291 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) { 292 goto err; 293 } 294 295 ret = 1; 296 297err: 298 BN_CTX_end(ctx); 299 BN_CTX_free(new_ctx); 300 return ret; 301} 302