|
26 | 26 | #include <openssl/obj_mac.h>
|
27 | 27 | #include <openssl/provider.h>
|
28 | 28 | #include <openssl/err.h>
|
| 29 | +#include <openssl/core_names.h> |
29 | 30 |
|
30 | 31 | #include <ica_api.h>
|
31 | 32 |
|
@@ -754,6 +755,98 @@ static int verify_message_prehashed(const char* provider, const char *algo,
|
754 | 755 | }
|
755 | 756 | #endif
|
756 | 757 |
|
| 758 | +#ifdef OSSL_PKEY_PARAM_RSA_DERIVE_FROM_PQ |
| 759 | +static int export_import_key_from_pq(const char* provider, EVP_PKEY *rsa_pkey) |
| 760 | +{ |
| 761 | + char props[200]; |
| 762 | + EVP_PKEY_CTX *ctx = NULL; |
| 763 | + OSSL_PARAM *export_params = NULL; |
| 764 | + OSSL_PARAM import_params[7]; |
| 765 | + OSSL_PARAM *export_params2 = NULL; |
| 766 | + EVP_PKEY *rsa_pkey2 = NULL; |
| 767 | + int ok = 0, i, k, derive_from_pq = 1; |
| 768 | + |
| 769 | + sprintf(props, "%sprovider=%s", provider != NULL ? "?" : "", |
| 770 | + provider != NULL ? provider : "default"); |
| 771 | + |
| 772 | + ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", props); |
| 773 | + if (ctx == NULL) { |
| 774 | + fprintf(stderr, "EVP_PKEY_CTX_new_from_name failed\n"); |
| 775 | + goto out; |
| 776 | + } |
| 777 | + |
| 778 | + if (EVP_PKEY_todata(rsa_pkey, EVP_PKEY_KEYPAIR, &export_params) != 1) { |
| 779 | + fprintf(stderr, "EVP_PKEY_todata failed\n"); |
| 780 | + goto out; |
| 781 | + } |
| 782 | + |
| 783 | + for (i = 0, k = 0; export_params[i].key != NULL && k < 5; i++) { |
| 784 | + if (strcmp(export_params[i].key, OSSL_PKEY_PARAM_RSA_N) == 0 || |
| 785 | + strcmp(export_params[i].key, OSSL_PKEY_PARAM_RSA_E) == 0 || |
| 786 | + strcmp(export_params[i].key, OSSL_PKEY_PARAM_RSA_D) == 0 || |
| 787 | + strcmp(export_params[i].key, OSSL_PKEY_PARAM_RSA_FACTOR1) == 0 || |
| 788 | + strcmp(export_params[i].key, OSSL_PKEY_PARAM_RSA_FACTOR2) == 0) { |
| 789 | + import_params[k] = export_params[i]; |
| 790 | + k++; |
| 791 | + } |
| 792 | + } |
| 793 | + import_params[k++] = |
| 794 | + OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_RSA_DERIVE_FROM_PQ, |
| 795 | + &derive_from_pq); |
| 796 | + import_params[k++] = OSSL_PARAM_construct_end(); |
| 797 | + |
| 798 | + if (EVP_PKEY_fromdata_init(ctx) <= 0) { |
| 799 | + fprintf(stderr, "EVP_PKEY_fromdata_init failed\n"); |
| 800 | + goto out; |
| 801 | + } |
| 802 | + |
| 803 | + if (EVP_PKEY_fromdata(ctx, &rsa_pkey2, EVP_PKEY_KEYPAIR, |
| 804 | + import_params) != 1) { |
| 805 | + fprintf(stderr, "EVP_PKEY_fromdata failed\n"); |
| 806 | + goto out; |
| 807 | + } |
| 808 | + |
| 809 | + /* |
| 810 | + * Export the new key again to compare the key components. EVP_PKEY_eq() |
| 811 | + * would only compare the public key components, but not the private |
| 812 | + * components. |
| 813 | + */ |
| 814 | + if (EVP_PKEY_todata(rsa_pkey2, EVP_PKEY_KEYPAIR, &export_params2) != 1) { |
| 815 | + fprintf(stderr, "EVP_PKEY_todata failed\n"); |
| 816 | + goto out; |
| 817 | + } |
| 818 | + |
| 819 | + ok = 1; |
| 820 | + |
| 821 | + for (i = 0; export_params[i].key != NULL; i++) { |
| 822 | + for (k = 0; export_params2[k].key != NULL; k++) { |
| 823 | + if (strcmp(export_params[i].key, export_params2[k].key) == 0) { |
| 824 | + if (export_params[i].data_size != export_params2[k].data_size || |
| 825 | + memcmp(export_params[i].data, export_params2[k].data, |
| 826 | + export_params[i].data_size) != 0) { |
| 827 | + fprintf(stderr, "Key component '%s' is different\n", |
| 828 | + export_params[i].key); |
| 829 | + ok = 0; |
| 830 | + } |
| 831 | + break; |
| 832 | + } |
| 833 | + } |
| 834 | + } |
| 835 | + |
| 836 | +out: |
| 837 | + if (ctx != NULL) |
| 838 | + EVP_PKEY_CTX_free(ctx); |
| 839 | + if (export_params != NULL) |
| 840 | + OSSL_PARAM_free(export_params); |
| 841 | + if (export_params2 != NULL) |
| 842 | + OSSL_PARAM_free(export_params2); |
| 843 | + if (rsa_pkey2 != NULL) |
| 844 | + EVP_PKEY_free(rsa_pkey2); |
| 845 | + |
| 846 | + return ok; |
| 847 | +} |
| 848 | +#endif |
| 849 | + |
757 | 850 | static int check_rsakey(int bits, const char *algo, const char *name)
|
758 | 851 | {
|
759 | 852 | int ok = 0;
|
@@ -890,6 +983,14 @@ static int check_rsakey(int bits, const char *algo, const char *name)
|
890 | 983 | skip:
|
891 | 984 | #endif
|
892 | 985 |
|
| 986 | +#ifdef OSSL_PKEY_PARAM_RSA_DERIVE_FROM_PQ |
| 987 | + if (!export_import_key_from_pq("ibmca", rsa_pkey)) |
| 988 | + goto out; |
| 989 | + |
| 990 | + if (!export_import_key_from_pq(NULL, rsa_pkey)) |
| 991 | + goto out; |
| 992 | +#endif |
| 993 | + |
893 | 994 | ok = 1;
|
894 | 995 |
|
895 | 996 | out:
|
|
0 commit comments