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>
66 int EC_GROUP_get_basis_type(const EC_GROUP *group)
70 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
71 NID_X9_62_characteristic_two_field)
72 /* everything else is currently not supported */
75 while (group->poly[i] != 0)
79 return NID_X9_62_ppBasis;
81 return NID_X9_62_tpBasis;
83 /* everything else is currently not supported */
87 int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
92 if (EC_GROUP_method_of(group)->group_set_curve != ec_GF2m_simple_group_set_curve
93 || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] == 0)))
95 ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
105 int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
106 unsigned int *k2, unsigned int *k3)
111 if (EC_GROUP_method_of(group)->group_set_curve != ec_GF2m_simple_group_set_curve
112 || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] != 0) && (group->poly[3] != 0) && (group->poly[4] == 0)))
114 ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
119 *k1 = group->poly[3];
121 *k2 = group->poly[2];
123 *k3 = group->poly[1];
130 /* some structures needed for the asn1 encoding */
131 typedef struct x9_62_pentanomial_st {
137 typedef struct x9_62_characteristic_two_st {
142 /* NID_X9_62_onBasis */
144 /* NID_X9_62_tpBasis */
145 ASN1_INTEGER *tpBasis;
146 /* NID_X9_62_ppBasis */
147 X9_62_PENTANOMIAL *ppBasis;
151 } X9_62_CHARACTERISTIC_TWO;
153 typedef struct x9_62_fieldid_st {
154 ASN1_OBJECT *fieldType;
157 /* NID_X9_62_prime_field */
159 /* NID_X9_62_characteristic_two_field */
160 X9_62_CHARACTERISTIC_TWO *char_two;
166 typedef struct x9_62_curve_st {
167 ASN1_OCTET_STRING *a;
168 ASN1_OCTET_STRING *b;
169 ASN1_BIT_STRING *seed;
172 typedef struct ec_parameters_st {
174 X9_62_FIELDID *fieldID;
176 ASN1_OCTET_STRING *base;
178 ASN1_INTEGER *cofactor;
181 struct ecpk_parameters_st {
184 ASN1_OBJECT *named_curve;
185 ECPARAMETERS *parameters;
186 ASN1_NULL *implicitlyCA;
188 }/* ECPKPARAMETERS */;
190 /* SEC1 ECPrivateKey */
191 typedef struct ec_privatekey_st {
193 ASN1_OCTET_STRING *privateKey;
194 ECPKPARAMETERS *parameters;
195 ASN1_BIT_STRING *publicKey;
198 /* the OpenSSL ASN.1 definitions */
199 ASN1_SEQUENCE(X9_62_PENTANOMIAL) = {
200 ASN1_SIMPLE(X9_62_PENTANOMIAL, k1, LONG),
201 ASN1_SIMPLE(X9_62_PENTANOMIAL, k2, LONG),
202 ASN1_SIMPLE(X9_62_PENTANOMIAL, k3, LONG)
203 } ASN1_SEQUENCE_END(X9_62_PENTANOMIAL)
205 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
206 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
208 ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY);
210 ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = {
211 ADB_ENTRY(NID_X9_62_onBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.onBasis, ASN1_NULL)),
212 ADB_ENTRY(NID_X9_62_tpBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.tpBasis, ASN1_INTEGER)),
213 ADB_ENTRY(NID_X9_62_ppBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.ppBasis, X9_62_PENTANOMIAL))
214 } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL);
216 ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = {
217 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, m, LONG),
218 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT),
219 ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO)
220 } ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO)
222 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
223 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
225 ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY);
227 ASN1_ADB(X9_62_FIELDID) = {
228 ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)),
229 ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO))
230 } ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL);
232 ASN1_SEQUENCE(X9_62_FIELDID) = {
233 ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
234 ASN1_ADB_OBJECT(X9_62_FIELDID)
235 } ASN1_SEQUENCE_END(X9_62_FIELDID)
237 ASN1_SEQUENCE(X9_62_CURVE) = {
238 ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING),
239 ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING),
240 ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING)
241 } ASN1_SEQUENCE_END(X9_62_CURVE)
243 ASN1_SEQUENCE(ECPARAMETERS) = {
244 ASN1_SIMPLE(ECPARAMETERS, version, LONG),
245 ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID),
246 ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE),
247 ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING),
248 ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
249 ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER)
250 } ASN1_SEQUENCE_END(ECPARAMETERS)
252 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
253 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
255 ASN1_CHOICE(ECPKPARAMETERS) = {
256 ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT),
257 ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
258 ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL)
259 } ASN1_CHOICE_END(ECPKPARAMETERS)
261 DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
262 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPKPARAMETERS, ECPKPARAMETERS)
263 IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
265 ASN1_SEQUENCE(EC_PRIVATEKEY) = {
266 ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG),
267 ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
268 ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
269 ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
270 } ASN1_SEQUENCE_END(EC_PRIVATEKEY)
272 DECLARE_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
273 DECLARE_ASN1_ENCODE_FUNCTIONS_const(EC_PRIVATEKEY, EC_PRIVATEKEY)
274 IMPLEMENT_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
276 /* some declarations of internal function */
278 /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
279 static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *);
280 /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
281 static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
282 /* ec_asn1_parameters2group() creates a EC_GROUP object from a
283 * ECPARAMETERS object */
284 static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *);
285 /* ec_asn1_group2parameters() creates a ECPARAMETERS object from a
287 static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *,ECPARAMETERS *);
288 /* ec_asn1_pkparameters2group() creates a EC_GROUP object from a
289 * ECPKPARAMETERS object */
290 static EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *);
291 /* ec_asn1_group2pkparameters() creates a ECPKPARAMETERS object from a
293 static ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *,
297 /* the function definitions */
299 static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field)
304 if (group == NULL || field == NULL)
307 /* clear the old values (if necessary) */
308 if (field->fieldType != NULL)
309 ASN1_OBJECT_free(field->fieldType);
310 if (field->p.other != NULL)
311 ASN1_TYPE_free(field->p.other);
313 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
314 /* set OID for the field */
315 if ((field->fieldType = OBJ_nid2obj(nid)) == NULL)
317 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
321 if (nid == NID_X9_62_prime_field)
323 if ((tmp = BN_new()) == NULL)
325 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
328 /* the parameters are specified by the prime number p */
329 if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL))
331 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
334 /* set the prime number */
335 field->p.prime = BN_to_ASN1_INTEGER(tmp,NULL);
336 if (field->p.prime == NULL)
338 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
342 else /* nid == NID_X9_62_characteristic_two_field */
345 X9_62_CHARACTERISTIC_TWO *char_two;
347 field->p.char_two = X9_62_CHARACTERISTIC_TWO_new();
348 char_two = field->p.char_two;
350 if (char_two == NULL)
352 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
356 char_two->m = (long)EC_GROUP_get_degree(group);
358 field_type = EC_GROUP_get_basis_type(group);
362 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
365 /* set base type OID */
366 if ((char_two->type = OBJ_nid2obj(field_type)) == NULL)
368 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
372 if (field_type == NID_X9_62_tpBasis)
376 if (!EC_GROUP_get_trinomial_basis(group, &k))
379 char_two->p.tpBasis = ASN1_INTEGER_new();
380 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))
387 ECerr(EC_F_EC_ASN1_GROUP2FIELDID,
392 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)
402 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
407 char_two->p.ppBasis->k1 = (long)k1;
408 char_two->p.ppBasis->k2 = (long)k2;
409 char_two->p.ppBasis->k3 = (long)k3;
411 else /* field_type == NID_X9_62_onBasis */
413 /* for ONB the parameters are (asn1) NULL */
414 char_two->p.onBasis = ASN1_NULL_new();
415 if (!char_two->p.onBasis)
417 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
430 static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
433 BIGNUM *tmp_1=NULL, *tmp_2=NULL;
434 unsigned char *buffer_1=NULL, *buffer_2=NULL,
435 *a_buf=NULL, *b_buf=NULL;
437 unsigned char char_zero = 0;
439 if (!group || !curve || !curve->a || !curve->b)
442 if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL)
444 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
448 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
451 if (nid == NID_X9_62_prime_field)
453 if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL))
455 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
459 else /* nid == NID_X9_62_characteristic_two_field */
461 if (!EC_GROUP_get_curve_GF2m(group, NULL, tmp_1, tmp_2, NULL))
463 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
468 len_1 = (size_t)BN_num_bytes(tmp_1);
469 len_2 = (size_t)BN_num_bytes(tmp_2);
473 /* len_1 == 0 => a == 0 */
479 if ((buffer_1 = OPENSSL_malloc(len_1)) == NULL)
481 ECerr(EC_F_EC_ASN1_GROUP2CURVE,
482 ERR_R_MALLOC_FAILURE);
485 if ( (len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0)
487 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
495 /* len_2 == 0 => b == 0 */
501 if ((buffer_2 = OPENSSL_malloc(len_2)) == NULL)
503 ECerr(EC_F_EC_ASN1_GROUP2CURVE,
504 ERR_R_MALLOC_FAILURE);
507 if ( (len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0)
509 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
516 if (!M_ASN1_OCTET_STRING_set(curve->a, a_buf, len_1) ||
517 !M_ASN1_OCTET_STRING_set(curve->b, b_buf, len_2))
519 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
523 /* set the seed (optional) */
527 if ((curve->seed = ASN1_BIT_STRING_new()) == NULL)
529 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
532 if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
533 (int)group->seed_len))
535 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
543 ASN1_BIT_STRING_free(curve->seed);
551 OPENSSL_free(buffer_1);
553 OPENSSL_free(buffer_2);
561 static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group,
566 ECPARAMETERS *ret=NULL;
568 unsigned char *buffer=NULL;
569 const EC_POINT *point=NULL;
570 point_conversion_form_t form;
572 if ((tmp = BN_new()) == NULL)
574 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
580 if ((ret = ECPARAMETERS_new()) == NULL)
582 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS,
583 ERR_R_MALLOC_FAILURE);
590 /* set the version (always one) */
591 ret->version = (long)0x1;
593 /* set the fieldID */
594 if (!ec_asn1_group2fieldid(group, ret->fieldID))
596 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
601 if (!ec_asn1_group2curve(group, ret->curve))
603 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
607 /* set the base point */
608 if ((point = EC_GROUP_get0_generator(group)) == NULL)
610 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, EC_R_UNDEFINED_GENERATOR);
614 form = EC_GROUP_get_point_conversion_form(group);
616 len = EC_POINT_point2oct(group, point, form, NULL, len, NULL);
619 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
622 if ((buffer = OPENSSL_malloc(len)) == NULL)
624 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
627 if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL))
629 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
632 if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL)
634 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
637 if (!ASN1_OCTET_STRING_set(ret->base, buffer, len))
639 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
644 if (!EC_GROUP_get_order(group, tmp, NULL))
646 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
649 ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
650 if (ret->order == NULL)
652 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
656 /* set the cofactor (optional) */
657 if (EC_GROUP_get_cofactor(group, tmp, NULL))
659 ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
660 if (ret->cofactor == NULL)
662 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
672 ECPARAMETERS_free(ret);
678 OPENSSL_free(buffer);
682 ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *group,
683 ECPKPARAMETERS *params)
686 ECPKPARAMETERS *ret = params;
690 if ((ret = ECPKPARAMETERS_new()) == NULL)
692 ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS,
693 ERR_R_MALLOC_FAILURE);
699 if (ret->type == 0 && ret->value.named_curve)
700 ASN1_OBJECT_free(ret->value.named_curve);
701 else if (ret->type == 1 && ret->value.parameters)
702 ECPARAMETERS_free(ret->value.parameters);
705 if (EC_GROUP_get_asn1_flag(group))
707 /* use the asn1 OID to describe the
708 * the elliptic curve parameters
710 tmp = EC_GROUP_get_curve_name(group);
714 if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
718 /* we don't kmow the nid => ERROR */
723 /* use the ECPARAMETERS structure */
725 if ((ret->value.parameters = ec_asn1_group2parameters(
726 group, NULL)) == NULL)
732 ECPKPARAMETERS_free(ret);
738 static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
741 EC_GROUP *ret = NULL;
742 BIGNUM *p = NULL, *a = NULL, *b = NULL;
743 EC_POINT *point=NULL;
745 if (!params->fieldID || !params->fieldID->fieldType ||
746 !params->fieldID->p.ptr)
748 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
752 /* now extract the curve parameters a and b */
753 if (!params->curve || !params->curve->a ||
754 !params->curve->a->data || !params->curve->b ||
755 !params->curve->b->data)
757 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
760 a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
763 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
766 b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
769 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
773 /* get the field parameters */
774 tmp = OBJ_obj2nid(params->fieldID->fieldType);
776 if (tmp == NID_X9_62_characteristic_two_field)
778 X9_62_CHARACTERISTIC_TWO *char_two;
780 char_two = params->fieldID->p.char_two;
782 if ((p = BN_new()) == NULL)
784 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
788 /* get the base type */
789 tmp = OBJ_obj2nid(char_two->type);
791 if (tmp == NID_X9_62_tpBasis)
795 if (!char_two->p.tpBasis)
797 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
801 tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
802 /* create the polynomial */
803 if (!BN_set_bit(p, (int)char_two->m))
805 if (!BN_set_bit(p, (int)tmp_long))
807 if (!BN_set_bit(p, 0))
810 else if (tmp == NID_X9_62_ppBasis)
812 X9_62_PENTANOMIAL *penta;
814 penta = char_two->p.ppBasis;
817 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
820 /* create the polynomial */
821 if (!BN_set_bit(p, (int)char_two->m)) goto err;
822 if (!BN_set_bit(p, (int)penta->k1)) goto err;
823 if (!BN_set_bit(p, (int)penta->k2)) goto err;
824 if (!BN_set_bit(p, (int)penta->k3)) goto err;
825 if (!BN_set_bit(p, 0)) goto err;
827 else if (tmp == NID_X9_62_onBasis)
829 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED);
834 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
838 /* create the EC_GROUP structure */
839 ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
842 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
846 else if (tmp == NID_X9_62_prime_field)
848 /* we have a curve over a prime field */
849 /* extract the prime number */
850 if (!params->fieldID->p.prime)
852 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
855 p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
858 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
861 /* create the EC_GROUP structure */
862 ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
865 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
870 /* extract seed (optional) */
871 if (params->curve->seed != NULL)
873 if (ret->seed != NULL)
874 OPENSSL_free(ret->seed);
875 if (!(ret->seed = OPENSSL_malloc(params->curve->seed->length)))
877 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
878 ERR_R_MALLOC_FAILURE);
881 memcpy(ret->seed, params->curve->seed->data,
882 params->curve->seed->length);
883 ret->seed_len = params->curve->seed->length;
886 if (!params->order || !params->base || !params->base->data)
888 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
892 if ((point = EC_POINT_new(ret)) == NULL) goto err;
894 /* set the point conversion form */
895 EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
896 (params->base->data[0] & ~0x01));
898 /* extract the ec point */
899 if (!EC_POINT_oct2point(ret, point, params->base->data,
900 params->base->length, NULL))
902 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
906 /* extract the order */
907 if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL)
909 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
913 /* extract the cofactor (optional) */
914 if (params->cofactor == NULL)
923 if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL)
925 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
928 /* set the generator, order and cofactor (if present) */
929 if (!EC_GROUP_set_generator(ret, point, a, b))
931 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
940 EC_GROUP_clear_free(ret);
951 EC_POINT_free(point);
955 EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *params)
962 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,
963 EC_R_MISSING_PARAMETERS);
967 if (params->type == 0)
968 { /* the curve is given by an OID */
969 tmp = OBJ_obj2nid(params->value.named_curve);
970 if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL)
972 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,
973 EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
976 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
978 else if (params->type == 1)
979 { /* the parameters are given by a ECPARAMETERS
981 ret = ec_asn1_parameters2group(params->value.parameters);
984 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB);
987 EC_GROUP_set_asn1_flag(ret, 0x0);
989 else if (params->type == 2)
995 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_ASN1_ERROR);
1002 /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
1004 EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
1006 EC_GROUP *group = NULL;
1007 ECPKPARAMETERS *params = NULL;
1009 if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL)
1011 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
1012 ECPKPARAMETERS_free(params);
1016 if ((group = ec_asn1_pkparameters2group(params)) == NULL)
1018 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
1024 EC_GROUP_clear_free(*a);
1028 ECPKPARAMETERS_free(params);
1032 int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
1035 ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(a, NULL);
1038 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
1041 if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0)
1043 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
1044 ECPKPARAMETERS_free(tmp);
1047 ECPKPARAMETERS_free(tmp);
1051 /* some EC_KEY functions */
1053 EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
1057 EC_PRIVATEKEY *priv_key=NULL;
1059 if ((priv_key = EC_PRIVATEKEY_new()) == NULL)
1061 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1065 if ((priv_key = d2i_EC_PRIVATEKEY(&priv_key, in, len)) == NULL)
1067 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1068 EC_PRIVATEKEY_free(priv_key);
1072 if (a == NULL || *a == NULL)
1074 if ((ret = EC_KEY_new()) == NULL)
1076 ECerr(EC_F_D2I_ECPRIVATEKEY,
1077 ERR_R_MALLOC_FAILURE);
1086 if (priv_key->parameters)
1089 EC_GROUP_clear_free(ret->group);
1090 ret->group = ec_asn1_pkparameters2group(priv_key->parameters);
1093 if (ret->group == NULL)
1095 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1099 ret->version = priv_key->version;
1101 if (priv_key->privateKey)
1103 ret->priv_key = BN_bin2bn(
1104 M_ASN1_STRING_data(priv_key->privateKey),
1105 M_ASN1_STRING_length(priv_key->privateKey),
1107 if (ret->priv_key == NULL)
1109 ECerr(EC_F_D2I_ECPRIVATEKEY,
1116 ECerr(EC_F_D2I_ECPRIVATEKEY,
1117 EC_R_MISSING_PRIVATE_KEY);
1121 if (priv_key->publicKey)
1123 const unsigned char *pub_oct;
1127 EC_POINT_clear_free(ret->pub_key);
1128 ret->pub_key = EC_POINT_new(ret->group);
1129 if (ret->pub_key == NULL)
1131 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1134 pub_oct = M_ASN1_STRING_data(priv_key->publicKey);
1135 pub_oct_len = M_ASN1_STRING_length(priv_key->publicKey);
1136 /* save the point conversion form */
1137 ret->conv_form = (point_conversion_form_t)(pub_oct[0] & ~0x01);
1138 if (!EC_POINT_oct2point(ret->group, ret->pub_key,
1139 pub_oct, pub_oct_len, NULL))
1141 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1156 EC_PRIVATEKEY_free(priv_key);
1161 int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out)
1164 unsigned char *buffer=NULL;
1165 size_t buf_len=0, tmp_len;
1166 EC_PRIVATEKEY *priv_key=NULL;
1168 if (a == NULL || a->group == NULL || a->priv_key == NULL)
1170 ECerr(EC_F_I2D_ECPRIVATEKEY,
1171 ERR_R_PASSED_NULL_PARAMETER);
1175 if ((priv_key = EC_PRIVATEKEY_new()) == NULL)
1177 ECerr(EC_F_I2D_ECPRIVATEKEY,
1178 ERR_R_MALLOC_FAILURE);
1182 priv_key->version = a->version;
1184 buf_len = (size_t)BN_num_bytes(a->priv_key);
1185 buffer = OPENSSL_malloc(buf_len);
1188 ECerr(EC_F_I2D_ECPRIVATEKEY,
1189 ERR_R_MALLOC_FAILURE);
1193 if (!BN_bn2bin(a->priv_key, buffer))
1195 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_BN_LIB);
1199 if (!M_ASN1_OCTET_STRING_set(priv_key->privateKey, buffer, buf_len))
1201 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
1205 if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS))
1207 if ((priv_key->parameters = ec_asn1_group2pkparameters(
1208 a->group, priv_key->parameters)) == NULL)
1210 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1215 if (!(a->enc_flag & EC_PKEY_NO_PUBKEY))
1217 priv_key->publicKey = M_ASN1_BIT_STRING_new();
1218 if (priv_key->publicKey == NULL)
1220 ECerr(EC_F_I2D_ECPRIVATEKEY,
1221 ERR_R_MALLOC_FAILURE);
1225 tmp_len = EC_POINT_point2oct(a->group, a->pub_key,
1226 a->conv_form, NULL, 0, NULL);
1228 if (tmp_len > buf_len)
1230 unsigned char *tmp_buffer = OPENSSL_realloc(buffer, tmp_len);
1233 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1236 buffer = tmp_buffer;
1240 if (!EC_POINT_point2oct(a->group, a->pub_key,
1241 a->conv_form, buffer, buf_len, NULL))
1243 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1247 if (!M_ASN1_BIT_STRING_set(priv_key->publicKey, buffer,
1250 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
1255 if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0)
1257 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1263 OPENSSL_free(buffer);
1265 EC_PRIVATEKEY_free(priv_key);
1269 int i2d_ECParameters(EC_KEY *a, unsigned char **out)
1273 ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1276 return i2d_ECPKParameters(a->group, out);
1279 EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
1283 if (in == NULL || *in == NULL)
1285 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1289 if (a == NULL || *a == NULL)
1291 if ((ret = EC_KEY_new()) == NULL)
1293 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
1302 if (!d2i_ECPKParameters(&ret->group, in, len))
1304 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
1311 EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
1315 if (a == NULL || (*a) == NULL || (*a)->group == NULL)
1317 /* sorry, but a EC_GROUP-structur is necessary
1318 * to set the public key */
1319 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1323 if (ret->pub_key == NULL &&
1324 (ret->pub_key = EC_POINT_new(ret->group)) == NULL)
1326 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1329 if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL))
1331 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
1334 /* save the point conversion form */
1335 ret->conv_form = (point_conversion_form_t)(*in[0] & ~0x01);
1340 int i2o_ECPublicKey(EC_KEY *a, unsigned char **out)
1347 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1351 buf_len = EC_POINT_point2oct(a->group, a->pub_key,
1352 a->conv_form, NULL, 0, NULL);
1354 if (out == NULL || buf_len == 0)
1355 /* out == NULL => just return the length of the octet string */
1360 if ((*out = OPENSSL_malloc(buf_len)) == NULL)
1362 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1367 if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
1368 *out, buf_len, NULL))
1370 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);