1 /* crypto/ec/ec_asn1.c */
3 * Written by Nils Larsch for the OpenSSL project.
5 /* ====================================================================
6 * Copyright (c) 2000-2003 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
61 #include <openssl/err.h>
62 #include <openssl/asn1t.h>
63 #include <openssl/objects.h>
65 int EC_GROUP_get_basis_type(const EC_GROUP *group)
69 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
70 NID_X9_62_characteristic_two_field)
71 /* everything else is currently not supported */
74 while (group->poly[i] != 0)
78 return NID_X9_62_ppBasis;
80 return NID_X9_62_tpBasis;
82 /* everything else is currently not supported */
86 int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
91 if (EC_GROUP_method_of(group)->group_set_curve !=
92 ec_GF2m_simple_group_set_curve || !((group->poly[0] != 0)
93 && (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_GROUP_method_of(group)->group_set_curve !=
113 ec_GF2m_simple_group_set_curve || !((group->poly[0] != 0)
114 && (group->poly[1] != 0)
115 && (group->poly[2] != 0)
116 && (group->poly[3] != 0)
117 && (group->poly[4] == 0))) {
118 ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS,
119 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
124 *k1 = group->poly[3];
126 *k2 = group->poly[2];
128 *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 } 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 } 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 } 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 } 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 } 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 } 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 } 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 if (field->fieldType != NULL)
320 ASN1_OBJECT_free(field->fieldType);
321 if (field->p.other != NULL)
322 ASN1_TYPE_free(field->p.other);
324 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
325 /* set OID for the field */
326 if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
327 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
331 if (nid == NID_X9_62_prime_field) {
332 if ((tmp = BN_new()) == NULL) {
333 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
336 /* the parameters are specified by the prime number p */
337 if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL)) {
338 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
341 /* set the prime number */
342 field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
343 if (field->p.prime == NULL) {
344 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
347 } else { /* nid == NID_X9_62_characteristic_two_field */
350 X9_62_CHARACTERISTIC_TWO *char_two;
352 field->p.char_two = X9_62_CHARACTERISTIC_TWO_new();
353 char_two = field->p.char_two;
355 if (char_two == NULL) {
356 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
360 char_two->m = (long)EC_GROUP_get_degree(group);
362 field_type = EC_GROUP_get_basis_type(group);
364 if (field_type == 0) {
365 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
368 /* set base type OID */
369 if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) {
370 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
374 if (field_type == NID_X9_62_tpBasis) {
377 if (!EC_GROUP_get_trinomial_basis(group, &k))
380 char_two->p.tpBasis = ASN1_INTEGER_new();
381 if (!char_two->p.tpBasis) {
382 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
385 if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) {
386 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
389 } else if (field_type == NID_X9_62_ppBasis) {
390 unsigned int k1, k2, k3;
392 if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))
395 char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
396 if (!char_two->p.ppBasis) {
397 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
402 char_two->p.ppBasis->k1 = (long)k1;
403 char_two->p.ppBasis->k2 = (long)k2;
404 char_two->p.ppBasis->k3 = (long)k3;
405 } else { /* field_type == NID_X9_62_onBasis */
407 /* for ONB the parameters are (asn1) NULL */
408 char_two->p.onBasis = ASN1_NULL_new();
409 if (!char_two->p.onBasis) {
410 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
423 static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
426 BIGNUM *tmp_1 = NULL, *tmp_2 = NULL;
427 unsigned char *buffer_1 = NULL, *buffer_2 = NULL,
428 *a_buf = NULL, *b_buf = NULL;
430 unsigned char char_zero = 0;
432 if (!group || !curve || !curve->a || !curve->b)
435 if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
436 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
440 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
443 if (nid == NID_X9_62_prime_field) {
444 if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL)) {
445 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
448 } else { /* nid == NID_X9_62_characteristic_two_field */
450 if (!EC_GROUP_get_curve_GF2m(group, NULL, tmp_1, tmp_2, NULL)) {
451 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
456 len_1 = (size_t)BN_num_bytes(tmp_1);
457 len_2 = (size_t)BN_num_bytes(tmp_2);
460 /* len_1 == 0 => a == 0 */
464 if ((buffer_1 = OPENSSL_malloc(len_1)) == NULL) {
465 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
468 if ((len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0) {
469 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
476 /* len_2 == 0 => b == 0 */
480 if ((buffer_2 = OPENSSL_malloc(len_2)) == NULL) {
481 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
484 if ((len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0) {
485 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
492 if (!M_ASN1_OCTET_STRING_set(curve->a, a_buf, len_1) ||
493 !M_ASN1_OCTET_STRING_set(curve->b, b_buf, len_2)) {
494 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
498 /* set the seed (optional) */
501 if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
502 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
505 curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
506 curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
507 if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
508 (int)group->seed_len)) {
509 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
514 ASN1_BIT_STRING_free(curve->seed);
522 OPENSSL_free(buffer_1);
524 OPENSSL_free(buffer_2);
532 static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group,
537 ECPARAMETERS *ret = NULL;
539 unsigned char *buffer = NULL;
540 const EC_POINT *point = NULL;
541 point_conversion_form_t form;
543 if ((tmp = BN_new()) == NULL) {
544 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
549 if ((ret = ECPARAMETERS_new()) == NULL) {
550 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
556 /* set the version (always one) */
557 ret->version = (long)0x1;
559 /* set the fieldID */
560 if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
561 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
566 if (!ec_asn1_group2curve(group, ret->curve)) {
567 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
571 /* set the base point */
572 if ((point = EC_GROUP_get0_generator(group)) == NULL) {
573 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, EC_R_UNDEFINED_GENERATOR);
577 form = EC_GROUP_get_point_conversion_form(group);
579 len = EC_POINT_point2oct(group, point, form, NULL, len, NULL);
581 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
584 if ((buffer = OPENSSL_malloc(len)) == NULL) {
585 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
588 if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL)) {
589 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
592 if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
593 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
596 if (!ASN1_OCTET_STRING_set(ret->base, buffer, len)) {
597 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
602 if (!EC_GROUP_get_order(group, tmp, NULL)) {
603 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
606 ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
607 if (ret->order == NULL) {
608 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
612 /* set the cofactor (optional) */
613 if (EC_GROUP_get_cofactor(group, tmp, NULL)) {
614 ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
615 if (ret->cofactor == NULL) {
616 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
625 ECPARAMETERS_free(ret);
631 OPENSSL_free(buffer);
635 ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *group,
636 ECPKPARAMETERS *params)
639 ECPKPARAMETERS *ret = params;
642 if ((ret = ECPKPARAMETERS_new()) == NULL) {
643 ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS, ERR_R_MALLOC_FAILURE);
647 if (ret->type == 0 && ret->value.named_curve)
648 ASN1_OBJECT_free(ret->value.named_curve);
649 else if (ret->type == 1 && ret->value.parameters)
650 ECPARAMETERS_free(ret->value.parameters);
653 if (EC_GROUP_get_asn1_flag(group)) {
655 * use the asn1 OID to describe the the elliptic curve parameters
657 tmp = EC_GROUP_get_curve_name(group);
660 if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
663 /* we don't kmow the nid => ERROR */
666 /* use the ECPARAMETERS structure */
668 if ((ret->value.parameters =
669 ec_asn1_group2parameters(group, NULL)) == NULL)
674 ECPKPARAMETERS_free(ret);
680 static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
683 EC_GROUP *ret = NULL;
684 BIGNUM *p = NULL, *a = NULL, *b = NULL;
685 EC_POINT *point = NULL;
688 if (!params->fieldID || !params->fieldID->fieldType ||
689 !params->fieldID->p.ptr) {
690 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
694 /* now extract the curve parameters a and b */
695 if (!params->curve || !params->curve->a ||
696 !params->curve->a->data || !params->curve->b ||
697 !params->curve->b->data) {
698 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
701 a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
703 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
706 b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
708 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
712 /* get the field parameters */
713 tmp = OBJ_obj2nid(params->fieldID->fieldType);
715 if (tmp == NID_X9_62_characteristic_two_field) {
716 X9_62_CHARACTERISTIC_TWO *char_two;
718 char_two = params->fieldID->p.char_two;
720 field_bits = char_two->m;
721 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
722 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
726 if ((p = BN_new()) == NULL) {
727 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
731 /* get the base type */
732 tmp = OBJ_obj2nid(char_two->type);
734 if (tmp == NID_X9_62_tpBasis) {
737 if (!char_two->p.tpBasis) {
738 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
742 tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
744 if (!(char_two->m > tmp_long && tmp_long > 0)) {
745 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
746 EC_R_INVALID_TRINOMIAL_BASIS);
750 /* create the polynomial */
751 if (!BN_set_bit(p, (int)char_two->m))
753 if (!BN_set_bit(p, (int)tmp_long))
755 if (!BN_set_bit(p, 0))
757 } else if (tmp == NID_X9_62_ppBasis) {
758 X9_62_PENTANOMIAL *penta;
760 penta = char_two->p.ppBasis;
762 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
767 (char_two->m > penta->k3 && penta->k3 > penta->k2
768 && penta->k2 > penta->k1 && penta->k1 > 0)) {
769 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
770 EC_R_INVALID_PENTANOMIAL_BASIS);
774 /* create the polynomial */
775 if (!BN_set_bit(p, (int)char_two->m))
777 if (!BN_set_bit(p, (int)penta->k1))
779 if (!BN_set_bit(p, (int)penta->k2))
781 if (!BN_set_bit(p, (int)penta->k3))
783 if (!BN_set_bit(p, 0))
785 } else if (tmp == NID_X9_62_onBasis) {
786 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED);
790 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
794 /* create the EC_GROUP structure */
795 ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
796 } else if (tmp == NID_X9_62_prime_field) {
797 /* we have a curve over a prime field */
798 /* extract the prime number */
799 if (!params->fieldID->p.prime) {
800 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
803 p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
805 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
809 if (BN_is_negative(p) || BN_is_zero(p)) {
810 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
814 field_bits = BN_num_bits(p);
815 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
816 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
820 /* create the EC_GROUP structure */
821 ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
823 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
828 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
832 /* extract seed (optional) */
833 if (params->curve->seed != NULL) {
834 if (ret->seed != NULL)
835 OPENSSL_free(ret->seed);
836 if (!(ret->seed = OPENSSL_malloc(params->curve->seed->length))) {
837 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
840 memcpy(ret->seed, params->curve->seed->data,
841 params->curve->seed->length);
842 ret->seed_len = params->curve->seed->length;
845 if (!params->order || !params->base || !params->base->data) {
846 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
850 if ((point = EC_POINT_new(ret)) == NULL)
853 /* set the point conversion form */
854 EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
855 (params->base->data[0] & ~0x01));
857 /* extract the ec point */
858 if (!EC_POINT_oct2point(ret, point, params->base->data,
859 params->base->length, NULL)) {
860 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
864 /* extract the order */
865 if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
866 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
869 if (BN_is_negative(a) || BN_is_zero(a)) {
870 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
873 if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
874 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
878 /* extract the cofactor (optional) */
879 if (params->cofactor == NULL) {
884 } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
885 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
888 /* set the generator, order and cofactor (if present) */
889 if (!EC_GROUP_set_generator(ret, point, a, b)) {
890 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
898 EC_GROUP_clear_free(ret);
909 EC_POINT_free(point);
913 EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *params)
915 EC_GROUP *ret = NULL;
918 if (params == NULL) {
919 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_MISSING_PARAMETERS);
923 if (params->type == 0) { /* the curve is given by an OID */
924 tmp = OBJ_obj2nid(params->value.named_curve);
925 if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
926 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,
927 EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
930 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
931 } else if (params->type == 1) { /* the parameters are given by a
932 * ECPARAMETERS structure */
933 ret = ec_asn1_parameters2group(params->value.parameters);
935 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB);
938 EC_GROUP_set_asn1_flag(ret, 0x0);
939 } else if (params->type == 2) { /* implicitlyCA */
942 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_ASN1_ERROR);
949 /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
951 EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
953 EC_GROUP *group = NULL;
954 ECPKPARAMETERS *params = NULL;
956 if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL) {
957 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
958 ECPKPARAMETERS_free(params);
962 if ((group = ec_asn1_pkparameters2group(params)) == NULL) {
963 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
968 EC_GROUP_clear_free(*a);
972 ECPKPARAMETERS_free(params);
976 int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
979 ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(a, NULL);
981 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
984 if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
985 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
986 ECPKPARAMETERS_free(tmp);
989 ECPKPARAMETERS_free(tmp);
993 /* some EC_KEY functions */
995 EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
999 EC_PRIVATEKEY *priv_key = NULL;
1001 if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
1002 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1006 if ((priv_key = d2i_EC_PRIVATEKEY(&priv_key, in, len)) == NULL) {
1007 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1008 EC_PRIVATEKEY_free(priv_key);
1012 if (a == NULL || *a == NULL) {
1013 if ((ret = EC_KEY_new()) == NULL) {
1014 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1020 if (priv_key->parameters) {
1022 EC_GROUP_clear_free(ret->group);
1023 ret->group = ec_asn1_pkparameters2group(priv_key->parameters);
1026 if (ret->group == NULL) {
1027 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1031 ret->version = priv_key->version;
1033 if (priv_key->privateKey) {
1034 ret->priv_key = BN_bin2bn(M_ASN1_STRING_data(priv_key->privateKey),
1035 M_ASN1_STRING_length(priv_key->privateKey),
1037 if (ret->priv_key == NULL) {
1038 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_BN_LIB);
1042 ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
1046 if (priv_key->publicKey) {
1047 const unsigned char *pub_oct;
1051 EC_POINT_clear_free(ret->pub_key);
1052 ret->pub_key = EC_POINT_new(ret->group);
1053 if (ret->pub_key == NULL) {
1054 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1057 pub_oct = M_ASN1_STRING_data(priv_key->publicKey);
1058 pub_oct_len = M_ASN1_STRING_length(priv_key->publicKey);
1059 /* save the point conversion form */
1060 ret->conv_form = (point_conversion_form_t) (pub_oct[0] & ~0x01);
1061 if (!EC_POINT_oct2point(ret->group, ret->pub_key,
1062 pub_oct, pub_oct_len, NULL)) {
1063 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1073 if (ret && (a == NULL || *a != ret))
1079 EC_PRIVATEKEY_free(priv_key);
1084 int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out)
1086 int ret = 0, ok = 0;
1087 unsigned char *buffer = NULL;
1088 size_t buf_len = 0, tmp_len;
1089 EC_PRIVATEKEY *priv_key = NULL;
1091 if (a == NULL || a->group == NULL || a->priv_key == NULL) {
1092 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
1096 if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
1097 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1101 priv_key->version = a->version;
1103 buf_len = (size_t)BN_num_bytes(a->priv_key);
1104 buffer = OPENSSL_malloc(buf_len);
1105 if (buffer == NULL) {
1106 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1110 if (!BN_bn2bin(a->priv_key, buffer)) {
1111 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_BN_LIB);
1115 if (!M_ASN1_OCTET_STRING_set(priv_key->privateKey, buffer, buf_len)) {
1116 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
1120 if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
1121 if ((priv_key->parameters =
1122 ec_asn1_group2pkparameters(a->group,
1123 priv_key->parameters)) == NULL) {
1124 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1129 if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
1130 priv_key->publicKey = M_ASN1_BIT_STRING_new();
1131 if (priv_key->publicKey == NULL) {
1132 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1136 tmp_len = EC_POINT_point2oct(a->group, a->pub_key,
1137 a->conv_form, NULL, 0, NULL);
1139 if (tmp_len > buf_len) {
1140 unsigned char *tmp_buffer = OPENSSL_realloc(buffer, tmp_len);
1142 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1145 buffer = tmp_buffer;
1149 if (!EC_POINT_point2oct(a->group, a->pub_key,
1150 a->conv_form, buffer, buf_len, NULL)) {
1151 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1155 priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
1156 priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
1157 if (!M_ASN1_BIT_STRING_set(priv_key->publicKey, buffer, buf_len)) {
1158 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
1163 if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
1164 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1170 OPENSSL_free(buffer);
1172 EC_PRIVATEKEY_free(priv_key);
1173 return (ok ? ret : 0);
1176 int i2d_ECParameters(EC_KEY *a, unsigned char **out)
1179 ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1182 return i2d_ECPKParameters(a->group, out);
1185 EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
1189 if (in == NULL || *in == NULL) {
1190 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1194 if (a == NULL || *a == NULL) {
1195 if ((ret = EC_KEY_new()) == NULL) {
1196 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
1202 if (!d2i_ECPKParameters(&ret->group, in, len)) {
1203 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
1204 if (a == NULL || *a != ret)
1215 EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
1219 if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
1221 * sorry, but a EC_GROUP-structur is necessary to set the public key
1223 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1227 if (ret->pub_key == NULL &&
1228 (ret->pub_key = EC_POINT_new(ret->group)) == NULL) {
1229 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1232 if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL)) {
1233 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
1236 /* save the point conversion form */
1237 ret->conv_form = (point_conversion_form_t) (*in[0] & ~0x01);
1242 int i2o_ECPublicKey(EC_KEY *a, unsigned char **out)
1248 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1252 buf_len = EC_POINT_point2oct(a->group, a->pub_key,
1253 a->conv_form, NULL, 0, NULL);
1255 if (out == NULL || buf_len == 0)
1256 /* out == NULL => just return the length of the octet string */
1260 if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
1261 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1266 if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
1267 *out, buf_len, NULL)) {
1268 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);