1/* 2 * Copyright 2002-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_key.h> 12 13#include <string.h> 14 15#include <openssl/ec.h> 16#include <openssl/ecdsa.h> 17#include <openssl/engine.h> 18#include <openssl/err.h> 19#include <openssl/ex_data.h> 20#include <openssl/mem.h> 21#include <openssl/thread.h> 22 23#include "../../internal.h" 24#include "../bcm_interface.h" 25#include "../delocate.h" 26#include "../ecdsa/internal.h" 27#include "../service_indicator/internal.h" 28#include "internal.h" 29 30 31DEFINE_STATIC_EX_DATA_CLASS(g_ec_ex_data_class) 32 33static EC_WRAPPED_SCALAR *ec_wrapped_scalar_new(const EC_GROUP *group) { 34 EC_WRAPPED_SCALAR *wrapped = reinterpret_cast<EC_WRAPPED_SCALAR *>( 35 OPENSSL_zalloc(sizeof(EC_WRAPPED_SCALAR))); 36 if (wrapped == NULL) { 37 return NULL; 38 } 39 40 wrapped->bignum.d = wrapped->scalar.words; 41 wrapped->bignum.width = group->order.N.width; 42 wrapped->bignum.dmax = group->order.N.width; 43 wrapped->bignum.flags = BN_FLG_STATIC_DATA; 44 return wrapped; 45} 46 47static void ec_wrapped_scalar_free(EC_WRAPPED_SCALAR *scalar) { 48 OPENSSL_free(scalar); 49} 50 51EC_KEY *EC_KEY_new(void) { return EC_KEY_new_method(NULL); } 52 53EC_KEY *EC_KEY_new_method(const ENGINE *engine) { 54 EC_KEY *ret = reinterpret_cast<EC_KEY *>(OPENSSL_zalloc(sizeof(EC_KEY))); 55 if (ret == NULL) { 56 return NULL; 57 } 58 59 if (engine) { 60 ret->ecdsa_meth = ENGINE_get_ECDSA_method(engine); 61 } 62 if (ret->ecdsa_meth) { 63 METHOD_ref(ret->ecdsa_meth); 64 } 65 66 ret->conv_form = POINT_CONVERSION_UNCOMPRESSED; 67 ret->references = 1; 68 69 CRYPTO_new_ex_data(&ret->ex_data); 70 71 if (ret->ecdsa_meth && ret->ecdsa_meth->init && !ret->ecdsa_meth->init(ret)) { 72 CRYPTO_free_ex_data(g_ec_ex_data_class_bss_get(), ret, &ret->ex_data); 73 if (ret->ecdsa_meth) { 74 METHOD_unref(ret->ecdsa_meth); 75 } 76 OPENSSL_free(ret); 77 return NULL; 78 } 79 80 return ret; 81} 82 83EC_KEY *EC_KEY_new_by_curve_name(int nid) { 84 EC_KEY *ret = EC_KEY_new(); 85 if (ret == NULL) { 86 return NULL; 87 } 88 ret->group = EC_GROUP_new_by_curve_name(nid); 89 if (ret->group == NULL) { 90 EC_KEY_free(ret); 91 return NULL; 92 } 93 return ret; 94} 95 96void EC_KEY_free(EC_KEY *r) { 97 if (r == NULL) { 98 return; 99 } 100 101 if (!CRYPTO_refcount_dec_and_test_zero(&r->references)) { 102 return; 103 } 104 105 if (r->ecdsa_meth) { 106 if (r->ecdsa_meth->finish) { 107 r->ecdsa_meth->finish(r); 108 } 109 METHOD_unref(r->ecdsa_meth); 110 } 111 112 CRYPTO_free_ex_data(g_ec_ex_data_class_bss_get(), r, &r->ex_data); 113 114 EC_GROUP_free(r->group); 115 EC_POINT_free(r->pub_key); 116 ec_wrapped_scalar_free(r->priv_key); 117 118 OPENSSL_free(r); 119} 120 121EC_KEY *EC_KEY_dup(const EC_KEY *src) { 122 if (src == NULL) { 123 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); 124 return NULL; 125 } 126 127 EC_KEY *ret = EC_KEY_new(); 128 if (ret == NULL) { 129 return NULL; 130 } 131 132 if ((src->group != NULL && !EC_KEY_set_group(ret, src->group)) || 133 (src->pub_key != NULL && !EC_KEY_set_public_key(ret, src->pub_key)) || 134 (src->priv_key != NULL && 135 !EC_KEY_set_private_key(ret, EC_KEY_get0_private_key(src)))) { 136 EC_KEY_free(ret); 137 return NULL; 138 } 139 140 ret->enc_flag = src->enc_flag; 141 ret->conv_form = src->conv_form; 142 return ret; 143} 144 145int EC_KEY_up_ref(EC_KEY *r) { 146 CRYPTO_refcount_inc(&r->references); 147 return 1; 148} 149 150int EC_KEY_is_opaque(const EC_KEY *key) { 151 return key->ecdsa_meth && (key->ecdsa_meth->flags & ECDSA_FLAG_OPAQUE); 152} 153 154const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key) { return key->group; } 155 156int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group) { 157 // If |key| already has a group, it is an error to switch to another one. 158 if (key->group != NULL) { 159 if (EC_GROUP_cmp(key->group, group, NULL) != 0) { 160 OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH); 161 return 0; 162 } 163 return 1; 164 } 165 166 assert(key->priv_key == NULL); 167 assert(key->pub_key == NULL); 168 169 EC_GROUP_free(key->group); 170 key->group = EC_GROUP_dup(group); 171 return key->group != NULL; 172} 173 174const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key) { 175 return key->priv_key != NULL ? &key->priv_key->bignum : NULL; 176} 177 178int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key) { 179 if (key->group == NULL) { 180 OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS); 181 return 0; 182 } 183 184 EC_WRAPPED_SCALAR *scalar = ec_wrapped_scalar_new(key->group); 185 if (scalar == NULL) { 186 return 0; 187 } 188 if (!ec_bignum_to_scalar(key->group, &scalar->scalar, priv_key) || 189 // Zero is not a valid private key, so it is safe to leak the result of 190 // this comparison. 191 constant_time_declassify_int( 192 ec_scalar_is_zero(key->group, &scalar->scalar))) { 193 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_PRIVATE_KEY); 194 ec_wrapped_scalar_free(scalar); 195 return 0; 196 } 197 ec_wrapped_scalar_free(key->priv_key); 198 key->priv_key = scalar; 199 return 1; 200} 201 202const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key) { 203 return key->pub_key; 204} 205 206int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key) { 207 if (key->group == NULL) { 208 OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS); 209 return 0; 210 } 211 212 if (pub_key != NULL && EC_GROUP_cmp(key->group, pub_key->group, NULL) != 0) { 213 OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH); 214 return 0; 215 } 216 217 EC_POINT_free(key->pub_key); 218 key->pub_key = EC_POINT_dup(pub_key, key->group); 219 return (key->pub_key == NULL) ? 0 : 1; 220} 221 222unsigned int EC_KEY_get_enc_flags(const EC_KEY *key) { return key->enc_flag; } 223 224void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags) { 225 key->enc_flag = flags; 226} 227 228point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key) { 229 return key->conv_form; 230} 231 232void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform) { 233 key->conv_form = cform; 234} 235 236int EC_KEY_check_key(const EC_KEY *eckey) { 237 if (!eckey || !eckey->group || !eckey->pub_key) { 238 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); 239 return 0; 240 } 241 242 if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) { 243 OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY); 244 return 0; 245 } 246 247 // Test whether the public key is on the elliptic curve. 248 if (!EC_POINT_is_on_curve(eckey->group, eckey->pub_key, NULL)) { 249 OPENSSL_PUT_ERROR(EC, EC_R_POINT_IS_NOT_ON_CURVE); 250 return 0; 251 } 252 253 // Check the public and private keys match. 254 // 255 // NOTE: this is a FIPS pair-wise consistency check for the ECDH case. See SP 256 // 800-56Ar3, page 36. 257 if (eckey->priv_key != NULL) { 258 EC_JACOBIAN point; 259 if (!ec_point_mul_scalar_base(eckey->group, &point, 260 &eckey->priv_key->scalar)) { 261 OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB); 262 return 0; 263 } 264 // Leaking this comparison only leaks whether |eckey|'s public key was 265 // correct. 266 if (!constant_time_declassify_int(ec_GFp_simple_points_equal( 267 eckey->group, &point, &eckey->pub_key->raw))) { 268 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_PRIVATE_KEY); 269 return 0; 270 } 271 } 272 273 return 1; 274} 275 276int EC_KEY_check_fips(const EC_KEY *key) { 277 int ret = 0; 278 FIPS_service_indicator_lock_state(); 279 280 if (EC_KEY_is_opaque(key)) { 281 // Opaque keys can't be checked. 282 OPENSSL_PUT_ERROR(EC, EC_R_PUBLIC_KEY_VALIDATION_FAILED); 283 goto end; 284 } 285 286 if (!EC_KEY_check_key(key)) { 287 goto end; 288 } 289 290 if (key->priv_key) { 291 uint8_t digest[BCM_SHA256_DIGEST_LENGTH] = {0}; 292 uint8_t sig[ECDSA_MAX_FIXED_LEN]; 293 size_t sig_len; 294 if (!ecdsa_sign_fixed(digest, sizeof(digest), sig, &sig_len, sizeof(sig), 295 key)) { 296 goto end; 297 } 298 if (boringssl_fips_break_test("ECDSA_PWCT")) { 299 digest[0] = ~digest[0]; 300 } 301 if (!ecdsa_verify_fixed(digest, sizeof(digest), sig, sig_len, key)) { 302 OPENSSL_PUT_ERROR(EC, EC_R_PUBLIC_KEY_VALIDATION_FAILED); 303 goto end; 304 } 305 } 306 307 ret = 1; 308 309end: 310 FIPS_service_indicator_unlock_state(); 311 if (ret) { 312 EC_KEY_keygen_verify_service_indicator(key); 313 } 314 315 return ret; 316} 317 318int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, const BIGNUM *x, 319 const BIGNUM *y) { 320 EC_POINT *point = NULL; 321 int ok = 0; 322 323 if (!key || !key->group || !x || !y) { 324 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); 325 return 0; 326 } 327 328 point = EC_POINT_new(key->group); 329 if (point == NULL || 330 !EC_POINT_set_affine_coordinates_GFp(key->group, point, x, y, NULL) || 331 !EC_KEY_set_public_key(key, point) || !EC_KEY_check_key(key)) { 332 goto err; 333 } 334 335 ok = 1; 336 337err: 338 EC_POINT_free(point); 339 return ok; 340} 341 342int EC_KEY_oct2key(EC_KEY *key, const uint8_t *in, size_t len, BN_CTX *ctx) { 343 if (key->group == NULL) { 344 OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS); 345 return 0; 346 } 347 348 EC_POINT *point = EC_POINT_new(key->group); 349 int ok = point != NULL && 350 EC_POINT_oct2point(key->group, point, in, len, ctx) && 351 EC_KEY_set_public_key(key, point); 352 EC_POINT_free(point); 353 return ok; 354} 355 356size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form, 357 uint8_t **out_buf, BN_CTX *ctx) { 358 if (key == NULL || key->pub_key == NULL || key->group == NULL) { 359 OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS); 360 return 0; 361 } 362 363 return EC_POINT_point2buf(key->group, key->pub_key, form, out_buf, ctx); 364} 365 366int EC_KEY_oct2priv(EC_KEY *key, const uint8_t *in, size_t len) { 367 if (key->group == NULL) { 368 OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS); 369 return 0; 370 } 371 372 if (len != BN_num_bytes(EC_GROUP_get0_order(key->group))) { 373 OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); 374 return 0; 375 } 376 377 BIGNUM *priv_key = BN_bin2bn(in, len, NULL); 378 int ok = priv_key != NULL && // 379 EC_KEY_set_private_key(key, priv_key); 380 BN_free(priv_key); 381 return ok; 382} 383 384size_t EC_KEY_priv2oct(const EC_KEY *key, uint8_t *out, size_t max_out) { 385 if (key->group == NULL || key->priv_key == NULL) { 386 OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS); 387 return 0; 388 } 389 390 size_t len = BN_num_bytes(EC_GROUP_get0_order(key->group)); 391 if (out == NULL) { 392 return len; 393 } 394 395 if (max_out < len) { 396 OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL); 397 return 0; 398 } 399 400 size_t bytes_written; 401 ec_scalar_to_bytes(key->group, out, &bytes_written, &key->priv_key->scalar); 402 assert(bytes_written == len); 403 return len; 404} 405 406size_t EC_KEY_priv2buf(const EC_KEY *key, uint8_t **out_buf) { 407 *out_buf = NULL; 408 size_t len = EC_KEY_priv2oct(key, NULL, 0); 409 if (len == 0) { 410 return 0; 411 } 412 413 uint8_t *buf = reinterpret_cast<uint8_t *>(OPENSSL_malloc(len)); 414 if (buf == NULL) { 415 return 0; 416 } 417 418 len = EC_KEY_priv2oct(key, buf, len); 419 if (len == 0) { 420 OPENSSL_free(buf); 421 return 0; 422 } 423 424 *out_buf = buf; 425 return len; 426} 427 428int EC_KEY_generate_key(EC_KEY *key) { 429 if (key == NULL || key->group == NULL) { 430 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); 431 return 0; 432 } 433 434 // Check that the group order is FIPS compliant (FIPS 186-4 B.4.2). 435 if (EC_GROUP_order_bits(key->group) < 160) { 436 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_GROUP_ORDER); 437 return 0; 438 } 439 440 static const uint8_t kDefaultAdditionalData[32] = {0}; 441 EC_WRAPPED_SCALAR *priv_key = ec_wrapped_scalar_new(key->group); 442 EC_POINT *pub_key = EC_POINT_new(key->group); 443 if (priv_key == NULL || pub_key == NULL || 444 // Generate the private key by testing candidates (FIPS 186-4 B.4.2). 445 !ec_random_nonzero_scalar(key->group, &priv_key->scalar, 446 kDefaultAdditionalData) || 447 !ec_point_mul_scalar_base(key->group, &pub_key->raw, &priv_key->scalar)) { 448 EC_POINT_free(pub_key); 449 ec_wrapped_scalar_free(priv_key); 450 return 0; 451 } 452 453 // The public key is derived from the private key, but it is public. 454 // 455 // TODO(crbug.com/boringssl/677): This isn't quite right. While |pub_key| 456 // represents a public point, it is still in Jacobian form and the exact 457 // Jacobian representation is secret. We need to make it affine first. See 458 // discussion in the bug. 459 CONSTTIME_DECLASSIFY(&pub_key->raw, sizeof(pub_key->raw)); 460 461 ec_wrapped_scalar_free(key->priv_key); 462 key->priv_key = priv_key; 463 EC_POINT_free(key->pub_key); 464 key->pub_key = pub_key; 465 return 1; 466} 467 468int EC_KEY_generate_key_fips(EC_KEY *eckey) { 469 if (eckey == NULL || eckey->group == NULL) { 470 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); 471 return 0; 472 } 473 474 boringssl_ensure_ecc_self_test(); 475 476 if (EC_KEY_generate_key(eckey) && EC_KEY_check_fips(eckey)) { 477 return 1; 478 } 479 480 EC_POINT_free(eckey->pub_key); 481 ec_wrapped_scalar_free(eckey->priv_key); 482 eckey->pub_key = NULL; 483 eckey->priv_key = NULL; 484 return 0; 485} 486 487int EC_KEY_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused, 488 CRYPTO_EX_dup *dup_unused, 489 CRYPTO_EX_free *free_func) { 490 return CRYPTO_get_ex_new_index_ex(g_ec_ex_data_class_bss_get(), argl, argp, 491 free_func); 492} 493 494int EC_KEY_set_ex_data(EC_KEY *d, int idx, void *arg) { 495 return CRYPTO_set_ex_data(&d->ex_data, idx, arg); 496} 497 498void *EC_KEY_get_ex_data(const EC_KEY *d, int idx) { 499 return CRYPTO_get_ex_data(&d->ex_data, idx); 500} 501 502void EC_KEY_set_asn1_flag(EC_KEY *key, int flag) {} 503