uncrustify as demanded.
[oweals/gnunet.git] / src / util / perf_crypto_rsa.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2014 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @author Christian Grothoff
23  * @file util/perf_crypto_rsa.c
24  * @brief measure performance of RSA signing
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include <gauger.h>
29
30
31 /**
32  * Evaluate RSA performance.
33  *
34  * @param len keylength to evaluate with
35  */
36 static void
37 eval(unsigned int len)
38 {
39   struct GNUNET_TIME_Absolute start;
40   struct GNUNET_CRYPTO_RsaSignature *sig;
41   struct GNUNET_CRYPTO_RsaSignature *rsig;
42   struct GNUNET_CRYPTO_RsaPublicKey *public_key;
43   struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
44   struct GNUNET_CRYPTO_RsaBlindingKeySecret bsec[10];
45   unsigned int i;
46   char sbuf[128];
47   char *bbuf;
48   size_t bbuf_len;
49   struct GNUNET_HashCode hc;
50
51   start = GNUNET_TIME_absolute_get();
52   for (i = 0; i < 10; i++)
53     {
54       private_key = GNUNET_CRYPTO_rsa_private_key_create(len);
55       GNUNET_CRYPTO_rsa_private_key_free(private_key);
56     }
57   printf("10x %u-key generation took %s\n",
58          len,
59          GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start),
60                                                 GNUNET_YES));
61   GNUNET_snprintf(sbuf,
62                   sizeof(sbuf),
63                   "RSA %u-key generation",
64                   len);
65   GAUGER("UTIL", sbuf,
66          64 * 1024 / (1 +
67                       GNUNET_TIME_absolute_get_duration
68                         (start).rel_value_us / 1000LL), "keys/ms");
69   private_key = GNUNET_CRYPTO_rsa_private_key_create(len);
70   public_key = GNUNET_CRYPTO_rsa_private_key_get_public(private_key);
71   for (i = 0; i < 10; i++)
72     GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK,
73                                &bsec[i], sizeof(bsec[0]));
74   /*
75      start = GNUNET_TIME_absolute_get ();
76      for (i=0;i<10;i++)
77      rsa_blinding_key_derive(public_key, &bsec[i]);
78      printf ("10x %u-blinding key generation took %s\n",
79           len,
80           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
81                                                   GNUNET_YES));
82      GNUNET_snprintf (sbuf,
83                    sizeof (sbuf),
84                    "RSA %u-blinding key generation",
85                    len);
86      GAUGER ("UTIL", sbuf,
87           64 * 1024 / (1 +
88                        GNUNET_TIME_absolute_get_duration
89                        (start).rel_value_us / 1000LL), "keys/ms");
90    */
91   start = GNUNET_TIME_absolute_get();
92   GNUNET_CRYPTO_hash("test", 4, &hc);
93   for (i = 0; i < 10; i++)
94     {
95       GNUNET_CRYPTO_rsa_blind(&hc,
96                               &bsec[i],
97                               public_key,
98                               &bbuf, &bbuf_len);
99       GNUNET_free(bbuf);
100     }
101   printf("10x %u-blinding took %s\n",
102          len,
103          GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start),
104                                                 GNUNET_YES));
105   GNUNET_snprintf(sbuf,
106                   sizeof(sbuf),
107                   "RSA %u-blinding",
108                   len);
109   GAUGER("UTIL",
110          sbuf,
111          64 * 1024 / (1 +
112                       GNUNET_TIME_absolute_get_duration
113                         (start).rel_value_us / 1000LL), "ops/ms");
114   GNUNET_CRYPTO_rsa_blind(&hc,
115                           &bsec[0],
116                           public_key,
117                           &bbuf, &bbuf_len);
118   start = GNUNET_TIME_absolute_get();
119   for (i = 0; i < 10; i++)
120     {
121       sig = GNUNET_CRYPTO_rsa_sign_blinded(private_key,
122                                            bbuf, bbuf_len);
123       GNUNET_CRYPTO_rsa_signature_free(sig);
124     }
125   printf("10x %u-signing took %s\n",
126          len,
127          GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start),
128                                                 GNUNET_YES));
129   GNUNET_snprintf(sbuf,
130                   sizeof(sbuf),
131                   "RSA %u-signing",
132                   len);
133   GAUGER("UTIL",
134          sbuf,
135          64 * 1024 / (1 +
136                       GNUNET_TIME_absolute_get_duration
137                         (start).rel_value_us / 1000LL), "ops/ms");
138   sig = GNUNET_CRYPTO_rsa_sign_blinded(private_key,
139                                        bbuf,
140                                        bbuf_len);
141   start = GNUNET_TIME_absolute_get();
142   for (i = 0; i < 10; i++)
143     {
144       rsig = GNUNET_CRYPTO_rsa_unblind(sig,
145                                        &bsec[0],
146                                        public_key);
147       GNUNET_CRYPTO_rsa_signature_free(rsig);
148     }
149   printf("10x %u-unblinding took %s\n",
150          len,
151          GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start),
152                                                 GNUNET_YES));
153   GNUNET_snprintf(sbuf,
154                   sizeof(sbuf),
155                   "RSA %u-unblinding",
156                   len);
157   GAUGER("UTIL",
158          sbuf,
159          64 * 1024 / (1 +
160                       GNUNET_TIME_absolute_get_duration
161                         (start).rel_value_us / 1000LL), "ops/ms");
162   rsig = GNUNET_CRYPTO_rsa_unblind(sig,
163                                    &bsec[0],
164                                    public_key);
165   start = GNUNET_TIME_absolute_get();
166   for (i = 0; i < 10; i++)
167     {
168       GNUNET_assert(GNUNET_OK ==
169                     GNUNET_CRYPTO_rsa_verify(&hc,
170                                              rsig,
171                                              public_key));
172     }
173   printf("10x %u-verifying took %s\n",
174          len,
175          GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start),
176                                                 GNUNET_YES));
177   GNUNET_snprintf(sbuf,
178                   sizeof(sbuf),
179                   "RSA %u-verification",
180                   len);
181   GAUGER("UTIL",
182          sbuf,
183          64 * 1024 / (1 +
184                       GNUNET_TIME_absolute_get_duration
185                         (start).rel_value_us / 1000LL), "ops/ms");
186   GNUNET_CRYPTO_rsa_signature_free(sig);
187   GNUNET_CRYPTO_rsa_public_key_free(public_key);
188   GNUNET_CRYPTO_rsa_private_key_free(private_key);
189   GNUNET_free(bbuf);
190 }
191
192
193 int
194 main(int argc, char *argv[])
195 {
196   eval(1024);
197   eval(2048);
198   /* eval (4096); */
199   return 0;
200 }
201
202
203 /* end of perf_crypto_rsa.c */