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 #ifndef OPENSSL_NO_EC2M
87 int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
92 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
93 NID_X9_62_characteristic_two_field
94 || !((group->poly[0] != 0) && (group->poly[1] != 0)
95 && (group->poly[2] == 0))) {
96 ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS,
97 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
107 int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
108 unsigned int *k2, unsigned int *k3)
113 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
114 NID_X9_62_characteristic_two_field
115 || !((group->poly[0] != 0) && (group->poly[1] != 0)
116 && (group->poly[2] != 0) && (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];
134 /* some structures needed for the asn1 encoding */
135 typedef struct x9_62_pentanomial_st {
141 typedef struct x9_62_characteristic_two_st {
146 /* NID_X9_62_onBasis */
148 /* NID_X9_62_tpBasis */
149 ASN1_INTEGER *tpBasis;
150 /* NID_X9_62_ppBasis */
151 X9_62_PENTANOMIAL *ppBasis;
155 } X9_62_CHARACTERISTIC_TWO;
157 typedef struct x9_62_fieldid_st {
158 ASN1_OBJECT *fieldType;
161 /* NID_X9_62_prime_field */
163 /* NID_X9_62_characteristic_two_field */
164 X9_62_CHARACTERISTIC_TWO *char_two;
170 typedef struct x9_62_curve_st {
171 ASN1_OCTET_STRING *a;
172 ASN1_OCTET_STRING *b;
173 ASN1_BIT_STRING *seed;
176 typedef struct ec_parameters_st {
178 X9_62_FIELDID *fieldID;
180 ASN1_OCTET_STRING *base;
182 ASN1_INTEGER *cofactor;
185 struct ecpk_parameters_st {
188 ASN1_OBJECT *named_curve;
189 ECPARAMETERS *parameters;
190 ASN1_NULL *implicitlyCA;
192 } /* ECPKPARAMETERS */ ;
194 /* SEC1 ECPrivateKey */
195 typedef struct ec_privatekey_st {
197 ASN1_OCTET_STRING *privateKey;
198 ECPKPARAMETERS *parameters;
199 ASN1_BIT_STRING *publicKey;
202 /* the OpenSSL ASN.1 definitions */
203 ASN1_SEQUENCE(X9_62_PENTANOMIAL) = {
204 ASN1_SIMPLE(X9_62_PENTANOMIAL, k1, LONG),
205 ASN1_SIMPLE(X9_62_PENTANOMIAL, k2, LONG),
206 ASN1_SIMPLE(X9_62_PENTANOMIAL, k3, LONG)
207 } ASN1_SEQUENCE_END(X9_62_PENTANOMIAL)
209 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
210 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
212 ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY);
214 ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = {
215 ADB_ENTRY(NID_X9_62_onBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.onBasis, ASN1_NULL)),
216 ADB_ENTRY(NID_X9_62_tpBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.tpBasis, ASN1_INTEGER)),
217 ADB_ENTRY(NID_X9_62_ppBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.ppBasis, X9_62_PENTANOMIAL))
218 } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL);
220 ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = {
221 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, m, LONG),
222 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT),
223 ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO)
224 } ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO)
226 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
227 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
229 ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY);
231 ASN1_ADB(X9_62_FIELDID) = {
232 ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)),
233 ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO))
234 } ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL);
236 ASN1_SEQUENCE(X9_62_FIELDID) = {
237 ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
238 ASN1_ADB_OBJECT(X9_62_FIELDID)
239 } ASN1_SEQUENCE_END(X9_62_FIELDID)
241 ASN1_SEQUENCE(X9_62_CURVE) = {
242 ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING),
243 ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING),
244 ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING)
245 } ASN1_SEQUENCE_END(X9_62_CURVE)
247 ASN1_SEQUENCE(ECPARAMETERS) = {
248 ASN1_SIMPLE(ECPARAMETERS, version, LONG),
249 ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID),
250 ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE),
251 ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING),
252 ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
253 ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER)
254 } ASN1_SEQUENCE_END(ECPARAMETERS)
256 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
257 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
259 ASN1_CHOICE(ECPKPARAMETERS) = {
260 ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT),
261 ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
262 ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL)
263 } ASN1_CHOICE_END(ECPKPARAMETERS)
265 DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
266 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPKPARAMETERS, ECPKPARAMETERS)
267 IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
269 ASN1_SEQUENCE(EC_PRIVATEKEY) = {
270 ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG),
271 ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
272 ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
273 ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
274 } ASN1_SEQUENCE_END(EC_PRIVATEKEY)
276 DECLARE_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
277 DECLARE_ASN1_ENCODE_FUNCTIONS_const(EC_PRIVATEKEY, EC_PRIVATEKEY)
278 IMPLEMENT_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
280 /* some declarations of internal function */
282 /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
283 static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *);
284 /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
285 static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
287 * ec_asn1_parameters2group() creates a EC_GROUP object from a ECPARAMETERS
290 static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *);
292 * ec_asn1_group2parameters() creates a ECPARAMETERS object from a EC_GROUP
295 static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *,
298 * ec_asn1_pkparameters2group() creates a EC_GROUP object from a
299 * ECPKPARAMETERS object
301 static EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *);
303 * ec_asn1_group2pkparameters() creates a ECPKPARAMETERS object from a
306 static ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *,
309 /* the function definitions */
311 static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field)
316 if (group == NULL || field == NULL)
319 /* clear the old values (if necessary) */
320 ASN1_OBJECT_free(field->fieldType);
321 ASN1_TYPE_free(field->p.other);
323 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
324 /* set OID for the field */
325 if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
326 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
330 if (nid == NID_X9_62_prime_field) {
331 if ((tmp = BN_new()) == NULL) {
332 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
335 /* the parameters are specified by the prime number p */
336 if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL)) {
337 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
340 /* set the prime number */
341 field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
342 if (field->p.prime == NULL) {
343 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
346 } else /* nid == NID_X9_62_characteristic_two_field */
347 #ifdef OPENSSL_NO_EC2M
349 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED);
355 X9_62_CHARACTERISTIC_TWO *char_two;
357 field->p.char_two = X9_62_CHARACTERISTIC_TWO_new();
358 char_two = field->p.char_two;
360 if (char_two == NULL) {
361 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
365 char_two->m = (long)EC_GROUP_get_degree(group);
367 field_type = EC_GROUP_get_basis_type(group);
369 if (field_type == 0) {
370 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
373 /* set base type OID */
374 if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) {
375 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
379 if (field_type == NID_X9_62_tpBasis) {
382 if (!EC_GROUP_get_trinomial_basis(group, &k))
385 char_two->p.tpBasis = ASN1_INTEGER_new();
386 if (!char_two->p.tpBasis) {
387 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
390 if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) {
391 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
394 } else if (field_type == NID_X9_62_ppBasis) {
395 unsigned int k1, k2, k3;
397 if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))
400 char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
401 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;
410 } else { /* field_type == NID_X9_62_onBasis */
412 /* for ONB the parameters are (asn1) NULL */
413 char_two->p.onBasis = ASN1_NULL_new();
414 if (!char_two->p.onBasis) {
415 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
429 static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
432 BIGNUM *tmp_1 = NULL, *tmp_2 = NULL;
433 unsigned char *buffer_1 = NULL, *buffer_2 = NULL,
434 *a_buf = NULL, *b_buf = NULL;
436 unsigned char char_zero = 0;
438 if (!group || !curve || !curve->a || !curve->b)
441 if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
442 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
446 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
449 if (nid == NID_X9_62_prime_field) {
450 if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL)) {
451 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
455 #ifndef OPENSSL_NO_EC2M
456 else { /* nid == NID_X9_62_characteristic_two_field */
458 if (!EC_GROUP_get_curve_GF2m(group, NULL, tmp_1, tmp_2, NULL)) {
459 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
464 len_1 = (size_t)BN_num_bytes(tmp_1);
465 len_2 = (size_t)BN_num_bytes(tmp_2);
468 /* len_1 == 0 => a == 0 */
472 if ((buffer_1 = OPENSSL_malloc(len_1)) == NULL) {
473 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
476 if ((len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0) {
477 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
484 /* len_2 == 0 => b == 0 */
488 if ((buffer_2 = OPENSSL_malloc(len_2)) == NULL) {
489 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
492 if ((len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0) {
493 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
500 if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len_1) ||
501 !ASN1_OCTET_STRING_set(curve->b, b_buf, len_2)) {
502 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
506 /* set the seed (optional) */
509 if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
510 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
513 curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
514 curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
515 if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
516 (int)group->seed_len)) {
517 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
521 ASN1_BIT_STRING_free(curve->seed);
528 OPENSSL_free(buffer_1);
530 OPENSSL_free(buffer_2);
538 static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group,
543 ECPARAMETERS *ret = NULL;
545 unsigned char *buffer = NULL;
546 const EC_POINT *point = NULL;
547 point_conversion_form_t form;
549 if ((tmp = BN_new()) == NULL) {
550 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
555 if ((ret = ECPARAMETERS_new()) == NULL) {
556 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
562 /* set the version (always one) */
563 ret->version = (long)0x1;
565 /* set the fieldID */
566 if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
567 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
572 if (!ec_asn1_group2curve(group, ret->curve)) {
573 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
577 /* set the base point */
578 if ((point = EC_GROUP_get0_generator(group)) == NULL) {
579 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, EC_R_UNDEFINED_GENERATOR);
583 form = EC_GROUP_get_point_conversion_form(group);
585 len = EC_POINT_point2oct(group, point, form, NULL, len, NULL);
587 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
590 if ((buffer = OPENSSL_malloc(len)) == NULL) {
591 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
594 if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL)) {
595 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
598 if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
599 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
602 if (!ASN1_OCTET_STRING_set(ret->base, buffer, len)) {
603 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
608 if (!EC_GROUP_get_order(group, tmp, NULL)) {
609 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
612 ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
613 if (ret->order == NULL) {
614 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
618 /* set the cofactor (optional) */
619 if (EC_GROUP_get_cofactor(group, tmp, NULL)) {
620 ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
621 if (ret->cofactor == NULL) {
622 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
631 ECPARAMETERS_free(ret);
637 OPENSSL_free(buffer);
641 ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *group,
642 ECPKPARAMETERS *params)
645 ECPKPARAMETERS *ret = params;
648 if ((ret = ECPKPARAMETERS_new()) == NULL) {
649 ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS, ERR_R_MALLOC_FAILURE);
654 ASN1_OBJECT_free(ret->value.named_curve);
655 else if (ret->type == 1 && ret->value.parameters)
656 ECPARAMETERS_free(ret->value.parameters);
659 if (EC_GROUP_get_asn1_flag(group)) {
661 * use the asn1 OID to describe the the elliptic curve parameters
663 tmp = EC_GROUP_get_curve_name(group);
666 if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
669 /* we don't kmow the nid => ERROR */
672 /* use the ECPARAMETERS structure */
674 if ((ret->value.parameters =
675 ec_asn1_group2parameters(group, NULL)) == NULL)
680 ECPKPARAMETERS_free(ret);
686 static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
689 EC_GROUP *ret = NULL;
690 BIGNUM *p = NULL, *a = NULL, *b = NULL;
691 EC_POINT *point = NULL;
694 if (!params->fieldID || !params->fieldID->fieldType ||
695 !params->fieldID->p.ptr) {
696 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
700 /* now extract the curve parameters a and b */
701 if (!params->curve || !params->curve->a ||
702 !params->curve->a->data || !params->curve->b ||
703 !params->curve->b->data) {
704 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
707 a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
709 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
712 b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
714 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
718 /* get the field parameters */
719 tmp = OBJ_obj2nid(params->fieldID->fieldType);
720 if (tmp == NID_X9_62_characteristic_two_field)
721 #ifdef OPENSSL_NO_EC2M
723 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_GF2M_NOT_SUPPORTED);
728 X9_62_CHARACTERISTIC_TWO *char_two;
730 char_two = params->fieldID->p.char_two;
732 field_bits = char_two->m;
733 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
734 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
738 if ((p = BN_new()) == NULL) {
739 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
743 /* get the base type */
744 tmp = OBJ_obj2nid(char_two->type);
746 if (tmp == NID_X9_62_tpBasis) {
749 if (!char_two->p.tpBasis) {
750 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
754 tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
756 if (!(char_two->m > tmp_long && tmp_long > 0)) {
757 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
758 EC_R_INVALID_TRINOMIAL_BASIS);
762 /* create the polynomial */
763 if (!BN_set_bit(p, (int)char_two->m))
765 if (!BN_set_bit(p, (int)tmp_long))
767 if (!BN_set_bit(p, 0))
769 } else if (tmp == NID_X9_62_ppBasis) {
770 X9_62_PENTANOMIAL *penta;
772 penta = char_two->p.ppBasis;
774 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
779 (char_two->m > penta->k3 && penta->k3 > penta->k2
780 && penta->k2 > penta->k1 && penta->k1 > 0)) {
781 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
782 EC_R_INVALID_PENTANOMIAL_BASIS);
786 /* create the polynomial */
787 if (!BN_set_bit(p, (int)char_two->m))
789 if (!BN_set_bit(p, (int)penta->k1))
791 if (!BN_set_bit(p, (int)penta->k2))
793 if (!BN_set_bit(p, (int)penta->k3))
795 if (!BN_set_bit(p, 0))
797 } else if (tmp == NID_X9_62_onBasis) {
798 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED);
802 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
806 /* create the EC_GROUP structure */
807 ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
810 else if (tmp == NID_X9_62_prime_field) {
811 /* we have a curve over a prime field */
812 /* extract the prime number */
813 if (!params->fieldID->p.prime) {
814 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
817 p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
819 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
823 if (BN_is_negative(p) || BN_is_zero(p)) {
824 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
828 field_bits = BN_num_bits(p);
829 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
830 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
834 /* create the EC_GROUP structure */
835 ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
837 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
842 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
846 /* extract seed (optional) */
847 if (params->curve->seed != NULL) {
848 if (ret->seed != NULL)
849 OPENSSL_free(ret->seed);
850 if (!(ret->seed = OPENSSL_malloc(params->curve->seed->length))) {
851 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
854 memcpy(ret->seed, params->curve->seed->data,
855 params->curve->seed->length);
856 ret->seed_len = params->curve->seed->length;
859 if (!params->order || !params->base || !params->base->data) {
860 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
864 if ((point = EC_POINT_new(ret)) == NULL)
867 /* set the point conversion form */
868 EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
869 (params->base->data[0] & ~0x01));
871 /* extract the ec point */
872 if (!EC_POINT_oct2point(ret, point, params->base->data,
873 params->base->length, NULL)) {
874 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
878 /* extract the order */
879 if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
880 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
883 if (BN_is_negative(a) || BN_is_zero(a)) {
884 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
887 if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
888 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
892 /* extract the cofactor (optional) */
893 if (params->cofactor == NULL) {
898 } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
899 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
902 /* set the generator, order and cofactor (if present) */
903 if (!EC_GROUP_set_generator(ret, point, a, b)) {
904 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
912 EC_GROUP_clear_free(ret);
922 EC_POINT_free(point);
926 EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *params)
928 EC_GROUP *ret = NULL;
931 if (params == NULL) {
932 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_MISSING_PARAMETERS);
936 if (params->type == 0) { /* the curve is given by an OID */
937 tmp = OBJ_obj2nid(params->value.named_curve);
938 if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
939 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,
940 EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
943 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
944 } else if (params->type == 1) { /* the parameters are given by a
945 * ECPARAMETERS structure */
946 ret = ec_asn1_parameters2group(params->value.parameters);
948 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB);
951 EC_GROUP_set_asn1_flag(ret, 0x0);
952 } else if (params->type == 2) { /* implicitlyCA */
955 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_ASN1_ERROR);
962 /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
964 EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
966 EC_GROUP *group = NULL;
967 ECPKPARAMETERS *params = NULL;
969 if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL) {
970 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
971 ECPKPARAMETERS_free(params);
975 if ((group = ec_asn1_pkparameters2group(params)) == NULL) {
976 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
977 ECPKPARAMETERS_free(params);
982 EC_GROUP_clear_free(*a);
986 ECPKPARAMETERS_free(params);
990 int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
993 ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(a, NULL);
995 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
998 if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
999 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
1000 ECPKPARAMETERS_free(tmp);
1003 ECPKPARAMETERS_free(tmp);
1007 /* some EC_KEY functions */
1009 EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
1013 EC_PRIVATEKEY *priv_key = NULL;
1015 if ((priv_key = d2i_EC_PRIVATEKEY(NULL, in, len)) == NULL) {
1016 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1020 if (a == NULL || *a == NULL) {
1021 if ((ret = EC_KEY_new()) == NULL) {
1022 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1028 if (priv_key->parameters) {
1029 EC_GROUP_clear_free(ret->group);
1030 ret->group = ec_asn1_pkparameters2group(priv_key->parameters);
1033 if (ret->group == NULL) {
1034 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1038 ret->version = priv_key->version;
1040 if (priv_key->privateKey) {
1041 ret->priv_key = BN_bin2bn(ASN1_STRING_data(priv_key->privateKey),
1042 ASN1_STRING_length(priv_key->privateKey),
1044 if (ret->priv_key == NULL) {
1045 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_BN_LIB);
1049 ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
1053 EC_POINT_clear_free(ret->pub_key);
1054 ret->pub_key = EC_POINT_new(ret->group);
1055 if (ret->pub_key == NULL) {
1056 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1060 if (priv_key->publicKey) {
1061 const unsigned char *pub_oct;
1064 pub_oct = ASN1_STRING_data(priv_key->publicKey);
1065 pub_oct_len = ASN1_STRING_length(priv_key->publicKey);
1067 * The first byte - point conversion form - must be present.
1069 if (pub_oct_len <= 0) {
1070 ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_BUFFER_TOO_SMALL);
1073 /* Save the point conversion form. */
1074 ret->conv_form = (point_conversion_form_t) (pub_oct[0] & ~0x01);
1075 if (!EC_POINT_oct2point(ret->group, ret->pub_key,
1076 pub_oct, (size_t)(pub_oct_len), NULL)) {
1077 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1082 (ret->group, ret->pub_key, ret->priv_key, NULL, NULL, NULL)) {
1083 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1086 /* Remember the original private-key-only encoding. */
1087 ret->enc_flag |= EC_PKEY_NO_PUBKEY;
1095 if (a == NULL || *a != ret)
1101 EC_PRIVATEKEY_free(priv_key);
1106 int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out)
1108 int ret = 0, ok = 0;
1109 unsigned char *buffer = NULL;
1110 size_t buf_len = 0, tmp_len, bn_len;
1111 EC_PRIVATEKEY *priv_key = NULL;
1113 if (a == NULL || a->group == NULL || a->priv_key == NULL ||
1114 (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
1115 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
1119 if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
1120 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1124 priv_key->version = a->version;
1126 bn_len = (size_t)BN_num_bytes(a->priv_key);
1128 /* Octetstring may need leading zeros if BN is to short */
1130 buf_len = (EC_GROUP_get_degree(a->group) + 7) / 8;
1132 if (bn_len > buf_len) {
1133 ECerr(EC_F_I2D_ECPRIVATEKEY, EC_R_BUFFER_TOO_SMALL);
1137 buffer = OPENSSL_malloc(buf_len);
1138 if (buffer == NULL) {
1139 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1143 if (!BN_bn2bin(a->priv_key, buffer + buf_len - bn_len)) {
1144 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_BN_LIB);
1148 if (buf_len - bn_len > 0) {
1149 memset(buffer, 0, buf_len - bn_len);
1152 if (!ASN1_OCTET_STRING_set(priv_key->privateKey, buffer, buf_len)) {
1153 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
1157 if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
1158 if ((priv_key->parameters =
1159 ec_asn1_group2pkparameters(a->group,
1160 priv_key->parameters)) == NULL) {
1161 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1166 if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
1167 priv_key->publicKey = ASN1_BIT_STRING_new();
1168 if (priv_key->publicKey == NULL) {
1169 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1173 tmp_len = EC_POINT_point2oct(a->group, a->pub_key,
1174 a->conv_form, NULL, 0, NULL);
1176 if (tmp_len > buf_len) {
1177 unsigned char *tmp_buffer = OPENSSL_realloc(buffer, tmp_len);
1179 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1182 buffer = tmp_buffer;
1186 if (!EC_POINT_point2oct(a->group, a->pub_key,
1187 a->conv_form, buffer, buf_len, NULL)) {
1188 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1192 priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
1193 priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
1194 if (!ASN1_BIT_STRING_set(priv_key->publicKey, buffer, buf_len)) {
1195 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
1200 if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
1201 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1207 OPENSSL_free(buffer);
1209 EC_PRIVATEKEY_free(priv_key);
1210 return (ok ? ret : 0);
1213 int i2d_ECParameters(EC_KEY *a, unsigned char **out)
1216 ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1219 return i2d_ECPKParameters(a->group, out);
1222 EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
1226 if (in == NULL || *in == NULL) {
1227 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1231 if (a == NULL || *a == NULL) {
1232 if ((ret = EC_KEY_new()) == NULL) {
1233 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
1239 if (!d2i_ECPKParameters(&ret->group, in, len)) {
1240 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
1241 if (a == NULL || *a != ret)
1252 EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
1256 if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
1258 * sorry, but a EC_GROUP-structur is necessary to set the public key
1260 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1264 if (ret->pub_key == NULL &&
1265 (ret->pub_key = EC_POINT_new(ret->group)) == NULL) {
1266 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1269 if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL)) {
1270 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
1273 /* save the point conversion form */
1274 ret->conv_form = (point_conversion_form_t) (*in[0] & ~0x01);
1279 int i2o_ECPublicKey(EC_KEY *a, unsigned char **out)
1285 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1289 buf_len = EC_POINT_point2oct(a->group, a->pub_key,
1290 a->conv_form, NULL, 0, NULL);
1292 if (out == NULL || buf_len == 0)
1293 /* out == NULL => just return the length of the octet string */
1297 if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
1298 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1303 if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
1304 *out, buf_len, NULL)) {
1305 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);