Skip to content

Commit 2529831

Browse files
committed
test/provider: Add test for import with OSSL_PKEY_PARAM_RSA_DERIVE_FROM_PQ
Add tests to perform an export of an RSA key to params, and then import the params but only with OSSL_PARAM_RSA_FACTOR1, OSSL_PARAM_RSA_FACTOR2, OSSL_PARAM_RSA_N and OSSL_PKEY_PARAM_RSA_E, and with param OSSL_PKEY_PARAM_RSA_DERIVE_FROM_PQ set to 1. The resulting key is expected to be equal to the original key. Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
1 parent eb113e2 commit 2529831

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

test/provider/rsakey.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <openssl/obj_mac.h>
2727
#include <openssl/provider.h>
2828
#include <openssl/err.h>
29+
#include <openssl/core_names.h>
2930

3031
#include <ica_api.h>
3132

@@ -754,6 +755,98 @@ static int verify_message_prehashed(const char* provider, const char *algo,
754755
}
755756
#endif
756757

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+
757850
static int check_rsakey(int bits, const char *algo, const char *name)
758851
{
759852
int ok = 0;
@@ -890,6 +983,14 @@ static int check_rsakey(int bits, const char *algo, const char *name)
890983
skip:
891984
#endif
892985

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+
893994
ok = 1;
894995

895996
out:

0 commit comments

Comments
 (0)