2 * Copyright 2002-2021 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.h"
23 #include "crypto/asn1_dsa.h"
27 /* some structures needed for the asn1 encoding */
28 typedef struct x9_62_pentanomial_st
{
34 typedef struct x9_62_characteristic_two_st
{
39 /* NID_X9_62_onBasis */
41 /* NID_X9_62_tpBasis */
42 ASN1_INTEGER
*tpBasis
;
43 /* NID_X9_62_ppBasis */
44 X9_62_PENTANOMIAL
*ppBasis
;
48 } X9_62_CHARACTERISTIC_TWO
;
50 typedef struct x9_62_fieldid_st
{
51 ASN1_OBJECT
*fieldType
;
54 /* NID_X9_62_prime_field */
56 /* NID_X9_62_characteristic_two_field */
57 X9_62_CHARACTERISTIC_TWO
*char_two
;
63 typedef struct x9_62_curve_st
{
66 ASN1_BIT_STRING
*seed
;
69 struct ec_parameters_st
{
71 X9_62_FIELDID
*fieldID
;
73 ASN1_OCTET_STRING
*base
;
75 ASN1_INTEGER
*cofactor
;
76 } /* ECPARAMETERS */ ;
79 ECPKPARAMETERS_TYPE_NAMED
= 0,
80 ECPKPARAMETERS_TYPE_EXPLICIT
,
81 ECPKPARAMETERS_TYPE_IMPLICIT
82 } ecpk_parameters_type_t
;
84 struct ecpk_parameters_st
{
87 ASN1_OBJECT
*named_curve
;
88 ECPARAMETERS
*parameters
;
89 ASN1_NULL
*implicitlyCA
;
91 } /* ECPKPARAMETERS */ ;
93 /* SEC1 ECPrivateKey */
94 typedef struct ec_privatekey_st
{
96 ASN1_OCTET_STRING
*privateKey
;
97 ECPKPARAMETERS
*parameters
;
98 ASN1_BIT_STRING
*publicKey
;
101 /* the OpenSSL ASN.1 definitions */
102 ASN1_SEQUENCE(X9_62_PENTANOMIAL
) = {
103 ASN1_EMBED(X9_62_PENTANOMIAL
, k1
, INT32
),
104 ASN1_EMBED(X9_62_PENTANOMIAL
, k2
, INT32
),
105 ASN1_EMBED(X9_62_PENTANOMIAL
, k3
, INT32
)
106 } static_ASN1_SEQUENCE_END(X9_62_PENTANOMIAL
)
108 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL
)
109 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL
)
111 ASN1_ADB_TEMPLATE(char_two_def
) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, p
.other
, ASN1_ANY
);
113 ASN1_ADB(X9_62_CHARACTERISTIC_TWO
) = {
114 ADB_ENTRY(NID_X9_62_onBasis
, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, p
.onBasis
, ASN1_NULL
)),
115 ADB_ENTRY(NID_X9_62_tpBasis
, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, p
.tpBasis
, ASN1_INTEGER
)),
116 ADB_ENTRY(NID_X9_62_ppBasis
, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, p
.ppBasis
, X9_62_PENTANOMIAL
))
117 } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO
, 0, type
, 0, &char_two_def_tt
, NULL
);
119 ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO
) = {
120 ASN1_EMBED(X9_62_CHARACTERISTIC_TWO
, m
, INT32
),
121 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, type
, ASN1_OBJECT
),
122 ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO
)
123 } static_ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO
)
125 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO
)
126 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO
)
128 ASN1_ADB_TEMPLATE(fieldID_def
) = ASN1_SIMPLE(X9_62_FIELDID
, p
.other
, ASN1_ANY
);
130 ASN1_ADB(X9_62_FIELDID
) = {
131 ADB_ENTRY(NID_X9_62_prime_field
, ASN1_SIMPLE(X9_62_FIELDID
, p
.prime
, ASN1_INTEGER
)),
132 ADB_ENTRY(NID_X9_62_characteristic_two_field
, ASN1_SIMPLE(X9_62_FIELDID
, p
.char_two
, X9_62_CHARACTERISTIC_TWO
))
133 } ASN1_ADB_END(X9_62_FIELDID
, 0, fieldType
, 0, &fieldID_def_tt
, NULL
);
135 ASN1_SEQUENCE(X9_62_FIELDID
) = {
136 ASN1_SIMPLE(X9_62_FIELDID
, fieldType
, ASN1_OBJECT
),
137 ASN1_ADB_OBJECT(X9_62_FIELDID
)
138 } static_ASN1_SEQUENCE_END(X9_62_FIELDID
)
140 ASN1_SEQUENCE(X9_62_CURVE
) = {
141 ASN1_SIMPLE(X9_62_CURVE
, a
, ASN1_OCTET_STRING
),
142 ASN1_SIMPLE(X9_62_CURVE
, b
, ASN1_OCTET_STRING
),
143 ASN1_OPT(X9_62_CURVE
, seed
, ASN1_BIT_STRING
)
144 } static_ASN1_SEQUENCE_END(X9_62_CURVE
)
146 ASN1_SEQUENCE(ECPARAMETERS
) = {
147 ASN1_EMBED(ECPARAMETERS
, version
, INT32
),
148 ASN1_SIMPLE(ECPARAMETERS
, fieldID
, X9_62_FIELDID
),
149 ASN1_SIMPLE(ECPARAMETERS
, curve
, X9_62_CURVE
),
150 ASN1_SIMPLE(ECPARAMETERS
, base
, ASN1_OCTET_STRING
),
151 ASN1_SIMPLE(ECPARAMETERS
, order
, ASN1_INTEGER
),
152 ASN1_OPT(ECPARAMETERS
, cofactor
, ASN1_INTEGER
)
153 } ASN1_SEQUENCE_END(ECPARAMETERS
)
155 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS
)
156 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS
)
158 ASN1_CHOICE(ECPKPARAMETERS
) = {
159 ASN1_SIMPLE(ECPKPARAMETERS
, value
.named_curve
, ASN1_OBJECT
),
160 ASN1_SIMPLE(ECPKPARAMETERS
, value
.parameters
, ECPARAMETERS
),
161 ASN1_SIMPLE(ECPKPARAMETERS
, value
.implicitlyCA
, ASN1_NULL
)
162 } ASN1_CHOICE_END(ECPKPARAMETERS
)
164 DECLARE_ASN1_FUNCTIONS(ECPKPARAMETERS
)
165 DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECPKPARAMETERS
, ECPKPARAMETERS
)
166 IMPLEMENT_ASN1_FUNCTIONS(ECPKPARAMETERS
)
168 ASN1_SEQUENCE(EC_PRIVATEKEY
) = {
169 ASN1_EMBED(EC_PRIVATEKEY
, version
, INT32
),
170 ASN1_SIMPLE(EC_PRIVATEKEY
, privateKey
, ASN1_OCTET_STRING
),
171 ASN1_EXP_OPT(EC_PRIVATEKEY
, parameters
, ECPKPARAMETERS
, 0),
172 ASN1_EXP_OPT(EC_PRIVATEKEY
, publicKey
, ASN1_BIT_STRING
, 1)
173 } static_ASN1_SEQUENCE_END(EC_PRIVATEKEY
)
175 DECLARE_ASN1_FUNCTIONS(EC_PRIVATEKEY
)
176 DECLARE_ASN1_ENCODE_FUNCTIONS_name(EC_PRIVATEKEY
, EC_PRIVATEKEY
)
177 IMPLEMENT_ASN1_FUNCTIONS(EC_PRIVATEKEY
)
179 /* some declarations of internal function */
181 /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
182 static int ec_asn1_group2fieldid(const EC_GROUP
*, X9_62_FIELDID
*);
183 /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
184 static int ec_asn1_group2curve(const EC_GROUP
*, X9_62_CURVE
*);
186 /* the function definitions */
188 static int ec_asn1_group2fieldid(const EC_GROUP
*group
, X9_62_FIELDID
*field
)
193 if (group
== NULL
|| field
== NULL
)
196 /* clear the old values (if necessary) */
197 ASN1_OBJECT_free(field
->fieldType
);
198 ASN1_TYPE_free(field
->p
.other
);
200 nid
= EC_GROUP_get_field_type(group
);
201 /* set OID for the field */
202 if ((field
->fieldType
= OBJ_nid2obj(nid
)) == NULL
) {
203 ERR_raise(ERR_LIB_EC
, ERR_R_OBJ_LIB
);
207 if (nid
== NID_X9_62_prime_field
) {
208 if ((tmp
= BN_new()) == NULL
) {
209 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
212 /* the parameters are specified by the prime number p */
213 if (!EC_GROUP_get_curve(group
, tmp
, NULL
, NULL
, NULL
)) {
214 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
217 /* set the prime number */
218 field
->p
.prime
= BN_to_ASN1_INTEGER(tmp
, NULL
);
219 if (field
->p
.prime
== NULL
) {
220 ERR_raise(ERR_LIB_EC
, ERR_R_ASN1_LIB
);
223 } else if (nid
== NID_X9_62_characteristic_two_field
)
224 #ifdef OPENSSL_NO_EC2M
226 ERR_raise(ERR_LIB_EC
, EC_R_GF2M_NOT_SUPPORTED
);
232 X9_62_CHARACTERISTIC_TWO
*char_two
;
234 field
->p
.char_two
= X9_62_CHARACTERISTIC_TWO_new();
235 char_two
= field
->p
.char_two
;
237 if (char_two
== NULL
) {
238 ERR_raise(ERR_LIB_EC
, ERR_R_ASN1_LIB
);
242 char_two
->m
= (long)EC_GROUP_get_degree(group
);
244 field_type
= EC_GROUP_get_basis_type(group
);
246 if (field_type
== 0) {
247 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
250 /* set base type OID */
251 if ((char_two
->type
= OBJ_nid2obj(field_type
)) == NULL
) {
252 ERR_raise(ERR_LIB_EC
, ERR_R_OBJ_LIB
);
256 if (field_type
== NID_X9_62_tpBasis
) {
259 if (!EC_GROUP_get_trinomial_basis(group
, &k
))
262 char_two
->p
.tpBasis
= ASN1_INTEGER_new();
263 if (char_two
->p
.tpBasis
== NULL
) {
264 ERR_raise(ERR_LIB_EC
, ERR_R_ASN1_LIB
);
267 if (!ASN1_INTEGER_set(char_two
->p
.tpBasis
, (long)k
)) {
268 ERR_raise(ERR_LIB_EC
, ERR_R_ASN1_LIB
);
271 } else if (field_type
== NID_X9_62_ppBasis
) {
272 unsigned int k1
, k2
, k3
;
274 if (!EC_GROUP_get_pentanomial_basis(group
, &k1
, &k2
, &k3
))
277 char_two
->p
.ppBasis
= X9_62_PENTANOMIAL_new();
278 if (char_two
->p
.ppBasis
== NULL
) {
279 ERR_raise(ERR_LIB_EC
, ERR_R_ASN1_LIB
);
284 char_two
->p
.ppBasis
->k1
= (long)k1
;
285 char_two
->p
.ppBasis
->k2
= (long)k2
;
286 char_two
->p
.ppBasis
->k3
= (long)k3
;
287 } else { /* field_type == NID_X9_62_onBasis */
289 /* for ONB the parameters are (asn1) NULL */
290 char_two
->p
.onBasis
= ASN1_NULL_new();
291 if (char_two
->p
.onBasis
== NULL
) {
292 ERR_raise(ERR_LIB_EC
, ERR_R_ASN1_LIB
);
299 ERR_raise(ERR_LIB_EC
, EC_R_UNSUPPORTED_FIELD
);
310 static int ec_asn1_group2curve(const EC_GROUP
*group
, X9_62_CURVE
*curve
)
313 BIGNUM
*tmp_1
= NULL
, *tmp_2
= NULL
;
314 unsigned char *a_buf
= NULL
, *b_buf
= NULL
;
317 if (!group
|| !curve
|| !curve
->a
|| !curve
->b
)
320 if ((tmp_1
= BN_new()) == NULL
|| (tmp_2
= BN_new()) == NULL
) {
321 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
326 if (!EC_GROUP_get_curve(group
, NULL
, tmp_1
, tmp_2
, NULL
)) {
327 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
332 * Per SEC 1, the curve coefficients must be padded up to size. See C.2's
333 * definition of Curve, C.1's definition of FieldElement, and 2.3.5's
334 * definition of how to encode the field elements.
336 len
= ((size_t)EC_GROUP_get_degree(group
) + 7) / 8;
337 if ((a_buf
= OPENSSL_malloc(len
)) == NULL
338 || (b_buf
= OPENSSL_malloc(len
)) == NULL
)
340 if (BN_bn2binpad(tmp_1
, a_buf
, len
) < 0
341 || BN_bn2binpad(tmp_2
, b_buf
, len
) < 0) {
342 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
347 if (!ASN1_OCTET_STRING_set(curve
->a
, a_buf
, len
)
348 || !ASN1_OCTET_STRING_set(curve
->b
, b_buf
, len
)) {
349 ERR_raise(ERR_LIB_EC
, ERR_R_ASN1_LIB
);
353 /* set the seed (optional) */
356 if ((curve
->seed
= ASN1_BIT_STRING_new()) == NULL
) {
357 ERR_raise(ERR_LIB_EC
, ERR_R_ASN1_LIB
);
360 ossl_asn1_string_set_bits_left(curve
->seed
, 0);
361 if (!ASN1_BIT_STRING_set(curve
->seed
, group
->seed
,
362 (int)group
->seed_len
)) {
363 ERR_raise(ERR_LIB_EC
, ERR_R_ASN1_LIB
);
367 ASN1_BIT_STRING_free(curve
->seed
);
381 ECPARAMETERS
*EC_GROUP_get_ecparameters(const EC_GROUP
*group
,
382 ECPARAMETERS
*params
)
385 ECPARAMETERS
*ret
= NULL
;
387 unsigned char *buffer
= NULL
;
388 const EC_POINT
*point
= NULL
;
389 point_conversion_form_t form
;
392 if (params
== NULL
) {
393 if ((ret
= ECPARAMETERS_new()) == NULL
) {
394 ERR_raise(ERR_LIB_EC
, ERR_R_ASN1_LIB
);
400 /* set the version (always one) */
401 ret
->version
= (long)0x1;
403 /* set the fieldID */
404 if (!ec_asn1_group2fieldid(group
, ret
->fieldID
)) {
405 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
410 if (!ec_asn1_group2curve(group
, ret
->curve
)) {
411 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
415 /* set the base point */
416 if ((point
= EC_GROUP_get0_generator(group
)) == NULL
) {
417 ERR_raise(ERR_LIB_EC
, EC_R_UNDEFINED_GENERATOR
);
421 form
= EC_GROUP_get_point_conversion_form(group
);
423 len
= EC_POINT_point2buf(group
, point
, form
, &buffer
, NULL
);
425 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
428 if (ret
->base
== NULL
&& (ret
->base
= ASN1_OCTET_STRING_new()) == NULL
) {
429 OPENSSL_free(buffer
);
430 ERR_raise(ERR_LIB_EC
, ERR_R_ASN1_LIB
);
433 ASN1_STRING_set0(ret
->base
, buffer
, len
);
436 tmp
= EC_GROUP_get0_order(group
);
438 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
441 ret
->order
= BN_to_ASN1_INTEGER(tmp
, orig
= ret
->order
);
442 if (ret
->order
== NULL
) {
444 ERR_raise(ERR_LIB_EC
, ERR_R_ASN1_LIB
);
448 /* set the cofactor (optional) */
449 tmp
= EC_GROUP_get0_cofactor(group
);
451 ret
->cofactor
= BN_to_ASN1_INTEGER(tmp
, orig
= ret
->cofactor
);
452 if (ret
->cofactor
== NULL
) {
453 ret
->cofactor
= orig
;
454 ERR_raise(ERR_LIB_EC
, ERR_R_ASN1_LIB
);
463 ECPARAMETERS_free(ret
);
467 ECPKPARAMETERS
*EC_GROUP_get_ecpkparameters(const EC_GROUP
*group
,
468 ECPKPARAMETERS
*params
)
471 ECPKPARAMETERS
*ret
= params
;
474 if ((ret
= ECPKPARAMETERS_new()) == NULL
) {
475 ERR_raise(ERR_LIB_EC
, ERR_R_ASN1_LIB
);
479 if (ret
->type
== ECPKPARAMETERS_TYPE_NAMED
)
480 ASN1_OBJECT_free(ret
->value
.named_curve
);
481 else if (ret
->type
== ECPKPARAMETERS_TYPE_EXPLICIT
482 && ret
->value
.parameters
!= NULL
)
483 ECPARAMETERS_free(ret
->value
.parameters
);
486 if (EC_GROUP_get_asn1_flag(group
) == OPENSSL_EC_NAMED_CURVE
) {
488 * use the asn1 OID to describe the elliptic curve parameters
490 tmp
= EC_GROUP_get_curve_name(group
);
492 ASN1_OBJECT
*asn1obj
= OBJ_nid2obj(tmp
);
494 if (asn1obj
== NULL
|| OBJ_length(asn1obj
) == 0) {
495 ASN1_OBJECT_free(asn1obj
);
496 ERR_raise(ERR_LIB_EC
, EC_R_MISSING_OID
);
499 ret
->type
= ECPKPARAMETERS_TYPE_NAMED
;
500 ret
->value
.named_curve
= asn1obj
;
503 /* we don't know the nid => ERROR */
506 /* use the ECPARAMETERS structure */
507 ret
->type
= ECPKPARAMETERS_TYPE_EXPLICIT
;
508 if ((ret
->value
.parameters
=
509 EC_GROUP_get_ecparameters(group
, NULL
)) == NULL
)
514 ECPKPARAMETERS_free(ret
);
520 EC_GROUP
*EC_GROUP_new_from_ecparameters(const ECPARAMETERS
*params
)
523 EC_GROUP
*ret
= NULL
, *dup
= NULL
;
524 BIGNUM
*p
= NULL
, *a
= NULL
, *b
= NULL
;
525 EC_POINT
*point
= NULL
;
527 int curve_name
= NID_undef
;
530 if (params
->fieldID
== NULL
531 || params
->fieldID
->fieldType
== NULL
532 || params
->fieldID
->p
.ptr
== NULL
) {
533 ERR_raise(ERR_LIB_EC
, EC_R_ASN1_ERROR
);
538 * Now extract the curve parameters a and b. Note that, although SEC 1
539 * specifies the length of their encodings, historical versions of OpenSSL
540 * encoded them incorrectly, so we must accept any length for backwards
543 if (params
->curve
== NULL
544 || params
->curve
->a
== NULL
|| params
->curve
->a
->data
== NULL
545 || params
->curve
->b
== NULL
|| params
->curve
->b
->data
== NULL
) {
546 ERR_raise(ERR_LIB_EC
, EC_R_ASN1_ERROR
);
549 a
= BN_bin2bn(params
->curve
->a
->data
, params
->curve
->a
->length
, NULL
);
551 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
554 b
= BN_bin2bn(params
->curve
->b
->data
, params
->curve
->b
->length
, NULL
);
556 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
560 /* get the field parameters */
561 tmp
= OBJ_obj2nid(params
->fieldID
->fieldType
);
562 if (tmp
== NID_X9_62_characteristic_two_field
)
563 #ifdef OPENSSL_NO_EC2M
565 ERR_raise(ERR_LIB_EC
, EC_R_GF2M_NOT_SUPPORTED
);
570 X9_62_CHARACTERISTIC_TWO
*char_two
;
572 char_two
= params
->fieldID
->p
.char_two
;
574 field_bits
= char_two
->m
;
575 if (field_bits
> OPENSSL_ECC_MAX_FIELD_BITS
) {
576 ERR_raise(ERR_LIB_EC
, EC_R_FIELD_TOO_LARGE
);
580 if ((p
= BN_new()) == NULL
) {
581 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
585 /* get the base type */
586 tmp
= OBJ_obj2nid(char_two
->type
);
588 if (tmp
== NID_X9_62_tpBasis
) {
591 if (!char_two
->p
.tpBasis
) {
592 ERR_raise(ERR_LIB_EC
, EC_R_ASN1_ERROR
);
596 tmp_long
= ASN1_INTEGER_get(char_two
->p
.tpBasis
);
598 if (!(char_two
->m
> tmp_long
&& tmp_long
> 0)) {
599 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_TRINOMIAL_BASIS
);
603 /* create the polynomial */
604 if (!BN_set_bit(p
, (int)char_two
->m
))
606 if (!BN_set_bit(p
, (int)tmp_long
))
608 if (!BN_set_bit(p
, 0))
610 } else if (tmp
== NID_X9_62_ppBasis
) {
611 X9_62_PENTANOMIAL
*penta
;
613 penta
= char_two
->p
.ppBasis
;
615 ERR_raise(ERR_LIB_EC
, EC_R_ASN1_ERROR
);
620 (char_two
->m
> penta
->k3
&& penta
->k3
> penta
->k2
621 && penta
->k2
> penta
->k1
&& penta
->k1
> 0)) {
622 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_PENTANOMIAL_BASIS
);
626 /* create the polynomial */
627 if (!BN_set_bit(p
, (int)char_two
->m
))
629 if (!BN_set_bit(p
, (int)penta
->k1
))
631 if (!BN_set_bit(p
, (int)penta
->k2
))
633 if (!BN_set_bit(p
, (int)penta
->k3
))
635 if (!BN_set_bit(p
, 0))
637 } else if (tmp
== NID_X9_62_onBasis
) {
638 ERR_raise(ERR_LIB_EC
, EC_R_NOT_IMPLEMENTED
);
642 ERR_raise(ERR_LIB_EC
, EC_R_ASN1_ERROR
);
646 /* create the EC_GROUP structure */
647 ret
= EC_GROUP_new_curve_GF2m(p
, a
, b
, NULL
);
650 else if (tmp
== NID_X9_62_prime_field
) {
651 /* we have a curve over a prime field */
652 /* extract the prime number */
653 if (params
->fieldID
->p
.prime
== NULL
) {
654 ERR_raise(ERR_LIB_EC
, EC_R_ASN1_ERROR
);
657 p
= ASN1_INTEGER_to_BN(params
->fieldID
->p
.prime
, NULL
);
659 ERR_raise(ERR_LIB_EC
, ERR_R_ASN1_LIB
);
663 if (BN_is_negative(p
) || BN_is_zero(p
)) {
664 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_FIELD
);
668 field_bits
= BN_num_bits(p
);
669 if (field_bits
> OPENSSL_ECC_MAX_FIELD_BITS
) {
670 ERR_raise(ERR_LIB_EC
, EC_R_FIELD_TOO_LARGE
);
674 /* create the EC_GROUP structure */
675 ret
= EC_GROUP_new_curve_GFp(p
, a
, b
, NULL
);
677 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_FIELD
);
682 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
686 /* extract seed (optional) */
687 if (params
->curve
->seed
!= NULL
) {
689 * This happens for instance with
690 * fuzz/corpora/asn1/65cf44e85614c62f10cf3b7a7184c26293a19e4a
691 * and causes the OPENSSL_malloc below to choke on the
692 * zero length allocation request.
694 if (params
->curve
->seed
->length
== 0) {
695 ERR_raise(ERR_LIB_EC
, EC_R_ASN1_ERROR
);
698 OPENSSL_free(ret
->seed
);
699 if ((ret
->seed
= OPENSSL_malloc(params
->curve
->seed
->length
)) == NULL
)
701 memcpy(ret
->seed
, params
->curve
->seed
->data
,
702 params
->curve
->seed
->length
);
703 ret
->seed_len
= params
->curve
->seed
->length
;
706 if (params
->order
== NULL
707 || params
->base
== NULL
708 || params
->base
->data
== NULL
709 || params
->base
->length
== 0) {
710 ERR_raise(ERR_LIB_EC
, EC_R_ASN1_ERROR
);
714 if ((point
= EC_POINT_new(ret
)) == NULL
)
717 /* set the point conversion form */
718 EC_GROUP_set_point_conversion_form(ret
, (point_conversion_form_t
)
719 (params
->base
->data
[0] & ~0x01));
721 /* extract the ec point */
722 if (!EC_POINT_oct2point(ret
, point
, params
->base
->data
,
723 params
->base
->length
, NULL
)) {
724 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
728 /* extract the order */
729 if (ASN1_INTEGER_to_BN(params
->order
, a
) == NULL
) {
730 ERR_raise(ERR_LIB_EC
, ERR_R_ASN1_LIB
);
733 if (BN_is_negative(a
) || BN_is_zero(a
)) {
734 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GROUP_ORDER
);
737 if (BN_num_bits(a
) > (int)field_bits
+ 1) { /* Hasse bound */
738 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GROUP_ORDER
);
742 /* extract the cofactor (optional) */
743 if (params
->cofactor
== NULL
) {
746 } else if (ASN1_INTEGER_to_BN(params
->cofactor
, b
) == NULL
) {
747 ERR_raise(ERR_LIB_EC
, ERR_R_ASN1_LIB
);
750 /* set the generator, order and cofactor (if present) */
751 if (!EC_GROUP_set_generator(ret
, point
, a
, b
)) {
752 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
757 * Check if the explicit parameters group just created matches one of the
760 * We create a copy of the group just built, so that we can remove optional
761 * fields for the lookup: we do this to avoid the possibility that one of
762 * the optional parameters is used to force the library into using a less
763 * performant and less secure EC_METHOD instead of the specialized one.
764 * In any case, `seed` is not really used in any computation, while a
765 * cofactor different from the one in the built-in table is just
766 * mathematically wrong anyway and should not be used.
768 if ((ctx
= BN_CTX_new()) == NULL
) {
769 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
772 if ((dup
= EC_GROUP_dup(ret
)) == NULL
773 || EC_GROUP_set_seed(dup
, NULL
, 0) != 1
774 || !EC_GROUP_set_generator(dup
, point
, a
, NULL
)) {
775 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
778 if ((curve_name
= ossl_ec_curve_nid_from_params(dup
, ctx
)) != NID_undef
) {
780 * The input explicit parameters successfully matched one of the
781 * built-in curves: often for built-in curves we have specialized
782 * methods with better performance and hardening.
784 * In this case we replace the `EC_GROUP` created through explicit
785 * parameters with one created from a named group.
787 EC_GROUP
*named_group
= NULL
;
789 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
791 * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
792 * the same curve, we prefer the SECP nid when matching explicit
793 * parameters as that is associated with a specialized EC_METHOD.
795 if (curve_name
== NID_wap_wsg_idm_ecid_wtls12
)
796 curve_name
= NID_secp224r1
;
797 #endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
799 if ((named_group
= EC_GROUP_new_by_curve_name(curve_name
)) == NULL
) {
800 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
807 * Set the flag so that EC_GROUPs created from explicit parameters are
808 * serialized using explicit parameters by default.
810 EC_GROUP_set_asn1_flag(ret
, OPENSSL_EC_EXPLICIT_CURVE
);
813 * If the input params do not contain the optional seed field we make
814 * sure it is not added to the returned group.
816 * The seed field is not really used inside libcrypto anyway, and
817 * adding it to parsed explicit parameter keys would alter their DER
818 * encoding output (because of the extra field) which could impact
819 * applications fingerprinting keys by their DER encoding.
821 if (params
->curve
->seed
== NULL
) {
822 if (EC_GROUP_set_seed(ret
, NULL
, 0) != 1)
839 EC_POINT_free(point
);
846 EC_GROUP
*EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS
*params
)
848 EC_GROUP
*ret
= NULL
;
851 if (params
== NULL
) {
852 ERR_raise(ERR_LIB_EC
, EC_R_MISSING_PARAMETERS
);
856 if (params
->type
== ECPKPARAMETERS_TYPE_NAMED
) {
857 /* the curve is given by an OID */
858 tmp
= OBJ_obj2nid(params
->value
.named_curve
);
859 if ((ret
= EC_GROUP_new_by_curve_name(tmp
)) == NULL
) {
860 ERR_raise(ERR_LIB_EC
, EC_R_EC_GROUP_NEW_BY_NAME_FAILURE
);
863 EC_GROUP_set_asn1_flag(ret
, OPENSSL_EC_NAMED_CURVE
);
864 } else if (params
->type
== ECPKPARAMETERS_TYPE_EXPLICIT
) {
865 /* the parameters are given by an ECPARAMETERS structure */
866 ret
= EC_GROUP_new_from_ecparameters(params
->value
.parameters
);
868 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
871 EC_GROUP_set_asn1_flag(ret
, OPENSSL_EC_EXPLICIT_CURVE
);
872 } else if (params
->type
== ECPKPARAMETERS_TYPE_IMPLICIT
) {
873 /* implicit parameters inherited from CA - unsupported */
876 ERR_raise(ERR_LIB_EC
, EC_R_ASN1_ERROR
);
883 /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
885 EC_GROUP
*d2i_ECPKParameters(EC_GROUP
**a
, const unsigned char **in
, long len
)
887 EC_GROUP
*group
= NULL
;
888 ECPKPARAMETERS
*params
= NULL
;
889 const unsigned char *p
= *in
;
891 if ((params
= d2i_ECPKPARAMETERS(NULL
, &p
, len
)) == NULL
) {
892 ECPKPARAMETERS_free(params
);
896 if ((group
= EC_GROUP_new_from_ecpkparameters(params
)) == NULL
) {
897 ECPKPARAMETERS_free(params
);
901 if (params
->type
== ECPKPARAMETERS_TYPE_EXPLICIT
)
902 group
->decoded_from_explicit_params
= 1;
909 ECPKPARAMETERS_free(params
);
914 int i2d_ECPKParameters(const EC_GROUP
*a
, unsigned char **out
)
917 ECPKPARAMETERS
*tmp
= EC_GROUP_get_ecpkparameters(a
, NULL
);
919 ERR_raise(ERR_LIB_EC
, EC_R_GROUP2PKPARAMETERS_FAILURE
);
922 if ((ret
= i2d_ECPKPARAMETERS(tmp
, out
)) == 0) {
923 ERR_raise(ERR_LIB_EC
, EC_R_I2D_ECPKPARAMETERS_FAILURE
);
924 ECPKPARAMETERS_free(tmp
);
927 ECPKPARAMETERS_free(tmp
);
931 /* some EC_KEY functions */
933 EC_KEY
*d2i_ECPrivateKey(EC_KEY
**a
, const unsigned char **in
, long len
)
936 EC_PRIVATEKEY
*priv_key
= NULL
;
937 const unsigned char *p
= *in
;
939 if ((priv_key
= d2i_EC_PRIVATEKEY(NULL
, &p
, len
)) == NULL
)
942 if (a
== NULL
|| *a
== NULL
) {
943 if ((ret
= EC_KEY_new()) == NULL
) {
944 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
950 if (priv_key
->parameters
) {
951 EC_GROUP_free(ret
->group
);
952 ret
->group
= EC_GROUP_new_from_ecpkparameters(priv_key
->parameters
);
953 if (ret
->group
!= NULL
954 && priv_key
->parameters
->type
== ECPKPARAMETERS_TYPE_EXPLICIT
)
955 ret
->group
->decoded_from_explicit_params
= 1;
958 if (ret
->group
== NULL
) {
959 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
963 ret
->version
= priv_key
->version
;
965 if (priv_key
->privateKey
) {
966 ASN1_OCTET_STRING
*pkey
= priv_key
->privateKey
;
967 if (EC_KEY_oct2priv(ret
, ASN1_STRING_get0_data(pkey
),
968 ASN1_STRING_length(pkey
)) == 0)
971 ERR_raise(ERR_LIB_EC
, EC_R_MISSING_PRIVATE_KEY
);
975 if (EC_GROUP_get_curve_name(ret
->group
) == NID_sm2
)
976 EC_KEY_set_flags(ret
, EC_FLAG_SM2_RANGE
);
978 EC_POINT_clear_free(ret
->pub_key
);
979 ret
->pub_key
= EC_POINT_new(ret
->group
);
980 if (ret
->pub_key
== NULL
) {
981 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
985 if (priv_key
->publicKey
) {
986 const unsigned char *pub_oct
;
989 pub_oct
= ASN1_STRING_get0_data(priv_key
->publicKey
);
990 pub_oct_len
= ASN1_STRING_length(priv_key
->publicKey
);
991 if (!EC_KEY_oct2key(ret
, pub_oct
, pub_oct_len
, NULL
)) {
992 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
996 if (ret
->group
->meth
->keygenpub
== NULL
997 || ret
->group
->meth
->keygenpub(ret
) == 0)
999 /* Remember the original private-key-only encoding. */
1000 ret
->enc_flag
|= EC_PKEY_NO_PUBKEY
;
1005 EC_PRIVATEKEY_free(priv_key
);
1011 if (a
== NULL
|| *a
!= ret
)
1013 EC_PRIVATEKEY_free(priv_key
);
1017 int i2d_ECPrivateKey(const EC_KEY
*a
, unsigned char **out
)
1019 int ret
= 0, ok
= 0;
1020 unsigned char *priv
= NULL
, *pub
= NULL
;
1021 size_t privlen
= 0, publen
= 0;
1023 EC_PRIVATEKEY
*priv_key
= NULL
;
1025 if (a
== NULL
|| a
->group
== NULL
||
1026 (!(a
->enc_flag
& EC_PKEY_NO_PUBKEY
) && a
->pub_key
== NULL
)) {
1027 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
1031 if ((priv_key
= EC_PRIVATEKEY_new()) == NULL
) {
1032 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
1036 priv_key
->version
= a
->version
;
1038 privlen
= EC_KEY_priv2buf(a
, &priv
);
1041 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
1045 ASN1_STRING_set0(priv_key
->privateKey
, priv
, privlen
);
1048 if (!(a
->enc_flag
& EC_PKEY_NO_PARAMETERS
)) {
1049 if ((priv_key
->parameters
=
1050 EC_GROUP_get_ecpkparameters(a
->group
,
1051 priv_key
->parameters
)) == NULL
) {
1052 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
1057 if (!(a
->enc_flag
& EC_PKEY_NO_PUBKEY
)) {
1058 priv_key
->publicKey
= ASN1_BIT_STRING_new();
1059 if (priv_key
->publicKey
== NULL
) {
1060 ERR_raise(ERR_LIB_EC
, ERR_R_ASN1_LIB
);
1064 publen
= EC_KEY_key2buf(a
, a
->conv_form
, &pub
, NULL
);
1067 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
1071 ossl_asn1_string_set_bits_left(priv_key
->publicKey
, 0);
1072 ASN1_STRING_set0(priv_key
->publicKey
, pub
, publen
);
1076 if ((ret
= i2d_EC_PRIVATEKEY(priv_key
, out
)) == 0) {
1077 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
1082 OPENSSL_clear_free(priv
, privlen
);
1084 EC_PRIVATEKEY_free(priv_key
);
1085 return (ok
? ret
: 0);
1088 int i2d_ECParameters(const EC_KEY
*a
, unsigned char **out
)
1091 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
1094 return i2d_ECPKParameters(a
->group
, out
);
1097 EC_KEY
*d2i_ECParameters(EC_KEY
**a
, const unsigned char **in
, long len
)
1101 if (in
== NULL
|| *in
== NULL
) {
1102 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
1106 if (a
== NULL
|| *a
== NULL
) {
1107 if ((ret
= EC_KEY_new()) == NULL
) {
1108 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
1114 if (!d2i_ECPKParameters(&ret
->group
, in
, len
)) {
1115 if (a
== NULL
|| *a
!= ret
)
1122 if (EC_GROUP_get_curve_name(ret
->group
) == NID_sm2
)
1123 EC_KEY_set_flags(ret
, EC_FLAG_SM2_RANGE
);
1133 EC_KEY
*o2i_ECPublicKey(EC_KEY
**a
, const unsigned char **in
, long len
)
1137 if (a
== NULL
|| (*a
) == NULL
|| (*a
)->group
== NULL
) {
1139 * sorry, but a EC_GROUP-structure is necessary to set the public key
1141 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
1145 /* EC_KEY_opt2key updates dirty_cnt */
1146 if (!EC_KEY_oct2key(ret
, *in
, len
, NULL
)) {
1147 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
1154 int i2o_ECPublicKey(const EC_KEY
*a
, unsigned char **out
)
1160 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
1164 buf_len
= EC_POINT_point2oct(a
->group
, a
->pub_key
,
1165 a
->conv_form
, NULL
, 0, NULL
);
1167 if (out
== NULL
|| buf_len
== 0)
1168 /* out == NULL => just return the length of the octet string */
1172 if ((*out
= OPENSSL_malloc(buf_len
)) == NULL
)
1176 if (!EC_POINT_point2oct(a
->group
, a
->pub_key
, a
->conv_form
,
1177 *out
, buf_len
, NULL
)) {
1178 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
1190 DECLARE_ASN1_FUNCTIONS(ECDSA_SIG
)
1191 DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECDSA_SIG
, ECDSA_SIG
)
1193 #endif /* FIPS_MODULE */
1195 ECDSA_SIG
*ECDSA_SIG_new(void)
1197 ECDSA_SIG
*sig
= OPENSSL_zalloc(sizeof(*sig
));
1202 void ECDSA_SIG_free(ECDSA_SIG
*sig
)
1206 BN_clear_free(sig
->r
);
1207 BN_clear_free(sig
->s
);
1211 ECDSA_SIG
*d2i_ECDSA_SIG(ECDSA_SIG
**psig
, const unsigned char **ppin
, long len
)
1217 if (psig
!= NULL
&& *psig
!= NULL
) {
1220 sig
= ECDSA_SIG_new();
1228 if (sig
->r
== NULL
|| sig
->s
== NULL
1229 || ossl_decode_der_dsa_sig(sig
->r
, sig
->s
, ppin
, (size_t)len
) == 0) {
1230 if (psig
== NULL
|| *psig
== NULL
)
1231 ECDSA_SIG_free(sig
);
1234 if (psig
!= NULL
&& *psig
== NULL
)
1239 int i2d_ECDSA_SIG(const ECDSA_SIG
*sig
, unsigned char **ppout
)
1241 BUF_MEM
*buf
= NULL
;
1245 if (ppout
== NULL
) {
1246 if (!WPACKET_init_null(&pkt
, 0))
1248 } else if (*ppout
== NULL
) {
1249 if ((buf
= BUF_MEM_new()) == NULL
1250 || !WPACKET_init_len(&pkt
, buf
, 0)) {
1255 if (!WPACKET_init_static_len(&pkt
, *ppout
, SIZE_MAX
, 0))
1259 if (!ossl_encode_der_dsa_sig(&pkt
, sig
->r
, sig
->s
)
1260 || !WPACKET_get_total_written(&pkt
, &encoded_len
)
1261 || !WPACKET_finish(&pkt
)) {
1263 WPACKET_cleanup(&pkt
);
1267 if (ppout
!= NULL
) {
1268 if (*ppout
== NULL
) {
1269 *ppout
= (unsigned char *)buf
->data
;
1273 *ppout
+= encoded_len
;
1277 return (int)encoded_len
;
1280 void ECDSA_SIG_get0(const ECDSA_SIG
*sig
, const BIGNUM
**pr
, const BIGNUM
**ps
)
1288 const BIGNUM
*ECDSA_SIG_get0_r(const ECDSA_SIG
*sig
)
1293 const BIGNUM
*ECDSA_SIG_get0_s(const ECDSA_SIG
*sig
)
1298 int ECDSA_SIG_set0(ECDSA_SIG
*sig
, BIGNUM
*r
, BIGNUM
*s
)
1300 if (r
== NULL
|| s
== NULL
)
1302 BN_clear_free(sig
->r
);
1303 BN_clear_free(sig
->s
);
1309 int ECDSA_size(const EC_KEY
*ec
)
1313 const EC_GROUP
*group
;
1318 group
= EC_KEY_get0_group(ec
);
1322 bn
= EC_GROUP_get0_order(group
);
1326 sig
.r
= sig
.s
= (BIGNUM
*)bn
;
1327 ret
= i2d_ECDSA_SIG(&sig
, NULL
);