f7948ccab2aa4fbcd5419f44f3de46b64cdb7a9e
[openssl.git] / crypto / ec / ec_key.c
1 /*
2  * Written by Nils Larsch for the OpenSSL project.
3  */
4 /* ====================================================================
5  * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. All advertising materials mentioning features or use of this
20  *    software must display the following acknowledgment:
21  *    "This product includes software developed by the OpenSSL Project
22  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
23  *
24  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25  *    endorse or promote products derived from this software without
26  *    prior written permission. For written permission, please contact
27  *    openssl-core@openssl.org.
28  *
29  * 5. Products derived from this software may not be called "OpenSSL"
30  *    nor may "OpenSSL" appear in their names without prior written
31  *    permission of the OpenSSL Project.
32  *
33  * 6. Redistributions of any form whatsoever must retain the following
34  *    acknowledgment:
35  *    "This product includes software developed by the OpenSSL Project
36  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49  * OF THE POSSIBILITY OF SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This product includes cryptographic software written by Eric Young
53  * (eay@cryptsoft.com).  This product includes software written by Tim
54  * Hudson (tjh@cryptsoft.com).
55  *
56  */
57 /* ====================================================================
58  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
59  * Portions originally developed by SUN MICROSYSTEMS, INC., and
60  * contributed to the OpenSSL project.
61  */
62
63 #include <internal/cryptlib.h>
64 #include <string.h>
65 #include "ec_lcl.h"
66 #include <openssl/err.h>
67 #include <openssl/engine.h>
68
69 EC_KEY *EC_KEY_new(void)
70 {
71     return EC_KEY_new_method(NULL);
72 }
73
74 EC_KEY *EC_KEY_new_by_curve_name(int nid)
75 {
76     EC_KEY *ret = EC_KEY_new();
77     if (ret == NULL)
78         return NULL;
79     ret->group = EC_GROUP_new_by_curve_name(nid);
80     if (ret->group == NULL) {
81         EC_KEY_free(ret);
82         return NULL;
83     }
84     if (ret->meth->set_group != NULL
85         && ret->meth->set_group(ret, ret->group) == 0) {
86         EC_KEY_free(ret);
87         return NULL;
88     }
89     return ret;
90 }
91
92 void EC_KEY_free(EC_KEY *r)
93 {
94     int i;
95
96     if (r == NULL)
97         return;
98
99     CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
100     REF_PRINT_COUNT("EC_KEY", r);
101     if (i > 0)
102         return;
103     REF_ASSERT_ISNT(i < 0);
104
105     if (r->meth->finish != NULL)
106         r->meth->finish(r);
107
108 #ifndef OPENSSL_NO_ENGINE
109     ENGINE_finish(r->engine);
110 #endif
111
112     if (r->group && r->group->meth->keyfinish)
113         r->group->meth->keyfinish(r);
114
115     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);
116     CRYPTO_THREAD_lock_free(r->lock);
117     EC_GROUP_free(r->group);
118     EC_POINT_free(r->pub_key);
119     BN_clear_free(r->priv_key);
120
121     OPENSSL_clear_free((void *)r, sizeof(EC_KEY));
122 }
123
124 EC_KEY *EC_KEY_copy(EC_KEY *dest, EC_KEY *src)
125 {
126     if (dest == NULL || src == NULL) {
127         ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
128         return NULL;
129     }
130     if (src->meth != dest->meth) {
131         if (dest->meth->finish != NULL)
132             dest->meth->finish(dest);
133         if (dest->group && dest->group->meth->keyfinish)
134             dest->group->meth->keyfinish(dest);
135 #ifndef OPENSSL_NO_ENGINE
136         if (ENGINE_finish(dest->engine) == 0)
137             return 0;
138         dest->engine = NULL;
139 #endif
140     }
141     /* copy the parameters */
142     if (src->group != NULL) {
143         const EC_METHOD *meth = EC_GROUP_method_of(src->group);
144         /* clear the old group */
145         EC_GROUP_free(dest->group);
146         dest->group = EC_GROUP_new(meth);
147         if (dest->group == NULL)
148             return NULL;
149         if (!EC_GROUP_copy(dest->group, src->group))
150             return NULL;
151     }
152     /*  copy the public key */
153     if (src->pub_key != NULL && src->group != NULL) {
154         EC_POINT_free(dest->pub_key);
155         dest->pub_key = EC_POINT_new(src->group);
156         if (dest->pub_key == NULL)
157             return NULL;
158         if (!EC_POINT_copy(dest->pub_key, src->pub_key))
159             return NULL;
160     }
161     /* copy the private key */
162     if (src->priv_key != NULL) {
163         if (dest->priv_key == NULL) {
164             dest->priv_key = BN_new();
165             if (dest->priv_key == NULL)
166                 return NULL;
167         }
168         if (!BN_copy(dest->priv_key, src->priv_key))
169             return NULL;
170         if (src->group->meth->keycopy
171             && src->group->meth->keycopy(dest, src) == 0)
172             return NULL;
173     }
174
175
176     /* copy the rest */
177     dest->enc_flag = src->enc_flag;
178     dest->conv_form = src->conv_form;
179     dest->version = src->version;
180     dest->flags = src->flags;
181     if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
182                             &dest->ex_data, &src->ex_data))
183         return NULL;
184
185     if (src->meth != dest->meth) {
186 #ifndef OPENSSL_NO_ENGINE
187         if (src->engine != NULL && ENGINE_init(src->engine) == 0)
188             return NULL;
189         dest->engine = src->engine;
190 #endif
191         dest->meth = src->meth;
192     }
193
194     if (src->meth->copy != NULL && src->meth->copy(dest, src) == 0)
195         return NULL;
196
197     return dest;
198 }
199
200 EC_KEY *EC_KEY_dup(EC_KEY *ec_key)
201 {
202     EC_KEY *ret = EC_KEY_new_method(ec_key->engine);
203
204     if (ret == NULL)
205         return NULL;
206
207     if (EC_KEY_copy(ret, ec_key) == NULL) {
208         EC_KEY_free(ret);
209         return NULL;
210     }
211     return ret;
212 }
213
214 int EC_KEY_up_ref(EC_KEY *r)
215 {
216     int i;
217
218     if (CRYPTO_atomic_add(&r->references, 1, &i, r->lock) <= 0)
219         return 0;
220
221     REF_PRINT_COUNT("EC_KEY", r);
222     REF_ASSERT_ISNT(i < 2);
223     return ((i > 1) ? 1 : 0);
224 }
225
226 int EC_KEY_generate_key(EC_KEY *eckey)
227 {
228     if (eckey == NULL || eckey->group == NULL) {
229         ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
230         return 0;
231     }
232     if (eckey->meth->keygen != NULL)
233         return eckey->meth->keygen(eckey);
234     ECerr(EC_F_EC_KEY_GENERATE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
235     return 0;
236 }
237
238 int ossl_ec_key_gen(EC_KEY *eckey)
239 {
240     OPENSSL_assert(eckey->group->meth->keygen != NULL);
241     return eckey->group->meth->keygen(eckey);
242 }
243
244 int ec_key_simple_generate_key(EC_KEY *eckey)
245 {
246     int ok = 0;
247     BN_CTX *ctx = NULL;
248     BIGNUM *priv_key = NULL;
249     const BIGNUM *order = NULL;
250     EC_POINT *pub_key = NULL;
251
252     if ((ctx = BN_CTX_new()) == NULL)
253         goto err;
254
255     if (eckey->priv_key == NULL) {
256         priv_key = BN_new();
257         if (priv_key == NULL)
258             goto err;
259     } else
260         priv_key = eckey->priv_key;
261
262     order = EC_GROUP_get0_order(eckey->group);
263     if (order == NULL)
264         goto err;
265
266     do
267         if (!BN_rand_range(priv_key, order))
268             goto err;
269     while (BN_is_zero(priv_key)) ;
270
271     if (eckey->pub_key == NULL) {
272         pub_key = EC_POINT_new(eckey->group);
273         if (pub_key == NULL)
274             goto err;
275     } else
276         pub_key = eckey->pub_key;
277
278     if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx))
279         goto err;
280
281     eckey->priv_key = priv_key;
282     eckey->pub_key = pub_key;
283
284     ok = 1;
285
286  err:
287     if (eckey->pub_key == NULL)
288         EC_POINT_free(pub_key);
289     if (eckey->priv_key != priv_key)
290         BN_free(priv_key);
291     BN_CTX_free(ctx);
292     return ok;
293 }
294
295 int ec_key_simple_generate_public_key(EC_KEY *eckey)
296 {
297     return EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,
298                         NULL, NULL);
299 }
300
301 int EC_KEY_check_key(const EC_KEY *eckey)
302 {
303     if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
304         ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
305         return 0;
306     }
307
308     if (eckey->group->meth->keycheck == NULL) {
309         ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
310         return 0;
311     }
312
313     return eckey->group->meth->keycheck(eckey);
314 }
315
316 int ec_key_simple_check_key(const EC_KEY *eckey)
317 {
318     int ok = 0;
319     BN_CTX *ctx = NULL;
320     const BIGNUM *order = NULL;
321     EC_POINT *point = NULL;
322
323     if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
324         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
325         return 0;
326     }
327
328     if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
329         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_AT_INFINITY);
330         goto err;
331     }
332
333     if ((ctx = BN_CTX_new()) == NULL)
334         goto err;
335     if ((point = EC_POINT_new(eckey->group)) == NULL)
336         goto err;
337
338     /* testing whether the pub_key is on the elliptic curve */
339     if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
340         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
341         goto err;
342     }
343     /* testing whether pub_key * order is the point at infinity */
344     order = eckey->group->order;
345     if (BN_is_zero(order)) {
346         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
347         goto err;
348     }
349     if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
350         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
351         goto err;
352     }
353     if (!EC_POINT_is_at_infinity(eckey->group, point)) {
354         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
355         goto err;
356     }
357     /*
358      * in case the priv_key is present : check if generator * priv_key ==
359      * pub_key
360      */
361     if (eckey->priv_key != NULL) {
362         if (BN_cmp(eckey->priv_key, order) >= 0) {
363             ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
364             goto err;
365         }
366         if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
367                           NULL, NULL, ctx)) {
368             ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
369             goto err;
370         }
371         if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
372             ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
373             goto err;
374         }
375     }
376     ok = 1;
377  err:
378     BN_CTX_free(ctx);
379     EC_POINT_free(point);
380     return ok;
381 }
382
383 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
384                                              BIGNUM *y)
385 {
386     BN_CTX *ctx = NULL;
387     BIGNUM *tx, *ty;
388     EC_POINT *point = NULL;
389     int ok = 0;
390 #ifndef OPENSSL_NO_EC2M
391     int tmp_nid, is_char_two = 0;
392 #endif
393
394     if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
395         ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
396               ERR_R_PASSED_NULL_PARAMETER);
397         return 0;
398     }
399     ctx = BN_CTX_new();
400     if (ctx == NULL)
401         goto err;
402
403     point = EC_POINT_new(key->group);
404
405     if (point == NULL)
406         goto err;
407
408     tx = BN_CTX_get(ctx);
409     ty = BN_CTX_get(ctx);
410     if (ty == NULL)
411         goto err;
412
413 #ifndef OPENSSL_NO_EC2M
414     tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(key->group));
415
416     if (tmp_nid == NID_X9_62_characteristic_two_field)
417         is_char_two = 1;
418
419     if (is_char_two) {
420         if (!EC_POINT_set_affine_coordinates_GF2m(key->group, point,
421                                                   x, y, ctx))
422             goto err;
423         if (!EC_POINT_get_affine_coordinates_GF2m(key->group, point,
424                                                   tx, ty, ctx))
425             goto err;
426     } else
427 #endif
428     {
429         if (!EC_POINT_set_affine_coordinates_GFp(key->group, point,
430                                                  x, y, ctx))
431             goto err;
432         if (!EC_POINT_get_affine_coordinates_GFp(key->group, point,
433                                                  tx, ty, ctx))
434             goto err;
435     }
436     /*
437      * Check if retrieved coordinates match originals and are less than field
438      * order: if not values are out of range.
439      */
440     if (BN_cmp(x, tx) || BN_cmp(y, ty)
441         || (BN_cmp(x, key->group->field) >= 0)
442         || (BN_cmp(y, key->group->field) >= 0)) {
443         ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
444               EC_R_COORDINATES_OUT_OF_RANGE);
445         goto err;
446     }
447
448     if (!EC_KEY_set_public_key(key, point))
449         goto err;
450
451     if (EC_KEY_check_key(key) == 0)
452         goto err;
453
454     ok = 1;
455
456  err:
457     BN_CTX_free(ctx);
458     EC_POINT_free(point);
459     return ok;
460
461 }
462
463 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
464 {
465     return key->group;
466 }
467
468 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
469 {
470     if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)
471         return 0;
472     EC_GROUP_free(key->group);
473     key->group = EC_GROUP_dup(group);
474     return (key->group == NULL) ? 0 : 1;
475 }
476
477 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
478 {
479     return key->priv_key;
480 }
481
482 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
483 {
484     if (key->group == NULL || key->group->meth == NULL)
485         return 0;
486     if (key->group->meth->set_private
487         && key->meth->set_private(key, priv_key) == 0)
488         return 0;
489     if (key->meth->set_private != NULL
490         && key->meth->set_private(key, priv_key) == 0)
491         return 0;
492     BN_clear_free(key->priv_key);
493     key->priv_key = BN_dup(priv_key);
494     return (key->priv_key == NULL) ? 0 : 1;
495 }
496
497 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
498 {
499     return key->pub_key;
500 }
501
502 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
503 {
504     if (key->meth->set_public != NULL
505         && key->meth->set_public(key, pub_key) == 0)
506         return 0;
507     EC_POINT_free(key->pub_key);
508     key->pub_key = EC_POINT_dup(pub_key, key->group);
509     return (key->pub_key == NULL) ? 0 : 1;
510 }
511
512 unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
513 {
514     return key->enc_flag;
515 }
516
517 void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
518 {
519     key->enc_flag = flags;
520 }
521
522 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
523 {
524     return key->conv_form;
525 }
526
527 void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
528 {
529     key->conv_form = cform;
530     if (key->group != NULL)
531         EC_GROUP_set_point_conversion_form(key->group, cform);
532 }
533
534 void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
535 {
536     if (key->group != NULL)
537         EC_GROUP_set_asn1_flag(key->group, flag);
538 }
539
540 int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
541 {
542     if (key->group == NULL)
543         return 0;
544     return EC_GROUP_precompute_mult(key->group, ctx);
545 }
546
547 int EC_KEY_get_flags(const EC_KEY *key)
548 {
549     return key->flags;
550 }
551
552 void EC_KEY_set_flags(EC_KEY *key, int flags)
553 {
554     key->flags |= flags;
555 }
556
557 void EC_KEY_clear_flags(EC_KEY *key, int flags)
558 {
559     key->flags &= ~flags;
560 }
561
562 size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
563                         unsigned char **pbuf, BN_CTX *ctx)
564 {
565     if (key == NULL || key->pub_key == NULL || key->group == NULL)
566         return 0;
567     return EC_POINT_point2buf(key->group, key->pub_key, form, pbuf, ctx);
568 }
569
570 int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
571                    BN_CTX *ctx)
572 {
573     if (key == NULL || key->group == NULL)
574         return 0;
575     if (key->pub_key == NULL)
576         key->pub_key = EC_POINT_new(key->group);
577     if (key->pub_key == NULL)
578         return 0;
579     if (EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx) == 0)
580         return 0;
581     /*
582      * Save the point conversion form.
583      * For non-custom curves the first octet of the buffer (excluding
584      * the last significant bit) contains the point conversion form.
585      * EC_POINT_oct2point() has already performed sanity checking of
586      * the buffer so we know it is valid.
587      */
588     if ((key->group->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0)
589         key->conv_form = (point_conversion_form_t)(buf[0] & ~0x01);
590     return 1;
591 }
592
593 size_t EC_KEY_priv2oct(const EC_KEY *eckey, unsigned char *buf, size_t len)
594 {
595     if (eckey->group == NULL || eckey->group->meth == NULL)
596         return 0;
597     if (eckey->group->meth->priv2oct == NULL) {
598         ECerr(EC_F_EC_KEY_PRIV2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
599         return 0;
600     }
601
602     return eckey->group->meth->priv2oct(eckey, buf, len);
603 }
604
605 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
606                               unsigned char *buf, size_t len)
607 {
608     size_t buf_len;
609
610     buf_len = (EC_GROUP_order_bits(eckey->group) + 7) / 8;
611     if (eckey->priv_key == NULL)
612         return 0;
613     if (buf == NULL)
614         return buf_len;
615     else if (len < buf_len)
616         return 0;
617
618     /* Octetstring may need leading zeros if BN is to short */
619
620     if (BN_bn2binpad(eckey->priv_key, buf, buf_len) == -1) {
621         ECerr(EC_F_EC_KEY_SIMPLE_PRIV2OCT, EC_R_BUFFER_TOO_SMALL);
622         return 0;
623     }
624
625     return buf_len;
626 }
627
628 int EC_KEY_oct2priv(EC_KEY *eckey, unsigned char *buf, size_t len)
629 {
630     if (eckey->group == NULL || eckey->group->meth == NULL)
631         return 0;
632     if (eckey->group->meth->oct2priv == NULL) {
633         ECerr(EC_F_EC_KEY_OCT2PRIV, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
634         return 0;
635     }
636     return eckey->group->meth->oct2priv(eckey, buf, len);
637 }
638
639 int ec_key_simple_oct2priv(EC_KEY *eckey, unsigned char *buf, size_t len)
640 {
641     if (eckey->priv_key == NULL)
642         eckey->priv_key = BN_secure_new();
643     if (eckey->priv_key == NULL) {
644         ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_MALLOC_FAILURE);
645         return 0;
646     }
647     eckey->priv_key = BN_bin2bn(buf, len, eckey->priv_key);
648     if (eckey->priv_key == NULL) {
649         ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_BN_LIB);
650         return 0;
651     }
652     return 1;
653 }
654
655 size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf)
656 {
657     size_t len;
658     unsigned char *buf;
659     len = EC_KEY_priv2oct(eckey, NULL, 0);
660     if (len == 0)
661         return 0;
662     buf = OPENSSL_malloc(len);
663     if (buf == NULL)
664         return 0;
665     len = EC_KEY_priv2oct(eckey, buf, len);
666     if (len == 0) {
667         OPENSSL_free(buf);
668         return 0;
669     }
670     *pbuf = buf;
671     return len;
672 }
673
674 int EC_KEY_can_sign(const EC_KEY *eckey)
675 {
676     if (eckey->group == NULL || eckey->group->meth == NULL
677         || (eckey->group->meth->flags & EC_FLAGS_NO_SIGN))
678         return 0;
679     return 1;
680 }