[PROV][KMGMT][KEXCH][EC] Implement EC keymgtm and ECDH
[openssl.git] / crypto / ec / ec_key.c
1 /*
2  * Copyright 2002-2019 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4  *
5  * Licensed under the Apache License 2.0 (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  */
10
11 /*
12  * ECDSA low level APIs are deprecated for public use, but still ok for
13  * internal use.
14  */
15 #include "internal/deprecated.h"
16
17 #include "internal/cryptlib.h"
18 #include <string.h>
19 #include "ec_local.h"
20 #include "internal/refcount.h"
21 #include <openssl/err.h>
22 #include <openssl/engine.h>
23
24 #ifndef FIPS_MODE
25 EC_KEY *EC_KEY_new(void)
26 {
27     return ec_key_new_method_int(NULL, NULL);
28 }
29 #endif
30
31 EC_KEY *EC_KEY_new_ex(OPENSSL_CTX *ctx)
32 {
33     return ec_key_new_method_int(ctx, NULL);
34 }
35
36 EC_KEY *EC_KEY_new_by_curve_name_ex(OPENSSL_CTX *ctx, int nid)
37 {
38     EC_KEY *ret = EC_KEY_new_ex(ctx);
39     if (ret == NULL)
40         return NULL;
41     ret->group = EC_GROUP_new_by_curve_name_ex(ctx, nid);
42     if (ret->group == NULL) {
43         EC_KEY_free(ret);
44         return NULL;
45     }
46     if (ret->meth->set_group != NULL
47         && ret->meth->set_group(ret, ret->group) == 0) {
48         EC_KEY_free(ret);
49         return NULL;
50     }
51     return ret;
52 }
53
54 #ifndef FIPS_MODE
55 EC_KEY *EC_KEY_new_by_curve_name(int nid)
56 {
57     return EC_KEY_new_by_curve_name_ex(NULL, nid);
58 }
59 #endif
60
61 void EC_KEY_free(EC_KEY *r)
62 {
63     int i;
64
65     if (r == NULL)
66         return;
67
68     CRYPTO_DOWN_REF(&r->references, &i, r->lock);
69     REF_PRINT_COUNT("EC_KEY", r);
70     if (i > 0)
71         return;
72     REF_ASSERT_ISNT(i < 0);
73
74     if (r->meth != NULL && r->meth->finish != NULL)
75         r->meth->finish(r);
76
77 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
78     ENGINE_finish(r->engine);
79 #endif
80
81     if (r->group && r->group->meth->keyfinish)
82         r->group->meth->keyfinish(r);
83
84 #ifndef FIPS_MODE
85     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);
86 #endif
87     CRYPTO_THREAD_lock_free(r->lock);
88     EC_GROUP_free(r->group);
89     EC_POINT_free(r->pub_key);
90     BN_clear_free(r->priv_key);
91
92     OPENSSL_clear_free((void *)r, sizeof(EC_KEY));
93 }
94
95 EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
96 {
97     if (dest == NULL || src == NULL) {
98         ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
99         return NULL;
100     }
101     if (src->meth != dest->meth) {
102         if (dest->meth->finish != NULL)
103             dest->meth->finish(dest);
104         if (dest->group && dest->group->meth->keyfinish)
105             dest->group->meth->keyfinish(dest);
106 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
107         if (ENGINE_finish(dest->engine) == 0)
108             return 0;
109         dest->engine = NULL;
110 #endif
111     }
112     dest->libctx = src->libctx;
113     /* copy the parameters */
114     if (src->group != NULL) {
115         const EC_METHOD *meth = EC_GROUP_method_of(src->group);
116         /* clear the old group */
117         EC_GROUP_free(dest->group);
118         dest->group = EC_GROUP_new_ex(src->libctx, meth);
119         if (dest->group == NULL)
120             return NULL;
121         if (!EC_GROUP_copy(dest->group, src->group))
122             return NULL;
123
124         /*  copy the public key */
125         if (src->pub_key != NULL) {
126             EC_POINT_free(dest->pub_key);
127             dest->pub_key = EC_POINT_new(src->group);
128             if (dest->pub_key == NULL)
129                 return NULL;
130             if (!EC_POINT_copy(dest->pub_key, src->pub_key))
131                 return NULL;
132         }
133         /* copy the private key */
134         if (src->priv_key != NULL) {
135             if (dest->priv_key == NULL) {
136                 dest->priv_key = BN_new();
137                 if (dest->priv_key == NULL)
138                     return NULL;
139             }
140             if (!BN_copy(dest->priv_key, src->priv_key))
141                 return NULL;
142             if (src->group->meth->keycopy
143                 && src->group->meth->keycopy(dest, src) == 0)
144                 return NULL;
145         }
146     }
147
148
149     /* copy the rest */
150     dest->enc_flag = src->enc_flag;
151     dest->conv_form = src->conv_form;
152     dest->version = src->version;
153     dest->flags = src->flags;
154 #ifndef FIPS_MODE
155     if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
156                             &dest->ex_data, &src->ex_data))
157         return NULL;
158 #endif
159
160     if (src->meth != dest->meth) {
161 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
162         if (src->engine != NULL && ENGINE_init(src->engine) == 0)
163             return NULL;
164         dest->engine = src->engine;
165 #endif
166         dest->meth = src->meth;
167     }
168
169     if (src->meth->copy != NULL && src->meth->copy(dest, src) == 0)
170         return NULL;
171
172     dest->dirty_cnt++;
173
174     return dest;
175 }
176
177 EC_KEY *EC_KEY_dup(const EC_KEY *ec_key)
178 {
179     EC_KEY *ret = ec_key_new_method_int(ec_key->libctx, ec_key->engine);
180
181     if (ret == NULL)
182         return NULL;
183
184     if (EC_KEY_copy(ret, ec_key) == NULL) {
185         EC_KEY_free(ret);
186         return NULL;
187     }
188     return ret;
189 }
190
191 int EC_KEY_up_ref(EC_KEY *r)
192 {
193     int i;
194
195     if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
196         return 0;
197
198     REF_PRINT_COUNT("EC_KEY", r);
199     REF_ASSERT_ISNT(i < 2);
200     return ((i > 1) ? 1 : 0);
201 }
202
203 ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey)
204 {
205     return eckey->engine;
206 }
207
208 int EC_KEY_generate_key(EC_KEY *eckey)
209 {
210     if (eckey == NULL || eckey->group == NULL) {
211         ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
212         return 0;
213     }
214     if (eckey->meth->keygen != NULL) {
215         int ret;
216
217         ret = eckey->meth->keygen(eckey);
218         if (ret == 1)
219             eckey->dirty_cnt++;
220
221         return ret;
222     }
223     ECerr(EC_F_EC_KEY_GENERATE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
224     return 0;
225 }
226
227 int ossl_ec_key_gen(EC_KEY *eckey)
228 {
229     int ret;
230
231     ret = eckey->group->meth->keygen(eckey);
232
233     if (ret == 1)
234         eckey->dirty_cnt++;
235     return ret;
236 }
237
238 /*
239  * ECC Key generation.
240  * See SP800-56AR3 5.6.1.2.2 "Key Pair Generation by Testing Candidates"
241  *
242  * Params:
243  *     eckey An EC key object that contains domain params. The generated keypair
244  *           is stored in this object.
245  * Returns 1 if the keypair was generated or 0 otherwise.
246  */
247 int ec_key_simple_generate_key(EC_KEY *eckey)
248 {
249     int ok = 0;
250     BIGNUM *priv_key = NULL;
251     const BIGNUM *order = NULL;
252     EC_POINT *pub_key = NULL;
253     const EC_GROUP *group = eckey->group;
254     BN_CTX *ctx = BN_CTX_secure_new_ex(eckey->libctx);
255
256     if (ctx == NULL)
257         goto err;
258
259     if (eckey->priv_key == NULL) {
260         priv_key = BN_secure_new();
261         if (priv_key == NULL)
262             goto err;
263     } else
264         priv_key = eckey->priv_key;
265
266     /*
267      * Steps (1-2): Check domain parameters and security strength.
268      * These steps must be done by the user. This would need to be
269      * stated in the security policy.
270      */
271
272     order = EC_GROUP_get0_order(group);
273     if (order == NULL)
274         goto err;
275
276     /*
277      * Steps (3-7): priv_key = DRBG_RAND(order_n_bits) (range [1, n-1]).
278      * Although this is slightly different from the standard, it is effectively
279      * equivalent as it gives an unbiased result ranging from 1..n-1. It is also
280      * faster as the standard needs to retry more often. Also doing
281      * 1 + rand[0..n-2] would effect the way that tests feed dummy entropy into
282      * rand so the simpler backward compatible method has been used here.
283      */
284     do
285         if (!BN_priv_rand_range_ex(priv_key, order, ctx))
286             goto err;
287     while (BN_is_zero(priv_key)) ;
288
289     if (eckey->pub_key == NULL) {
290         pub_key = EC_POINT_new(group);
291         if (pub_key == NULL)
292             goto err;
293     } else
294         pub_key = eckey->pub_key;
295
296     /* Step (8) : pub_key = priv_key * G (where G is a point on the curve) */
297     if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx))
298         goto err;
299
300     eckey->priv_key = priv_key;
301     eckey->pub_key = pub_key;
302     priv_key = NULL;
303     pub_key = NULL;
304
305     eckey->dirty_cnt++;
306
307     ok = 1;
308
309 err:
310     /* Step (9): If there is an error return an invalid keypair. */
311     if (!ok) {
312         BN_clear(eckey->priv_key);
313         if (eckey->pub_key != NULL)
314             EC_POINT_set_to_infinity(group, eckey->pub_key);
315     }
316
317     EC_POINT_free(pub_key);
318     BN_clear_free(priv_key);
319     BN_CTX_free(ctx);
320     return ok;
321 }
322
323 int ec_key_simple_generate_public_key(EC_KEY *eckey)
324 {
325     int ret;
326
327     /*
328      * See SP800-56AR3 5.6.1.2.2: Step (8)
329      * pub_key = priv_key * G (where G is a point on the curve)
330      */
331     ret = EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,
332                        NULL, NULL);
333
334     if (ret == 1)
335         eckey->dirty_cnt++;
336
337     return ret;
338 }
339
340 int EC_KEY_check_key(const EC_KEY *eckey)
341 {
342     if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
343         ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
344         return 0;
345     }
346
347     if (eckey->group->meth->keycheck == NULL) {
348         ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
349         return 0;
350     }
351
352     return eckey->group->meth->keycheck(eckey);
353 }
354
355 /*
356  * Check the range of the EC public key.
357  * See SP800-56A R3 Section 5.6.2.3.3 (Part 2)
358  * i.e.
359  *  - If q = odd prime p: Verify that xQ and yQ are integers in the
360  *    interval[0, p - 1], OR
361  *  - If q = 2m: Verify that xQ and yQ are bit strings of length m bits.
362  * Returns 1 if the public key has a valid range, otherwise it returns 0.
363  */
364 static int ec_key_public_range_check(BN_CTX *ctx, const EC_KEY *key)
365 {
366     int ret = 0;
367     BIGNUM *x, *y;
368
369     BN_CTX_start(ctx);
370     x = BN_CTX_get(ctx);
371     y = BN_CTX_get(ctx);
372     if (y == NULL)
373         goto err;
374
375     if (!EC_POINT_get_affine_coordinates(key->group, key->pub_key, x, y, ctx))
376         goto err;
377
378     if (EC_METHOD_get_field_type(key->group->meth) == NID_X9_62_prime_field) {
379         if (BN_is_negative(x)
380             || BN_cmp(x, key->group->field) >= 0
381             || BN_is_negative(y)
382             || BN_cmp(y, key->group->field) >= 0) {
383             goto err;
384         }
385     } else {
386         int m = EC_GROUP_get_degree(key->group);
387         if (BN_num_bits(x) > m || BN_num_bits(y) > m) {
388             goto err;
389         }
390     }
391     ret = 1;
392 err:
393     BN_CTX_end(ctx);
394     return ret;
395 }
396
397 /*
398  * ECC Key validation as specified in SP800-56A R3.
399  *    Section 5.6.2.3.3 ECC Full Public-Key Validation
400  *    Section 5.6.2.1.2 Owner Assurance of Private-Key Validity
401  *    Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
402  * NOTES:
403  *    Before calling this method in fips mode, there should be an assurance that
404  *    an approved elliptic-curve group is used.
405  * Returns 1 if the key is valid, otherwise it returns 0.
406  */
407 int ec_key_simple_check_key(const EC_KEY *eckey)
408 {
409     int ok = 0;
410     BN_CTX *ctx = NULL;
411     const BIGNUM *order = NULL;
412     EC_POINT *point = NULL;
413
414     if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
415         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
416         return 0;
417     }
418
419     /* 5.6.2.3.3 (Step 1): Q != infinity */
420     if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
421         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_AT_INFINITY);
422         goto err;
423     }
424
425     if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL)
426         goto err;
427
428     if ((point = EC_POINT_new(eckey->group)) == NULL)
429         goto err;
430
431     /* 5.6.2.3.3 (Step 2) Test if the public key is in range */
432     if (!ec_key_public_range_check(ctx, eckey)) {
433         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_COORDINATES_OUT_OF_RANGE);
434         goto err;
435     }
436
437     /* 5.6.2.3.3 (Step 3) is the pub_key on the elliptic curve */
438     if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
439         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
440         goto err;
441     }
442
443     order = eckey->group->order;
444     if (BN_is_zero(order)) {
445         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
446         goto err;
447     }
448     /* 5.6.2.3.3 (Step 4) : pub_key * order is the point at infinity. */
449     if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
450         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
451         goto err;
452     }
453     if (!EC_POINT_is_at_infinity(eckey->group, point)) {
454         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
455         goto err;
456     }
457
458     if (eckey->priv_key != NULL) {
459         /*
460          * 5.6.2.1.2 Owner Assurance of Private-Key Validity
461          * The private key is in the range [1, order-1]
462          */
463         if (BN_cmp(eckey->priv_key, BN_value_one()) < 0
464                 || BN_cmp(eckey->priv_key, order) >= 0) {
465             ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
466             goto err;
467         }
468         /*
469          * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency (b)
470          * Check if generator * priv_key = pub_key
471          */
472         if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
473                           NULL, NULL, ctx)) {
474             ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
475             goto err;
476         }
477         if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
478             ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
479             goto err;
480         }
481     }
482     ok = 1;
483  err:
484     BN_CTX_free(ctx);
485     EC_POINT_free(point);
486     return ok;
487 }
488
489 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
490                                              BIGNUM *y)
491 {
492     BN_CTX *ctx = NULL;
493     BIGNUM *tx, *ty;
494     EC_POINT *point = NULL;
495     int ok = 0;
496
497     if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
498         ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
499               ERR_R_PASSED_NULL_PARAMETER);
500         return 0;
501     }
502     ctx = BN_CTX_new_ex(key->libctx);
503     if (ctx == NULL)
504         return 0;
505
506     BN_CTX_start(ctx);
507     point = EC_POINT_new(key->group);
508
509     if (point == NULL)
510         goto err;
511
512     tx = BN_CTX_get(ctx);
513     ty = BN_CTX_get(ctx);
514     if (ty == NULL)
515         goto err;
516
517     if (!EC_POINT_set_affine_coordinates(key->group, point, x, y, ctx))
518         goto err;
519     if (!EC_POINT_get_affine_coordinates(key->group, point, tx, ty, ctx))
520         goto err;
521
522     /*
523      * Check if retrieved coordinates match originals. The range check is done
524      * inside EC_KEY_check_key().
525      */
526     if (BN_cmp(x, tx) || BN_cmp(y, ty)) {
527         ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
528               EC_R_COORDINATES_OUT_OF_RANGE);
529         goto err;
530     }
531
532     /* EC_KEY_set_public_key updates dirty_cnt */
533     if (!EC_KEY_set_public_key(key, point))
534         goto err;
535
536     if (EC_KEY_check_key(key) == 0)
537         goto err;
538
539     ok = 1;
540
541  err:
542     BN_CTX_end(ctx);
543     BN_CTX_free(ctx);
544     EC_POINT_free(point);
545     return ok;
546
547 }
548
549 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
550 {
551     return key->group;
552 }
553
554 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
555 {
556     if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)
557         return 0;
558     EC_GROUP_free(key->group);
559     key->group = EC_GROUP_dup(group);
560     key->dirty_cnt++;
561     return (key->group == NULL) ? 0 : 1;
562 }
563
564 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
565 {
566     return key->priv_key;
567 }
568
569 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
570 {
571     if (key->group == NULL || key->group->meth == NULL)
572         return 0;
573     if (key->group->meth->set_private != NULL
574         && key->group->meth->set_private(key, priv_key) == 0)
575         return 0;
576     if (key->meth->set_private != NULL
577         && key->meth->set_private(key, priv_key) == 0)
578         return 0;
579     BN_clear_free(key->priv_key);
580     key->priv_key = BN_dup(priv_key);
581     key->dirty_cnt++;
582     return (key->priv_key == NULL) ? 0 : 1;
583 }
584
585 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
586 {
587     return key->pub_key;
588 }
589
590 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
591 {
592     if (key->meth->set_public != NULL
593         && key->meth->set_public(key, pub_key) == 0)
594         return 0;
595     EC_POINT_free(key->pub_key);
596     key->pub_key = EC_POINT_dup(pub_key, key->group);
597     key->dirty_cnt++;
598     return (key->pub_key == NULL) ? 0 : 1;
599 }
600
601 unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
602 {
603     return key->enc_flag;
604 }
605
606 void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
607 {
608     key->enc_flag = flags;
609 }
610
611 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
612 {
613     return key->conv_form;
614 }
615
616 void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
617 {
618     key->conv_form = cform;
619     if (key->group != NULL)
620         EC_GROUP_set_point_conversion_form(key->group, cform);
621 }
622
623 void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
624 {
625     if (key->group != NULL)
626         EC_GROUP_set_asn1_flag(key->group, flag);
627 }
628
629 int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
630 {
631     if (key->group == NULL)
632         return 0;
633     return EC_GROUP_precompute_mult(key->group, ctx);
634 }
635
636 int EC_KEY_get_flags(const EC_KEY *key)
637 {
638     return key->flags;
639 }
640
641 void EC_KEY_set_flags(EC_KEY *key, int flags)
642 {
643     key->flags |= flags;
644     key->dirty_cnt++;
645 }
646
647 void EC_KEY_clear_flags(EC_KEY *key, int flags)
648 {
649     key->flags &= ~flags;
650     key->dirty_cnt++;
651 }
652
653 size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
654                         unsigned char **pbuf, BN_CTX *ctx)
655 {
656     if (key == NULL || key->pub_key == NULL || key->group == NULL)
657         return 0;
658     return EC_POINT_point2buf(key->group, key->pub_key, form, pbuf, ctx);
659 }
660
661 int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
662                    BN_CTX *ctx)
663 {
664     if (key == NULL || key->group == NULL)
665         return 0;
666     if (key->pub_key == NULL)
667         key->pub_key = EC_POINT_new(key->group);
668     if (key->pub_key == NULL)
669         return 0;
670     if (EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx) == 0)
671         return 0;
672     key->dirty_cnt++;
673     /*
674      * Save the point conversion form.
675      * For non-custom curves the first octet of the buffer (excluding
676      * the last significant bit) contains the point conversion form.
677      * EC_POINT_oct2point() has already performed sanity checking of
678      * the buffer so we know it is valid.
679      */
680     if ((key->group->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0)
681         key->conv_form = (point_conversion_form_t)(buf[0] & ~0x01);
682     return 1;
683 }
684
685 size_t EC_KEY_priv2oct(const EC_KEY *eckey,
686                        unsigned char *buf, size_t len)
687 {
688     if (eckey->group == NULL || eckey->group->meth == NULL)
689         return 0;
690     if (eckey->group->meth->priv2oct == NULL) {
691         ECerr(EC_F_EC_KEY_PRIV2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
692         return 0;
693     }
694
695     return eckey->group->meth->priv2oct(eckey, buf, len);
696 }
697
698 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
699                               unsigned char *buf, size_t len)
700 {
701     size_t buf_len;
702
703     buf_len = (EC_GROUP_order_bits(eckey->group) + 7) / 8;
704     if (eckey->priv_key == NULL)
705         return 0;
706     if (buf == NULL)
707         return buf_len;
708     else if (len < buf_len)
709         return 0;
710
711     /* Octetstring may need leading zeros if BN is to short */
712
713     if (BN_bn2binpad(eckey->priv_key, buf, buf_len) == -1) {
714         ECerr(EC_F_EC_KEY_SIMPLE_PRIV2OCT, EC_R_BUFFER_TOO_SMALL);
715         return 0;
716     }
717
718     return buf_len;
719 }
720
721 int EC_KEY_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
722 {
723     int ret;
724
725     if (eckey->group == NULL || eckey->group->meth == NULL)
726         return 0;
727     if (eckey->group->meth->oct2priv == NULL) {
728         ECerr(EC_F_EC_KEY_OCT2PRIV, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
729         return 0;
730     }
731     ret = eckey->group->meth->oct2priv(eckey, buf, len);
732     if (ret == 1)
733         eckey->dirty_cnt++;
734     return ret;
735 }
736
737 int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
738 {
739     if (eckey->priv_key == NULL)
740         eckey->priv_key = BN_secure_new();
741     if (eckey->priv_key == NULL) {
742         ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_MALLOC_FAILURE);
743         return 0;
744     }
745     eckey->priv_key = BN_bin2bn(buf, len, eckey->priv_key);
746     if (eckey->priv_key == NULL) {
747         ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_BN_LIB);
748         return 0;
749     }
750     eckey->dirty_cnt++;
751     return 1;
752 }
753
754 size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf)
755 {
756     size_t len;
757     unsigned char *buf;
758
759     len = EC_KEY_priv2oct(eckey, NULL, 0);
760     if (len == 0)
761         return 0;
762     if ((buf = OPENSSL_malloc(len)) == NULL) {
763         ECerr(EC_F_EC_KEY_PRIV2BUF, ERR_R_MALLOC_FAILURE);
764         return 0;
765     }
766     len = EC_KEY_priv2oct(eckey, buf, len);
767     if (len == 0) {
768         OPENSSL_free(buf);
769         return 0;
770     }
771     *pbuf = buf;
772     return len;
773 }
774
775 int EC_KEY_can_sign(const EC_KEY *eckey)
776 {
777     if (eckey->group == NULL || eckey->group->meth == NULL
778         || (eckey->group->meth->flags & EC_FLAGS_NO_SIGN))
779         return 0;
780     return 1;
781 }