| 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 |
// Laboratory |
| 8 |
// |
| 9 |
// SPDX-License-Identifier: Apache-2.0 |
| 10 |
|
| 11 |
#include <openssl/ec.h> |
| 12 |
|
| 13 |
#include <assert.h> |
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
#include <openssl/bn.h> |
| 17 |
#include <openssl/err.h> |
| 18 |
#include <openssl/mem.h> |
| 19 |
#include <openssl/nid.h> |
| 20 |
|
| 21 |
#include "internal.h" |
| 22 |
#include "../../internal.h" |
| 23 |
#include "../bn/internal.h" |
| 24 |
#include "../delocate.h" |
| 25 |
|
| 26 |
#include "builtin_curves.h" |
| 27 |
|
| 28 |
|
| 29 |
static void ec_point_free(EC_POINT *point, int free_group); |
| 30 |
|
| 31 |
static void ec_group_init_static_mont(BN_MONT_CTX *mont, size_t num_words, |
| 32 |
const BN_ULONG *modulus, |
| 33 |
const BN_ULONG *rr, uint64_t n0) { |
| 34 |
bn_set_static_words(&mont->N, modulus, num_words); |
| 35 |
bn_set_static_words(&mont->RR, rr, num_words); |
| 36 |
#if defined(OPENSSL_64_BIT) |
| 37 |
mont->n0[0] = n0; |
| 38 |
#elif defined(OPENSSL_32_BIT) |
| 39 |
mont->n0[0] = (uint32_t)n0; |
| 40 |
mont->n0[1] = (uint32_t)(n0 >> 32); |
| 41 |
#else |
| 42 |
#error "unknown word length" |
| 43 |
#endif |
| 44 |
} |
| 45 |
|
| 46 |
static void ec_group_set_a_minus3(EC_GROUP *group) { |
| 47 |
const EC_FELEM *one = ec_felem_one(group); |
| 48 |
group->a_is_minus3 = 1; |
| 49 |
ec_felem_neg(group, &group->a, one); |
| 50 |
ec_felem_sub(group, &group->a, &group->a, one); |
| 51 |
ec_felem_sub(group, &group->a, &group->a, one); |
| 52 |
} |
| 53 |
|
| 54 |
static void ec_group_set_a_zero(EC_GROUP *group) { |
| 55 |
group->a_is_minus3 = 0; |
| 56 |
OPENSSL_memset(group->a.words, 0, sizeof(EC_FELEM)); |
| 57 |
} |
| 58 |
|
| 59 |
DEFINE_METHOD_FUNCTION(EC_GROUP, EC_group_p224) { |
| 60 |
out->curve_name = NID_secp224r1; |
| 61 |
out->comment = "NIST P-224"; |
| 62 |
// 1.3.132.0.33 |
| 63 |
static const uint8_t kOIDP224[] = {0x2b, 0x81, 0x04, 0x00, 0x21}; |
| 64 |
OPENSSL_memcpy(out->oid, kOIDP224, sizeof(kOIDP224)); |
| 65 |
out->oid_len = sizeof(kOIDP224); |
| 66 |
|
| 67 |
ec_group_init_static_mont(&out->field, OPENSSL_ARRAY_SIZE(kP224Field), |
| 68 |
kP224Field, kP224FieldRR, kP224FieldN0); |
| 69 |
ec_group_init_static_mont(&out->order, OPENSSL_ARRAY_SIZE(kP224Order), |
| 70 |
kP224Order, kP224OrderRR, kP224OrderN0); |
| 71 |
|
| 72 |
#if defined(BORINGSSL_HAS_UINT128) && !defined(OPENSSL_SMALL) |
| 73 |
out->meth = EC_GFp_nistp224_method(); |
| 74 |
OPENSSL_memcpy(out->generator.raw.X.words, kP224GX, sizeof(kP224GX)); |
| 75 |
OPENSSL_memcpy(out->generator.raw.Y.words, kP224GY, sizeof(kP224GY)); |
| 76 |
out->generator.raw.Z.words[0] = 1; |
| 77 |
OPENSSL_memcpy(out->b.words, kP224B, sizeof(kP224B)); |
| 78 |
#else |
| 79 |
out->meth = EC_GFp_mont_method(); |
| 80 |
OPENSSL_memcpy(out->generator.raw.X.words, kP224MontGX, sizeof(kP224MontGX)); |
| 81 |
OPENSSL_memcpy(out->generator.raw.Y.words, kP224MontGY, sizeof(kP224MontGY)); |
| 82 |
OPENSSL_memcpy(out->generator.raw.Z.words, kP224FieldR, sizeof(kP224FieldR)); |
| 83 |
OPENSSL_memcpy(out->b.words, kP224MontB, sizeof(kP224MontB)); |
| 84 |
#endif |
| 85 |
out->generator.group = out; |
| 86 |
|
| 87 |
ec_group_set_a_minus3(out); |
| 88 |
out->has_order = 1; |
| 89 |
out->field_greater_than_order = 1; |
| 90 |
out->conv_form = POINT_CONVERSION_UNCOMPRESSED; |
| 91 |
out->mutable_ec_group = 0; |
| 92 |
} |
| 93 |
|
| 94 |
DEFINE_METHOD_FUNCTION(EC_GROUP, EC_group_p256) { |
| 95 |
out->curve_name = NID_X9_62_prime256v1; |
| 96 |
out->comment = "NIST P-256"; |
| 97 |
// 1.2.840.10045.3.1.7 |
| 98 |
static const uint8_t kOIDP256[] = {0x2a, 0x86, 0x48, 0xce, |
| 99 |
0x3d, 0x03, 0x01, 0x07}; |
| 100 |
OPENSSL_memcpy(out->oid, kOIDP256, sizeof(kOIDP256)); |
| 101 |
out->oid_len = sizeof(kOIDP256); |
| 102 |
|
| 103 |
ec_group_init_static_mont(&out->field, OPENSSL_ARRAY_SIZE(kP256Field), |
| 104 |
kP256Field, kP256FieldRR, kP256FieldN0); |
| 105 |
ec_group_init_static_mont(&out->order, OPENSSL_ARRAY_SIZE(kP256Order), |
| 106 |
kP256Order, kP256OrderRR, kP256OrderN0); |
| 107 |
|
| 108 |
#if !defined(OPENSSL_NO_ASM) && \ |
| 109 |
(defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \ |
| 110 |
!defined(OPENSSL_SMALL) |
| 111 |
out->meth = EC_GFp_nistz256_method(); |
| 112 |
#else |
| 113 |
out->meth = EC_GFp_nistp256_method(); |
| 114 |
#endif |
| 115 |
out->generator.group = out; |
| 116 |
OPENSSL_memcpy(out->generator.raw.X.words, kP256MontGX, sizeof(kP256MontGX)); |
| 117 |
OPENSSL_memcpy(out->generator.raw.Y.words, kP256MontGY, sizeof(kP256MontGY)); |
| 118 |
OPENSSL_memcpy(out->generator.raw.Z.words, kP256FieldR, sizeof(kP256FieldR)); |
| 119 |
OPENSSL_memcpy(out->b.words, kP256MontB, sizeof(kP256MontB)); |
| 120 |
|
| 121 |
ec_group_set_a_minus3(out); |
| 122 |
out->has_order = 1; |
| 123 |
out->field_greater_than_order = 1; |
| 124 |
out->conv_form = POINT_CONVERSION_UNCOMPRESSED; |
| 125 |
out->mutable_ec_group = 0; |
| 126 |
} |
| 127 |
|
| 128 |
DEFINE_METHOD_FUNCTION(EC_GROUP, EC_group_p384) { |
| 129 |
out->curve_name = NID_secp384r1; |
| 130 |
out->comment = "NIST P-384"; |
| 131 |
// 1.3.132.0.34 |
| 132 |
static const uint8_t kOIDP384[] = {0x2b, 0x81, 0x04, 0x00, 0x22}; |
| 133 |
OPENSSL_memcpy(out->oid, kOIDP384, sizeof(kOIDP384)); |
| 134 |
out->oid_len = sizeof(kOIDP384); |
| 135 |
|
| 136 |
ec_group_init_static_mont(&out->field, OPENSSL_ARRAY_SIZE(kP384Field), |
| 137 |
kP384Field, kP384FieldRR, kP384FieldN0); |
| 138 |
ec_group_init_static_mont(&out->order, OPENSSL_ARRAY_SIZE(kP384Order), |
| 139 |
kP384Order, kP384OrderRR, kP384OrderN0); |
| 140 |
|
| 141 |
#if !defined(OPENSSL_SMALL) |
| 142 |
out->meth = EC_GFp_nistp384_method(); |
| 143 |
#else |
| 144 |
out->meth = EC_GFp_mont_method(); |
| 145 |
#endif |
| 146 |
|
| 147 |
out->generator.group = out; |
| 148 |
OPENSSL_memcpy(out->generator.raw.X.words, kP384MontGX, sizeof(kP384MontGX)); |
| 149 |
OPENSSL_memcpy(out->generator.raw.Y.words, kP384MontGY, sizeof(kP384MontGY)); |
| 150 |
OPENSSL_memcpy(out->generator.raw.Z.words, kP384FieldR, sizeof(kP384FieldR)); |
| 151 |
OPENSSL_memcpy(out->b.words, kP384MontB, sizeof(kP384MontB)); |
| 152 |
|
| 153 |
ec_group_set_a_minus3(out); |
| 154 |
out->has_order = 1; |
| 155 |
out->field_greater_than_order = 1; |
| 156 |
out->conv_form = POINT_CONVERSION_UNCOMPRESSED; |
| 157 |
out->mutable_ec_group = 0; |
| 158 |
} |
| 159 |
|
| 160 |
DEFINE_METHOD_FUNCTION(EC_GROUP, EC_group_p521) { |
| 161 |
out->curve_name = NID_secp521r1; |
| 162 |
out->comment = "NIST P-521"; |
| 163 |
// 1.3.132.0.35 |
| 164 |
static const uint8_t kOIDP521[] = {0x2b, 0x81, 0x04, 0x00, 0x23}; |
| 165 |
OPENSSL_memcpy(out->oid, kOIDP521, sizeof(kOIDP521)); |
| 166 |
out->oid_len = sizeof(kOIDP521); |
| 167 |
|
| 168 |
ec_group_init_static_mont(&out->field, OPENSSL_ARRAY_SIZE(kP521Field), |
| 169 |
kP521Field, kP521FieldRR, kP521FieldN0); |
| 170 |
ec_group_init_static_mont(&out->order, OPENSSL_ARRAY_SIZE(kP521Order), |
| 171 |
kP521Order, kP521OrderRR, kP521OrderN0); |
| 172 |
|
| 173 |
#if !defined(OPENSSL_SMALL) |
| 174 |
out->meth = EC_GFp_nistp521_method(); |
| 175 |
OPENSSL_memcpy(out->generator.raw.X.words, kP521GX, sizeof(kP521GX)); |
| 176 |
OPENSSL_memcpy(out->generator.raw.Y.words, kP521GY, sizeof(kP521GY)); |
| 177 |
out->generator.raw.Z.words[0] = 1; |
| 178 |
OPENSSL_memcpy(out->b.words, kP521B, sizeof(kP521B)); |
| 179 |
#else |
| 180 |
out->meth = EC_GFp_mont_method(); |
| 181 |
OPENSSL_memcpy(out->generator.raw.X.words, kP521MontGX, sizeof(kP521MontGX)); |
| 182 |
OPENSSL_memcpy(out->generator.raw.Y.words, kP521MontGY, sizeof(kP521MontGY)); |
| 183 |
OPENSSL_memcpy(out->generator.raw.Z.words, kP521FieldR, sizeof(kP521FieldR)); |
| 184 |
OPENSSL_memcpy(out->b.words, kP521MontB, sizeof(kP521MontB)); |
| 185 |
#endif |
| 186 |
|
| 187 |
out->generator.group = out; |
| 188 |
|
| 189 |
ec_group_set_a_minus3(out); |
| 190 |
out->has_order = 1; |
| 191 |
out->field_greater_than_order = 1; |
| 192 |
out->conv_form = POINT_CONVERSION_UNCOMPRESSED; |
| 193 |
out->mutable_ec_group = 0; |
| 194 |
} |
| 195 |
|
| 196 |
DEFINE_METHOD_FUNCTION(EC_GROUP, EC_group_secp256k1) { |
| 197 |
out->curve_name = NID_secp256k1; |
| 198 |
out->comment = "secp256k1"; |
| 199 |
// 1.3.132.0.10 |
| 200 |
static const uint8_t kOIDP256K1[] = {0x2b, 0x81, 0x04, 0x00, 0x0a}; |
| 201 |
OPENSSL_memcpy(out->oid, kOIDP256K1, sizeof(kOIDP256K1)); |
| 202 |
out->oid_len = sizeof(kOIDP256K1); |
| 203 |
|
| 204 |
ec_group_init_static_mont(&out->field, OPENSSL_ARRAY_SIZE(ksecp256k1Field), |
| 205 |
ksecp256k1Field, ksecp256k1FieldRR, ksecp256k1FieldN0); |
| 206 |
ec_group_init_static_mont(&out->order, OPENSSL_ARRAY_SIZE(ksecp256k1Order), |
| 207 |
ksecp256k1Order, ksecp256k1OrderRR, ksecp256k1OrderN0); |
| 208 |
|
| 209 |
|
| 210 |
out->meth = EC_GFp_mont_method(); |
| 211 |
out->generator.group = out; |
| 212 |
OPENSSL_memcpy(out->generator.raw.X.words, ksecp256k1MontGX, sizeof(ksecp256k1MontGX)); |
| 213 |
OPENSSL_memcpy(out->generator.raw.Y.words, ksecp256k1MontGY, sizeof(ksecp256k1MontGY)); |
| 214 |
OPENSSL_memcpy(out->generator.raw.Z.words, ksecp256k1FieldR, sizeof(ksecp256k1FieldR)); |
| 215 |
OPENSSL_memcpy(out->b.words, ksecp256k1MontB, sizeof(ksecp256k1MontB)); |
| 216 |
|
| 217 |
ec_group_set_a_zero(out); |
| 218 |
out->has_order = 1; |
| 219 |
out->field_greater_than_order = 1; |
| 220 |
out->conv_form = POINT_CONVERSION_UNCOMPRESSED; |
| 221 |
out->mutable_ec_group = 0; |
| 222 |
} |
| 223 |
|
| 224 |
EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a, |
| 225 |
const BIGNUM *b, BN_CTX *ctx) { |
| 226 |
if (BN_num_bytes(p) > EC_MAX_BYTES) { |
| 227 |
OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FIELD); |
| 228 |
return NULL; |
| 229 |
} |
| 230 |
|
| 231 |
BN_CTX *new_ctx = NULL; |
| 232 |
if (ctx == NULL) { |
| 233 |
ctx = new_ctx = BN_CTX_new(); |
| 234 |
if (ctx == NULL) { |
| 235 |
return NULL; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
// Historically, |a| and |b| were not required to be fully reduced. |
| 240 |
// TODO(davidben): Can this be removed? |
| 241 |
EC_GROUP *ret = NULL; |
| 242 |
BN_CTX_start(ctx); |
| 243 |
BIGNUM *a_reduced = BN_CTX_get(ctx); |
| 244 |
BIGNUM *b_reduced = BN_CTX_get(ctx); |
| 245 |
if (a_reduced == NULL || b_reduced == NULL || |
| 246 |
!BN_nnmod(a_reduced, a, p, ctx) || |
| 247 |
!BN_nnmod(b_reduced, b, p, ctx)) { |
| 248 |
goto err; |
| 249 |
} |
| 250 |
|
| 251 |
ret = OPENSSL_zalloc(sizeof(EC_GROUP)); |
| 252 |
if (ret == NULL) { |
| 253 |
return NULL; |
| 254 |
} |
| 255 |
ret->mutable_ec_group = 1; |
| 256 |
ret->conv_form = POINT_CONVERSION_UNCOMPRESSED; |
| 257 |
ret->meth = EC_GFp_mont_method(); |
| 258 |
bn_mont_ctx_init(&ret->field); |
| 259 |
bn_mont_ctx_init(&ret->order); |
| 260 |
ret->generator.group = ret; |
| 261 |
if (!ec_GFp_simple_group_set_curve(ret, p, a_reduced, b_reduced, ctx)) { |
| 262 |
EC_GROUP_free(ret); |
| 263 |
ret = NULL; |
| 264 |
goto err; |
| 265 |
} |
| 266 |
|
| 267 |
err: |
| 268 |
BN_CTX_end(ctx); |
| 269 |
BN_CTX_free(new_ctx); |
| 270 |
return ret; |
| 271 |
} |
| 272 |
|
| 273 |
int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator, |
| 274 |
const BIGNUM *order, const BIGNUM *cofactor) { |
| 275 |
if (group->curve_name != NID_undef || group->has_order || |
| 276 |
EC_GROUP_cmp(generator->group, group, NULL)) { |
| 277 |
// |EC_GROUP_set_generator| may only be used with |EC_GROUP|s returned by |
| 278 |
// |EC_GROUP_new_curve_GFp| and may only used once on each group. |
| 279 |
// |generator| must be of the same |EC_GROUP| as |group|. |
| 280 |
OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 281 |
return 0; |
| 282 |
} |
| 283 |
|
| 284 |
if (BN_num_bytes(order) > EC_MAX_BYTES) { |
| 285 |
OPENSSL_PUT_ERROR(EC, EC_R_INVALID_GROUP_ORDER); |
| 286 |
return 0; |
| 287 |
} |
| 288 |
|
| 289 |
// Require a cofactor of one for custom curves, which implies prime order. |
| 290 |
if (!BN_is_one(cofactor)) { |
| 291 |
OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COFACTOR); |
| 292 |
return 0; |
| 293 |
} |
| 294 |
|
| 295 |
// Require that p < 2×order. This simplifies some ECDSA operations. |
| 296 |
// |
| 297 |
// Note any curve which did not satisfy this must have been invalid or use a |
| 298 |
// tiny prime (less than 17). See the proof in |field_element_to_scalar| in |
| 299 |
// the ECDSA implementation. |
| 300 |
int ret = 0; |
| 301 |
BIGNUM *tmp = BN_new(); |
| 302 |
if (tmp == NULL || |
| 303 |
!BN_lshift1(tmp, order)) { |
| 304 |
goto err; |
| 305 |
} |
| 306 |
if (BN_cmp(tmp, &group->field.N) <= 0) { |
| 307 |
OPENSSL_PUT_ERROR(EC, EC_R_INVALID_GROUP_ORDER); |
| 308 |
goto err; |
| 309 |
} |
| 310 |
|
| 311 |
EC_AFFINE affine; |
| 312 |
if (!ec_jacobian_to_affine(group, &affine, &generator->raw) || |
| 313 |
!BN_MONT_CTX_set(&group->order, order, NULL)) { |
| 314 |
goto err; |
| 315 |
} |
| 316 |
|
| 317 |
group->field_greater_than_order = BN_cmp(&group->field.N, order) > 0; |
| 318 |
group->generator.raw.X = affine.X; |
| 319 |
group->generator.raw.Y = affine.Y; |
| 320 |
// |raw.Z| was set to 1 by |EC_GROUP_new_curve_GFp|. |
| 321 |
group->has_order = 1; |
| 322 |
ret = 1; |
| 323 |
|
| 324 |
err: |
| 325 |
BN_free(tmp); |
| 326 |
return ret; |
| 327 |
} |
| 328 |
|
| 329 |
EC_GROUP *EC_GROUP_new_by_curve_name(int nid) { |
| 330 |
switch (nid) { |
| 331 |
case NID_secp224r1: |
| 332 |
return (EC_GROUP *)EC_group_p224(); |
| 333 |
case NID_X9_62_prime256v1: |
| 334 |
return (EC_GROUP *)EC_group_p256(); |
| 335 |
case NID_secp384r1: |
| 336 |
return (EC_GROUP *)EC_group_p384(); |
| 337 |
case NID_secp521r1: |
| 338 |
return (EC_GROUP *)EC_group_p521(); |
| 339 |
case NID_secp256k1: |
| 340 |
return (EC_GROUP *)EC_group_secp256k1(); |
| 341 |
default: |
| 342 |
OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP); |
| 343 |
return NULL; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
EC_GROUP *EC_GROUP_new_by_curve_name_mutable(int nid) { |
| 348 |
EC_GROUP *ret = NULL; |
| 349 |
switch (nid) { |
| 350 |
case NID_secp224r1: |
| 351 |
ret = (EC_GROUP *)OPENSSL_memdup(EC_group_p224(), sizeof(EC_GROUP)); |
| 352 |
break; |
| 353 |
case NID_X9_62_prime256v1: |
| 354 |
ret = (EC_GROUP *)OPENSSL_memdup(EC_group_p256(), sizeof(EC_GROUP)); |
| 355 |
break; |
| 356 |
case NID_secp384r1: |
| 357 |
ret = (EC_GROUP *)OPENSSL_memdup(EC_group_p384(), sizeof(EC_GROUP)); |
| 358 |
break; |
| 359 |
case NID_secp521r1: |
| 360 |
ret = (EC_GROUP *)OPENSSL_memdup(EC_group_p521(), sizeof(EC_GROUP)); |
| 361 |
break; |
| 362 |
case NID_secp256k1: |
| 363 |
ret = (EC_GROUP *)OPENSSL_memdup(EC_group_secp256k1(), sizeof(EC_GROUP)); |
| 364 |
break; |
| 365 |
default: |
| 366 |
OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP); |
| 367 |
return NULL; |
| 368 |
} |
| 369 |
if (ret == NULL) { |
| 370 |
return NULL; |
| 371 |
} |
| 372 |
ret->mutable_ec_group = 1; |
| 373 |
return ret; |
| 374 |
} |
| 375 |
|
| 376 |
void EC_GROUP_free(EC_GROUP *group) { |
| 377 |
if (group == NULL) { |
| 378 |
return; |
| 379 |
} |
| 380 |
|
| 381 |
if (!group->mutable_ec_group) { |
| 382 |
if (group->curve_name != NID_undef) { |
| 383 |
// Built-in curves are static. |
| 384 |
return; |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
bn_mont_ctx_cleanup(&group->order); |
| 389 |
bn_mont_ctx_cleanup(&group->field); |
| 390 |
OPENSSL_free(group); |
| 391 |
} |
| 392 |
|
| 393 |
EC_GROUP *EC_GROUP_dup(const EC_GROUP *a) { |
| 394 |
if (a == NULL) { |
| 395 |
return NULL; |
| 396 |
} |
| 397 |
|
| 398 |
if (!a->mutable_ec_group) { |
| 399 |
if (a->curve_name != NID_undef) { |
| 400 |
// Built-in curves are static. |
| 401 |
return (EC_GROUP *)a; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
// Directly duplicate the |EC_GROUP| if it was dynamically allocated. We do a |
| 406 |
// shallow copy first, then deep copy the elements that have nested pointer |
| 407 |
// redirections. |
| 408 |
EC_GROUP *ret = OPENSSL_memdup(a, sizeof(EC_GROUP)); |
| 409 |
if (ret == NULL) { |
| 410 |
return NULL; |
| 411 |
} |
| 412 |
ret->generator.group = ret; |
| 413 |
bn_mont_ctx_init(&ret->field); |
| 414 |
bn_mont_ctx_init(&ret->order); |
| 415 |
if (!BN_MONT_CTX_copy(&ret->field, &a->field) || |
| 416 |
!BN_MONT_CTX_copy(&ret->order, &a->order)) { |
| 417 |
EC_GROUP_free(ret); |
| 418 |
ret = NULL; |
| 419 |
} |
| 420 |
return ret; |
| 421 |
} |
| 422 |
|
| 423 |
int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ignored) { |
| 424 |
// Note this function returns 0 if equal and non-zero otherwise. |
| 425 |
if (a == b) { |
| 426 |
return 0; |
| 427 |
} |
| 428 |
// Built-in static curves may be compared by curve name alone. |
| 429 |
if (a->curve_name != b->curve_name) { |
| 430 |
return 1; |
| 431 |
} |
| 432 |
if (a->curve_name != NID_undef) { |
| 433 |
// |NID_undef| indicates a custom curve. If we're comparing custom curves |
| 434 |
// we fall through and compare the entire curve structure below. |
| 435 |
return 0; |
| 436 |
} |
| 437 |
|
| 438 |
// |a| and |b| are both custom curves. If both are incomplete (due to legacy |
| 439 |
// OpenSSL mistakes, custom curve construction is sadly done in two parts |
| 440 |
// |EC_GROUP_new_curve_GFp| -> |EC_GROUP_set_generator|), we only compare |
| 441 |
// the parts that are available. |
| 442 |
return a->meth != b->meth || a->has_order != b->has_order || |
| 443 |
BN_cmp(&a->field.N, &b->field.N) != 0 || |
| 444 |
!ec_felem_equal(a, &a->a, &b->a) || !ec_felem_equal(a, &a->b, &b->b) || |
| 445 |
// We compare the rest of the entire curve structure if both |a| and |
| 446 |
// |b| are complete. |
| 447 |
(a->has_order && b->has_order && |
| 448 |
(BN_cmp(&a->order.N, &b->order.N) != 0 || |
| 449 |
!ec_GFp_simple_points_equal(a, &a->generator.raw, |
| 450 |
&b->generator.raw))); |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group) { |
| 455 |
return group->has_order ? &group->generator : NULL; |
| 456 |
} |
| 457 |
|
| 458 |
const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group) { |
| 459 |
assert(group->has_order); |
| 460 |
return &group->order.N; |
| 461 |
} |
| 462 |
|
| 463 |
int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx) { |
| 464 |
if (BN_copy(order, EC_GROUP_get0_order(group)) == NULL) { |
| 465 |
return 0; |
| 466 |
} |
| 467 |
return 1; |
| 468 |
} |
| 469 |
|
| 470 |
int EC_GROUP_order_bits(const EC_GROUP *group) { |
| 471 |
return BN_num_bits(&group->order.N); |
| 472 |
} |
| 473 |
|
| 474 |
int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, |
| 475 |
BN_CTX *ctx) { |
| 476 |
// All |EC_GROUP|s have cofactor 1. |
| 477 |
return BN_set_word(cofactor, 1); |
| 478 |
} |
| 479 |
|
| 480 |
int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *out_p, BIGNUM *out_a, |
| 481 |
BIGNUM *out_b, BN_CTX *ctx) { |
| 482 |
return ec_GFp_simple_group_get_curve(group, out_p, out_a, out_b); |
| 483 |
} |
| 484 |
|
| 485 |
int EC_GROUP_get_curve_name(const EC_GROUP *group) { return group->curve_name; } |
| 486 |
|
| 487 |
unsigned EC_GROUP_get_degree(const EC_GROUP *group) { |
| 488 |
return BN_num_bits(&group->field.N); |
| 489 |
} |
| 490 |
|
| 491 |
const char *EC_curve_nid2nist(int nid) { |
| 492 |
switch (nid) { |
| 493 |
case NID_secp224r1: |
| 494 |
return "P-224"; |
| 495 |
case NID_X9_62_prime256v1: |
| 496 |
return "P-256"; |
| 497 |
case NID_secp384r1: |
| 498 |
return "P-384"; |
| 499 |
case NID_secp521r1: |
| 500 |
return "P-521"; |
| 501 |
} |
| 502 |
return NULL; |
| 503 |
} |
| 504 |
|
| 505 |
int EC_curve_nist2nid(const char *name) { |
| 506 |
if (strcmp(name, "P-224") == 0) { |
| 507 |
return NID_secp224r1; |
| 508 |
} |
| 509 |
if (strcmp(name, "P-256") == 0) { |
| 510 |
return NID_X9_62_prime256v1; |
| 511 |
} |
| 512 |
if (strcmp(name, "P-384") == 0) { |
| 513 |
return NID_secp384r1; |
| 514 |
} |
| 515 |
if (strcmp(name, "P-521") == 0) { |
| 516 |
return NID_secp521r1; |
| 517 |
} |
| 518 |
return NID_undef; |
| 519 |
} |
| 520 |
|
| 521 |
EC_POINT *EC_POINT_new(const EC_GROUP *group) { |
| 522 |
if (group == NULL) { |
| 523 |
OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); |
| 524 |
return NULL; |
| 525 |
} |
| 526 |
|
| 527 |
EC_POINT *ret = OPENSSL_malloc(sizeof *ret); |
| 528 |
if (ret == NULL) { |
| 529 |
return NULL; |
| 530 |
} |
| 531 |
|
| 532 |
ret->group = EC_GROUP_dup(group); |
| 533 |
ec_GFp_simple_point_init(&ret->raw); |
| 534 |
return ret; |
| 535 |
} |
| 536 |
|
| 537 |
static void ec_point_free(EC_POINT *point, int free_group) { |
| 538 |
if (!point) { |
| 539 |
return; |
| 540 |
} |
| 541 |
if (free_group) { |
| 542 |
EC_GROUP_free(point->group); |
| 543 |
} |
| 544 |
OPENSSL_free(point); |
| 545 |
} |
| 546 |
|
| 547 |
void EC_POINT_free(EC_POINT *point) { |
| 548 |
ec_point_free(point, 1 /* free group */); |
| 549 |
} |
| 550 |
|
| 551 |
void EC_POINT_clear_free(EC_POINT *point) { EC_POINT_free(point); } |
| 552 |
|
| 553 |
int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src) { |
| 554 |
if (EC_GROUP_cmp(dest->group, src->group, NULL) != 0) { |
| 555 |
OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
| 556 |
return 0; |
| 557 |
} |
| 558 |
if (dest == src) { |
| 559 |
return 1; |
| 560 |
} |
| 561 |
ec_GFp_simple_point_copy(&dest->raw, &src->raw); |
| 562 |
return 1; |
| 563 |
} |
| 564 |
|
| 565 |
EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group) { |
| 566 |
if (a == NULL) { |
| 567 |
return NULL; |
| 568 |
} |
| 569 |
|
| 570 |
EC_POINT *ret = EC_POINT_new(group); |
| 571 |
if (ret == NULL || !EC_POINT_copy(ret, a)) { |
| 572 |
EC_POINT_free(ret); |
| 573 |
return NULL; |
| 574 |
} |
| 575 |
|
| 576 |
return ret; |
| 577 |
} |
| 578 |
|
| 579 |
int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point) { |
| 580 |
if (EC_GROUP_cmp(group, point->group, NULL) != 0) { |
| 581 |
OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
| 582 |
return 0; |
| 583 |
} |
| 584 |
ec_GFp_simple_point_set_to_infinity(group, &point->raw); |
| 585 |
return 1; |
| 586 |
} |
| 587 |
|
| 588 |
int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point) { |
| 589 |
if (EC_GROUP_cmp(group, point->group, NULL) != 0) { |
| 590 |
OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
| 591 |
return 0; |
| 592 |
} |
| 593 |
return ec_GFp_simple_is_at_infinity(group, &point->raw); |
| 594 |
} |
| 595 |
|
| 596 |
int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point, |
| 597 |
BN_CTX *ctx) { |
| 598 |
if (EC_GROUP_cmp(group, point->group, NULL) != 0) { |
| 599 |
OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
| 600 |
return 0; |
| 601 |
} |
| 602 |
return ec_GFp_simple_is_on_curve(group, &point->raw); |
| 603 |
} |
| 604 |
|
| 605 |
int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, |
| 606 |
BN_CTX *ctx) { |
| 607 |
if (EC_GROUP_cmp(group, a->group, NULL) != 0 || |
| 608 |
EC_GROUP_cmp(group, b->group, NULL) != 0) { |
| 609 |
OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
| 610 |
return -1; |
| 611 |
} |
| 612 |
|
| 613 |
// Note |EC_POINT_cmp| returns zero for equality and non-zero for inequality. |
| 614 |
return ec_GFp_simple_points_equal(group, &a->raw, &b->raw) ? 0 : 1; |
| 615 |
} |
| 616 |
|
| 617 |
int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, |
| 618 |
const EC_POINT *point, BIGNUM *x, |
| 619 |
BIGNUM *y, BN_CTX *ctx) { |
| 620 |
if (group->meth->point_get_affine_coordinates == 0) { |
| 621 |
OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 622 |
return 0; |
| 623 |
} |
| 624 |
if (EC_GROUP_cmp(group, point->group, NULL) != 0) { |
| 625 |
OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
| 626 |
return 0; |
| 627 |
} |
| 628 |
EC_FELEM x_felem, y_felem; |
| 629 |
if (!group->meth->point_get_affine_coordinates(group, &point->raw, |
| 630 |
x == NULL ? NULL : &x_felem, |
| 631 |
y == NULL ? NULL : &y_felem) || |
| 632 |
(x != NULL && !ec_felem_to_bignum(group, x, &x_felem)) || |
| 633 |
(y != NULL && !ec_felem_to_bignum(group, y, &y_felem))) { |
| 634 |
return 0; |
| 635 |
} |
| 636 |
return 1; |
| 637 |
} |
| 638 |
|
| 639 |
int EC_POINT_get_affine_coordinates(const EC_GROUP *group, |
| 640 |
const EC_POINT *point, BIGNUM *x, BIGNUM *y, |
| 641 |
BN_CTX *ctx) { |
| 642 |
return EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx); |
| 643 |
} |
| 644 |
|
| 645 |
void ec_affine_to_jacobian(const EC_GROUP *group, EC_JACOBIAN *out, |
| 646 |
const EC_AFFINE *p) { |
| 647 |
out->X = p->X; |
| 648 |
out->Y = p->Y; |
| 649 |
out->Z = *ec_felem_one(group); |
| 650 |
} |
| 651 |
|
| 652 |
int ec_jacobian_to_affine(const EC_GROUP *group, EC_AFFINE *out, |
| 653 |
const EC_JACOBIAN *p) { |
| 654 |
return group->meth->point_get_affine_coordinates(group, p, &out->X, &out->Y); |
| 655 |
} |
| 656 |
|
| 657 |
int ec_jacobian_to_affine_batch(const EC_GROUP *group, EC_AFFINE *out, |
| 658 |
const EC_JACOBIAN *in, size_t num) { |
| 659 |
if (group->meth->jacobian_to_affine_batch == NULL) { |
| 660 |
OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 661 |
return 0; |
| 662 |
} |
| 663 |
return group->meth->jacobian_to_affine_batch(group, out, in, num); |
| 664 |
} |
| 665 |
|
| 666 |
int ec_point_set_affine_coordinates(const EC_GROUP *group, EC_AFFINE *out, |
| 667 |
const EC_FELEM *x, const EC_FELEM *y) { |
| 668 |
void (*const felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a, |
| 669 |
const EC_FELEM *b) = group->meth->felem_mul; |
| 670 |
void (*const felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a) = |
| 671 |
group->meth->felem_sqr; |
| 672 |
|
| 673 |
// Check if the point is on the curve. |
| 674 |
EC_FELEM lhs, rhs; |
| 675 |
felem_sqr(group, &lhs, y); // lhs = y^2 |
| 676 |
felem_sqr(group, &rhs, x); // rhs = x^2 |
| 677 |
ec_felem_add(group, &rhs, &rhs, &group->a); // rhs = x^2 + a |
| 678 |
felem_mul(group, &rhs, &rhs, x); // rhs = x^3 + ax |
| 679 |
ec_felem_add(group, &rhs, &rhs, &group->b); // rhs = x^3 + ax + b |
| 680 |
|
| 681 |
if (!ec_felem_equal(group, &lhs, &rhs)) { |
| 682 |
OPENSSL_PUT_ERROR(EC, EC_R_POINT_IS_NOT_ON_CURVE); |
| 683 |
// In the event of an error, defend against the caller not checking the |
| 684 |
// return value by setting a known safe value. Note this may not be possible |
| 685 |
// if the caller is in the process of constructing an arbitrary group and |
| 686 |
// the generator is missing. |
| 687 |
if (group->has_order) { |
| 688 |
out->X = group->generator.raw.X; |
| 689 |
out->Y = group->generator.raw.Y; |
| 690 |
} |
| 691 |
return 0; |
| 692 |
} |
| 693 |
|
| 694 |
out->X = *x; |
| 695 |
out->Y = *y; |
| 696 |
return 1; |
| 697 |
} |
| 698 |
|
| 699 |
int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point, |
| 700 |
const BIGNUM *x, const BIGNUM *y, |
| 701 |
BN_CTX *ctx) { |
| 702 |
if (EC_GROUP_cmp(group, point->group, NULL) != 0) { |
| 703 |
OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
| 704 |
return 0; |
| 705 |
} |
| 706 |
|
| 707 |
if (x == NULL || y == NULL) { |
| 708 |
OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); |
| 709 |
return 0; |
| 710 |
} |
| 711 |
|
| 712 |
EC_FELEM x_felem, y_felem; |
| 713 |
EC_AFFINE affine; |
| 714 |
if (!ec_bignum_to_felem(group, &x_felem, x) || |
| 715 |
!ec_bignum_to_felem(group, &y_felem, y) || |
| 716 |
!ec_point_set_affine_coordinates(group, &affine, &x_felem, &y_felem)) { |
| 717 |
// In the event of an error, defend against the caller not checking the |
| 718 |
// return value by setting a known safe value. |
| 719 |
ec_set_to_safe_point(group, &point->raw); |
| 720 |
return 0; |
| 721 |
} |
| 722 |
|
| 723 |
ec_affine_to_jacobian(group, &point->raw, &affine); |
| 724 |
return 1; |
| 725 |
} |
| 726 |
|
| 727 |
int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point, |
| 728 |
const BIGNUM *x, const BIGNUM *y, |
| 729 |
BN_CTX *ctx) { |
| 730 |
return EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx); |
| 731 |
} |
| 732 |
|
| 733 |
int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, |
| 734 |
const EC_POINT *b, BN_CTX *ctx) { |
| 735 |
if (EC_GROUP_cmp(group, r->group, NULL) != 0 || |
| 736 |
EC_GROUP_cmp(group, a->group, NULL) != 0 || |
| 737 |
EC_GROUP_cmp(group, b->group, NULL) != 0) { |
| 738 |
OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
| 739 |
return 0; |
| 740 |
} |
| 741 |
group->meth->add(group, &r->raw, &a->raw, &b->raw); |
| 742 |
return 1; |
| 743 |
} |
| 744 |
|
| 745 |
int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, |
| 746 |
BN_CTX *ctx) { |
| 747 |
if (EC_GROUP_cmp(group, r->group, NULL) != 0 || |
| 748 |
EC_GROUP_cmp(group, a->group, NULL) != 0) { |
| 749 |
OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
| 750 |
return 0; |
| 751 |
} |
| 752 |
group->meth->dbl(group, &r->raw, &a->raw); |
| 753 |
return 1; |
| 754 |
} |
| 755 |
|
| 756 |
|
| 757 |
int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx) { |
| 758 |
if (EC_GROUP_cmp(group, a->group, NULL) != 0) { |
| 759 |
OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
| 760 |
return 0; |
| 761 |
} |
| 762 |
ec_GFp_simple_invert(group, &a->raw); |
| 763 |
return 1; |
| 764 |
} |
| 765 |
|
| 766 |
static int arbitrary_bignum_to_scalar(const EC_GROUP *group, EC_SCALAR *out, |
| 767 |
const BIGNUM *in, BN_CTX *ctx) { |
| 768 |
if (ec_bignum_to_scalar(group, out, in)) { |
| 769 |
return 1; |
| 770 |
} |
| 771 |
|
| 772 |
ERR_clear_error(); |
| 773 |
|
| 774 |
// This is an unusual input, so we do not guarantee constant-time processing. |
| 775 |
BN_CTX_start(ctx); |
| 776 |
BIGNUM *tmp = BN_CTX_get(ctx); |
| 777 |
int ok = tmp != NULL && |
| 778 |
BN_nnmod(tmp, in, EC_GROUP_get0_order(group), ctx) && |
| 779 |
ec_bignum_to_scalar(group, out, tmp); |
| 780 |
BN_CTX_end(ctx); |
| 781 |
return ok; |
| 782 |
} |
| 783 |
|
| 784 |
int ec_point_mul_no_self_test(const EC_GROUP *group, EC_POINT *r, |
| 785 |
const BIGNUM *g_scalar, const EC_POINT *p, |
| 786 |
const BIGNUM *p_scalar, BN_CTX *ctx) { |
| 787 |
// Previously, this function set |r| to the point at infinity if there was |
| 788 |
// nothing to multiply. But, nobody should be calling this function with |
| 789 |
// nothing to multiply in the first place. |
| 790 |
if ((g_scalar == NULL && p_scalar == NULL) || |
| 791 |
(p == NULL) != (p_scalar == NULL)) { |
| 792 |
OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); |
| 793 |
return 0; |
| 794 |
} |
| 795 |
|
| 796 |
if (EC_GROUP_cmp(group, r->group, NULL) != 0 || |
| 797 |
(p != NULL && EC_GROUP_cmp(group, p->group, NULL) != 0)) { |
| 798 |
OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
| 799 |
return 0; |
| 800 |
} |
| 801 |
|
| 802 |
int ret = 0; |
| 803 |
BN_CTX *new_ctx = NULL; |
| 804 |
if (ctx == NULL) { |
| 805 |
new_ctx = BN_CTX_new(); |
| 806 |
if (new_ctx == NULL) { |
| 807 |
goto err; |
| 808 |
} |
| 809 |
ctx = new_ctx; |
| 810 |
} |
| 811 |
|
| 812 |
// If both |g_scalar| and |p_scalar| are non-NULL, |
| 813 |
// |ec_point_mul_scalar_public| would share the doublings between the two |
| 814 |
// products, which would be more efficient. However, we conservatively assume |
| 815 |
// the caller needs a constant-time operation. (ECDSA verification does not |
| 816 |
// use this function.) |
| 817 |
// |
| 818 |
// Previously, the low-level constant-time multiplication function aligned |
| 819 |
// with this function's calling convention, but this was misleading. Curves |
| 820 |
// which combined the two multiplications did not avoid the doubling case |
| 821 |
// in the incomplete addition formula and were not constant-time. |
| 822 |
|
| 823 |
if (g_scalar != NULL) { |
| 824 |
EC_SCALAR scalar; |
| 825 |
if (!arbitrary_bignum_to_scalar(group, &scalar, g_scalar, ctx) || |
| 826 |
!ec_point_mul_scalar_base(group, &r->raw, &scalar)) { |
| 827 |
goto err; |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
if (p_scalar != NULL) { |
| 832 |
EC_SCALAR scalar; |
| 833 |
EC_JACOBIAN tmp; |
| 834 |
if (!arbitrary_bignum_to_scalar(group, &scalar, p_scalar, ctx) || |
| 835 |
!ec_point_mul_scalar(group, &tmp, &p->raw, &scalar)) { |
| 836 |
goto err; |
| 837 |
} |
| 838 |
if (g_scalar == NULL) { |
| 839 |
OPENSSL_memcpy(&r->raw, &tmp, sizeof(EC_JACOBIAN)); |
| 840 |
} else { |
| 841 |
group->meth->add(group, &r->raw, &r->raw, &tmp); |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
ret = 1; |
| 846 |
|
| 847 |
err: |
| 848 |
BN_CTX_free(new_ctx); |
| 849 |
return ret; |
| 850 |
} |
| 851 |
|
| 852 |
int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar, |
| 853 |
const EC_POINT *p, const BIGNUM *p_scalar, BN_CTX *ctx) { |
| 854 |
boringssl_ensure_ecc_self_test(); |
| 855 |
SET_DIT_AUTO_RESET; |
| 856 |
|
| 857 |
return ec_point_mul_no_self_test(group, r, g_scalar, p, p_scalar, ctx); |
| 858 |
} |
| 859 |
|
| 860 |
int ec_point_mul_scalar_public(const EC_GROUP *group, EC_JACOBIAN *r, |
| 861 |
const EC_SCALAR *g_scalar, const EC_JACOBIAN *p, |
| 862 |
const EC_SCALAR *p_scalar) { |
| 863 |
if (g_scalar == NULL || p_scalar == NULL || p == NULL) { |
| 864 |
OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); |
| 865 |
return 0; |
| 866 |
} |
| 867 |
|
| 868 |
if (group->meth->mul_public == NULL) { |
| 869 |
return group->meth->mul_public_batch(group, r, g_scalar, p, p_scalar, 1); |
| 870 |
} |
| 871 |
|
| 872 |
group->meth->mul_public(group, r, g_scalar, p, p_scalar); |
| 873 |
return 1; |
| 874 |
} |
| 875 |
|
| 876 |
int ec_point_mul_scalar_public_batch(const EC_GROUP *group, EC_JACOBIAN *r, |
| 877 |
const EC_SCALAR *g_scalar, |
| 878 |
const EC_JACOBIAN *points, |
| 879 |
const EC_SCALAR *scalars, size_t num) { |
| 880 |
if (group->meth->mul_public_batch == NULL) { |
| 881 |
OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 882 |
return 0; |
| 883 |
} |
| 884 |
|
| 885 |
return group->meth->mul_public_batch(group, r, g_scalar, points, scalars, |
| 886 |
num); |
| 887 |
} |
| 888 |
|
| 889 |
int ec_point_mul_scalar(const EC_GROUP *group, EC_JACOBIAN *r, |
| 890 |
const EC_JACOBIAN *p, const EC_SCALAR *scalar) { |
| 891 |
SET_DIT_AUTO_RESET; |
| 892 |
if (p == NULL || scalar == NULL) { |
| 893 |
OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); |
| 894 |
return 0; |
| 895 |
} |
| 896 |
|
| 897 |
group->meth->mul(group, r, p, scalar); |
| 898 |
|
| 899 |
// Check the result is on the curve to defend against fault attacks or bugs. |
| 900 |
// This has negligible cost compared to the multiplication. |
| 901 |
if (!ec_GFp_simple_is_on_curve(group, r)) { |
| 902 |
OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
| 903 |
return 0; |
| 904 |
} |
| 905 |
|
| 906 |
return 1; |
| 907 |
} |
| 908 |
|
| 909 |
int ec_point_mul_scalar_base(const EC_GROUP *group, EC_JACOBIAN *r, |
| 910 |
const EC_SCALAR *scalar) { |
| 911 |
SET_DIT_AUTO_RESET; |
| 912 |
if (scalar == NULL) { |
| 913 |
OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); |
| 914 |
return 0; |
| 915 |
} |
| 916 |
|
| 917 |
group->meth->mul_base(group, r, scalar); |
| 918 |
|
| 919 |
// Check the result is on the curve to defend against fault attacks or bugs. |
| 920 |
// This has negligible cost compared to the multiplication. This can only |
| 921 |
// happen on bug or CPU fault, so it is okay to leak this information (if the |
| 922 |
// computed point is on the curve or not). The alternative would be to |
| 923 |
// proceed with bad data. |
| 924 |
if (!constant_time_declassify_int(ec_GFp_simple_is_on_curve(group, r))) { |
| 925 |
OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
| 926 |
return 0; |
| 927 |
} |
| 928 |
|
| 929 |
return 1; |
| 930 |
} |
| 931 |
|
| 932 |
int ec_point_mul_scalar_batch(const EC_GROUP *group, EC_JACOBIAN *r, |
| 933 |
const EC_JACOBIAN *p0, const EC_SCALAR *scalar0, |
| 934 |
const EC_JACOBIAN *p1, const EC_SCALAR *scalar1, |
| 935 |
const EC_JACOBIAN *p2, |
| 936 |
const EC_SCALAR *scalar2) { |
| 937 |
if (group->meth->mul_batch == NULL) { |
| 938 |
OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 939 |
return 0; |
| 940 |
} |
| 941 |
|
| 942 |
group->meth->mul_batch(group, r, p0, scalar0, p1, scalar1, p2, scalar2); |
| 943 |
|
| 944 |
// Check the result is on the curve to defend against fault attacks or bugs. |
| 945 |
// This has negligible cost compared to the multiplication. |
| 946 |
if (!ec_GFp_simple_is_on_curve(group, r)) { |
| 947 |
OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
| 948 |
return 0; |
| 949 |
} |
| 950 |
|
| 951 |
return 1; |
| 952 |
} |
| 953 |
|
| 954 |
int ec_init_precomp(const EC_GROUP *group, EC_PRECOMP *out, |
| 955 |
const EC_JACOBIAN *p) { |
| 956 |
if (group->meth->init_precomp == NULL) { |
| 957 |
OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 958 |
return 0; |
| 959 |
} |
| 960 |
|
| 961 |
return group->meth->init_precomp(group, out, p); |
| 962 |
} |
| 963 |
|
| 964 |
int ec_point_mul_scalar_precomp(const EC_GROUP *group, EC_JACOBIAN *r, |
| 965 |
const EC_PRECOMP *p0, const EC_SCALAR *scalar0, |
| 966 |
const EC_PRECOMP *p1, const EC_SCALAR *scalar1, |
| 967 |
const EC_PRECOMP *p2, |
| 968 |
const EC_SCALAR *scalar2) { |
| 969 |
if (group->meth->mul_precomp == NULL) { |
| 970 |
OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 971 |
return 0; |
| 972 |
} |
| 973 |
|
| 974 |
group->meth->mul_precomp(group, r, p0, scalar0, p1, scalar1, p2, scalar2); |
| 975 |
|
| 976 |
// Check the result is on the curve to defend against fault attacks or bugs. |
| 977 |
// This has negligible cost compared to the multiplication. |
| 978 |
if (!ec_GFp_simple_is_on_curve(group, r)) { |
| 979 |
OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
| 980 |
return 0; |
| 981 |
} |
| 982 |
|
| 983 |
return 1; |
| 984 |
} |
| 985 |
|
| 986 |
void ec_point_select(const EC_GROUP *group, EC_JACOBIAN *out, BN_ULONG mask, |
| 987 |
const EC_JACOBIAN *a, const EC_JACOBIAN *b) { |
| 988 |
ec_felem_select(group, &out->X, mask, &a->X, &b->X); |
| 989 |
ec_felem_select(group, &out->Y, mask, &a->Y, &b->Y); |
| 990 |
ec_felem_select(group, &out->Z, mask, &a->Z, &b->Z); |
| 991 |
} |
| 992 |
|
| 993 |
void ec_affine_select(const EC_GROUP *group, EC_AFFINE *out, BN_ULONG mask, |
| 994 |
const EC_AFFINE *a, const EC_AFFINE *b) { |
| 995 |
ec_felem_select(group, &out->X, mask, &a->X, &b->X); |
| 996 |
ec_felem_select(group, &out->Y, mask, &a->Y, &b->Y); |
| 997 |
} |
| 998 |
|
| 999 |
void ec_precomp_select(const EC_GROUP *group, EC_PRECOMP *out, BN_ULONG mask, |
| 1000 |
const EC_PRECOMP *a, const EC_PRECOMP *b) { |
| 1001 |
OPENSSL_STATIC_ASSERT(sizeof(out->comb) == sizeof(*out), |
| 1002 |
out_comb_does_not_span_the_entire_structure) |
| 1003 |
for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(out->comb); i++) { |
| 1004 |
ec_affine_select(group, &out->comb[i], mask, &a->comb[i], &b->comb[i]); |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
int ec_cmp_x_coordinate(const EC_GROUP *group, const EC_JACOBIAN *p, |
| 1009 |
const EC_SCALAR *r) { |
| 1010 |
return group->meth->cmp_x_coordinate(group, p, r); |
| 1011 |
} |
| 1012 |
|
| 1013 |
int ec_get_x_coordinate_as_scalar(const EC_GROUP *group, EC_SCALAR *out, |
| 1014 |
const EC_JACOBIAN *p) { |
| 1015 |
uint8_t bytes[EC_MAX_BYTES]; |
| 1016 |
size_t len; |
| 1017 |
if (!ec_get_x_coordinate_as_bytes(group, bytes, &len, sizeof(bytes), p)) { |
| 1018 |
return 0; |
| 1019 |
} |
| 1020 |
|
| 1021 |
// The x-coordinate is bounded by p, but we need a scalar, bounded by the |
| 1022 |
// order. These may not have the same size. However, we must have p < 2×order, |
| 1023 |
// assuming p is not tiny (p >= 17). |
| 1024 |
// |
| 1025 |
// Thus |bytes| will fit in |order.width + 1| words, and we can reduce by |
| 1026 |
// performing at most one subtraction. |
| 1027 |
// |
| 1028 |
// Proof: We only work with prime order curves, so the number of points on |
| 1029 |
// the curve is the order. Thus Hasse's theorem gives: |
| 1030 |
// |
| 1031 |
// |order - (p + 1)| <= 2×sqrt(p) |
| 1032 |
// p + 1 - order <= 2×sqrt(p) |
| 1033 |
// p + 1 - 2×sqrt(p) <= order |
| 1034 |
// p + 1 - 2×(p/4) < order (p/4 > sqrt(p) for p >= 17) |
| 1035 |
// p/2 < p/2 + 1 < order |
| 1036 |
// p < 2×order |
| 1037 |
// |
| 1038 |
// Additionally, one can manually check this property for built-in curves. It |
| 1039 |
// is enforced for legacy custom curves in |EC_GROUP_set_generator|. |
| 1040 |
const BIGNUM *order = EC_GROUP_get0_order(group); |
| 1041 |
BN_ULONG words[EC_MAX_WORDS + 1] = {0}; |
| 1042 |
bn_big_endian_to_words(words, order->width + 1, bytes, len); |
| 1043 |
bn_reduce_once(out->words, words, /*carry=*/words[order->width], order->d, |
| 1044 |
order->width); |
| 1045 |
return 1; |
| 1046 |
} |
| 1047 |
|
| 1048 |
int ec_get_x_coordinate_as_bytes(const EC_GROUP *group, uint8_t *out, |
| 1049 |
size_t *out_len, size_t max_out, |
| 1050 |
const EC_JACOBIAN *p) { |
| 1051 |
size_t len = BN_num_bytes(&group->field.N); |
| 1052 |
assert(len <= EC_MAX_BYTES); |
| 1053 |
if (max_out < len) { |
| 1054 |
OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL); |
| 1055 |
return 0; |
| 1056 |
} |
| 1057 |
|
| 1058 |
EC_FELEM x; |
| 1059 |
if (!group->meth->point_get_affine_coordinates(group, p, &x, NULL)) { |
| 1060 |
return 0; |
| 1061 |
} |
| 1062 |
|
| 1063 |
ec_felem_to_bytes(group, out, out_len, &x); |
| 1064 |
*out_len = len; |
| 1065 |
return 1; |
| 1066 |
} |
| 1067 |
|
| 1068 |
void ec_set_to_safe_point(const EC_GROUP *group, EC_JACOBIAN *out) { |
| 1069 |
if (group->has_order) { |
| 1070 |
ec_GFp_simple_point_copy(out, &group->generator.raw); |
| 1071 |
} else { |
| 1072 |
// The generator can be missing if the caller is in the process of |
| 1073 |
// constructing an arbitrary group. In this case, we give up and use the |
| 1074 |
// point at infinity. |
| 1075 |
ec_GFp_simple_point_set_to_infinity(group, out); |
| 1076 |
} |
| 1077 |
} |
| 1078 |
|
| 1079 |
void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag) {} |
| 1080 |
|
| 1081 |
int EC_GROUP_get_asn1_flag(const EC_GROUP *group) { |
| 1082 |
return OPENSSL_EC_NAMED_CURVE; |
| 1083 |
} |
| 1084 |
|
| 1085 |
const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group) { |
| 1086 |
// This function exists purely to give callers a way to call |
| 1087 |
// |EC_METHOD_get_field_type|. cryptography.io crashes if |EC_GROUP_method_of| |
| 1088 |
// returns NULL, so return some other garbage pointer. |
| 1089 |
return (const EC_METHOD *)0x12340000; |
| 1090 |
} |
| 1091 |
|
| 1092 |
int EC_METHOD_get_field_type(const EC_METHOD *meth) { |
| 1093 |
return NID_X9_62_prime_field; |
| 1094 |
} |
| 1095 |
|
| 1096 |
void EC_GROUP_set_point_conversion_form(EC_GROUP *group, |
| 1097 |
point_conversion_form_t form) { |
| 1098 |
// |conv_form| can only be set with OpenSSL compatible dynamically allocated |
| 1099 |
// groups. |
| 1100 |
if (group->mutable_ec_group) { |
| 1101 |
group->conv_form = form; |
| 1102 |
} |
| 1103 |
} |
| 1104 |
|
| 1105 |
point_conversion_form_t EC_GROUP_get_point_conversion_form( |
| 1106 |
const EC_GROUP *group) { |
| 1107 |
return group->conv_form; |
| 1108 |
} |
| 1109 |
|
| 1110 |
size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *seed, |
| 1111 |
size_t len) { |
| 1112 |
return 0; |
| 1113 |
} |
| 1114 |
|
| 1115 |
unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group) { return NULL; } |
| 1116 |
|
| 1117 |
size_t EC_GROUP_get_seed_len(const EC_GROUP *group) { return 0; } |