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 */ ;
77 struct ecpk_parameters_st
{
80 ASN1_OBJECT
*named_curve
;
81 ECPARAMETERS
*parameters
;
82 ASN1_NULL
*implicitlyCA
;
84 } /* ECPKPARAMETERS */ ;
86 /* SEC1 ECPrivateKey */
87 typedef struct ec_privatekey_st
{
89 ASN1_OCTET_STRING
*privateKey
;
90 ECPKPARAMETERS
*parameters
;
91 ASN1_BIT_STRING
*publicKey
;
94 /* the OpenSSL ASN.1 definitions */
95 ASN1_SEQUENCE(X9_62_PENTANOMIAL
) = {
96 ASN1_EMBED(X9_62_PENTANOMIAL
, k1
, INT32
),
97 ASN1_EMBED(X9_62_PENTANOMIAL
, k2
, INT32
),
98 ASN1_EMBED(X9_62_PENTANOMIAL
, k3
, INT32
)
99 } static_ASN1_SEQUENCE_END(X9_62_PENTANOMIAL
)
101 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL
)
102 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL
)
104 ASN1_ADB_TEMPLATE(char_two_def
) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, p
.other
, ASN1_ANY
);
106 ASN1_ADB(X9_62_CHARACTERISTIC_TWO
) = {
107 ADB_ENTRY(NID_X9_62_onBasis
, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, p
.onBasis
, ASN1_NULL
)),
108 ADB_ENTRY(NID_X9_62_tpBasis
, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, p
.tpBasis
, ASN1_INTEGER
)),
109 ADB_ENTRY(NID_X9_62_ppBasis
, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, p
.ppBasis
, X9_62_PENTANOMIAL
))
110 } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO
, 0, type
, 0, &char_two_def_tt
, NULL
);
112 ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO
) = {
113 ASN1_EMBED(X9_62_CHARACTERISTIC_TWO
, m
, INT32
),
114 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, type
, ASN1_OBJECT
),
115 ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO
)
116 } static_ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO
)
118 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO
)
119 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO
)
121 ASN1_ADB_TEMPLATE(fieldID_def
) = ASN1_SIMPLE(X9_62_FIELDID
, p
.other
, ASN1_ANY
);
123 ASN1_ADB(X9_62_FIELDID
) = {
124 ADB_ENTRY(NID_X9_62_prime_field
, ASN1_SIMPLE(X9_62_FIELDID
, p
.prime
, ASN1_INTEGER
)),
125 ADB_ENTRY(NID_X9_62_characteristic_two_field
, ASN1_SIMPLE(X9_62_FIELDID
, p
.char_two
, X9_62_CHARACTERISTIC_TWO
))
126 } ASN1_ADB_END(X9_62_FIELDID
, 0, fieldType
, 0, &fieldID_def_tt
, NULL
);
128 ASN1_SEQUENCE(X9_62_FIELDID
) = {
129 ASN1_SIMPLE(X9_62_FIELDID
, fieldType
, ASN1_OBJECT
),
130 ASN1_ADB_OBJECT(X9_62_FIELDID
)
131 } static_ASN1_SEQUENCE_END(X9_62_FIELDID
)
133 ASN1_SEQUENCE(X9_62_CURVE
) = {
134 ASN1_SIMPLE(X9_62_CURVE
, a
, ASN1_OCTET_STRING
),
135 ASN1_SIMPLE(X9_62_CURVE
, b
, ASN1_OCTET_STRING
),
136 ASN1_OPT(X9_62_CURVE
, seed
, ASN1_BIT_STRING
)
137 } static_ASN1_SEQUENCE_END(X9_62_CURVE
)
139 ASN1_SEQUENCE(ECPARAMETERS
) = {
140 ASN1_EMBED(ECPARAMETERS
, version
, INT32
),
141 ASN1_SIMPLE(ECPARAMETERS
, fieldID
, X9_62_FIELDID
),
142 ASN1_SIMPLE(ECPARAMETERS
, curve
, X9_62_CURVE
),
143 ASN1_SIMPLE(ECPARAMETERS
, base
, ASN1_OCTET_STRING
),
144 ASN1_SIMPLE(ECPARAMETERS
, order
, ASN1_INTEGER
),
145 ASN1_OPT(ECPARAMETERS
, cofactor
, ASN1_INTEGER
)
146 } ASN1_SEQUENCE_END(ECPARAMETERS
)
148 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS
)
149 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS
)
151 ASN1_CHOICE(ECPKPARAMETERS
) = {
152 ASN1_SIMPLE(ECPKPARAMETERS
, value
.named_curve
, ASN1_OBJECT
),
153 ASN1_SIMPLE(ECPKPARAMETERS
, value
.parameters
, ECPARAMETERS
),
154 ASN1_SIMPLE(ECPKPARAMETERS
, value
.implicitlyCA
, ASN1_NULL
)
155 } ASN1_CHOICE_END(ECPKPARAMETERS
)
157 DECLARE_ASN1_FUNCTIONS(ECPKPARAMETERS
)
158 DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECPKPARAMETERS
, ECPKPARAMETERS
)
159 IMPLEMENT_ASN1_FUNCTIONS(ECPKPARAMETERS
)
161 ASN1_SEQUENCE(EC_PRIVATEKEY
) = {
162 ASN1_EMBED(EC_PRIVATEKEY
, version
, INT32
),
163 ASN1_SIMPLE(EC_PRIVATEKEY
, privateKey
, ASN1_OCTET_STRING
),
164 ASN1_EXP_OPT(EC_PRIVATEKEY
, parameters
, ECPKPARAMETERS
, 0),
165 ASN1_EXP_OPT(EC_PRIVATEKEY
, publicKey
, ASN1_BIT_STRING
, 1)
166 } static_ASN1_SEQUENCE_END(EC_PRIVATEKEY
)
168 DECLARE_ASN1_FUNCTIONS(EC_PRIVATEKEY
)
169 DECLARE_ASN1_ENCODE_FUNCTIONS_name(EC_PRIVATEKEY
, EC_PRIVATEKEY
)
170 IMPLEMENT_ASN1_FUNCTIONS(EC_PRIVATEKEY
)
172 /* some declarations of internal function */
174 /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
175 static int ec_asn1_group2fieldid(const EC_GROUP
*, X9_62_FIELDID
*);
176 /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
177 static int ec_asn1_group2curve(const EC_GROUP
*, X9_62_CURVE
*);
179 /* the function definitions */
181 static int ec_asn1_group2fieldid(const EC_GROUP
*group
, X9_62_FIELDID
*field
)
186 if (group
== NULL
|| field
== NULL
)
189 /* clear the old values (if necessary) */
190 ASN1_OBJECT_free(field
->fieldType
);
191 ASN1_TYPE_free(field
->p
.other
);
193 nid
= EC_GROUP_get_field_type(group
);
194 /* set OID for the field */
195 if ((field
->fieldType
= OBJ_nid2obj(nid
)) == NULL
) {
196 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_OBJ_LIB
);
200 if (nid
== NID_X9_62_prime_field
) {
201 if ((tmp
= BN_new()) == NULL
) {
202 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_MALLOC_FAILURE
);
205 /* the parameters are specified by the prime number p */
206 if (!EC_GROUP_get_curve(group
, tmp
, NULL
, NULL
, NULL
)) {
207 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_EC_LIB
);
210 /* set the prime number */
211 field
->p
.prime
= BN_to_ASN1_INTEGER(tmp
, NULL
);
212 if (field
->p
.prime
== NULL
) {
213 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_ASN1_LIB
);
216 } else if (nid
== NID_X9_62_characteristic_two_field
)
217 #ifdef OPENSSL_NO_EC2M
219 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, EC_R_GF2M_NOT_SUPPORTED
);
225 X9_62_CHARACTERISTIC_TWO
*char_two
;
227 field
->p
.char_two
= X9_62_CHARACTERISTIC_TWO_new();
228 char_two
= field
->p
.char_two
;
230 if (char_two
== NULL
) {
231 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_MALLOC_FAILURE
);
235 char_two
->m
= (long)EC_GROUP_get_degree(group
);
237 field_type
= EC_GROUP_get_basis_type(group
);
239 if (field_type
== 0) {
240 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_EC_LIB
);
243 /* set base type OID */
244 if ((char_two
->type
= OBJ_nid2obj(field_type
)) == NULL
) {
245 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_OBJ_LIB
);
249 if (field_type
== NID_X9_62_tpBasis
) {
252 if (!EC_GROUP_get_trinomial_basis(group
, &k
))
255 char_two
->p
.tpBasis
= ASN1_INTEGER_new();
256 if (char_two
->p
.tpBasis
== NULL
) {
257 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_MALLOC_FAILURE
);
260 if (!ASN1_INTEGER_set(char_two
->p
.tpBasis
, (long)k
)) {
261 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_ASN1_LIB
);
264 } else if (field_type
== NID_X9_62_ppBasis
) {
265 unsigned int k1
, k2
, k3
;
267 if (!EC_GROUP_get_pentanomial_basis(group
, &k1
, &k2
, &k3
))
270 char_two
->p
.ppBasis
= X9_62_PENTANOMIAL_new();
271 if (char_two
->p
.ppBasis
== NULL
) {
272 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_MALLOC_FAILURE
);
277 char_two
->p
.ppBasis
->k1
= (long)k1
;
278 char_two
->p
.ppBasis
->k2
= (long)k2
;
279 char_two
->p
.ppBasis
->k3
= (long)k3
;
280 } else { /* field_type == NID_X9_62_onBasis */
282 /* for ONB the parameters are (asn1) NULL */
283 char_two
->p
.onBasis
= ASN1_NULL_new();
284 if (char_two
->p
.onBasis
== NULL
) {
285 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_MALLOC_FAILURE
);
292 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, EC_R_UNSUPPORTED_FIELD
);
303 static int ec_asn1_group2curve(const EC_GROUP
*group
, X9_62_CURVE
*curve
)
306 BIGNUM
*tmp_1
= NULL
, *tmp_2
= NULL
;
307 unsigned char *a_buf
= NULL
, *b_buf
= NULL
;
310 if (!group
|| !curve
|| !curve
->a
|| !curve
->b
)
313 if ((tmp_1
= BN_new()) == NULL
|| (tmp_2
= BN_new()) == NULL
) {
314 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_MALLOC_FAILURE
);
319 if (!EC_GROUP_get_curve(group
, NULL
, tmp_1
, tmp_2
, NULL
)) {
320 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_EC_LIB
);
325 * Per SEC 1, the curve coefficients must be padded up to size. See C.2's
326 * definition of Curve, C.1's definition of FieldElement, and 2.3.5's
327 * definition of how to encode the field elements.
329 len
= ((size_t)EC_GROUP_get_degree(group
) + 7) / 8;
330 if ((a_buf
= OPENSSL_malloc(len
)) == NULL
331 || (b_buf
= OPENSSL_malloc(len
)) == NULL
) {
332 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_MALLOC_FAILURE
);
335 if (BN_bn2binpad(tmp_1
, a_buf
, len
) < 0
336 || BN_bn2binpad(tmp_2
, b_buf
, len
) < 0) {
337 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_BN_LIB
);
342 if (!ASN1_OCTET_STRING_set(curve
->a
, a_buf
, len
)
343 || !ASN1_OCTET_STRING_set(curve
->b
, b_buf
, len
)) {
344 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_ASN1_LIB
);
348 /* set the seed (optional) */
351 if ((curve
->seed
= ASN1_BIT_STRING_new()) == NULL
) {
352 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_MALLOC_FAILURE
);
355 curve
->seed
->flags
&= ~(ASN1_STRING_FLAG_BITS_LEFT
| 0x07);
356 curve
->seed
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
357 if (!ASN1_BIT_STRING_set(curve
->seed
, group
->seed
,
358 (int)group
->seed_len
)) {
359 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_ASN1_LIB
);
363 ASN1_BIT_STRING_free(curve
->seed
);
377 ECPARAMETERS
*EC_GROUP_get_ecparameters(const EC_GROUP
*group
,
378 ECPARAMETERS
*params
)
381 ECPARAMETERS
*ret
= NULL
;
383 unsigned char *buffer
= NULL
;
384 const EC_POINT
*point
= NULL
;
385 point_conversion_form_t form
;
388 if (params
== NULL
) {
389 if ((ret
= ECPARAMETERS_new()) == NULL
) {
390 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS
, ERR_R_MALLOC_FAILURE
);
396 /* set the version (always one) */
397 ret
->version
= (long)0x1;
399 /* set the fieldID */
400 if (!ec_asn1_group2fieldid(group
, ret
->fieldID
)) {
401 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS
, ERR_R_EC_LIB
);
406 if (!ec_asn1_group2curve(group
, ret
->curve
)) {
407 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS
, ERR_R_EC_LIB
);
411 /* set the base point */
412 if ((point
= EC_GROUP_get0_generator(group
)) == NULL
) {
413 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS
, EC_R_UNDEFINED_GENERATOR
);
417 form
= EC_GROUP_get_point_conversion_form(group
);
419 len
= EC_POINT_point2buf(group
, point
, form
, &buffer
, NULL
);
421 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS
, ERR_R_EC_LIB
);
424 if (ret
->base
== NULL
&& (ret
->base
= ASN1_OCTET_STRING_new()) == NULL
) {
425 OPENSSL_free(buffer
);
426 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS
, ERR_R_MALLOC_FAILURE
);
429 ASN1_STRING_set0(ret
->base
, buffer
, len
);
432 tmp
= EC_GROUP_get0_order(group
);
434 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS
, ERR_R_EC_LIB
);
437 ret
->order
= BN_to_ASN1_INTEGER(tmp
, orig
= ret
->order
);
438 if (ret
->order
== NULL
) {
440 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS
, ERR_R_ASN1_LIB
);
444 /* set the cofactor (optional) */
445 tmp
= EC_GROUP_get0_cofactor(group
);
447 ret
->cofactor
= BN_to_ASN1_INTEGER(tmp
, orig
= ret
->cofactor
);
448 if (ret
->cofactor
== NULL
) {
449 ret
->cofactor
= orig
;
450 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS
, ERR_R_ASN1_LIB
);
459 ECPARAMETERS_free(ret
);
463 ECPKPARAMETERS
*EC_GROUP_get_ecpkparameters(const EC_GROUP
*group
,
464 ECPKPARAMETERS
*params
)
467 ECPKPARAMETERS
*ret
= params
;
470 if ((ret
= ECPKPARAMETERS_new()) == NULL
) {
471 ECerr(EC_F_EC_GROUP_GET_ECPKPARAMETERS
, ERR_R_MALLOC_FAILURE
);
476 ASN1_OBJECT_free(ret
->value
.named_curve
);
477 else if (ret
->type
== 1 && ret
->value
.parameters
)
478 ECPARAMETERS_free(ret
->value
.parameters
);
481 if (EC_GROUP_get_asn1_flag(group
)) {
483 * use the asn1 OID to describe the elliptic curve parameters
485 tmp
= EC_GROUP_get_curve_name(group
);
487 ASN1_OBJECT
*asn1obj
= OBJ_nid2obj(tmp
);
489 if (asn1obj
== NULL
|| OBJ_length(asn1obj
) == 0) {
490 ASN1_OBJECT_free(asn1obj
);
491 ECerr(EC_F_EC_GROUP_GET_ECPKPARAMETERS
, EC_R_MISSING_OID
);
495 ret
->value
.named_curve
= asn1obj
;
498 /* we don't know the nid => ERROR */
501 /* use the ECPARAMETERS structure */
503 if ((ret
->value
.parameters
=
504 EC_GROUP_get_ecparameters(group
, NULL
)) == NULL
)
509 ECPKPARAMETERS_free(ret
);
515 EC_GROUP
*EC_GROUP_new_from_ecparameters(const ECPARAMETERS
*params
)
518 EC_GROUP
*ret
= NULL
, *dup
= NULL
;
519 BIGNUM
*p
= NULL
, *a
= NULL
, *b
= NULL
;
520 EC_POINT
*point
= NULL
;
522 int curve_name
= NID_undef
;
525 if (params
->fieldID
== NULL
526 || params
->fieldID
->fieldType
== NULL
527 || params
->fieldID
->p
.ptr
== NULL
) {
528 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_ASN1_ERROR
);
533 * Now extract the curve parameters a and b. Note that, although SEC 1
534 * specifies the length of their encodings, historical versions of OpenSSL
535 * encoded them incorrectly, so we must accept any length for backwards
538 if (params
->curve
== NULL
539 || params
->curve
->a
== NULL
|| params
->curve
->a
->data
== NULL
540 || params
->curve
->b
== NULL
|| params
->curve
->b
->data
== NULL
) {
541 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_ASN1_ERROR
);
544 a
= BN_bin2bn(params
->curve
->a
->data
, params
->curve
->a
->length
, NULL
);
546 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_BN_LIB
);
549 b
= BN_bin2bn(params
->curve
->b
->data
, params
->curve
->b
->length
, NULL
);
551 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_BN_LIB
);
555 /* get the field parameters */
556 tmp
= OBJ_obj2nid(params
->fieldID
->fieldType
);
557 if (tmp
== NID_X9_62_characteristic_two_field
)
558 #ifdef OPENSSL_NO_EC2M
560 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_GF2M_NOT_SUPPORTED
);
565 X9_62_CHARACTERISTIC_TWO
*char_two
;
567 char_two
= params
->fieldID
->p
.char_two
;
569 field_bits
= char_two
->m
;
570 if (field_bits
> OPENSSL_ECC_MAX_FIELD_BITS
) {
571 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_FIELD_TOO_LARGE
);
575 if ((p
= BN_new()) == NULL
) {
576 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_MALLOC_FAILURE
);
580 /* get the base type */
581 tmp
= OBJ_obj2nid(char_two
->type
);
583 if (tmp
== NID_X9_62_tpBasis
) {
586 if (!char_two
->p
.tpBasis
) {
587 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_ASN1_ERROR
);
591 tmp_long
= ASN1_INTEGER_get(char_two
->p
.tpBasis
);
593 if (!(char_two
->m
> tmp_long
&& tmp_long
> 0)) {
594 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
,
595 EC_R_INVALID_TRINOMIAL_BASIS
);
599 /* create the polynomial */
600 if (!BN_set_bit(p
, (int)char_two
->m
))
602 if (!BN_set_bit(p
, (int)tmp_long
))
604 if (!BN_set_bit(p
, 0))
606 } else if (tmp
== NID_X9_62_ppBasis
) {
607 X9_62_PENTANOMIAL
*penta
;
609 penta
= char_two
->p
.ppBasis
;
611 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_ASN1_ERROR
);
616 (char_two
->m
> penta
->k3
&& penta
->k3
> penta
->k2
617 && penta
->k2
> penta
->k1
&& penta
->k1
> 0)) {
618 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
,
619 EC_R_INVALID_PENTANOMIAL_BASIS
);
623 /* create the polynomial */
624 if (!BN_set_bit(p
, (int)char_two
->m
))
626 if (!BN_set_bit(p
, (int)penta
->k1
))
628 if (!BN_set_bit(p
, (int)penta
->k2
))
630 if (!BN_set_bit(p
, (int)penta
->k3
))
632 if (!BN_set_bit(p
, 0))
634 } else if (tmp
== NID_X9_62_onBasis
) {
635 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_NOT_IMPLEMENTED
);
639 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_ASN1_ERROR
);
643 /* create the EC_GROUP structure */
644 ret
= EC_GROUP_new_curve_GF2m(p
, a
, b
, NULL
);
647 else if (tmp
== NID_X9_62_prime_field
) {
648 /* we have a curve over a prime field */
649 /* extract the prime number */
650 if (params
->fieldID
->p
.prime
== NULL
) {
651 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_ASN1_ERROR
);
654 p
= ASN1_INTEGER_to_BN(params
->fieldID
->p
.prime
, NULL
);
656 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_ASN1_LIB
);
660 if (BN_is_negative(p
) || BN_is_zero(p
)) {
661 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_INVALID_FIELD
);
665 field_bits
= BN_num_bits(p
);
666 if (field_bits
> OPENSSL_ECC_MAX_FIELD_BITS
) {
667 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_FIELD_TOO_LARGE
);
671 /* create the EC_GROUP structure */
672 ret
= EC_GROUP_new_curve_GFp(p
, a
, b
, NULL
);
674 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_INVALID_FIELD
);
679 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_EC_LIB
);
683 /* extract seed (optional) */
684 if (params
->curve
->seed
!= NULL
) {
685 OPENSSL_free(ret
->seed
);
686 if ((ret
->seed
= OPENSSL_malloc(params
->curve
->seed
->length
)) == NULL
) {
687 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_MALLOC_FAILURE
);
690 memcpy(ret
->seed
, params
->curve
->seed
->data
,
691 params
->curve
->seed
->length
);
692 ret
->seed_len
= params
->curve
->seed
->length
;
695 if (params
->order
== NULL
696 || params
->base
== NULL
697 || params
->base
->data
== NULL
) {
698 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_ASN1_ERROR
);
702 if ((point
= EC_POINT_new(ret
)) == NULL
)
705 /* set the point conversion form */
706 EC_GROUP_set_point_conversion_form(ret
, (point_conversion_form_t
)
707 (params
->base
->data
[0] & ~0x01));
709 /* extract the ec point */
710 if (!EC_POINT_oct2point(ret
, point
, params
->base
->data
,
711 params
->base
->length
, NULL
)) {
712 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_EC_LIB
);
716 /* extract the order */
717 if ((a
= ASN1_INTEGER_to_BN(params
->order
, a
)) == NULL
) {
718 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_ASN1_LIB
);
721 if (BN_is_negative(a
) || BN_is_zero(a
)) {
722 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_INVALID_GROUP_ORDER
);
725 if (BN_num_bits(a
) > (int)field_bits
+ 1) { /* Hasse bound */
726 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_INVALID_GROUP_ORDER
);
730 /* extract the cofactor (optional) */
731 if (params
->cofactor
== NULL
) {
734 } else if ((b
= ASN1_INTEGER_to_BN(params
->cofactor
, b
)) == NULL
) {
735 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_ASN1_LIB
);
738 /* set the generator, order and cofactor (if present) */
739 if (!EC_GROUP_set_generator(ret
, point
, a
, b
)) {
740 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_EC_LIB
);
745 * Check if the explicit parameters group just created matches one of the
748 * We create a copy of the group just built, so that we can remove optional
749 * fields for the lookup: we do this to avoid the possibility that one of
750 * the optional parameters is used to force the library into using a less
751 * performant and less secure EC_METHOD instead of the specialized one.
752 * In any case, `seed` is not really used in any computation, while a
753 * cofactor different from the one in the built-in table is just
754 * mathematically wrong anyway and should not be used.
756 if ((ctx
= BN_CTX_new()) == NULL
) {
757 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_BN_LIB
);
760 if ((dup
= EC_GROUP_dup(ret
)) == NULL
761 || EC_GROUP_set_seed(dup
, NULL
, 0) != 1
762 || !EC_GROUP_set_generator(dup
, point
, a
, NULL
)) {
763 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_EC_LIB
);
766 if ((curve_name
= ec_curve_nid_from_params(dup
, ctx
)) != NID_undef
) {
768 * The input explicit parameters successfully matched one of the
769 * built-in curves: often for built-in curves we have specialized
770 * methods with better performance and hardening.
772 * In this case we replace the `EC_GROUP` created through explicit
773 * parameters with one created from a named group.
775 EC_GROUP
*named_group
= NULL
;
777 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
779 * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
780 * the same curve, we prefer the SECP nid when matching explicit
781 * parameters as that is associated with a specialized EC_METHOD.
783 if (curve_name
== NID_wap_wsg_idm_ecid_wtls12
)
784 curve_name
= NID_secp224r1
;
785 #endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
787 if ((named_group
= EC_GROUP_new_by_curve_name(curve_name
)) == NULL
) {
788 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_EC_LIB
);
795 * Set the flag so that EC_GROUPs created from explicit parameters are
796 * serialized using explicit parameters by default.
798 EC_GROUP_set_asn1_flag(ret
, OPENSSL_EC_EXPLICIT_CURVE
);
801 * If the input params do not contain the optional seed field we make
802 * sure it is not added to the returned group.
804 * The seed field is not really used inside libcrypto anyway, and
805 * adding it to parsed explicit parameter keys would alter their DER
806 * encoding output (because of the extra field) which could impact
807 * applications fingerprinting keys by their DER encoding.
809 if (params
->curve
->seed
== NULL
) {
810 if (EC_GROUP_set_seed(ret
, NULL
, 0) != 1)
827 EC_POINT_free(point
);
834 EC_GROUP
*EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS
*params
)
836 EC_GROUP
*ret
= NULL
;
839 if (params
== NULL
) {
840 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS
, EC_R_MISSING_PARAMETERS
);
844 if (params
->type
== 0) { /* the curve is given by an OID */
845 tmp
= OBJ_obj2nid(params
->value
.named_curve
);
846 if ((ret
= EC_GROUP_new_by_curve_name(tmp
)) == NULL
) {
847 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS
,
848 EC_R_EC_GROUP_NEW_BY_NAME_FAILURE
);
851 EC_GROUP_set_asn1_flag(ret
, OPENSSL_EC_NAMED_CURVE
);
852 } else if (params
->type
== 1) { /* the parameters are given by a
853 * ECPARAMETERS structure */
854 ret
= EC_GROUP_new_from_ecparameters(params
->value
.parameters
);
856 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS
, ERR_R_EC_LIB
);
859 EC_GROUP_set_asn1_flag(ret
, OPENSSL_EC_EXPLICIT_CURVE
);
860 } else if (params
->type
== 2) { /* implicitlyCA */
863 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS
, EC_R_ASN1_ERROR
);
870 /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
872 EC_GROUP
*d2i_ECPKParameters(EC_GROUP
**a
, const unsigned char **in
, long len
)
874 EC_GROUP
*group
= NULL
;
875 ECPKPARAMETERS
*params
= NULL
;
876 const unsigned char *p
= *in
;
878 if ((params
= d2i_ECPKPARAMETERS(NULL
, &p
, len
)) == NULL
) {
879 ECerr(EC_F_D2I_ECPKPARAMETERS
, EC_R_D2I_ECPKPARAMETERS_FAILURE
);
880 ECPKPARAMETERS_free(params
);
884 if ((group
= EC_GROUP_new_from_ecpkparameters(params
)) == NULL
) {
885 ECerr(EC_F_D2I_ECPKPARAMETERS
, EC_R_PKPARAMETERS2GROUP_FAILURE
);
886 ECPKPARAMETERS_free(params
);
895 ECPKPARAMETERS_free(params
);
900 int i2d_ECPKParameters(const EC_GROUP
*a
, unsigned char **out
)
903 ECPKPARAMETERS
*tmp
= EC_GROUP_get_ecpkparameters(a
, NULL
);
905 ECerr(EC_F_I2D_ECPKPARAMETERS
, EC_R_GROUP2PKPARAMETERS_FAILURE
);
908 if ((ret
= i2d_ECPKPARAMETERS(tmp
, out
)) == 0) {
909 ECerr(EC_F_I2D_ECPKPARAMETERS
, EC_R_I2D_ECPKPARAMETERS_FAILURE
);
910 ECPKPARAMETERS_free(tmp
);
913 ECPKPARAMETERS_free(tmp
);
917 /* some EC_KEY functions */
919 EC_KEY
*d2i_ECPrivateKey(EC_KEY
**a
, const unsigned char **in
, long len
)
922 EC_PRIVATEKEY
*priv_key
= NULL
;
923 const unsigned char *p
= *in
;
925 if ((priv_key
= d2i_EC_PRIVATEKEY(NULL
, &p
, len
)) == NULL
) {
926 ECerr(EC_F_D2I_ECPRIVATEKEY
, ERR_R_EC_LIB
);
930 if (a
== NULL
|| *a
== NULL
) {
931 if ((ret
= EC_KEY_new()) == NULL
) {
932 ECerr(EC_F_D2I_ECPRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
938 if (priv_key
->parameters
) {
939 EC_GROUP_free(ret
->group
);
940 ret
->group
= EC_GROUP_new_from_ecpkparameters(priv_key
->parameters
);
943 if (ret
->group
== NULL
) {
944 ECerr(EC_F_D2I_ECPRIVATEKEY
, ERR_R_EC_LIB
);
948 ret
->version
= priv_key
->version
;
950 if (priv_key
->privateKey
) {
951 ASN1_OCTET_STRING
*pkey
= priv_key
->privateKey
;
952 if (EC_KEY_oct2priv(ret
, ASN1_STRING_get0_data(pkey
),
953 ASN1_STRING_length(pkey
)) == 0)
956 ECerr(EC_F_D2I_ECPRIVATEKEY
, EC_R_MISSING_PRIVATE_KEY
);
960 EC_POINT_clear_free(ret
->pub_key
);
961 ret
->pub_key
= EC_POINT_new(ret
->group
);
962 if (ret
->pub_key
== NULL
) {
963 ECerr(EC_F_D2I_ECPRIVATEKEY
, ERR_R_EC_LIB
);
967 if (priv_key
->publicKey
) {
968 const unsigned char *pub_oct
;
971 pub_oct
= ASN1_STRING_get0_data(priv_key
->publicKey
);
972 pub_oct_len
= ASN1_STRING_length(priv_key
->publicKey
);
973 if (!EC_KEY_oct2key(ret
, pub_oct
, pub_oct_len
, NULL
)) {
974 ECerr(EC_F_D2I_ECPRIVATEKEY
, ERR_R_EC_LIB
);
978 if (ret
->group
->meth
->keygenpub
== NULL
979 || ret
->group
->meth
->keygenpub(ret
) == 0)
981 /* Remember the original private-key-only encoding. */
982 ret
->enc_flag
|= EC_PKEY_NO_PUBKEY
;
987 EC_PRIVATEKEY_free(priv_key
);
993 if (a
== NULL
|| *a
!= ret
)
995 EC_PRIVATEKEY_free(priv_key
);
999 int i2d_ECPrivateKey(const EC_KEY
*a
, unsigned char **out
)
1001 int ret
= 0, ok
= 0;
1002 unsigned char *priv
= NULL
, *pub
= NULL
;
1003 size_t privlen
= 0, publen
= 0;
1005 EC_PRIVATEKEY
*priv_key
= NULL
;
1007 if (a
== NULL
|| a
->group
== NULL
||
1008 (!(a
->enc_flag
& EC_PKEY_NO_PUBKEY
) && a
->pub_key
== NULL
)) {
1009 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
1013 if ((priv_key
= EC_PRIVATEKEY_new()) == NULL
) {
1014 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
1018 priv_key
->version
= a
->version
;
1020 privlen
= EC_KEY_priv2buf(a
, &priv
);
1023 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_EC_LIB
);
1027 ASN1_STRING_set0(priv_key
->privateKey
, priv
, privlen
);
1030 if (!(a
->enc_flag
& EC_PKEY_NO_PARAMETERS
)) {
1031 if ((priv_key
->parameters
=
1032 EC_GROUP_get_ecpkparameters(a
->group
,
1033 priv_key
->parameters
)) == NULL
) {
1034 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_EC_LIB
);
1039 if (!(a
->enc_flag
& EC_PKEY_NO_PUBKEY
)) {
1040 priv_key
->publicKey
= ASN1_BIT_STRING_new();
1041 if (priv_key
->publicKey
== NULL
) {
1042 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
1046 publen
= EC_KEY_key2buf(a
, a
->conv_form
, &pub
, NULL
);
1049 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_EC_LIB
);
1053 priv_key
->publicKey
->flags
&= ~(ASN1_STRING_FLAG_BITS_LEFT
| 0x07);
1054 priv_key
->publicKey
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
1055 ASN1_STRING_set0(priv_key
->publicKey
, pub
, publen
);
1059 if ((ret
= i2d_EC_PRIVATEKEY(priv_key
, out
)) == 0) {
1060 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_EC_LIB
);
1065 OPENSSL_clear_free(priv
, privlen
);
1067 EC_PRIVATEKEY_free(priv_key
);
1068 return (ok
? ret
: 0);
1071 int i2d_ECParameters(const EC_KEY
*a
, unsigned char **out
)
1074 ECerr(EC_F_I2D_ECPARAMETERS
, ERR_R_PASSED_NULL_PARAMETER
);
1077 return i2d_ECPKParameters(a
->group
, out
);
1080 EC_KEY
*d2i_ECParameters(EC_KEY
**a
, const unsigned char **in
, long len
)
1084 if (in
== NULL
|| *in
== NULL
) {
1085 ECerr(EC_F_D2I_ECPARAMETERS
, ERR_R_PASSED_NULL_PARAMETER
);
1089 if (a
== NULL
|| *a
== NULL
) {
1090 if ((ret
= EC_KEY_new()) == NULL
) {
1091 ECerr(EC_F_D2I_ECPARAMETERS
, ERR_R_MALLOC_FAILURE
);
1097 if (!d2i_ECPKParameters(&ret
->group
, in
, len
)) {
1098 ECerr(EC_F_D2I_ECPARAMETERS
, ERR_R_EC_LIB
);
1099 if (a
== NULL
|| *a
!= ret
)
1113 EC_KEY
*o2i_ECPublicKey(EC_KEY
**a
, const unsigned char **in
, long len
)
1117 if (a
== NULL
|| (*a
) == NULL
|| (*a
)->group
== NULL
) {
1119 * sorry, but a EC_GROUP-structure is necessary to set the public key
1121 ECerr(EC_F_O2I_ECPUBLICKEY
, ERR_R_PASSED_NULL_PARAMETER
);
1125 /* EC_KEY_opt2key updates dirty_cnt */
1126 if (!EC_KEY_oct2key(ret
, *in
, len
, NULL
)) {
1127 ECerr(EC_F_O2I_ECPUBLICKEY
, ERR_R_EC_LIB
);
1134 int i2o_ECPublicKey(const EC_KEY
*a
, unsigned char **out
)
1140 ECerr(EC_F_I2O_ECPUBLICKEY
, ERR_R_PASSED_NULL_PARAMETER
);
1144 buf_len
= EC_POINT_point2oct(a
->group
, a
->pub_key
,
1145 a
->conv_form
, NULL
, 0, NULL
);
1147 if (out
== NULL
|| buf_len
== 0)
1148 /* out == NULL => just return the length of the octet string */
1152 if ((*out
= OPENSSL_malloc(buf_len
)) == NULL
) {
1153 ECerr(EC_F_I2O_ECPUBLICKEY
, ERR_R_MALLOC_FAILURE
);
1158 if (!EC_POINT_point2oct(a
->group
, a
->pub_key
, a
->conv_form
,
1159 *out
, buf_len
, NULL
)) {
1160 ECerr(EC_F_I2O_ECPUBLICKEY
, ERR_R_EC_LIB
);
1172 DECLARE_ASN1_FUNCTIONS(ECDSA_SIG
)
1173 DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECDSA_SIG
, ECDSA_SIG
)
1175 #endif /* FIPS_MODULE */
1177 ECDSA_SIG
*ECDSA_SIG_new(void)
1179 ECDSA_SIG
*sig
= OPENSSL_zalloc(sizeof(*sig
));
1181 ECerr(EC_F_ECDSA_SIG_NEW
, ERR_R_MALLOC_FAILURE
);
1185 void ECDSA_SIG_free(ECDSA_SIG
*sig
)
1189 BN_clear_free(sig
->r
);
1190 BN_clear_free(sig
->s
);
1194 ECDSA_SIG
*d2i_ECDSA_SIG(ECDSA_SIG
**psig
, const unsigned char **ppin
, long len
)
1200 if (psig
!= NULL
&& *psig
!= NULL
) {
1203 sig
= ECDSA_SIG_new();
1211 if (decode_der_dsa_sig(sig
->r
, sig
->s
, ppin
, (size_t)len
) == 0) {
1212 if (psig
== NULL
|| *psig
== NULL
)
1213 ECDSA_SIG_free(sig
);
1216 if (psig
!= NULL
&& *psig
== NULL
)
1221 int i2d_ECDSA_SIG(const ECDSA_SIG
*sig
, unsigned char **ppout
)
1223 BUF_MEM
*buf
= NULL
;
1227 if (ppout
== NULL
) {
1228 if (!WPACKET_init_null(&pkt
, 0))
1230 } else if (*ppout
== NULL
) {
1231 if ((buf
= BUF_MEM_new()) == NULL
1232 || !WPACKET_init_len(&pkt
, buf
, 0)) {
1237 if (!WPACKET_init_static_len(&pkt
, *ppout
, SIZE_MAX
, 0))
1241 if (!encode_der_dsa_sig(&pkt
, sig
->r
, sig
->s
)
1242 || !WPACKET_get_total_written(&pkt
, &encoded_len
)
1243 || !WPACKET_finish(&pkt
)) {
1245 WPACKET_cleanup(&pkt
);
1249 if (ppout
!= NULL
) {
1250 if (*ppout
== NULL
) {
1251 *ppout
= (unsigned char *)buf
->data
;
1255 *ppout
+= encoded_len
;
1259 return (int)encoded_len
;
1262 void ECDSA_SIG_get0(const ECDSA_SIG
*sig
, const BIGNUM
**pr
, const BIGNUM
**ps
)
1270 const BIGNUM
*ECDSA_SIG_get0_r(const ECDSA_SIG
*sig
)
1275 const BIGNUM
*ECDSA_SIG_get0_s(const ECDSA_SIG
*sig
)
1280 int ECDSA_SIG_set0(ECDSA_SIG
*sig
, BIGNUM
*r
, BIGNUM
*s
)
1282 if (r
== NULL
|| s
== NULL
)
1284 BN_clear_free(sig
->r
);
1285 BN_clear_free(sig
->s
);
1291 int ECDSA_size(const EC_KEY
*ec
)
1295 const EC_GROUP
*group
;
1300 group
= EC_KEY_get0_group(ec
);
1304 bn
= EC_GROUP_get0_order(group
);
1308 sig
.r
= sig
.s
= (BIGNUM
*)bn
;
1309 ret
= i2d_ECDSA_SIG(&sig
, NULL
);