Skip to content

Commit c57d1e5

Browse files
committed
test/provider: Add tests for DHKEM-IKM option with EC keygen
Use the DHKEM-IKM option with EC keygen with the IBMCA provider and the default provider and compare the generated keys, they must be equal. This only works for P-256, P-384, and P-521 curves. Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
1 parent e818aee commit c57d1e5

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

test/provider/eckey.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ static int check_eckey(int nid, const char *curvename)
431431
OSSL_PARAM params[2];
432432
unsigned int nonce_type;
433433
#endif
434+
#ifdef OSSL_PKEY_PARAM_DHKEM_IKM
435+
EVP_PKEY *ec_pkey1 = NULL, *ec_pkey2 = NULL;
436+
const char dhkem_ikm[100] = { 0 };
437+
#endif
434438

435439
memset(digest, 0, sizeof(digest));
436440

@@ -442,6 +446,40 @@ static int check_eckey(int nid, const char *curvename)
442446
goto out;
443447
}
444448

449+
#ifdef OSSL_PKEY_PARAM_DHKEM_IKM
450+
/* Test DHKEM keygen */
451+
switch (nid) {
452+
case NID_X9_62_prime256v1:
453+
case NID_secp384r1:
454+
case NID_secp521r1:
455+
params[0] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_DHKEM_IKM,
456+
(void *)dhkem_ikm,
457+
sizeof(dhkem_ikm));
458+
params[1] = OSSL_PARAM_construct_end();
459+
460+
/* Keygen using DHKEM with IBMCA provider */
461+
if (!generate_key("ibmca", nid, curvename, params, NULL, &ec_pkey1))
462+
goto out;
463+
464+
/* Keygen using DHKEM with default provider */
465+
if (!generate_key(NULL, nid, curvename, params, NULL, &ec_pkey2))
466+
goto out;
467+
468+
/* Compare key from IBMCA with key from default provider */
469+
if (!EVP_PKEY_eq(ec_pkey1, ec_pkey2)) {
470+
fprintf(stderr, "EC keys generated via DHKEM do not match\n");
471+
ok = 1;
472+
goto out;
473+
}
474+
475+
EVP_PKEY_free(ec_pkey1);
476+
ec_pkey1 = NULL;
477+
EVP_PKEY_free(ec_pkey2);
478+
ec_pkey2 = NULL;
479+
break;
480+
}
481+
#endif
482+
445483
/* Sign with IBMCA provider */
446484
siglen = sizeof(sigbuf);
447485
if (!sign_single("ibmca", ec_pkey, digest, sizeof(digest),
@@ -564,6 +602,12 @@ static int check_eckey(int nid, const char *curvename)
564602
EVP_PKEY_free(peer_pkey);
565603
if (ec_pkey)
566604
EVP_PKEY_free(ec_pkey);
605+
#ifdef OSSL_PKEY_PARAM_DHKEM_IKM
606+
if (ec_pkey1)
607+
EVP_PKEY_free(ec_pkey1);
608+
if (ec_pkey2)
609+
EVP_PKEY_free(ec_pkey2);
610+
#endif
567611

568612
ERR_print_errors_fp(stderr);
569613

0 commit comments

Comments
 (0)