2 * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <openssl/err.h>
13 #include <openssl/asn1t.h>
14 #include <openssl/objects.h>
16 int EC_GROUP_get_basis_type(const EC_GROUP
*group
)
20 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group
)) !=
21 NID_X9_62_characteristic_two_field
)
22 /* everything else is currently not supported */
25 while (group
->poly
[i
] != 0)
29 return NID_X9_62_ppBasis
;
31 return NID_X9_62_tpBasis
;
33 /* everything else is currently not supported */
37 #ifndef OPENSSL_NO_EC2M
38 int EC_GROUP_get_trinomial_basis(const EC_GROUP
*group
, unsigned int *k
)
43 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group
)) !=
44 NID_X9_62_characteristic_two_field
45 || !((group
->poly
[0] != 0) && (group
->poly
[1] != 0)
46 && (group
->poly
[2] == 0))) {
47 ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS
,
48 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
58 int EC_GROUP_get_pentanomial_basis(const EC_GROUP
*group
, unsigned int *k1
,
59 unsigned int *k2
, unsigned int *k3
)
64 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group
)) !=
65 NID_X9_62_characteristic_two_field
66 || !((group
->poly
[0] != 0) && (group
->poly
[1] != 0)
67 && (group
->poly
[2] != 0) && (group
->poly
[3] != 0)
68 && (group
->poly
[4] == 0))) {
69 ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS
,
70 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
85 /* some structures needed for the asn1 encoding */
86 typedef struct x9_62_pentanomial_st
{
92 typedef struct x9_62_characteristic_two_st
{
97 /* NID_X9_62_onBasis */
99 /* NID_X9_62_tpBasis */
100 ASN1_INTEGER
*tpBasis
;
101 /* NID_X9_62_ppBasis */
102 X9_62_PENTANOMIAL
*ppBasis
;
106 } X9_62_CHARACTERISTIC_TWO
;
108 typedef struct x9_62_fieldid_st
{
109 ASN1_OBJECT
*fieldType
;
112 /* NID_X9_62_prime_field */
114 /* NID_X9_62_characteristic_two_field */
115 X9_62_CHARACTERISTIC_TWO
*char_two
;
121 typedef struct x9_62_curve_st
{
122 ASN1_OCTET_STRING
*a
;
123 ASN1_OCTET_STRING
*b
;
124 ASN1_BIT_STRING
*seed
;
127 struct ec_parameters_st
{
129 X9_62_FIELDID
*fieldID
;
131 ASN1_OCTET_STRING
*base
;
133 ASN1_INTEGER
*cofactor
;
134 } /* ECPARAMETERS */ ;
136 struct ecpk_parameters_st
{
139 ASN1_OBJECT
*named_curve
;
140 ECPARAMETERS
*parameters
;
141 ASN1_NULL
*implicitlyCA
;
143 } /* ECPKPARAMETERS */ ;
145 /* SEC1 ECPrivateKey */
146 typedef struct ec_privatekey_st
{
148 ASN1_OCTET_STRING
*privateKey
;
149 ECPKPARAMETERS
*parameters
;
150 ASN1_BIT_STRING
*publicKey
;
153 /* the OpenSSL ASN.1 definitions */
154 ASN1_SEQUENCE(X9_62_PENTANOMIAL
) = {
155 ASN1_SIMPLE(X9_62_PENTANOMIAL
, k1
, LONG
),
156 ASN1_SIMPLE(X9_62_PENTANOMIAL
, k2
, LONG
),
157 ASN1_SIMPLE(X9_62_PENTANOMIAL
, k3
, LONG
)
158 } static_ASN1_SEQUENCE_END(X9_62_PENTANOMIAL
)
160 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL
)
161 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL
)
163 ASN1_ADB_TEMPLATE(char_two_def
) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, p
.other
, ASN1_ANY
);
165 ASN1_ADB(X9_62_CHARACTERISTIC_TWO
) = {
166 ADB_ENTRY(NID_X9_62_onBasis
, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, p
.onBasis
, ASN1_NULL
)),
167 ADB_ENTRY(NID_X9_62_tpBasis
, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, p
.tpBasis
, ASN1_INTEGER
)),
168 ADB_ENTRY(NID_X9_62_ppBasis
, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, p
.ppBasis
, X9_62_PENTANOMIAL
))
169 } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO
, 0, type
, 0, &char_two_def_tt
, NULL
);
171 ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO
) = {
172 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, m
, LONG
),
173 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, type
, ASN1_OBJECT
),
174 ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO
)
175 } static_ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO
)
177 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO
)
178 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO
)
180 ASN1_ADB_TEMPLATE(fieldID_def
) = ASN1_SIMPLE(X9_62_FIELDID
, p
.other
, ASN1_ANY
);
182 ASN1_ADB(X9_62_FIELDID
) = {
183 ADB_ENTRY(NID_X9_62_prime_field
, ASN1_SIMPLE(X9_62_FIELDID
, p
.prime
, ASN1_INTEGER
)),
184 ADB_ENTRY(NID_X9_62_characteristic_two_field
, ASN1_SIMPLE(X9_62_FIELDID
, p
.char_two
, X9_62_CHARACTERISTIC_TWO
))
185 } ASN1_ADB_END(X9_62_FIELDID
, 0, fieldType
, 0, &fieldID_def_tt
, NULL
);
187 ASN1_SEQUENCE(X9_62_FIELDID
) = {
188 ASN1_SIMPLE(X9_62_FIELDID
, fieldType
, ASN1_OBJECT
),
189 ASN1_ADB_OBJECT(X9_62_FIELDID
)
190 } static_ASN1_SEQUENCE_END(X9_62_FIELDID
)
192 ASN1_SEQUENCE(X9_62_CURVE
) = {
193 ASN1_SIMPLE(X9_62_CURVE
, a
, ASN1_OCTET_STRING
),
194 ASN1_SIMPLE(X9_62_CURVE
, b
, ASN1_OCTET_STRING
),
195 ASN1_OPT(X9_62_CURVE
, seed
, ASN1_BIT_STRING
)
196 } static_ASN1_SEQUENCE_END(X9_62_CURVE
)
198 ASN1_SEQUENCE(ECPARAMETERS
) = {
199 ASN1_SIMPLE(ECPARAMETERS
, version
, LONG
),
200 ASN1_SIMPLE(ECPARAMETERS
, fieldID
, X9_62_FIELDID
),
201 ASN1_SIMPLE(ECPARAMETERS
, curve
, X9_62_CURVE
),
202 ASN1_SIMPLE(ECPARAMETERS
, base
, ASN1_OCTET_STRING
),
203 ASN1_SIMPLE(ECPARAMETERS
, order
, ASN1_INTEGER
),
204 ASN1_OPT(ECPARAMETERS
, cofactor
, ASN1_INTEGER
)
205 } ASN1_SEQUENCE_END(ECPARAMETERS
)
207 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS
)
208 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS
)
210 ASN1_CHOICE(ECPKPARAMETERS
) = {
211 ASN1_SIMPLE(ECPKPARAMETERS
, value
.named_curve
, ASN1_OBJECT
),
212 ASN1_SIMPLE(ECPKPARAMETERS
, value
.parameters
, ECPARAMETERS
),
213 ASN1_SIMPLE(ECPKPARAMETERS
, value
.implicitlyCA
, ASN1_NULL
)
214 } ASN1_CHOICE_END(ECPKPARAMETERS
)
216 DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS
)
217 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPKPARAMETERS
, ECPKPARAMETERS
)
218 IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS
)
220 ASN1_SEQUENCE(EC_PRIVATEKEY
) = {
221 ASN1_SIMPLE(EC_PRIVATEKEY
, version
, LONG
),
222 ASN1_SIMPLE(EC_PRIVATEKEY
, privateKey
, ASN1_OCTET_STRING
),
223 ASN1_EXP_OPT(EC_PRIVATEKEY
, parameters
, ECPKPARAMETERS
, 0),
224 ASN1_EXP_OPT(EC_PRIVATEKEY
, publicKey
, ASN1_BIT_STRING
, 1)
225 } static_ASN1_SEQUENCE_END(EC_PRIVATEKEY
)
227 DECLARE_ASN1_FUNCTIONS_const(EC_PRIVATEKEY
)
228 DECLARE_ASN1_ENCODE_FUNCTIONS_const(EC_PRIVATEKEY
, EC_PRIVATEKEY
)
229 IMPLEMENT_ASN1_FUNCTIONS_const(EC_PRIVATEKEY
)
231 /* some declarations of internal function */
233 /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
234 static int ec_asn1_group2fieldid(const EC_GROUP
*, X9_62_FIELDID
*);
235 /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
236 static int ec_asn1_group2curve(const EC_GROUP
*, X9_62_CURVE
*);
238 /* the function definitions */
240 static int ec_asn1_group2fieldid(const EC_GROUP
*group
, X9_62_FIELDID
*field
)
245 if (group
== NULL
|| field
== NULL
)
248 /* clear the old values (if necessary) */
249 ASN1_OBJECT_free(field
->fieldType
);
250 ASN1_TYPE_free(field
->p
.other
);
252 nid
= EC_METHOD_get_field_type(EC_GROUP_method_of(group
));
253 /* set OID for the field */
254 if ((field
->fieldType
= OBJ_nid2obj(nid
)) == NULL
) {
255 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_OBJ_LIB
);
259 if (nid
== NID_X9_62_prime_field
) {
260 if ((tmp
= BN_new()) == NULL
) {
261 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_MALLOC_FAILURE
);
264 /* the parameters are specified by the prime number p */
265 if (!EC_GROUP_get_curve_GFp(group
, tmp
, NULL
, NULL
, NULL
)) {
266 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_EC_LIB
);
269 /* set the prime number */
270 field
->p
.prime
= BN_to_ASN1_INTEGER(tmp
, NULL
);
271 if (field
->p
.prime
== NULL
) {
272 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_ASN1_LIB
);
275 } else if (nid
== NID_X9_62_characteristic_two_field
)
276 #ifdef OPENSSL_NO_EC2M
278 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, EC_R_GF2M_NOT_SUPPORTED
);
284 X9_62_CHARACTERISTIC_TWO
*char_two
;
286 field
->p
.char_two
= X9_62_CHARACTERISTIC_TWO_new();
287 char_two
= field
->p
.char_two
;
289 if (char_two
== NULL
) {
290 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_MALLOC_FAILURE
);
294 char_two
->m
= (long)EC_GROUP_get_degree(group
);
296 field_type
= EC_GROUP_get_basis_type(group
);
298 if (field_type
== 0) {
299 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_EC_LIB
);
302 /* set base type OID */
303 if ((char_two
->type
= OBJ_nid2obj(field_type
)) == NULL
) {
304 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_OBJ_LIB
);
308 if (field_type
== NID_X9_62_tpBasis
) {
311 if (!EC_GROUP_get_trinomial_basis(group
, &k
))
314 char_two
->p
.tpBasis
= ASN1_INTEGER_new();
315 if (char_two
->p
.tpBasis
== NULL
) {
316 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_MALLOC_FAILURE
);
319 if (!ASN1_INTEGER_set(char_two
->p
.tpBasis
, (long)k
)) {
320 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_ASN1_LIB
);
323 } else if (field_type
== NID_X9_62_ppBasis
) {
324 unsigned int k1
, k2
, k3
;
326 if (!EC_GROUP_get_pentanomial_basis(group
, &k1
, &k2
, &k3
))
329 char_two
->p
.ppBasis
= X9_62_PENTANOMIAL_new();
330 if (char_two
->p
.ppBasis
== NULL
) {
331 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_MALLOC_FAILURE
);
336 char_two
->p
.ppBasis
->k1
= (long)k1
;
337 char_two
->p
.ppBasis
->k2
= (long)k2
;
338 char_two
->p
.ppBasis
->k3
= (long)k3
;
339 } else { /* field_type == NID_X9_62_onBasis */
341 /* for ONB the parameters are (asn1) NULL */
342 char_two
->p
.onBasis
= ASN1_NULL_new();
343 if (char_two
->p
.onBasis
== NULL
) {
344 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_MALLOC_FAILURE
);
351 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, EC_R_UNSUPPORTED_FIELD
);
362 static int ec_asn1_group2curve(const EC_GROUP
*group
, X9_62_CURVE
*curve
)
365 BIGNUM
*tmp_1
= NULL
, *tmp_2
= NULL
;
366 unsigned char *buffer_1
= NULL
, *buffer_2
= NULL
,
367 *a_buf
= NULL
, *b_buf
= NULL
;
369 unsigned char char_zero
= 0;
371 if (!group
|| !curve
|| !curve
->a
|| !curve
->b
)
374 if ((tmp_1
= BN_new()) == NULL
|| (tmp_2
= BN_new()) == NULL
) {
375 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_MALLOC_FAILURE
);
379 nid
= EC_METHOD_get_field_type(EC_GROUP_method_of(group
));
382 if (nid
== NID_X9_62_prime_field
) {
383 if (!EC_GROUP_get_curve_GFp(group
, NULL
, tmp_1
, tmp_2
, NULL
)) {
384 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_EC_LIB
);
388 #ifndef OPENSSL_NO_EC2M
389 else { /* nid == NID_X9_62_characteristic_two_field */
391 if (!EC_GROUP_get_curve_GF2m(group
, NULL
, tmp_1
, tmp_2
, NULL
)) {
392 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_EC_LIB
);
397 len_1
= (size_t)BN_num_bytes(tmp_1
);
398 len_2
= (size_t)BN_num_bytes(tmp_2
);
401 /* len_1 == 0 => a == 0 */
405 if ((buffer_1
= OPENSSL_malloc(len_1
)) == NULL
) {
406 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_MALLOC_FAILURE
);
409 if ((len_1
= BN_bn2bin(tmp_1
, buffer_1
)) == 0) {
410 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_BN_LIB
);
417 /* len_2 == 0 => b == 0 */
421 if ((buffer_2
= OPENSSL_malloc(len_2
)) == NULL
) {
422 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_MALLOC_FAILURE
);
425 if ((len_2
= BN_bn2bin(tmp_2
, buffer_2
)) == 0) {
426 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_BN_LIB
);
433 if (!ASN1_OCTET_STRING_set(curve
->a
, a_buf
, len_1
) ||
434 !ASN1_OCTET_STRING_set(curve
->b
, b_buf
, len_2
)) {
435 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_ASN1_LIB
);
439 /* set the seed (optional) */
442 if ((curve
->seed
= ASN1_BIT_STRING_new()) == NULL
) {
443 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_MALLOC_FAILURE
);
446 curve
->seed
->flags
&= ~(ASN1_STRING_FLAG_BITS_LEFT
| 0x07);
447 curve
->seed
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
448 if (!ASN1_BIT_STRING_set(curve
->seed
, group
->seed
,
449 (int)group
->seed_len
)) {
450 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_ASN1_LIB
);
454 ASN1_BIT_STRING_free(curve
->seed
);
461 OPENSSL_free(buffer_1
);
462 OPENSSL_free(buffer_2
);
468 ECPARAMETERS
*EC_GROUP_get_ecparameters(const EC_GROUP
*group
,
469 ECPARAMETERS
*params
)
472 ECPARAMETERS
*ret
= NULL
;
474 unsigned char *buffer
= NULL
;
475 const EC_POINT
*point
= NULL
;
476 point_conversion_form_t form
;
478 if (params
== NULL
) {
479 if ((ret
= ECPARAMETERS_new()) == NULL
) {
480 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS
, ERR_R_MALLOC_FAILURE
);
486 /* set the version (always one) */
487 ret
->version
= (long)0x1;
489 /* set the fieldID */
490 if (!ec_asn1_group2fieldid(group
, ret
->fieldID
)) {
491 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS
, ERR_R_EC_LIB
);
496 if (!ec_asn1_group2curve(group
, ret
->curve
)) {
497 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS
, ERR_R_EC_LIB
);
501 /* set the base point */
502 if ((point
= EC_GROUP_get0_generator(group
)) == NULL
) {
503 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS
, EC_R_UNDEFINED_GENERATOR
);
507 form
= EC_GROUP_get_point_conversion_form(group
);
509 len
= EC_POINT_point2buf(group
, point
, form
, &buffer
, NULL
);
511 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS
, ERR_R_EC_LIB
);
514 if (ret
->base
== NULL
&& (ret
->base
= ASN1_OCTET_STRING_new()) == NULL
) {
515 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS
, ERR_R_MALLOC_FAILURE
);
518 if (!ASN1_OCTET_STRING_set(ret
->base
, buffer
, len
)) {
519 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS
, ERR_R_ASN1_LIB
);
524 tmp
= EC_GROUP_get0_order(group
);
526 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS
, ERR_R_EC_LIB
);
529 ret
->order
= BN_to_ASN1_INTEGER(tmp
, ret
->order
);
530 if (ret
->order
== NULL
) {
531 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS
, ERR_R_ASN1_LIB
);
535 /* set the cofactor (optional) */
536 tmp
= EC_GROUP_get0_cofactor(group
);
538 ret
->cofactor
= BN_to_ASN1_INTEGER(tmp
, ret
->cofactor
);
539 if (ret
->cofactor
== NULL
) {
540 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS
, ERR_R_ASN1_LIB
);
549 ECPARAMETERS_free(ret
);
550 OPENSSL_free(buffer
);
554 ECPKPARAMETERS
*EC_GROUP_get_ecpkparameters(const EC_GROUP
*group
,
555 ECPKPARAMETERS
*params
)
558 ECPKPARAMETERS
*ret
= params
;
561 if ((ret
= ECPKPARAMETERS_new()) == NULL
) {
562 ECerr(EC_F_EC_GROUP_GET_ECPKPARAMETERS
, ERR_R_MALLOC_FAILURE
);
567 ASN1_OBJECT_free(ret
->value
.named_curve
);
568 else if (ret
->type
== 1 && ret
->value
.parameters
)
569 ECPARAMETERS_free(ret
->value
.parameters
);
572 if (EC_GROUP_get_asn1_flag(group
)) {
574 * use the asn1 OID to describe the the elliptic curve parameters
576 tmp
= EC_GROUP_get_curve_name(group
);
579 if ((ret
->value
.named_curve
= OBJ_nid2obj(tmp
)) == NULL
)
582 /* we don't know the nid => ERROR */
585 /* use the ECPARAMETERS structure */
587 if ((ret
->value
.parameters
=
588 EC_GROUP_get_ecparameters(group
, NULL
)) == NULL
)
593 ECPKPARAMETERS_free(ret
);
599 EC_GROUP
*EC_GROUP_new_from_ecparameters(const ECPARAMETERS
*params
)
602 EC_GROUP
*ret
= NULL
;
603 BIGNUM
*p
= NULL
, *a
= NULL
, *b
= NULL
;
604 EC_POINT
*point
= NULL
;
607 if (!params
->fieldID
|| !params
->fieldID
->fieldType
||
608 !params
->fieldID
->p
.ptr
) {
609 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_ASN1_ERROR
);
613 /* now extract the curve parameters a and b */
614 if (!params
->curve
|| !params
->curve
->a
||
615 !params
->curve
->a
->data
|| !params
->curve
->b
||
616 !params
->curve
->b
->data
) {
617 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_ASN1_ERROR
);
620 a
= BN_bin2bn(params
->curve
->a
->data
, params
->curve
->a
->length
, NULL
);
622 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_BN_LIB
);
625 b
= BN_bin2bn(params
->curve
->b
->data
, params
->curve
->b
->length
, NULL
);
627 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_BN_LIB
);
631 /* get the field parameters */
632 tmp
= OBJ_obj2nid(params
->fieldID
->fieldType
);
633 if (tmp
== NID_X9_62_characteristic_two_field
)
634 #ifdef OPENSSL_NO_EC2M
636 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_GF2M_NOT_SUPPORTED
);
641 X9_62_CHARACTERISTIC_TWO
*char_two
;
643 char_two
= params
->fieldID
->p
.char_two
;
645 field_bits
= char_two
->m
;
646 if (field_bits
> OPENSSL_ECC_MAX_FIELD_BITS
) {
647 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_FIELD_TOO_LARGE
);
651 if ((p
= BN_new()) == NULL
) {
652 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_MALLOC_FAILURE
);
656 /* get the base type */
657 tmp
= OBJ_obj2nid(char_two
->type
);
659 if (tmp
== NID_X9_62_tpBasis
) {
662 if (!char_two
->p
.tpBasis
) {
663 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_ASN1_ERROR
);
667 tmp_long
= ASN1_INTEGER_get(char_two
->p
.tpBasis
);
669 if (!(char_two
->m
> tmp_long
&& tmp_long
> 0)) {
670 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
,
671 EC_R_INVALID_TRINOMIAL_BASIS
);
675 /* create the polynomial */
676 if (!BN_set_bit(p
, (int)char_two
->m
))
678 if (!BN_set_bit(p
, (int)tmp_long
))
680 if (!BN_set_bit(p
, 0))
682 } else if (tmp
== NID_X9_62_ppBasis
) {
683 X9_62_PENTANOMIAL
*penta
;
685 penta
= char_two
->p
.ppBasis
;
687 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_ASN1_ERROR
);
692 (char_two
->m
> penta
->k3
&& penta
->k3
> penta
->k2
693 && penta
->k2
> penta
->k1
&& penta
->k1
> 0)) {
694 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
,
695 EC_R_INVALID_PENTANOMIAL_BASIS
);
699 /* create the polynomial */
700 if (!BN_set_bit(p
, (int)char_two
->m
))
702 if (!BN_set_bit(p
, (int)penta
->k1
))
704 if (!BN_set_bit(p
, (int)penta
->k2
))
706 if (!BN_set_bit(p
, (int)penta
->k3
))
708 if (!BN_set_bit(p
, 0))
710 } else if (tmp
== NID_X9_62_onBasis
) {
711 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_NOT_IMPLEMENTED
);
715 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_ASN1_ERROR
);
719 /* create the EC_GROUP structure */
720 ret
= EC_GROUP_new_curve_GF2m(p
, a
, b
, NULL
);
723 else if (tmp
== NID_X9_62_prime_field
) {
724 /* we have a curve over a prime field */
725 /* extract the prime number */
726 if (!params
->fieldID
->p
.prime
) {
727 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_ASN1_ERROR
);
730 p
= ASN1_INTEGER_to_BN(params
->fieldID
->p
.prime
, NULL
);
732 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_ASN1_LIB
);
736 if (BN_is_negative(p
) || BN_is_zero(p
)) {
737 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_INVALID_FIELD
);
741 field_bits
= BN_num_bits(p
);
742 if (field_bits
> OPENSSL_ECC_MAX_FIELD_BITS
) {
743 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_FIELD_TOO_LARGE
);
747 /* create the EC_GROUP structure */
748 ret
= EC_GROUP_new_curve_GFp(p
, a
, b
, NULL
);
750 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_INVALID_FIELD
);
755 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_EC_LIB
);
759 /* extract seed (optional) */
760 if (params
->curve
->seed
!= NULL
) {
761 OPENSSL_free(ret
->seed
);
762 if ((ret
->seed
= OPENSSL_malloc(params
->curve
->seed
->length
)) == NULL
) {
763 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_MALLOC_FAILURE
);
766 memcpy(ret
->seed
, params
->curve
->seed
->data
,
767 params
->curve
->seed
->length
);
768 ret
->seed_len
= params
->curve
->seed
->length
;
771 if (!params
->order
|| !params
->base
|| !params
->base
->data
) {
772 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_ASN1_ERROR
);
776 if ((point
= EC_POINT_new(ret
)) == NULL
)
779 /* set the point conversion form */
780 EC_GROUP_set_point_conversion_form(ret
, (point_conversion_form_t
)
781 (params
->base
->data
[0] & ~0x01));
783 /* extract the ec point */
784 if (!EC_POINT_oct2point(ret
, point
, params
->base
->data
,
785 params
->base
->length
, NULL
)) {
786 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_EC_LIB
);
790 /* extract the order */
791 if ((a
= ASN1_INTEGER_to_BN(params
->order
, a
)) == NULL
) {
792 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_ASN1_LIB
);
795 if (BN_is_negative(a
) || BN_is_zero(a
)) {
796 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_INVALID_GROUP_ORDER
);
799 if (BN_num_bits(a
) > (int)field_bits
+ 1) { /* Hasse bound */
800 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, EC_R_INVALID_GROUP_ORDER
);
804 /* extract the cofactor (optional) */
805 if (params
->cofactor
== NULL
) {
808 } else if ((b
= ASN1_INTEGER_to_BN(params
->cofactor
, b
)) == NULL
) {
809 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_ASN1_LIB
);
812 /* set the generator, order and cofactor (if present) */
813 if (!EC_GROUP_set_generator(ret
, point
, a
, b
)) {
814 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS
, ERR_R_EC_LIB
);
822 EC_GROUP_clear_free(ret
);
829 EC_POINT_free(point
);
833 EC_GROUP
*EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS
*params
)
835 EC_GROUP
*ret
= NULL
;
838 if (params
== NULL
) {
839 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS
, EC_R_MISSING_PARAMETERS
);
843 if (params
->type
== 0) { /* the curve is given by an OID */
844 tmp
= OBJ_obj2nid(params
->value
.named_curve
);
845 if ((ret
= EC_GROUP_new_by_curve_name(tmp
)) == NULL
) {
846 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS
,
847 EC_R_EC_GROUP_NEW_BY_NAME_FAILURE
);
850 EC_GROUP_set_asn1_flag(ret
, OPENSSL_EC_NAMED_CURVE
);
851 } else if (params
->type
== 1) { /* the parameters are given by a
852 * ECPARAMETERS structure */
853 ret
= EC_GROUP_new_from_ecparameters(params
->value
.parameters
);
855 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS
, ERR_R_EC_LIB
);
858 EC_GROUP_set_asn1_flag(ret
, 0x0);
859 } else if (params
->type
== 2) { /* implicitlyCA */
862 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS
, EC_R_ASN1_ERROR
);
869 /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
871 EC_GROUP
*d2i_ECPKParameters(EC_GROUP
**a
, const unsigned char **in
, long len
)
873 EC_GROUP
*group
= NULL
;
874 ECPKPARAMETERS
*params
= NULL
;
875 const unsigned char *p
= *in
;
877 if ((params
= d2i_ECPKPARAMETERS(NULL
, &p
, len
)) == NULL
) {
878 ECerr(EC_F_D2I_ECPKPARAMETERS
, EC_R_D2I_ECPKPARAMETERS_FAILURE
);
879 ECPKPARAMETERS_free(params
);
883 if ((group
= EC_GROUP_new_from_ecpkparameters(params
)) == NULL
) {
884 ECerr(EC_F_D2I_ECPKPARAMETERS
, EC_R_PKPARAMETERS2GROUP_FAILURE
);
885 ECPKPARAMETERS_free(params
);
890 EC_GROUP_clear_free(*a
);
894 ECPKPARAMETERS_free(params
);
899 int i2d_ECPKParameters(const EC_GROUP
*a
, unsigned char **out
)
902 ECPKPARAMETERS
*tmp
= EC_GROUP_get_ecpkparameters(a
, NULL
);
904 ECerr(EC_F_I2D_ECPKPARAMETERS
, EC_R_GROUP2PKPARAMETERS_FAILURE
);
907 if ((ret
= i2d_ECPKPARAMETERS(tmp
, out
)) == 0) {
908 ECerr(EC_F_I2D_ECPKPARAMETERS
, EC_R_I2D_ECPKPARAMETERS_FAILURE
);
909 ECPKPARAMETERS_free(tmp
);
912 ECPKPARAMETERS_free(tmp
);
916 /* some EC_KEY functions */
918 EC_KEY
*d2i_ECPrivateKey(EC_KEY
**a
, const unsigned char **in
, long len
)
921 EC_PRIVATEKEY
*priv_key
= NULL
;
922 const unsigned char *p
= *in
;
924 if ((priv_key
= d2i_EC_PRIVATEKEY(NULL
, &p
, len
)) == NULL
) {
925 ECerr(EC_F_D2I_ECPRIVATEKEY
, ERR_R_EC_LIB
);
929 if (a
== NULL
|| *a
== NULL
) {
930 if ((ret
= EC_KEY_new()) == NULL
) {
931 ECerr(EC_F_D2I_ECPRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
937 if (priv_key
->parameters
) {
938 EC_GROUP_clear_free(ret
->group
);
939 ret
->group
= EC_GROUP_new_from_ecpkparameters(priv_key
->parameters
);
942 if (ret
->group
== NULL
) {
943 ECerr(EC_F_D2I_ECPRIVATEKEY
, ERR_R_EC_LIB
);
947 ret
->version
= priv_key
->version
;
949 if (priv_key
->privateKey
) {
950 ASN1_OCTET_STRING
*pkey
= priv_key
->privateKey
;
951 if (EC_KEY_oct2priv(ret
, ASN1_STRING_data(pkey
),
952 ASN1_STRING_length(pkey
)) == 0)
955 ECerr(EC_F_D2I_ECPRIVATEKEY
, EC_R_MISSING_PRIVATE_KEY
);
959 EC_POINT_clear_free(ret
->pub_key
);
960 ret
->pub_key
= EC_POINT_new(ret
->group
);
961 if (ret
->pub_key
== NULL
) {
962 ECerr(EC_F_D2I_ECPRIVATEKEY
, ERR_R_EC_LIB
);
966 if (priv_key
->publicKey
) {
967 const unsigned char *pub_oct
;
970 pub_oct
= ASN1_STRING_data(priv_key
->publicKey
);
971 pub_oct_len
= ASN1_STRING_length(priv_key
->publicKey
);
972 if (!EC_KEY_oct2key(ret
, pub_oct
, pub_oct_len
, NULL
)) {
973 ECerr(EC_F_D2I_ECPRIVATEKEY
, ERR_R_EC_LIB
);
977 if (ret
->group
->meth
->keygenpub
== NULL
978 || ret
->group
->meth
->keygenpub(ret
) == 0)
980 /* Remember the original private-key-only encoding. */
981 ret
->enc_flag
|= EC_PKEY_NO_PUBKEY
;
986 EC_PRIVATEKEY_free(priv_key
);
991 if (a
== NULL
|| *a
!= ret
)
993 EC_PRIVATEKEY_free(priv_key
);
997 int i2d_ECPrivateKey(EC_KEY
*a
, unsigned char **out
)
1000 unsigned char *priv
= NULL
, *pub
= NULL
;
1001 size_t privlen
= 0, publen
= 0;
1003 EC_PRIVATEKEY
*priv_key
= NULL
;
1005 if (a
== NULL
|| a
->group
== NULL
||
1006 (!(a
->enc_flag
& EC_PKEY_NO_PUBKEY
) && a
->pub_key
== NULL
)) {
1007 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
1011 if ((priv_key
= EC_PRIVATEKEY_new()) == NULL
) {
1012 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
1016 priv_key
->version
= a
->version
;
1018 privlen
= EC_KEY_priv2buf(a
, &priv
);
1021 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_EC_LIB
);
1025 ASN1_STRING_set0(priv_key
->privateKey
, priv
, privlen
);
1028 if (!(a
->enc_flag
& EC_PKEY_NO_PARAMETERS
)) {
1029 if ((priv_key
->parameters
=
1030 EC_GROUP_get_ecpkparameters(a
->group
,
1031 priv_key
->parameters
)) == NULL
) {
1032 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_EC_LIB
);
1037 if (!(a
->enc_flag
& EC_PKEY_NO_PUBKEY
)) {
1038 priv_key
->publicKey
= ASN1_BIT_STRING_new();
1039 if (priv_key
->publicKey
== NULL
) {
1040 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
1044 publen
= EC_KEY_key2buf(a
, a
->conv_form
, &pub
, NULL
);
1047 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_EC_LIB
);
1051 priv_key
->publicKey
->flags
&= ~(ASN1_STRING_FLAG_BITS_LEFT
| 0x07);
1052 priv_key
->publicKey
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
1053 ASN1_STRING_set0(priv_key
->publicKey
, pub
, publen
);
1057 if ((ret
= i2d_EC_PRIVATEKEY(priv_key
, out
)) == 0) {
1058 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_EC_LIB
);
1063 OPENSSL_clear_free(priv
, privlen
);
1065 EC_PRIVATEKEY_free(priv_key
);
1066 return (ok
? ret
: 0);
1069 int i2d_ECParameters(EC_KEY
*a
, unsigned char **out
)
1072 ECerr(EC_F_I2D_ECPARAMETERS
, ERR_R_PASSED_NULL_PARAMETER
);
1075 return i2d_ECPKParameters(a
->group
, out
);
1078 EC_KEY
*d2i_ECParameters(EC_KEY
**a
, const unsigned char **in
, long len
)
1082 if (in
== NULL
|| *in
== NULL
) {
1083 ECerr(EC_F_D2I_ECPARAMETERS
, ERR_R_PASSED_NULL_PARAMETER
);
1087 if (a
== NULL
|| *a
== NULL
) {
1088 if ((ret
= EC_KEY_new()) == NULL
) {
1089 ECerr(EC_F_D2I_ECPARAMETERS
, ERR_R_MALLOC_FAILURE
);
1095 if (!d2i_ECPKParameters(&ret
->group
, in
, len
)) {
1096 ECerr(EC_F_D2I_ECPARAMETERS
, ERR_R_EC_LIB
);
1097 if (a
== NULL
|| *a
!= ret
)
1108 EC_KEY
*o2i_ECPublicKey(EC_KEY
**a
, const unsigned char **in
, long len
)
1112 if (a
== NULL
|| (*a
) == NULL
|| (*a
)->group
== NULL
) {
1114 * sorry, but a EC_GROUP-structure is necessary to set the public key
1116 ECerr(EC_F_O2I_ECPUBLICKEY
, ERR_R_PASSED_NULL_PARAMETER
);
1120 if (!EC_KEY_oct2key(ret
, *in
, len
, NULL
)) {
1121 ECerr(EC_F_O2I_ECPUBLICKEY
, ERR_R_EC_LIB
);
1128 int i2o_ECPublicKey(EC_KEY
*a
, unsigned char **out
)
1134 ECerr(EC_F_I2O_ECPUBLICKEY
, ERR_R_PASSED_NULL_PARAMETER
);
1138 buf_len
= EC_POINT_point2oct(a
->group
, a
->pub_key
,
1139 a
->conv_form
, NULL
, 0, NULL
);
1141 if (out
== NULL
|| buf_len
== 0)
1142 /* out == NULL => just return the length of the octet string */
1146 if ((*out
= OPENSSL_malloc(buf_len
)) == NULL
) {
1147 ECerr(EC_F_I2O_ECPUBLICKEY
, ERR_R_MALLOC_FAILURE
);
1152 if (!EC_POINT_point2oct(a
->group
, a
->pub_key
, a
->conv_form
,
1153 *out
, buf_len
, NULL
)) {
1154 ECerr(EC_F_I2O_ECPUBLICKEY
, ERR_R_EC_LIB
);
1166 ASN1_SEQUENCE(ECDSA_SIG
) = {
1167 ASN1_SIMPLE(ECDSA_SIG
, r
, CBIGNUM
),
1168 ASN1_SIMPLE(ECDSA_SIG
, s
, CBIGNUM
)
1169 } static_ASN1_SEQUENCE_END(ECDSA_SIG
)
1171 DECLARE_ASN1_FUNCTIONS_const(ECDSA_SIG
)
1172 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECDSA_SIG
, ECDSA_SIG
)
1173 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ECDSA_SIG
, ECDSA_SIG
, ECDSA_SIG
)
1175 ECDSA_SIG
*ECDSA_SIG_new(void)
1177 ECDSA_SIG
*sig
= OPENSSL_zalloc(sizeof(*sig
));
1179 ECerr(EC_F_ECDSA_SIG_NEW
, ERR_R_MALLOC_FAILURE
);
1183 void ECDSA_SIG_free(ECDSA_SIG
*sig
)
1187 BN_clear_free(sig
->r
);
1188 BN_clear_free(sig
->s
);
1192 void ECDSA_SIG_get0(const ECDSA_SIG
*sig
, const BIGNUM
**pr
, const BIGNUM
**ps
)
1200 int ECDSA_SIG_set0(ECDSA_SIG
*sig
, BIGNUM
*r
, BIGNUM
*s
)
1202 if (r
== NULL
|| s
== NULL
)
1204 BN_clear_free(sig
->r
);
1205 BN_clear_free(sig
->s
);
1211 int ECDSA_size(const EC_KEY
*r
)
1215 unsigned char buf
[4];
1216 const EC_GROUP
*group
;
1220 group
= EC_KEY_get0_group(r
);
1224 i
= EC_GROUP_order_bits(group
);
1227 bs
.length
= (i
+ 7) / 8;
1229 bs
.type
= V_ASN1_INTEGER
;
1230 /* If the top bit is set the asn1 encoding is 1 larger. */
1233 i
= i2d_ASN1_INTEGER(&bs
, NULL
);
1234 i
+= i
; /* r and s */
1235 ret
= ASN1_object_size(1, i
, V_ASN1_SEQUENCE
);