]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - benchmark/sign.c
As "openssl speed" does not support GOST parameters,
[openssl-gost/engine.git] / benchmark / sign.c
1 /**********************************************************************
2  *             Copyright (c) 2018 Cryptocom LTD                       *
3  *       This file is distributed under the same license as OpenSSL   *
4  **********************************************************************/
5 #include <stdio.h>
6 #include <string.h>
7 #include <time.h>
8 #include <getopt.h>
9 #include <openssl/rand.h>
10 #include <openssl/conf.h>
11 #include <openssl/err.h>
12 #include <openssl/evp.h>
13 #include <openssl/pem.h>
14 #include <openssl/engine.h>
15
16 static EVP_PKEY *create_key(char *algname, char *param)
17 {
18         EVP_PKEY *key1 = EVP_PKEY_new(), *newkey = NULL;
19         EVP_PKEY_CTX *ctx = NULL;
20
21         if(EVP_PKEY_set_type_str(key1, algname, strlen(algname)) <= 0)
22         {
23                 goto err;
24         }
25         if(!(ctx = EVP_PKEY_CTX_new(key1, NULL)))
26         {
27                 goto err;
28         }
29         EVP_PKEY_keygen_init(ctx);
30         if(ERR_peek_last_error())
31         {
32                 goto err;
33         }
34         if(EVP_PKEY_CTX_ctrl_str(ctx, "paramset", param) <= 0)
35         {
36                 goto err;
37         }
38         if(EVP_PKEY_keygen(ctx, &newkey) <= 0)
39         {
40                 goto err;
41         }
42 err:
43         if(ctx)
44                 EVP_PKEY_CTX_free(ctx);
45         EVP_PKEY_free(key1);
46         return newkey;
47 }
48
49 void usage(char *name)
50 {
51         fprintf(stderr, "usage: %s [-l data_len] [-c cycles]\n", name);
52         exit(1);
53 }
54
55 int main(int argc, char **argv)
56 {
57         unsigned char *data;
58         const EVP_MD *mdtype;
59         EVP_MD_CTX md_ctx;
60         int siglen;
61         unsigned char *sigbuf;
62         EVP_PKEY *pkey;
63         unsigned int compter;
64         time_t debut, fin;
65         unsigned int data_len = 1024;
66         unsigned int cycles = 8000;
67         int option;
68         opterr = 0;
69         while((option = getopt(argc, argv, "l:c:")) >= 0)
70         {
71                 if(option == ':') option = optopt;
72                 if(optarg && (optarg[0] == '-')) { optind--; optarg = NULL; }
73                 switch (option)
74                 {
75                         case 'l':
76                                 data_len = atoi(optarg);
77                                 break;
78                         case 'c':
79                                 cycles = atoi(optarg);
80                                 break;
81                         default:
82                                 usage(argv[0]);
83                                 break;
84                 }
85         }
86         if (optind < argc) usage(argv[0]);
87         OPENSSL_config(NULL);
88         ERR_load_crypto_strings();
89         OpenSSL_add_all_algorithms();
90         mdtype = EVP_get_digestbyname("md_gost12_256");
91         pkey = create_key("gost2012_256", "A");
92         data = (unsigned char *) malloc(data_len);
93         printf("wait"); fflush(stdout);
94         siglen = EVP_PKEY_size(pkey);
95         sigbuf = malloc(siglen);
96         debut = time(NULL);
97         for(compter = 0; compter < cycles; compter++)
98         {
99                 EVP_SignInit(&md_ctx, mdtype);
100                 EVP_SignUpdate(&md_ctx, data, data_len);
101                 EVP_SignFinal(&md_ctx, sigbuf, (unsigned int *) &siglen, pkey);
102                 EVP_MD_CTX_cleanup(&md_ctx);
103         }
104         fin = time(NULL);
105         printf("\b\b\b\b");
106         if ((fin - debut) < 3) { printf("cycles too low\n"); exit(1); }
107         printf("sign: %d/s\n", compter / (unsigned int)(fin - debut));
108         EVP_PKEY_free(pkey);
109         free(sigbuf);
110         free(data);
111         exit(0);
112 }