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

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