2 * Copyright 2002-2018 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
12 #include <openssl/err.h>
13 #include <openssl/asn1t.h>
14 #include <openssl/objects.h>
15 #include "internal/nelem.h"
16 #include "internal/asn1_dsa.h"
20 int EC_GROUP_get_basis_type(const EC_GROUP *group)
24 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
25 NID_X9_62_characteristic_two_field)
26 /* everything else is currently not supported */
29 /* Find the last non-zero element of group->poly[] */
31 i < (int)OSSL_NELEM(group->poly) && group->poly[i] != 0;
36 return NID_X9_62_ppBasis;
38 return NID_X9_62_tpBasis;
40 /* everything else is currently not supported */
44 #ifndef OPENSSL_NO_EC2M
45 int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
50 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
51 NID_X9_62_characteristic_two_field
52 || !((group->poly[0] != 0) && (group->poly[1] != 0)
53 && (group->poly[2] == 0))) {
54 ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS,
55 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
65 int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
66 unsigned int *k2, unsigned int *k3)
71 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
72 NID_X9_62_characteristic_two_field
73 || !((group->poly[0] != 0) && (group->poly[1] != 0)
74 && (group->poly[2] != 0) && (group->poly[3] != 0)
75 && (group->poly[4] == 0))) {
76 ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS,
77 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
92 /* some structures needed for the asn1 encoding */
93 typedef struct x9_62_pentanomial_st {
99 typedef struct x9_62_characteristic_two_st {
104 /* NID_X9_62_onBasis */
106 /* NID_X9_62_tpBasis */
107 ASN1_INTEGER *tpBasis;
108 /* NID_X9_62_ppBasis */
109 X9_62_PENTANOMIAL *ppBasis;
113 } X9_62_CHARACTERISTIC_TWO;
115 typedef struct x9_62_fieldid_st {
116 ASN1_OBJECT *fieldType;
119 /* NID_X9_62_prime_field */
121 /* NID_X9_62_characteristic_two_field */
122 X9_62_CHARACTERISTIC_TWO *char_two;
128 typedef struct x9_62_curve_st {
129 ASN1_OCTET_STRING *a;
130 ASN1_OCTET_STRING *b;
131 ASN1_BIT_STRING *seed;
134 struct ec_parameters_st {
136 X9_62_FIELDID *fieldID;
138 ASN1_OCTET_STRING *base;
140 ASN1_INTEGER *cofactor;
141 } /* ECPARAMETERS */ ;
143 struct ecpk_parameters_st {
146 ASN1_OBJECT *named_curve;
147 ECPARAMETERS *parameters;
148 ASN1_NULL *implicitlyCA;
150 } /* ECPKPARAMETERS */ ;
152 /* SEC1 ECPrivateKey */
153 typedef struct ec_privatekey_st {
155 ASN1_OCTET_STRING *privateKey;
156 ECPKPARAMETERS *parameters;
157 ASN1_BIT_STRING *publicKey;
160 /* the OpenSSL ASN.1 definitions */
161 ASN1_SEQUENCE(X9_62_PENTANOMIAL) = {
162 ASN1_EMBED(X9_62_PENTANOMIAL, k1, INT32),
163 ASN1_EMBED(X9_62_PENTANOMIAL, k2, INT32),
164 ASN1_EMBED(X9_62_PENTANOMIAL, k3, INT32)
165 } static_ASN1_SEQUENCE_END(X9_62_PENTANOMIAL)
167 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
168 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
170 ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY);
172 ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = {
173 ADB_ENTRY(NID_X9_62_onBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.onBasis, ASN1_NULL)),
174 ADB_ENTRY(NID_X9_62_tpBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.tpBasis, ASN1_INTEGER)),
175 ADB_ENTRY(NID_X9_62_ppBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.ppBasis, X9_62_PENTANOMIAL))
176 } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL);
178 ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = {
179 ASN1_EMBED(X9_62_CHARACTERISTIC_TWO, m, INT32),
180 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT),
181 ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO)
182 } static_ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO)
184 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
185 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
187 ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY);
189 ASN1_ADB(X9_62_FIELDID) = {
190 ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)),
191 ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO))
192 } ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL);
194 ASN1_SEQUENCE(X9_62_FIELDID) = {
195 ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
196 ASN1_ADB_OBJECT(X9_62_FIELDID)
197 } static_ASN1_SEQUENCE_END(X9_62_FIELDID)
199 ASN1_SEQUENCE(X9_62_CURVE) = {
200 ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING),
201 ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING),
202 ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING)
203 } static_ASN1_SEQUENCE_END(X9_62_CURVE)
205 ASN1_SEQUENCE(ECPARAMETERS) = {
206 ASN1_EMBED(ECPARAMETERS, version, INT32),
207 ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID),
208 ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE),
209 ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING),
210 ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
211 ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER)
212 } ASN1_SEQUENCE_END(ECPARAMETERS)
214 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
215 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
217 ASN1_CHOICE(ECPKPARAMETERS) = {
218 ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT),
219 ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
220 ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL)
221 } ASN1_CHOICE_END(ECPKPARAMETERS)
223 DECLARE_ASN1_FUNCTIONS(ECPKPARAMETERS)
224 DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECPKPARAMETERS, ECPKPARAMETERS)
225 IMPLEMENT_ASN1_FUNCTIONS(ECPKPARAMETERS)
227 ASN1_SEQUENCE(EC_PRIVATEKEY) = {
228 ASN1_EMBED(EC_PRIVATEKEY, version, INT32),
229 ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
230 ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
231 ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
232 } static_ASN1_SEQUENCE_END(EC_PRIVATEKEY)
234 DECLARE_ASN1_FUNCTIONS(EC_PRIVATEKEY)
235 DECLARE_ASN1_ENCODE_FUNCTIONS_name(EC_PRIVATEKEY, EC_PRIVATEKEY)
236 IMPLEMENT_ASN1_FUNCTIONS(EC_PRIVATEKEY)
238 /* some declarations of internal function */
240 /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
241 static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *);
242 /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
243 static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
245 /* the function definitions */
247 static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field)
252 if (group == NULL || field == NULL)
255 /* clear the old values (if necessary) */
256 ASN1_OBJECT_free(field->fieldType);
257 ASN1_TYPE_free(field->p.other);
259 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
260 /* set OID for the field */
261 if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
262 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
266 if (nid == NID_X9_62_prime_field) {
267 if ((tmp = BN_new()) == NULL) {
268 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
271 /* the parameters are specified by the prime number p */
272 if (!EC_GROUP_get_curve(group, tmp, NULL, NULL, NULL)) {
273 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
276 /* set the prime number */
277 field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
278 if (field->p.prime == NULL) {
279 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
282 } else if (nid == NID_X9_62_characteristic_two_field)
283 #ifdef OPENSSL_NO_EC2M
285 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED);
291 X9_62_CHARACTERISTIC_TWO *char_two;
293 field->p.char_two = X9_62_CHARACTERISTIC_TWO_new();
294 char_two = field->p.char_two;
296 if (char_two == NULL) {
297 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
301 char_two->m = (long)EC_GROUP_get_degree(group);
303 field_type = EC_GROUP_get_basis_type(group);
305 if (field_type == 0) {
306 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
309 /* set base type OID */
310 if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) {
311 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
315 if (field_type == NID_X9_62_tpBasis) {
318 if (!EC_GROUP_get_trinomial_basis(group, &k))
321 char_two->p.tpBasis = ASN1_INTEGER_new();
322 if (char_two->p.tpBasis == NULL) {
323 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
326 if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) {
327 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
330 } else if (field_type == NID_X9_62_ppBasis) {
331 unsigned int k1, k2, k3;
333 if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))
336 char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
337 if (char_two->p.ppBasis == NULL) {
338 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
343 char_two->p.ppBasis->k1 = (long)k1;
344 char_two->p.ppBasis->k2 = (long)k2;
345 char_two->p.ppBasis->k3 = (long)k3;
346 } else { /* field_type == NID_X9_62_onBasis */
348 /* for ONB the parameters are (asn1) NULL */
349 char_two->p.onBasis = ASN1_NULL_new();
350 if (char_two->p.onBasis == NULL) {
351 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
358 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_UNSUPPORTED_FIELD);
369 static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
372 BIGNUM *tmp_1 = NULL, *tmp_2 = NULL;
373 unsigned char *a_buf = NULL, *b_buf = NULL;
376 if (!group || !curve || !curve->a || !curve->b)
379 if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
380 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
385 if (!EC_GROUP_get_curve(group, NULL, tmp_1, tmp_2, NULL)) {
386 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
391 * Per SEC 1, the curve coefficients must be padded up to size. See C.2's
392 * definition of Curve, C.1's definition of FieldElement, and 2.3.5's
393 * definition of how to encode the field elements.
395 len = ((size_t)EC_GROUP_get_degree(group) + 7) / 8;
396 if ((a_buf = OPENSSL_malloc(len)) == NULL
397 || (b_buf = OPENSSL_malloc(len)) == NULL) {
398 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
401 if (BN_bn2binpad(tmp_1, a_buf, len) < 0
402 || BN_bn2binpad(tmp_2, b_buf, len) < 0) {
403 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
408 if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len)
409 || !ASN1_OCTET_STRING_set(curve->b, b_buf, len)) {
410 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
414 /* set the seed (optional) */
417 if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
418 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
421 curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
422 curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
423 if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
424 (int)group->seed_len)) {
425 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
429 ASN1_BIT_STRING_free(curve->seed);
443 ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group,
444 ECPARAMETERS *params)
447 ECPARAMETERS *ret = NULL;
449 unsigned char *buffer = NULL;
450 const EC_POINT *point = NULL;
451 point_conversion_form_t form;
453 if (params == NULL) {
454 if ((ret = ECPARAMETERS_new()) == NULL) {
455 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
461 /* set the version (always one) */
462 ret->version = (long)0x1;
464 /* set the fieldID */
465 if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
466 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
471 if (!ec_asn1_group2curve(group, ret->curve)) {
472 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
476 /* set the base point */
477 if ((point = EC_GROUP_get0_generator(group)) == NULL) {
478 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, EC_R_UNDEFINED_GENERATOR);
482 form = EC_GROUP_get_point_conversion_form(group);
484 len = EC_POINT_point2buf(group, point, form, &buffer, NULL);
486 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
489 if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
490 OPENSSL_free(buffer);
491 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
494 ASN1_STRING_set0(ret->base, buffer, len);
497 tmp = EC_GROUP_get0_order(group);
499 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
502 ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
503 if (ret->order == NULL) {
504 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
508 /* set the cofactor (optional) */
509 tmp = EC_GROUP_get0_cofactor(group);
511 ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
512 if (ret->cofactor == NULL) {
513 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
522 ECPARAMETERS_free(ret);
526 ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group,
527 ECPKPARAMETERS *params)
530 ECPKPARAMETERS *ret = params;
533 if ((ret = ECPKPARAMETERS_new()) == NULL) {
534 ECerr(EC_F_EC_GROUP_GET_ECPKPARAMETERS, ERR_R_MALLOC_FAILURE);
539 ASN1_OBJECT_free(ret->value.named_curve);
540 else if (ret->type == 1 && ret->value.parameters)
541 ECPARAMETERS_free(ret->value.parameters);
544 if (EC_GROUP_get_asn1_flag(group)) {
546 * use the asn1 OID to describe the elliptic curve parameters
548 tmp = EC_GROUP_get_curve_name(group);
551 if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
554 /* we don't know the nid => ERROR */
557 /* use the ECPARAMETERS structure */
559 if ((ret->value.parameters =
560 EC_GROUP_get_ecparameters(group, NULL)) == NULL)
565 ECPKPARAMETERS_free(ret);
571 EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params)
574 EC_GROUP *ret = NULL, *dup = NULL;
575 BIGNUM *p = NULL, *a = NULL, *b = NULL;
576 EC_POINT *point = NULL;
578 int curve_name = NID_undef;
581 if (!params->fieldID || !params->fieldID->fieldType ||
582 !params->fieldID->p.ptr) {
583 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
588 * Now extract the curve parameters a and b. Note that, although SEC 1
589 * specifies the length of their encodings, historical versions of OpenSSL
590 * encoded them incorrectly, so we must accept any length for backwards
593 if (!params->curve || !params->curve->a ||
594 !params->curve->a->data || !params->curve->b ||
595 !params->curve->b->data) {
596 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
599 a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
601 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
604 b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
606 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
610 /* get the field parameters */
611 tmp = OBJ_obj2nid(params->fieldID->fieldType);
612 if (tmp == NID_X9_62_characteristic_two_field)
613 #ifdef OPENSSL_NO_EC2M
615 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_GF2M_NOT_SUPPORTED);
620 X9_62_CHARACTERISTIC_TWO *char_two;
622 char_two = params->fieldID->p.char_two;
624 field_bits = char_two->m;
625 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
626 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
630 if ((p = BN_new()) == NULL) {
631 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
635 /* get the base type */
636 tmp = OBJ_obj2nid(char_two->type);
638 if (tmp == NID_X9_62_tpBasis) {
641 if (!char_two->p.tpBasis) {
642 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
646 tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
648 if (!(char_two->m > tmp_long && tmp_long > 0)) {
649 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
650 EC_R_INVALID_TRINOMIAL_BASIS);
654 /* create the polynomial */
655 if (!BN_set_bit(p, (int)char_two->m))
657 if (!BN_set_bit(p, (int)tmp_long))
659 if (!BN_set_bit(p, 0))
661 } else if (tmp == NID_X9_62_ppBasis) {
662 X9_62_PENTANOMIAL *penta;
664 penta = char_two->p.ppBasis;
666 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
671 (char_two->m > penta->k3 && penta->k3 > penta->k2
672 && penta->k2 > penta->k1 && penta->k1 > 0)) {
673 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
674 EC_R_INVALID_PENTANOMIAL_BASIS);
678 /* create the polynomial */
679 if (!BN_set_bit(p, (int)char_two->m))
681 if (!BN_set_bit(p, (int)penta->k1))
683 if (!BN_set_bit(p, (int)penta->k2))
685 if (!BN_set_bit(p, (int)penta->k3))
687 if (!BN_set_bit(p, 0))
689 } else if (tmp == NID_X9_62_onBasis) {
690 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_NOT_IMPLEMENTED);
694 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
698 /* create the EC_GROUP structure */
699 ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
702 else if (tmp == NID_X9_62_prime_field) {
703 /* we have a curve over a prime field */
704 /* extract the prime number */
705 if (!params->fieldID->p.prime) {
706 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
709 p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
711 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
715 if (BN_is_negative(p) || BN_is_zero(p)) {
716 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
720 field_bits = BN_num_bits(p);
721 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
722 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
726 /* create the EC_GROUP structure */
727 ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
729 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
734 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
738 /* extract seed (optional) */
739 if (params->curve->seed != NULL) {
740 OPENSSL_free(ret->seed);
741 if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {
742 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
745 memcpy(ret->seed, params->curve->seed->data,
746 params->curve->seed->length);
747 ret->seed_len = params->curve->seed->length;
750 if (!params->order || !params->base || !params->base->data) {
751 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
755 if ((point = EC_POINT_new(ret)) == NULL)
758 /* set the point conversion form */
759 EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
760 (params->base->data[0] & ~0x01));
762 /* extract the ec point */
763 if (!EC_POINT_oct2point(ret, point, params->base->data,
764 params->base->length, NULL)) {
765 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
769 /* extract the order */
770 if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
771 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
774 if (BN_is_negative(a) || BN_is_zero(a)) {
775 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
778 if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
779 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
783 /* extract the cofactor (optional) */
784 if (params->cofactor == NULL) {
787 } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
788 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
791 /* set the generator, order and cofactor (if present) */
792 if (!EC_GROUP_set_generator(ret, point, a, b)) {
793 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
798 * Check if the explicit parameters group just created matches one of the
801 * We create a copy of the group just built, so that we can remove optional
802 * fields for the lookup: we do this to avoid the possibility that one of
803 * the optional parameters is used to force the library into using a less
804 * performant and less secure EC_METHOD instead of the specialized one.
805 * In any case, `seed` is not really used in any computation, while a
806 * cofactor different from the one in the built-in table is just
807 * mathematically wrong anyway and should not be used.
809 if ((ctx = BN_CTX_new()) == NULL) {
810 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
813 if ((dup = EC_GROUP_dup(ret)) == NULL
814 || EC_GROUP_set_seed(dup, NULL, 0) != 1
815 || !EC_GROUP_set_generator(dup, point, a, NULL)) {
816 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
819 if ((curve_name = ec_curve_nid_from_params(dup, ctx)) != NID_undef) {
821 * The input explicit parameters successfully matched one of the
822 * built-in curves: often for built-in curves we have specialized
823 * methods with better performance and hardening.
825 * In this case we replace the `EC_GROUP` created through explicit
826 * parameters with one created from a named group.
828 EC_GROUP *named_group = NULL;
830 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
832 * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
833 * the same curve, we prefer the SECP nid when matching explicit
834 * parameters as that is associated with a specialized EC_METHOD.
836 if (curve_name == NID_wap_wsg_idm_ecid_wtls12)
837 curve_name = NID_secp224r1;
838 #endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
840 if ((named_group = EC_GROUP_new_by_curve_name(curve_name)) == NULL) {
841 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
848 * Set the flag so that EC_GROUPs created from explicit parameters are
849 * serialized using explicit parameters by default.
851 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE);
866 EC_POINT_free(point);
873 EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params)
875 EC_GROUP *ret = NULL;
878 if (params == NULL) {
879 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_MISSING_PARAMETERS);
883 if (params->type == 0) { /* the curve is given by an OID */
884 tmp = OBJ_obj2nid(params->value.named_curve);
885 if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
886 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS,
887 EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
890 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
891 } else if (params->type == 1) { /* the parameters are given by a
892 * ECPARAMETERS structure */
893 ret = EC_GROUP_new_from_ecparameters(params->value.parameters);
895 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, ERR_R_EC_LIB);
898 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE);
899 } else if (params->type == 2) { /* implicitlyCA */
902 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_ASN1_ERROR);
909 /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
911 EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
913 EC_GROUP *group = NULL;
914 ECPKPARAMETERS *params = NULL;
915 const unsigned char *p = *in;
917 if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) {
918 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
919 ECPKPARAMETERS_free(params);
923 if ((group = EC_GROUP_new_from_ecpkparameters(params)) == NULL) {
924 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
925 ECPKPARAMETERS_free(params);
934 ECPKPARAMETERS_free(params);
939 int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
942 ECPKPARAMETERS *tmp = EC_GROUP_get_ecpkparameters(a, NULL);
944 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
947 if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
948 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
949 ECPKPARAMETERS_free(tmp);
952 ECPKPARAMETERS_free(tmp);
956 /* some EC_KEY functions */
958 EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
961 EC_PRIVATEKEY *priv_key = NULL;
962 const unsigned char *p = *in;
964 if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) {
965 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
969 if (a == NULL || *a == NULL) {
970 if ((ret = EC_KEY_new()) == NULL) {
971 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
977 if (priv_key->parameters) {
978 EC_GROUP_free(ret->group);
979 ret->group = EC_GROUP_new_from_ecpkparameters(priv_key->parameters);
982 if (ret->group == NULL) {
983 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
987 ret->version = priv_key->version;
989 if (priv_key->privateKey) {
990 ASN1_OCTET_STRING *pkey = priv_key->privateKey;
991 if (EC_KEY_oct2priv(ret, ASN1_STRING_get0_data(pkey),
992 ASN1_STRING_length(pkey)) == 0)
995 ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
999 EC_POINT_clear_free(ret->pub_key);
1000 ret->pub_key = EC_POINT_new(ret->group);
1001 if (ret->pub_key == NULL) {
1002 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1006 if (priv_key->publicKey) {
1007 const unsigned char *pub_oct;
1010 pub_oct = ASN1_STRING_get0_data(priv_key->publicKey);
1011 pub_oct_len = ASN1_STRING_length(priv_key->publicKey);
1012 if (!EC_KEY_oct2key(ret, pub_oct, pub_oct_len, NULL)) {
1013 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1017 if (ret->group->meth->keygenpub == NULL
1018 || ret->group->meth->keygenpub(ret) == 0)
1020 /* Remember the original private-key-only encoding. */
1021 ret->enc_flag |= EC_PKEY_NO_PUBKEY;
1026 EC_PRIVATEKEY_free(priv_key);
1031 if (a == NULL || *a != ret)
1033 EC_PRIVATEKEY_free(priv_key);
1037 int i2d_ECPrivateKey(const EC_KEY *a, unsigned char **out)
1039 int ret = 0, ok = 0;
1040 unsigned char *priv= NULL, *pub= NULL;
1041 size_t privlen = 0, publen = 0;
1043 EC_PRIVATEKEY *priv_key = NULL;
1045 if (a == NULL || a->group == NULL ||
1046 (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
1047 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
1051 if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
1052 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1056 priv_key->version = a->version;
1058 privlen = EC_KEY_priv2buf(a, &priv);
1061 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1065 ASN1_STRING_set0(priv_key->privateKey, priv, privlen);
1068 if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
1069 if ((priv_key->parameters =
1070 EC_GROUP_get_ecpkparameters(a->group,
1071 priv_key->parameters)) == NULL) {
1072 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1077 if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
1078 priv_key->publicKey = ASN1_BIT_STRING_new();
1079 if (priv_key->publicKey == NULL) {
1080 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1084 publen = EC_KEY_key2buf(a, a->conv_form, &pub, NULL);
1087 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1091 priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
1092 priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
1093 ASN1_STRING_set0(priv_key->publicKey, pub, publen);
1097 if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
1098 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1103 OPENSSL_clear_free(priv, privlen);
1105 EC_PRIVATEKEY_free(priv_key);
1106 return (ok ? ret : 0);
1109 int i2d_ECParameters(const EC_KEY *a, unsigned char **out)
1112 ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1115 return i2d_ECPKParameters(a->group, out);
1118 EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
1122 if (in == NULL || *in == NULL) {
1123 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1127 if (a == NULL || *a == NULL) {
1128 if ((ret = EC_KEY_new()) == NULL) {
1129 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
1135 if (!d2i_ECPKParameters(&ret->group, in, len)) {
1136 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
1137 if (a == NULL || *a != ret)
1148 EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
1152 if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
1154 * sorry, but a EC_GROUP-structure is necessary to set the public key
1156 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1160 if (!EC_KEY_oct2key(ret, *in, len, NULL)) {
1161 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
1168 int i2o_ECPublicKey(const EC_KEY *a, unsigned char **out)
1174 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1178 buf_len = EC_POINT_point2oct(a->group, a->pub_key,
1179 a->conv_form, NULL, 0, NULL);
1181 if (out == NULL || buf_len == 0)
1182 /* out == NULL => just return the length of the octet string */
1186 if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
1187 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1192 if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
1193 *out, buf_len, NULL)) {
1194 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
1206 DECLARE_ASN1_FUNCTIONS(ECDSA_SIG)
1207 DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECDSA_SIG, ECDSA_SIG)
1209 #endif /* FIPS_MODE */
1211 ECDSA_SIG *ECDSA_SIG_new(void)
1213 ECDSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig));
1215 ECerr(EC_F_ECDSA_SIG_NEW, ERR_R_MALLOC_FAILURE);
1219 void ECDSA_SIG_free(ECDSA_SIG *sig)
1223 BN_clear_free(sig->r);
1224 BN_clear_free(sig->s);
1228 ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **psig, const unsigned char **ppin, long len)
1234 if (psig != NULL && *psig != NULL) {
1237 sig = ECDSA_SIG_new();
1245 if (decode_der_dsa_sig(sig->r, sig->s, ppin, (size_t)len) == 0) {
1246 if (psig == NULL || *psig == NULL)
1247 ECDSA_SIG_free(sig);
1250 if (psig != NULL && *psig == NULL)
1255 int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **ppout)
1257 BUF_MEM *buf = NULL;
1261 if (ppout == NULL) {
1262 if (!WPACKET_init_null(&pkt, 0))
1264 } else if (*ppout == NULL) {
1265 if ((buf = BUF_MEM_new()) == NULL
1266 || !WPACKET_init_len(&pkt, buf, 0)) {
1271 if (!WPACKET_init_static_len(&pkt, *ppout, SIZE_MAX, 0))
1275 if (!encode_der_dsa_sig(&pkt, sig->r, sig->s)
1276 || !WPACKET_get_total_written(&pkt, &encoded_len)
1277 || !WPACKET_finish(&pkt)) {
1279 WPACKET_cleanup(&pkt);
1283 if (ppout != NULL) {
1284 if (*ppout == NULL) {
1285 *ppout = (unsigned char *)buf->data;
1289 *ppout += encoded_len;
1293 return (int)encoded_len;
1296 void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
1304 const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig)
1309 const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig)
1314 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
1316 if (r == NULL || s == NULL)
1318 BN_clear_free(sig->r);
1319 BN_clear_free(sig->s);
1326 int ECDSA_size(const EC_KEY *r)
1330 unsigned char buf[4];
1331 const EC_GROUP *group;
1335 group = EC_KEY_get0_group(r);
1339 i = EC_GROUP_order_bits(group);
1342 bs.length = (i + 7) / 8;
1344 bs.type = V_ASN1_INTEGER;
1345 /* If the top bit is set the asn1 encoding is 1 larger. */
1348 i = i2d_ASN1_INTEGER(&bs, NULL);
1349 i += i; /* r and s */
1350 ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);