From 4c1e5cb38485986014c57b77de20c390714c5f1d Mon Sep 17 00:00:00 2001
From: zhengxiaoxiao <zhengxiaoxiao2@huawei.com>
Date: Thu, 10 Oct 2024 07:54:19 +0800
Subject: [PATCH] add GCRY_MD_SM3_PGP set to 109

Co-authored-by: Huaxin Lu <luhuaxin1@huawei.com>
Signed-off-by: zhengxiaoxiao <zhengxiaoxiao2@huawei.com>
---
 cipher/md.c     | 25 +++++++++++++++++++++++++
 cipher/sm3.c    | 15 +++++++++++++++
 config.h.in     |  3 +++
 configure.ac    |  7 ++++++-
 src/cipher.h    |  3 +++
 src/gcrypt.h.in |  5 +++++
 6 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/cipher/md.c b/cipher/md.c
index 34336b5..c9aeb3c 100644
--- a/cipher/md.c
+++ b/cipher/md.c
@@ -99,6 +99,9 @@ static const gcry_md_spec_t * const digest_list[] =
 #endif
 #if USE_SM3
      &_gcry_digest_spec_sm3,
+#endif
+#if USE_SM3_PGP
+     &_gcry_digest_spec_sm3_pgp,
 #endif
      NULL
   };
@@ -153,6 +156,24 @@ static const gcry_md_spec_t * const digest_list_algo0[] =
 #endif
   };
 
+#if USE_SM3_PGP
+/* Digest implementations starting with index 100 (enum gcry_md_algos) */
+static const gcry_md_spec_t * const digest_list_algo100[] =
+  {
+    NULL, /* Private or Experimental Use 100 */
+    NULL, /* Private or Experimental Use 101 */
+    NULL, /* Private or Experimental Use 102 */
+    NULL, /* Private or Experimental Use 103 */
+    NULL, /* Private or Experimental Use 104 */
+    NULL, /* Private or Experimental Use 105 */
+    NULL, /* Private or Experimental Use 106 */
+    NULL, /* Private or Experimental Use 107 */
+    NULL, /* Private or Experimental Use 108 */
+    &_gcry_digest_spec_sm3_pgp,
+    NULL /* Private or Experimental Use 110 */
+  };
+#endif
+
 /* Digest implementations starting with index 301 (enum gcry_md_algos) */
 static const gcry_md_spec_t * const digest_list_algo301[] =
   {
@@ -307,6 +328,10 @@ spec_from_algo (int algo)
     spec = digest_list_algo0[algo];
   else if (algo >= 301 && algo < 301 + DIM(digest_list_algo301))
     spec = digest_list_algo301[algo - 301];
+#if USE_SM3_PGP
+  else if (algo >= 100 && algo < 100 + DIM(digest_list_algo100))
+    spec = digest_list_algo100[algo - 100];
+#endif
 
   if (spec)
     gcry_assert (spec->algo == algo);
diff --git a/cipher/sm3.c b/cipher/sm3.c
index 0ab5f50..d109c7d 100644
--- a/cipher/sm3.c
+++ b/cipher/sm3.c
@@ -503,6 +503,9 @@ run_selftests (int algo, int extended, selftest_report_func_t report)
   switch (algo)
     {
     case GCRY_MD_SM3:
+#if USE_SM3_PGP
+    case GCRY_MD_SM3_PGP:
+#endif
       ec = selftests_sm3 (extended, report);
       break;
     default:
@@ -535,3 +538,15 @@ const gcry_md_spec_t _gcry_digest_spec_sm3 =
     sizeof (SM3_CONTEXT),
     run_selftests
   };
+
+#if USE_SM3_PGP
+const gcry_md_spec_t _gcry_digest_spec_sm3_pgp =
+  {
+    GCRY_MD_SM3_PGP, {0, 0},
+    "SM3", asn_sm3, DIM (asn_sm3), oid_spec_sm3, 32,
+    sm3_init, _gcry_md_block_write, sm3_final, sm3_read, NULL,
+    _gcry_sm3_hash_buffers,
+    sizeof (SM3_CONTEXT),
+    run_selftests
+  };
+#endif
diff --git a/config.h.in b/config.h.in
index cfd150f..6aa5992 100644
--- a/config.h.in
+++ b/config.h.in
@@ -632,6 +632,9 @@
 /* Defined if this module should be included */
 #undef USE_SM3
 
+/* Defined if this module should be included */
+#undef USE_SM3_PGP
+
 /* Defined if this module should be included */
 #undef USE_SM4
 
diff --git a/configure.ac b/configure.ac
index 24ec2eb..81892ae 100644
--- a/configure.ac
+++ b/configure.ac
@@ -221,7 +221,7 @@ enabled_pubkey_ciphers=""
 # Definitions for message digests.
 available_digests="crc gostr3411-94 md2 md4 md5 rmd160 sha1 sha256 sha512"
 available_digests="$available_digests sha3 tiger whirlpool stribog blake2"
-available_digests="$available_digests sm3"
+available_digests="$available_digests sm3 sm3_pgp"
 enabled_digests=""
 
 # Definitions for kdfs (optional ones)
@@ -3064,6 +3064,11 @@ if test "$found" = "1" ; then
    esac
 fi
 
+LIST_MEMBER(sm3_pgp, $enabled_digests)
+if test "$found" = "1" ; then
+   AC_DEFINE(USE_SM3_PGP, 1, [Defined if this module should be included])
+fi
+
 # SHA-1 needs to be included always for example because it is used by
 # random-csprng.c.
 GCRYPT_DIGESTS="$GCRYPT_DIGESTS sha1.lo"
diff --git a/src/cipher.h b/src/cipher.h
index 87f8c4d..03ce82f 100644
--- a/src/cipher.h
+++ b/src/cipher.h
@@ -208,6 +208,9 @@ extern const gcry_md_spec_t _gcry_digest_spec_blake2s_224;
 extern const gcry_md_spec_t _gcry_digest_spec_blake2s_160;
 extern const gcry_md_spec_t _gcry_digest_spec_blake2s_128;
 extern const gcry_md_spec_t _gcry_digest_spec_sm3;
+#if USE_SM3_PGP
+extern const gcry_md_spec_t _gcry_digest_spec_sm3_pgp;
+#endif
 
 /* Declarations for the pubkey cipher specifications.  */
 extern gcry_pk_spec_t _gcry_pubkey_spec_rsa;
diff --git a/src/gcrypt.h.in b/src/gcrypt.h.in
index 58581da..581f41e 100644
--- a/src/gcrypt.h.in
+++ b/src/gcrypt.h.in
@@ -1259,6 +1259,11 @@ enum gcry_md_algos
     GCRY_MD_SHA512  = 10,
     GCRY_MD_SHA224  = 11,
 
+#if USE_SM3_PGP
+    /* 100-110 openPGP Private or Experimental Use */
+    GCRY_MD_SM3_PGP       = 109,
+#endif
+
     GCRY_MD_MD4           = 301,
     GCRY_MD_CRC32         = 302,
     GCRY_MD_CRC32_RFC1510 = 303,
-- 
2.43.0

