[PATCH 2/2] tests: Add basic test cases for sm2

Tianjia Zhang tianjia.zhang at linux.alibaba.com
Mon Jan 20 04:42:25 CET 2020


* tests/basic.c (check_pubkey): Add test cases for ecc-sm2.
--

Signed-off-by: Tianjia Zhang <tianjia.zhang at linux.alibaba.com>
---
 tests/basic.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 49 insertions(+), 3 deletions(-)

diff --git a/tests/basic.c b/tests/basic.c
index 2a41fb82..82209b1b 100644
--- a/tests/basic.c
+++ b/tests/basic.c
@@ -12594,6 +12594,16 @@ check_pubkey_sign_ecdsa (int n, gcry_sexp_t skey, gcry_sexp_t pkey)
         /* */    "000102030405060708090A0B0C0D0E0F#))",
         0
       },
+      { 256,
+        "(data (flags sm2)\n"
+        " (hash sm3 #112233445566778899AABBCCDDEEFF00"
+        /* */       "123456789ABCDEF0123456789ABCDEF0#))",
+        0,
+        "(data (flags sm2)\n"
+        " (hash sm3 #B524F552CD82B8B028476E005C377FB1"
+        /* */       "9A87E6FC682D48BB5D42E3D9B9EFFE76#))",
+        0
+      },
       { 0, NULL }
     };
 
@@ -12638,7 +12648,8 @@ check_pubkey_sign_ecdsa (int n, gcry_sexp_t skey, gcry_sexp_t pkey)
 
 
 static void
-check_pubkey_crypt (int n, gcry_sexp_t skey, gcry_sexp_t pkey, int algo)
+check_pubkey_crypt (int n, gcry_sexp_t skey, gcry_sexp_t pkey, int algo,
+                    int exact_match)
 {
   gcry_error_t rc;
   gcry_sexp_t plain = NULL;
@@ -12751,6 +12762,13 @@ check_pubkey_crypt (int n, gcry_sexp_t skey, gcry_sexp_t pkey, int algo)
 	NULL,
 	0,
 	GPG_ERR_CONFLICT },
+      { GCRY_PK_ECDSA,
+        "(data\n (flags sm2)\n (hash-algo sm3)\n"
+        " (value #1234567890ABCDEF1234567890ABCDEF#))\n",
+        NULL,
+        1,
+        0,
+        0 },
       { 0, NULL }
     };
 
@@ -12758,7 +12776,7 @@ check_pubkey_crypt (int n, gcry_sexp_t skey, gcry_sexp_t pkey, int algo)
 
   for (dataidx = 0; datas[dataidx].data; dataidx++)
     {
-      if (datas[dataidx].algo && datas[dataidx].algo != algo)
+      if ((datas[dataidx].algo || exact_match) && datas[dataidx].algo != algo)
 	continue;
 
       if (verbose)
@@ -12908,7 +12926,12 @@ do_check_one_pubkey (int n, gcry_sexp_t skey, gcry_sexp_t pkey,
        check_pubkey_sign (n, skey, pkey, algo);
    }
  if (flags & FLAG_CRYPT)
-   check_pubkey_crypt (n, skey, pkey, algo);
+   {
+     if (algo == GCRY_PK_ECDSA)
+       check_pubkey_crypt (n, skey, pkey, algo, 1);
+     else
+       check_pubkey_crypt (n, skey, pkey, algo, 0);
+   }
  if (grip && (flags & FLAG_GRIP))
    check_pubkey_grip (n, grip, skey, pkey, algo);
 }
@@ -13270,6 +13293,29 @@ check_pubkey (void)
       "      F1050E2E8FF49C85C23CBE7DED0E7CE6A594896B8F62888F"
       "      DBC5C8821305E2EA42BF01E37300116281#)))\n",
 
+      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
+      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" }
+  },
+  { /* sm2 test */
+    GCRY_PK_ECDSA, FLAG_CRYPT | FLAG_SIGN,
+    {
+      "(private-key\n"
+      " (ecc\n"
+      "  (curve sm2p256v1)\n"
+      "  (q #04"
+      "      8759389A34AAAD07ECF4E0C8C2650A4459C8D926EE2378324E0261C52538CB47"
+      "      7528106B1E0B7C8DD5FF29A9C86A89065656EB33154BC0556091EF8AC9D17D78#)"
+      "  (d #41EBDBA9C98CBECCE7249CF18BFD427FF8EA0B2FAB7B9D305D9D9BF4DB6ADFC2#)"
+      "))",
+
+      "(public-key\n"
+      " (ecc\n"
+      "  (curve sm2p256v1)\n"
+      "  (q #04"
+      "      8759389A34AAAD07ECF4E0C8C2650A4459C8D926EE2378324E0261C52538CB47"
+      "      7528106B1E0B7C8DD5FF29A9C86A89065656EB33154BC0556091EF8AC9D17D78#)"
+      "))",
+
       "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
       "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" }
     }
-- 
2.17.1




More information about the Gcrypt-devel mailing list