Blame SOURCES/0021-COMMON-EP11-Allow-to-select-Dilithium-variant-via-mo.patch

397a93
From 57cd8cd4db0d68c08b123b669f1cf57bed0fe34d Mon Sep 17 00:00:00 2001
397a93
From: Ingo Franzki <ifranzki@linux.ibm.com>
397a93
Date: Thu, 17 Feb 2022 10:27:56 +0100
397a93
Subject: [PATCH 21/34] COMMON/EP11: Allow to select Dilithium variant via mode
397a93
 or key form attribute
397a93
397a93
Attributes CKA_IBM_DILITHIUM_KEYFORM or CKA_IBM_DILITHIUM_MODE can
397a93
be used to select the Dilithium variant to use.
397a93
397a93
Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
397a93
---
397a93
 testcases/common/common.c          |   4 +-
397a93
 usr/lib/common/asn1.c              |  20 +-
397a93
 usr/lib/common/h_extern.h          |  11 +-
397a93
 usr/lib/common/key.c               | 443 +++++++++++++++++++++++--------------
397a93
 usr/lib/ep11_stdll/ep11_specific.c |  84 +++++--
397a93
 5 files changed, 375 insertions(+), 187 deletions(-)
397a93
397a93
diff --git a/testcases/common/common.c b/testcases/common/common.c
397a93
index 8ec5043b..fac9e4e6 100644
397a93
--- a/testcases/common/common.c
397a93
+++ b/testcases/common/common.c
397a93
@@ -629,7 +629,7 @@ CK_RV create_DilithiumPrivateKey(CK_SESSION_HANDLE session,
397a93
     CK_UTF8CHAR label[] = "A Dilithium private key object";
397a93
     CK_BYTE subject[] = {0};
397a93
     CK_BYTE id[] = { 123 };
397a93
-    CK_ULONG keyform = IBM_DILITHIUM_KEYFORM_ROUND2;
397a93
+    CK_ULONG keyform = CK_IBM_DILITHIUM_KEYFORM_ROUND2_65;
397a93
     CK_RV rc;
397a93
 
397a93
     CK_BBOOL true = TRUE;
397a93
@@ -678,7 +678,7 @@ CK_RV create_DilithiumPublicKey(CK_SESSION_HANDLE session,
397a93
     CK_KEY_TYPE keyType = CKK_IBM_PQC_DILITHIUM;
397a93
     CK_UTF8CHAR label[] = "A Dilithium public key object";
397a93
     CK_BBOOL true = TRUE;
397a93
-    CK_ULONG keyform = IBM_DILITHIUM_KEYFORM_ROUND2;
397a93
+    CK_ULONG keyform = CK_IBM_DILITHIUM_KEYFORM_ROUND2_65;
397a93
     CK_ATTRIBUTE template[] = {
397a93
         {CKA_CLASS, &class, sizeof(class)},
397a93
         {CKA_KEY_TYPE, &keyType, sizeof(keyType)},
397a93
diff --git a/usr/lib/common/asn1.c b/usr/lib/common/asn1.c
397a93
index dbf06dfd..85d3924c 100644
397a93
--- a/usr/lib/common/asn1.c
397a93
+++ b/usr/lib/common/asn1.c
397a93
@@ -3788,7 +3788,8 @@ CK_RV ber_decode_IBM_DilithiumPublicKey(CK_BYTE *data,
397a93
                                         CK_ULONG data_len,
397a93
                                         CK_ATTRIBUTE **rho_attr,
397a93
                                         CK_ATTRIBUTE **t1_attr,
397a93
-                                        CK_ATTRIBUTE **value_attr)
397a93
+                                        CK_ATTRIBUTE **value_attr,
397a93
+                                        const struct pqc_oid **oid)
397a93
 {
397a93
     CK_ATTRIBUTE *rho_attr_temp = NULL;
397a93
     CK_ATTRIBUTE *t1_attr_temp = NULL;
397a93
@@ -3818,8 +3819,8 @@ CK_RV ber_decode_IBM_DilithiumPublicKey(CK_BYTE *data,
397a93
        return rc;
397a93
     }
397a93
 
397a93
-    if (algoid_len != dilithium_r2_65_len ||
397a93
-        memcmp(algoid, dilithium_r2_65, dilithium_r2_65_len) != 0) {
397a93
+    *oid = find_pqc_by_oid(dilithium_oids, algoid, algoid_len);
397a93
+    if (*oid == NULL) {
397a93
         TRACE_ERROR("%s\n", ock_err(ERR_FUNCTION_FAILED));
397a93
         return CKR_FUNCTION_FAILED;
397a93
     }
397a93
@@ -4155,7 +4156,8 @@ CK_RV ber_decode_IBM_DilithiumPrivateKey(CK_BYTE *data,
397a93
                                          CK_ATTRIBUTE **s2,
397a93
                                          CK_ATTRIBUTE **t0,
397a93
                                          CK_ATTRIBUTE **t1,
397a93
-                                         CK_ATTRIBUTE **value)
397a93
+                                         CK_ATTRIBUTE **value,
397a93
+                                         const struct pqc_oid **oid)
397a93
 {
397a93
     CK_ATTRIBUTE *rho_attr = NULL, *seed_attr = NULL;
397a93
     CK_ATTRIBUTE *tr_attr = NULL, *s1_attr = NULL, *s2_attr = NULL;
397a93
@@ -4175,8 +4177,14 @@ CK_RV ber_decode_IBM_DilithiumPrivateKey(CK_BYTE *data,
397a93
         return rc;
397a93
     }
397a93
 
397a93
-    if (len != dilithium_r2_65_len + ber_NULLLen ||
397a93
-        memcmp(algoid, dilithium_r2_65, dilithium_r2_65_len) != 0) {
397a93
+    if (len <= ber_NULLLen ||
397a93
+        memcmp(algoid + len - ber_NULLLen, ber_NULL, ber_NULLLen) != 0) {
397a93
+        TRACE_ERROR("%s\n", ock_err(ERR_FUNCTION_FAILED));
397a93
+        return CKR_FUNCTION_FAILED;
397a93
+    }
397a93
+    len -= ber_NULLLen;
397a93
+    *oid = find_pqc_by_oid(dilithium_oids, algoid, len);
397a93
+    if (*oid == NULL) {
397a93
         TRACE_ERROR("%s\n", ock_err(ERR_FUNCTION_FAILED));
397a93
         return CKR_FUNCTION_FAILED;
397a93
     }
397a93
diff --git a/usr/lib/common/h_extern.h b/usr/lib/common/h_extern.h
397a93
index 53909e99..fdbcacd9 100644
397a93
--- a/usr/lib/common/h_extern.h
397a93
+++ b/usr/lib/common/h_extern.h
397a93
@@ -26,6 +26,7 @@
397a93
 #include <stdio.h>
397a93
 #include "dlist.h"
397a93
 #include "host_defs.h"
397a93
+#include "pqc_defs.h"
397a93
 
397a93
 #include <openssl/evp.h>
397a93
 
397a93
@@ -2504,6 +2505,10 @@ CK_RV ibm_dilithium_priv_unwrap(TEMPLATE *tmpl, CK_BYTE *data,
397a93
 CK_RV ibm_dilithium_priv_unwrap_get_data(TEMPLATE *tmpl,
397a93
                                          CK_BYTE *data, CK_ULONG total_length,
397a93
                                          CK_BBOOL add_value);
397a93
+const struct pqc_oid *ibm_pqc_get_keyform_mode(TEMPLATE *tmpl,
397a93
+                                               CK_MECHANISM_TYPE mech);
397a93
+CK_RV ibm_pqc_add_keyform_mode(TEMPLATE *tmpl, const struct pqc_oid *oid,
397a93
+                               CK_MECHANISM_TYPE mech);
397a93
 
397a93
 // diffie-hellman routines
397a93
 //
397a93
@@ -2750,7 +2755,8 @@ CK_RV ber_decode_IBM_DilithiumPublicKey(CK_BYTE *data,
397a93
                                         CK_ULONG data_len,
397a93
                                         CK_ATTRIBUTE **rho_attr,
397a93
                                         CK_ATTRIBUTE **t1_attr,
397a93
-                                        CK_ATTRIBUTE **value_attr);
397a93
+                                        CK_ATTRIBUTE **value_attr,
397a93
+                                        const struct pqc_oid **oid);
397a93
 
397a93
 CK_RV ber_encode_IBM_DilithiumPrivateKey(CK_BBOOL length_only,
397a93
                                          CK_BYTE **data,
397a93
@@ -2773,7 +2779,8 @@ CK_RV ber_decode_IBM_DilithiumPrivateKey(CK_BYTE *data,
397a93
                                          CK_ATTRIBUTE **s2,
397a93
                                          CK_ATTRIBUTE **t0,
397a93
                                          CK_ATTRIBUTE **t1,
397a93
-                                         CK_ATTRIBUTE **value);
397a93
+                                         CK_ATTRIBUTE **value,
397a93
+                                         const struct pqc_oid **oid);
397a93
 
397a93
 typedef CK_RV (*t_rsa_encrypt)(STDLL_TokData_t *, CK_BYTE *in_data,
397a93
                                CK_ULONG in_data_len, CK_BYTE *out_data,
397a93
diff --git a/usr/lib/common/key.c b/usr/lib/common/key.c
397a93
index b0050816..ba40cefd 100644
397a93
--- a/usr/lib/common/key.c
397a93
+++ b/usr/lib/common/key.c
397a93
@@ -2654,6 +2654,117 @@ error:
397a93
     return rc;
397a93
 }
397a93
 
397a93
+static CK_RV ibm_pqc_keyform_mode_attrs_by_mech(CK_MECHANISM_TYPE mech,
397a93
+                                                CK_ATTRIBUTE_TYPE *keyform_attr,
397a93
+                                                CK_ATTRIBUTE_TYPE *mode_attr,
397a93
+                                                const struct pqc_oid **oids)
397a93
+{
397a93
+    switch (mech) {
397a93
+    case CKM_IBM_DILITHIUM:
397a93
+        *keyform_attr = CKA_IBM_DILITHIUM_KEYFORM;
397a93
+        *mode_attr = CKA_IBM_DILITHIUM_MODE;
397a93
+        *oids = dilithium_oids;
397a93
+        break;
397a93
+    case CKM_IBM_KYBER:
397a93
+        *keyform_attr = CKA_IBM_KYBER_KEYFORM;
397a93
+        *mode_attr = CKA_IBM_KYBER_MODE;
397a93
+        *oids = kyber_oids;
397a93
+        break;
397a93
+    default:
397a93
+        TRACE_ERROR("Unsupported mechanims: 0x%lx\n", mech);
397a93
+        return CKR_MECHANISM_INVALID;
397a93
+    }
397a93
+
397a93
+    return CKR_OK;
397a93
+}
397a93
+
397a93
+const struct pqc_oid *ibm_pqc_get_keyform_mode(TEMPLATE *tmpl,
397a93
+                                               CK_MECHANISM_TYPE mech)
397a93
+{
397a93
+    CK_ATTRIBUTE *attr = NULL;
397a93
+    const struct pqc_oid *oids, *oid;
397a93
+    CK_ATTRIBUTE_TYPE keyform_attr;
397a93
+    CK_ATTRIBUTE_TYPE mode_attr;
397a93
+
397a93
+    if (ibm_pqc_keyform_mode_attrs_by_mech(mech, &keyform_attr,
397a93
+                                           &mode_attr, &oids) != CKR_OK)
397a93
+        return NULL;
397a93
+
397a93
+    if (template_attribute_find(tmpl, keyform_attr, &attr) &&
397a93
+        attr->ulValueLen == sizeof(CK_ULONG) && attr->pValue != NULL) {
397a93
+        oid = find_pqc_by_keyform(oids, *(CK_ULONG *)(attr->pValue));
397a93
+        if (oid == NULL) {
397a93
+            TRACE_ERROR("KEYFORM attribute specifies an invalid value: %lu\n",
397a93
+                        *(CK_ULONG *)(attr->pValue));
397a93
+            return NULL;
397a93
+        }
397a93
+        return oid;
397a93
+    }
397a93
+
397a93
+    if (template_attribute_find(tmpl, mode_attr, &attr) &&
397a93
+        attr->ulValueLen != 0 && attr->pValue != NULL) {
397a93
+        oid = find_pqc_by_oid(oids, attr->pValue, attr->ulValueLen);
397a93
+        if (oid == NULL) {
397a93
+            TRACE_ERROR("MODE attribute specifies an invalid value\n");
397a93
+            return NULL;
397a93
+        }
397a93
+        return oid;
397a93
+    }
397a93
+
397a93
+    TRACE_ERROR("Neither KEYFORM nor MODE found\n");
397a93
+    return NULL;
397a93
+}
397a93
+
397a93
+CK_RV ibm_pqc_add_keyform_mode(TEMPLATE *tmpl, const struct pqc_oid *oid,
397a93
+                               CK_MECHANISM_TYPE mech)
397a93
+{
397a93
+    CK_ATTRIBUTE *mode = NULL;
397a93
+    CK_ATTRIBUTE *keyform = NULL;
397a93
+    CK_RV rc;
397a93
+    CK_ATTRIBUTE_TYPE keyform_attr;
397a93
+    CK_ATTRIBUTE_TYPE mode_attr;
397a93
+    const struct pqc_oid *oids;
397a93
+
397a93
+    if (ibm_pqc_keyform_mode_attrs_by_mech(mech, &keyform_attr,
397a93
+                                           &mode_attr, &oids) != CKR_OK)
397a93
+        return CKR_MECHANISM_INVALID;
397a93
+
397a93
+    rc = build_attribute(mode_attr, (CK_BYTE *)oid->oid, oid->oid_len, &mode);
397a93
+    if (rc != CKR_OK) {
397a93
+        TRACE_DEVEL("build_attribute failed\n");
397a93
+        goto error;
397a93
+    }
397a93
+    rc = template_update_attribute(tmpl, mode);
397a93
+    if (rc != CKR_OK) {
397a93
+        TRACE_DEVEL("template_update_attribute failed.\n");
397a93
+        goto error;
397a93
+    }
397a93
+    mode = NULL;
397a93
+
397a93
+    rc = build_attribute(keyform_attr, (CK_BYTE *)&oid->keyform,
397a93
+                         sizeof(CK_ULONG), &keyform);
397a93
+    if (rc != CKR_OK) {
397a93
+        TRACE_DEVEL("build_attribute failed\n");
397a93
+        goto error;
397a93
+    }
397a93
+    rc = template_update_attribute(tmpl, keyform);
397a93
+    if (rc != CKR_OK) {
397a93
+        TRACE_DEVEL("template_update_attribute failed.\n");
397a93
+        goto error;
397a93
+    }
397a93
+    keyform = NULL;
397a93
+
397a93
+    return CKR_OK;
397a93
+
397a93
+error:
397a93
+    if (mode)
397a93
+        free(mode);
397a93
+    if (keyform)
397a93
+        free(keyform);
397a93
+
397a93
+    return rc;
397a93
+}
397a93
+
397a93
 /*
397a93
  * Extract the SubjectPublicKeyInfo from the Dilithium public key
397a93
  */
397a93
@@ -2662,21 +2773,12 @@ CK_RV ibm_dilithium_publ_get_spki(TEMPLATE *tmpl, CK_BBOOL length_only,
397a93
 {
397a93
     CK_ATTRIBUTE *rho = NULL;
397a93
     CK_ATTRIBUTE *t1 = NULL;
397a93
-    CK_ULONG keyform;
397a93
+    const struct pqc_oid *oid;
397a93
     CK_RV rc;
397a93
 
397a93
-    rc = template_attribute_get_ulong(tmpl, CKA_IBM_DILITHIUM_KEYFORM,
397a93
-                                      &keyform);
397a93
-    if (rc != CKR_OK) {
397a93
-        TRACE_ERROR("Could not find CKA_IBM_DILITHIUM_KEYFORM for the key.\n");
397a93
-        return rc;
397a93
-    }
397a93
-
397a93
-    if ( keyform != IBM_DILITHIUM_KEYFORM_ROUND2) {
397a93
-        TRACE_ERROR("This key has an unexpected CKA_IBM_DILITHIUM_KEYFORM: "
397a93
-                    "%ld \n", keyform);
397a93
-        return CKR_ATTRIBUTE_VALUE_INVALID;
397a93
-    }
397a93
+    oid = ibm_pqc_get_keyform_mode(tmpl, CKM_IBM_DILITHIUM);
397a93
+    if (oid == NULL)
397a93
+       return CKR_TEMPLATE_INCOMPLETE;
397a93
 
397a93
     rc = template_attribute_get_non_empty(tmpl, CKA_IBM_DILITHIUM_RHO, &rho);
397a93
     if (rc != CKR_OK) {
397a93
@@ -2690,8 +2792,7 @@ CK_RV ibm_dilithium_publ_get_spki(TEMPLATE *tmpl, CK_BBOOL length_only,
397a93
     }
397a93
 
397a93
     rc = ber_encode_IBM_DilithiumPublicKey(length_only, data, data_len,
397a93
-                                           dilithium_r2_65,
397a93
-                                           dilithium_r2_65_len,
397a93
+                                           oid->oid, oid->oid_len,
397a93
                                            rho, t1);
397a93
     if (rc != CKR_OK) {
397a93
         TRACE_ERROR("ber_encode_IBM_DilithiumPublicKey failed.\n");
397a93
@@ -2709,23 +2810,12 @@ CK_RV ibm_dilithium_priv_wrap_get_data(TEMPLATE *tmpl,
397a93
     CK_ATTRIBUTE *rho = NULL, *seed = NULL;
397a93
     CK_ATTRIBUTE *tr = NULL, *s1 = NULL, *s2 = NULL;
397a93
     CK_ATTRIBUTE *t0 = NULL, *t1 = NULL;
397a93
-    CK_ULONG keyform;
397a93
+    const struct pqc_oid *oid;
397a93
     CK_RV rc;
397a93
 
397a93
-    /* A private Dilithium key must have a keyform value */
397a93
-    rc = template_attribute_get_ulong(tmpl, CKA_IBM_DILITHIUM_KEYFORM,
397a93
-                                      &keyform);
397a93
-    if (rc != CKR_OK) {
397a93
-        TRACE_ERROR("Could not find CKA_IBM_DILITHIUM_KEYFORM for the key.\n");
397a93
-        return rc;
397a93
-    }
397a93
-
397a93
-    /* Check if it's an expected keyform */
397a93
-    if (keyform != IBM_DILITHIUM_KEYFORM_ROUND2) {
397a93
-        TRACE_ERROR("This key has an unexpected CKA_IBM_DILITHIUM_KEYFORM: %ld\n",
397a93
-                    keyform);
397a93
-        return CKR_ATTRIBUTE_VALUE_INVALID;
397a93
-    }
397a93
+    oid = ibm_pqc_get_keyform_mode(tmpl, CKM_IBM_DILITHIUM);
397a93
+    if (oid == NULL)
397a93
+       return CKR_TEMPLATE_INCOMPLETE;
397a93
 
397a93
     rc = template_attribute_get_non_empty(tmpl, CKA_IBM_DILITHIUM_RHO, &rho);
397a93
     if (rc != CKR_OK) {
397a93
@@ -2770,8 +2860,7 @@ CK_RV ibm_dilithium_priv_wrap_get_data(TEMPLATE *tmpl,
397a93
     }
397a93
 
397a93
     rc = ber_encode_IBM_DilithiumPrivateKey(length_only, data, data_len,
397a93
-                                            dilithium_r2_65,
397a93
-                                            dilithium_r2_65_len,
397a93
+                                            oid->oid, oid->oid_len,
397a93
                                             rho, seed, tr, s1, s2, t0, t1);
397a93
     if (rc != CKR_OK) {
397a93
         TRACE_DEVEL("ber_encode_IBM_DilithiumPrivateKey failed\n");
397a93
@@ -2787,15 +2876,22 @@ CK_RV ibm_dilithium_priv_unwrap_get_data(TEMPLATE *tmpl, CK_BYTE *data,
397a93
     CK_ATTRIBUTE *rho = NULL;
397a93
     CK_ATTRIBUTE *t1 = NULL;
397a93
     CK_ATTRIBUTE *value = NULL;
397a93
+    const struct pqc_oid *oid;
397a93
     CK_RV rc;
397a93
 
397a93
     rc = ber_decode_IBM_DilithiumPublicKey(data, total_length, &rho, &t1,
397a93
-                                           &value);
397a93
+                                           &value, &oid;;
397a93
     if (rc != CKR_OK) {
397a93
         TRACE_ERROR("ber_decode_DilithiumPublicKey failed\n");
397a93
         return rc;
397a93
     }
397a93
 
397a93
+    rc = ibm_pqc_add_keyform_mode(tmpl, oid, CKM_IBM_DILITHIUM);
397a93
+    if (rc != CKR_OK) {
397a93
+        TRACE_ERROR("ibm_pqc_add_keyform_mode failed\n");
397a93
+        return rc;
397a93
+    }
397a93
+
397a93
     rc = template_update_attribute(tmpl, rho);
397a93
     if (rc != CKR_OK) {
397a93
         TRACE_DEVEL("template_update_attribute failed.\n");
397a93
@@ -2839,16 +2935,23 @@ CK_RV ibm_dilithium_priv_unwrap(TEMPLATE *tmpl, CK_BYTE *data,
397a93
 {
397a93
     CK_ATTRIBUTE *rho = NULL, *seed = NULL, *tr = NULL, *value = NULL;
397a93
     CK_ATTRIBUTE *s1 = NULL, *s2 = NULL, *t0 = NULL, *t1 = NULL;
397a93
+    const struct pqc_oid *oid;
397a93
     CK_RV rc;
397a93
 
397a93
     rc = ber_decode_IBM_DilithiumPrivateKey(data, total_length,
397a93
                                             &rho, &seed, &tr, &s1, &s2, &t0,
397a93
-                                            &t1, &value);
397a93
+                                            &t1, &value, &oid;;
397a93
     if (rc != CKR_OK) {
397a93
         TRACE_ERROR("der_decode_IBM_DilithiumPrivateKey failed\n");
397a93
         return rc;
397a93
     }
397a93
 
397a93
+    rc = ibm_pqc_add_keyform_mode(tmpl, oid, CKM_IBM_DILITHIUM);
397a93
+    if (rc != CKR_OK) {
397a93
+        TRACE_ERROR("ibm_pqc_add_keyform_mode failed\n");
397a93
+        return rc;
397a93
+    }
397a93
+
397a93
     rc = template_update_attribute(tmpl, rho);
397a93
     if (rc != CKR_OK) {
397a93
         TRACE_ERROR("template_update_attribute failed\n");
397a93
@@ -4660,19 +4763,17 @@ CK_RV ibm_dilithium_publ_set_default_attributes(TEMPLATE *tmpl, CK_ULONG mode)
397a93
     CK_ATTRIBUTE *type_attr = NULL;
397a93
     CK_ATTRIBUTE *rho_attr = NULL;
397a93
     CK_ATTRIBUTE *t1_attr = NULL;
397a93
-    CK_ATTRIBUTE *keyform_attr = NULL;
397a93
     CK_ATTRIBUTE *value_attr = NULL;
397a93
     CK_RV rc;
397a93
 
397a93
     publ_key_set_default_attributes(tmpl, mode);
397a93
 
397a93
     type_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE) + sizeof(CK_KEY_TYPE));
397a93
-    keyform_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE) + sizeof(CK_ULONG));
397a93
     rho_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE));
397a93
     t1_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE));
397a93
     value_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE));
397a93
 
397a93
-    if (!type_attr || !rho_attr || !t1_attr || !keyform_attr || !value_attr) {
397a93
+    if (!type_attr || !rho_attr || !t1_attr || !value_attr) {
397a93
         TRACE_ERROR("%s\n", ock_err(ERR_HOST_MEMORY));
397a93
         rc = CKR_HOST_MEMORY;
397a93
         goto error;
397a93
@@ -4683,11 +4784,6 @@ CK_RV ibm_dilithium_publ_set_default_attributes(TEMPLATE *tmpl, CK_ULONG mode)
397a93
     type_attr->pValue = (CK_BYTE *) type_attr + sizeof(CK_ATTRIBUTE);
397a93
     *(CK_KEY_TYPE *) type_attr->pValue = CKK_IBM_PQC_DILITHIUM;
397a93
 
397a93
-    keyform_attr->type = CKA_IBM_DILITHIUM_KEYFORM;
397a93
-    keyform_attr->ulValueLen = sizeof(CK_ULONG);
397a93
-    keyform_attr->pValue = (CK_BYTE *) keyform_attr + sizeof(CK_ATTRIBUTE);
397a93
-    *(CK_ULONG *) keyform_attr->pValue = IBM_DILITHIUM_KEYFORM_ROUND2;
397a93
-
397a93
     rho_attr->type = CKA_IBM_DILITHIUM_RHO;
397a93
     rho_attr->ulValueLen = 0;
397a93
     rho_attr->pValue = NULL;
397a93
@@ -4718,12 +4814,6 @@ CK_RV ibm_dilithium_publ_set_default_attributes(TEMPLATE *tmpl, CK_ULONG mode)
397a93
         goto error;
397a93
     }
397a93
     t1_attr = NULL;
397a93
-    rc = template_update_attribute(tmpl, keyform_attr);
397a93
-    if (rc != CKR_OK) {
397a93
-        TRACE_ERROR("template_update_attribute failed\n");
397a93
-        goto error;
397a93
-    }
397a93
-    keyform_attr = NULL;
397a93
     rc = template_update_attribute(tmpl, value_attr);
397a93
     if (rc != CKR_OK) {
397a93
         TRACE_ERROR("template_update_attribute failed\n");
397a93
@@ -4740,8 +4830,6 @@ error:
397a93
         free(rho_attr);
397a93
     if (t1_attr)
397a93
         free(t1_attr);
397a93
-    if (keyform_attr)
397a93
-        free(keyform_attr);
397a93
     if (value_attr)
397a93
         free(value_attr);
397a93
 
397a93
@@ -4760,14 +4848,12 @@ CK_RV ibm_dilithium_priv_set_default_attributes(TEMPLATE *tmpl, CK_ULONG mode)
397a93
     CK_ATTRIBUTE *s2_attr = NULL;
397a93
     CK_ATTRIBUTE *t0_attr = NULL;
397a93
     CK_ATTRIBUTE *t1_attr = NULL;
397a93
-    CK_ATTRIBUTE *keyform_attr = NULL;
397a93
     CK_ATTRIBUTE *value_attr = NULL;
397a93
     CK_RV rc;
397a93
 
397a93
     priv_key_set_default_attributes(tmpl, mode);
397a93
 
397a93
     type_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE) + sizeof(CK_KEY_TYPE));
397a93
-    keyform_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE) + sizeof(CK_ULONG));
397a93
     rho_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE));
397a93
     seed_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE));
397a93
     tr_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE));
397a93
@@ -4778,7 +4864,7 @@ CK_RV ibm_dilithium_priv_set_default_attributes(TEMPLATE *tmpl, CK_ULONG mode)
397a93
     value_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE));
397a93
 
397a93
     if (!type_attr || !rho_attr || !seed_attr || !tr_attr || !s1_attr
397a93
-        || !s2_attr || !t0_attr || !t1_attr || !keyform_attr || !value_attr) {
397a93
+        || !s2_attr || !t0_attr || !t1_attr || !value_attr) {
397a93
         TRACE_ERROR("%s\n", ock_err(ERR_HOST_MEMORY));
397a93
         rc = CKR_HOST_MEMORY;
397a93
         goto error;
397a93
@@ -4789,11 +4875,6 @@ CK_RV ibm_dilithium_priv_set_default_attributes(TEMPLATE *tmpl, CK_ULONG mode)
397a93
     type_attr->pValue = (CK_BYTE *) type_attr + sizeof(CK_ATTRIBUTE);
397a93
     *(CK_KEY_TYPE *) type_attr->pValue = CKK_IBM_PQC_DILITHIUM;
397a93
 
397a93
-    keyform_attr->type = CKA_IBM_DILITHIUM_KEYFORM;
397a93
-    keyform_attr->ulValueLen = sizeof(CK_ULONG);
397a93
-    keyform_attr->pValue = (CK_BYTE *) keyform_attr + sizeof(CK_ATTRIBUTE);
397a93
-    *(CK_ULONG *) keyform_attr->pValue = IBM_DILITHIUM_KEYFORM_ROUND2;
397a93
-
397a93
     rho_attr->type = CKA_IBM_DILITHIUM_RHO;
397a93
     rho_attr->ulValueLen = 0;
397a93
     rho_attr->pValue = NULL;
397a93
@@ -4832,12 +4913,6 @@ CK_RV ibm_dilithium_priv_set_default_attributes(TEMPLATE *tmpl, CK_ULONG mode)
397a93
         goto error;
397a93
     }
397a93
     type_attr = NULL;
397a93
-    rc = template_update_attribute(tmpl, keyform_attr);
397a93
-    if (rc != CKR_OK) {
397a93
-        TRACE_ERROR("template_update_attribute failed\n");
397a93
-        goto error;
397a93
-    }
397a93
-    keyform_attr = NULL;
397a93
     rc = template_update_attribute(tmpl, rho_attr);
397a93
     if (rc != CKR_OK) {
397a93
         TRACE_ERROR("template_update_attribute failed\n");
397a93
@@ -4906,38 +4981,59 @@ error:
397a93
         free(t0_attr);
397a93
     if (t1_attr)
397a93
         free(t1_attr);
397a93
-    if (keyform_attr)
397a93
-        free(keyform_attr);
397a93
     if (value_attr)
397a93
         free(value_attr);
397a93
 
397a93
     return rc;
397a93
 }
397a93
 
397a93
-// ibm_dilithium_publ_check_required_attributes()
397a93
-//
397a93
-CK_RV ibm_dilithium_publ_check_required_attributes(TEMPLATE *tmpl, CK_ULONG mode)
397a93
+static CK_RV ibm_pqc_check_attributes(TEMPLATE *tmpl, CK_ULONG mode,
397a93
+                                      CK_MECHANISM_TYPE mech,
397a93
+                                      CK_ULONG *req_attrs,
397a93
+                                      CK_ULONG num_req_attrs)
397a93
 {
397a93
+    CK_ATTRIBUTE_TYPE keyform_attr;
397a93
+    CK_ATTRIBUTE_TYPE mode_attr;
397a93
     CK_ATTRIBUTE *attr = NULL;
397a93
-    static CK_ULONG req_attrs[] = {
397a93
-        CKA_IBM_DILITHIUM_KEYFORM,
397a93
-        CKA_IBM_DILITHIUM_RHO,
397a93
-        CKA_IBM_DILITHIUM_T1,
397a93
-    };
397a93
+    CK_BBOOL keyform_present = FALSE;
397a93
+    CK_BBOOL mode_present = FALSE;
397a93
+    const struct pqc_oid *oids, *oid;
397a93
     CK_ULONG i;
397a93
     CK_RV rc;
397a93
 
397a93
+    if (ibm_pqc_keyform_mode_attrs_by_mech(mech, &keyform_attr,
397a93
+                                           &mode_attr, &oids) != CKR_OK)
397a93
+        return CKR_MECHANISM_INVALID;
397a93
+
397a93
+    if (template_attribute_find(tmpl, keyform_attr, &attr) &&
397a93
+        attr->ulValueLen == sizeof(CK_ULONG) && attr->pValue != NULL) {
397a93
+        oid = find_pqc_by_keyform(oids, *(CK_ULONG *)(attr->pValue));
397a93
+        if (oid == NULL) {
397a93
+            TRACE_ERROR("%s, attribute KEYFORM has an unsupported value.\n",
397a93
+                        ock_err(ERR_ATTRIBUTE_VALUE_INVALID));
397a93
+            return CKR_ATTRIBUTE_VALUE_INVALID;
397a93
+        }
397a93
+        keyform_present = TRUE;
397a93
+    }
397a93
+
397a93
+    if (template_attribute_find(tmpl, mode_attr, &attr) &&
397a93
+        attr->ulValueLen > 0 && attr->pValue != NULL) {
397a93
+        oid = find_pqc_by_oid(oids, attr->pValue, attr->ulValueLen);
397a93
+        if (oid == NULL) {
397a93
+            TRACE_ERROR("%s, attribute MODE has an unsupported value.\n",
397a93
+                        ock_err(ERR_ATTRIBUTE_VALUE_INVALID));
397a93
+            return CKR_ATTRIBUTE_VALUE_INVALID;
397a93
+        }
397a93
+        mode_present = TRUE;
397a93
+    }
397a93
+
397a93
     switch (mode) {
397a93
-    case MODE_KEYGEN:
397a93
-    case MODE_UNWRAP:
397a93
-        /* Attrs will be added during keygen/unwrap */
397a93
-        break;
397a93
     case MODE_CREATE:
397a93
         /* Either CKA_VALUE or all other attrs must be present */
397a93
         if (template_attribute_find(tmpl, CKA_VALUE, &attr) &&
397a93
             attr->ulValueLen > 0 && attr->pValue != NULL)
397a93
             break;
397a93
-        for (i = 0; i < sizeof(req_attrs) / sizeof(req_attrs[0]); i++) {
397a93
+        for (i = 0; i < num_req_attrs; i++) {
397a93
             rc = template_attribute_get_non_empty(tmpl, req_attrs[i], &attr);
397a93
             if (rc != CKR_OK) {
397a93
                 if (rc != CKR_ATTRIBUTE_VALUE_INVALID)
397a93
@@ -4946,27 +5042,58 @@ CK_RV ibm_dilithium_publ_check_required_attributes(TEMPLATE *tmpl, CK_ULONG mode
397a93
                 return rc;
397a93
             }
397a93
         }
397a93
+        /* fallthrough */
397a93
+    case MODE_KEYGEN:
397a93
+        /* Either keyform or mode or none of it must be present */
397a93
+        if (keyform_present && mode_present) {
397a93
+            TRACE_ERROR("%s, only one of KEYFORM or MODE can be specified .\n",
397a93
+                        ock_err(ERR_TEMPLATE_INCONSISTENT));
397a93
+            return CKR_TEMPLATE_INCONSISTENT;
397a93
+        }
397a93
+        break;
397a93
+    case MODE_UNWRAP:
397a93
+        /* neither keyform or mode must be present */
397a93
+        if (keyform_present || mode_present) {
397a93
+            TRACE_ERROR("%s, none of KEYFORM or MODE can be specified .\n",
397a93
+                        ock_err(ERR_TEMPLATE_INCONSISTENT));
397a93
+            return CKR_TEMPLATE_INCONSISTENT;
397a93
+        }
397a93
         break;
397a93
     case MODE_COPY:
397a93
-        /* CKA_VALUE and all other attrs must be present */
397a93
-        if (!template_attribute_find(tmpl, CKA_VALUE, &attr) &&
397a93
-            attr->ulValueLen > 0 && attr->pValue != NULL) {
397a93
-            TRACE_ERROR("%s, attribute CKA_VALUE missing.\n",
397a93
+        /* All attributes must be present */
397a93
+        if (!keyform_present || !mode_present) {
397a93
+            TRACE_ERROR("%s, KEYFORM or MODE must be specified .\n",
397a93
                         ock_err(ERR_TEMPLATE_INCOMPLETE));
397a93
             return CKR_TEMPLATE_INCOMPLETE;
397a93
         }
397a93
-        for (i = 0; i < sizeof(req_attrs) / sizeof(req_attrs[0]); i++) {
397a93
-            rc = template_attribute_get_non_empty(tmpl, req_attrs[i], &attr);
397a93
-            if (rc != CKR_OK) {
397a93
-                if (rc != CKR_ATTRIBUTE_VALUE_INVALID)
397a93
-                    TRACE_ERROR("%s, attribute %08lX missing.\n",
397a93
-                               ock_err(ERR_TEMPLATE_INCOMPLETE), req_attrs[i]);
397a93
-                return rc;
397a93
+        for (i = 0; i < num_req_attrs; i++) {
397a93
+            if (!template_attribute_find(tmpl, req_attrs[i], &attr)) {
397a93
+                TRACE_ERROR("%s, attribute %08lX missing.\n",
397a93
+                            ock_err(ERR_TEMPLATE_INCOMPLETE), req_attrs[i]);
397a93
+                return CKR_TEMPLATE_INCOMPLETE;
397a93
             }
397a93
         }
397a93
         break;
397a93
     }
397a93
 
397a93
+    return CKR_OK;
397a93
+}
397a93
+
397a93
+// ibm_dilithium_publ_check_required_attributes()
397a93
+//
397a93
+CK_RV ibm_dilithium_publ_check_required_attributes(TEMPLATE *tmpl, CK_ULONG mode)
397a93
+{
397a93
+    static CK_ULONG req_attrs[] = {
397a93
+        CKA_IBM_DILITHIUM_RHO,
397a93
+        CKA_IBM_DILITHIUM_T1,
397a93
+    };
397a93
+    CK_RV rc;
397a93
+
397a93
+    rc = ibm_pqc_check_attributes(tmpl, mode, CKM_IBM_DILITHIUM, req_attrs,
397a93
+                                  sizeof(req_attrs) / sizeof(req_attrs[0]));
397a93
+    if (rc != CKR_OK)
397a93
+        return rc;
397a93
+
397a93
     /* All required attrs found, check them */
397a93
     return publ_key_check_required_attributes(tmpl, mode);
397a93
 }
397a93
@@ -4975,9 +5102,7 @@ CK_RV ibm_dilithium_publ_check_required_attributes(TEMPLATE *tmpl, CK_ULONG mode
397a93
 //
397a93
 CK_RV ibm_dilithium_priv_check_required_attributes(TEMPLATE *tmpl, CK_ULONG mode)
397a93
 {
397a93
-    CK_ATTRIBUTE *attr = NULL;
397a93
     static CK_ULONG req_attrs[] = {
397a93
-        CKA_IBM_DILITHIUM_KEYFORM,
397a93
         CKA_IBM_DILITHIUM_RHO,
397a93
         CKA_IBM_DILITHIUM_SEED,
397a93
         CKA_IBM_DILITHIUM_TR,
397a93
@@ -4986,52 +5111,62 @@ CK_RV ibm_dilithium_priv_check_required_attributes(TEMPLATE *tmpl, CK_ULONG mode
397a93
         CKA_IBM_DILITHIUM_T0,
397a93
         CKA_IBM_DILITHIUM_T1,
397a93
     };
397a93
-    CK_ULONG i;
397a93
     CK_RV rc;
397a93
 
397a93
-    switch (mode) {
397a93
-    case MODE_KEYGEN:
397a93
-    case MODE_UNWRAP:
397a93
-        /* Attrs will be added during keygen/unwrap */
397a93
-        break;
397a93
-    case MODE_CREATE:
397a93
-        /* Either CKA_VALUE or all other attrs must be present */
397a93
-        if (template_attribute_find(tmpl, CKA_VALUE, &attr) &&
397a93
-            attr->ulValueLen > 0 && attr->pValue != NULL)
397a93
-            break;
397a93
-        for (i = 0; i < sizeof(req_attrs) / sizeof(req_attrs[0]); i++) {
397a93
-            rc = template_attribute_get_non_empty(tmpl, req_attrs[i], &attr);
397a93
-            if (rc != CKR_OK) {
397a93
-                if (rc != CKR_ATTRIBUTE_VALUE_INVALID)
397a93
-                    TRACE_ERROR("%s, attribute %08lX missing.\n",
397a93
-                               ock_err(ERR_TEMPLATE_INCOMPLETE), req_attrs[i]);
397a93
-                return rc;
397a93
-            }
397a93
-        }
397a93
-        break;
397a93
-    case MODE_COPY:
397a93
-        /* CKA_VALUE and all other attrs must be present */
397a93
-        if (!template_attribute_find(tmpl, CKA_VALUE, &attr) &&
397a93
-            attr->ulValueLen > 0 && attr->pValue != NULL) {
397a93
-            TRACE_ERROR("%s, attribute CKA_VALUE missing.\n",
397a93
-                        ock_err(ERR_TEMPLATE_INCOMPLETE));
397a93
-            return CKR_TEMPLATE_INCOMPLETE;
397a93
+    rc = ibm_pqc_check_attributes(tmpl, mode, CKM_IBM_DILITHIUM, req_attrs,
397a93
+                                  sizeof(req_attrs) / sizeof(req_attrs[0]));
397a93
+    if (rc != CKR_OK)
397a93
+        return rc;
397a93
+
397a93
+    /* All required attrs found, check them */
397a93
+    return priv_key_check_required_attributes(tmpl, mode);
397a93
+}
397a93
+
397a93
+static CK_RV ibm_pqc_validate_keyform_mode(CK_ATTRIBUTE *attr, CK_ULONG mode,
397a93
+                                           CK_MECHANISM_TYPE mech)
397a93
+{
397a93
+    CK_ATTRIBUTE_TYPE keyform_attr;
397a93
+    CK_ATTRIBUTE_TYPE mode_attr;
397a93
+    const struct pqc_oid *oids, *oid;
397a93
 
397a93
+    if (ibm_pqc_keyform_mode_attrs_by_mech(mech, &keyform_attr,
397a93
+                                           &mode_attr, &oids) != CKR_OK)
397a93
+        return CKR_MECHANISM_INVALID;
397a93
+
397a93
+    if (attr->type == keyform_attr) {
397a93
+        if (mode == MODE_CREATE || mode == MODE_KEYGEN) {
397a93
+            if (attr->ulValueLen != sizeof(CK_ULONG) || attr->pValue == NULL) {
397a93
+                TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID));
397a93
+                return CKR_ATTRIBUTE_VALUE_INVALID;
397a93
+            }
397a93
+            oid = find_pqc_by_keyform(oids, *((CK_ULONG *)attr->pValue));
397a93
+            if (oid == NULL) {
397a93
+                TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID));
397a93
+                return CKR_ATTRIBUTE_VALUE_INVALID;
397a93
+            }
397a93
+            return CKR_OK;
397a93
         }
397a93
-        for (i = 0; i < sizeof(req_attrs) / sizeof(req_attrs[0]); i++) {
397a93
-            rc = template_attribute_get_non_empty(tmpl, req_attrs[i], &attr);
397a93
-            if (rc != CKR_OK) {
397a93
-                if (rc != CKR_ATTRIBUTE_VALUE_INVALID)
397a93
-                    TRACE_ERROR("%s, attribute %08lX missing.\n",
397a93
-                               ock_err(ERR_TEMPLATE_INCOMPLETE), req_attrs[i]);
397a93
-                return rc;
397a93
+        TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_READ_ONLY));
397a93
+        return CKR_ATTRIBUTE_READ_ONLY;
397a93
+    }
397a93
+    if (attr->type == mode_attr) {
397a93
+        if (mode == MODE_CREATE || mode == MODE_KEYGEN) {
397a93
+            if (attr->ulValueLen == 0 || attr->pValue == NULL) {
397a93
+                TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID));
397a93
+                return CKR_ATTRIBUTE_VALUE_INVALID;
397a93
+            }
397a93
+            oid = find_pqc_by_oid(oids, attr->pValue, attr->ulValueLen);
397a93
+            if (oid == NULL) {
397a93
+                TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID));
397a93
+                return CKR_ATTRIBUTE_VALUE_INVALID;
397a93
             }
397a93
+            return CKR_OK;
397a93
         }
397a93
-        break;
397a93
+        TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_READ_ONLY));
397a93
+        return CKR_ATTRIBUTE_READ_ONLY;
397a93
     }
397a93
 
397a93
-    /* All required attrs found, check them */
397a93
-    return priv_key_check_required_attributes(tmpl, mode);
397a93
+    return CKR_OK;
397a93
 }
397a93
 
397a93
 // ibm_dilithium_publ_validate_attribute()
397a93
@@ -5040,7 +5175,15 @@ CK_RV ibm_dilithium_publ_validate_attribute(STDLL_TokData_t *tokdata,
397a93
                                             TEMPLATE *tmpl, CK_ATTRIBUTE *attr,
397a93
                                             CK_ULONG mode)
397a93
 {
397a93
+    CK_RV rc;
397a93
+
397a93
     switch (attr->type) {
397a93
+    case CKA_IBM_DILITHIUM_KEYFORM:
397a93
+    case CKA_IBM_DILITHIUM_MODE:
397a93
+        rc = ibm_pqc_validate_keyform_mode(attr, mode, CKM_IBM_DILITHIUM);
397a93
+        if (rc != CKR_OK)
397a93
+            return rc;
397a93
+        return CKR_OK;
397a93
     case CKA_IBM_DILITHIUM_RHO:
397a93
     case CKA_IBM_DILITHIUM_T1:
397a93
     case CKA_VALUE:
397a93
@@ -5048,22 +5191,6 @@ CK_RV ibm_dilithium_publ_validate_attribute(STDLL_TokData_t *tokdata,
397a93
             return CKR_OK;
397a93
         TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_READ_ONLY));
397a93
         return CKR_ATTRIBUTE_READ_ONLY;
397a93
-    case CKA_IBM_DILITHIUM_KEYFORM:
397a93
-        if (mode == MODE_CREATE || mode == MODE_KEYGEN) {
397a93
-            if (attr->ulValueLen != sizeof(CK_ULONG) || attr->pValue == NULL) {
397a93
-                TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID));
397a93
-                return CKR_ATTRIBUTE_VALUE_INVALID;
397a93
-            }
397a93
-            switch (*((CK_ULONG *)attr->pValue)) {
397a93
-            case IBM_DILITHIUM_KEYFORM_ROUND2:
397a93
-                return CKR_OK;
397a93
-            default:
397a93
-                TRACE_ERROR("%s\n", ock_err(CKR_ATTRIBUTE_VALUE_INVALID));
397a93
-                return CKR_ATTRIBUTE_VALUE_INVALID;
397a93
-            }
397a93
-        }
397a93
-        TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_READ_ONLY));
397a93
-        return CKR_ATTRIBUTE_READ_ONLY;
397a93
     default:
397a93
         return publ_key_validate_attribute(tokdata, tmpl, attr, mode);
397a93
     }
397a93
@@ -5075,7 +5202,15 @@ CK_RV ibm_dilithium_priv_validate_attribute(STDLL_TokData_t *tokdata,
397a93
                                             TEMPLATE *tmpl, CK_ATTRIBUTE *attr,
397a93
                                             CK_ULONG mode)
397a93
 {
397a93
+    CK_RV rc;
397a93
+
397a93
     switch (attr->type) {
397a93
+    case CKA_IBM_DILITHIUM_KEYFORM:
397a93
+    case CKA_IBM_DILITHIUM_MODE:
397a93
+        rc = ibm_pqc_validate_keyform_mode(attr, mode, CKM_IBM_DILITHIUM);
397a93
+        if (rc != CKR_OK)
397a93
+            return rc;
397a93
+        return CKR_OK;
397a93
     case CKA_IBM_DILITHIUM_RHO:
397a93
     case CKA_IBM_DILITHIUM_SEED:
397a93
     case CKA_IBM_DILITHIUM_TR:
397a93
@@ -5088,22 +5223,6 @@ CK_RV ibm_dilithium_priv_validate_attribute(STDLL_TokData_t *tokdata,
397a93
             return CKR_OK;
397a93
         TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_READ_ONLY));
397a93
         return CKR_ATTRIBUTE_READ_ONLY;
397a93
-    case CKA_IBM_DILITHIUM_KEYFORM:
397a93
-        if (mode == MODE_CREATE || mode == MODE_KEYGEN) {
397a93
-            if (attr->ulValueLen != sizeof(CK_ULONG) || attr->pValue == NULL) {
397a93
-                TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID));
397a93
-                return CKR_ATTRIBUTE_VALUE_INVALID;
397a93
-            }
397a93
-            switch (*((CK_ULONG *)attr->pValue)) {
397a93
-            case IBM_DILITHIUM_KEYFORM_ROUND2:
397a93
-                return CKR_OK;
397a93
-            default:
397a93
-                TRACE_ERROR("%s\n", ock_err(CKR_ATTRIBUTE_VALUE_INVALID));
397a93
-                return CKR_ATTRIBUTE_VALUE_INVALID;
397a93
-            }
397a93
-        }
397a93
-        TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_READ_ONLY));
397a93
-        return CKR_ATTRIBUTE_READ_ONLY;
397a93
     default:
397a93
         return priv_key_validate_attribute(tokdata, tmpl, attr, mode);
397a93
     }
397a93
diff --git a/usr/lib/ep11_stdll/ep11_specific.c b/usr/lib/ep11_stdll/ep11_specific.c
397a93
index 9221b8cd..c440cac5 100644
397a93
--- a/usr/lib/ep11_stdll/ep11_specific.c
397a93
+++ b/usr/lib/ep11_stdll/ep11_specific.c
397a93
@@ -158,8 +158,8 @@ const char label[] = "ep11tok";
397a93
 #define MAX_CSUMSIZE 64
397a93
 #define EP11_CSUMSIZE 3
397a93
 #define MAX_DIGEST_STATE_BYTES 1024
397a93
-#define MAX_CRYPT_STATE_BYTES 8192
397a93
-#define MAX_SIGN_STATE_BYTES 8192
397a93
+#define MAX_CRYPT_STATE_BYTES 12288
397a93
+#define MAX_SIGN_STATE_BYTES 12288
397a93
 #define MAX_APQN 256
397a93
 #define EP11_BLOB_WKID_OFFSET 32
397a93
 
397a93
@@ -1937,7 +1937,9 @@ static CK_BBOOL attr_applicable_for_ep11(STDLL_TokData_t * tokdata,
397a93
     case CKK_IBM_PQC_DILITHIUM:
397a93
         if (attr->type == CKA_ENCRYPT || attr->type == CKA_DECRYPT ||
397a93
             attr->type == CKA_WRAP || attr->type == CKA_UNWRAP ||
397a93
-            attr->type == CKA_DERIVE)
397a93
+            attr->type == CKA_DERIVE || 
397a93
+            attr->type == CKA_IBM_DILITHIUM_KEYFORM ||
397a93
+            attr->type == CKA_IBM_DILITHIUM_MODE)
397a93
             return CK_FALSE;
397a93
         break;
397a93
     default:
397a93
@@ -3587,6 +3589,7 @@ static CK_RV import_IBM_Dilithium_key(STDLL_TokData_t *tokdata, SESSION *sess,
397a93
     ep11_session_t *ep11_session = (ep11_session_t *) sess->private_data;
397a93
     CK_ATTRIBUTE *value_attr = NULL;
397a93
     CK_BBOOL data_alloced = TRUE;
397a93
+    const struct pqc_oid *oid;
397a93
 
397a93
     memcpy(iv, "1234567812345678", AES_BLOCK_SIZE);
397a93
 
397a93
@@ -3611,7 +3614,8 @@ static CK_RV import_IBM_Dilithium_key(STDLL_TokData_t *tokdata, SESSION *sess,
397a93
         /* Make an SPKI for the public IBM Dilithium key */
397a93
 
397a93
         /* A public IBM Dilithium key must either have a CKA_VALUE containing
397a93
-         * the SPKI, or must have a keyform value and the individual attributes
397a93
+         * the SPKI, or must have a keyform/mode value and the individual
397a93
+         * attributes
397a93
          */
397a93
         if (template_attribute_find(dilithium_key_obj->template,
397a93
                                     CKA_VALUE, &value_attr) &&
397a93
@@ -3621,7 +3625,10 @@ static CK_RV import_IBM_Dilithium_key(STDLL_TokData_t *tokdata, SESSION *sess,
397a93
             data_len = value_attr->ulValueLen;
397a93
             data_alloced = FALSE;
397a93
 
397a93
-            /* Decode SPKI and add public key attributes */
397a93
+            /*
397a93
+             * Decode SPKI and add public key attributes. This also adds the
397a93
+             * keyform and mode attributes to the template.
397a93
+             */
397a93
             rc = ibm_dilithium_priv_unwrap_get_data(dilithium_key_obj->template,
397a93
                                                     data, data_len, FALSE);
397a93
             if (rc != CKR_OK) {
397a93
@@ -3641,6 +3648,21 @@ static CK_RV import_IBM_Dilithium_key(STDLL_TokData_t *tokdata, SESSION *sess,
397a93
                            "data_len=0x%lx\n", __func__, class, rc, data_len);
397a93
             }
397a93
 
397a93
+            /* Ensure both, keyform and mode attributes are added */
397a93
+            oid = ibm_pqc_get_keyform_mode(dilithium_key_obj->template,
397a93
+                                           CKM_IBM_DILITHIUM);
397a93
+            if (oid == NULL) {
397a93
+                rc = CKR_TEMPLATE_INCOMPLETE;
397a93
+                goto done;
397a93
+            }
397a93
+
397a93
+            rc = ibm_pqc_add_keyform_mode(dilithium_key_obj->template,
397a93
+                                          oid, CKM_IBM_DILITHIUM);
397a93
+            if (rc != CKR_OK) {
397a93
+                TRACE_ERROR("ibm_pqc_add_keyform_mode failed\n");
397a93
+                goto done;
397a93
+            }
397a93
+
397a93
             /* Add SPKI as CKA_VALUE to public key (z/OS ICSF compatibility) */
397a93
             rc = build_attribute(CKA_VALUE, data, data_len, &value_attr);
397a93
             if (rc != CKR_OK) {
397a93
@@ -3677,8 +3699,8 @@ static CK_RV import_IBM_Dilithium_key(STDLL_TokData_t *tokdata, SESSION *sess,
397a93
         /* imported private IBM Dilithium key goes here */
397a93
 
397a93
         /* A public IBM Dilithium key must either have a CKA_VALUE containing
397a93
-         * the PKCS#8 encoded private key, or must have a keyform value and the
397a93
-         * individual attributes
397a93
+         * the PKCS#8 encoded private key, or must have a keyform/mode value
397a93
+         * and the individual attributes
397a93
          */
397a93
         if (template_attribute_find(dilithium_key_obj->template,
397a93
                                     CKA_VALUE, &value_attr) &&
397a93
@@ -3696,8 +3718,9 @@ static CK_RV import_IBM_Dilithium_key(STDLL_TokData_t *tokdata, SESSION *sess,
397a93
                 goto done;
397a93
             }
397a93
         } else {
397a93
-            /* extract the secret data to be wrapped
397a93
-             * since this is AES_CBC_PAD, padding is done in mechanism.
397a93
+            /* Extract the secret data to be wrapped since this is AES_CBC_PAD,
397a93
+             * padding is done in mechanism. This also adds the keyform and mode
397a93
+             * attributes to the template.
397a93
              */
397a93
             rc = ibm_dilithium_priv_wrap_get_data(dilithium_key_obj->template,
397a93
                                                   FALSE, &data, &data_len);
397a93
@@ -3705,6 +3728,21 @@ static CK_RV import_IBM_Dilithium_key(STDLL_TokData_t *tokdata, SESSION *sess,
397a93
                 TRACE_DEVEL("%s Dilithium wrap get data failed\n", __func__);
397a93
                 goto done;
397a93
             }
397a93
+
397a93
+            /* Ensure both, keyform and mode attributes are added */
397a93
+            oid = ibm_pqc_get_keyform_mode(dilithium_key_obj->template,
397a93
+                                           CKM_IBM_DILITHIUM);
397a93
+            if (oid == NULL) {
397a93
+                rc = CKR_TEMPLATE_INCOMPLETE;
397a93
+                goto done;
397a93
+            }
397a93
+
397a93
+            rc = ibm_pqc_add_keyform_mode(dilithium_key_obj->template,
397a93
+                                          oid, CKM_IBM_DILITHIUM);
397a93
+            if (rc != CKR_OK) {
397a93
+                TRACE_ERROR("ibm_pqc_add_keyform_mode failed\n");
397a93
+                goto done;
397a93
+            }
397a93
         }
397a93
 
397a93
         /* encrypt */
397a93
@@ -6456,8 +6494,7 @@ static CK_RV ibm_dilithium_generate_keypair(STDLL_TokData_t *tokdata,
397a93
     CK_ULONG new_publ_attrs_len = 0, new_priv_attrs_len = 0;
397a93
     CK_ATTRIBUTE *new_publ_attrs2 = NULL, *new_priv_attrs2 = NULL;
397a93
     CK_ULONG new_publ_attrs2_len = 0, new_priv_attrs2_len = 0;
397a93
-    const CK_BYTE dilithium_oid[] = { 0x06, 0x0b, 0x2b, 0x06, 0x01, 0x04, 0x01,
397a93
-                                      0x02, 0x82, 0x0b, 0x01, 0x06, 0x05 };
397a93
+    const struct pqc_oid *dilithium_oid;
397a93
 
397a93
     if (pMechanism->mechanism != CKM_IBM_DILITHIUM) {
397a93
         TRACE_ERROR("Invalid mechanism provided for %s\n ", __func__);
397a93
@@ -6480,9 +6517,25 @@ static CK_RV ibm_dilithium_generate_keypair(STDLL_TokData_t *tokdata,
397a93
         goto error;
397a93
     }
397a93
 
397a93
+    dilithium_oid = ibm_pqc_get_keyform_mode(publ_tmpl, CKM_IBM_DILITHIUM);
397a93
+    if (dilithium_oid == NULL)
397a93
+        dilithium_oid = ibm_pqc_get_keyform_mode(priv_tmpl, CKM_IBM_DILITHIUM);
397a93
+    if (dilithium_oid == NULL)
397a93
+        dilithium_oid = find_pqc_by_keyform(dilithium_oids,
397a93
+                                            CK_IBM_DILITHIUM_KEYFORM_ROUND2_65);
397a93
+    if (dilithium_oid == NULL) {
397a93
+        TRACE_ERROR("%s Failed to determine Dilithium OID\n", __func__);
397a93
+        rc = CKR_FUNCTION_FAILED;
397a93
+        goto error;
397a93
+    }
397a93
+
397a93
+    TRACE_INFO("%s Generate Dilithium key with keyform %lu\n", __func__,
397a93
+               dilithium_oid->keyform);
397a93
+
397a93
     rc = add_to_attribute_array(&new_publ_attrs, &new_publ_attrs_len,
397a93
-                                CKA_IBM_PQC_PARAMS, (CK_BYTE *)dilithium_oid,
397a93
-                                sizeof(dilithium_oid));
397a93
+                                CKA_IBM_PQC_PARAMS,
397a93
+                                (CK_BYTE *)dilithium_oid->oid,
397a93
+                                dilithium_oid->oid_len);
397a93
     if (rc != CKR_OK) {
397a93
         TRACE_ERROR("%s add_to_attribute_array failed with rc=0x%lx\n",
397a93
                     __func__, rc);
397a93
@@ -6490,8 +6543,9 @@ static CK_RV ibm_dilithium_generate_keypair(STDLL_TokData_t *tokdata,
397a93
     }
397a93
 
397a93
     rc = add_to_attribute_array(&new_priv_attrs, &new_priv_attrs_len,
397a93
-                                CKA_IBM_PQC_PARAMS,(CK_BYTE *)dilithium_oid,
397a93
-                                sizeof(dilithium_oid));
397a93
+                                CKA_IBM_PQC_PARAMS,
397a93
+                                (CK_BYTE *)dilithium_oid->oid,
397a93
+                                dilithium_oid->oid_len);
397a93
     if (rc != CKR_OK) {
397a93
         TRACE_ERROR("%s add_to_attribute_array failed with rc=0x%lx\n",
397a93
                     __func__, rc);
397a93
-- 
397a93
2.16.2.windows.1
397a93