2 * Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <openssl/err.h>
13 #include <openssl/asn1t.h>
14 #include <openssl/objects.h>
15 #include "internal/nelem.h"
17 int EC_GROUP_get_basis_type(const EC_GROUP *group)
21 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
22 NID_X9_62_characteristic_two_field)
23 /* everything else is currently not supported */
26 /* Find the last non-zero element of group->poly[] */
28 i < (int)OSSL_NELEM(group->poly) && group->poly[i] != 0;
33 return NID_X9_62_ppBasis;
35 return NID_X9_62_tpBasis;
37 /* everything else is currently not supported */
41 #ifndef OPENSSL_NO_EC2M
42 int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
47 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
48 NID_X9_62_characteristic_two_field
49 || !((group->poly[0] != 0) && (group->poly[1] != 0)
50 && (group->poly[2] == 0))) {
51 ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS,
52 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
62 int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
63 unsigned int *k2, unsigned int *k3)
68 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
69 NID_X9_62_characteristic_two_field
70 || !((group->poly[0] != 0) && (group->poly[1] != 0)
71 && (group->poly[2] != 0) && (group->poly[3] != 0)
72 && (group->poly[4] == 0))) {
73 ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS,
74 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
89 /* some structures needed for the asn1 encoding */
90 typedef struct x9_62_pentanomial_st {
96 typedef struct x9_62_characteristic_two_st {
101 /* NID_X9_62_onBasis */
103 /* NID_X9_62_tpBasis */
104 ASN1_INTEGER *tpBasis;
105 /* NID_X9_62_ppBasis */
106 X9_62_PENTANOMIAL *ppBasis;
110 } X9_62_CHARACTERISTIC_TWO;
112 typedef struct x9_62_fieldid_st {
113 ASN1_OBJECT *fieldType;
116 /* NID_X9_62_prime_field */
118 /* NID_X9_62_characteristic_two_field */
119 X9_62_CHARACTERISTIC_TWO *char_two;
125 typedef struct x9_62_curve_st {
126 ASN1_OCTET_STRING *a;
127 ASN1_OCTET_STRING *b;
128 ASN1_BIT_STRING *seed;
131 struct ec_parameters_st {
133 X9_62_FIELDID *fieldID;
135 ASN1_OCTET_STRING *base;
137 ASN1_INTEGER *cofactor;
138 } /* ECPARAMETERS */ ;
141 ECPKPARAMETERS_TYPE_NAMED = 0,
142 ECPKPARAMETERS_TYPE_EXPLICIT,
143 ECPKPARAMETERS_TYPE_IMPLICIT
144 } ecpk_parameters_type_t;
146 struct ecpk_parameters_st {
149 ASN1_OBJECT *named_curve;
150 ECPARAMETERS *parameters;
151 ASN1_NULL *implicitlyCA;
153 } /* ECPKPARAMETERS */ ;
155 /* SEC1 ECPrivateKey */
156 typedef struct ec_privatekey_st {
158 ASN1_OCTET_STRING *privateKey;
159 ECPKPARAMETERS *parameters;
160 ASN1_BIT_STRING *publicKey;
163 /* the OpenSSL ASN.1 definitions */
164 ASN1_SEQUENCE(X9_62_PENTANOMIAL) = {
165 ASN1_EMBED(X9_62_PENTANOMIAL, k1, INT32),
166 ASN1_EMBED(X9_62_PENTANOMIAL, k2, INT32),
167 ASN1_EMBED(X9_62_PENTANOMIAL, k3, INT32)
168 } static_ASN1_SEQUENCE_END(X9_62_PENTANOMIAL)
170 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
171 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
173 ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY);
175 ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = {
176 ADB_ENTRY(NID_X9_62_onBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.onBasis, ASN1_NULL)),
177 ADB_ENTRY(NID_X9_62_tpBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.tpBasis, ASN1_INTEGER)),
178 ADB_ENTRY(NID_X9_62_ppBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.ppBasis, X9_62_PENTANOMIAL))
179 } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL);
181 ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = {
182 ASN1_EMBED(X9_62_CHARACTERISTIC_TWO, m, INT32),
183 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT),
184 ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO)
185 } static_ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO)
187 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
188 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
190 ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY);
192 ASN1_ADB(X9_62_FIELDID) = {
193 ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)),
194 ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO))
195 } ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL);
197 ASN1_SEQUENCE(X9_62_FIELDID) = {
198 ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
199 ASN1_ADB_OBJECT(X9_62_FIELDID)
200 } static_ASN1_SEQUENCE_END(X9_62_FIELDID)
202 ASN1_SEQUENCE(X9_62_CURVE) = {
203 ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING),
204 ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING),
205 ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING)
206 } static_ASN1_SEQUENCE_END(X9_62_CURVE)
208 ASN1_SEQUENCE(ECPARAMETERS) = {
209 ASN1_EMBED(ECPARAMETERS, version, INT32),
210 ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID),
211 ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE),
212 ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING),
213 ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
214 ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER)
215 } ASN1_SEQUENCE_END(ECPARAMETERS)
217 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
218 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
220 ASN1_CHOICE(ECPKPARAMETERS) = {
221 ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT),
222 ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
223 ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL)
224 } ASN1_CHOICE_END(ECPKPARAMETERS)
226 DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
227 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPKPARAMETERS, ECPKPARAMETERS)
228 IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
230 ASN1_SEQUENCE(EC_PRIVATEKEY) = {
231 ASN1_EMBED(EC_PRIVATEKEY, version, INT32),
232 ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
233 ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
234 ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
235 } static_ASN1_SEQUENCE_END(EC_PRIVATEKEY)
237 DECLARE_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
238 DECLARE_ASN1_ENCODE_FUNCTIONS_const(EC_PRIVATEKEY, EC_PRIVATEKEY)
239 IMPLEMENT_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
241 /* some declarations of internal function */
243 /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
244 static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *);
245 /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
246 static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
248 /* the function definitions */
250 static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field)
255 if (group == NULL || field == NULL)
258 /* clear the old values (if necessary) */
259 ASN1_OBJECT_free(field->fieldType);
260 ASN1_TYPE_free(field->p.other);
262 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
263 /* set OID for the field */
264 if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
265 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
269 if (nid == NID_X9_62_prime_field) {
270 if ((tmp = BN_new()) == NULL) {
271 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
274 /* the parameters are specified by the prime number p */
275 if (!EC_GROUP_get_curve(group, tmp, NULL, NULL, NULL)) {
276 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
279 /* set the prime number */
280 field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
281 if (field->p.prime == NULL) {
282 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
285 } else if (nid == NID_X9_62_characteristic_two_field)
286 #ifdef OPENSSL_NO_EC2M
288 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED);
294 X9_62_CHARACTERISTIC_TWO *char_two;
296 field->p.char_two = X9_62_CHARACTERISTIC_TWO_new();
297 char_two = field->p.char_two;
299 if (char_two == NULL) {
300 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
304 char_two->m = (long)EC_GROUP_get_degree(group);
306 field_type = EC_GROUP_get_basis_type(group);
308 if (field_type == 0) {
309 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
312 /* set base type OID */
313 if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) {
314 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
318 if (field_type == NID_X9_62_tpBasis) {
321 if (!EC_GROUP_get_trinomial_basis(group, &k))
324 char_two->p.tpBasis = ASN1_INTEGER_new();
325 if (char_two->p.tpBasis == NULL) {
326 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
329 if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) {
330 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
333 } else if (field_type == NID_X9_62_ppBasis) {
334 unsigned int k1, k2, k3;
336 if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))
339 char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
340 if (char_two->p.ppBasis == NULL) {
341 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
346 char_two->p.ppBasis->k1 = (long)k1;
347 char_two->p.ppBasis->k2 = (long)k2;
348 char_two->p.ppBasis->k3 = (long)k3;
349 } else { /* field_type == NID_X9_62_onBasis */
351 /* for ONB the parameters are (asn1) NULL */
352 char_two->p.onBasis = ASN1_NULL_new();
353 if (char_two->p.onBasis == NULL) {
354 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
361 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_UNSUPPORTED_FIELD);
372 static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
375 BIGNUM *tmp_1 = NULL, *tmp_2 = NULL;
376 unsigned char *a_buf = NULL, *b_buf = NULL;
379 if (!group || !curve || !curve->a || !curve->b)
382 if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
383 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
388 if (!EC_GROUP_get_curve(group, NULL, tmp_1, tmp_2, NULL)) {
389 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
394 * Per SEC 1, the curve coefficients must be padded up to size. See C.2's
395 * definition of Curve, C.1's definition of FieldElement, and 2.3.5's
396 * definition of how to encode the field elements.
398 len = ((size_t)EC_GROUP_get_degree(group) + 7) / 8;
399 if ((a_buf = OPENSSL_malloc(len)) == NULL
400 || (b_buf = OPENSSL_malloc(len)) == NULL) {
401 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
404 if (BN_bn2binpad(tmp_1, a_buf, len) < 0
405 || BN_bn2binpad(tmp_2, b_buf, len) < 0) {
406 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
411 if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len)
412 || !ASN1_OCTET_STRING_set(curve->b, b_buf, len)) {
413 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
417 /* set the seed (optional) */
420 if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
421 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
424 curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
425 curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
426 if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
427 (int)group->seed_len)) {
428 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
432 ASN1_BIT_STRING_free(curve->seed);
446 ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group,
447 ECPARAMETERS *params)
450 ECPARAMETERS *ret = NULL;
452 unsigned char *buffer = NULL;
453 const EC_POINT *point = NULL;
454 point_conversion_form_t form;
457 if (params == NULL) {
458 if ((ret = ECPARAMETERS_new()) == NULL) {
459 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
465 /* set the version (always one) */
466 ret->version = (long)0x1;
468 /* set the fieldID */
469 if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
470 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
475 if (!ec_asn1_group2curve(group, ret->curve)) {
476 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
480 /* set the base point */
481 if ((point = EC_GROUP_get0_generator(group)) == NULL) {
482 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, EC_R_UNDEFINED_GENERATOR);
486 form = EC_GROUP_get_point_conversion_form(group);
488 len = EC_POINT_point2buf(group, point, form, &buffer, NULL);
490 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
493 if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
494 OPENSSL_free(buffer);
495 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
498 ASN1_STRING_set0(ret->base, buffer, len);
501 tmp = EC_GROUP_get0_order(group);
503 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
506 ret->order = BN_to_ASN1_INTEGER(tmp, orig = ret->order);
507 if (ret->order == NULL) {
509 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
513 /* set the cofactor (optional) */
514 tmp = EC_GROUP_get0_cofactor(group);
516 ret->cofactor = BN_to_ASN1_INTEGER(tmp, orig = ret->cofactor);
517 if (ret->cofactor == NULL) {
518 ret->cofactor = orig;
519 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
528 ECPARAMETERS_free(ret);
532 ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group,
533 ECPKPARAMETERS *params)
536 ECPKPARAMETERS *ret = params;
539 if ((ret = ECPKPARAMETERS_new()) == NULL) {
540 ECerr(EC_F_EC_GROUP_GET_ECPKPARAMETERS, ERR_R_MALLOC_FAILURE);
544 if (ret->type == ECPKPARAMETERS_TYPE_NAMED)
545 ASN1_OBJECT_free(ret->value.named_curve);
546 else if (ret->type == ECPKPARAMETERS_TYPE_EXPLICIT
547 && ret->value.parameters != NULL)
548 ECPARAMETERS_free(ret->value.parameters);
551 if (EC_GROUP_get_asn1_flag(group)) {
553 * use the asn1 OID to describe the elliptic curve parameters
555 tmp = EC_GROUP_get_curve_name(group);
557 ASN1_OBJECT *asn1obj = OBJ_nid2obj(tmp);
559 if (asn1obj == NULL || OBJ_length(asn1obj) == 0) {
560 ASN1_OBJECT_free(asn1obj);
561 ECerr(EC_F_EC_GROUP_GET_ECPKPARAMETERS, EC_R_MISSING_OID);
564 ret->type = ECPKPARAMETERS_TYPE_NAMED;
565 ret->value.named_curve = asn1obj;
568 /* we don't know the nid => ERROR */
571 /* use the ECPARAMETERS structure */
572 ret->type = ECPKPARAMETERS_TYPE_EXPLICIT;
573 if ((ret->value.parameters =
574 EC_GROUP_get_ecparameters(group, NULL)) == NULL)
579 ECPKPARAMETERS_free(ret);
585 EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params)
588 EC_GROUP *ret = NULL, *dup = NULL;
589 BIGNUM *p = NULL, *a = NULL, *b = NULL;
590 EC_POINT *point = NULL;
592 int curve_name = NID_undef;
595 if (!params->fieldID || !params->fieldID->fieldType ||
596 !params->fieldID->p.ptr) {
597 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
602 * Now extract the curve parameters a and b. Note that, although SEC 1
603 * specifies the length of their encodings, historical versions of OpenSSL
604 * encoded them incorrectly, so we must accept any length for backwards
607 if (!params->curve || !params->curve->a ||
608 !params->curve->a->data || !params->curve->b ||
609 !params->curve->b->data) {
610 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
613 a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
615 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
618 b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
620 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
624 /* get the field parameters */
625 tmp = OBJ_obj2nid(params->fieldID->fieldType);
626 if (tmp == NID_X9_62_characteristic_two_field)
627 #ifdef OPENSSL_NO_EC2M
629 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_GF2M_NOT_SUPPORTED);
634 X9_62_CHARACTERISTIC_TWO *char_two;
636 char_two = params->fieldID->p.char_two;
638 field_bits = char_two->m;
639 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
640 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
644 if ((p = BN_new()) == NULL) {
645 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
649 /* get the base type */
650 tmp = OBJ_obj2nid(char_two->type);
652 if (tmp == NID_X9_62_tpBasis) {
655 if (!char_two->p.tpBasis) {
656 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
660 tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
662 if (!(char_two->m > tmp_long && tmp_long > 0)) {
663 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
664 EC_R_INVALID_TRINOMIAL_BASIS);
668 /* create the polynomial */
669 if (!BN_set_bit(p, (int)char_two->m))
671 if (!BN_set_bit(p, (int)tmp_long))
673 if (!BN_set_bit(p, 0))
675 } else if (tmp == NID_X9_62_ppBasis) {
676 X9_62_PENTANOMIAL *penta;
678 penta = char_two->p.ppBasis;
680 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
685 (char_two->m > penta->k3 && penta->k3 > penta->k2
686 && penta->k2 > penta->k1 && penta->k1 > 0)) {
687 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
688 EC_R_INVALID_PENTANOMIAL_BASIS);
692 /* create the polynomial */
693 if (!BN_set_bit(p, (int)char_two->m))
695 if (!BN_set_bit(p, (int)penta->k1))
697 if (!BN_set_bit(p, (int)penta->k2))
699 if (!BN_set_bit(p, (int)penta->k3))
701 if (!BN_set_bit(p, 0))
703 } else if (tmp == NID_X9_62_onBasis) {
704 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_NOT_IMPLEMENTED);
708 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
712 /* create the EC_GROUP structure */
713 ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
716 else if (tmp == NID_X9_62_prime_field) {
717 /* we have a curve over a prime field */
718 /* extract the prime number */
719 if (!params->fieldID->p.prime) {
720 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
723 p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
725 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
729 if (BN_is_negative(p) || BN_is_zero(p)) {
730 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
734 field_bits = BN_num_bits(p);
735 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
736 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
740 /* create the EC_GROUP structure */
741 ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
743 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
748 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
752 /* extract seed (optional) */
753 if (params->curve->seed != NULL) {
754 OPENSSL_free(ret->seed);
755 if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {
756 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
759 memcpy(ret->seed, params->curve->seed->data,
760 params->curve->seed->length);
761 ret->seed_len = params->curve->seed->length;
764 if (params->order == NULL
765 || params->base == NULL
766 || params->base->data == NULL
767 || params->base->length == 0) {
768 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
772 if ((point = EC_POINT_new(ret)) == NULL)
775 /* set the point conversion form */
776 EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
777 (params->base->data[0] & ~0x01));
779 /* extract the ec point */
780 if (!EC_POINT_oct2point(ret, point, params->base->data,
781 params->base->length, NULL)) {
782 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
786 /* extract the order */
787 if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
788 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
791 if (BN_is_negative(a) || BN_is_zero(a)) {
792 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
795 if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
796 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
800 /* extract the cofactor (optional) */
801 if (params->cofactor == NULL) {
804 } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
805 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
808 /* set the generator, order and cofactor (if present) */
809 if (!EC_GROUP_set_generator(ret, point, a, b)) {
810 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
815 * Check if the explicit parameters group just created matches one of the
818 * We create a copy of the group just built, so that we can remove optional
819 * fields for the lookup: we do this to avoid the possibility that one of
820 * the optional parameters is used to force the library into using a less
821 * performant and less secure EC_METHOD instead of the specialized one.
822 * In any case, `seed` is not really used in any computation, while a
823 * cofactor different from the one in the built-in table is just
824 * mathematically wrong anyway and should not be used.
826 if ((ctx = BN_CTX_new()) == NULL) {
827 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
830 if ((dup = EC_GROUP_dup(ret)) == NULL
831 || EC_GROUP_set_seed(dup, NULL, 0) != 1
832 || !EC_GROUP_set_generator(dup, point, a, NULL)) {
833 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
836 if ((curve_name = ec_curve_nid_from_params(dup, ctx)) != NID_undef) {
838 * The input explicit parameters successfully matched one of the
839 * built-in curves: often for built-in curves we have specialized
840 * methods with better performance and hardening.
842 * In this case we replace the `EC_GROUP` created through explicit
843 * parameters with one created from a named group.
845 EC_GROUP *named_group = NULL;
847 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
849 * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
850 * the same curve, we prefer the SECP nid when matching explicit
851 * parameters as that is associated with a specialized EC_METHOD.
853 if (curve_name == NID_wap_wsg_idm_ecid_wtls12)
854 curve_name = NID_secp224r1;
855 #endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
857 if ((named_group = EC_GROUP_new_by_curve_name(curve_name)) == NULL) {
858 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
865 * Set the flag so that EC_GROUPs created from explicit parameters are
866 * serialized using explicit parameters by default.
868 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE);
871 * If the input params do not contain the optional seed field we make
872 * sure it is not added to the returned group.
874 * The seed field is not really used inside libcrypto anyway, and
875 * adding it to parsed explicit parameter keys would alter their DER
876 * encoding output (because of the extra field) which could impact
877 * applications fingerprinting keys by their DER encoding.
879 if (params->curve->seed == NULL) {
880 if (EC_GROUP_set_seed(ret, NULL, 0) != 1)
897 EC_POINT_free(point);
904 EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params)
906 EC_GROUP *ret = NULL;
909 if (params == NULL) {
910 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_MISSING_PARAMETERS);
914 if (params->type == ECPKPARAMETERS_TYPE_NAMED) {
915 /* the curve is given by an OID */
916 tmp = OBJ_obj2nid(params->value.named_curve);
917 if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
918 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS,
919 EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
922 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
923 } else if (params->type == ECPKPARAMETERS_TYPE_EXPLICIT) {
924 /* the parameters are given by an ECPARAMETERS structure */
925 ret = EC_GROUP_new_from_ecparameters(params->value.parameters);
927 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, ERR_R_EC_LIB);
930 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE);
931 } else if (params->type == ECPKPARAMETERS_TYPE_IMPLICIT) {
932 /* implicit parameters inherited from CA - unsupported */
935 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_ASN1_ERROR);
942 /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
944 EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
946 EC_GROUP *group = NULL;
947 ECPKPARAMETERS *params = NULL;
948 const unsigned char *p = *in;
950 if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) {
951 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
952 ECPKPARAMETERS_free(params);
956 if ((group = EC_GROUP_new_from_ecpkparameters(params)) == NULL) {
957 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
958 ECPKPARAMETERS_free(params);
962 if (params->type == ECPKPARAMETERS_TYPE_EXPLICIT)
963 group->decoded_from_explicit_params = 1;
970 ECPKPARAMETERS_free(params);
975 int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
978 ECPKPARAMETERS *tmp = EC_GROUP_get_ecpkparameters(a, NULL);
980 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
983 if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
984 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
985 ECPKPARAMETERS_free(tmp);
988 ECPKPARAMETERS_free(tmp);
992 /* some EC_KEY functions */
994 EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
997 EC_PRIVATEKEY *priv_key = NULL;
998 const unsigned char *p = *in;
1000 if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) {
1001 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1005 if (a == NULL || *a == NULL) {
1006 if ((ret = EC_KEY_new()) == NULL) {
1007 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1013 if (priv_key->parameters) {
1014 EC_GROUP_free(ret->group);
1015 ret->group = EC_GROUP_new_from_ecpkparameters(priv_key->parameters);
1016 if (ret->group != NULL
1017 && priv_key->parameters->type == ECPKPARAMETERS_TYPE_EXPLICIT)
1018 ret->group->decoded_from_explicit_params = 1;
1021 if (ret->group == NULL) {
1022 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1026 ret->version = priv_key->version;
1028 if (priv_key->privateKey) {
1029 ASN1_OCTET_STRING *pkey = priv_key->privateKey;
1030 if (EC_KEY_oct2priv(ret, ASN1_STRING_get0_data(pkey),
1031 ASN1_STRING_length(pkey)) == 0)
1034 ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
1038 EC_POINT_clear_free(ret->pub_key);
1039 ret->pub_key = EC_POINT_new(ret->group);
1040 if (ret->pub_key == NULL) {
1041 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1045 if (priv_key->publicKey) {
1046 const unsigned char *pub_oct;
1049 pub_oct = ASN1_STRING_get0_data(priv_key->publicKey);
1050 pub_oct_len = ASN1_STRING_length(priv_key->publicKey);
1051 if (!EC_KEY_oct2key(ret, pub_oct, pub_oct_len, NULL)) {
1052 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1056 if (ret->group->meth->keygenpub == NULL
1057 || ret->group->meth->keygenpub(ret) == 0)
1059 /* Remember the original private-key-only encoding. */
1060 ret->enc_flag |= EC_PKEY_NO_PUBKEY;
1065 EC_PRIVATEKEY_free(priv_key);
1070 if (a == NULL || *a != ret)
1072 EC_PRIVATEKEY_free(priv_key);
1076 int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out)
1078 int ret = 0, ok = 0;
1079 unsigned char *priv= NULL, *pub= NULL;
1080 size_t privlen = 0, publen = 0;
1082 EC_PRIVATEKEY *priv_key = NULL;
1084 if (a == NULL || a->group == NULL ||
1085 (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
1086 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
1090 if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
1091 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1095 priv_key->version = a->version;
1097 privlen = EC_KEY_priv2buf(a, &priv);
1100 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1104 ASN1_STRING_set0(priv_key->privateKey, priv, privlen);
1107 if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
1108 if ((priv_key->parameters =
1109 EC_GROUP_get_ecpkparameters(a->group,
1110 priv_key->parameters)) == NULL) {
1111 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1116 if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
1117 priv_key->publicKey = ASN1_BIT_STRING_new();
1118 if (priv_key->publicKey == NULL) {
1119 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1123 publen = EC_KEY_key2buf(a, a->conv_form, &pub, NULL);
1126 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1130 priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
1131 priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
1132 ASN1_STRING_set0(priv_key->publicKey, pub, publen);
1136 if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
1137 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1142 OPENSSL_clear_free(priv, privlen);
1144 EC_PRIVATEKEY_free(priv_key);
1145 return (ok ? ret : 0);
1148 int i2d_ECParameters(EC_KEY *a, unsigned char **out)
1151 ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1154 return i2d_ECPKParameters(a->group, out);
1157 EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
1161 if (in == NULL || *in == NULL) {
1162 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1166 if (a == NULL || *a == NULL) {
1167 if ((ret = EC_KEY_new()) == NULL) {
1168 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
1174 if (!d2i_ECPKParameters(&ret->group, in, len)) {
1175 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
1176 if (a == NULL || *a != ret)
1187 EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
1191 if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
1193 * sorry, but a EC_GROUP-structure is necessary to set the public key
1195 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1199 if (!EC_KEY_oct2key(ret, *in, len, NULL)) {
1200 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
1207 int i2o_ECPublicKey(const EC_KEY *a, unsigned char **out)
1213 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1217 buf_len = EC_POINT_point2oct(a->group, a->pub_key,
1218 a->conv_form, NULL, 0, NULL);
1220 if (out == NULL || buf_len == 0)
1221 /* out == NULL => just return the length of the octet string */
1225 if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
1226 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1231 if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
1232 *out, buf_len, NULL)) {
1233 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
1245 ASN1_SEQUENCE(ECDSA_SIG) = {
1246 ASN1_SIMPLE(ECDSA_SIG, r, CBIGNUM),
1247 ASN1_SIMPLE(ECDSA_SIG, s, CBIGNUM)
1248 } static_ASN1_SEQUENCE_END(ECDSA_SIG)
1250 DECLARE_ASN1_FUNCTIONS_const(ECDSA_SIG)
1251 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECDSA_SIG, ECDSA_SIG)
1252 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ECDSA_SIG, ECDSA_SIG, ECDSA_SIG)
1254 ECDSA_SIG *ECDSA_SIG_new(void)
1256 ECDSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig));
1258 ECerr(EC_F_ECDSA_SIG_NEW, ERR_R_MALLOC_FAILURE);
1262 void ECDSA_SIG_free(ECDSA_SIG *sig)
1266 BN_clear_free(sig->r);
1267 BN_clear_free(sig->s);
1271 void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
1279 const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig)
1284 const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig)
1289 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
1291 if (r == NULL || s == NULL)
1293 BN_clear_free(sig->r);
1294 BN_clear_free(sig->s);
1300 int ECDSA_size(const EC_KEY *r)
1304 unsigned char buf[4];
1305 const EC_GROUP *group;
1309 group = EC_KEY_get0_group(r);
1313 i = EC_GROUP_order_bits(group);
1316 bs.length = (i + 7) / 8;
1318 bs.type = V_ASN1_INTEGER;
1319 /* If the top bit is set the asn1 encoding is 1 larger. */
1322 i = i2d_ASN1_INTEGER(&bs, NULL);
1323 i += i; /* r and s */
1324 ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);