]> wagner.pp.ru Git - openssl-gost/engine.git/commitdiff
Don't recreate GOST EC groups every time (#358)
authorDmitry Belyavskiy <beldmit@users.noreply.github.com>
Tue, 28 Sep 2021 14:39:39 +0000 (17:39 +0300)
committerGitHub <noreply@github.com>
Tue, 28 Sep 2021 14:39:39 +0000 (16:39 +0200)
* Don't recreate GOST EC groups every time

For multiple keygen it saves 6% time

gost_ec_sign.c
gost_eng.c
gost_lcl.h
gost_params.c

index e4800a461d1452d77991619010567fbef6ad3875..47996e1666a4d3b4534d0db064dec59d07018582 100644 (file)
@@ -58,6 +58,27 @@ static R3410_ec_params *gost_nid2params(int nid)
     return NULL;
 }
 
+void free_cached_groups()
+{
+    R3410_ec_params *params;
+
+    /* Search nid in 2012 paramset */
+    params = R3410_2012_512_paramset;
+    while (params->nid != NID_undef) {
+       EC_GROUP_free(params->group);
+       params->group = NULL;
+        params++;
+    }
+
+    /* Search nid in 2001 paramset */
+    params = R3410_2001_paramset;
+    while (params->nid != NID_undef) {
+       EC_GROUP_free(params->group);
+       params->group = NULL;
+        params++;
+    }
+}
+
 /*
  * Fills EC_KEY structure hidden in the app_data field of DSA structure
  * with parameter information, extracted from parameter array in
@@ -73,7 +94,7 @@ int fill_GOST_EC_params(EC_KEY *eckey, int nid)
     EC_POINT *P = NULL;
     BIGNUM *p = NULL, *q = NULL, *a = NULL, *b = NULL, *x = NULL, *y =
         NULL, *cofactor = NULL;
-    BN_CTX *ctx;
+    BN_CTX *ctx = NULL;
     int ok = 0;
 
     if (!eckey || !params) {
@@ -81,6 +102,15 @@ int fill_GOST_EC_params(EC_KEY *eckey, int nid)
         return 0;
     }
 
+    if (params->group) {
+        EC_GROUP_set_curve_name(params->group, nid);
+        if (!EC_KEY_set_group(eckey, params->group)) {
+            GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
+            goto end;
+        }
+        return 1;
+    }
+
     if (!(ctx = BN_CTX_new())) {
         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
         return 0;
@@ -132,16 +162,14 @@ int fill_GOST_EC_params(EC_KEY *eckey, int nid)
         goto end;
     }
     EC_GROUP_set_curve_name(grp, nid);
+    params->group = grp;
     if (!EC_KEY_set_group(eckey, grp)) {
         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
         goto end;
     }
     ok = 1;
  end:
-    if (P)
-        EC_POINT_free(P);
-    if (grp)
-        EC_GROUP_free(grp);
+    EC_POINT_free(P);
     BN_CTX_end(ctx);
     BN_CTX_free(ctx);
     return ok;
index d2371d297640d3f82d53a339a7838f9b1003fc5d..92af15ed72daabf09114ca8da297a09f3f453fe3 100644 (file)
@@ -310,6 +310,8 @@ static int gost_engine_destroy(ENGINE* e) {
         *minfo->ameth = NULL;
     }
 
+    free_cached_groups();
+
     ERR_unload_GOST_strings();
 
     return 1;
index c3015c9e439f6a970baae464fba3c084bb5f8fc1..c518ed0333e92a981f3a4c08ace9110d201714f9 100644 (file)
@@ -36,11 +36,14 @@ typedef struct R3410_ec {
     char *x;
     char *y;
     char *cofactor;
+    EC_GROUP *group;
 } R3410_ec_params;
 
 extern R3410_ec_params R3410_2001_paramset[],
     *R3410_2012_256_paramset, R3410_2012_512_paramset[];
 
+void free_cached_groups(void);
+
 extern const ENGINE_CMD_DEFN gost_cmds[];
 int gost_control_func(ENGINE *e, int cmd, long i, void *p, void (*f) (void));
 const char *get_gost_engine_param(int param);
index 9f9806e1962b1890d513c690cdc101c5aea5c250..5b00221f1a77f1c1bec0821eca2958c61f1fed53 100644 (file)
@@ -20,7 +20,7 @@ R3410_ec_params R3410_2001_paramset[] = {
      "8000000000000000000000000000000150FE8A1892976154C59CFC193ACCF5B3",
      "2",
      "08E2A8A0E65147D4BD6316030E16D19C85C97F0A9CA267122B96ABBCEA7E8FC8",
-     "1"}
+     "1", NULL}
     ,
     /*
      * 1.2.643.2.2.35.1
@@ -32,7 +32,7 @@ R3410_ec_params R3410_2001_paramset[] = {
      "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF6C611070995AD10045841B09B761B893",
      "1",
      "8D91E471E0989CDA27DF505A453F2B7635294F2DDF23E3B122ACC99C9E9F1E14",
-     "1"}
+     "1", NULL}
     ,
     /*
      * 1.2.643.2.2.35.2
@@ -44,7 +44,7 @@ R3410_ec_params R3410_2001_paramset[] = {
      "800000000000000000000000000000015F700CFFF1A624E5E497161BCC8A198F",
      "1",
      "3FA8124359F96680B83D1C3EB2C070E5C545C9858D03ECFB744BF8D717717EFC",
-     "1"}
+     "1", NULL}
     ,
     /*
      * 1.2.643.2.2.35.3
@@ -56,7 +56,7 @@ R3410_ec_params R3410_2001_paramset[] = {
      "9B9F605F5A858107AB1EC85E6B41C8AA582CA3511EDDFB74F02F3A6598980BB9",
      "0",
      "41ECE55743711A8C3CBF3783CD08C0EE4D4DC440D4641A8F366E550DFDB3BB67",
-     "1"}
+     "1", NULL}
     ,
     /*
      * 1.2.643.2.2.36.0
@@ -68,7 +68,7 @@ R3410_ec_params R3410_2001_paramset[] = {
      "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF6C611070995AD10045841B09B761B893",
      "1",
      "8D91E471E0989CDA27DF505A453F2B7635294F2DDF23E3B122ACC99C9E9F1E14",
-     "1"}
+     "1", NULL}
     ,
     /*
      * 1.2.643.2.2.36.1
@@ -80,7 +80,7 @@ R3410_ec_params R3410_2001_paramset[] = {
      "9B9F605F5A858107AB1EC85E6B41C8AA582CA3511EDDFB74F02F3A6598980BB9",
      "0",
      "41ECE55743711A8C3CBF3783CD08C0EE4D4DC440D4641A8F366E550DFDB3BB67",
-     "1"}
+     "1", NULL}
     ,
     /*
      * 1.2.643.7.1.2.1.1.1 Edvards
@@ -99,9 +99,9 @@ R3410_ec_params R3410_2001_paramset[] = {
      /* y */
      "32879423AB1A0375895786C4BB46E9565FDE0B5344766740AF268ADB32322E5C",
      /* cofactor */
-     "4"}
+     "4", NULL}
     ,
-    {NID_undef, NULL, NULL, NULL, NULL, NULL, NULL, NULL}
+    {NID_undef, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}
 };
 
 /* Parameters of GOST 34.10-2012 */
@@ -127,7 +127,7 @@ R3410_ec_params R3410_2012_512_paramset[] = {
      /* y */
      "2BB312A43BD2CE6E0D020613C857ACDDCFBF061E91E5F2C3F32447C259F39B2C"
      "83AB156D77F1496BF7EB3351E1EE4E43DC1A18B91B24640B6DBB92CB1ADD371E",
-     "1"}
+     "1", NULL}
     ,
     {NID_id_tc26_gost_3410_2012_512_paramSetA,
      /* a */
@@ -147,7 +147,7 @@ R3410_ec_params R3410_2012_512_paramset[] = {
      /* y */
      "7503CFE87A836AE3A61B8816E25450E6CE5E1C93ACF1ABC1778064FDCBEFA921DF16"
      "26BE4FD036E93D75E6A50E3A41E98028FE5FC235F5B889A589CB5215F2A4",
-     "1"}
+     "1", NULL}
     ,
     {NID_id_tc26_gost_3410_2012_512_paramSetB,
      /* a */
@@ -167,7 +167,7 @@ R3410_ec_params R3410_2012_512_paramset[] = {
      /* y */
      "1A8F7EDA389B094C2C071E3647A8940F3C123B697578C213BE6DD9E6C8EC7335"
      "DCB228FD1EDF4A39152CBCAAF8C0398828041055F94CEEEC7E21340780FE41BD",
-     "1"}
+     "1", NULL}
     ,
     {NID_id_tc26_gost_3410_2012_512_paramSetC,
      /* a */
@@ -188,7 +188,7 @@ R3410_ec_params R3410_2012_512_paramset[] = {
      /* y */
      "F5CE40D95B5EB899ABBCCFF5911CB8577939804D6527378B8C108C3D2090FF9B"
      "E18E2D33E3021ED2EF32D85822423B6304F726AA854BAE07D0396E9A9ADDC40F",
-     "4"}
+     "4", NULL}
     ,
-    {NID_undef, NULL, NULL, NULL, NULL, NULL, NULL, NULL}
+    {NID_undef, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}
 };