1 /* $OpenBSD: ec_ameth.c,v 1.18 2017/01/29 17:49:23 beck Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2006 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/opensslconf.h>
63 #include <openssl/bn.h>
64 #include <openssl/ec.h>
65 #include <openssl/err.h>
66 #include <openssl/x509.h>
69 #include "asn1_locl.h"
72 eckey_param2type(int *pptype
, void **ppval
, EC_KEY
* ec_key
)
74 const EC_GROUP
*group
;
76 if (ec_key
== NULL
|| (group
= EC_KEY_get0_group(ec_key
)) == NULL
) {
77 ECerror(EC_R_MISSING_PARAMETERS
);
80 if (EC_GROUP_get_asn1_flag(group
) &&
81 (nid
= EC_GROUP_get_curve_name(group
))) {
82 /* we have a 'named curve' => just set the OID */
83 *ppval
= OBJ_nid2obj(nid
);
84 *pptype
= V_ASN1_OBJECT
;
86 /* explicit parameters */
87 ASN1_STRING
*pstr
= NULL
;
88 pstr
= ASN1_STRING_new();
91 pstr
->length
= i2d_ECParameters(ec_key
, &pstr
->data
);
92 if (pstr
->length
<= 0) {
93 ASN1_STRING_free(pstr
);
94 ECerror(ERR_R_EC_LIB
);
98 *pptype
= V_ASN1_SEQUENCE
;
104 eckey_pub_encode(X509_PUBKEY
* pk
, const EVP_PKEY
* pkey
)
106 EC_KEY
*ec_key
= pkey
->pkey
.ec
;
109 unsigned char *penc
= NULL
, *p
;
112 if (!eckey_param2type(&ptype
, &pval
, ec_key
)) {
113 ECerror(ERR_R_EC_LIB
);
116 penclen
= i2o_ECPublicKey(ec_key
, NULL
);
119 penc
= malloc(penclen
);
123 penclen
= i2o_ECPublicKey(ec_key
, &p
);
126 if (X509_PUBKEY_set0_param(pk
, OBJ_nid2obj(EVP_PKEY_EC
),
127 ptype
, pval
, penc
, penclen
))
130 if (ptype
== V_ASN1_OBJECT
)
131 ASN1_OBJECT_free(pval
);
133 ASN1_STRING_free(pval
);
139 eckey_type2param(int ptype
, void *pval
)
141 EC_KEY
*eckey
= NULL
;
143 if (ptype
== V_ASN1_SEQUENCE
) {
144 ASN1_STRING
*pstr
= pval
;
145 const unsigned char *pm
= NULL
;
149 pmlen
= pstr
->length
;
150 if (!(eckey
= d2i_ECParameters(NULL
, &pm
, pmlen
))) {
151 ECerror(EC_R_DECODE_ERROR
);
154 } else if (ptype
== V_ASN1_OBJECT
) {
155 ASN1_OBJECT
*poid
= pval
;
159 * type == V_ASN1_OBJECT => the parameters are given by an
162 if ((eckey
= EC_KEY_new()) == NULL
) {
163 ECerror(ERR_R_MALLOC_FAILURE
);
166 group
= EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid
));
169 EC_GROUP_set_asn1_flag(group
, OPENSSL_EC_NAMED_CURVE
);
170 if (EC_KEY_set_group(eckey
, group
) == 0)
172 EC_GROUP_free(group
);
174 ECerror(EC_R_DECODE_ERROR
);
187 eckey_pub_decode(EVP_PKEY
* pkey
, X509_PUBKEY
* pubkey
)
189 const unsigned char *p
= NULL
;
192 EC_KEY
*eckey
= NULL
;
195 if (!X509_PUBKEY_get0_param(NULL
, &p
, &pklen
, &palg
, pubkey
))
197 X509_ALGOR_get0(NULL
, &ptype
, &pval
, palg
);
199 eckey
= eckey_type2param(ptype
, pval
);
202 ECerror(ERR_R_EC_LIB
);
205 /* We have parameters now set public key */
206 if (!o2i_ECPublicKey(&eckey
, &p
, pklen
)) {
207 ECerror(EC_R_DECODE_ERROR
);
210 EVP_PKEY_assign_EC_KEY(pkey
, eckey
);
220 eckey_pub_cmp(const EVP_PKEY
* a
, const EVP_PKEY
* b
)
223 const EC_GROUP
*group
= EC_KEY_get0_group(b
->pkey
.ec
);
224 const EC_POINT
*pa
= EC_KEY_get0_public_key(a
->pkey
.ec
), *pb
= EC_KEY_get0_public_key(b
->pkey
.ec
);
226 r
= EC_POINT_cmp(group
, pa
, pb
, NULL
);
235 eckey_priv_decode(EVP_PKEY
* pkey
, PKCS8_PRIV_KEY_INFO
* p8
)
237 const unsigned char *p
= NULL
;
240 EC_KEY
*eckey
= NULL
;
243 if (!PKCS8_pkey_get0(NULL
, &p
, &pklen
, &palg
, p8
))
245 X509_ALGOR_get0(NULL
, &ptype
, &pval
, palg
);
247 eckey
= eckey_type2param(ptype
, pval
);
252 /* We have parameters now set private key */
253 if (!d2i_ECPrivateKey(&eckey
, &p
, pklen
)) {
254 ECerror(EC_R_DECODE_ERROR
);
257 /* calculate public key (if necessary) */
258 if (EC_KEY_get0_public_key(eckey
) == NULL
) {
259 const BIGNUM
*priv_key
;
260 const EC_GROUP
*group
;
263 * the public key was not included in the SEC1 private key =>
264 * calculate the public key
266 group
= EC_KEY_get0_group(eckey
);
267 pub_key
= EC_POINT_new(group
);
268 if (pub_key
== NULL
) {
269 ECerror(ERR_R_EC_LIB
);
272 if (!EC_POINT_copy(pub_key
, EC_GROUP_get0_generator(group
))) {
273 EC_POINT_free(pub_key
);
274 ECerror(ERR_R_EC_LIB
);
277 priv_key
= EC_KEY_get0_private_key(eckey
);
278 if (!EC_POINT_mul(group
, pub_key
, priv_key
, NULL
, NULL
, NULL
)) {
279 EC_POINT_free(pub_key
);
280 ECerror(ERR_R_EC_LIB
);
283 if (EC_KEY_set_public_key(eckey
, pub_key
) == 0) {
284 EC_POINT_free(pub_key
);
285 ECerror(ERR_R_EC_LIB
);
288 EC_POINT_free(pub_key
);
290 EVP_PKEY_assign_EC_KEY(pkey
, eckey
);
294 ECerror(ERR_R_EC_LIB
);
302 eckey_priv_encode(PKCS8_PRIV_KEY_INFO
* p8
, const EVP_PKEY
* pkey
)
305 unsigned char *ep
, *p
;
308 unsigned int tmp_flags
, old_flags
;
310 ec_key
= pkey
->pkey
.ec
;
312 if (!eckey_param2type(&ptype
, &pval
, ec_key
)) {
313 ECerror(EC_R_DECODE_ERROR
);
316 /* set the private key */
319 * do not include the parameters in the SEC1 private key see PKCS#11
322 old_flags
= EC_KEY_get_enc_flags(ec_key
);
323 tmp_flags
= old_flags
| EC_PKEY_NO_PARAMETERS
;
324 EC_KEY_set_enc_flags(ec_key
, tmp_flags
);
325 eplen
= i2d_ECPrivateKey(ec_key
, NULL
);
327 EC_KEY_set_enc_flags(ec_key
, old_flags
);
328 ECerror(ERR_R_EC_LIB
);
333 EC_KEY_set_enc_flags(ec_key
, old_flags
);
334 ECerror(ERR_R_MALLOC_FAILURE
);
338 if (!i2d_ECPrivateKey(ec_key
, &p
)) {
339 EC_KEY_set_enc_flags(ec_key
, old_flags
);
341 ECerror(ERR_R_EC_LIB
);
344 /* restore old encoding flags */
345 EC_KEY_set_enc_flags(ec_key
, old_flags
);
347 if (!PKCS8_pkey_set0(p8
, OBJ_nid2obj(NID_X9_62_id_ecPublicKey
), 0,
348 ptype
, pval
, ep
, eplen
))
355 int_ec_size(const EVP_PKEY
* pkey
)
357 return ECDSA_size(pkey
->pkey
.ec
);
361 ec_bits(const EVP_PKEY
* pkey
)
363 BIGNUM
*order
= BN_new();
364 const EC_GROUP
*group
;
371 group
= EC_KEY_get0_group(pkey
->pkey
.ec
);
372 if (!EC_GROUP_get_order(group
, order
, NULL
)) {
377 ret
= BN_num_bits(order
);
383 ec_missing_parameters(const EVP_PKEY
* pkey
)
385 if (EC_KEY_get0_group(pkey
->pkey
.ec
) == NULL
)
391 ec_copy_parameters(EVP_PKEY
* to
, const EVP_PKEY
* from
)
393 return EC_KEY_set_group(to
->pkey
.ec
, EC_KEY_get0_group(from
->pkey
.ec
));
397 ec_cmp_parameters(const EVP_PKEY
* a
, const EVP_PKEY
* b
)
399 const EC_GROUP
*group_a
= EC_KEY_get0_group(a
->pkey
.ec
), *group_b
= EC_KEY_get0_group(b
->pkey
.ec
);
400 if (EC_GROUP_cmp(group_a
, group_b
, NULL
))
407 int_ec_free(EVP_PKEY
* pkey
)
409 EC_KEY_free(pkey
->pkey
.ec
);
413 do_EC_KEY_print(BIO
* bp
, const EC_KEY
* x
, int off
, int ktype
)
415 unsigned char *buffer
= NULL
;
417 size_t buf_len
= 0, i
;
418 int ret
= 0, reason
= ERR_R_BIO_LIB
;
419 BIGNUM
*pub_key
= NULL
, *order
= NULL
;
421 const EC_GROUP
*group
;
422 const EC_POINT
*public_key
;
423 const BIGNUM
*priv_key
;
425 if (x
== NULL
|| (group
= EC_KEY_get0_group(x
)) == NULL
) {
426 reason
= ERR_R_PASSED_NULL_PARAMETER
;
431 reason
= ERR_R_MALLOC_FAILURE
;
435 public_key
= EC_KEY_get0_public_key(x
);
436 if ((pub_key
= EC_POINT_point2bn(group
, public_key
,
437 EC_KEY_get_conv_form(x
), NULL
, ctx
)) == NULL
) {
438 reason
= ERR_R_EC_LIB
;
442 buf_len
= (size_t) BN_num_bytes(pub_key
);
445 priv_key
= EC_KEY_get0_private_key(x
);
446 if (priv_key
&& (i
= (size_t) BN_num_bytes(priv_key
)) > buf_len
)
453 if ((buffer
= malloc(buf_len
)) == NULL
) {
454 reason
= ERR_R_MALLOC_FAILURE
;
459 ecstr
= "Private-Key";
461 ecstr
= "Public-Key";
463 ecstr
= "ECDSA-Parameters";
465 if (!BIO_indent(bp
, off
, 128))
467 if ((order
= BN_new()) == NULL
)
469 if (!EC_GROUP_get_order(group
, order
, NULL
))
471 if (BIO_printf(bp
, "%s: (%d bit)\n", ecstr
,
472 BN_num_bits(order
)) <= 0)
475 if ((priv_key
!= NULL
) && !ASN1_bn_print(bp
, "priv:", priv_key
,
478 if ((pub_key
!= NULL
) && !ASN1_bn_print(bp
, "pub: ", pub_key
,
481 if (!ECPKParameters_print(bp
, group
, off
))
495 eckey_param_decode(EVP_PKEY
* pkey
,
496 const unsigned char **pder
, int derlen
)
499 if (!(eckey
= d2i_ECParameters(NULL
, pder
, derlen
))) {
500 ECerror(ERR_R_EC_LIB
);
503 EVP_PKEY_assign_EC_KEY(pkey
, eckey
);
508 eckey_param_encode(const EVP_PKEY
* pkey
, unsigned char **pder
)
510 return i2d_ECParameters(pkey
->pkey
.ec
, pder
);
514 eckey_param_print(BIO
* bp
, const EVP_PKEY
* pkey
, int indent
,
517 return do_EC_KEY_print(bp
, pkey
->pkey
.ec
, indent
, 0);
521 eckey_pub_print(BIO
* bp
, const EVP_PKEY
* pkey
, int indent
,
524 return do_EC_KEY_print(bp
, pkey
->pkey
.ec
, indent
, 1);
529 eckey_priv_print(BIO
* bp
, const EVP_PKEY
* pkey
, int indent
,
532 return do_EC_KEY_print(bp
, pkey
->pkey
.ec
, indent
, 2);
536 old_ec_priv_decode(EVP_PKEY
* pkey
,
537 const unsigned char **pder
, int derlen
)
540 if (!(ec
= d2i_ECPrivateKey(NULL
, pder
, derlen
))) {
541 ECerror(EC_R_DECODE_ERROR
);
544 EVP_PKEY_assign_EC_KEY(pkey
, ec
);
549 old_ec_priv_encode(const EVP_PKEY
* pkey
, unsigned char **pder
)
551 return i2d_ECPrivateKey(pkey
->pkey
.ec
, pder
);
555 ec_pkey_ctrl(EVP_PKEY
* pkey
, int op
, long arg1
, void *arg2
)
558 case ASN1_PKEY_CTRL_PKCS7_SIGN
:
561 X509_ALGOR
*alg1
, *alg2
;
562 PKCS7_SIGNER_INFO_get0_algs(arg2
, NULL
, &alg1
, &alg2
);
563 if (alg1
== NULL
|| alg1
->algorithm
== NULL
)
565 hnid
= OBJ_obj2nid(alg1
->algorithm
);
566 if (hnid
== NID_undef
)
568 if (!OBJ_find_sigid_by_algs(&snid
, hnid
, EVP_PKEY_id(pkey
)))
570 X509_ALGOR_set0(alg2
, OBJ_nid2obj(snid
), V_ASN1_UNDEF
, 0);
574 case ASN1_PKEY_CTRL_DEFAULT_MD_NID
:
575 *(int *) arg2
= NID_sha1
;
585 const EVP_PKEY_ASN1_METHOD eckey_asn1_meth
= {
586 .pkey_id
= EVP_PKEY_EC
,
587 .pkey_base_id
= EVP_PKEY_EC
,
590 .info
= "OpenSSL EC algorithm",
592 .pub_decode
= eckey_pub_decode
,
593 .pub_encode
= eckey_pub_encode
,
594 .pub_cmp
= eckey_pub_cmp
,
595 .pub_print
= eckey_pub_print
,
597 .priv_decode
= eckey_priv_decode
,
598 .priv_encode
= eckey_priv_encode
,
599 .priv_print
= eckey_priv_print
,
601 .pkey_size
= int_ec_size
,
602 .pkey_bits
= ec_bits
,
604 .param_decode
= eckey_param_decode
,
605 .param_encode
= eckey_param_encode
,
606 .param_missing
= ec_missing_parameters
,
607 .param_copy
= ec_copy_parameters
,
608 .param_cmp
= ec_cmp_parameters
,
609 .param_print
= eckey_param_print
,
611 .pkey_free
= int_ec_free
,
612 .pkey_ctrl
= ec_pkey_ctrl
,
613 .old_priv_decode
= old_ec_priv_decode
,
614 .old_priv_encode
= old_ec_priv_encode