Fix speed sm2 bug
authorChenglong Zhang <auvkone@gmail.com>
Wed, 10 Feb 2021 09:52:29 +0000 (17:52 +0800)
committerTomas Mraz <tomas@openssl.org>
Fri, 19 Feb 2021 12:55:51 +0000 (13:55 +0100)
Should create PKEY CTX with EVP_PKEY_SM2;
each job should have its own sm2_pkey;
loopargs[i].sigsize should be set after EVP_DigestSign().

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14144)

apps/speed.c

index c41fca483fb1d1b6faa6f04cf40747782aaf33cf..e867448015992bcc6935fe776d41c7f3391ede83 100644 (file)
@@ -1283,12 +1283,14 @@ static int SM2_sign_loop(void *args)
     unsigned char *buf = tempargs->buf;
     EVP_MD_CTX **sm2ctx = tempargs->sm2_ctx;
     unsigned char *sm2sig = tempargs->buf2;
-    size_t sm2sigsize = tempargs->sigsize;
-    const size_t max_size = tempargs->sigsize;
+    size_t sm2sigsize;
     int ret, count;
     EVP_PKEY **sm2_pkey = tempargs->sm2_pkey;
+    const size_t max_size = EVP_PKEY_size(sm2_pkey[testnum]);
 
     for (count = 0; COND(sm2_c[testnum][0]); count++) {
+        sm2sigsize = max_size;
+
         if (!EVP_DigestSignInit(sm2ctx[testnum], NULL, EVP_sm3(),
                                 NULL, sm2_pkey[testnum])) {
             BIO_printf(bio_err, "SM2 init sign failure\n");
@@ -1306,7 +1308,6 @@ static int SM2_sign_loop(void *args)
         }
         /* update the latest returned size and always use the fixed buffer size */
         tempargs->sigsize = sm2sigsize;
-        sm2sigsize = max_size;
     }
 
     return count;
@@ -3567,8 +3568,9 @@ int speed_main(int argc, char **argv)
                     || loopargs[i].sm2_vfy_ctx[testnum] == NULL)
                 break;
 
-            /* SM2 keys are generated as normal EC keys with a special curve */
-            st = !((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) == NULL
+            sm2_pkey = NULL;
+
+            st = !((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SM2, NULL)) == NULL
                 || EVP_PKEY_keygen_init(pctx) <= 0
                 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
                     sm2_curves[testnum].nid) <= 0
@@ -3615,11 +3617,9 @@ int speed_main(int argc, char **argv)
             op_count = 1;
         } else {
             for (i = 0; i < loopargs_len; i++) {
-                size_t sm2_sigsize = loopargs[i].sigsize;
-
                 /* Perform SM2 signature test */
                 st = EVP_DigestSign(loopargs[i].sm2_ctx[testnum],
-                                    loopargs[i].buf2, &sm2_sigsize,
+                                    loopargs[i].buf2, &loopargs[i].sigsize,
                                     loopargs[i].buf, 20);
                 if (st == 0)
                     break;