Blame SOURCES/0030-p11sak-Support-additional-Dilithium-variants.patch

447573
From 3f8b4270a7601b42f15f13f54b9b5fc207a14723 Mon Sep 17 00:00:00 2001
447573
From: Ingo Franzki <ifranzki@linux.ibm.com>
447573
Date: Tue, 8 Nov 2022 16:46:26 +0100
447573
Subject: [PATCH 30/34] p11sak: Support additional Dilithium variants
447573
447573
Support the following Dilithium versions to be specified with the
447573
generate-key command: r2_65 (as of today), r2_87, r3_44, r3_65, r3_87.
447573
447573
Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
447573
---
447573
 man/man1/p11sak.1.in     | 12 ++++++++++-
447573
 usr/sbin/p11sak/p11sak.c | 53 ++++++++++++++++++++++++++++++++++++++++++++----
447573
 2 files changed, 60 insertions(+), 5 deletions(-)
447573
447573
diff --git a/man/man1/p11sak.1.in b/man/man1/p11sak.1.in
447573
index a2c2b879..6938b203 100644
447573
--- a/man/man1/p11sak.1.in
447573
+++ b/man/man1/p11sak.1.in
447573
@@ -262,7 +262,7 @@ Use the
447573
 command and key argument to generate an IBM Dilithium key, where
447573
 .I VERSION
447573
 specifies the version of the IBM Dilithium keypair. The following arguments can be used for respective keys:
447573
-.B r2_65
447573
+.B r2_65 | r2_87 | r3_44 | r3_65 | r3_87
447573
 .PP
447573
 The
447573
 .B \-\-slot
447573
@@ -368,6 +368,16 @@ to select the EC curve used to generate the key.
447573
 .
447573
 .
447573
 
447573
+.SS "r2_6|r2_87|r3_44|r3_65|r3_875"
447573
+the
447573
+.B ibm-dilithium
447573
+argument has to be followed by either of these
447573
+.I VERSION
447573
+to select the IBM dilithium version used to generate the key.
447573
+.PP
447573
+.
447573
+.
447573
+.
447573
 .SH OPTIONS
447573
 
447573
 .SS "\-\-slot SLOTID"
447573
diff --git a/usr/sbin/p11sak/p11sak.c b/usr/sbin/p11sak/p11sak.c
447573
index 8cfcb21d..5ceb145b 100644
447573
--- a/usr/sbin/p11sak/p11sak.c
447573
+++ b/usr/sbin/p11sak/p11sak.c
447573
@@ -387,7 +387,7 @@ static void print_gen_help(void)
447573
     printf("          brainpoolP320r1 | brainpoolP320t1 | brainpoolP384r1 | brainpoolP384t1 | \n");
447573
     printf("          brainpoolP512r1 | brainpoolP512t1 | curve25519 | curve448 | ed25519 | \n");
447573
     printf("          ed448]\n");
447573
-    printf("      ibm-dilithium [r2_65]\n");
447573
+    printf("      ibm-dilithium [r2_65 | r2_87 | r3_44 | r3_65 | r3_87]\n");
447573
     printf("\n Options:\n");
447573
     printf(
447573
             "      --slot SLOTID                           openCryptoki repository token SLOTID.\n");
447573
@@ -526,6 +526,10 @@ static void print_gen_ibm_dilithium_help(void)
447573
     printf("\n Usage: p11sak generate-key ibm-dilithium [ARGS] [OPTIONS]\n");
447573
     printf("\n Args:\n");
447573
     printf("      r2_65\n");
447573
+    printf("      r2_87\n");
447573
+    printf("      r3_44\n");
447573
+    printf("      r3_65\n");
447573
+    printf("      r3_87\n");
447573
     printf("\n Options:\n");
447573
     printf(
447573
             "      --slot SLOTID                           openCryptoki repository token SLOTID.\n");
447573
@@ -764,6 +768,35 @@ static CK_RV read_ec_args(const char *ECcurve, CK_ATTRIBUTE *pubattr,
447573
 
447573
     return CKR_OK;
447573
 }
447573
+/**
447573
+ * Builds the CKA_IBM_DILITHIUM_KEYFORM attribute from the given version.
447573
+ */
447573
+static CK_RV read_dilithium_args(const char *dilithium_ver, CK_ULONG *keyform,
447573
+                                 CK_ATTRIBUTE *pubattr, CK_ULONG *pubcount)
447573
+{
447573
+    if (strcasecmp(dilithium_ver, "r2_65") == 0) {
447573
+        *keyform = CK_IBM_DILITHIUM_KEYFORM_ROUND2_65;
447573
+    } else if (strcasecmp(dilithium_ver, "r2_87") == 0) {
447573
+        *keyform = CK_IBM_DILITHIUM_KEYFORM_ROUND2_87;
447573
+    } else if (strcasecmp(dilithium_ver, "r3_44") == 0) {
447573
+        *keyform =  CK_IBM_DILITHIUM_KEYFORM_ROUND3_44;
447573
+    } else if (strcasecmp(dilithium_ver, "r3_65") == 0) {
447573
+        *keyform = CK_IBM_DILITHIUM_KEYFORM_ROUND3_65;
447573
+    } else if (strcasecmp(dilithium_ver, "r3_87") == 0) {
447573
+        *keyform = CK_IBM_DILITHIUM_KEYFORM_ROUND3_87;
447573
+    } else {
447573
+        fprintf(stderr, "Unexpected case while parsing dilithium version.\n");
447573
+        fprintf(stderr, "Note: not all tokens support all versions.\n");
447573
+        return CKR_ARGUMENTS_BAD;
447573
+    }
447573
+
447573
+    pubattr[*pubcount].type = CKA_IBM_DILITHIUM_KEYFORM;
447573
+    pubattr[*pubcount].ulValueLen = sizeof(CK_ULONG);
447573
+    pubattr[*pubcount].pValue = keyform;
447573
+    (*pubcount)++;
447573
+
447573
+    return CKR_OK;
447573
+}
447573
 /**
447573
  * Builds two CKA_LABEL attributes from given label.
447573
  */
447573
@@ -1096,6 +1129,8 @@ static CK_RV key_pair_gen(CK_SESSION_HANDLE session, CK_SLOT_ID slot,
447573
     if (rc != CKR_OK) {
447573
         if (is_rejected_by_policy(rc, session))
447573
             fprintf(stderr, "Key pair generation rejected by policy\n");
447573
+        else if (kt == kt_IBM_DILITHIUM && rc == CKR_KEY_SIZE_RANGE)
447573
+            fprintf(stderr, "IBM Dilithum version is not supported\n");
447573
         else
447573
             fprintf(stderr, "Key pair generation failed (error code 0x%lX: %s)\n", rc,
447573
                     p11_get_ckr(rc));
447573
@@ -1845,11 +1880,15 @@ static CK_RV check_args_gen_key(p11sak_kt *kt, CK_ULONG keylength,
447573
     case kt_IBM_DILITHIUM:
447573
         if (dilithium_ver == NULL) {
447573
             fprintf(stderr,
447573
-                    "Cipher key type [%d] supported but Dilithium version not set in arguments. Try adding argument <r2_65>\n",
447573
+                    "Cipher key type [%d] supported but Dilithium version not set in arguments. Try adding argument <r2_65>, <r2_87>, <r3_44>, <r3_65>, or <r3_87>\n",
447573
                     *kt);
447573
             return CKR_ARGUMENTS_BAD;
447573
         }
447573
-        if (strcasecmp(dilithium_ver, "r2_65") == 0) {
447573
+        if (strcasecmp(dilithium_ver, "r2_65") == 0 ||
447573
+            strcasecmp(dilithium_ver, "r2_87") == 0 ||
447573
+            strcasecmp(dilithium_ver, "r3_44") == 0 ||
447573
+            strcasecmp(dilithium_ver, "r3_65") == 0 ||
447573
+            strcasecmp(dilithium_ver, "r3_87") == 0) {
447573
             break;
447573
         } else {
447573
             fprintf(stderr, "IBM Dilithium version [%s] not supported \n", dilithium_ver);
447573
@@ -2450,7 +2489,7 @@ static CK_RV generate_asymmetric_key(CK_SESSION_HANDLE session, CK_SLOT_ID slot,
447573
     CK_ATTRIBUTE prv_attr[KEY_MAX_BOOL_ATTR_COUNT + 2];
447573
     CK_ULONG prv_acount = 0;
447573
     CK_MECHANISM mech;
447573
-    CK_ULONG i;
447573
+    CK_ULONG i, keyform;
447573
     CK_RV rc;
447573
     const char separator = ':';
447573
 
447573
@@ -2475,6 +2514,12 @@ static CK_RV generate_asymmetric_key(CK_SESSION_HANDLE session, CK_SLOT_ID slot,
447573
         }
447573
         break;
447573
     case kt_IBM_DILITHIUM:
447573
+        rc = read_dilithium_args(dilithium_ver, &keyform,
447573
+                                 pub_attr, &pub_acount);
447573
+        if (rc) {
447573
+            fprintf(stderr, "Error parsing Dilithium parameters!\n");
447573
+            goto done;
447573
+        }
447573
         printf("Generating Dilithium keypair with %s\n", dilithium_ver);
447573
         break;
447573
     default:
447573
-- 
447573
2.16.2.windows.1
447573