2 * Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 * ECDSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
18 #include <openssl/err.h>
19 #include <openssl/asn1t.h>
20 #include <openssl/objects.h>
21 #include "internal/nelem.h"
22 #include "crypto/asn1_dsa.h"
26 /* some structures needed for the asn1 encoding */
27 typedef struct x9_62_pentanomial_st {
33 typedef struct x9_62_characteristic_two_st {
38 /* NID_X9_62_onBasis */
40 /* NID_X9_62_tpBasis */
41 ASN1_INTEGER *tpBasis;
42 /* NID_X9_62_ppBasis */
43 X9_62_PENTANOMIAL *ppBasis;
47 } X9_62_CHARACTERISTIC_TWO;
49 typedef struct x9_62_fieldid_st {
50 ASN1_OBJECT *fieldType;
53 /* NID_X9_62_prime_field */
55 /* NID_X9_62_characteristic_two_field */
56 X9_62_CHARACTERISTIC_TWO *char_two;
62 typedef struct x9_62_curve_st {
65 ASN1_BIT_STRING *seed;
68 struct ec_parameters_st {
70 X9_62_FIELDID *fieldID;
72 ASN1_OCTET_STRING *base;
74 ASN1_INTEGER *cofactor;
75 } /* ECPARAMETERS */ ;
78 ECPKPARAMETERS_TYPE_NAMED = 0,
79 ECPKPARAMETERS_TYPE_EXPLICIT,
80 ECPKPARAMETERS_TYPE_IMPLICIT
81 } ecpk_parameters_type_t;
83 struct ecpk_parameters_st {
86 ASN1_OBJECT *named_curve;
87 ECPARAMETERS *parameters;
88 ASN1_NULL *implicitlyCA;
90 } /* ECPKPARAMETERS */ ;
92 /* SEC1 ECPrivateKey */
93 typedef struct ec_privatekey_st {
95 ASN1_OCTET_STRING *privateKey;
96 ECPKPARAMETERS *parameters;
97 ASN1_BIT_STRING *publicKey;
100 /* the OpenSSL ASN.1 definitions */
101 ASN1_SEQUENCE(X9_62_PENTANOMIAL) = {
102 ASN1_EMBED(X9_62_PENTANOMIAL, k1, INT32),
103 ASN1_EMBED(X9_62_PENTANOMIAL, k2, INT32),
104 ASN1_EMBED(X9_62_PENTANOMIAL, k3, INT32)
105 } static_ASN1_SEQUENCE_END(X9_62_PENTANOMIAL)
107 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
108 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
110 ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY);
112 ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = {
113 ADB_ENTRY(NID_X9_62_onBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.onBasis, ASN1_NULL)),
114 ADB_ENTRY(NID_X9_62_tpBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.tpBasis, ASN1_INTEGER)),
115 ADB_ENTRY(NID_X9_62_ppBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.ppBasis, X9_62_PENTANOMIAL))
116 } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL);
118 ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = {
119 ASN1_EMBED(X9_62_CHARACTERISTIC_TWO, m, INT32),
120 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT),
121 ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO)
122 } static_ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO)
124 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
125 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
127 ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY);
129 ASN1_ADB(X9_62_FIELDID) = {
130 ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)),
131 ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO))
132 } ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL);
134 ASN1_SEQUENCE(X9_62_FIELDID) = {
135 ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
136 ASN1_ADB_OBJECT(X9_62_FIELDID)
137 } static_ASN1_SEQUENCE_END(X9_62_FIELDID)
139 ASN1_SEQUENCE(X9_62_CURVE) = {
140 ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING),
141 ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING),
142 ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING)
143 } static_ASN1_SEQUENCE_END(X9_62_CURVE)
145 ASN1_SEQUENCE(ECPARAMETERS) = {
146 ASN1_EMBED(ECPARAMETERS, version, INT32),
147 ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID),
148 ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE),
149 ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING),
150 ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
151 ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER)
152 } ASN1_SEQUENCE_END(ECPARAMETERS)
154 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
155 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
157 ASN1_CHOICE(ECPKPARAMETERS) = {
158 ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT),
159 ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
160 ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL)
161 } ASN1_CHOICE_END(ECPKPARAMETERS)
163 DECLARE_ASN1_FUNCTIONS(ECPKPARAMETERS)
164 DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECPKPARAMETERS, ECPKPARAMETERS)
165 IMPLEMENT_ASN1_FUNCTIONS(ECPKPARAMETERS)
167 ASN1_SEQUENCE(EC_PRIVATEKEY) = {
168 ASN1_EMBED(EC_PRIVATEKEY, version, INT32),
169 ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
170 ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
171 ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
172 } static_ASN1_SEQUENCE_END(EC_PRIVATEKEY)
174 DECLARE_ASN1_FUNCTIONS(EC_PRIVATEKEY)
175 DECLARE_ASN1_ENCODE_FUNCTIONS_name(EC_PRIVATEKEY, EC_PRIVATEKEY)
176 IMPLEMENT_ASN1_FUNCTIONS(EC_PRIVATEKEY)
178 /* some declarations of internal function */
180 /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
181 static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *);
182 /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
183 static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
185 /* the function definitions */
187 static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field)
192 if (group == NULL || field == NULL)
195 /* clear the old values (if necessary) */
196 ASN1_OBJECT_free(field->fieldType);
197 ASN1_TYPE_free(field->p.other);
199 nid = EC_GROUP_get_field_type(group);
200 /* set OID for the field */
201 if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
202 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
206 if (nid == NID_X9_62_prime_field) {
207 if ((tmp = BN_new()) == NULL) {
208 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
211 /* the parameters are specified by the prime number p */
212 if (!EC_GROUP_get_curve(group, tmp, NULL, NULL, NULL)) {
213 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
216 /* set the prime number */
217 field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
218 if (field->p.prime == NULL) {
219 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
222 } else if (nid == NID_X9_62_characteristic_two_field)
223 #ifdef OPENSSL_NO_EC2M
225 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED);
231 X9_62_CHARACTERISTIC_TWO *char_two;
233 field->p.char_two = X9_62_CHARACTERISTIC_TWO_new();
234 char_two = field->p.char_two;
236 if (char_two == NULL) {
237 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
241 char_two->m = (long)EC_GROUP_get_degree(group);
243 field_type = EC_GROUP_get_basis_type(group);
245 if (field_type == 0) {
246 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
249 /* set base type OID */
250 if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) {
251 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
255 if (field_type == NID_X9_62_tpBasis) {
258 if (!EC_GROUP_get_trinomial_basis(group, &k))
261 char_two->p.tpBasis = ASN1_INTEGER_new();
262 if (char_two->p.tpBasis == NULL) {
263 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
266 if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) {
267 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
270 } else if (field_type == NID_X9_62_ppBasis) {
271 unsigned int k1, k2, k3;
273 if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))
276 char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
277 if (char_two->p.ppBasis == NULL) {
278 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
283 char_two->p.ppBasis->k1 = (long)k1;
284 char_two->p.ppBasis->k2 = (long)k2;
285 char_two->p.ppBasis->k3 = (long)k3;
286 } else { /* field_type == NID_X9_62_onBasis */
288 /* for ONB the parameters are (asn1) NULL */
289 char_two->p.onBasis = ASN1_NULL_new();
290 if (char_two->p.onBasis == NULL) {
291 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
298 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_UNSUPPORTED_FIELD);
309 static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
312 BIGNUM *tmp_1 = NULL, *tmp_2 = NULL;
313 unsigned char *a_buf = NULL, *b_buf = NULL;
316 if (!group || !curve || !curve->a || !curve->b)
319 if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
320 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
325 if (!EC_GROUP_get_curve(group, NULL, tmp_1, tmp_2, NULL)) {
326 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
331 * Per SEC 1, the curve coefficients must be padded up to size. See C.2's
332 * definition of Curve, C.1's definition of FieldElement, and 2.3.5's
333 * definition of how to encode the field elements.
335 len = ((size_t)EC_GROUP_get_degree(group) + 7) / 8;
336 if ((a_buf = OPENSSL_malloc(len)) == NULL
337 || (b_buf = OPENSSL_malloc(len)) == NULL) {
338 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
341 if (BN_bn2binpad(tmp_1, a_buf, len) < 0
342 || BN_bn2binpad(tmp_2, b_buf, len) < 0) {
343 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
348 if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len)
349 || !ASN1_OCTET_STRING_set(curve->b, b_buf, len)) {
350 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
354 /* set the seed (optional) */
357 if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
358 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
361 curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
362 curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
363 if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
364 (int)group->seed_len)) {
365 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
369 ASN1_BIT_STRING_free(curve->seed);
383 ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group,
384 ECPARAMETERS *params)
387 ECPARAMETERS *ret = NULL;
389 unsigned char *buffer = NULL;
390 const EC_POINT *point = NULL;
391 point_conversion_form_t form;
394 if (params == NULL) {
395 if ((ret = ECPARAMETERS_new()) == NULL) {
396 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
402 /* set the version (always one) */
403 ret->version = (long)0x1;
405 /* set the fieldID */
406 if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
407 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
412 if (!ec_asn1_group2curve(group, ret->curve)) {
413 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
417 /* set the base point */
418 if ((point = EC_GROUP_get0_generator(group)) == NULL) {
419 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, EC_R_UNDEFINED_GENERATOR);
423 form = EC_GROUP_get_point_conversion_form(group);
425 len = EC_POINT_point2buf(group, point, form, &buffer, NULL);
427 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
430 if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
431 OPENSSL_free(buffer);
432 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
435 ASN1_STRING_set0(ret->base, buffer, len);
438 tmp = EC_GROUP_get0_order(group);
440 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
443 ret->order = BN_to_ASN1_INTEGER(tmp, orig = ret->order);
444 if (ret->order == NULL) {
446 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
450 /* set the cofactor (optional) */
451 tmp = EC_GROUP_get0_cofactor(group);
453 ret->cofactor = BN_to_ASN1_INTEGER(tmp, orig = ret->cofactor);
454 if (ret->cofactor == NULL) {
455 ret->cofactor = orig;
456 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
465 ECPARAMETERS_free(ret);
469 ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group,
470 ECPKPARAMETERS *params)
473 ECPKPARAMETERS *ret = params;
476 if ((ret = ECPKPARAMETERS_new()) == NULL) {
477 ECerr(EC_F_EC_GROUP_GET_ECPKPARAMETERS, ERR_R_MALLOC_FAILURE);
481 if (ret->type == ECPKPARAMETERS_TYPE_NAMED)
482 ASN1_OBJECT_free(ret->value.named_curve);
483 else if (ret->type == ECPKPARAMETERS_TYPE_EXPLICIT
484 && ret->value.parameters != NULL)
485 ECPARAMETERS_free(ret->value.parameters);
488 if (EC_GROUP_get_asn1_flag(group)) {
490 * use the asn1 OID to describe the elliptic curve parameters
492 tmp = EC_GROUP_get_curve_name(group);
494 ASN1_OBJECT *asn1obj = OBJ_nid2obj(tmp);
496 if (asn1obj == NULL || OBJ_length(asn1obj) == 0) {
497 ASN1_OBJECT_free(asn1obj);
498 ECerr(EC_F_EC_GROUP_GET_ECPKPARAMETERS, EC_R_MISSING_OID);
501 ret->type = ECPKPARAMETERS_TYPE_NAMED;
502 ret->value.named_curve = asn1obj;
505 /* we don't know the nid => ERROR */
508 /* use the ECPARAMETERS structure */
509 ret->type = ECPKPARAMETERS_TYPE_EXPLICIT;
510 if ((ret->value.parameters =
511 EC_GROUP_get_ecparameters(group, NULL)) == NULL)
516 ECPKPARAMETERS_free(ret);
522 EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params)
525 EC_GROUP *ret = NULL, *dup = NULL;
526 BIGNUM *p = NULL, *a = NULL, *b = NULL;
527 EC_POINT *point = NULL;
529 int curve_name = NID_undef;
532 if (params->fieldID == NULL
533 || params->fieldID->fieldType == NULL
534 || params->fieldID->p.ptr == NULL) {
535 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
540 * Now extract the curve parameters a and b. Note that, although SEC 1
541 * specifies the length of their encodings, historical versions of OpenSSL
542 * encoded them incorrectly, so we must accept any length for backwards
545 if (params->curve == NULL
546 || params->curve->a == NULL || params->curve->a->data == NULL
547 || params->curve->b == NULL || params->curve->b->data == NULL) {
548 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
551 a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
553 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
556 b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
558 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
562 /* get the field parameters */
563 tmp = OBJ_obj2nid(params->fieldID->fieldType);
564 if (tmp == NID_X9_62_characteristic_two_field)
565 #ifdef OPENSSL_NO_EC2M
567 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_GF2M_NOT_SUPPORTED);
572 X9_62_CHARACTERISTIC_TWO *char_two;
574 char_two = params->fieldID->p.char_two;
576 field_bits = char_two->m;
577 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
578 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
582 if ((p = BN_new()) == NULL) {
583 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
587 /* get the base type */
588 tmp = OBJ_obj2nid(char_two->type);
590 if (tmp == NID_X9_62_tpBasis) {
593 if (!char_two->p.tpBasis) {
594 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
598 tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
600 if (!(char_two->m > tmp_long && tmp_long > 0)) {
601 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
602 EC_R_INVALID_TRINOMIAL_BASIS);
606 /* create the polynomial */
607 if (!BN_set_bit(p, (int)char_two->m))
609 if (!BN_set_bit(p, (int)tmp_long))
611 if (!BN_set_bit(p, 0))
613 } else if (tmp == NID_X9_62_ppBasis) {
614 X9_62_PENTANOMIAL *penta;
616 penta = char_two->p.ppBasis;
618 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
623 (char_two->m > penta->k3 && penta->k3 > penta->k2
624 && penta->k2 > penta->k1 && penta->k1 > 0)) {
625 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
626 EC_R_INVALID_PENTANOMIAL_BASIS);
630 /* create the polynomial */
631 if (!BN_set_bit(p, (int)char_two->m))
633 if (!BN_set_bit(p, (int)penta->k1))
635 if (!BN_set_bit(p, (int)penta->k2))
637 if (!BN_set_bit(p, (int)penta->k3))
639 if (!BN_set_bit(p, 0))
641 } else if (tmp == NID_X9_62_onBasis) {
642 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_NOT_IMPLEMENTED);
646 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
650 /* create the EC_GROUP structure */
651 ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
654 else if (tmp == NID_X9_62_prime_field) {
655 /* we have a curve over a prime field */
656 /* extract the prime number */
657 if (params->fieldID->p.prime == NULL) {
658 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
661 p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
663 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
667 if (BN_is_negative(p) || BN_is_zero(p)) {
668 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
672 field_bits = BN_num_bits(p);
673 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
674 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
678 /* create the EC_GROUP structure */
679 ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
681 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
686 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
690 /* extract seed (optional) */
691 if (params->curve->seed != NULL) {
692 OPENSSL_free(ret->seed);
693 if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {
694 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
697 memcpy(ret->seed, params->curve->seed->data,
698 params->curve->seed->length);
699 ret->seed_len = params->curve->seed->length;
702 if (params->order == NULL
703 || params->base == NULL
704 || params->base->data == NULL) {
705 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
709 if ((point = EC_POINT_new(ret)) == NULL)
712 /* set the point conversion form */
713 EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
714 (params->base->data[0] & ~0x01));
716 /* extract the ec point */
717 if (!EC_POINT_oct2point(ret, point, params->base->data,
718 params->base->length, NULL)) {
719 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
723 /* extract the order */
724 if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
725 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
728 if (BN_is_negative(a) || BN_is_zero(a)) {
729 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
732 if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
733 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
737 /* extract the cofactor (optional) */
738 if (params->cofactor == NULL) {
741 } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
742 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
745 /* set the generator, order and cofactor (if present) */
746 if (!EC_GROUP_set_generator(ret, point, a, b)) {
747 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
752 * Check if the explicit parameters group just created matches one of the
755 * We create a copy of the group just built, so that we can remove optional
756 * fields for the lookup: we do this to avoid the possibility that one of
757 * the optional parameters is used to force the library into using a less
758 * performant and less secure EC_METHOD instead of the specialized one.
759 * In any case, `seed` is not really used in any computation, while a
760 * cofactor different from the one in the built-in table is just
761 * mathematically wrong anyway and should not be used.
763 if ((ctx = BN_CTX_new()) == NULL) {
764 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
767 if ((dup = EC_GROUP_dup(ret)) == NULL
768 || EC_GROUP_set_seed(dup, NULL, 0) != 1
769 || !EC_GROUP_set_generator(dup, point, a, NULL)) {
770 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
773 if ((curve_name = ec_curve_nid_from_params(dup, ctx)) != NID_undef) {
775 * The input explicit parameters successfully matched one of the
776 * built-in curves: often for built-in curves we have specialized
777 * methods with better performance and hardening.
779 * In this case we replace the `EC_GROUP` created through explicit
780 * parameters with one created from a named group.
782 EC_GROUP *named_group = NULL;
784 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
786 * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
787 * the same curve, we prefer the SECP nid when matching explicit
788 * parameters as that is associated with a specialized EC_METHOD.
790 if (curve_name == NID_wap_wsg_idm_ecid_wtls12)
791 curve_name = NID_secp224r1;
792 #endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
794 if ((named_group = EC_GROUP_new_by_curve_name(curve_name)) == NULL) {
795 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
802 * Set the flag so that EC_GROUPs created from explicit parameters are
803 * serialized using explicit parameters by default.
805 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE);
808 * If the input params do not contain the optional seed field we make
809 * sure it is not added to the returned group.
811 * The seed field is not really used inside libcrypto anyway, and
812 * adding it to parsed explicit parameter keys would alter their DER
813 * encoding output (because of the extra field) which could impact
814 * applications fingerprinting keys by their DER encoding.
816 if (params->curve->seed == NULL) {
817 if (EC_GROUP_set_seed(ret, NULL, 0) != 1)
834 EC_POINT_free(point);
841 EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params)
843 EC_GROUP *ret = NULL;
846 if (params == NULL) {
847 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_MISSING_PARAMETERS);
851 if (params->type == ECPKPARAMETERS_TYPE_NAMED) {
852 /* the curve is given by an OID */
853 tmp = OBJ_obj2nid(params->value.named_curve);
854 if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
855 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS,
856 EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
859 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
860 } else if (params->type == ECPKPARAMETERS_TYPE_EXPLICIT) {
861 /* the parameters are given by an ECPARAMETERS structure */
862 ret = EC_GROUP_new_from_ecparameters(params->value.parameters);
864 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, ERR_R_EC_LIB);
867 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE);
868 } else if (params->type == ECPKPARAMETERS_TYPE_IMPLICIT) {
869 /* implicit parameters inherited from CA - unsupported */
872 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_ASN1_ERROR);
879 /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
881 EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
883 EC_GROUP *group = NULL;
884 ECPKPARAMETERS *params = NULL;
885 const unsigned char *p = *in;
887 if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) {
888 ECPKPARAMETERS_free(params);
892 if ((group = EC_GROUP_new_from_ecpkparameters(params)) == NULL) {
893 ECPKPARAMETERS_free(params);
897 if (params->type == ECPKPARAMETERS_TYPE_EXPLICIT)
898 group->decoded_from_explicit_params = 1;
905 ECPKPARAMETERS_free(params);
910 int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
913 ECPKPARAMETERS *tmp = EC_GROUP_get_ecpkparameters(a, NULL);
915 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
918 if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
919 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
920 ECPKPARAMETERS_free(tmp);
923 ECPKPARAMETERS_free(tmp);
927 /* some EC_KEY functions */
929 EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
932 EC_PRIVATEKEY *priv_key = NULL;
933 const unsigned char *p = *in;
935 if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL)
938 if (a == NULL || *a == NULL) {
939 if ((ret = EC_KEY_new()) == NULL) {
940 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
946 if (priv_key->parameters) {
947 EC_GROUP_free(ret->group);
948 ret->group = EC_GROUP_new_from_ecpkparameters(priv_key->parameters);
949 if (ret->group != NULL
950 && priv_key->parameters->type == ECPKPARAMETERS_TYPE_EXPLICIT)
951 ret->group->decoded_from_explicit_params = 1;
954 if (ret->group == NULL) {
955 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
959 ret->version = priv_key->version;
961 if (priv_key->privateKey) {
962 ASN1_OCTET_STRING *pkey = priv_key->privateKey;
963 if (EC_KEY_oct2priv(ret, ASN1_STRING_get0_data(pkey),
964 ASN1_STRING_length(pkey)) == 0)
967 ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
971 EC_POINT_clear_free(ret->pub_key);
972 ret->pub_key = EC_POINT_new(ret->group);
973 if (ret->pub_key == NULL) {
974 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
978 if (priv_key->publicKey) {
979 const unsigned char *pub_oct;
982 pub_oct = ASN1_STRING_get0_data(priv_key->publicKey);
983 pub_oct_len = ASN1_STRING_length(priv_key->publicKey);
984 if (!EC_KEY_oct2key(ret, pub_oct, pub_oct_len, NULL)) {
985 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
989 if (ret->group->meth->keygenpub == NULL
990 || ret->group->meth->keygenpub(ret) == 0)
992 /* Remember the original private-key-only encoding. */
993 ret->enc_flag |= EC_PKEY_NO_PUBKEY;
998 EC_PRIVATEKEY_free(priv_key);
1004 if (a == NULL || *a != ret)
1006 EC_PRIVATEKEY_free(priv_key);
1010 int i2d_ECPrivateKey(const EC_KEY *a, unsigned char **out)
1012 int ret = 0, ok = 0;
1013 unsigned char *priv= NULL, *pub= NULL;
1014 size_t privlen = 0, publen = 0;
1016 EC_PRIVATEKEY *priv_key = NULL;
1018 if (a == NULL || a->group == NULL ||
1019 (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
1020 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
1024 if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
1025 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1029 priv_key->version = a->version;
1031 privlen = EC_KEY_priv2buf(a, &priv);
1034 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1038 ASN1_STRING_set0(priv_key->privateKey, priv, privlen);
1041 if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
1042 if ((priv_key->parameters =
1043 EC_GROUP_get_ecpkparameters(a->group,
1044 priv_key->parameters)) == NULL) {
1045 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1050 if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
1051 priv_key->publicKey = ASN1_BIT_STRING_new();
1052 if (priv_key->publicKey == NULL) {
1053 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1057 publen = EC_KEY_key2buf(a, a->conv_form, &pub, NULL);
1060 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1064 priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
1065 priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
1066 ASN1_STRING_set0(priv_key->publicKey, pub, publen);
1070 if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
1071 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1076 OPENSSL_clear_free(priv, privlen);
1078 EC_PRIVATEKEY_free(priv_key);
1079 return (ok ? ret : 0);
1082 int i2d_ECParameters(const EC_KEY *a, unsigned char **out)
1085 ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1088 return i2d_ECPKParameters(a->group, out);
1091 EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
1095 if (in == NULL || *in == NULL) {
1096 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1100 if (a == NULL || *a == NULL) {
1101 if ((ret = EC_KEY_new()) == NULL) {
1102 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
1108 if (!d2i_ECPKParameters(&ret->group, in, len)) {
1109 if (a == NULL || *a != ret)
1123 EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
1127 if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
1129 * sorry, but a EC_GROUP-structure is necessary to set the public key
1131 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1135 /* EC_KEY_opt2key updates dirty_cnt */
1136 if (!EC_KEY_oct2key(ret, *in, len, NULL)) {
1137 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
1144 int i2o_ECPublicKey(const EC_KEY *a, unsigned char **out)
1150 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1154 buf_len = EC_POINT_point2oct(a->group, a->pub_key,
1155 a->conv_form, NULL, 0, NULL);
1157 if (out == NULL || buf_len == 0)
1158 /* out == NULL => just return the length of the octet string */
1162 if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
1163 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1168 if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
1169 *out, buf_len, NULL)) {
1170 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
1182 DECLARE_ASN1_FUNCTIONS(ECDSA_SIG)
1183 DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECDSA_SIG, ECDSA_SIG)
1185 #endif /* FIPS_MODULE */
1187 ECDSA_SIG *ECDSA_SIG_new(void)
1189 ECDSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig));
1191 ECerr(EC_F_ECDSA_SIG_NEW, ERR_R_MALLOC_FAILURE);
1195 void ECDSA_SIG_free(ECDSA_SIG *sig)
1199 BN_clear_free(sig->r);
1200 BN_clear_free(sig->s);
1204 ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **psig, const unsigned char **ppin, long len)
1210 if (psig != NULL && *psig != NULL) {
1213 sig = ECDSA_SIG_new();
1221 if (ossl_decode_der_dsa_sig(sig->r, sig->s, ppin, (size_t)len) == 0) {
1222 if (psig == NULL || *psig == NULL)
1223 ECDSA_SIG_free(sig);
1226 if (psig != NULL && *psig == NULL)
1231 int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **ppout)
1233 BUF_MEM *buf = NULL;
1237 if (ppout == NULL) {
1238 if (!WPACKET_init_null(&pkt, 0))
1240 } else if (*ppout == NULL) {
1241 if ((buf = BUF_MEM_new()) == NULL
1242 || !WPACKET_init_len(&pkt, buf, 0)) {
1247 if (!WPACKET_init_static_len(&pkt, *ppout, SIZE_MAX, 0))
1251 if (!encode_der_dsa_sig(&pkt, sig->r, sig->s)
1252 || !WPACKET_get_total_written(&pkt, &encoded_len)
1253 || !WPACKET_finish(&pkt)) {
1255 WPACKET_cleanup(&pkt);
1259 if (ppout != NULL) {
1260 if (*ppout == NULL) {
1261 *ppout = (unsigned char *)buf->data;
1265 *ppout += encoded_len;
1269 return (int)encoded_len;
1272 void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
1280 const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig)
1285 const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig)
1290 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
1292 if (r == NULL || s == NULL)
1294 BN_clear_free(sig->r);
1295 BN_clear_free(sig->s);
1301 int ECDSA_size(const EC_KEY *ec)
1305 const EC_GROUP *group;
1310 group = EC_KEY_get0_group(ec);
1314 bn = EC_GROUP_get0_order(group);
1318 sig.r = sig.s = (BIGNUM *)bn;
1319 ret = i2d_ECDSA_SIG(&sig, NULL);