]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ec/ec_key.c
Deprecate the ECDSA and EV_KEY_METHOD functions.
[thirdparty/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 return dest;
173 }
174
175 EC_KEY *EC_KEY_dup(const EC_KEY *ec_key)
176 {
177 EC_KEY *ret = ec_key_new_method_int(ec_key->libctx, ec_key->engine);
178
179 if (ret == NULL)
180 return NULL;
181
182 if (EC_KEY_copy(ret, ec_key) == NULL) {
183 EC_KEY_free(ret);
184 return NULL;
185 }
186 return ret;
187 }
188
189 int EC_KEY_up_ref(EC_KEY *r)
190 {
191 int i;
192
193 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
194 return 0;
195
196 REF_PRINT_COUNT("EC_KEY", r);
197 REF_ASSERT_ISNT(i < 2);
198 return ((i > 1) ? 1 : 0);
199 }
200
201 ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey)
202 {
203 return eckey->engine;
204 }
205
206 int EC_KEY_generate_key(EC_KEY *eckey)
207 {
208 if (eckey == NULL || eckey->group == NULL) {
209 ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
210 return 0;
211 }
212 if (eckey->meth->keygen != NULL)
213 return eckey->meth->keygen(eckey);
214 ECerr(EC_F_EC_KEY_GENERATE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
215 return 0;
216 }
217
218 int ossl_ec_key_gen(EC_KEY *eckey)
219 {
220 return eckey->group->meth->keygen(eckey);
221 }
222
223 /*
224 * ECC Key generation.
225 * See SP800-56AR3 5.6.1.2.2 "Key Pair Generation by Testing Candidates"
226 *
227 * Params:
228 * eckey An EC key object that contains domain params. The generated keypair
229 * is stored in this object.
230 * Returns 1 if the keypair was generated or 0 otherwise.
231 */
232 int ec_key_simple_generate_key(EC_KEY *eckey)
233 {
234 int ok = 0;
235 BIGNUM *priv_key = NULL;
236 const BIGNUM *order = NULL;
237 EC_POINT *pub_key = NULL;
238 const EC_GROUP *group = eckey->group;
239 BN_CTX *ctx = BN_CTX_secure_new_ex(eckey->libctx);
240
241 if (ctx == NULL)
242 goto err;
243
244 if (eckey->priv_key == NULL) {
245 priv_key = BN_secure_new();
246 if (priv_key == NULL)
247 goto err;
248 } else
249 priv_key = eckey->priv_key;
250
251 /*
252 * Steps (1-2): Check domain parameters and security strength.
253 * These steps must be done by the user. This would need to be
254 * stated in the security policy.
255 */
256
257 order = EC_GROUP_get0_order(group);
258 if (order == NULL)
259 goto err;
260
261 /*
262 * Steps (3-7): priv_key = DRBG_RAND(order_n_bits) (range [1, n-1]).
263 * Although this is slightly different from the standard, it is effectively
264 * equivalent as it gives an unbiased result ranging from 1..n-1. It is also
265 * faster as the standard needs to retry more often. Also doing
266 * 1 + rand[0..n-2] would effect the way that tests feed dummy entropy into
267 * rand so the simpler backward compatible method has been used here.
268 */
269 do
270 if (!BN_priv_rand_range_ex(priv_key, order, ctx))
271 goto err;
272 while (BN_is_zero(priv_key)) ;
273
274 if (eckey->pub_key == NULL) {
275 pub_key = EC_POINT_new(group);
276 if (pub_key == NULL)
277 goto err;
278 } else
279 pub_key = eckey->pub_key;
280
281 /* Step (8) : pub_key = priv_key * G (where G is a point on the curve) */
282 if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx))
283 goto err;
284
285 eckey->priv_key = priv_key;
286 eckey->pub_key = pub_key;
287 priv_key = NULL;
288 pub_key = NULL;
289
290 ok = 1;
291
292 err:
293 /* Step (9): If there is an error return an invalid keypair. */
294 if (!ok) {
295 BN_clear(eckey->priv_key);
296 if (eckey->pub_key != NULL)
297 EC_POINT_set_to_infinity(group, eckey->pub_key);
298 }
299
300 EC_POINT_free(pub_key);
301 BN_clear_free(priv_key);
302 BN_CTX_free(ctx);
303 return ok;
304 }
305
306 int ec_key_simple_generate_public_key(EC_KEY *eckey)
307 {
308 /*
309 * See SP800-56AR3 5.6.1.2.2: Step (8)
310 * pub_key = priv_key * G (where G is a point on the curve)
311 */
312 return EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,
313 NULL, NULL);
314 }
315
316 int EC_KEY_check_key(const EC_KEY *eckey)
317 {
318 if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
319 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
320 return 0;
321 }
322
323 if (eckey->group->meth->keycheck == NULL) {
324 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
325 return 0;
326 }
327
328 return eckey->group->meth->keycheck(eckey);
329 }
330
331 /*
332 * Check the range of the EC public key.
333 * See SP800-56A R3 Section 5.6.2.3.3 (Part 2)
334 * i.e.
335 * - If q = odd prime p: Verify that xQ and yQ are integers in the
336 * interval[0, p - 1], OR
337 * - If q = 2m: Verify that xQ and yQ are bit strings of length m bits.
338 * Returns 1 if the public key has a valid range, otherwise it returns 0.
339 */
340 static int ec_key_public_range_check(BN_CTX *ctx, const EC_KEY *key)
341 {
342 int ret = 0;
343 BIGNUM *x, *y;
344
345 BN_CTX_start(ctx);
346 x = BN_CTX_get(ctx);
347 y = BN_CTX_get(ctx);
348 if (y == NULL)
349 goto err;
350
351 if (!EC_POINT_get_affine_coordinates(key->group, key->pub_key, x, y, ctx))
352 goto err;
353
354 if (EC_METHOD_get_field_type(key->group->meth) == NID_X9_62_prime_field) {
355 if (BN_is_negative(x)
356 || BN_cmp(x, key->group->field) >= 0
357 || BN_is_negative(y)
358 || BN_cmp(y, key->group->field) >= 0) {
359 goto err;
360 }
361 } else {
362 int m = EC_GROUP_get_degree(key->group);
363 if (BN_num_bits(x) > m || BN_num_bits(y) > m) {
364 goto err;
365 }
366 }
367 ret = 1;
368 err:
369 BN_CTX_end(ctx);
370 return ret;
371 }
372
373 /*
374 * ECC Key validation as specified in SP800-56A R3.
375 * Section 5.6.2.3.3 ECC Full Public-Key Validation
376 * Section 5.6.2.1.2 Owner Assurance of Private-Key Validity
377 * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
378 * NOTES:
379 * Before calling this method in fips mode, there should be an assurance that
380 * an approved elliptic-curve group is used.
381 * Returns 1 if the key is valid, otherwise it returns 0.
382 */
383 int ec_key_simple_check_key(const EC_KEY *eckey)
384 {
385 int ok = 0;
386 BN_CTX *ctx = NULL;
387 const BIGNUM *order = NULL;
388 EC_POINT *point = NULL;
389
390 if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
391 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
392 return 0;
393 }
394
395 /* 5.6.2.3.3 (Step 1): Q != infinity */
396 if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
397 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_AT_INFINITY);
398 goto err;
399 }
400
401 if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL)
402 goto err;
403
404 if ((point = EC_POINT_new(eckey->group)) == NULL)
405 goto err;
406
407 /* 5.6.2.3.3 (Step 2) Test if the public key is in range */
408 if (!ec_key_public_range_check(ctx, eckey)) {
409 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_COORDINATES_OUT_OF_RANGE);
410 goto err;
411 }
412
413 /* 5.6.2.3.3 (Step 3) is the pub_key on the elliptic curve */
414 if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
415 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
416 goto err;
417 }
418
419 order = eckey->group->order;
420 if (BN_is_zero(order)) {
421 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
422 goto err;
423 }
424 /* 5.6.2.3.3 (Step 4) : pub_key * order is the point at infinity. */
425 if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
426 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
427 goto err;
428 }
429 if (!EC_POINT_is_at_infinity(eckey->group, point)) {
430 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
431 goto err;
432 }
433
434 if (eckey->priv_key != NULL) {
435 /*
436 * 5.6.2.1.2 Owner Assurance of Private-Key Validity
437 * The private key is in the range [1, order-1]
438 */
439 if (BN_cmp(eckey->priv_key, BN_value_one()) < 0
440 || BN_cmp(eckey->priv_key, order) >= 0) {
441 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
442 goto err;
443 }
444 /*
445 * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency (b)
446 * Check if generator * priv_key = pub_key
447 */
448 if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
449 NULL, NULL, ctx)) {
450 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
451 goto err;
452 }
453 if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
454 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
455 goto err;
456 }
457 }
458 ok = 1;
459 err:
460 BN_CTX_free(ctx);
461 EC_POINT_free(point);
462 return ok;
463 }
464
465 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
466 BIGNUM *y)
467 {
468 BN_CTX *ctx = NULL;
469 BIGNUM *tx, *ty;
470 EC_POINT *point = NULL;
471 int ok = 0;
472
473 if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
474 ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
475 ERR_R_PASSED_NULL_PARAMETER);
476 return 0;
477 }
478 ctx = BN_CTX_new_ex(key->libctx);
479 if (ctx == NULL)
480 return 0;
481
482 BN_CTX_start(ctx);
483 point = EC_POINT_new(key->group);
484
485 if (point == NULL)
486 goto err;
487
488 tx = BN_CTX_get(ctx);
489 ty = BN_CTX_get(ctx);
490 if (ty == NULL)
491 goto err;
492
493 if (!EC_POINT_set_affine_coordinates(key->group, point, x, y, ctx))
494 goto err;
495 if (!EC_POINT_get_affine_coordinates(key->group, point, tx, ty, ctx))
496 goto err;
497
498 /*
499 * Check if retrieved coordinates match originals. The range check is done
500 * inside EC_KEY_check_key().
501 */
502 if (BN_cmp(x, tx) || BN_cmp(y, ty)) {
503 ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
504 EC_R_COORDINATES_OUT_OF_RANGE);
505 goto err;
506 }
507
508 if (!EC_KEY_set_public_key(key, point))
509 goto err;
510
511 if (EC_KEY_check_key(key) == 0)
512 goto err;
513
514 ok = 1;
515
516 err:
517 BN_CTX_end(ctx);
518 BN_CTX_free(ctx);
519 EC_POINT_free(point);
520 return ok;
521
522 }
523
524 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
525 {
526 return key->group;
527 }
528
529 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
530 {
531 if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)
532 return 0;
533 EC_GROUP_free(key->group);
534 key->group = EC_GROUP_dup(group);
535 return (key->group == NULL) ? 0 : 1;
536 }
537
538 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
539 {
540 return key->priv_key;
541 }
542
543 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
544 {
545 if (key->group == NULL || key->group->meth == NULL)
546 return 0;
547 if (key->group->meth->set_private != NULL
548 && key->group->meth->set_private(key, priv_key) == 0)
549 return 0;
550 if (key->meth->set_private != NULL
551 && key->meth->set_private(key, priv_key) == 0)
552 return 0;
553 BN_clear_free(key->priv_key);
554 key->priv_key = BN_dup(priv_key);
555 return (key->priv_key == NULL) ? 0 : 1;
556 }
557
558 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
559 {
560 return key->pub_key;
561 }
562
563 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
564 {
565 if (key->meth->set_public != NULL
566 && key->meth->set_public(key, pub_key) == 0)
567 return 0;
568 EC_POINT_free(key->pub_key);
569 key->pub_key = EC_POINT_dup(pub_key, key->group);
570 return (key->pub_key == NULL) ? 0 : 1;
571 }
572
573 unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
574 {
575 return key->enc_flag;
576 }
577
578 void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
579 {
580 key->enc_flag = flags;
581 }
582
583 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
584 {
585 return key->conv_form;
586 }
587
588 void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
589 {
590 key->conv_form = cform;
591 if (key->group != NULL)
592 EC_GROUP_set_point_conversion_form(key->group, cform);
593 }
594
595 void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
596 {
597 if (key->group != NULL)
598 EC_GROUP_set_asn1_flag(key->group, flag);
599 }
600
601 int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
602 {
603 if (key->group == NULL)
604 return 0;
605 return EC_GROUP_precompute_mult(key->group, ctx);
606 }
607
608 int EC_KEY_get_flags(const EC_KEY *key)
609 {
610 return key->flags;
611 }
612
613 void EC_KEY_set_flags(EC_KEY *key, int flags)
614 {
615 key->flags |= flags;
616 }
617
618 void EC_KEY_clear_flags(EC_KEY *key, int flags)
619 {
620 key->flags &= ~flags;
621 }
622
623 size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
624 unsigned char **pbuf, BN_CTX *ctx)
625 {
626 if (key == NULL || key->pub_key == NULL || key->group == NULL)
627 return 0;
628 return EC_POINT_point2buf(key->group, key->pub_key, form, pbuf, ctx);
629 }
630
631 int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
632 BN_CTX *ctx)
633 {
634 if (key == NULL || key->group == NULL)
635 return 0;
636 if (key->pub_key == NULL)
637 key->pub_key = EC_POINT_new(key->group);
638 if (key->pub_key == NULL)
639 return 0;
640 if (EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx) == 0)
641 return 0;
642 /*
643 * Save the point conversion form.
644 * For non-custom curves the first octet of the buffer (excluding
645 * the last significant bit) contains the point conversion form.
646 * EC_POINT_oct2point() has already performed sanity checking of
647 * the buffer so we know it is valid.
648 */
649 if ((key->group->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0)
650 key->conv_form = (point_conversion_form_t)(buf[0] & ~0x01);
651 return 1;
652 }
653
654 size_t EC_KEY_priv2oct(const EC_KEY *eckey,
655 unsigned char *buf, size_t len)
656 {
657 if (eckey->group == NULL || eckey->group->meth == NULL)
658 return 0;
659 if (eckey->group->meth->priv2oct == NULL) {
660 ECerr(EC_F_EC_KEY_PRIV2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
661 return 0;
662 }
663
664 return eckey->group->meth->priv2oct(eckey, buf, len);
665 }
666
667 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
668 unsigned char *buf, size_t len)
669 {
670 size_t buf_len;
671
672 buf_len = (EC_GROUP_order_bits(eckey->group) + 7) / 8;
673 if (eckey->priv_key == NULL)
674 return 0;
675 if (buf == NULL)
676 return buf_len;
677 else if (len < buf_len)
678 return 0;
679
680 /* Octetstring may need leading zeros if BN is to short */
681
682 if (BN_bn2binpad(eckey->priv_key, buf, buf_len) == -1) {
683 ECerr(EC_F_EC_KEY_SIMPLE_PRIV2OCT, EC_R_BUFFER_TOO_SMALL);
684 return 0;
685 }
686
687 return buf_len;
688 }
689
690 int EC_KEY_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
691 {
692 if (eckey->group == NULL || eckey->group->meth == NULL)
693 return 0;
694 if (eckey->group->meth->oct2priv == NULL) {
695 ECerr(EC_F_EC_KEY_OCT2PRIV, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
696 return 0;
697 }
698 return eckey->group->meth->oct2priv(eckey, buf, len);
699 }
700
701 int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
702 {
703 if (eckey->priv_key == NULL)
704 eckey->priv_key = BN_secure_new();
705 if (eckey->priv_key == NULL) {
706 ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_MALLOC_FAILURE);
707 return 0;
708 }
709 eckey->priv_key = BN_bin2bn(buf, len, eckey->priv_key);
710 if (eckey->priv_key == NULL) {
711 ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_BN_LIB);
712 return 0;
713 }
714 return 1;
715 }
716
717 size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf)
718 {
719 size_t len;
720 unsigned char *buf;
721
722 len = EC_KEY_priv2oct(eckey, NULL, 0);
723 if (len == 0)
724 return 0;
725 if ((buf = OPENSSL_malloc(len)) == NULL) {
726 ECerr(EC_F_EC_KEY_PRIV2BUF, ERR_R_MALLOC_FAILURE);
727 return 0;
728 }
729 len = EC_KEY_priv2oct(eckey, buf, len);
730 if (len == 0) {
731 OPENSSL_free(buf);
732 return 0;
733 }
734 *pbuf = buf;
735 return len;
736 }
737
738 int EC_KEY_can_sign(const EC_KEY *eckey)
739 {
740 if (eckey->group == NULL || eckey->group->meth == NULL
741 || (eckey->group->meth->flags & EC_FLAGS_NO_SIGN))
742 return 0;
743 return 1;
744 }