diff options
| author | tb <> | 2024-11-16 10:38:10 +0000 |
|---|---|---|
| committer | tb <> | 2024-11-16 10:38:10 +0000 |
| commit | b1f07f73ab7656f59ffa98b14ae58c3676daf4b3 (patch) | |
| tree | 2eb0f983e5bc974a2ceef774d66dbddefc4c88ec /src/lib/libcrypto/ec/ec_key.c | |
| parent | e1b97e0219c5fcfb1e2f5b08fba245e61a96e1ac (diff) | |
| download | openbsd-b1f07f73ab7656f59ffa98b14ae58c3676daf4b3.tar.gz openbsd-b1f07f73ab7656f59ffa98b14ae58c3676daf4b3.tar.bz2 openbsd-b1f07f73ab7656f59ffa98b14ae58c3676daf4b3.zip | |
Merge ec_kmeth into ec_key
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libcrypto/ec/ec_key.c | 272 |
1 files changed, 271 insertions, 1 deletions
diff --git a/src/lib/libcrypto/ec/ec_key.c b/src/lib/libcrypto/ec/ec_key.c index 662a7c0f49..a0a8ff2084 100644 --- a/src/lib/libcrypto/ec/ec_key.c +++ b/src/lib/libcrypto/ec/ec_key.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: ec_key.c,v 1.47 2024/11/15 08:49:07 tb Exp $ */ | 1 | /* $OpenBSD: ec_key.c,v 1.48 2024/11/16 10:38:10 tb Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Written by Nils Larsch for the OpenSSL project. | 3 | * Written by Nils Larsch for the OpenSSL project. |
| 4 | */ | 4 | */ |
| @@ -65,10 +65,12 @@ | |||
| 65 | 65 | ||
| 66 | #include <openssl/opensslconf.h> | 66 | #include <openssl/opensslconf.h> |
| 67 | 67 | ||
| 68 | #include <openssl/ec.h> | ||
| 68 | #include <openssl/err.h> | 69 | #include <openssl/err.h> |
| 69 | 70 | ||
| 70 | #include "bn_local.h" | 71 | #include "bn_local.h" |
| 71 | #include "ec_local.h" | 72 | #include "ec_local.h" |
| 73 | #include "ecdsa_local.h" | ||
| 72 | 74 | ||
| 73 | EC_KEY * | 75 | EC_KEY * |
| 74 | EC_KEY_new(void) | 76 | EC_KEY_new(void) |
| @@ -535,3 +537,271 @@ EC_KEY_clear_flags(EC_KEY *key, int flags) | |||
| 535 | key->flags &= ~flags; | 537 | key->flags &= ~flags; |
| 536 | } | 538 | } |
| 537 | LCRYPTO_ALIAS(EC_KEY_clear_flags); | 539 | LCRYPTO_ALIAS(EC_KEY_clear_flags); |
| 540 | |||
| 541 | const EC_KEY_METHOD * | ||
| 542 | EC_KEY_get_method(const EC_KEY *key) | ||
| 543 | { | ||
| 544 | return key->meth; | ||
| 545 | } | ||
| 546 | LCRYPTO_ALIAS(EC_KEY_get_method); | ||
| 547 | |||
| 548 | int | ||
| 549 | EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth) | ||
| 550 | { | ||
| 551 | void (*finish)(EC_KEY *key) = key->meth->finish; | ||
| 552 | |||
| 553 | if (finish != NULL) | ||
| 554 | finish(key); | ||
| 555 | |||
| 556 | key->meth = meth; | ||
| 557 | if (meth->init != NULL) | ||
| 558 | return meth->init(key); | ||
| 559 | return 1; | ||
| 560 | } | ||
| 561 | LCRYPTO_ALIAS(EC_KEY_set_method); | ||
| 562 | |||
| 563 | EC_KEY * | ||
| 564 | EC_KEY_new_method(ENGINE *engine) | ||
| 565 | { | ||
| 566 | EC_KEY *ret; | ||
| 567 | |||
| 568 | if ((ret = calloc(1, sizeof(EC_KEY))) == NULL) { | ||
| 569 | ECerror(ERR_R_MALLOC_FAILURE); | ||
| 570 | return NULL; | ||
| 571 | } | ||
| 572 | ret->meth = EC_KEY_get_default_method(); | ||
| 573 | ret->version = 1; | ||
| 574 | ret->flags = 0; | ||
| 575 | ret->group = NULL; | ||
| 576 | ret->pub_key = NULL; | ||
| 577 | ret->priv_key = NULL; | ||
| 578 | ret->enc_flag = 0; | ||
| 579 | ret->conv_form = POINT_CONVERSION_UNCOMPRESSED; | ||
| 580 | ret->references = 1; | ||
| 581 | |||
| 582 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data)) | ||
| 583 | goto err; | ||
| 584 | if (ret->meth->init != NULL && ret->meth->init(ret) == 0) | ||
| 585 | goto err; | ||
| 586 | |||
| 587 | return ret; | ||
| 588 | |||
| 589 | err: | ||
| 590 | EC_KEY_free(ret); | ||
| 591 | return NULL; | ||
| 592 | } | ||
| 593 | LCRYPTO_ALIAS(EC_KEY_new_method); | ||
| 594 | |||
| 595 | EC_KEY_METHOD * | ||
| 596 | EC_KEY_METHOD_new(const EC_KEY_METHOD *meth) | ||
| 597 | { | ||
| 598 | EC_KEY_METHOD *ret; | ||
| 599 | |||
| 600 | if ((ret = calloc(1, sizeof(*meth))) == NULL) | ||
| 601 | return NULL; | ||
| 602 | if (meth != NULL) | ||
| 603 | *ret = *meth; | ||
| 604 | ret->flags |= EC_KEY_METHOD_DYNAMIC; | ||
| 605 | return ret; | ||
| 606 | } | ||
| 607 | LCRYPTO_ALIAS(EC_KEY_METHOD_new); | ||
| 608 | |||
| 609 | void | ||
| 610 | EC_KEY_METHOD_free(EC_KEY_METHOD *meth) | ||
| 611 | { | ||
| 612 | if (meth == NULL) | ||
| 613 | return; | ||
| 614 | if (meth->flags & EC_KEY_METHOD_DYNAMIC) | ||
| 615 | free(meth); | ||
| 616 | } | ||
| 617 | LCRYPTO_ALIAS(EC_KEY_METHOD_free); | ||
| 618 | |||
| 619 | void | ||
| 620 | EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth, | ||
| 621 | int (*init)(EC_KEY *key), | ||
| 622 | void (*finish)(EC_KEY *key), | ||
| 623 | int (*copy)(EC_KEY *dest, const EC_KEY *src), | ||
| 624 | int (*set_group)(EC_KEY *key, const EC_GROUP *grp), | ||
| 625 | int (*set_private)(EC_KEY *key, const BIGNUM *priv_key), | ||
| 626 | int (*set_public)(EC_KEY *key, const EC_POINT *pub_key)) | ||
| 627 | { | ||
| 628 | meth->init = init; | ||
| 629 | meth->finish = finish; | ||
| 630 | meth->copy = copy; | ||
| 631 | meth->set_group = set_group; | ||
| 632 | meth->set_private = set_private; | ||
| 633 | meth->set_public = set_public; | ||
| 634 | } | ||
| 635 | LCRYPTO_ALIAS(EC_KEY_METHOD_set_init); | ||
| 636 | |||
| 637 | void | ||
| 638 | EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth, int (*keygen)(EC_KEY *key)) | ||
| 639 | { | ||
| 640 | meth->keygen = keygen; | ||
| 641 | } | ||
| 642 | LCRYPTO_ALIAS(EC_KEY_METHOD_set_keygen); | ||
| 643 | |||
| 644 | void | ||
| 645 | EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth, | ||
| 646 | int (*ckey)(unsigned char **out, size_t *out_len, const EC_POINT *pub_key, | ||
| 647 | const EC_KEY *ecdh)) | ||
| 648 | { | ||
| 649 | meth->compute_key = ckey; | ||
| 650 | } | ||
| 651 | LCRYPTO_ALIAS(EC_KEY_METHOD_set_compute_key); | ||
| 652 | |||
| 653 | void | ||
| 654 | EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth, | ||
| 655 | int (*sign)(int type, const unsigned char *dgst, | ||
| 656 | int dlen, unsigned char *sig, unsigned int *siglen, | ||
| 657 | const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey), | ||
| 658 | int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, | ||
| 659 | BIGNUM **kinvp, BIGNUM **rp), | ||
| 660 | ECDSA_SIG *(*sign_sig)(const unsigned char *dgst, | ||
| 661 | int dgst_len, const BIGNUM *in_kinv, | ||
| 662 | const BIGNUM *in_r, EC_KEY *eckey)) | ||
| 663 | { | ||
| 664 | meth->sign = sign; | ||
| 665 | meth->sign_setup = sign_setup; | ||
| 666 | meth->sign_sig = sign_sig; | ||
| 667 | } | ||
| 668 | LCRYPTO_ALIAS(EC_KEY_METHOD_set_sign); | ||
| 669 | |||
| 670 | void | ||
| 671 | EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth, | ||
| 672 | int (*verify)(int type, const unsigned char *dgst, int dgst_len, | ||
| 673 | const unsigned char *sigbuf, int sig_len, EC_KEY *eckey), | ||
| 674 | int (*verify_sig)(const unsigned char *dgst, int dgst_len, | ||
| 675 | const ECDSA_SIG *sig, EC_KEY *eckey)) | ||
| 676 | { | ||
| 677 | meth->verify = verify; | ||
| 678 | meth->verify_sig = verify_sig; | ||
| 679 | } | ||
| 680 | LCRYPTO_ALIAS(EC_KEY_METHOD_set_verify); | ||
| 681 | |||
| 682 | |||
| 683 | void | ||
| 684 | EC_KEY_METHOD_get_init(const EC_KEY_METHOD *meth, | ||
| 685 | int (**pinit)(EC_KEY *key), | ||
| 686 | void (**pfinish)(EC_KEY *key), | ||
| 687 | int (**pcopy)(EC_KEY *dest, const EC_KEY *src), | ||
| 688 | int (**pset_group)(EC_KEY *key, const EC_GROUP *grp), | ||
| 689 | int (**pset_private)(EC_KEY *key, const BIGNUM *priv_key), | ||
| 690 | int (**pset_public)(EC_KEY *key, const EC_POINT *pub_key)) | ||
| 691 | { | ||
| 692 | if (pinit != NULL) | ||
| 693 | *pinit = meth->init; | ||
| 694 | if (pfinish != NULL) | ||
| 695 | *pfinish = meth->finish; | ||
| 696 | if (pcopy != NULL) | ||
| 697 | *pcopy = meth->copy; | ||
| 698 | if (pset_group != NULL) | ||
| 699 | *pset_group = meth->set_group; | ||
| 700 | if (pset_private != NULL) | ||
| 701 | *pset_private = meth->set_private; | ||
| 702 | if (pset_public != NULL) | ||
| 703 | *pset_public = meth->set_public; | ||
| 704 | } | ||
| 705 | LCRYPTO_ALIAS(EC_KEY_METHOD_get_init); | ||
| 706 | |||
| 707 | void | ||
| 708 | EC_KEY_METHOD_get_keygen(const EC_KEY_METHOD *meth, | ||
| 709 | int (**pkeygen)(EC_KEY *key)) | ||
| 710 | { | ||
| 711 | if (pkeygen != NULL) | ||
| 712 | *pkeygen = meth->keygen; | ||
| 713 | } | ||
| 714 | LCRYPTO_ALIAS(EC_KEY_METHOD_get_keygen); | ||
| 715 | |||
| 716 | void | ||
| 717 | EC_KEY_METHOD_get_compute_key(const EC_KEY_METHOD *meth, | ||
| 718 | int (**pck)(unsigned char **out, size_t *out_len, const EC_POINT *pub_key, | ||
| 719 | const EC_KEY *ecdh)) | ||
| 720 | { | ||
| 721 | if (pck != NULL) | ||
| 722 | *pck = meth->compute_key; | ||
| 723 | } | ||
| 724 | LCRYPTO_ALIAS(EC_KEY_METHOD_get_compute_key); | ||
| 725 | |||
| 726 | void | ||
| 727 | EC_KEY_METHOD_get_sign(const EC_KEY_METHOD *meth, | ||
| 728 | int (**psign)(int type, const unsigned char *dgst, | ||
| 729 | int dlen, unsigned char *sig, unsigned int *siglen, | ||
| 730 | const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey), | ||
| 731 | int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, | ||
| 732 | BIGNUM **kinvp, BIGNUM **rp), | ||
| 733 | ECDSA_SIG *(**psign_sig)(const unsigned char *dgst, | ||
| 734 | int dgst_len, const BIGNUM *in_kinv, const BIGNUM *in_r, | ||
| 735 | EC_KEY *eckey)) | ||
| 736 | { | ||
| 737 | if (psign != NULL) | ||
| 738 | *psign = meth->sign; | ||
| 739 | if (psign_setup != NULL) | ||
| 740 | *psign_setup = meth->sign_setup; | ||
| 741 | if (psign_sig != NULL) | ||
| 742 | *psign_sig = meth->sign_sig; | ||
| 743 | } | ||
| 744 | LCRYPTO_ALIAS(EC_KEY_METHOD_get_sign); | ||
| 745 | |||
| 746 | void | ||
| 747 | EC_KEY_METHOD_get_verify(const EC_KEY_METHOD *meth, | ||
| 748 | int (**pverify)(int type, const unsigned char *dgst, int dgst_len, | ||
| 749 | const unsigned char *sigbuf, int sig_len, EC_KEY *eckey), | ||
| 750 | int (**pverify_sig)(const unsigned char *dgst, int dgst_len, | ||
| 751 | const ECDSA_SIG *sig, EC_KEY *eckey)) | ||
| 752 | { | ||
| 753 | if (pverify != NULL) | ||
| 754 | *pverify = meth->verify; | ||
| 755 | if (pverify_sig != NULL) | ||
| 756 | *pverify_sig = meth->verify_sig; | ||
| 757 | } | ||
| 758 | LCRYPTO_ALIAS(EC_KEY_METHOD_get_verify); | ||
| 759 | |||
| 760 | static const EC_KEY_METHOD openssl_ec_key_method = { | ||
| 761 | .name = "OpenSSL EC_KEY method", | ||
| 762 | .flags = 0, | ||
| 763 | |||
| 764 | .init = NULL, | ||
| 765 | .finish = NULL, | ||
| 766 | .copy = NULL, | ||
| 767 | |||
| 768 | .set_group = NULL, | ||
| 769 | .set_private = NULL, | ||
| 770 | .set_public = NULL, | ||
| 771 | |||
| 772 | .keygen = ec_key_gen, | ||
| 773 | .compute_key = ecdh_compute_key, | ||
| 774 | |||
| 775 | .sign = ecdsa_sign, | ||
| 776 | .sign_setup = ecdsa_sign_setup, | ||
| 777 | .sign_sig = ecdsa_sign_sig, | ||
| 778 | |||
| 779 | .verify = ecdsa_verify, | ||
| 780 | .verify_sig = ecdsa_verify_sig, | ||
| 781 | }; | ||
| 782 | |||
| 783 | const EC_KEY_METHOD * | ||
| 784 | EC_KEY_OpenSSL(void) | ||
| 785 | { | ||
| 786 | return &openssl_ec_key_method; | ||
| 787 | } | ||
| 788 | LCRYPTO_ALIAS(EC_KEY_OpenSSL); | ||
| 789 | |||
| 790 | const EC_KEY_METHOD *default_ec_key_meth = &openssl_ec_key_method; | ||
| 791 | |||
| 792 | const EC_KEY_METHOD * | ||
| 793 | EC_KEY_get_default_method(void) | ||
| 794 | { | ||
| 795 | return default_ec_key_meth; | ||
| 796 | } | ||
| 797 | LCRYPTO_ALIAS(EC_KEY_get_default_method); | ||
| 798 | |||
| 799 | void | ||
| 800 | EC_KEY_set_default_method(const EC_KEY_METHOD *meth) | ||
| 801 | { | ||
| 802 | if (meth == NULL) | ||
| 803 | default_ec_key_meth = &openssl_ec_key_method; | ||
| 804 | else | ||
| 805 | default_ec_key_meth = meth; | ||
| 806 | } | ||
| 807 | LCRYPTO_ALIAS(EC_KEY_set_default_method); | ||
