[PATCH 1/3] Add GMAC-SM4 and Poly1305-SM4

Jussi Kivilinna jussi.kivilinna at iki.fi
Wed Jan 4 18:53:03 CET 2023


* cipher/cipher.c (cipher_list_algo301): Remove comma at the end
of last entry.
* cipher/mac-gmac.c (map_mac_algo_to_cipher): Add SM4.
(_gcry_mac_type_spec_gmac_sm4): New.
* cipher/max-internal.h (_gcry_mac_type_spec_gmac_sm4)
(_gcry_mac_type_spec_poly1305mac_sm4): New.
* cipher/mac-poly1305.c (poly1305mac_open): Add SM4.
(_gcry_mac_type_spec_poly1305mac_sm4): New.
* cipher/mac.c (mac_list, mac_list_algo401, mac_list_algo501): Add
GMAC-SM4 and Poly1304-SM4.
(mac_list_algo101): Remove comma at the end of last entry.
* cipher/md.c (digest_list_algo301): Remove comma at the end of
last entry.
* doc/gcrypt.texi: Add GCRY_MAC_GMAC_SM4 and GCRY_MAC_POLY1305_SM4.
* src/gcrypt.h.in (GCRY_MAC_GMAC_SM4, GCRY_MAC_POLY1305_SM4): New.
* tests/bench-slope.c (bench_mac_init): Setup IV for
GCRY_MAC_POLY1305_SM4.
* tests/benchmark.c (mac_bench): Likewise.
--

Signed-off-by: Jussi Kivilinna <jussi.kivilinna at iki.fi>
---
 cipher/cipher.c       |  6 +++---
 cipher/mac-gmac.c     |  8 ++++++++
 cipher/mac-internal.h |  6 ++++++
 cipher/mac-poly1305.c |  9 +++++++++
 cipher/mac.c          | 22 +++++++++++++++++-----
 cipher/md.c           |  4 ++--
 doc/gcrypt.texi       |  8 ++++++++
 src/gcrypt.h.in       |  4 +++-
 tests/bench-slope.c   |  1 +
 tests/benchmark.c     |  2 +-
 10 files changed, 58 insertions(+), 12 deletions(-)

diff --git a/cipher/cipher.c b/cipher/cipher.c
index 026c1511..6f92b75a 100644
--- a/cipher/cipher.c
+++ b/cipher/cipher.c
@@ -91,7 +91,7 @@ static gcry_cipher_spec_t * const cipher_list[] =
 #if USE_SM4
      &_gcry_cipher_spec_sm4,
 #endif
-    NULL
+     NULL
   };
 
 /* Cipher implementations starting with index 0 (enum gcry_cipher_algos) */
@@ -207,9 +207,9 @@ static gcry_cipher_spec_t * const cipher_list_algo301[] =
     NULL,
 #endif
 #if USE_SM4
-     &_gcry_cipher_spec_sm4,
+     &_gcry_cipher_spec_sm4
 #else
-    NULL,
+    NULL
 #endif
   };
 
diff --git a/cipher/mac-gmac.c b/cipher/mac-gmac.c
index 12f515eb..5e350010 100644
--- a/cipher/mac-gmac.c
+++ b/cipher/mac-gmac.c
@@ -45,6 +45,8 @@ map_mac_algo_to_cipher (int mac_algo)
       return GCRY_CIPHER_SERPENT128;
     case GCRY_MAC_GMAC_SEED:
       return GCRY_CIPHER_SEED;
+    case GCRY_MAC_GMAC_SM4:
+      return GCRY_CIPHER_SM4;
     }
 }
 
@@ -185,3 +187,9 @@ const gcry_mac_spec_t _gcry_mac_type_spec_gmac_camellia = {
   &gmac_ops
 };
 #endif
+#if USE_SM4
+const gcry_mac_spec_t _gcry_mac_type_spec_gmac_sm4 = {
+  GCRY_MAC_GMAC_SM4, {0, 0}, "GMAC_SM4",
+  &gmac_ops
+};
+#endif
diff --git a/cipher/mac-internal.h b/cipher/mac-internal.h
index 01998152..39876f55 100644
--- a/cipher/mac-internal.h
+++ b/cipher/mac-internal.h
@@ -253,6 +253,9 @@ extern const gcry_mac_spec_t _gcry_mac_type_spec_gmac_seed;
 #if USE_CAMELLIA
 extern const gcry_mac_spec_t _gcry_mac_type_spec_gmac_camellia;
 #endif
+#if USE_SM4
+extern const gcry_mac_spec_t _gcry_mac_type_spec_gmac_sm4;
+#endif
 
 /*
  * The Poly1305 MAC algorithm specifications (mac-poly1305.c).
@@ -273,3 +276,6 @@ extern const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_serpent;
 #if USE_SEED
 extern const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_seed;
 #endif
+#if USE_SM4
+extern const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_sm4;
+#endif
diff --git a/cipher/mac-poly1305.c b/cipher/mac-poly1305.c
index 3abc7774..5b6c489e 100644
--- a/cipher/mac-poly1305.c
+++ b/cipher/mac-poly1305.c
@@ -83,6 +83,9 @@ poly1305mac_open (gcry_mac_hd_t h)
     case GCRY_MAC_POLY1305_SEED:
       cipher_algo = GCRY_CIPHER_SEED;
       break;
+    case GCRY_MAC_POLY1305_SM4:
+      cipher_algo = GCRY_CIPHER_SM4;
+      break;
     }
 
   err = _gcry_cipher_open_internal (&mac_ctx->hd, cipher_algo,
@@ -362,3 +365,9 @@ const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_seed = {
   &poly1305mac_ops
 };
 #endif
+#if USE_SM4
+const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_sm4 = {
+  GCRY_MAC_POLY1305_SM4, {0, 0}, "POLY1305_SM4",
+  &poly1305mac_ops
+};
+#endif
diff --git a/cipher/mac.c b/cipher/mac.c
index ba1eb300..05d2c64c 100644
--- a/cipher/mac.c
+++ b/cipher/mac.c
@@ -132,8 +132,10 @@ static const gcry_mac_spec_t * const mac_list[] = {
   &_gcry_mac_type_spec_poly1305mac,
 #if USE_SM4
   &_gcry_mac_type_spec_cmac_sm4,
+  &_gcry_mac_type_spec_gmac_sm4,
+  &_gcry_mac_type_spec_poly1305mac_sm4,
 #endif
-  NULL,
+  NULL
 };
 
 /* HMAC implementations start with index 101 (enum gcry_mac_algos) */
@@ -242,10 +244,10 @@ static const gcry_mac_spec_t * const mac_list_algo101[] =
 #endif
 #if USE_SHA512
     &_gcry_mac_type_spec_hmac_sha512_256,
-    &_gcry_mac_type_spec_hmac_sha512_224,
+    &_gcry_mac_type_spec_hmac_sha512_224
 #else
     NULL,
-    NULL,
+    NULL
 #endif
   };
 
@@ -338,7 +340,12 @@ static const gcry_mac_spec_t * const mac_list_algo401[] =
     NULL,
 #endif
 #if USE_SEED
-    &_gcry_mac_type_spec_gmac_seed
+    &_gcry_mac_type_spec_gmac_seed,
+#else
+    NULL,
+#endif
+#if USE_SM4
+    &_gcry_mac_type_spec_gmac_sm4
 #else
     NULL
 #endif
@@ -369,7 +376,12 @@ static const gcry_mac_spec_t * const mac_list_algo501[] =
     NULL,
 #endif
 #if USE_SEED
-    &_gcry_mac_type_spec_poly1305mac_seed
+    &_gcry_mac_type_spec_poly1305mac_seed,
+#else
+    NULL,
+#endif
+#if USE_SM4
+    &_gcry_mac_type_spec_poly1305mac_sm4
 #else
     NULL
 #endif
diff --git a/cipher/md.c b/cipher/md.c
index 34336b5c..40a862f6 100644
--- a/cipher/md.c
+++ b/cipher/md.c
@@ -240,10 +240,10 @@ static const gcry_md_spec_t * const digest_list_algo301[] =
 #endif
 #if USE_SHA512
     &_gcry_digest_spec_sha512_256,
-    &_gcry_digest_spec_sha512_224,
+    &_gcry_digest_spec_sha512_224
 #else
     NULL,
-    NULL,
+    NULL
 #endif
   };
 
diff --git a/doc/gcrypt.texi b/doc/gcrypt.texi
index 74615757..db4ad1e6 100644
--- a/doc/gcrypt.texi
+++ b/doc/gcrypt.texi
@@ -4261,6 +4261,10 @@ block cipher algorithm.
 This is GMAC message authentication algorithm based on the SEED
 block cipher algorithm.
 
+ at item GCRY_MAC_GMAC_SM4
+This is GMAC message authentication algorithm based on the SM4
+block cipher algorithm.
+
 @item GCRY_MAC_POLY1305
 This is plain Poly1305 message authentication algorithm, used with
 one-time key.
@@ -4285,6 +4289,10 @@ key and one-time nonce.
 This is Poly1305-SEED message authentication algorithm, used with
 key and one-time nonce.
 
+ at item GCRY_MAC_POLY1305_SM4
+This is Poly1305-SM4 message authentication algorithm, used with
+key and one-time nonce.
+
 @item GCRY_MAC_GOST28147_IMIT
 This is MAC construction defined in GOST 28147-89 (see RFC 5830 Section 8).
 
diff --git a/src/gcrypt.h.in b/src/gcrypt.h.in
index 8451a4ce..47d73339 100644
--- a/src/gcrypt.h.in
+++ b/src/gcrypt.h.in
@@ -1516,13 +1516,15 @@ enum gcry_mac_algos
     GCRY_MAC_GMAC_TWOFISH       = 403,
     GCRY_MAC_GMAC_SERPENT       = 404,
     GCRY_MAC_GMAC_SEED          = 405,
+    GCRY_MAC_GMAC_SM4           = 406,
 
     GCRY_MAC_POLY1305           = 501,
     GCRY_MAC_POLY1305_AES       = 502,
     GCRY_MAC_POLY1305_CAMELLIA  = 503,
     GCRY_MAC_POLY1305_TWOFISH   = 504,
     GCRY_MAC_POLY1305_SERPENT   = 505,
-    GCRY_MAC_POLY1305_SEED      = 506
+    GCRY_MAC_POLY1305_SEED      = 506,
+    GCRY_MAC_POLY1305_SM4       = 507
   };
 
 /* Flags used with the open function.  */
diff --git a/tests/bench-slope.c b/tests/bench-slope.c
index 1cad6813..eb301569 100644
--- a/tests/bench-slope.c
+++ b/tests/bench-slope.c
@@ -2063,6 +2063,7 @@ bench_mac_init (struct bench_obj *obj)
     case GCRY_MAC_POLY1305_TWOFISH:
     case GCRY_MAC_POLY1305_SERPENT:
     case GCRY_MAC_POLY1305_SEED:
+    case GCRY_MAC_POLY1305_SM4:
       gcry_mac_setiv (hd, key, 16);
       break;
     }
diff --git a/tests/benchmark.c b/tests/benchmark.c
index e9223f5a..60abd2cb 100644
--- a/tests/benchmark.c
+++ b/tests/benchmark.c
@@ -652,7 +652,7 @@ mac_bench ( const char *algoname )
   for (i=0; i < bufsize; i++)
     buf[i] = i;
 
-  if (algo >= GCRY_MAC_POLY1305_AES && algo <= GCRY_MAC_POLY1305_SEED)
+  if (algo >= GCRY_MAC_POLY1305_AES && algo <= GCRY_MAC_POLY1305_SM4)
     {
       static const char iv[16] = { 1, 2, 3, 4, };
       err = gcry_mac_setiv(hd, iv, sizeof(iv));
-- 
2.37.2




More information about the Gcrypt-devel mailing list