Skip to content

Commit

Permalink
Tests to check gen keys through check_rsa_key() and check_ec_key()
Browse files Browse the repository at this point in the history
Signed-off-by: Sahana Prasad <sahana@redhat.com>
  • Loading branch information
sahanaprasad07 authored and simo5 committed Sep 29, 2023
1 parent fb3aa3b commit f9b04f9
Showing 1 changed file with 55 additions and 39 deletions.
94 changes: 55 additions & 39 deletions tests/tgenkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,52 @@ static void hexify(char *out, unsigned char *byte, size_t len)
out[len * 3] = '\0';
}

static void check_rsa_key(EVP_PKEY *pubkey)
{
BIGNUM *tmp = NULL;
int ret;

ret = EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_RSA_E, &tmp);
if (ret != 1) {
fprintf(stderr, "Failed to get E param from public key");
exit(EXIT_FAILURE);
} else {
BN_free(tmp);
tmp = NULL;
}
ret = EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_RSA_N, &tmp);
if (ret != 1) {
fprintf(stderr, "Failed to get N param from public key");
exit(EXIT_FAILURE);
} else {
BN_free(tmp);
tmp = NULL;
}
}

static void check_ec_key(EVP_PKEY *pubkey)
{
BIGNUM *tmp = NULL;
int ret;

ret = EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_EC_PUB_X, &tmp);
if (ret != 1) {
fprintf(stderr, "Failed to get X param from public key");
exit(EXIT_FAILURE);
} else {
BN_free(tmp);
tmp = NULL;
}
ret = EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_EC_PUB_Y, &tmp);
if (ret != 1) {
fprintf(stderr, "Failed to get Y param from public key");
exit(EXIT_FAILURE);
} else {
BN_free(tmp);
tmp = NULL;
}
}

static void check_keys(OSSL_STORE_CTX *store, const char *key_type)
{
OSSL_STORE_INFO *info;
Expand Down Expand Up @@ -70,46 +116,10 @@ static void check_keys(OSSL_STORE_CTX *store, const char *key_type)
}

/* check we can get pub params from key */
if (strcmp(key_type, "RSA") == 0) {
BIGNUM *tmp = NULL;
int ret;

ret = EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_RSA_E, &tmp);
if (ret != 1) {
fprintf(stderr, "Failed to get E param from public key");
exit(EXIT_FAILURE);
} else {
BN_free(tmp);
tmp = NULL;
}
ret = EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_RSA_N, &tmp);
if (ret != 1) {
fprintf(stderr, "Failed to get N param from public key");
exit(EXIT_FAILURE);
} else {
BN_free(tmp);
tmp = NULL;
}
if (strcmp(key_type, "RSA") == 0 || strcmp(key_type, "RSA-PSS") == 0) {
check_rsa_key(pubkey);
} else if (strcmp(key_type, "EC") == 0) {
BIGNUM *tmp = NULL;
int ret;

ret = EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_EC_PUB_X, &tmp);
if (ret != 1) {
fprintf(stderr, "Failed to get X param from public key");
exit(EXIT_FAILURE);
} else {
BN_free(tmp);
tmp = NULL;
}
ret = EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_EC_PUB_Y, &tmp);
if (ret != 1) {
fprintf(stderr, "Failed to get Y param from public key");
exit(EXIT_FAILURE);
} else {
BN_free(tmp);
tmp = NULL;
}
check_ec_key(pubkey);
}

EVP_PKEY_free(privkey);
Expand Down Expand Up @@ -161,6 +171,12 @@ static void gen_keys(const char *key_type, const char *label, const char *idhex,
exit(EXIT_FAILURE);
}

if (strcmp(key_type, "RSA") == 0 || strcmp(key_type, "RSA-PSS") == 0) {
check_rsa_key(key);
} else if (strcmp(key_type, "EC") == 0) {
check_ec_key(key);
}

EVP_PKEY_free(key);
key = NULL;
EVP_PKEY_CTX_free(ctx);
Expand Down

0 comments on commit f9b04f9

Please sign in to comment.