Skip to content

Commit acba1d9

Browse files
committed
provider: Support importing of RSA keys with just ME components
RSA private keys may contain just CRT (p, q, dp, dq, qinv) or ME (d) components, or all of them. If an application imports a private RSA key from just the ME components (m, e, and private d), then the IBMCA provider can not use ica_rsa_crt() to perform private key operations. Therefore let an RSA key also contain the private key components in ME format, and use ica_rsa_mod_expo() if only the ME components are available. RSA keys are still always generated in CRT format, but it now allows to import an RSA private key in ME format. Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
1 parent f8a60b6 commit acba1d9

File tree

5 files changed

+427
-222
lines changed

5 files changed

+427
-222
lines changed

src/provider/p_ibmca.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ struct ibmca_key {
124124
union {
125125
struct {
126126
size_t bits;
127-
ica_rsa_key_crt_t private;
127+
size_t keylength;
128+
ica_rsa_key_crt_t private_crt;
129+
ica_rsa_key_mod_expo_t private_me;
128130
ica_rsa_key_mod_expo_t public;
129131
struct ibmca_pss_params pss; /* For type EVP_PKEY_RSA_PSS only */
130132
BN_BLINDING *blinding;
@@ -176,6 +178,9 @@ struct ibmca_op_ctx *ibmca_keymgmt_gen_init(
176178
int (*dup_cb)
177179
(const struct ibmca_op_ctx *ctx,
178180
struct ibmca_op_ctx *new_ctx));
181+
bool ibmca_keymgmt_rsa_pub_valid(const ica_rsa_key_mod_expo_t *public);
182+
bool ibmca_keymgmt_rsa_priv_crt_valid(const ica_rsa_key_crt_t *private_crt);
183+
bool ibmca_keymgmt_rsa_priv_me_valid(const ica_rsa_key_mod_expo_t *private_me);
179184

180185
OSSL_FUNC_keymgmt_free_fn ibmca_keymgmt_free;
181186
OSSL_FUNC_keymgmt_dup_fn ibmca_keymgmt_dup;
@@ -519,8 +524,8 @@ int ibmca_keymgmt_rsa_derive_kdk(struct ibmca_key *key,
519524

520525
int ibmca_keymgmt_rsa_pub_as_bn(struct ibmca_key *key, BIGNUM **n, BIGNUM **e);
521526

522-
int ibmca_rsa_crt_with_blinding(struct ibmca_key *key, const unsigned char *in,
523-
unsigned char *out, size_t rsa_size);
527+
int ibmca_rsa_priv_with_blinding(struct ibmca_key *key, const unsigned char *in,
528+
unsigned char *out, size_t rsa_size);
524529

525530
int ossl_bn_rsa_do_unblind(const unsigned char *intermediate,
526531
const BIGNUM *unblind,

src/provider/rsa_asym_cipher.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ static int ibmca_asym_cipher_rsa_decrypt(void *vctx,
844844
}
845845

846846
/* Perform private key decrypt */
847-
rc = ibmca_rsa_crt_with_blinding(ctx->key, in, dec_data, rsa_size);
847+
rc = ibmca_rsa_priv_with_blinding(ctx->key, in, dec_data, rsa_size);
848848
if (rc != 1) {
849849
ibmca_debug_op_ctx(ctx, "ibmca_asym_cipher_rsa_with_blinding failed");
850850

src/provider/rsa_blinding.c

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,8 @@ static int ibmca_rsa_blinding_invert(struct ibmca_key *key,
392392
return rc;
393393
}
394394

395-
int ibmca_rsa_crt_with_blinding(struct ibmca_key *key, const unsigned char *in,
396-
unsigned char *out, size_t rsa_size)
395+
int ibmca_rsa_priv_with_blinding(struct ibmca_key *key, const unsigned char *in,
396+
unsigned char *out, size_t rsa_size)
397397
{
398398
BN_BLINDING *blinding;
399399
bool local_blinding = false;
@@ -404,7 +404,7 @@ int ibmca_rsa_crt_with_blinding(struct ibmca_key *key, const unsigned char *in,
404404

405405
ibmca_debug_key(key, "key: %p rsa_size: %lu", key, rsa_size);
406406

407-
if (rsa_size != key->rsa.private.key_length) {
407+
if (rsa_size != key->rsa.keylength) {
408408
put_error_key(key, IBMCA_ERR_INTERNAL_ERROR,
409409
"rsa_size is not modulus size");
410410
goto out;
@@ -445,11 +445,26 @@ int ibmca_rsa_crt_with_blinding(struct ibmca_key *key, const unsigned char *in,
445445
goto out;
446446
}
447447

448-
rc = ica_rsa_crt(key->provctx->ica_adapter, buf,
449-
&key->rsa.private, buf + rsa_size);
450-
if (rc != 0) {
451-
ibmca_debug_key(key, "ERROR: ica_rsa_crt failed with: %s",
452-
strerror(rc));
448+
if (ibmca_keymgmt_rsa_priv_crt_valid(&key->rsa.private_crt)) {
449+
rc = ica_rsa_crt(key->provctx->ica_adapter, buf,
450+
&key->rsa.private_crt, buf + rsa_size);
451+
if (rc != 0) {
452+
ibmca_debug_key(key, "ERROR: ica_rsa_crt failed with: %s",
453+
strerror(rc));
454+
rc = 0;
455+
goto out;
456+
}
457+
} else if (ibmca_keymgmt_rsa_priv_me_valid(&key->rsa.private_me)) {
458+
rc = ica_rsa_mod_expo(key->provctx->ica_adapter, buf,
459+
&key->rsa.private_me, buf + rsa_size);
460+
if (rc != 0) {
461+
ibmca_debug_key(key, "ERROR: ica_rsa_mod_expo failed with: %s",
462+
strerror(rc));
463+
rc = 0;
464+
goto out;
465+
}
466+
} else {
467+
put_error_key(key, IBMCA_ERR_INTERNAL_ERROR, "No private key");
453468
rc = 0;
454469
goto out;
455470
}

0 commit comments

Comments
 (0)