|
|
1f6f0c |
From 49ce634ed6fa1fddc2bb34fd0f89c0ea0cc368ee Mon Sep 17 00:00:00 2001
|
|
|
1f6f0c |
From: Ingo Franzki <ifranzki@linux.ibm.com>
|
|
|
1f6f0c |
Date: Tue, 1 Mar 2022 09:15:15 +0100
|
|
|
1f6f0c |
Subject: [PATCH 25/34] COMMON/EP11: Add Kyber key type and mechanism
|
|
|
1f6f0c |
|
|
|
1f6f0c |
Kyber requires an EP11 host library of version 4.0 or later,
|
|
|
1f6f0c |
and a CEX8P crypto card.
|
|
|
1f6f0c |
|
|
|
1f6f0c |
Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
|
|
|
1f6f0c |
---
|
|
|
1f6f0c |
usr/lib/api/mechtable.inc | 1 +
|
|
|
1f6f0c |
usr/lib/api/policy.c | 2 +
|
|
|
1f6f0c |
usr/lib/common/asn1.c | 563 +++++++++++++++++++++++++++++++++++
|
|
|
1f6f0c |
usr/lib/common/h_extern.h | 59 ++++
|
|
|
1f6f0c |
usr/lib/common/key.c | 476 +++++++++++++++++++++++++++++
|
|
|
1f6f0c |
usr/lib/common/key_mgr.c | 10 +
|
|
|
1f6f0c |
usr/lib/common/mech_ec.c | 3 +
|
|
|
1f6f0c |
usr/lib/common/template.c | 12 +
|
|
|
1f6f0c |
usr/lib/ep11_stdll/ep11_specific.c | 42 ++-
|
|
|
1f6f0c |
usr/lib/ep11_stdll/ep11cpfilter.conf | 2 +-
|
|
|
1f6f0c |
10 files changed, 1167 insertions(+), 3 deletions(-)
|
|
|
1f6f0c |
|
|
|
1f6f0c |
diff --git a/usr/lib/api/mechtable.inc b/usr/lib/api/mechtable.inc
|
|
|
1f6f0c |
index e3d14e3e..7aa72fbf 100644
|
|
|
1f6f0c |
--- a/usr/lib/api/mechtable.inc
|
|
|
1f6f0c |
+++ b/usr/lib/api/mechtable.inc
|
|
|
1f6f0c |
@@ -84,6 +84,7 @@ const struct mechrow mechtable_rows[] =
|
|
|
1f6f0c |
{ "CKM_IBM_EC_X448", CKM_IBM_EC_X448, 0, MC_INFORMATION_UNAVAILABLE, MCF_DERIVE },
|
|
|
1f6f0c |
{ "CKM_IBM_ED25519_SHA512", CKM_IBM_ED25519_SHA512, 128, MC_KEY_DEPENDENT, MCF_SIGNVERIFY },
|
|
|
1f6f0c |
{ "CKM_IBM_ED448_SHA3", CKM_IBM_ED448_SHA3, 144, MC_KEY_DEPENDENT, MCF_SIGNVERIFY },
|
|
|
1f6f0c |
+ { "CKM_IBM_KYBER", CKM_IBM_KYBER, 0, MC_KEY_DEPENDENT, MCF_KEYGEN | MCF_ENCRYPTDECRYPT | MCF_DERIVE | MCF_NEEDSPARAM},
|
|
|
1f6f0c |
{ "CKM_IBM_SHA3_224", CKM_IBM_SHA3_224, 144, 24, MCF_DIGEST },
|
|
|
1f6f0c |
{ "CKM_IBM_SHA3_224_HMAC", CKM_IBM_SHA3_224_HMAC, 144, 24, MCF_SIGNVERIFY },
|
|
|
1f6f0c |
{ "CKM_IBM_SHA3_256", CKM_IBM_SHA3_256, 136, 32, MCF_DIGEST },
|
|
|
1f6f0c |
diff --git a/usr/lib/api/policy.c b/usr/lib/api/policy.c
|
|
|
1f6f0c |
index 4bee5180..b513a8a9 100644
|
|
|
1f6f0c |
--- a/usr/lib/api/policy.c
|
|
|
1f6f0c |
+++ b/usr/lib/api/policy.c
|
|
|
1f6f0c |
@@ -333,6 +333,7 @@ static CK_RV policy_extract_key_data(get_attr_val_f getattr, void *d,
|
|
|
1f6f0c |
*comptarget = COMPARE_SYMMETRIC;
|
|
|
1f6f0c |
break;
|
|
|
1f6f0c |
case CKK_IBM_PQC_DILITHIUM:
|
|
|
1f6f0c |
+ case CKK_IBM_PQC_KYBER:
|
|
|
1f6f0c |
rv = policy_get_pqc_args(*(CK_ULONG *)keytype->pValue, getattr, d,
|
|
|
1f6f0c |
free_attr, size, siglen, oid, oidlen);
|
|
|
1f6f0c |
*comptarget = COMPARE_PQC;
|
|
|
1f6f0c |
@@ -1062,6 +1063,7 @@ static CK_RV policy_update_mech_info(policy_t p, CK_MECHANISM_TYPE mech,
|
|
|
1f6f0c |
}
|
|
|
1f6f0c |
break;
|
|
|
1f6f0c |
case CKM_IBM_DILITHIUM:
|
|
|
1f6f0c |
+ case CKM_IBM_KYBER:
|
|
|
1f6f0c |
break;
|
|
|
1f6f0c |
case CKM_IBM_SHA3_224:
|
|
|
1f6f0c |
case CKM_IBM_SHA3_256:
|
|
|
1f6f0c |
diff --git a/usr/lib/common/asn1.c b/usr/lib/common/asn1.c
|
|
|
1f6f0c |
index 85d3924c..87cc5dfc 100644
|
|
|
1f6f0c |
--- a/usr/lib/common/asn1.c
|
|
|
1f6f0c |
+++ b/usr/lib/common/asn1.c
|
|
|
1f6f0c |
@@ -4384,3 +4384,566 @@ cleanup:
|
|
|
1f6f0c |
|
|
|
1f6f0c |
return rc;
|
|
|
1f6f0c |
}
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+/**
|
|
|
1f6f0c |
+ * An IBM Kyber public key is given by:
|
|
|
1f6f0c |
+ *
|
|
|
1f6f0c |
+ * SEQUENCE (2 elem)
|
|
|
1f6f0c |
+ * SEQUENCE (2 elem)
|
|
|
1f6f0c |
+ * OBJECT IDENTIFIER 1.3.6.1.4.1.2.267.5.xxx
|
|
|
1f6f0c |
+ * NULL
|
|
|
1f6f0c |
+ * BIT STRING (1 elem)
|
|
|
1f6f0c |
+ * SEQUENCE (1 elem)
|
|
|
1f6f0c |
+ * pk BIT STRING -- public key
|
|
|
1f6f0c |
+ */
|
|
|
1f6f0c |
+CK_RV ber_encode_IBM_KyberPublicKey(CK_BBOOL length_only,
|
|
|
1f6f0c |
+ CK_BYTE **data, CK_ULONG *data_len,
|
|
|
1f6f0c |
+ const CK_BYTE *oid, CK_ULONG oid_len,
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *pk)
|
|
|
1f6f0c |
+{
|
|
|
1f6f0c |
+ CK_BYTE *buf = NULL, *buf2 = NULL, *buf3 = NULL, *buf4 = NULL;
|
|
|
1f6f0c |
+ CK_BYTE *buf5 = NULL, *algid = NULL;
|
|
|
1f6f0c |
+ CK_ULONG len, len4, offset, total, total_len, algid_len;
|
|
|
1f6f0c |
+ CK_RV rc;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ UNUSED(length_only);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ offset = 0;
|
|
|
1f6f0c |
+ rc = 0;
|
|
|
1f6f0c |
+ total_len = 0;
|
|
|
1f6f0c |
+ total = 0;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* Calculate storage for AlgID sequence */
|
|
|
1f6f0c |
+ rc |= ber_encode_SEQUENCE(TRUE, NULL, &total_len, NULL,
|
|
|
1f6f0c |
+ oid_len + ber_NULLLen);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* Calculate storage for inner sequence */
|
|
|
1f6f0c |
+ rc |= ber_encode_INTEGER(TRUE, NULL, &len, NULL, pk->ulValueLen);
|
|
|
1f6f0c |
+ offset += len;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s ber_encode_Int failed with rc=0x%lx\n", __func__, rc);
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* Allocate storage for inner sequence */
|
|
|
1f6f0c |
+ buf = (CK_BYTE *) malloc(offset);
|
|
|
1f6f0c |
+ if (!buf) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s Memory allocation failed\n", __func__);
|
|
|
1f6f0c |
+ return CKR_HOST_MEMORY;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /**
|
|
|
1f6f0c |
+ * SEQUENCE (1 elem)
|
|
|
1f6f0c |
+ * BIT STRING -> pk
|
|
|
1f6f0c |
+ */
|
|
|
1f6f0c |
+ offset = 0;
|
|
|
1f6f0c |
+ rc = ber_encode_BIT_STRING(FALSE, &buf2, &len,
|
|
|
1f6f0c |
+ pk->pValue, pk->ulValueLen, 0);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s ber_encode_Int failed with rc=0x%lx\n", __func__, rc);
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ memcpy(buf + offset, buf2, len);
|
|
|
1f6f0c |
+ offset += len;
|
|
|
1f6f0c |
+ free(buf2);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = ber_encode_SEQUENCE(FALSE, &buf2, &len, buf, offset);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s ber_encode_Seq failed with rc=0x%lx\n", __func__, rc);
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ free(buf);
|
|
|
1f6f0c |
+ buf = NULL;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* Calculate length of outer sequence */
|
|
|
1f6f0c |
+ rc = ber_encode_BIT_STRING(TRUE, NULL, &total, buf2, len, 0);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s ber_encode_Oct_Str failed with rc=0x%lx\n", __func__, rc);
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ } else {
|
|
|
1f6f0c |
+ total_len += total;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* Allocate storage for outer sequence and bit string */
|
|
|
1f6f0c |
+ buf3 = (CK_BYTE *) malloc(total_len);
|
|
|
1f6f0c |
+ if (!buf3) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s Memory allocation failed\n", __func__);
|
|
|
1f6f0c |
+ rc = CKR_HOST_MEMORY;
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /*
|
|
|
1f6f0c |
+ * SEQUENCE (2 elem)
|
|
|
1f6f0c |
+ * OBJECT IDENTIFIER 1.3.6.1.4.1.2.267.5.xxx
|
|
|
1f6f0c |
+ * NULL <- no parms for this oid
|
|
|
1f6f0c |
+ */
|
|
|
1f6f0c |
+ buf5 = (CK_BYTE *) malloc(oid_len + ber_NULLLen);
|
|
|
1f6f0c |
+ if (!buf5) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s Memory allocation failed\n", __func__);
|
|
|
1f6f0c |
+ rc = CKR_HOST_MEMORY;
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ memcpy(buf5, oid, oid_len);
|
|
|
1f6f0c |
+ memcpy(buf5 + oid_len, ber_NULL, ber_NULLLen);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = ber_encode_SEQUENCE(FALSE, &algid, &algid_len, buf5,
|
|
|
1f6f0c |
+ oid_len + ber_NULLLen);
|
|
|
1f6f0c |
+ free(buf5);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s ber_encode_SEQUENCE failed with rc=0x%lx\n", __func__, rc);
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ total_len = algid_len;
|
|
|
1f6f0c |
+ memcpy(buf3, algid, algid_len);
|
|
|
1f6f0c |
+ free(algid);
|
|
|
1f6f0c |
+ algid = NULL;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /*
|
|
|
1f6f0c |
+ * BIT STRING (1 elem)
|
|
|
1f6f0c |
+ * SEQUENCE (1 elem)
|
|
|
1f6f0c |
+ * BIT STRING -> pk
|
|
|
1f6f0c |
+ */
|
|
|
1f6f0c |
+ rc = ber_encode_BIT_STRING(FALSE, &buf4, &len4, buf2, len, 0);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s ber_encode_BIT_STRING failed with rc=0x%lx\n", __func__, rc);
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ memcpy(buf3 + total_len, buf4, len4);
|
|
|
1f6f0c |
+ total_len += len4;
|
|
|
1f6f0c |
+ free(buf4);
|
|
|
1f6f0c |
+ buf4 = NULL;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /**
|
|
|
1f6f0c |
+ * SEQUENCE (2 elem)
|
|
|
1f6f0c |
+ * SEQUENCE (2 elem)
|
|
|
1f6f0c |
+ * OBJECT IDENTIFIER 1.3.6.1.4.1.2.267.5.xxx
|
|
|
1f6f0c |
+ * NULL -> no parms for this oid
|
|
|
1f6f0c |
+ * BIT STRING (1 elem)
|
|
|
1f6f0c |
+ * SEQUENCE (2 elem)
|
|
|
1f6f0c |
+ * BIT STRING -> pk
|
|
|
1f6f0c |
+ */
|
|
|
1f6f0c |
+ rc = ber_encode_SEQUENCE(FALSE, data, data_len, buf3, total_len);
|
|
|
1f6f0c |
+ if (rc != CKR_OK)
|
|
|
1f6f0c |
+ TRACE_ERROR("%s ber_encode_Seq failed with rc=0x%lx\n", __func__, rc);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+error:
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ if (buf)
|
|
|
1f6f0c |
+ free(buf);
|
|
|
1f6f0c |
+ if (buf2)
|
|
|
1f6f0c |
+ free(buf2);
|
|
|
1f6f0c |
+ if (buf3)
|
|
|
1f6f0c |
+ free(buf3);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+}
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+CK_RV ber_decode_IBM_KyberPublicKey(CK_BYTE *data,
|
|
|
1f6f0c |
+ CK_ULONG data_len,
|
|
|
1f6f0c |
+ CK_ATTRIBUTE **pk_attr,
|
|
|
1f6f0c |
+ CK_ATTRIBUTE **value_attr,
|
|
|
1f6f0c |
+ const struct pqc_oid **oid)
|
|
|
1f6f0c |
+{
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *pk_attr_temp = NULL;
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *value_attr_temp = NULL;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ CK_BYTE *algoid = NULL;
|
|
|
1f6f0c |
+ CK_ULONG algoid_len;
|
|
|
1f6f0c |
+ CK_BYTE *param = NULL;
|
|
|
1f6f0c |
+ CK_ULONG param_len;
|
|
|
1f6f0c |
+ CK_BYTE *val = NULL;
|
|
|
1f6f0c |
+ CK_ULONG val_len;
|
|
|
1f6f0c |
+ CK_BYTE *seq;
|
|
|
1f6f0c |
+ CK_ULONG seq_len;
|
|
|
1f6f0c |
+ CK_BYTE *pk;
|
|
|
1f6f0c |
+ CK_ULONG pk_len;
|
|
|
1f6f0c |
+ CK_ULONG field_len, raw_spki_len;
|
|
|
1f6f0c |
+ CK_RV rc;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ UNUSED(data_len); // XXX can this parameter be removed ?
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = ber_decode_SPKI(data, &algoid, &algoid_len, ¶m, ¶m_len,
|
|
|
1f6f0c |
+ &val, &val_len);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("ber_decode_SPKI failed\n");
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ *oid = find_pqc_by_oid(kyber_oids, algoid, algoid_len);
|
|
|
1f6f0c |
+ if (*oid == NULL) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s\n", ock_err(ERR_FUNCTION_FAILED));
|
|
|
1f6f0c |
+ return CKR_FUNCTION_FAILED;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* Decode sequence:
|
|
|
1f6f0c |
+ * SEQUENCE (1 elem)
|
|
|
1f6f0c |
+ * BIT STRING = pk
|
|
|
1f6f0c |
+ */
|
|
|
1f6f0c |
+ rc = ber_decode_SEQUENCE(val, &seq, &seq_len, &field_len);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("ber_decode_SEQUENCE failed\n");
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* Decode pk */
|
|
|
1f6f0c |
+ rc = ber_decode_BIT_STRING(seq, &pk, &pk_len, &field_len);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("ber_decode_INTEGER failed\n");
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ pk++; /* Remove unused-bits byte */
|
|
|
1f6f0c |
+ pk_len--;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* Build pk attribute */
|
|
|
1f6f0c |
+ rc = build_attribute(CKA_IBM_KYBER_PK, pk, pk_len, &pk_attr_temp);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("build_attribute failed\n");
|
|
|
1f6f0c |
+ goto cleanup;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* Add raw SPKI as CKA_VALUE to public key (z/OS ICSF compatibility) */
|
|
|
1f6f0c |
+ rc = ber_decode_SEQUENCE(data, &val, &val_len, &raw_spki_len);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s ber_decode_SEQUENCE failed with rc=0x%lx\n", __func__, rc);
|
|
|
1f6f0c |
+ goto cleanup;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ rc = build_attribute(CKA_VALUE, data, raw_spki_len, &value_attr_temp);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("build_attribute failed\n");
|
|
|
1f6f0c |
+ goto cleanup;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ *pk_attr = pk_attr_temp;
|
|
|
1f6f0c |
+ *value_attr = value_attr_temp;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ return CKR_OK;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+cleanup:
|
|
|
1f6f0c |
+ if (pk_attr_temp)
|
|
|
1f6f0c |
+ free(pk_attr_temp);
|
|
|
1f6f0c |
+ if (value_attr_temp)
|
|
|
1f6f0c |
+ free(value_attr_temp);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+}
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+/**
|
|
|
1f6f0c |
+ * An IBM Kyber private key is given by:
|
|
|
1f6f0c |
+ *
|
|
|
1f6f0c |
+ * KyberPrivateKey ::= SEQUENCE {
|
|
|
1f6f0c |
+ * version INTEGER, -- v0, reserved 0
|
|
|
1f6f0c |
+ * sk BIT STRING, -- private key
|
|
|
1f6f0c |
+ * pk [0] IMPLICIT OPTIONAL {
|
|
|
1f6f0c |
+ * pk||rs BIT STRING -- public key (pk) concatenated with 2x32 bytes rs
|
|
|
1f6f0c |
+ * }
|
|
|
1f6f0c |
+ * }
|
|
|
1f6f0c |
+ */
|
|
|
1f6f0c |
+CK_RV ber_encode_IBM_KyberPrivateKey(CK_BBOOL length_only,
|
|
|
1f6f0c |
+ CK_BYTE **data,
|
|
|
1f6f0c |
+ CK_ULONG *data_len,
|
|
|
1f6f0c |
+ const CK_BYTE *oid, CK_ULONG oid_len,
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *sk,
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *pk)
|
|
|
1f6f0c |
+{
|
|
|
1f6f0c |
+ CK_BYTE *buf = NULL, *buf2 = NULL, *buf3 = NULL;
|
|
|
1f6f0c |
+ CK_BYTE *algid = NULL, *algid_buf = NULL, *pk_rs = NULL;
|
|
|
1f6f0c |
+ CK_ULONG len, len2 = 0, offset, algid_len = 0;
|
|
|
1f6f0c |
+ CK_BYTE version[] = { 0 };
|
|
|
1f6f0c |
+ CK_RV rc;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* Calculate storage for sequence */
|
|
|
1f6f0c |
+ offset = 0;
|
|
|
1f6f0c |
+ rc = 0;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc |= ber_encode_SEQUENCE(TRUE, NULL, &algid_len, NULL,
|
|
|
1f6f0c |
+ oid_len + ber_NULLLen);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc |= ber_encode_INTEGER(TRUE, NULL, &len, NULL, sizeof(version));
|
|
|
1f6f0c |
+ offset += len;
|
|
|
1f6f0c |
+ rc |= ber_encode_BIT_STRING(TRUE, NULL, &len, NULL, sk->ulValueLen, 0);
|
|
|
1f6f0c |
+ offset += len;
|
|
|
1f6f0c |
+ if (pk) {
|
|
|
1f6f0c |
+ rc |= ber_encode_BIT_STRING(TRUE, NULL, &len2, NULL,
|
|
|
1f6f0c |
+ pk->ulValueLen + 64, 0);
|
|
|
1f6f0c |
+ rc |= ber_encode_CHOICE(TRUE, 0, NULL, &len, NULL, len2);
|
|
|
1f6f0c |
+ offset += len;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("Calculate storage for sequence failed\n");
|
|
|
1f6f0c |
+ return CKR_FUNCTION_FAILED;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ if (length_only == TRUE) {
|
|
|
1f6f0c |
+ rc = ber_encode_SEQUENCE(TRUE, NULL, &len, NULL, offset);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("ber_encode_SEQUENCE failed\n");
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ rc = ber_encode_PrivateKeyInfo(TRUE,
|
|
|
1f6f0c |
+ NULL, data_len,
|
|
|
1f6f0c |
+ NULL, algid_len,
|
|
|
1f6f0c |
+ NULL, len);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("ber_encode_PrivateKeyInfo failed\n");
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* Allocate storage for sequence */
|
|
|
1f6f0c |
+ buf = (CK_BYTE *) malloc(offset);
|
|
|
1f6f0c |
+ if (!buf) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s\n", ock_err(ERR_HOST_MEMORY));
|
|
|
1f6f0c |
+ return CKR_HOST_MEMORY;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ offset = 0;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* Version */
|
|
|
1f6f0c |
+ rc = ber_encode_INTEGER(FALSE, &buf2, &len, version, sizeof(version));
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("ber_encode_INTEGER of version failed\n");
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ memcpy(buf + offset, buf2, len);
|
|
|
1f6f0c |
+ offset += len;
|
|
|
1f6f0c |
+ free(buf2);
|
|
|
1f6f0c |
+ buf2 = NULL;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* sk */
|
|
|
1f6f0c |
+ rc = ber_encode_BIT_STRING(FALSE, &buf2, &len,
|
|
|
1f6f0c |
+ sk->pValue, sk->ulValueLen, 0);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("ber_encode_BIT_STRING of sk failed\n");
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ memcpy(buf + offset, buf2, len);
|
|
|
1f6f0c |
+ offset += len;
|
|
|
1f6f0c |
+ free(buf2);
|
|
|
1f6f0c |
+ buf2 = NULL;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* (pk) Optional bit-string of public key */
|
|
|
1f6f0c |
+ if (pk && pk->pValue) {
|
|
|
1f6f0c |
+ /* append rs to public key */
|
|
|
1f6f0c |
+ pk_rs = (CK_BYTE *)malloc(pk->ulValueLen + 64);
|
|
|
1f6f0c |
+ if (!pk_rs) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s\n", ock_err(ERR_HOST_MEMORY));
|
|
|
1f6f0c |
+ rc = CKR_HOST_MEMORY;
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ memcpy(pk_rs, pk->pValue, pk->ulValueLen);
|
|
|
1f6f0c |
+ memset(pk_rs + pk->ulValueLen, 0x30, 64);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = ber_encode_BIT_STRING(FALSE, &buf3, &len2,
|
|
|
1f6f0c |
+ pk_rs, pk->ulValueLen + 64, 0);
|
|
|
1f6f0c |
+ rc |= ber_encode_CHOICE(FALSE, 0, &buf2, &len, buf3, len2);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("encoding of pk value failed\n");
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ memcpy(buf + offset, buf2, len);
|
|
|
1f6f0c |
+ offset += len;
|
|
|
1f6f0c |
+ free(buf2);
|
|
|
1f6f0c |
+ buf2 = NULL;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* Encode sequence */
|
|
|
1f6f0c |
+ rc = ber_encode_SEQUENCE(FALSE, &buf2, &len, buf, offset);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("ber_encode_SEQUENCE failed\n");
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ algid_buf = (CK_BYTE *) malloc(oid_len + ber_NULLLen);
|
|
|
1f6f0c |
+ if (!algid_buf) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s Memory allocation failed\n", __func__);
|
|
|
1f6f0c |
+ rc = CKR_HOST_MEMORY;
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ memcpy(algid_buf, oid, oid_len);
|
|
|
1f6f0c |
+ memcpy(algid_buf + oid_len, ber_NULL, ber_NULLLen);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = ber_encode_SEQUENCE(FALSE, &algid, &algid_len, algid_buf,
|
|
|
1f6f0c |
+ oid_len + ber_NULLLen);
|
|
|
1f6f0c |
+ free(algid_buf);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s ber_encode_SEQUENCE failed with rc=0x%lx\n", __func__, rc);
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = ber_encode_PrivateKeyInfo(FALSE,
|
|
|
1f6f0c |
+ data, data_len,
|
|
|
1f6f0c |
+ algid, algid_len,
|
|
|
1f6f0c |
+ buf2, len);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("ber_encode_PrivateKeyInfo failed\n");
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+error:
|
|
|
1f6f0c |
+ if (buf3)
|
|
|
1f6f0c |
+ free(buf3);
|
|
|
1f6f0c |
+ if (buf2)
|
|
|
1f6f0c |
+ free(buf2);
|
|
|
1f6f0c |
+ if (buf)
|
|
|
1f6f0c |
+ free(buf);
|
|
|
1f6f0c |
+ if (algid)
|
|
|
1f6f0c |
+ free(algid);
|
|
|
1f6f0c |
+ if (pk_rs)
|
|
|
1f6f0c |
+ free(pk_rs);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+}
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+/**
|
|
|
1f6f0c |
+ * decode an IBM Kyber private key:
|
|
|
1f6f0c |
+ *
|
|
|
1f6f0c |
+ * KyberPrivateKey ::= SEQUENCE {
|
|
|
1f6f0c |
+ * version INTEGER, -- v0, reserved 0
|
|
|
1f6f0c |
+ * sk BIT STRING, -- private key
|
|
|
1f6f0c |
+ * pk [0] IMPLICIT OPTIONAL {
|
|
|
1f6f0c |
+ * pk||rs BIT STRING -- public key (pk) concatenated with 2x32 bytes rs
|
|
|
1f6f0c |
+ * }
|
|
|
1f6f0c |
+ * }
|
|
|
1f6f0c |
+ */
|
|
|
1f6f0c |
+CK_RV ber_decode_IBM_KyberPrivateKey(CK_BYTE *data,
|
|
|
1f6f0c |
+ CK_ULONG data_len,
|
|
|
1f6f0c |
+ CK_ATTRIBUTE **sk,
|
|
|
1f6f0c |
+ CK_ATTRIBUTE **pk,
|
|
|
1f6f0c |
+ CK_ATTRIBUTE **value,
|
|
|
1f6f0c |
+ const struct pqc_oid **oid)
|
|
|
1f6f0c |
+{
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *sk_attr = NULL, *pk_attr = NULL, *value_attr = NULL;
|
|
|
1f6f0c |
+ CK_BYTE *algoid = NULL;
|
|
|
1f6f0c |
+ CK_BYTE *kyber_priv_key = NULL;
|
|
|
1f6f0c |
+ CK_BYTE *buf = NULL;
|
|
|
1f6f0c |
+ CK_BYTE *tmp = NULL;
|
|
|
1f6f0c |
+ CK_ULONG offset, buf_len, field_len, len, option;
|
|
|
1f6f0c |
+ CK_RV rc;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* Check if this is a Kyber private key */
|
|
|
1f6f0c |
+ rc = ber_decode_PrivateKeyInfo(data, data_len, &algoid, &len,
|
|
|
1f6f0c |
+ &kyber_priv_key);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("ber_decode_PrivateKeyInfo failed\n");
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ if (len <= ber_NULLLen ||
|
|
|
1f6f0c |
+ memcmp(algoid + len - ber_NULLLen, ber_NULL, ber_NULLLen) != 0) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s\n", ock_err(ERR_FUNCTION_FAILED));
|
|
|
1f6f0c |
+ return CKR_FUNCTION_FAILED;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ len -= ber_NULLLen;
|
|
|
1f6f0c |
+ *oid = find_pqc_by_oid(kyber_oids, algoid, len);
|
|
|
1f6f0c |
+ if (*oid == NULL) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s\n", ock_err(ERR_FUNCTION_FAILED));
|
|
|
1f6f0c |
+ return CKR_FUNCTION_FAILED;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* Decode private Kyber key */
|
|
|
1f6f0c |
+ rc = ber_decode_SEQUENCE(kyber_priv_key, &buf, &buf_len, &field_len);
|
|
|
1f6f0c |
+ if (rc != CKR_OK)
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* Now build the attributes */
|
|
|
1f6f0c |
+ offset = 0;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* Skip the version */
|
|
|
1f6f0c |
+ rc = ber_decode_INTEGER(buf + offset, &tmp, &len, &field_len);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("ber_decode_INTEGER failed\n");
|
|
|
1f6f0c |
+ goto cleanup;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ offset += field_len;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* sk */
|
|
|
1f6f0c |
+ rc = ber_decode_BIT_STRING(buf + offset, &tmp, &len, &field_len);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("ber_decode_BIT_STRING of (sk) failed\n");
|
|
|
1f6f0c |
+ goto cleanup;
|
|
|
1f6f0c |
+ } else {
|
|
|
1f6f0c |
+ tmp++; /* Remove unused-bits byte */
|
|
|
1f6f0c |
+ len--;
|
|
|
1f6f0c |
+ rc = build_attribute(CKA_IBM_KYBER_SK, tmp, len, &sk_attr);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("build_attribute for (sk) failed\n");
|
|
|
1f6f0c |
+ goto cleanup;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ offset += field_len;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* pk (optional, within choice) */
|
|
|
1f6f0c |
+ if (offset < buf_len) {
|
|
|
1f6f0c |
+ rc = ber_decode_CHOICE(buf + offset, &tmp, &len, &field_len, &option);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("ber_decode_BIT_STRING of (t1) failed\n");
|
|
|
1f6f0c |
+ goto cleanup;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ if (option != 0x00) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("ber_decode_CHOICE returned invalid option %ld\n",
|
|
|
1f6f0c |
+ option);
|
|
|
1f6f0c |
+ goto cleanup;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ offset += field_len - len;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = ber_decode_BIT_STRING(buf + offset, &tmp, &len, &field_len);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("ber_decode_BIT_STRING of (pk) failed\n");
|
|
|
1f6f0c |
+ goto cleanup;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ tmp++; /* Remove unused-bits byte */
|
|
|
1f6f0c |
+ len--;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ if (len > 64)
|
|
|
1f6f0c |
+ len -= 64; /* Remove 'rs' */
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = build_attribute(CKA_IBM_KYBER_PK, tmp, len, &pk_attr);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("build_attribute for (pk) failed\n");
|
|
|
1f6f0c |
+ goto cleanup;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ offset += field_len;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* Check if buffer big enough */
|
|
|
1f6f0c |
+ if (offset > buf_len) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s\n", ock_err(ERR_FUNCTION_FAILED));
|
|
|
1f6f0c |
+ rc = CKR_FUNCTION_FAILED;
|
|
|
1f6f0c |
+ goto cleanup;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* Add private key as CKA_VALUE to public key (z/OS ICSF compatibility) */
|
|
|
1f6f0c |
+ rc = ber_decode_SEQUENCE(data, &tmp, &len, &field_len);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s ber_decode_SEQUENCE failed with rc=0x%lx\n", __func__, rc);
|
|
|
1f6f0c |
+ goto cleanup;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ rc = build_attribute(CKA_VALUE, data, field_len, &value_attr);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("build_attribute for (t1) failed\n");
|
|
|
1f6f0c |
+ goto cleanup;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ *sk = sk_attr;
|
|
|
1f6f0c |
+ *pk = pk_attr;
|
|
|
1f6f0c |
+ *value = value_attr;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ return CKR_OK;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+cleanup:
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ if (sk_attr)
|
|
|
1f6f0c |
+ free(sk_attr);
|
|
|
1f6f0c |
+ if (pk_attr)
|
|
|
1f6f0c |
+ free(pk_attr);
|
|
|
1f6f0c |
+ if (value_attr)
|
|
|
1f6f0c |
+ free(value_attr);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+}
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
diff --git a/usr/lib/common/h_extern.h b/usr/lib/common/h_extern.h
|
|
|
1f6f0c |
index fdbcacd9..ee1ae08d 100644
|
|
|
1f6f0c |
--- a/usr/lib/common/h_extern.h
|
|
|
1f6f0c |
+++ b/usr/lib/common/h_extern.h
|
|
|
1f6f0c |
@@ -2505,6 +2505,40 @@ CK_RV ibm_dilithium_priv_unwrap(TEMPLATE *tmpl, CK_BYTE *data,
|
|
|
1f6f0c |
CK_RV ibm_dilithium_priv_unwrap_get_data(TEMPLATE *tmpl,
|
|
|
1f6f0c |
CK_BYTE *data, CK_ULONG total_length,
|
|
|
1f6f0c |
CK_BBOOL add_value);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+// Kyber routines
|
|
|
1f6f0c |
+//
|
|
|
1f6f0c |
+CK_RV ibm_kyber_publ_check_required_attributes(TEMPLATE *tmpl, CK_ULONG mode);
|
|
|
1f6f0c |
+CK_RV ibm_kyber_publ_set_default_attributes(TEMPLATE *tmpl, CK_ULONG mode);
|
|
|
1f6f0c |
+CK_RV ibm_kyber_publ_validate_attribute(STDLL_TokData_t *tokdata, TEMPLATE *tmpl,
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *attr, CK_ULONG mode);
|
|
|
1f6f0c |
+CK_RV ibm_kyber_publ_get_spki(TEMPLATE *tmpl, CK_BBOOL length_only,
|
|
|
1f6f0c |
+ CK_BYTE **data, CK_ULONG *data_len);
|
|
|
1f6f0c |
+CK_RV ibm_kyber_priv_check_required_attributes(TEMPLATE *tmpl, CK_ULONG mode);
|
|
|
1f6f0c |
+CK_RV ibm_kyber_priv_set_default_attributes(TEMPLATE *tmpl, CK_ULONG mode);
|
|
|
1f6f0c |
+CK_RV ibm_kyber_priv_validate_attribute(STDLL_TokData_t *tokdata, TEMPLATE *tmpl,
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *attr, CK_ULONG mode);
|
|
|
1f6f0c |
+CK_RV ibm_kyber_priv_wrap_get_data(TEMPLATE *tmpl, CK_BBOOL length_only,
|
|
|
1f6f0c |
+ CK_BYTE **data, CK_ULONG *data_len);
|
|
|
1f6f0c |
+CK_RV ibm_kyber_priv_unwrap(TEMPLATE *tmpl, CK_BYTE *data,
|
|
|
1f6f0c |
+ CK_ULONG total_length, CK_BBOOL add_value);
|
|
|
1f6f0c |
+CK_RV ibm_kyber_priv_unwrap_get_data(TEMPLATE *tmpl,
|
|
|
1f6f0c |
+ CK_BYTE *data, CK_ULONG total_length,
|
|
|
1f6f0c |
+ CK_BBOOL add_value);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+// PQC helper routines
|
|
|
1f6f0c |
+//
|
|
|
1f6f0c |
+CK_RV ibm_pqc_publ_get_spki(TEMPLATE *tmpl, CK_KEY_TYPE keytype,
|
|
|
1f6f0c |
+ CK_BBOOL length_only,
|
|
|
1f6f0c |
+ CK_BYTE **data, CK_ULONG *data_len);
|
|
|
1f6f0c |
+CK_RV ibm_pqc_priv_wrap_get_data(TEMPLATE *tmpl, CK_KEY_TYPE keytype,
|
|
|
1f6f0c |
+ CK_BBOOL length_only,
|
|
|
1f6f0c |
+ CK_BYTE **data, CK_ULONG *data_len);
|
|
|
1f6f0c |
+CK_RV ibm_pqc_priv_unwrap(TEMPLATE *tmpl, CK_KEY_TYPE keytype, CK_BYTE *data,
|
|
|
1f6f0c |
+ CK_ULONG total_length, CK_BBOOL add_value);
|
|
|
1f6f0c |
+CK_RV ibm_pqc_priv_unwrap_get_data(TEMPLATE *tmpl, CK_KEY_TYPE keytype,
|
|
|
1f6f0c |
+ CK_BYTE *data, CK_ULONG total_length,
|
|
|
1f6f0c |
+ CK_BBOOL add_value);
|
|
|
1f6f0c |
const struct pqc_oid *ibm_pqc_get_keyform_mode(TEMPLATE *tmpl,
|
|
|
1f6f0c |
CK_MECHANISM_TYPE mech);
|
|
|
1f6f0c |
CK_RV ibm_pqc_add_keyform_mode(TEMPLATE *tmpl, const struct pqc_oid *oid,
|
|
|
1f6f0c |
@@ -2782,6 +2816,31 @@ CK_RV ber_decode_IBM_DilithiumPrivateKey(CK_BYTE *data,
|
|
|
1f6f0c |
CK_ATTRIBUTE **value,
|
|
|
1f6f0c |
const struct pqc_oid **oid);
|
|
|
1f6f0c |
|
|
|
1f6f0c |
+CK_RV ber_encode_IBM_KyberPublicKey(CK_BBOOL length_only,
|
|
|
1f6f0c |
+ CK_BYTE **data, CK_ULONG *data_len,
|
|
|
1f6f0c |
+ const CK_BYTE *oid, CK_ULONG oid_len,
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *pk);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+CK_RV ber_decode_IBM_KyberPublicKey(CK_BYTE *data,
|
|
|
1f6f0c |
+ CK_ULONG data_len,
|
|
|
1f6f0c |
+ CK_ATTRIBUTE **pk_attr,
|
|
|
1f6f0c |
+ CK_ATTRIBUTE **value_attr,
|
|
|
1f6f0c |
+ const struct pqc_oid **oid);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+CK_RV ber_encode_IBM_KyberPrivateKey(CK_BBOOL length_only,
|
|
|
1f6f0c |
+ CK_BYTE **data,
|
|
|
1f6f0c |
+ CK_ULONG *data_len,
|
|
|
1f6f0c |
+ const CK_BYTE *oid, CK_ULONG oid_len,
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *sk,
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *pk);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+CK_RV ber_decode_IBM_KyberPrivateKey(CK_BYTE *data,
|
|
|
1f6f0c |
+ CK_ULONG data_len,
|
|
|
1f6f0c |
+ CK_ATTRIBUTE **sk,
|
|
|
1f6f0c |
+ CK_ATTRIBUTE **pk,
|
|
|
1f6f0c |
+ CK_ATTRIBUTE **value,
|
|
|
1f6f0c |
+ const struct pqc_oid **oid);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
typedef CK_RV (*t_rsa_encrypt)(STDLL_TokData_t *, CK_BYTE *in_data,
|
|
|
1f6f0c |
CK_ULONG in_data_len, CK_BYTE *out_data,
|
|
|
1f6f0c |
OBJECT *key_obj);
|
|
|
1f6f0c |
diff --git a/usr/lib/common/key.c b/usr/lib/common/key.c
|
|
|
1f6f0c |
index ba40cefd..ef329452 100644
|
|
|
1f6f0c |
--- a/usr/lib/common/key.c
|
|
|
1f6f0c |
+++ b/usr/lib/common/key.c
|
|
|
1f6f0c |
@@ -722,6 +722,9 @@ CK_RV publ_key_get_spki(TEMPLATE *tmpl, CK_ULONG keytype, CK_BBOOL length_only,
|
|
|
1f6f0c |
case CKK_IBM_PQC_DILITHIUM:
|
|
|
1f6f0c |
rc = ibm_dilithium_publ_get_spki(tmpl, length_only, data, data_len);
|
|
|
1f6f0c |
break;
|
|
|
1f6f0c |
+ case CKK_IBM_PQC_KYBER:
|
|
|
1f6f0c |
+ rc = ibm_kyber_publ_get_spki(tmpl, length_only, data, data_len);
|
|
|
1f6f0c |
+ break;
|
|
|
1f6f0c |
default:
|
|
|
1f6f0c |
TRACE_ERROR("%s\n", ock_err(ERR_KEY_TYPE_INCONSISTENT));
|
|
|
1f6f0c |
return CKR_KEY_TYPE_INCONSISTENT;
|
|
|
1f6f0c |
@@ -1053,6 +1056,9 @@ CK_RV priv_key_unwrap(TEMPLATE *tmpl,
|
|
|
1f6f0c |
case CKK_IBM_PQC_DILITHIUM:
|
|
|
1f6f0c |
rc = ibm_dilithium_priv_unwrap(tmpl, data, data_len, TRUE);
|
|
|
1f6f0c |
break;
|
|
|
1f6f0c |
+ case CKK_IBM_PQC_KYBER:
|
|
|
1f6f0c |
+ rc = ibm_kyber_priv_unwrap(tmpl, data, data_len, TRUE);
|
|
|
1f6f0c |
+ break;
|
|
|
1f6f0c |
default:
|
|
|
1f6f0c |
TRACE_ERROR("%s\n", ock_err(ERR_WRAPPED_KEY_INVALID));
|
|
|
1f6f0c |
return CKR_WRAPPED_KEY_INVALID;
|
|
|
1f6f0c |
@@ -3030,6 +3036,240 @@ error:
|
|
|
1f6f0c |
return rc;
|
|
|
1f6f0c |
}
|
|
|
1f6f0c |
|
|
|
1f6f0c |
+/*
|
|
|
1f6f0c |
+ * Extract the SubjectPublicKeyInfo from the Kyber public key
|
|
|
1f6f0c |
+ */
|
|
|
1f6f0c |
+CK_RV ibm_kyber_publ_get_spki(TEMPLATE *tmpl, CK_BBOOL length_only,
|
|
|
1f6f0c |
+ CK_BYTE **data, CK_ULONG *data_len)
|
|
|
1f6f0c |
+{
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *pk = NULL;
|
|
|
1f6f0c |
+ const struct pqc_oid *oid;
|
|
|
1f6f0c |
+ CK_RV rc;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ oid = ibm_pqc_get_keyform_mode(tmpl, CKM_IBM_KYBER);
|
|
|
1f6f0c |
+ if (oid == NULL)
|
|
|
1f6f0c |
+ return CKR_TEMPLATE_INCOMPLETE;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = template_attribute_get_non_empty(tmpl, CKA_IBM_KYBER_PK, &pk;;
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("Could not find CKA_IBM_KYBER_PK for the key.\n");
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = ber_encode_IBM_KyberPublicKey(length_only, data, data_len,
|
|
|
1f6f0c |
+ oid->oid, oid->oid_len, pk);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("ber_encode_IBM_KyberPublicKey failed.\n");
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ return CKR_OK;
|
|
|
1f6f0c |
+}
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+CK_RV ibm_kyber_priv_wrap_get_data(TEMPLATE *tmpl,
|
|
|
1f6f0c |
+ CK_BBOOL length_only,
|
|
|
1f6f0c |
+ CK_BYTE **data, CK_ULONG *data_len)
|
|
|
1f6f0c |
+{
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *sk = NULL, *pk = NULL;
|
|
|
1f6f0c |
+ const struct pqc_oid *oid;
|
|
|
1f6f0c |
+ CK_RV rc;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ oid = ibm_pqc_get_keyform_mode(tmpl, CKM_IBM_KYBER);
|
|
|
1f6f0c |
+ if (oid == NULL)
|
|
|
1f6f0c |
+ return CKR_TEMPLATE_INCOMPLETE;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = template_attribute_get_non_empty(tmpl, CKA_IBM_KYBER_SK, &sk;;
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("Could not find CKA_IBM_KYBER_SK for the key.\n");
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = template_attribute_get_non_empty(tmpl, CKA_IBM_KYBER_PK, &pk;;
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("Could not find CKA_IBM_KYBER_PK for the key.\n");
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = ber_encode_IBM_KyberPrivateKey(length_only, data, data_len,
|
|
|
1f6f0c |
+ oid->oid, oid->oid_len, sk, pk);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("ber_encode_IBM_KyberPrivateKey failed\n");
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+}
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+CK_RV ibm_kyber_priv_unwrap_get_data(TEMPLATE *tmpl, CK_BYTE *data,
|
|
|
1f6f0c |
+ CK_ULONG total_length,
|
|
|
1f6f0c |
+ CK_BBOOL add_value)
|
|
|
1f6f0c |
+{
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *pk = NULL;
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *value = NULL;
|
|
|
1f6f0c |
+ const struct pqc_oid *oid;
|
|
|
1f6f0c |
+ CK_RV rc;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = ber_decode_IBM_KyberPublicKey(data, total_length, &pk,
|
|
|
1f6f0c |
+ &value, &oid;;
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("ber_decode_IBM_KyberPublicKey failed\n");
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = ibm_pqc_add_keyform_mode(tmpl, oid, CKM_IBM_KYBER);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("ibm_pqc_add_keyform_mode failed\n");
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = template_update_attribute(tmpl, pk);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("template_update_attribute failed.\n");
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ pk = NULL;
|
|
|
1f6f0c |
+ if (add_value) {
|
|
|
1f6f0c |
+ rc = template_update_attribute(tmpl, value);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("template_update_attribute failed.\n");
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ } else {
|
|
|
1f6f0c |
+ free(value);
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ value = NULL;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ return CKR_OK;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+error:
|
|
|
1f6f0c |
+ if (pk)
|
|
|
1f6f0c |
+ free(pk);
|
|
|
1f6f0c |
+ if (value)
|
|
|
1f6f0c |
+ free(value);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+}
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+//
|
|
|
1f6f0c |
+//
|
|
|
1f6f0c |
+CK_RV ibm_kyber_priv_unwrap(TEMPLATE *tmpl, CK_BYTE *data,
|
|
|
1f6f0c |
+ CK_ULONG total_length, CK_BBOOL add_value)
|
|
|
1f6f0c |
+{
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *sk = NULL, *pk = NULL, *value = NULL;
|
|
|
1f6f0c |
+ const struct pqc_oid *oid;
|
|
|
1f6f0c |
+ CK_RV rc;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = ber_decode_IBM_KyberPrivateKey(data, total_length,
|
|
|
1f6f0c |
+ &sk, &pk, &value, &oid;;
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("ber_decode_IBM_KyberPrivateKey failed\n");
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = ibm_pqc_add_keyform_mode(tmpl, oid, CKM_IBM_KYBER);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("ibm_pqc_add_keyform_mode failed\n");
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = template_update_attribute(tmpl, sk);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("template_update_attribute failed\n");
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ sk = NULL;
|
|
|
1f6f0c |
+ rc = template_update_attribute(tmpl, pk);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("template_update_attribute failed\n");
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ pk = NULL;
|
|
|
1f6f0c |
+ if (add_value) {
|
|
|
1f6f0c |
+ rc = template_update_attribute(tmpl, value);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_DEVEL("template_update_attribute failed.\n");
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ } else {
|
|
|
1f6f0c |
+ free(value);
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ value = NULL;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ return CKR_OK;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+error:
|
|
|
1f6f0c |
+ if (sk)
|
|
|
1f6f0c |
+ free(sk);
|
|
|
1f6f0c |
+ if (pk)
|
|
|
1f6f0c |
+ free(pk);
|
|
|
1f6f0c |
+ if (value)
|
|
|
1f6f0c |
+ free(value);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+}
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+CK_RV ibm_pqc_publ_get_spki(TEMPLATE *tmpl, CK_KEY_TYPE keytype,
|
|
|
1f6f0c |
+ CK_BBOOL length_only,
|
|
|
1f6f0c |
+ CK_BYTE **data, CK_ULONG *data_len)
|
|
|
1f6f0c |
+{
|
|
|
1f6f0c |
+ switch (keytype) {
|
|
|
1f6f0c |
+ case CKK_IBM_PQC_DILITHIUM:
|
|
|
1f6f0c |
+ return ibm_dilithium_publ_get_spki(tmpl, length_only, data, data_len);
|
|
|
1f6f0c |
+ case CKK_IBM_PQC_KYBER:
|
|
|
1f6f0c |
+ return ibm_kyber_publ_get_spki(tmpl, length_only, data, data_len);
|
|
|
1f6f0c |
+ default:
|
|
|
1f6f0c |
+ TRACE_DEVEL("Key type 0x%lx not supported.\n", keytype);
|
|
|
1f6f0c |
+ return CKR_KEY_TYPE_INCONSISTENT;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+}
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+CK_RV ibm_pqc_priv_wrap_get_data(TEMPLATE *tmpl, CK_KEY_TYPE keytype,
|
|
|
1f6f0c |
+ CK_BBOOL length_only,
|
|
|
1f6f0c |
+ CK_BYTE **data, CK_ULONG *data_len)
|
|
|
1f6f0c |
+{
|
|
|
1f6f0c |
+ switch (keytype) {
|
|
|
1f6f0c |
+ case CKK_IBM_PQC_DILITHIUM:
|
|
|
1f6f0c |
+ return ibm_dilithium_priv_wrap_get_data(tmpl, length_only, data,
|
|
|
1f6f0c |
+ data_len);
|
|
|
1f6f0c |
+ case CKK_IBM_PQC_KYBER:
|
|
|
1f6f0c |
+ return ibm_kyber_priv_wrap_get_data(tmpl, length_only, data, data_len);
|
|
|
1f6f0c |
+ default:
|
|
|
1f6f0c |
+ TRACE_DEVEL("Key type 0x%lx not supported.\n", keytype);
|
|
|
1f6f0c |
+ return CKR_KEY_TYPE_INCONSISTENT;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+}
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+CK_RV ibm_pqc_priv_unwrap(TEMPLATE *tmpl, CK_KEY_TYPE keytype, CK_BYTE *data,
|
|
|
1f6f0c |
+ CK_ULONG total_length, CK_BBOOL add_value)
|
|
|
1f6f0c |
+{
|
|
|
1f6f0c |
+ switch (keytype) {
|
|
|
1f6f0c |
+ case CKK_IBM_PQC_DILITHIUM:
|
|
|
1f6f0c |
+ return ibm_dilithium_priv_unwrap(tmpl, data, total_length, add_value);
|
|
|
1f6f0c |
+ case CKK_IBM_PQC_KYBER:
|
|
|
1f6f0c |
+ return ibm_kyber_priv_unwrap(tmpl, data, total_length, add_value);
|
|
|
1f6f0c |
+ default:
|
|
|
1f6f0c |
+ TRACE_DEVEL("Key type 0x%lx not supported.\n", keytype);
|
|
|
1f6f0c |
+ return CKR_KEY_TYPE_INCONSISTENT;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+}
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+CK_RV ibm_pqc_priv_unwrap_get_data(TEMPLATE *tmpl, CK_KEY_TYPE keytype,
|
|
|
1f6f0c |
+ CK_BYTE *data, CK_ULONG total_length,
|
|
|
1f6f0c |
+ CK_BBOOL add_value)
|
|
|
1f6f0c |
+{
|
|
|
1f6f0c |
+ switch (keytype) {
|
|
|
1f6f0c |
+ case CKK_IBM_PQC_DILITHIUM:
|
|
|
1f6f0c |
+ return ibm_dilithium_priv_unwrap_get_data(tmpl, data, total_length,
|
|
|
1f6f0c |
+ add_value);
|
|
|
1f6f0c |
+ case CKK_IBM_PQC_KYBER:
|
|
|
1f6f0c |
+ return ibm_kyber_priv_unwrap_get_data(tmpl, data, total_length,
|
|
|
1f6f0c |
+ add_value);
|
|
|
1f6f0c |
+ default:
|
|
|
1f6f0c |
+ TRACE_DEVEL("Key type 0x%lx not supported.\n", keytype);
|
|
|
1f6f0c |
+ return CKR_KEY_TYPE_INCONSISTENT;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+}
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
// dsa_publ_check_required_attributes()
|
|
|
1f6f0c |
//
|
|
|
1f6f0c |
CK_RV dsa_publ_check_required_attributes(TEMPLATE *tmpl, CK_ULONG mode)
|
|
|
1f6f0c |
@@ -4987,6 +5227,152 @@ error:
|
|
|
1f6f0c |
return rc;
|
|
|
1f6f0c |
}
|
|
|
1f6f0c |
|
|
|
1f6f0c |
+// ibm_dilithium_publ_set_default_attributes()
|
|
|
1f6f0c |
+//
|
|
|
1f6f0c |
+CK_RV ibm_kyber_publ_set_default_attributes(TEMPLATE *tmpl, CK_ULONG mode)
|
|
|
1f6f0c |
+{
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *type_attr = NULL;
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *pk_attr = NULL;
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *value_attr = NULL;
|
|
|
1f6f0c |
+ CK_RV rc;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ publ_key_set_default_attributes(tmpl, mode);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ type_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE) + sizeof(CK_KEY_TYPE));
|
|
|
1f6f0c |
+ pk_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE));
|
|
|
1f6f0c |
+ value_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE));
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ if (!type_attr || !pk_attr ||!value_attr) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s\n", ock_err(ERR_HOST_MEMORY));
|
|
|
1f6f0c |
+ rc = CKR_HOST_MEMORY;
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ type_attr->type = CKA_KEY_TYPE;
|
|
|
1f6f0c |
+ type_attr->ulValueLen = sizeof(CK_KEY_TYPE);
|
|
|
1f6f0c |
+ type_attr->pValue = (CK_BYTE *) type_attr + sizeof(CK_ATTRIBUTE);
|
|
|
1f6f0c |
+ *(CK_KEY_TYPE *) type_attr->pValue = CKK_IBM_PQC_KYBER;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ pk_attr->type = CKA_IBM_KYBER_PK;
|
|
|
1f6f0c |
+ pk_attr->ulValueLen = 0;
|
|
|
1f6f0c |
+ pk_attr->pValue = NULL;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ value_attr->type = CKA_VALUE;
|
|
|
1f6f0c |
+ value_attr->ulValueLen = 0;
|
|
|
1f6f0c |
+ value_attr->pValue = NULL;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = template_update_attribute(tmpl, type_attr);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("template_update_attribute failed\n");
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ type_attr = NULL;
|
|
|
1f6f0c |
+ rc = template_update_attribute(tmpl, pk_attr);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("template_update_attribute failed\n");
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ pk_attr = NULL;
|
|
|
1f6f0c |
+ rc = template_update_attribute(tmpl, value_attr);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("template_update_attribute failed\n");
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ value_attr = NULL;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ return CKR_OK;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+error:
|
|
|
1f6f0c |
+ if (type_attr)
|
|
|
1f6f0c |
+ free(type_attr);
|
|
|
1f6f0c |
+ if (pk_attr)
|
|
|
1f6f0c |
+ free(pk_attr);
|
|
|
1f6f0c |
+ if (value_attr)
|
|
|
1f6f0c |
+ free(value_attr);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+}
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+// ibm_dilithium_priv_set_default_attributes()
|
|
|
1f6f0c |
+//
|
|
|
1f6f0c |
+CK_RV ibm_kyber_priv_set_default_attributes(TEMPLATE *tmpl, CK_ULONG mode)
|
|
|
1f6f0c |
+{
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *type_attr = NULL;
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *sk_attr = NULL;
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *pk_attr = NULL;
|
|
|
1f6f0c |
+ CK_ATTRIBUTE *value_attr = NULL;
|
|
|
1f6f0c |
+ CK_RV rc;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ priv_key_set_default_attributes(tmpl, mode);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ type_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE) + sizeof(CK_KEY_TYPE));
|
|
|
1f6f0c |
+ sk_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE));
|
|
|
1f6f0c |
+ pk_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE));
|
|
|
1f6f0c |
+ value_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE));
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ if (!type_attr || !sk_attr || !pk_attr || !value_attr) {
|
|
|
1f6f0c |
+ TRACE_ERROR("%s\n", ock_err(ERR_HOST_MEMORY));
|
|
|
1f6f0c |
+ rc = CKR_HOST_MEMORY;
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ type_attr->type = CKA_KEY_TYPE;
|
|
|
1f6f0c |
+ type_attr->ulValueLen = sizeof(CK_KEY_TYPE);
|
|
|
1f6f0c |
+ type_attr->pValue = (CK_BYTE *) type_attr + sizeof(CK_ATTRIBUTE);
|
|
|
1f6f0c |
+ *(CK_KEY_TYPE *) type_attr->pValue = CKK_IBM_PQC_KYBER;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ sk_attr->type = CKA_IBM_KYBER_SK;
|
|
|
1f6f0c |
+ sk_attr->ulValueLen = 0;
|
|
|
1f6f0c |
+ sk_attr->pValue = NULL;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ pk_attr->type = CKA_IBM_KYBER_PK;
|
|
|
1f6f0c |
+ pk_attr->ulValueLen = 0;
|
|
|
1f6f0c |
+ pk_attr->pValue = NULL;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ value_attr->type = CKA_VALUE;
|
|
|
1f6f0c |
+ value_attr->ulValueLen = 0;
|
|
|
1f6f0c |
+ value_attr->pValue = NULL;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = template_update_attribute(tmpl, type_attr);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("template_update_attribute failed\n");
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ type_attr = NULL;
|
|
|
1f6f0c |
+ rc = template_update_attribute(tmpl, sk_attr);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("template_update_attribute failed\n");
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ sk_attr = NULL;
|
|
|
1f6f0c |
+ rc = template_update_attribute(tmpl, pk_attr);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("template_update_attribute failed\n");
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ pk_attr = NULL;
|
|
|
1f6f0c |
+ rc = template_update_attribute(tmpl, value_attr);
|
|
|
1f6f0c |
+ if (rc != CKR_OK) {
|
|
|
1f6f0c |
+ TRACE_ERROR("template_update_attribute failed\n");
|
|
|
1f6f0c |
+ goto error;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ value_attr = NULL;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ return CKR_OK;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+error:
|
|
|
1f6f0c |
+ if (type_attr)
|
|
|
1f6f0c |
+ free(type_attr);
|
|
|
1f6f0c |
+ if (sk_attr)
|
|
|
1f6f0c |
+ free(sk_attr);
|
|
|
1f6f0c |
+ if (pk_attr)
|
|
|
1f6f0c |
+ free(pk_attr);
|
|
|
1f6f0c |
+ if (value_attr)
|
|
|
1f6f0c |
+ free(value_attr);
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+}
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
static CK_RV ibm_pqc_check_attributes(TEMPLATE *tmpl, CK_ULONG mode,
|
|
|
1f6f0c |
CK_MECHANISM_TYPE mech,
|
|
|
1f6f0c |
CK_ULONG *req_attrs,
|
|
|
1f6f0c |
@@ -5122,6 +5508,43 @@ CK_RV ibm_dilithium_priv_check_required_attributes(TEMPLATE *tmpl, CK_ULONG mode
|
|
|
1f6f0c |
return priv_key_check_required_attributes(tmpl, mode);
|
|
|
1f6f0c |
}
|
|
|
1f6f0c |
|
|
|
1f6f0c |
+// ibm_kyber_publ_check_required_attributes()
|
|
|
1f6f0c |
+//
|
|
|
1f6f0c |
+CK_RV ibm_kyber_publ_check_required_attributes(TEMPLATE *tmpl, CK_ULONG mode)
|
|
|
1f6f0c |
+{
|
|
|
1f6f0c |
+ static CK_ULONG req_attrs[] = {
|
|
|
1f6f0c |
+ CKA_IBM_KYBER_PK,
|
|
|
1f6f0c |
+ };
|
|
|
1f6f0c |
+ CK_RV rc;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = ibm_pqc_check_attributes(tmpl, mode, CKM_IBM_KYBER, req_attrs,
|
|
|
1f6f0c |
+ sizeof(req_attrs) / sizeof(req_attrs[0]));
|
|
|
1f6f0c |
+ if (rc != CKR_OK)
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* All required attrs found, check them */
|
|
|
1f6f0c |
+ return publ_key_check_required_attributes(tmpl, mode);
|
|
|
1f6f0c |
+}
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+// ibm_kyber_priv_check_required_attributes()
|
|
|
1f6f0c |
+//
|
|
|
1f6f0c |
+CK_RV ibm_kyber_priv_check_required_attributes(TEMPLATE *tmpl, CK_ULONG mode)
|
|
|
1f6f0c |
+{
|
|
|
1f6f0c |
+ static CK_ULONG req_attrs[] = {
|
|
|
1f6f0c |
+ CKA_IBM_KYBER_SK,
|
|
|
1f6f0c |
+ CKA_IBM_KYBER_PK,
|
|
|
1f6f0c |
+ };
|
|
|
1f6f0c |
+ CK_RV rc;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ rc = ibm_pqc_check_attributes(tmpl, mode, CKM_IBM_KYBER, req_attrs,
|
|
|
1f6f0c |
+ sizeof(req_attrs) / sizeof(req_attrs[0]));
|
|
|
1f6f0c |
+ if (rc != CKR_OK)
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ /* All required attrs found, check them */
|
|
|
1f6f0c |
+ return priv_key_check_required_attributes(tmpl, mode);
|
|
|
1f6f0c |
+}
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
static CK_RV ibm_pqc_validate_keyform_mode(CK_ATTRIBUTE *attr, CK_ULONG mode,
|
|
|
1f6f0c |
CK_MECHANISM_TYPE mech)
|
|
|
1f6f0c |
{
|
|
|
1f6f0c |
@@ -5228,6 +5651,59 @@ CK_RV ibm_dilithium_priv_validate_attribute(STDLL_TokData_t *tokdata,
|
|
|
1f6f0c |
}
|
|
|
1f6f0c |
}
|
|
|
1f6f0c |
|
|
|
1f6f0c |
+// ibm_kyber_publ_validate_attribute()
|
|
|
1f6f0c |
+//
|
|
|
1f6f0c |
+CK_RV ibm_kyber_publ_validate_attribute(STDLL_TokData_t *tokdata,
|
|
|
1f6f0c |
+ TEMPLATE *tmpl, CK_ATTRIBUTE *attr,
|
|
|
1f6f0c |
+ CK_ULONG mode)
|
|
|
1f6f0c |
+{
|
|
|
1f6f0c |
+ CK_RV rc;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ switch (attr->type) {
|
|
|
1f6f0c |
+ case CKA_IBM_KYBER_KEYFORM:
|
|
|
1f6f0c |
+ case CKA_IBM_KYBER_MODE:
|
|
|
1f6f0c |
+ rc = ibm_pqc_validate_keyform_mode(attr, mode, CKM_IBM_KYBER);
|
|
|
1f6f0c |
+ if (rc != CKR_OK)
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+ return CKR_OK;
|
|
|
1f6f0c |
+ case CKA_IBM_KYBER_PK:
|
|
|
1f6f0c |
+ case CKA_VALUE:
|
|
|
1f6f0c |
+ if (mode == MODE_CREATE)
|
|
|
1f6f0c |
+ return CKR_OK;
|
|
|
1f6f0c |
+ TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_READ_ONLY));
|
|
|
1f6f0c |
+ return CKR_ATTRIBUTE_READ_ONLY;
|
|
|
1f6f0c |
+ default:
|
|
|
1f6f0c |
+ return publ_key_validate_attribute(tokdata, tmpl, attr, mode);
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+}
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+// ibm_kyber_priv_validate_attribute()
|
|
|
1f6f0c |
+//
|
|
|
1f6f0c |
+CK_RV ibm_kyber_priv_validate_attribute(STDLL_TokData_t *tokdata,
|
|
|
1f6f0c |
+ TEMPLATE *tmpl, CK_ATTRIBUTE *attr,
|
|
|
1f6f0c |
+ CK_ULONG mode)
|
|
|
1f6f0c |
+{
|
|
|
1f6f0c |
+ CK_RV rc;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+ switch (attr->type) {
|
|
|
1f6f0c |
+ case CKA_IBM_KYBER_KEYFORM:
|
|
|
1f6f0c |
+ case CKA_IBM_KYBER_MODE:
|
|
|
1f6f0c |
+ rc = ibm_pqc_validate_keyform_mode(attr, mode, CKM_IBM_KYBER);
|
|
|
1f6f0c |
+ if (rc != CKR_OK)
|
|
|
1f6f0c |
+ return rc;
|
|
|
1f6f0c |
+ return CKR_OK;
|
|
|
1f6f0c |
+ case CKA_IBM_KYBER_SK:
|
|
|
1f6f0c |
+ case CKA_IBM_KYBER_PK:
|
|
|
1f6f0c |
+ case CKA_VALUE:
|
|
|
1f6f0c |
+ if (mode == MODE_CREATE)
|
|
|
1f6f0c |
+ return CKR_OK;
|
|
|
1f6f0c |
+ TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_READ_ONLY));
|
|
|
1f6f0c |
+ return CKR_ATTRIBUTE_READ_ONLY;
|
|
|
1f6f0c |
+ default:
|
|
|
1f6f0c |
+ return priv_key_validate_attribute(tokdata, tmpl, attr, mode);
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+}
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
// generic_secret_check_required_attributes()
|
|
|
1f6f0c |
//
|
|
|
1f6f0c |
CK_RV generic_secret_check_required_attributes(TEMPLATE *tmpl, CK_ULONG mode)
|
|
|
1f6f0c |
diff --git a/usr/lib/common/key_mgr.c b/usr/lib/common/key_mgr.c
|
|
|
1f6f0c |
index 01103dc2..8fcdc88e 100644
|
|
|
1f6f0c |
--- a/usr/lib/common/key_mgr.c
|
|
|
1f6f0c |
+++ b/usr/lib/common/key_mgr.c
|
|
|
1f6f0c |
@@ -1421,6 +1421,16 @@ CK_RV key_mgr_get_private_key_type(CK_BYTE *keydata,
|
|
|
1f6f0c |
return CKR_OK;
|
|
|
1f6f0c |
}
|
|
|
1f6f0c |
}
|
|
|
1f6f0c |
+ // Check only the OBJECT IDENTIFIERs for KYBER
|
|
|
1f6f0c |
+ //
|
|
|
1f6f0c |
+ for (i = 0; kyber_oids[i].oid != NULL; i++) {
|
|
|
1f6f0c |
+ if (alg_len == kyber_oids[i].oid_len + ber_NULLLen &&
|
|
|
1f6f0c |
+ memcmp(alg, kyber_oids[i].oid, kyber_oids[i].oid_len) == 0 &&
|
|
|
1f6f0c |
+ memcmp(alg + kyber_oids[i].oid_len, ber_NULL, ber_NULLLen) == 0) {
|
|
|
1f6f0c |
+ *keytype = CKK_IBM_PQC_KYBER;
|
|
|
1f6f0c |
+ return CKR_OK;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
|
|
|
1f6f0c |
TRACE_ERROR("%s\n", ock_err(ERR_TEMPLATE_INCOMPLETE));
|
|
|
1f6f0c |
return CKR_TEMPLATE_INCOMPLETE;
|
|
|
1f6f0c |
diff --git a/usr/lib/common/mech_ec.c b/usr/lib/common/mech_ec.c
|
|
|
1f6f0c |
index 9df53b46..3a642f50 100644
|
|
|
1f6f0c |
--- a/usr/lib/common/mech_ec.c
|
|
|
1f6f0c |
+++ b/usr/lib/common/mech_ec.c
|
|
|
1f6f0c |
@@ -912,6 +912,9 @@ CK_RV pkcs_get_keytype(CK_ATTRIBUTE *attrs, CK_ULONG attrs_len,
|
|
|
1f6f0c |
case CKM_IBM_DILITHIUM:
|
|
|
1f6f0c |
*type = CKK_IBM_PQC_DILITHIUM;
|
|
|
1f6f0c |
break;
|
|
|
1f6f0c |
+ case CKM_IBM_KYBER:
|
|
|
1f6f0c |
+ *type = CKK_IBM_PQC_KYBER;
|
|
|
1f6f0c |
+ break;
|
|
|
1f6f0c |
default:
|
|
|
1f6f0c |
return CKR_MECHANISM_INVALID;
|
|
|
1f6f0c |
}
|
|
|
1f6f0c |
diff --git a/usr/lib/common/template.c b/usr/lib/common/template.c
|
|
|
1f6f0c |
index 3338e847..37831efc 100644
|
|
|
1f6f0c |
--- a/usr/lib/common/template.c
|
|
|
1f6f0c |
+++ b/usr/lib/common/template.c
|
|
|
1f6f0c |
@@ -164,6 +164,8 @@ CK_RV template_add_default_attributes(TEMPLATE *tmpl, TEMPLATE *basetmpl,
|
|
|
1f6f0c |
return dh_publ_set_default_attributes(tmpl, mode);
|
|
|
1f6f0c |
case CKK_IBM_PQC_DILITHIUM:
|
|
|
1f6f0c |
return ibm_dilithium_publ_set_default_attributes(tmpl, mode);
|
|
|
1f6f0c |
+ case CKK_IBM_PQC_KYBER:
|
|
|
1f6f0c |
+ return ibm_kyber_publ_set_default_attributes(tmpl, mode);
|
|
|
1f6f0c |
default:
|
|
|
1f6f0c |
TRACE_ERROR("%s: %lx\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID),
|
|
|
1f6f0c |
subclass);
|
|
|
1f6f0c |
@@ -181,6 +183,8 @@ CK_RV template_add_default_attributes(TEMPLATE *tmpl, TEMPLATE *basetmpl,
|
|
|
1f6f0c |
return dh_priv_set_default_attributes(tmpl, mode);
|
|
|
1f6f0c |
case CKK_IBM_PQC_DILITHIUM:
|
|
|
1f6f0c |
return ibm_dilithium_priv_set_default_attributes(tmpl, mode);
|
|
|
1f6f0c |
+ case CKK_IBM_PQC_KYBER:
|
|
|
1f6f0c |
+ return ibm_kyber_priv_set_default_attributes(tmpl, mode);
|
|
|
1f6f0c |
default:
|
|
|
1f6f0c |
TRACE_ERROR("%s: %lx\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID),
|
|
|
1f6f0c |
subclass);
|
|
|
1f6f0c |
@@ -409,6 +413,8 @@ CK_RV template_check_required_attributes(TEMPLATE *tmpl, CK_ULONG class,
|
|
|
1f6f0c |
return dh_publ_check_required_attributes(tmpl, mode);
|
|
|
1f6f0c |
case CKK_IBM_PQC_DILITHIUM:
|
|
|
1f6f0c |
return ibm_dilithium_publ_check_required_attributes(tmpl, mode);
|
|
|
1f6f0c |
+ case CKK_IBM_PQC_KYBER:
|
|
|
1f6f0c |
+ return ibm_kyber_publ_check_required_attributes(tmpl, mode);
|
|
|
1f6f0c |
default:
|
|
|
1f6f0c |
TRACE_ERROR("%s: %lx\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID),
|
|
|
1f6f0c |
subclass);
|
|
|
1f6f0c |
@@ -426,6 +432,8 @@ CK_RV template_check_required_attributes(TEMPLATE *tmpl, CK_ULONG class,
|
|
|
1f6f0c |
return dh_priv_check_required_attributes(tmpl, mode);
|
|
|
1f6f0c |
case CKK_IBM_PQC_DILITHIUM:
|
|
|
1f6f0c |
return ibm_dilithium_priv_check_required_attributes(tmpl, mode);
|
|
|
1f6f0c |
+ case CKK_IBM_PQC_KYBER:
|
|
|
1f6f0c |
+ return ibm_kyber_priv_check_required_attributes(tmpl, mode);
|
|
|
1f6f0c |
default:
|
|
|
1f6f0c |
TRACE_ERROR("%s: %lx\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID),
|
|
|
1f6f0c |
subclass);
|
|
|
1f6f0c |
@@ -1642,6 +1650,8 @@ CK_RV template_validate_attribute(STDLL_TokData_t *tokdata, TEMPLATE *tmpl,
|
|
|
1f6f0c |
return dh_publ_validate_attribute(tokdata, tmpl, attr, mode);
|
|
|
1f6f0c |
case CKK_IBM_PQC_DILITHIUM:
|
|
|
1f6f0c |
return ibm_dilithium_publ_validate_attribute(tokdata, tmpl, attr, mode);
|
|
|
1f6f0c |
+ case CKK_IBM_PQC_KYBER:
|
|
|
1f6f0c |
+ return ibm_kyber_publ_validate_attribute(tokdata, tmpl, attr, mode);
|
|
|
1f6f0c |
default:
|
|
|
1f6f0c |
TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID));
|
|
|
1f6f0c |
return CKR_ATTRIBUTE_VALUE_INVALID; // unknown key type
|
|
|
1f6f0c |
@@ -1658,6 +1668,8 @@ CK_RV template_validate_attribute(STDLL_TokData_t *tokdata, TEMPLATE *tmpl,
|
|
|
1f6f0c |
return dh_priv_validate_attribute(tokdata, tmpl, attr, mode);
|
|
|
1f6f0c |
case CKK_IBM_PQC_DILITHIUM:
|
|
|
1f6f0c |
return ibm_dilithium_priv_validate_attribute(tokdata, tmpl, attr, mode);
|
|
|
1f6f0c |
+ case CKK_IBM_PQC_KYBER:
|
|
|
1f6f0c |
+ return ibm_kyber_priv_validate_attribute(tokdata, tmpl, attr, mode);
|
|
|
1f6f0c |
default:
|
|
|
1f6f0c |
TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID));
|
|
|
1f6f0c |
return CKR_ATTRIBUTE_VALUE_INVALID; // unknown key type
|
|
|
1f6f0c |
diff --git a/usr/lib/ep11_stdll/ep11_specific.c b/usr/lib/ep11_stdll/ep11_specific.c
|
|
|
1f6f0c |
index 479951cb..44796dba 100644
|
|
|
1f6f0c |
--- a/usr/lib/ep11_stdll/ep11_specific.c
|
|
|
1f6f0c |
+++ b/usr/lib/ep11_stdll/ep11_specific.c
|
|
|
1f6f0c |
@@ -362,6 +362,13 @@ static const version_req_t ibm_dilithium_req_versions[] = {
|
|
|
1f6f0c |
};
|
|
|
1f6f0c |
#define NUM_DILITHIUM_REQ (sizeof(ibm_dilithium_req_versions) / sizeof(version_req_t))
|
|
|
1f6f0c |
|
|
|
1f6f0c |
+static const CK_VERSION ibm_cex8p_kyber_support = { .major = 8, .minor = 9 };
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
+static const version_req_t ibm_kyber_req_versions[] = {
|
|
|
1f6f0c |
+ { .card_type = 8, .min_firmware_version = &ibm_cex8p_kyber_support }
|
|
|
1f6f0c |
+};
|
|
|
1f6f0c |
+#define NUM_KYBER_REQ (sizeof(ibm_kyber_req_versions) / sizeof(version_req_t))
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
static const CK_VERSION ibm_cex6p_reencrypt_single_support =
|
|
|
1f6f0c |
{ .major = 6, .minor = 15 };
|
|
|
1f6f0c |
static const CK_VERSION ibm_cex7p_reencrypt_single_support =
|
|
|
1f6f0c |
@@ -1809,7 +1816,8 @@ static CK_RV check_key_attributes(STDLL_TokData_t * tokdata,
|
|
|
1f6f0c |
check_types = &check_types_pub[0];
|
|
|
1f6f0c |
attr_cnt = sizeof(check_types_pub) / sizeof(CK_ULONG);
|
|
|
1f6f0c |
}
|
|
|
1f6f0c |
- /* do nothing for CKM_DH_PKCS_KEY_PAIR_GEN and CKK_IBM_PQC_DILITHIUM */
|
|
|
1f6f0c |
+ /* do nothing for CKM_DH_PKCS_KEY_PAIR_GEN, CKK_IBM_PQC_DILITHIUM,
|
|
|
1f6f0c |
+ and CKK_IBM_PQC_KYBER */
|
|
|
1f6f0c |
break;
|
|
|
1f6f0c |
case CKO_PRIVATE_KEY:
|
|
|
1f6f0c |
if ((kt == CKK_EC) || (kt == CKK_ECDSA) || (kt == CKK_DSA)) {
|
|
|
1f6f0c |
@@ -1824,7 +1832,7 @@ static CK_RV check_key_attributes(STDLL_TokData_t * tokdata,
|
|
|
1f6f0c |
check_types = &check_types_derive[0];
|
|
|
1f6f0c |
attr_cnt = sizeof(check_types_derive) / sizeof(CK_ULONG);
|
|
|
1f6f0c |
}
|
|
|
1f6f0c |
- /* Do nothing for CKK_IBM_PQC_DILITHIUM */
|
|
|
1f6f0c |
+ /* Do nothing for CKK_IBM_PQC_DILITHIUM and CKK_IBM_PQC_KYBER */
|
|
|
1f6f0c |
break;
|
|
|
1f6f0c |
default:
|
|
|
1f6f0c |
return CKR_OK;
|
|
|
1f6f0c |
@@ -2029,6 +2037,13 @@ static CK_BBOOL attr_applicable_for_ep11(STDLL_TokData_t * tokdata,
|
|
|
1f6f0c |
attr->type == CKA_IBM_DILITHIUM_MODE)
|
|
|
1f6f0c |
return CK_FALSE;
|
|
|
1f6f0c |
break;
|
|
|
1f6f0c |
+ case CKK_IBM_PQC_KYBER:
|
|
|
1f6f0c |
+ if (attr->type == CKA_SIGN || attr->type == CKA_VERIFY ||
|
|
|
1f6f0c |
+ attr->type == CKA_WRAP || attr->type == CKA_UNWRAP ||
|
|
|
1f6f0c |
+ attr->type == CKA_IBM_KYBER_KEYFORM ||
|
|
|
1f6f0c |
+ attr->type == CKA_IBM_KYBER_MODE)
|
|
|
1f6f0c |
+ return CK_FALSE;
|
|
|
1f6f0c |
+ break;
|
|
|
1f6f0c |
default:
|
|
|
1f6f0c |
break;
|
|
|
1f6f0c |
}
|
|
|
1f6f0c |
@@ -9145,6 +9160,10 @@ CK_RV ep11tok_unwrap_key(STDLL_TokData_t * tokdata, SESSION * session,
|
|
|
1f6f0c |
rc = ibm_dilithium_priv_unwrap_get_data(key_obj->template,
|
|
|
1f6f0c |
csum, cslen, FALSE);
|
|
|
1f6f0c |
break;
|
|
|
1f6f0c |
+ case CKK_IBM_PQC_KYBER:
|
|
|
1f6f0c |
+ rc = ibm_kyber_priv_unwrap_get_data(key_obj->template,
|
|
|
1f6f0c |
+ csum, cslen, FALSE);
|
|
|
1f6f0c |
+ break;
|
|
|
1f6f0c |
}
|
|
|
1f6f0c |
|
|
|
1f6f0c |
if (rc != 0) {
|
|
|
1f6f0c |
@@ -9240,6 +9259,7 @@ static const CK_MECHANISM_TYPE ep11_supported_mech_list[] = {
|
|
|
1f6f0c |
CKM_IBM_EC_X448,
|
|
|
1f6f0c |
CKM_IBM_ED25519_SHA512,
|
|
|
1f6f0c |
CKM_IBM_ED448_SHA3,
|
|
|
1f6f0c |
+ CKM_IBM_KYBER,
|
|
|
1f6f0c |
CKM_IBM_SHA3_224,
|
|
|
1f6f0c |
CKM_IBM_SHA3_224_HMAC,
|
|
|
1f6f0c |
CKM_IBM_SHA3_256,
|
|
|
1f6f0c |
@@ -9478,6 +9498,7 @@ CK_RV ep11tok_is_mechanism_supported(STDLL_TokData_t *tokdata,
|
|
|
1f6f0c |
CK_VERSION ver1_3 = { .major = 1, .minor = 3 };
|
|
|
1f6f0c |
CK_VERSION ver3 = { .major = 3, .minor = 0 };
|
|
|
1f6f0c |
CK_VERSION ver3_1 = { .major = 3, .minor = 0x10 };
|
|
|
1f6f0c |
+ CK_VERSION ver4 = { .major = 4, .minor = 0 };
|
|
|
1f6f0c |
CK_BBOOL found = FALSE;
|
|
|
1f6f0c |
CK_ULONG i;
|
|
|
1f6f0c |
int status;
|
|
|
1f6f0c |
@@ -9630,6 +9651,23 @@ CK_RV ep11tok_is_mechanism_supported(STDLL_TokData_t *tokdata,
|
|
|
1f6f0c |
}
|
|
|
1f6f0c |
break;
|
|
|
1f6f0c |
|
|
|
1f6f0c |
+ case CKM_IBM_KYBER:
|
|
|
1f6f0c |
+ if (compare_ck_version(&ep11_data->ep11_lib_version, &ver4) < 0) {
|
|
|
1f6f0c |
+ TRACE_INFO("%s Mech '%s' banned due to host library version\n",
|
|
|
1f6f0c |
+ __func__, ep11_get_ckm(tokdata, type));
|
|
|
1f6f0c |
+ rc = CKR_MECHANISM_INVALID;
|
|
|
1f6f0c |
+ goto out;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ status = check_required_versions(tokdata, ibm_kyber_req_versions,
|
|
|
1f6f0c |
+ NUM_KYBER_REQ);
|
|
|
1f6f0c |
+ if (status != 1) {
|
|
|
1f6f0c |
+ TRACE_INFO("%s Mech '%s' banned due to mixed firmware versions\n",
|
|
|
1f6f0c |
+ __func__, ep11_get_ckm(tokdata, type));
|
|
|
1f6f0c |
+ rc = CKR_MECHANISM_INVALID;
|
|
|
1f6f0c |
+ goto out;
|
|
|
1f6f0c |
+ }
|
|
|
1f6f0c |
+ break;
|
|
|
1f6f0c |
+
|
|
|
1f6f0c |
case CKM_IBM_CPACF_WRAP:
|
|
|
1f6f0c |
if (compare_ck_version(&ep11_data->ep11_lib_version, &ver3) <= 0) {
|
|
|
1f6f0c |
TRACE_INFO("%s Mech '%s' banned due to host library version\n",
|
|
|
1f6f0c |
diff --git a/usr/lib/ep11_stdll/ep11cpfilter.conf b/usr/lib/ep11_stdll/ep11cpfilter.conf
|
|
|
1f6f0c |
index 9d6a2fc8..4353ec53 100644
|
|
|
1f6f0c |
--- a/usr/lib/ep11_stdll/ep11cpfilter.conf
|
|
|
1f6f0c |
+++ b/usr/lib/ep11_stdll/ep11cpfilter.conf
|
|
|
1f6f0c |
@@ -73,7 +73,7 @@ XCP_CPB_ALG_EC_25519: CKM_IBM_EC_X25519, CKM_IBM_ED25519_SHA512, CKM_IBM_EC_X448
|
|
|
1f6f0c |
XCP_CPB_ALG_NBSI2017: CKM_RSA_PKCS, CKM_SHA1_RSA_PKCS, CKM_SHA224_RSA_PKCS, CKM_SHA256_RSA_PKCS, CKM_SHA384_RSA_PKCS, CKM_SHA512_RSA_PKCS
|
|
|
1f6f0c |
|
|
|
1f6f0c |
#enable support of Dilithium
|
|
|
1f6f0c |
-XCP_CPB_ALG_PQC: CKM_IBM_DILITHIUM
|
|
|
1f6f0c |
+XCP_CPB_ALG_PQC: CKM_IBM_DILITHIUM, CKM_IBM_KYBER
|
|
|
1f6f0c |
|
|
|
1f6f0c |
# enable BTC-related functionality
|
|
|
1f6f0c |
XCP_CPB_BTC: CKM_IBM_BTC_DERIVE
|
|
|
1f6f0c |
--
|
|
|
1f6f0c |
2.16.2.windows.1
|
|
|
1f6f0c |
|