2 * Written by Nils Larsch for the OpenSSL project.
4 /* ====================================================================
5 * Copyright (c) 2000-2003 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * licensing@OpenSSL.org.
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
60 #include <openssl/err.h>
61 #include <openssl/asn1t.h>
62 #include <openssl/objects.h>
64 int EC_GROUP_get_basis_type(const EC_GROUP
*group
)
68 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group
)) !=
69 NID_X9_62_characteristic_two_field
)
70 /* everything else is currently not supported */
73 while (group
->poly
[i
] != 0)
77 return NID_X9_62_ppBasis
;
79 return NID_X9_62_tpBasis
;
81 /* everything else is currently not supported */
85 #ifndef OPENSSL_NO_EC2M
86 int EC_GROUP_get_trinomial_basis(const EC_GROUP
*group
, unsigned int *k
)
91 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group
)) !=
92 NID_X9_62_characteristic_two_field
93 || !((group
->poly
[0] != 0) && (group
->poly
[1] != 0)
94 && (group
->poly
[2] == 0))) {
95 ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS
,
96 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
106 int EC_GROUP_get_pentanomial_basis(const EC_GROUP
*group
, unsigned int *k1
,
107 unsigned int *k2
, unsigned int *k3
)
112 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group
)) !=
113 NID_X9_62_characteristic_two_field
114 || !((group
->poly
[0] != 0) && (group
->poly
[1] != 0)
115 && (group
->poly
[2] != 0) && (group
->poly
[3] != 0)
116 && (group
->poly
[4] == 0))) {
117 ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS
,
118 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
123 *k1
= group
->poly
[3];
125 *k2
= group
->poly
[2];
127 *k3
= group
->poly
[1];
133 /* some structures needed for the asn1 encoding */
134 typedef struct x9_62_pentanomial_st
{
140 typedef struct x9_62_characteristic_two_st
{
145 /* NID_X9_62_onBasis */
147 /* NID_X9_62_tpBasis */
148 ASN1_INTEGER
*tpBasis
;
149 /* NID_X9_62_ppBasis */
150 X9_62_PENTANOMIAL
*ppBasis
;
154 } X9_62_CHARACTERISTIC_TWO
;
156 typedef struct x9_62_fieldid_st
{
157 ASN1_OBJECT
*fieldType
;
160 /* NID_X9_62_prime_field */
162 /* NID_X9_62_characteristic_two_field */
163 X9_62_CHARACTERISTIC_TWO
*char_two
;
169 typedef struct x9_62_curve_st
{
170 ASN1_OCTET_STRING
*a
;
171 ASN1_OCTET_STRING
*b
;
172 ASN1_BIT_STRING
*seed
;
175 typedef struct ec_parameters_st
{
177 X9_62_FIELDID
*fieldID
;
179 ASN1_OCTET_STRING
*base
;
181 ASN1_INTEGER
*cofactor
;
184 struct ecpk_parameters_st
{
187 ASN1_OBJECT
*named_curve
;
188 ECPARAMETERS
*parameters
;
189 ASN1_NULL
*implicitlyCA
;
191 } /* ECPKPARAMETERS */ ;
193 /* SEC1 ECPrivateKey */
194 typedef struct ec_privatekey_st
{
196 ASN1_OCTET_STRING
*privateKey
;
197 ECPKPARAMETERS
*parameters
;
198 ASN1_BIT_STRING
*publicKey
;
201 /* the OpenSSL ASN.1 definitions */
202 ASN1_SEQUENCE(X9_62_PENTANOMIAL
) = {
203 ASN1_SIMPLE(X9_62_PENTANOMIAL
, k1
, LONG
),
204 ASN1_SIMPLE(X9_62_PENTANOMIAL
, k2
, LONG
),
205 ASN1_SIMPLE(X9_62_PENTANOMIAL
, k3
, LONG
)
206 } static_ASN1_SEQUENCE_END(X9_62_PENTANOMIAL
)
208 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL
)
209 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL
)
211 ASN1_ADB_TEMPLATE(char_two_def
) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, p
.other
, ASN1_ANY
);
213 ASN1_ADB(X9_62_CHARACTERISTIC_TWO
) = {
214 ADB_ENTRY(NID_X9_62_onBasis
, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, p
.onBasis
, ASN1_NULL
)),
215 ADB_ENTRY(NID_X9_62_tpBasis
, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, p
.tpBasis
, ASN1_INTEGER
)),
216 ADB_ENTRY(NID_X9_62_ppBasis
, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, p
.ppBasis
, X9_62_PENTANOMIAL
))
217 } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO
, 0, type
, 0, &char_two_def_tt
, NULL
);
219 ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO
) = {
220 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, m
, LONG
),
221 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO
, type
, ASN1_OBJECT
),
222 ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO
)
223 } static_ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO
)
225 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO
)
226 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO
)
228 ASN1_ADB_TEMPLATE(fieldID_def
) = ASN1_SIMPLE(X9_62_FIELDID
, p
.other
, ASN1_ANY
);
230 ASN1_ADB(X9_62_FIELDID
) = {
231 ADB_ENTRY(NID_X9_62_prime_field
, ASN1_SIMPLE(X9_62_FIELDID
, p
.prime
, ASN1_INTEGER
)),
232 ADB_ENTRY(NID_X9_62_characteristic_two_field
, ASN1_SIMPLE(X9_62_FIELDID
, p
.char_two
, X9_62_CHARACTERISTIC_TWO
))
233 } ASN1_ADB_END(X9_62_FIELDID
, 0, fieldType
, 0, &fieldID_def_tt
, NULL
);
235 ASN1_SEQUENCE(X9_62_FIELDID
) = {
236 ASN1_SIMPLE(X9_62_FIELDID
, fieldType
, ASN1_OBJECT
),
237 ASN1_ADB_OBJECT(X9_62_FIELDID
)
238 } static_ASN1_SEQUENCE_END(X9_62_FIELDID
)
240 ASN1_SEQUENCE(X9_62_CURVE
) = {
241 ASN1_SIMPLE(X9_62_CURVE
, a
, ASN1_OCTET_STRING
),
242 ASN1_SIMPLE(X9_62_CURVE
, b
, ASN1_OCTET_STRING
),
243 ASN1_OPT(X9_62_CURVE
, seed
, ASN1_BIT_STRING
)
244 } static_ASN1_SEQUENCE_END(X9_62_CURVE
)
246 ASN1_SEQUENCE(ECPARAMETERS
) = {
247 ASN1_SIMPLE(ECPARAMETERS
, version
, LONG
),
248 ASN1_SIMPLE(ECPARAMETERS
, fieldID
, X9_62_FIELDID
),
249 ASN1_SIMPLE(ECPARAMETERS
, curve
, X9_62_CURVE
),
250 ASN1_SIMPLE(ECPARAMETERS
, base
, ASN1_OCTET_STRING
),
251 ASN1_SIMPLE(ECPARAMETERS
, order
, ASN1_INTEGER
),
252 ASN1_OPT(ECPARAMETERS
, cofactor
, ASN1_INTEGER
)
253 } static_ASN1_SEQUENCE_END(ECPARAMETERS
)
255 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS
)
256 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS
)
258 ASN1_CHOICE(ECPKPARAMETERS
) = {
259 ASN1_SIMPLE(ECPKPARAMETERS
, value
.named_curve
, ASN1_OBJECT
),
260 ASN1_SIMPLE(ECPKPARAMETERS
, value
.parameters
, ECPARAMETERS
),
261 ASN1_SIMPLE(ECPKPARAMETERS
, value
.implicitlyCA
, ASN1_NULL
)
262 } static_ASN1_CHOICE_END(ECPKPARAMETERS
)
264 DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS
)
265 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPKPARAMETERS
, ECPKPARAMETERS
)
266 IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS
)
268 ASN1_SEQUENCE(EC_PRIVATEKEY
) = {
269 ASN1_SIMPLE(EC_PRIVATEKEY
, version
, LONG
),
270 ASN1_SIMPLE(EC_PRIVATEKEY
, privateKey
, ASN1_OCTET_STRING
),
271 ASN1_EXP_OPT(EC_PRIVATEKEY
, parameters
, ECPKPARAMETERS
, 0),
272 ASN1_EXP_OPT(EC_PRIVATEKEY
, publicKey
, ASN1_BIT_STRING
, 1)
273 } static_ASN1_SEQUENCE_END(EC_PRIVATEKEY
)
275 DECLARE_ASN1_FUNCTIONS_const(EC_PRIVATEKEY
)
276 DECLARE_ASN1_ENCODE_FUNCTIONS_const(EC_PRIVATEKEY
, EC_PRIVATEKEY
)
277 IMPLEMENT_ASN1_FUNCTIONS_const(EC_PRIVATEKEY
)
279 /* some declarations of internal function */
281 /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
282 static int ec_asn1_group2fieldid(const EC_GROUP
*, X9_62_FIELDID
*);
283 /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
284 static int ec_asn1_group2curve(const EC_GROUP
*, X9_62_CURVE
*);
286 * ec_asn1_parameters2group() creates a EC_GROUP object from a ECPARAMETERS
289 static EC_GROUP
*ec_asn1_parameters2group(const ECPARAMETERS
*);
291 * ec_asn1_group2parameters() creates a ECPARAMETERS object from a EC_GROUP
294 static ECPARAMETERS
*ec_asn1_group2parameters(const EC_GROUP
*,
297 * ec_asn1_pkparameters2group() creates a EC_GROUP object from a
298 * ECPKPARAMETERS object
300 static EC_GROUP
*ec_asn1_pkparameters2group(const ECPKPARAMETERS
*);
302 * ec_asn1_group2pkparameters() creates a ECPKPARAMETERS object from a
305 static ECPKPARAMETERS
*ec_asn1_group2pkparameters(const EC_GROUP
*,
308 /* the function definitions */
310 static int ec_asn1_group2fieldid(const EC_GROUP
*group
, X9_62_FIELDID
*field
)
315 if (group
== NULL
|| field
== NULL
)
318 /* clear the old values (if necessary) */
319 ASN1_OBJECT_free(field
->fieldType
);
320 ASN1_TYPE_free(field
->p
.other
);
322 nid
= EC_METHOD_get_field_type(EC_GROUP_method_of(group
));
323 /* set OID for the field */
324 if ((field
->fieldType
= OBJ_nid2obj(nid
)) == NULL
) {
325 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_OBJ_LIB
);
329 if (nid
== NID_X9_62_prime_field
) {
330 if ((tmp
= BN_new()) == NULL
) {
331 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_MALLOC_FAILURE
);
334 /* the parameters are specified by the prime number p */
335 if (!EC_GROUP_get_curve_GFp(group
, tmp
, NULL
, NULL
, NULL
)) {
336 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_EC_LIB
);
339 /* set the prime number */
340 field
->p
.prime
= BN_to_ASN1_INTEGER(tmp
, NULL
);
341 if (field
->p
.prime
== NULL
) {
342 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_ASN1_LIB
);
345 } else /* nid == NID_X9_62_characteristic_two_field */
346 #ifdef OPENSSL_NO_EC2M
348 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, EC_R_GF2M_NOT_SUPPORTED
);
354 X9_62_CHARACTERISTIC_TWO
*char_two
;
356 field
->p
.char_two
= X9_62_CHARACTERISTIC_TWO_new();
357 char_two
= field
->p
.char_two
;
359 if (char_two
== NULL
) {
360 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_MALLOC_FAILURE
);
364 char_two
->m
= (long)EC_GROUP_get_degree(group
);
366 field_type
= EC_GROUP_get_basis_type(group
);
368 if (field_type
== 0) {
369 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_EC_LIB
);
372 /* set base type OID */
373 if ((char_two
->type
= OBJ_nid2obj(field_type
)) == NULL
) {
374 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_OBJ_LIB
);
378 if (field_type
== NID_X9_62_tpBasis
) {
381 if (!EC_GROUP_get_trinomial_basis(group
, &k
))
384 char_two
->p
.tpBasis
= ASN1_INTEGER_new();
385 if (char_two
->p
.tpBasis
== NULL
) {
386 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_MALLOC_FAILURE
);
389 if (!ASN1_INTEGER_set(char_two
->p
.tpBasis
, (long)k
)) {
390 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_ASN1_LIB
);
393 } else if (field_type
== NID_X9_62_ppBasis
) {
394 unsigned int k1
, k2
, k3
;
396 if (!EC_GROUP_get_pentanomial_basis(group
, &k1
, &k2
, &k3
))
399 char_two
->p
.ppBasis
= X9_62_PENTANOMIAL_new();
400 if (char_two
->p
.ppBasis
== NULL
) {
401 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_MALLOC_FAILURE
);
406 char_two
->p
.ppBasis
->k1
= (long)k1
;
407 char_two
->p
.ppBasis
->k2
= (long)k2
;
408 char_two
->p
.ppBasis
->k3
= (long)k3
;
409 } else { /* field_type == NID_X9_62_onBasis */
411 /* for ONB the parameters are (asn1) NULL */
412 char_two
->p
.onBasis
= ASN1_NULL_new();
413 if (char_two
->p
.onBasis
== NULL
) {
414 ECerr(EC_F_EC_ASN1_GROUP2FIELDID
, ERR_R_MALLOC_FAILURE
);
428 static int ec_asn1_group2curve(const EC_GROUP
*group
, X9_62_CURVE
*curve
)
431 BIGNUM
*tmp_1
= NULL
, *tmp_2
= NULL
;
432 unsigned char *buffer_1
= NULL
, *buffer_2
= NULL
,
433 *a_buf
= NULL
, *b_buf
= NULL
;
435 unsigned char char_zero
= 0;
437 if (!group
|| !curve
|| !curve
->a
|| !curve
->b
)
440 if ((tmp_1
= BN_new()) == NULL
|| (tmp_2
= BN_new()) == NULL
) {
441 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_MALLOC_FAILURE
);
445 nid
= EC_METHOD_get_field_type(EC_GROUP_method_of(group
));
448 if (nid
== NID_X9_62_prime_field
) {
449 if (!EC_GROUP_get_curve_GFp(group
, NULL
, tmp_1
, tmp_2
, NULL
)) {
450 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_EC_LIB
);
454 #ifndef OPENSSL_NO_EC2M
455 else { /* nid == NID_X9_62_characteristic_two_field */
457 if (!EC_GROUP_get_curve_GF2m(group
, NULL
, tmp_1
, tmp_2
, NULL
)) {
458 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_EC_LIB
);
463 len_1
= (size_t)BN_num_bytes(tmp_1
);
464 len_2
= (size_t)BN_num_bytes(tmp_2
);
467 /* len_1 == 0 => a == 0 */
471 if ((buffer_1
= OPENSSL_malloc(len_1
)) == NULL
) {
472 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_MALLOC_FAILURE
);
475 if ((len_1
= BN_bn2bin(tmp_1
, buffer_1
)) == 0) {
476 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_BN_LIB
);
483 /* len_2 == 0 => b == 0 */
487 if ((buffer_2
= OPENSSL_malloc(len_2
)) == NULL
) {
488 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_MALLOC_FAILURE
);
491 if ((len_2
= BN_bn2bin(tmp_2
, buffer_2
)) == 0) {
492 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_BN_LIB
);
499 if (!ASN1_OCTET_STRING_set(curve
->a
, a_buf
, len_1
) ||
500 !ASN1_OCTET_STRING_set(curve
->b
, b_buf
, len_2
)) {
501 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_ASN1_LIB
);
505 /* set the seed (optional) */
508 if ((curve
->seed
= ASN1_BIT_STRING_new()) == NULL
) {
509 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_MALLOC_FAILURE
);
512 curve
->seed
->flags
&= ~(ASN1_STRING_FLAG_BITS_LEFT
| 0x07);
513 curve
->seed
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
514 if (!ASN1_BIT_STRING_set(curve
->seed
, group
->seed
,
515 (int)group
->seed_len
)) {
516 ECerr(EC_F_EC_ASN1_GROUP2CURVE
, ERR_R_ASN1_LIB
);
520 ASN1_BIT_STRING_free(curve
->seed
);
527 OPENSSL_free(buffer_1
);
528 OPENSSL_free(buffer_2
);
534 static ECPARAMETERS
*ec_asn1_group2parameters(const EC_GROUP
*group
,
538 ECPARAMETERS
*ret
= NULL
;
540 unsigned char *buffer
= NULL
;
541 const EC_POINT
*point
= NULL
;
542 point_conversion_form_t form
;
544 if ((tmp
= BN_new()) == NULL
) {
545 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS
, ERR_R_MALLOC_FAILURE
);
550 if ((ret
= ECPARAMETERS_new()) == NULL
) {
551 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS
, ERR_R_MALLOC_FAILURE
);
557 /* set the version (always one) */
558 ret
->version
= (long)0x1;
560 /* set the fieldID */
561 if (!ec_asn1_group2fieldid(group
, ret
->fieldID
)) {
562 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS
, ERR_R_EC_LIB
);
567 if (!ec_asn1_group2curve(group
, ret
->curve
)) {
568 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS
, ERR_R_EC_LIB
);
572 /* set the base point */
573 if ((point
= EC_GROUP_get0_generator(group
)) == NULL
) {
574 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS
, EC_R_UNDEFINED_GENERATOR
);
578 form
= EC_GROUP_get_point_conversion_form(group
);
580 len
= EC_POINT_point2buf(group
, point
, form
, &buffer
, NULL
);
582 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS
, ERR_R_EC_LIB
);
585 if (ret
->base
== NULL
&& (ret
->base
= ASN1_OCTET_STRING_new()) == NULL
) {
586 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS
, ERR_R_MALLOC_FAILURE
);
589 if (!ASN1_OCTET_STRING_set(ret
->base
, buffer
, len
)) {
590 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS
, ERR_R_ASN1_LIB
);
595 if (!EC_GROUP_get_order(group
, tmp
, NULL
)) {
596 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS
, ERR_R_EC_LIB
);
599 ret
->order
= BN_to_ASN1_INTEGER(tmp
, ret
->order
);
600 if (ret
->order
== NULL
) {
601 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS
, ERR_R_ASN1_LIB
);
605 /* set the cofactor (optional) */
606 if (EC_GROUP_get_cofactor(group
, tmp
, NULL
)) {
607 ret
->cofactor
= BN_to_ASN1_INTEGER(tmp
, ret
->cofactor
);
608 if (ret
->cofactor
== NULL
) {
609 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS
, ERR_R_ASN1_LIB
);
618 ECPARAMETERS_free(ret
);
620 OPENSSL_free(buffer
);
624 ECPKPARAMETERS
*ec_asn1_group2pkparameters(const EC_GROUP
*group
,
625 ECPKPARAMETERS
*params
)
628 ECPKPARAMETERS
*ret
= params
;
631 if ((ret
= ECPKPARAMETERS_new()) == NULL
) {
632 ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS
, ERR_R_MALLOC_FAILURE
);
637 ASN1_OBJECT_free(ret
->value
.named_curve
);
638 else if (ret
->type
== 1 && ret
->value
.parameters
)
639 ECPARAMETERS_free(ret
->value
.parameters
);
642 if (EC_GROUP_get_asn1_flag(group
)) {
644 * use the asn1 OID to describe the the elliptic curve parameters
646 tmp
= EC_GROUP_get_curve_name(group
);
649 if ((ret
->value
.named_curve
= OBJ_nid2obj(tmp
)) == NULL
)
652 /* we don't kmow the nid => ERROR */
655 /* use the ECPARAMETERS structure */
657 if ((ret
->value
.parameters
=
658 ec_asn1_group2parameters(group
, NULL
)) == NULL
)
663 ECPKPARAMETERS_free(ret
);
669 static EC_GROUP
*ec_asn1_parameters2group(const ECPARAMETERS
*params
)
672 EC_GROUP
*ret
= NULL
;
673 BIGNUM
*p
= NULL
, *a
= NULL
, *b
= NULL
;
674 EC_POINT
*point
= NULL
;
677 if (!params
->fieldID
|| !params
->fieldID
->fieldType
||
678 !params
->fieldID
->p
.ptr
) {
679 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, EC_R_ASN1_ERROR
);
683 /* now extract the curve parameters a and b */
684 if (!params
->curve
|| !params
->curve
->a
||
685 !params
->curve
->a
->data
|| !params
->curve
->b
||
686 !params
->curve
->b
->data
) {
687 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, EC_R_ASN1_ERROR
);
690 a
= BN_bin2bn(params
->curve
->a
->data
, params
->curve
->a
->length
, NULL
);
692 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, ERR_R_BN_LIB
);
695 b
= BN_bin2bn(params
->curve
->b
->data
, params
->curve
->b
->length
, NULL
);
697 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, ERR_R_BN_LIB
);
701 /* get the field parameters */
702 tmp
= OBJ_obj2nid(params
->fieldID
->fieldType
);
703 if (tmp
== NID_X9_62_characteristic_two_field
)
704 #ifdef OPENSSL_NO_EC2M
706 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, EC_R_GF2M_NOT_SUPPORTED
);
711 X9_62_CHARACTERISTIC_TWO
*char_two
;
713 char_two
= params
->fieldID
->p
.char_two
;
715 field_bits
= char_two
->m
;
716 if (field_bits
> OPENSSL_ECC_MAX_FIELD_BITS
) {
717 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, EC_R_FIELD_TOO_LARGE
);
721 if ((p
= BN_new()) == NULL
) {
722 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, ERR_R_MALLOC_FAILURE
);
726 /* get the base type */
727 tmp
= OBJ_obj2nid(char_two
->type
);
729 if (tmp
== NID_X9_62_tpBasis
) {
732 if (!char_two
->p
.tpBasis
) {
733 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, EC_R_ASN1_ERROR
);
737 tmp_long
= ASN1_INTEGER_get(char_two
->p
.tpBasis
);
739 if (!(char_two
->m
> tmp_long
&& tmp_long
> 0)) {
740 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
,
741 EC_R_INVALID_TRINOMIAL_BASIS
);
745 /* create the polynomial */
746 if (!BN_set_bit(p
, (int)char_two
->m
))
748 if (!BN_set_bit(p
, (int)tmp_long
))
750 if (!BN_set_bit(p
, 0))
752 } else if (tmp
== NID_X9_62_ppBasis
) {
753 X9_62_PENTANOMIAL
*penta
;
755 penta
= char_two
->p
.ppBasis
;
757 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, EC_R_ASN1_ERROR
);
762 (char_two
->m
> penta
->k3
&& penta
->k3
> penta
->k2
763 && penta
->k2
> penta
->k1
&& penta
->k1
> 0)) {
764 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
,
765 EC_R_INVALID_PENTANOMIAL_BASIS
);
769 /* create the polynomial */
770 if (!BN_set_bit(p
, (int)char_two
->m
))
772 if (!BN_set_bit(p
, (int)penta
->k1
))
774 if (!BN_set_bit(p
, (int)penta
->k2
))
776 if (!BN_set_bit(p
, (int)penta
->k3
))
778 if (!BN_set_bit(p
, 0))
780 } else if (tmp
== NID_X9_62_onBasis
) {
781 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, EC_R_NOT_IMPLEMENTED
);
785 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, EC_R_ASN1_ERROR
);
789 /* create the EC_GROUP structure */
790 ret
= EC_GROUP_new_curve_GF2m(p
, a
, b
, NULL
);
793 else if (tmp
== NID_X9_62_prime_field
) {
794 /* we have a curve over a prime field */
795 /* extract the prime number */
796 if (!params
->fieldID
->p
.prime
) {
797 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, EC_R_ASN1_ERROR
);
800 p
= ASN1_INTEGER_to_BN(params
->fieldID
->p
.prime
, NULL
);
802 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, ERR_R_ASN1_LIB
);
806 if (BN_is_negative(p
) || BN_is_zero(p
)) {
807 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, EC_R_INVALID_FIELD
);
811 field_bits
= BN_num_bits(p
);
812 if (field_bits
> OPENSSL_ECC_MAX_FIELD_BITS
) {
813 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, EC_R_FIELD_TOO_LARGE
);
817 /* create the EC_GROUP structure */
818 ret
= EC_GROUP_new_curve_GFp(p
, a
, b
, NULL
);
820 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, EC_R_INVALID_FIELD
);
825 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, ERR_R_EC_LIB
);
829 /* extract seed (optional) */
830 if (params
->curve
->seed
!= NULL
) {
831 OPENSSL_free(ret
->seed
);
832 if ((ret
->seed
= OPENSSL_malloc(params
->curve
->seed
->length
)) == NULL
) {
833 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, ERR_R_MALLOC_FAILURE
);
836 memcpy(ret
->seed
, params
->curve
->seed
->data
,
837 params
->curve
->seed
->length
);
838 ret
->seed_len
= params
->curve
->seed
->length
;
841 if (!params
->order
|| !params
->base
|| !params
->base
->data
) {
842 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, EC_R_ASN1_ERROR
);
846 if ((point
= EC_POINT_new(ret
)) == NULL
)
849 /* set the point conversion form */
850 EC_GROUP_set_point_conversion_form(ret
, (point_conversion_form_t
)
851 (params
->base
->data
[0] & ~0x01));
853 /* extract the ec point */
854 if (!EC_POINT_oct2point(ret
, point
, params
->base
->data
,
855 params
->base
->length
, NULL
)) {
856 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, ERR_R_EC_LIB
);
860 /* extract the order */
861 if ((a
= ASN1_INTEGER_to_BN(params
->order
, a
)) == NULL
) {
862 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, ERR_R_ASN1_LIB
);
865 if (BN_is_negative(a
) || BN_is_zero(a
)) {
866 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, EC_R_INVALID_GROUP_ORDER
);
869 if (BN_num_bits(a
) > (int)field_bits
+ 1) { /* Hasse bound */
870 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, EC_R_INVALID_GROUP_ORDER
);
874 /* extract the cofactor (optional) */
875 if (params
->cofactor
== NULL
) {
878 } else if ((b
= ASN1_INTEGER_to_BN(params
->cofactor
, b
)) == NULL
) {
879 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, ERR_R_ASN1_LIB
);
882 /* set the generator, order and cofactor (if present) */
883 if (!EC_GROUP_set_generator(ret
, point
, a
, b
)) {
884 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP
, ERR_R_EC_LIB
);
892 EC_GROUP_clear_free(ret
);
899 EC_POINT_free(point
);
903 EC_GROUP
*ec_asn1_pkparameters2group(const ECPKPARAMETERS
*params
)
905 EC_GROUP
*ret
= NULL
;
908 if (params
== NULL
) {
909 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP
, EC_R_MISSING_PARAMETERS
);
913 if (params
->type
== 0) { /* the curve is given by an OID */
914 tmp
= OBJ_obj2nid(params
->value
.named_curve
);
915 if ((ret
= EC_GROUP_new_by_curve_name(tmp
)) == NULL
) {
916 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP
,
917 EC_R_EC_GROUP_NEW_BY_NAME_FAILURE
);
920 EC_GROUP_set_asn1_flag(ret
, OPENSSL_EC_NAMED_CURVE
);
921 } else if (params
->type
== 1) { /* the parameters are given by a
922 * ECPARAMETERS structure */
923 ret
= ec_asn1_parameters2group(params
->value
.parameters
);
925 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP
, ERR_R_EC_LIB
);
928 EC_GROUP_set_asn1_flag(ret
, 0x0);
929 } else if (params
->type
== 2) { /* implicitlyCA */
932 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP
, EC_R_ASN1_ERROR
);
939 /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
941 EC_GROUP
*d2i_ECPKParameters(EC_GROUP
**a
, const unsigned char **in
, long len
)
943 EC_GROUP
*group
= NULL
;
944 ECPKPARAMETERS
*params
= NULL
;
945 const unsigned char *p
= *in
;
947 if ((params
= d2i_ECPKPARAMETERS(NULL
, &p
, len
)) == NULL
) {
948 ECerr(EC_F_D2I_ECPKPARAMETERS
, EC_R_D2I_ECPKPARAMETERS_FAILURE
);
949 ECPKPARAMETERS_free(params
);
953 if ((group
= ec_asn1_pkparameters2group(params
)) == NULL
) {
954 ECerr(EC_F_D2I_ECPKPARAMETERS
, EC_R_PKPARAMETERS2GROUP_FAILURE
);
955 ECPKPARAMETERS_free(params
);
960 EC_GROUP_clear_free(*a
);
964 ECPKPARAMETERS_free(params
);
969 int i2d_ECPKParameters(const EC_GROUP
*a
, unsigned char **out
)
972 ECPKPARAMETERS
*tmp
= ec_asn1_group2pkparameters(a
, NULL
);
974 ECerr(EC_F_I2D_ECPKPARAMETERS
, EC_R_GROUP2PKPARAMETERS_FAILURE
);
977 if ((ret
= i2d_ECPKPARAMETERS(tmp
, out
)) == 0) {
978 ECerr(EC_F_I2D_ECPKPARAMETERS
, EC_R_I2D_ECPKPARAMETERS_FAILURE
);
979 ECPKPARAMETERS_free(tmp
);
982 ECPKPARAMETERS_free(tmp
);
986 /* some EC_KEY functions */
988 EC_KEY
*d2i_ECPrivateKey(EC_KEY
**a
, const unsigned char **in
, long len
)
991 EC_PRIVATEKEY
*priv_key
= NULL
;
992 const unsigned char *p
= *in
;
994 if ((priv_key
= d2i_EC_PRIVATEKEY(NULL
, &p
, len
)) == NULL
) {
995 ECerr(EC_F_D2I_ECPRIVATEKEY
, ERR_R_EC_LIB
);
999 if (a
== NULL
|| *a
== NULL
) {
1000 if ((ret
= EC_KEY_new()) == NULL
) {
1001 ECerr(EC_F_D2I_ECPRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
1007 if (priv_key
->parameters
) {
1008 EC_GROUP_clear_free(ret
->group
);
1009 ret
->group
= ec_asn1_pkparameters2group(priv_key
->parameters
);
1012 if (ret
->group
== NULL
) {
1013 ECerr(EC_F_D2I_ECPRIVATEKEY
, ERR_R_EC_LIB
);
1017 ret
->version
= priv_key
->version
;
1019 if (priv_key
->privateKey
) {
1020 if (ret
->priv_key
== NULL
)
1021 ret
->priv_key
= BN_secure_new();
1022 if (ret
->priv_key
== NULL
) {
1023 ECerr(EC_F_D2I_ECPRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
1026 ret
->priv_key
= BN_bin2bn(ASN1_STRING_data(priv_key
->privateKey
),
1027 ASN1_STRING_length(priv_key
->privateKey
),
1029 if (ret
->priv_key
== NULL
) {
1030 ECerr(EC_F_D2I_ECPRIVATEKEY
, ERR_R_BN_LIB
);
1034 ECerr(EC_F_D2I_ECPRIVATEKEY
, EC_R_MISSING_PRIVATE_KEY
);
1038 EC_POINT_clear_free(ret
->pub_key
);
1039 ret
->pub_key
= EC_POINT_new(ret
->group
);
1040 if (ret
->pub_key
== NULL
) {
1041 ECerr(EC_F_D2I_ECPRIVATEKEY
, ERR_R_EC_LIB
);
1045 if (priv_key
->publicKey
) {
1046 const unsigned char *pub_oct
;
1049 pub_oct
= ASN1_STRING_data(priv_key
->publicKey
);
1050 pub_oct_len
= ASN1_STRING_length(priv_key
->publicKey
);
1052 * The first byte - point conversion form - must be present.
1054 if (pub_oct_len
<= 0) {
1055 ECerr(EC_F_D2I_ECPRIVATEKEY
, EC_R_BUFFER_TOO_SMALL
);
1058 /* Save the point conversion form. */
1059 ret
->conv_form
= (point_conversion_form_t
) (pub_oct
[0] & ~0x01);
1060 if (!EC_POINT_oct2point(ret
->group
, ret
->pub_key
,
1061 pub_oct
, (size_t)(pub_oct_len
), NULL
)) {
1062 ECerr(EC_F_D2I_ECPRIVATEKEY
, ERR_R_EC_LIB
);
1067 (ret
->group
, ret
->pub_key
, ret
->priv_key
, NULL
, NULL
, NULL
)) {
1068 ECerr(EC_F_D2I_ECPRIVATEKEY
, ERR_R_EC_LIB
);
1071 /* Remember the original private-key-only encoding. */
1072 ret
->enc_flag
|= EC_PKEY_NO_PUBKEY
;
1077 EC_PRIVATEKEY_free(priv_key
);
1082 if (a
== NULL
|| *a
!= ret
)
1084 EC_PRIVATEKEY_free(priv_key
);
1088 int i2d_ECPrivateKey(EC_KEY
*a
, unsigned char **out
)
1090 int ret
= 0, ok
= 0;
1091 unsigned char *buffer
= NULL
;
1092 size_t buf_len
= 0, tmp_len
, bn_len
;
1093 EC_PRIVATEKEY
*priv_key
= NULL
;
1095 if (a
== NULL
|| a
->group
== NULL
|| a
->priv_key
== NULL
||
1096 (!(a
->enc_flag
& EC_PKEY_NO_PUBKEY
) && a
->pub_key
== NULL
)) {
1097 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
1101 if ((priv_key
= EC_PRIVATEKEY_new()) == NULL
) {
1102 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
1106 priv_key
->version
= a
->version
;
1108 bn_len
= (size_t)BN_num_bytes(a
->priv_key
);
1110 /* Octetstring may need leading zeros if BN is to short */
1112 buf_len
= (EC_GROUP_get_degree(a
->group
) + 7) / 8;
1114 if (bn_len
> buf_len
) {
1115 ECerr(EC_F_I2D_ECPRIVATEKEY
, EC_R_BUFFER_TOO_SMALL
);
1119 buffer
= OPENSSL_malloc(buf_len
);
1120 if (buffer
== NULL
) {
1121 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
1125 if (!BN_bn2bin(a
->priv_key
, buffer
+ buf_len
- bn_len
)) {
1126 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_BN_LIB
);
1130 if (buf_len
- bn_len
> 0) {
1131 memset(buffer
, 0, buf_len
- bn_len
);
1134 if (!ASN1_OCTET_STRING_set(priv_key
->privateKey
, buffer
, buf_len
)) {
1135 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_ASN1_LIB
);
1139 if (!(a
->enc_flag
& EC_PKEY_NO_PARAMETERS
)) {
1140 if ((priv_key
->parameters
=
1141 ec_asn1_group2pkparameters(a
->group
,
1142 priv_key
->parameters
)) == NULL
) {
1143 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_EC_LIB
);
1148 if (!(a
->enc_flag
& EC_PKEY_NO_PUBKEY
)) {
1149 priv_key
->publicKey
= ASN1_BIT_STRING_new();
1150 if (priv_key
->publicKey
== NULL
) {
1151 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
1155 tmp_len
= EC_POINT_point2oct(a
->group
, a
->pub_key
,
1156 a
->conv_form
, NULL
, 0, NULL
);
1158 if (tmp_len
> buf_len
) {
1159 unsigned char *tmp_buffer
= OPENSSL_realloc(buffer
, tmp_len
);
1161 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
1164 buffer
= tmp_buffer
;
1168 if (!EC_POINT_point2oct(a
->group
, a
->pub_key
,
1169 a
->conv_form
, buffer
, buf_len
, NULL
)) {
1170 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_EC_LIB
);
1174 priv_key
->publicKey
->flags
&= ~(ASN1_STRING_FLAG_BITS_LEFT
| 0x07);
1175 priv_key
->publicKey
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
1176 if (!ASN1_BIT_STRING_set(priv_key
->publicKey
, buffer
, buf_len
)) {
1177 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_ASN1_LIB
);
1182 if ((ret
= i2d_EC_PRIVATEKEY(priv_key
, out
)) == 0) {
1183 ECerr(EC_F_I2D_ECPRIVATEKEY
, ERR_R_EC_LIB
);
1188 OPENSSL_free(buffer
);
1189 EC_PRIVATEKEY_free(priv_key
);
1190 return (ok
? ret
: 0);
1193 int i2d_ECParameters(EC_KEY
*a
, unsigned char **out
)
1196 ECerr(EC_F_I2D_ECPARAMETERS
, ERR_R_PASSED_NULL_PARAMETER
);
1199 return i2d_ECPKParameters(a
->group
, out
);
1202 EC_KEY
*d2i_ECParameters(EC_KEY
**a
, const unsigned char **in
, long len
)
1206 if (in
== NULL
|| *in
== NULL
) {
1207 ECerr(EC_F_D2I_ECPARAMETERS
, ERR_R_PASSED_NULL_PARAMETER
);
1211 if (a
== NULL
|| *a
== NULL
) {
1212 if ((ret
= EC_KEY_new()) == NULL
) {
1213 ECerr(EC_F_D2I_ECPARAMETERS
, ERR_R_MALLOC_FAILURE
);
1219 if (!d2i_ECPKParameters(&ret
->group
, in
, len
)) {
1220 ECerr(EC_F_D2I_ECPARAMETERS
, ERR_R_EC_LIB
);
1221 if (a
== NULL
|| *a
!= ret
)
1232 EC_KEY
*o2i_ECPublicKey(EC_KEY
**a
, const unsigned char **in
, long len
)
1236 if (a
== NULL
|| (*a
) == NULL
|| (*a
)->group
== NULL
) {
1238 * sorry, but a EC_GROUP-structur is necessary to set the public key
1240 ECerr(EC_F_O2I_ECPUBLICKEY
, ERR_R_PASSED_NULL_PARAMETER
);
1244 if (ret
->pub_key
== NULL
&&
1245 (ret
->pub_key
= EC_POINT_new(ret
->group
)) == NULL
) {
1246 ECerr(EC_F_O2I_ECPUBLICKEY
, ERR_R_MALLOC_FAILURE
);
1249 if (!EC_POINT_oct2point(ret
->group
, ret
->pub_key
, *in
, len
, NULL
)) {
1250 ECerr(EC_F_O2I_ECPUBLICKEY
, ERR_R_EC_LIB
);
1253 /* save the point conversion form */
1254 ret
->conv_form
= (point_conversion_form_t
) (*in
[0] & ~0x01);
1259 int i2o_ECPublicKey(EC_KEY
*a
, unsigned char **out
)
1265 ECerr(EC_F_I2O_ECPUBLICKEY
, ERR_R_PASSED_NULL_PARAMETER
);
1269 buf_len
= EC_POINT_point2oct(a
->group
, a
->pub_key
,
1270 a
->conv_form
, NULL
, 0, NULL
);
1272 if (out
== NULL
|| buf_len
== 0)
1273 /* out == NULL => just return the length of the octet string */
1277 if ((*out
= OPENSSL_malloc(buf_len
)) == NULL
) {
1278 ECerr(EC_F_I2O_ECPUBLICKEY
, ERR_R_MALLOC_FAILURE
);
1283 if (!EC_POINT_point2oct(a
->group
, a
->pub_key
, a
->conv_form
,
1284 *out
, buf_len
, NULL
)) {
1285 ECerr(EC_F_I2O_ECPUBLICKEY
, ERR_R_EC_LIB
);
1297 ASN1_SEQUENCE(ECDSA_SIG
) = {
1298 ASN1_SIMPLE(ECDSA_SIG
, r
, CBIGNUM
),
1299 ASN1_SIMPLE(ECDSA_SIG
, s
, CBIGNUM
)
1300 } static_ASN1_SEQUENCE_END(ECDSA_SIG
)
1302 DECLARE_ASN1_FUNCTIONS_const(ECDSA_SIG
)
1303 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECDSA_SIG
, ECDSA_SIG
)
1304 IMPLEMENT_ASN1_FUNCTIONS_const(ECDSA_SIG
)
1306 void ECDSA_SIG_get0(BIGNUM
**pr
, BIGNUM
**ps
, ECDSA_SIG
*sig
)
1314 int ECDSA_size(const EC_KEY
*r
)
1318 BIGNUM
*order
= NULL
;
1319 unsigned char buf
[4];
1320 const EC_GROUP
*group
;
1324 group
= EC_KEY_get0_group(r
);
1328 if ((order
= BN_new()) == NULL
)
1330 if (!EC_GROUP_get_order(group
, order
, NULL
)) {
1331 BN_clear_free(order
);
1334 i
= BN_num_bits(order
);
1335 bs
.length
= (i
+ 7) / 8;
1337 bs
.type
= V_ASN1_INTEGER
;
1338 /* If the top bit is set the asn1 encoding is 1 larger. */
1341 i
= i2d_ASN1_INTEGER(&bs
, NULL
);
1342 i
+= i
; /* r and s */
1343 ret
= ASN1_object_size(1, i
, V_ASN1_SEQUENCE
);
1344 BN_clear_free(order
);