Blame SOURCES/0018-COMMON-Add-post-quantum-algorithm-OIDs.patch

397a93
From 67ed25a8f7764e61647c3c31d09a1e60db38006b Mon Sep 17 00:00:00 2001
397a93
From: Ingo Franzki <ifranzki@linux.ibm.com>
397a93
Date: Wed, 16 Feb 2022 11:20:54 +0100
397a93
Subject: [PATCH 18/34] COMMON: Add post-quantum algorithm OIDs
397a93
397a93
Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
397a93
---
397a93
 usr/include/include.mk         |  2 +-
397a93
 usr/include/pqc_oids.h         | 48 ++++++++++++++++++++++++++
397a93
 usr/lib/common/common.mk       |  3 +-
397a93
 usr/lib/common/globals.c       |  1 +
397a93
 usr/lib/common/pqc_defs.h      | 48 ++++++++++++++++++++++++++
397a93
 usr/lib/common/pqc_supported.c | 78 ++++++++++++++++++++++++++++++++++++++++++
397a93
 6 files changed, 178 insertions(+), 2 deletions(-)
397a93
 create mode 100644 usr/include/pqc_oids.h
397a93
 create mode 100644 usr/lib/common/pqc_defs.h
397a93
 create mode 100644 usr/lib/common/pqc_supported.c
397a93
397a93
diff --git a/usr/include/include.mk b/usr/include/include.mk
397a93
index 79e593d7..a63e043f 100644
397a93
--- a/usr/include/include.mk
397a93
+++ b/usr/include/include.mk
397a93
@@ -3,7 +3,7 @@ opencryptokiincludedir = ${includedir}/opencryptoki
397a93
 opencryptokiinclude_HEADERS =						\
397a93
 	usr/include/apiclient.h	usr/include/pkcs11types.h		\
397a93
 	usr/include/pkcs11.h						\
397a93
-	usr/include/ec_curves.h
397a93
+	usr/include/ec_curves.h usr/include/pqc_oids.h
397a93
 
397a93
 noinst_HEADERS +=							\
397a93
 	usr/include/apictl.h usr/include/local_types.h			\
397a93
diff --git a/usr/include/pqc_oids.h b/usr/include/pqc_oids.h
397a93
new file mode 100644
397a93
index 00000000..0891373e
397a93
--- /dev/null
397a93
+++ b/usr/include/pqc_oids.h
397a93
@@ -0,0 +1,48 @@
397a93
+/*
397a93
+ * COPYRIGHT (c) International Business Machines Corp. 2022
397a93
+ *
397a93
+ * This program is provided under the terms of the Common Public License,
397a93
+ * version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
397a93
+ * software constitutes recipient's acceptance of CPL-1.0 terms which can be
397a93
+ * found in the file LICENSE file or at
397a93
+ * https://opensource.org/licenses/cpl1.0.php
397a93
+ */
397a93
+
397a93
+
397a93
+#ifndef _PQC_OIDS_H_
397a93
+#define _PQC_OIDS_H_
397a93
+
397a93
+/*
397a93
+ * OIDs and their DER encoding for the post-quantum crypto algorithms
397a93
+ * supported by OpenCryptoki:
397a93
+ */
397a93
+
397a93
+/* Dilithium Round 2 high-security (SHAKE-256): 1.3.6.1.4.1.2.267.1.6.5 */
397a93
+#define OCK_DILITHIUM_R2_65        { 0x06, 0x0B, 0x2B, 0x06, 0x01, 0x04, \
397a93
+                                     0x01, 0x02, 0x82, 0x0B, 0x01, 0x06, 0x05 }
397a93
+
397a93
+/* Dilithium Round 2 for outbound authentication: 1.3.6.1.4.1.2.267.1.8.7 */
397a93
+#define OCK_DILITHIUM_R2_87        { 0x06, 0x0B, 0x2B, 0x06, 0x01, 0x04, \
397a93
+                                     0x01, 0x02, 0x82, 0x0B, 0x01, 0x08, 0x07 }
397a93
+
397a93
+/* Dilithium Round 3 weak (SHAKE-256): 1.3.6.1.4.1.2.267.7.4.4 */
397a93
+#define OCK_DILITHIUM_R3_44        { 0x06, 0x0B, 0x2B, 0x06, 0x01, 0x04, \
397a93
+                                     0x01, 0x02, 0x82, 0x0B, 0x07, 0x04, 0x04 }
397a93
+
397a93
+/* Dilithium Round 3 recommended (SHAKE-256): 1.3.6.1.4.1.2.267.7.6.5 */
397a93
+#define OCK_DILITHIUM_R3_65        { 0x06, 0x0B, 0x2B, 0x06, 0x01, 0x04, \
397a93
+                                     0x01, 0x02, 0x82, 0x0B, 0x07, 0x06, 0x05 }
397a93
+
397a93
+/* Dilithium Round 3 high-security (SHAKE-256): 1.3.6.1.4.1.2.267.7.8.7 */
397a93
+#define OCK_DILITHIUM_R3_87        { 0x06, 0x0B, 0x2B, 0x06, 0x01, 0x04, \
397a93
+                                     0x01, 0x02, 0x82, 0x0B, 0x07, 0x08, 0x07 }
397a93
+
397a93
+/* Kyber Round 2 768 (SHAKE-128): 1.3.6.1.4.1.2.267.5.3.3 */
397a93
+#define OCK_KYBER_R2_768           { 0x06, 0x0B, 0x2B, 0x06, 0x01, 0x04, \
397a93
+                                     0x01, 0x02, 0x82, 0x0B, 0x05, 0x03, 0x03 }
397a93
+
397a93
+/* Kyber Round 2 1024 (SHAKE-128): 1.3.6.1.4.1.2.267.5.4.4 */
397a93
+#define OCK_KYBER_R2_1024          { 0x06, 0x0B, 0x2B, 0x06, 0x01, 0x04, \
397a93
+                                     0x01, 0x02, 0x82, 0x0B, 0x05, 0x04, 0x04 }
397a93
+
397a93
+#endif                          // _PQC_OIDS_H_
397a93
diff --git a/usr/lib/common/common.mk b/usr/lib/common/common.mk
397a93
index 6341cb74..93f1e21f 100644
397a93
--- a/usr/lib/common/common.mk
397a93
+++ b/usr/lib/common/common.mk
397a93
@@ -7,4 +7,5 @@ noinst_HEADERS +=							\
397a93
 	usr/lib/common/p11util.h usr/lib/common/event_client.h		\
397a93
 	usr/lib/common/list.h usr/lib/common/tok_specific.h		\
397a93
 	usr/lib/common/uri_enc.h usr/lib/common/uri.h 			\
397a93
-	usr/lib/common/buffer.h usr/lib/common/pin_prompt.h
397a93
+	usr/lib/common/buffer.h usr/lib/common/pin_prompt.h		\
397a93
+	usr/lib/common/pqc_defs.h
397a93
diff --git a/usr/lib/common/globals.c b/usr/lib/common/globals.c
397a93
index db4d352c..5b79e785 100644
397a93
--- a/usr/lib/common/globals.c
397a93
+++ b/usr/lib/common/globals.c
397a93
@@ -27,6 +27,7 @@
397a93
 #include "defs.h"
397a93
 #include "host_defs.h"
397a93
 #include "h_extern.h"
397a93
+#include "pqc_oids.h"
397a93
 
397a93
 struct ST_FCN_LIST function_list;
397a93
 
397a93
diff --git a/usr/lib/common/pqc_defs.h b/usr/lib/common/pqc_defs.h
397a93
new file mode 100644
397a93
index 00000000..51ee1200
397a93
--- /dev/null
397a93
+++ b/usr/lib/common/pqc_defs.h
397a93
@@ -0,0 +1,48 @@
397a93
+/*
397a93
+ * COPYRIGHT (c) International Business Machines Corp. 2022
397a93
+ *
397a93
+ * This program is provided under the terms of the Common Public License,
397a93
+ * version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
397a93
+ * software constitutes recipient's acceptance of CPL-1.0 terms which can be
397a93
+ * found in the file LICENSE file or at
397a93
+ * https://opensource.org/licenses/cpl1.0.php
397a93
+ */
397a93
+
397a93
+#ifndef _PQC_DEFS
397a93
+#define _PQC_DEFS
397a93
+
397a93
+#include <stdio.h>
397a93
+
397a93
+#include "pqc_oids.h"
397a93
+
397a93
+extern const CK_BYTE dilithium_r2_65[];
397a93
+extern const CK_ULONG dilithium_r2_65_len;
397a93
+extern const CK_BYTE dilithium_r2_87[];
397a93
+extern const CK_ULONG dilithium_r2_87_len;
397a93
+extern const CK_BYTE dilithium_r3_44[];
397a93
+extern const CK_ULONG dilithium_r3_44_len;
397a93
+extern const CK_BYTE dilithium_r3_56[];
397a93
+extern const CK_ULONG dilithium_r3_56_len;
397a93
+extern const CK_BYTE dilithium_r3_87[];
397a93
+extern const CK_ULONG dilithium_r3_87_len;
397a93
+
397a93
+extern const CK_BYTE kyber_r2_768[];
397a93
+extern const CK_ULONG kyber_r2_768_len;
397a93
+extern const CK_BYTE kyber_r2_1024[];
397a93
+extern const CK_ULONG kyber_r2_1024_len;
397a93
+
397a93
+struct pqc_oid {
397a93
+    const CK_BYTE *oid;
397a93
+    CK_ULONG oid_len;
397a93
+    CK_ULONG keyform;
397a93
+};
397a93
+
397a93
+extern const struct pqc_oid dilithium_oids[];
397a93
+extern const struct pqc_oid kyber_oids[];
397a93
+
397a93
+const struct pqc_oid *find_pqc_by_keyform(const struct pqc_oid *pqcs,
397a93
+                                          CK_ULONG keyform);
397a93
+const struct pqc_oid *find_pqc_by_oid(const struct pqc_oid *pqcs,
397a93
+                                      CK_BYTE *oid, CK_ULONG oid_len);
397a93
+
397a93
+#endif
397a93
diff --git a/usr/lib/common/pqc_supported.c b/usr/lib/common/pqc_supported.c
397a93
new file mode 100644
397a93
index 00000000..4f048c33
397a93
--- /dev/null
397a93
+++ b/usr/lib/common/pqc_supported.c
397a93
@@ -0,0 +1,78 @@
397a93
+/*
397a93
+ * COPYRIGHT (c) International Business Machines Corp. 2022
397a93
+ *
397a93
+ * This program is provided under the terms of the Common Public License,
397a93
+ * version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
397a93
+ * software constitutes recipient's acceptance of CPL-1.0 terms which can be
397a93
+ * found in the file LICENSE file or at
397a93
+ * https://opensource.org/licenses/cpl1.0.php
397a93
+ */
397a93
+
397a93
+#include <string.h>
397a93
+#include "pkcs11types.h"
397a93
+#include "pqc_defs.h"
397a93
+
397a93
+const CK_BYTE dilithium_r2_65[] = OCK_DILITHIUM_R2_65;
397a93
+const CK_ULONG dilithium_r2_65_len = sizeof(dilithium_r2_65);
397a93
+const CK_BYTE dilithium_r2_87[] = OCK_DILITHIUM_R2_87;
397a93
+const CK_ULONG dilithium_r2_87_len = sizeof(dilithium_r2_87);
397a93
+const CK_BYTE dilithium_r3_44[] = OCK_DILITHIUM_R3_44;
397a93
+const CK_ULONG dilithium_r3_44_len = sizeof(dilithium_r3_44);
397a93
+const CK_BYTE dilithium_r3_65[] = OCK_DILITHIUM_R3_65;
397a93
+const CK_ULONG dilithium_r3_65_len = sizeof(dilithium_r3_65);
397a93
+const CK_BYTE dilithium_r3_87[] = OCK_DILITHIUM_R3_87;
397a93
+const CK_ULONG dilithium_r3_87_len = sizeof(dilithium_r3_87);
397a93
+
397a93
+const struct pqc_oid dilithium_oids[] = {
397a93
+    { .oid = dilithium_r2_65, .oid_len = dilithium_r2_65_len,
397a93
+      .keyform = CK_IBM_DILITHIUM_KEYFORM_ROUND2_65 },
397a93
+    { .oid = dilithium_r2_87, .oid_len = dilithium_r2_87_len,
397a93
+      .keyform = CK_IBM_DILITHIUM_KEYFORM_ROUND2_87 },
397a93
+    { .oid = dilithium_r3_44, .oid_len = dilithium_r3_44_len,
397a93
+      .keyform = CK_IBM_DILITHIUM_KEYFORM_ROUND3_44 },
397a93
+    { .oid = dilithium_r3_65, .oid_len = dilithium_r3_65_len,
397a93
+      .keyform = CK_IBM_DILITHIUM_KEYFORM_ROUND3_65 },
397a93
+    { .oid = dilithium_r3_87, .oid_len = dilithium_r3_87_len,
397a93
+      .keyform = CK_IBM_DILITHIUM_KEYFORM_ROUND3_87 },
397a93
+    { .oid = NULL, .oid_len = 0, .keyform = 0 }
397a93
+};
397a93
+
397a93
+const CK_BYTE kyber_r2_768[] = OCK_KYBER_R2_768;
397a93
+const CK_ULONG kyber_r2_768_len = sizeof(kyber_r2_768);
397a93
+const CK_BYTE kyber_r2_1024[] = OCK_KYBER_R2_1024;
397a93
+const CK_ULONG kyber_r2_1024_len = sizeof(kyber_r2_1024);
397a93
+
397a93
+const struct pqc_oid kyber_oids[] = {
397a93
+    { .oid = kyber_r2_768, .oid_len = kyber_r2_768_len,
397a93
+       .keyform = CK_IBM_KYBER_KEYFORM_ROUND2_768 },
397a93
+    { .oid = kyber_r2_1024, .oid_len = kyber_r2_1024_len,
397a93
+      .keyform = CK_IBM_KYBER_KEYFORM_ROUND2_1024 },
397a93
+    { .oid = NULL, .oid_len = 0, .keyform = 0 }
397a93
+};
397a93
+
397a93
+const struct pqc_oid *find_pqc_by_keyform(const struct pqc_oid *pqcs,
397a93
+                                          CK_ULONG keyform)
397a93
+{
397a93
+    CK_ULONG i;
397a93
+
397a93
+    for (i = 0; pqcs[i].oid != NULL; i++) {
397a93
+        if (pqcs[i].keyform == keyform)
397a93
+            return &pqcs[i];
397a93
+    }
397a93
+
397a93
+    return NULL;
397a93
+}
397a93
+
397a93
+const struct pqc_oid *find_pqc_by_oid(const struct pqc_oid *pqcs,
397a93
+                                      CK_BYTE *oid, CK_ULONG oid_len)
397a93
+{
397a93
+    CK_ULONG i;
397a93
+
397a93
+    for (i = 0; pqcs[i].oid != NULL; i++) {
397a93
+        if (pqcs[i].oid_len == oid_len &&
397a93
+            memcmp(pqcs[i].oid, oid, oid_len) == 0)
397a93
+            return &pqcs[i];
397a93
+    }
397a93
+
397a93
+    return NULL;
397a93
+}
397a93
-- 
397a93
2.16.2.windows.1
397a93