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 #define OSSL_NELEM(x) (sizeof(x)/sizeof(x[0]))
67 int EC_GROUP_get_basis_type(const EC_GROUP *group)
71 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
72 NID_X9_62_characteristic_two_field)
73 /* everything else is currently not supported */
76 /* Find the last non-zero element of group->poly[] */
78 i < (int)OSSL_NELEM(group->poly) && group->poly[i] != 0;
83 return NID_X9_62_ppBasis;
85 return NID_X9_62_tpBasis;
87 /* everything else is currently not supported */
91 #ifndef OPENSSL_NO_EC2M
92 int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
97 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
98 NID_X9_62_characteristic_two_field
99 || !((group->poly[0] != 0) && (group->poly[1] != 0)
100 && (group->poly[2] == 0))) {
101 ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS,
102 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
112 int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
113 unsigned int *k2, unsigned int *k3)
118 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
119 NID_X9_62_characteristic_two_field
120 || !((group->poly[0] != 0) && (group->poly[1] != 0)
121 && (group->poly[2] != 0) && (group->poly[3] != 0)
122 && (group->poly[4] == 0))) {
123 ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS,
124 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
129 *k1 = group->poly[3];
131 *k2 = group->poly[2];
133 *k3 = group->poly[1];
139 /* some structures needed for the asn1 encoding */
140 typedef struct x9_62_pentanomial_st {
146 typedef struct x9_62_characteristic_two_st {
151 /* NID_X9_62_onBasis */
153 /* NID_X9_62_tpBasis */
154 ASN1_INTEGER *tpBasis;
155 /* NID_X9_62_ppBasis */
156 X9_62_PENTANOMIAL *ppBasis;
160 } X9_62_CHARACTERISTIC_TWO;
162 typedef struct x9_62_fieldid_st {
163 ASN1_OBJECT *fieldType;
166 /* NID_X9_62_prime_field */
168 /* NID_X9_62_characteristic_two_field */
169 X9_62_CHARACTERISTIC_TWO *char_two;
175 typedef struct x9_62_curve_st {
176 ASN1_OCTET_STRING *a;
177 ASN1_OCTET_STRING *b;
178 ASN1_BIT_STRING *seed;
181 typedef struct ec_parameters_st {
183 X9_62_FIELDID *fieldID;
185 ASN1_OCTET_STRING *base;
187 ASN1_INTEGER *cofactor;
190 struct ecpk_parameters_st {
193 ASN1_OBJECT *named_curve;
194 ECPARAMETERS *parameters;
195 ASN1_NULL *implicitlyCA;
197 } /* ECPKPARAMETERS */ ;
199 /* SEC1 ECPrivateKey */
200 typedef struct ec_privatekey_st {
202 ASN1_OCTET_STRING *privateKey;
203 ECPKPARAMETERS *parameters;
204 ASN1_BIT_STRING *publicKey;
207 /* the OpenSSL ASN.1 definitions */
208 ASN1_SEQUENCE(X9_62_PENTANOMIAL) = {
209 ASN1_SIMPLE(X9_62_PENTANOMIAL, k1, LONG),
210 ASN1_SIMPLE(X9_62_PENTANOMIAL, k2, LONG),
211 ASN1_SIMPLE(X9_62_PENTANOMIAL, k3, LONG)
212 } ASN1_SEQUENCE_END(X9_62_PENTANOMIAL)
214 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
215 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
217 ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY);
219 ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = {
220 ADB_ENTRY(NID_X9_62_onBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.onBasis, ASN1_NULL)),
221 ADB_ENTRY(NID_X9_62_tpBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.tpBasis, ASN1_INTEGER)),
222 ADB_ENTRY(NID_X9_62_ppBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.ppBasis, X9_62_PENTANOMIAL))
223 } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL);
225 ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = {
226 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, m, LONG),
227 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT),
228 ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO)
229 } ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO)
231 DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
232 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
234 ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY);
236 ASN1_ADB(X9_62_FIELDID) = {
237 ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)),
238 ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO))
239 } ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL);
241 ASN1_SEQUENCE(X9_62_FIELDID) = {
242 ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
243 ASN1_ADB_OBJECT(X9_62_FIELDID)
244 } ASN1_SEQUENCE_END(X9_62_FIELDID)
246 ASN1_SEQUENCE(X9_62_CURVE) = {
247 ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING),
248 ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING),
249 ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING)
250 } ASN1_SEQUENCE_END(X9_62_CURVE)
252 ASN1_SEQUENCE(ECPARAMETERS) = {
253 ASN1_SIMPLE(ECPARAMETERS, version, LONG),
254 ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID),
255 ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE),
256 ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING),
257 ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
258 ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER)
259 } ASN1_SEQUENCE_END(ECPARAMETERS)
261 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
262 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
264 ASN1_CHOICE(ECPKPARAMETERS) = {
265 ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT),
266 ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
267 ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL)
268 } ASN1_CHOICE_END(ECPKPARAMETERS)
270 DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
271 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPKPARAMETERS, ECPKPARAMETERS)
272 IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
274 ASN1_SEQUENCE(EC_PRIVATEKEY) = {
275 ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG),
276 ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
277 ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
278 ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
279 } ASN1_SEQUENCE_END(EC_PRIVATEKEY)
281 DECLARE_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
282 DECLARE_ASN1_ENCODE_FUNCTIONS_const(EC_PRIVATEKEY, EC_PRIVATEKEY)
283 IMPLEMENT_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
285 /* some declarations of internal function */
287 /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
288 static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *);
289 /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
290 static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
292 * ec_asn1_parameters2group() creates a EC_GROUP object from a ECPARAMETERS
295 static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *);
297 * ec_asn1_group2parameters() creates a ECPARAMETERS object from a EC_GROUP
300 static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *,
303 * ec_asn1_pkparameters2group() creates a EC_GROUP object from a
304 * ECPKPARAMETERS object
306 static EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *);
308 * ec_asn1_group2pkparameters() creates a ECPKPARAMETERS object from a
311 static ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *,
314 /* the function definitions */
316 static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field)
321 if (group == NULL || field == NULL)
324 /* clear the old values (if necessary) */
325 if (field->fieldType != NULL)
326 ASN1_OBJECT_free(field->fieldType);
327 if (field->p.other != NULL)
328 ASN1_TYPE_free(field->p.other);
330 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
331 /* set OID for the field */
332 if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
333 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
337 if (nid == NID_X9_62_prime_field) {
338 if ((tmp = BN_new()) == NULL) {
339 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
342 /* the parameters are specified by the prime number p */
343 if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL)) {
344 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
347 /* set the prime number */
348 field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
349 if (field->p.prime == NULL) {
350 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
353 } else /* nid == NID_X9_62_characteristic_two_field */
354 #ifdef OPENSSL_NO_EC2M
356 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED);
362 X9_62_CHARACTERISTIC_TWO *char_two;
364 field->p.char_two = X9_62_CHARACTERISTIC_TWO_new();
365 char_two = field->p.char_two;
367 if (char_two == NULL) {
368 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
372 char_two->m = (long)EC_GROUP_get_degree(group);
374 field_type = EC_GROUP_get_basis_type(group);
376 if (field_type == 0) {
377 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
380 /* set base type OID */
381 if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) {
382 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
386 if (field_type == NID_X9_62_tpBasis) {
389 if (!EC_GROUP_get_trinomial_basis(group, &k))
392 char_two->p.tpBasis = ASN1_INTEGER_new();
393 if (!char_two->p.tpBasis) {
394 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
397 if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) {
398 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
401 } else if (field_type == NID_X9_62_ppBasis) {
402 unsigned int k1, k2, k3;
404 if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))
407 char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
408 if (!char_two->p.ppBasis) {
409 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
414 char_two->p.ppBasis->k1 = (long)k1;
415 char_two->p.ppBasis->k2 = (long)k2;
416 char_two->p.ppBasis->k3 = (long)k3;
417 } else { /* field_type == NID_X9_62_onBasis */
419 /* for ONB the parameters are (asn1) NULL */
420 char_two->p.onBasis = ASN1_NULL_new();
421 if (!char_two->p.onBasis) {
422 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
436 static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
439 BIGNUM *tmp_1 = NULL, *tmp_2 = NULL;
440 unsigned char *buffer_1 = NULL, *buffer_2 = NULL,
441 *a_buf = NULL, *b_buf = NULL;
443 unsigned char char_zero = 0;
445 if (!group || !curve || !curve->a || !curve->b)
448 if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
449 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
453 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
456 if (nid == NID_X9_62_prime_field) {
457 if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL)) {
458 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
462 #ifndef OPENSSL_NO_EC2M
463 else { /* nid == NID_X9_62_characteristic_two_field */
465 if (!EC_GROUP_get_curve_GF2m(group, NULL, tmp_1, tmp_2, NULL)) {
466 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
471 len_1 = (size_t)BN_num_bytes(tmp_1);
472 len_2 = (size_t)BN_num_bytes(tmp_2);
475 /* len_1 == 0 => a == 0 */
479 if ((buffer_1 = OPENSSL_malloc(len_1)) == NULL) {
480 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
483 if ((len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0) {
484 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
491 /* len_2 == 0 => b == 0 */
495 if ((buffer_2 = OPENSSL_malloc(len_2)) == NULL) {
496 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
499 if ((len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0) {
500 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
507 if (!M_ASN1_OCTET_STRING_set(curve->a, a_buf, len_1) ||
508 !M_ASN1_OCTET_STRING_set(curve->b, b_buf, len_2)) {
509 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
513 /* set the seed (optional) */
516 if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
517 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
520 curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
521 curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
522 if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
523 (int)group->seed_len)) {
524 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
529 ASN1_BIT_STRING_free(curve->seed);
537 OPENSSL_free(buffer_1);
539 OPENSSL_free(buffer_2);
547 static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group,
552 ECPARAMETERS *ret = NULL;
554 unsigned char *buffer = NULL;
555 const EC_POINT *point = NULL;
556 point_conversion_form_t form;
558 if ((tmp = BN_new()) == NULL) {
559 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
564 if ((ret = ECPARAMETERS_new()) == NULL) {
565 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
571 /* set the version (always one) */
572 ret->version = (long)0x1;
574 /* set the fieldID */
575 if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
576 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
581 if (!ec_asn1_group2curve(group, ret->curve)) {
582 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
586 /* set the base point */
587 if ((point = EC_GROUP_get0_generator(group)) == NULL) {
588 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, EC_R_UNDEFINED_GENERATOR);
592 form = EC_GROUP_get_point_conversion_form(group);
594 len = EC_POINT_point2oct(group, point, form, NULL, len, NULL);
596 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
599 if ((buffer = OPENSSL_malloc(len)) == NULL) {
600 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
603 if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL)) {
604 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
607 if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
608 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
611 if (!ASN1_OCTET_STRING_set(ret->base, buffer, len)) {
612 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
617 if (!EC_GROUP_get_order(group, tmp, NULL)) {
618 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
621 ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
622 if (ret->order == NULL) {
623 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
627 /* set the cofactor (optional) */
628 if (EC_GROUP_get_cofactor(group, tmp, NULL)) {
629 ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
630 if (ret->cofactor == NULL) {
631 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
640 ECPARAMETERS_free(ret);
646 OPENSSL_free(buffer);
650 ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *group,
651 ECPKPARAMETERS *params)
654 ECPKPARAMETERS *ret = params;
657 if ((ret = ECPKPARAMETERS_new()) == NULL) {
658 ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS, ERR_R_MALLOC_FAILURE);
662 if (ret->type == 0 && ret->value.named_curve)
663 ASN1_OBJECT_free(ret->value.named_curve);
664 else if (ret->type == 1 && ret->value.parameters)
665 ECPARAMETERS_free(ret->value.parameters);
668 if (EC_GROUP_get_asn1_flag(group)) {
670 * use the asn1 OID to describe the the elliptic curve parameters
672 tmp = EC_GROUP_get_curve_name(group);
675 if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
678 /* we don't kmow the nid => ERROR */
681 /* use the ECPARAMETERS structure */
683 if ((ret->value.parameters =
684 ec_asn1_group2parameters(group, NULL)) == NULL)
689 ECPKPARAMETERS_free(ret);
695 static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
698 EC_GROUP *ret = NULL;
699 BIGNUM *p = NULL, *a = NULL, *b = NULL;
700 EC_POINT *point = NULL;
703 if (!params->fieldID || !params->fieldID->fieldType ||
704 !params->fieldID->p.ptr) {
705 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
709 /* now extract the curve parameters a and b */
710 if (!params->curve || !params->curve->a ||
711 !params->curve->a->data || !params->curve->b ||
712 !params->curve->b->data) {
713 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
716 a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
718 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
721 b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
723 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
727 /* get the field parameters */
728 tmp = OBJ_obj2nid(params->fieldID->fieldType);
729 if (tmp == NID_X9_62_characteristic_two_field)
730 #ifdef OPENSSL_NO_EC2M
732 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_GF2M_NOT_SUPPORTED);
737 X9_62_CHARACTERISTIC_TWO *char_two;
739 char_two = params->fieldID->p.char_two;
741 field_bits = char_two->m;
742 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
743 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
747 if ((p = BN_new()) == NULL) {
748 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
752 /* get the base type */
753 tmp = OBJ_obj2nid(char_two->type);
755 if (tmp == NID_X9_62_tpBasis) {
758 if (!char_two->p.tpBasis) {
759 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
763 tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
765 if (!(char_two->m > tmp_long && tmp_long > 0)) {
766 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
767 EC_R_INVALID_TRINOMIAL_BASIS);
771 /* create the polynomial */
772 if (!BN_set_bit(p, (int)char_two->m))
774 if (!BN_set_bit(p, (int)tmp_long))
776 if (!BN_set_bit(p, 0))
778 } else if (tmp == NID_X9_62_ppBasis) {
779 X9_62_PENTANOMIAL *penta;
781 penta = char_two->p.ppBasis;
783 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
788 (char_two->m > penta->k3 && penta->k3 > penta->k2
789 && penta->k2 > penta->k1 && penta->k1 > 0)) {
790 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
791 EC_R_INVALID_PENTANOMIAL_BASIS);
795 /* create the polynomial */
796 if (!BN_set_bit(p, (int)char_two->m))
798 if (!BN_set_bit(p, (int)penta->k1))
800 if (!BN_set_bit(p, (int)penta->k2))
802 if (!BN_set_bit(p, (int)penta->k3))
804 if (!BN_set_bit(p, 0))
806 } else if (tmp == NID_X9_62_onBasis) {
807 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED);
811 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
815 /* create the EC_GROUP structure */
816 ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
819 else if (tmp == NID_X9_62_prime_field) {
820 /* we have a curve over a prime field */
821 /* extract the prime number */
822 if (!params->fieldID->p.prime) {
823 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
826 p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
828 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
832 if (BN_is_negative(p) || BN_is_zero(p)) {
833 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
837 field_bits = BN_num_bits(p);
838 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
839 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
843 /* create the EC_GROUP structure */
844 ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
846 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
851 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
855 /* extract seed (optional) */
856 if (params->curve->seed != NULL) {
857 if (ret->seed != NULL)
858 OPENSSL_free(ret->seed);
859 if (!(ret->seed = OPENSSL_malloc(params->curve->seed->length))) {
860 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
863 memcpy(ret->seed, params->curve->seed->data,
864 params->curve->seed->length);
865 ret->seed_len = params->curve->seed->length;
868 if (!params->order || !params->base || !params->base->data) {
869 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
873 if ((point = EC_POINT_new(ret)) == NULL)
876 /* set the point conversion form */
877 EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
878 (params->base->data[0] & ~0x01));
880 /* extract the ec point */
881 if (!EC_POINT_oct2point(ret, point, params->base->data,
882 params->base->length, NULL)) {
883 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
887 /* extract the order */
888 if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
889 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
892 if (BN_is_negative(a) || BN_is_zero(a)) {
893 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
896 if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
897 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
901 /* extract the cofactor (optional) */
902 if (params->cofactor == NULL) {
907 } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
908 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
911 /* set the generator, order and cofactor (if present) */
912 if (!EC_GROUP_set_generator(ret, point, a, b)) {
913 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
921 EC_GROUP_clear_free(ret);
932 EC_POINT_free(point);
936 EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *params)
938 EC_GROUP *ret = NULL;
941 if (params == NULL) {
942 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_MISSING_PARAMETERS);
946 if (params->type == 0) { /* the curve is given by an OID */
947 tmp = OBJ_obj2nid(params->value.named_curve);
948 if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
949 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,
950 EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
953 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
954 } else if (params->type == 1) { /* the parameters are given by a
955 * ECPARAMETERS structure */
956 ret = ec_asn1_parameters2group(params->value.parameters);
958 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB);
961 EC_GROUP_set_asn1_flag(ret, 0x0);
962 } else if (params->type == 2) { /* implicitlyCA */
965 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_ASN1_ERROR);
972 /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
974 EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
976 EC_GROUP *group = NULL;
977 ECPKPARAMETERS *params = NULL;
978 const unsigned char *p = *in;
980 if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) {
981 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
982 ECPKPARAMETERS_free(params);
986 if ((group = ec_asn1_pkparameters2group(params)) == NULL) {
987 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
988 ECPKPARAMETERS_free(params);
993 EC_GROUP_clear_free(*a);
997 ECPKPARAMETERS_free(params);
1002 int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
1005 ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(a, NULL);
1007 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
1010 if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
1011 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
1012 ECPKPARAMETERS_free(tmp);
1015 ECPKPARAMETERS_free(tmp);
1019 /* some EC_KEY functions */
1021 EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
1025 EC_PRIVATEKEY *priv_key = NULL;
1026 const unsigned char *p = *in;
1028 if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) {
1029 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1033 if (a == NULL || *a == NULL) {
1034 if ((ret = EC_KEY_new()) == NULL) {
1035 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1041 if (priv_key->parameters) {
1043 EC_GROUP_clear_free(ret->group);
1044 ret->group = ec_asn1_pkparameters2group(priv_key->parameters);
1047 if (ret->group == NULL) {
1048 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1052 ret->version = priv_key->version;
1054 if (priv_key->privateKey) {
1055 ret->priv_key = BN_bin2bn(M_ASN1_STRING_data(priv_key->privateKey),
1056 M_ASN1_STRING_length(priv_key->privateKey),
1058 if (ret->priv_key == NULL) {
1059 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_BN_LIB);
1063 ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
1068 EC_POINT_clear_free(ret->pub_key);
1069 ret->pub_key = EC_POINT_new(ret->group);
1070 if (ret->pub_key == NULL) {
1071 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1075 if (priv_key->publicKey) {
1076 const unsigned char *pub_oct;
1079 pub_oct = M_ASN1_STRING_data(priv_key->publicKey);
1080 pub_oct_len = M_ASN1_STRING_length(priv_key->publicKey);
1082 * The first byte - point conversion form - must be present.
1084 if (pub_oct_len <= 0) {
1085 ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_BUFFER_TOO_SMALL);
1088 /* Save the point conversion form. */
1089 ret->conv_form = (point_conversion_form_t) (pub_oct[0] & ~0x01);
1090 if (!EC_POINT_oct2point(ret->group, ret->pub_key,
1091 pub_oct, (size_t)(pub_oct_len), NULL)) {
1092 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1097 (ret->group, ret->pub_key, ret->priv_key, NULL, NULL, NULL)) {
1098 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1101 /* Remember the original private-key-only encoding. */
1102 ret->enc_flag |= EC_PKEY_NO_PUBKEY;
1111 if (ret && (a == NULL || *a != ret))
1117 EC_PRIVATEKEY_free(priv_key);
1122 int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out)
1124 int ret = 0, ok = 0;
1125 unsigned char *buffer = NULL;
1126 size_t buf_len = 0, tmp_len, bn_len;
1127 EC_PRIVATEKEY *priv_key = NULL;
1129 if (a == NULL || a->group == NULL || a->priv_key == NULL ||
1130 (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
1131 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
1135 if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
1136 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1140 priv_key->version = a->version;
1142 bn_len = (size_t)BN_num_bytes(a->priv_key);
1144 /* Octetstring may need leading zeros if BN is to short */
1146 buf_len = (EC_GROUP_get_degree(a->group) + 7) / 8;
1148 if (bn_len > buf_len) {
1149 ECerr(EC_F_I2D_ECPRIVATEKEY, EC_R_BUFFER_TOO_SMALL);
1153 buffer = OPENSSL_malloc(buf_len);
1154 if (buffer == NULL) {
1155 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1159 if (!BN_bn2bin(a->priv_key, buffer + buf_len - bn_len)) {
1160 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_BN_LIB);
1164 if (buf_len - bn_len > 0) {
1165 memset(buffer, 0, buf_len - bn_len);
1168 if (!M_ASN1_OCTET_STRING_set(priv_key->privateKey, buffer, buf_len)) {
1169 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
1173 if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
1174 if ((priv_key->parameters =
1175 ec_asn1_group2pkparameters(a->group,
1176 priv_key->parameters)) == NULL) {
1177 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1182 if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
1183 priv_key->publicKey = M_ASN1_BIT_STRING_new();
1184 if (priv_key->publicKey == NULL) {
1185 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1189 tmp_len = EC_POINT_point2oct(a->group, a->pub_key,
1190 a->conv_form, NULL, 0, NULL);
1192 if (tmp_len > buf_len) {
1193 unsigned char *tmp_buffer = OPENSSL_realloc(buffer, tmp_len);
1195 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1198 buffer = tmp_buffer;
1202 if (!EC_POINT_point2oct(a->group, a->pub_key,
1203 a->conv_form, buffer, buf_len, NULL)) {
1204 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1208 priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
1209 priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
1210 if (!M_ASN1_BIT_STRING_set(priv_key->publicKey, buffer, buf_len)) {
1211 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
1216 if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
1217 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1223 OPENSSL_free(buffer);
1225 EC_PRIVATEKEY_free(priv_key);
1226 return (ok ? ret : 0);
1229 int i2d_ECParameters(EC_KEY *a, unsigned char **out)
1232 ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1235 return i2d_ECPKParameters(a->group, out);
1238 EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
1242 if (in == NULL || *in == NULL) {
1243 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1247 if (a == NULL || *a == NULL) {
1248 if ((ret = EC_KEY_new()) == NULL) {
1249 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
1255 if (!d2i_ECPKParameters(&ret->group, in, len)) {
1256 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
1257 if (a == NULL || *a != ret)
1268 EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
1272 if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
1274 * sorry, but a EC_GROUP-structur is necessary to set the public key
1276 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1280 if (ret->pub_key == NULL &&
1281 (ret->pub_key = EC_POINT_new(ret->group)) == NULL) {
1282 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1285 if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL)) {
1286 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
1289 /* save the point conversion form */
1290 ret->conv_form = (point_conversion_form_t) (*in[0] & ~0x01);
1295 int i2o_ECPublicKey(EC_KEY *a, unsigned char **out)
1301 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1305 buf_len = EC_POINT_point2oct(a->group, a->pub_key,
1306 a->conv_form, NULL, 0, NULL);
1308 if (out == NULL || buf_len == 0)
1309 /* out == NULL => just return the length of the octet string */
1313 if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
1314 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1319 if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
1320 *out, buf_len, NULL)) {
1321 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);