use NULL value in load_path_suffix to NOT load any files
[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 (
60             GNUNET_TIME_absolute_get_duration (start),
61             GNUNET_YES));
62   GNUNET_snprintf (sbuf,
63                    sizeof(sbuf),
64                    "RSA %u-key generation",
65                    len);
66   GAUGER ("UTIL", sbuf,
67           64 * 1024 / (1
68                        + GNUNET_TIME_absolute_get_duration
69                          (start).rel_value_us / 1000LL), "keys/ms");
70   private_key = GNUNET_CRYPTO_rsa_private_key_create (len);
71   public_key = GNUNET_CRYPTO_rsa_private_key_get_public (private_key);
72   for (i = 0; i < 10; i++)
73     GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
74                                 &bsec[i], sizeof(bsec[0]));
75   /*
76      start = GNUNET_TIME_absolute_get ();
77      for (i=0;i<10;i++)
78      rsa_blinding_key_derive(public_key, &bsec[i]);
79      printf ("10x %u-blinding key generation took %s\n",
80           len,
81           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
82                                                   GNUNET_YES));
83      GNUNET_snprintf (sbuf,
84                    sizeof (sbuf),
85                    "RSA %u-blinding key generation",
86                    len);
87      GAUGER ("UTIL", sbuf,
88           64 * 1024 / (1 +
89                        GNUNET_TIME_absolute_get_duration
90                        (start).rel_value_us / 1000LL), "keys/ms");
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 (
104             GNUNET_TIME_absolute_get_duration (start),
105             GNUNET_YES));
106   GNUNET_snprintf (sbuf,
107                    sizeof(sbuf),
108                    "RSA %u-blinding",
109                    len);
110   GAUGER ("UTIL",
111           sbuf,
112           64 * 1024 / (1
113                        + GNUNET_TIME_absolute_get_duration
114                          (start).rel_value_us / 1000LL), "ops/ms");
115   GNUNET_CRYPTO_rsa_blind (&hc,
116                            &bsec[0],
117                            public_key,
118                            &bbuf, &bbuf_len);
119   start = GNUNET_TIME_absolute_get ();
120   for (i = 0; i < 10; i++)
121   {
122     sig = GNUNET_CRYPTO_rsa_sign_blinded (private_key,
123                                           bbuf, bbuf_len);
124     GNUNET_CRYPTO_rsa_signature_free (sig);
125   }
126   printf ("10x %u-signing took %s\n",
127           len,
128           GNUNET_STRINGS_relative_time_to_string (
129             GNUNET_TIME_absolute_get_duration (start),
130             GNUNET_YES));
131   GNUNET_snprintf (sbuf,
132                    sizeof(sbuf),
133                    "RSA %u-signing",
134                    len);
135   GAUGER ("UTIL",
136           sbuf,
137           64 * 1024 / (1
138                        + GNUNET_TIME_absolute_get_duration
139                          (start).rel_value_us / 1000LL), "ops/ms");
140   sig = GNUNET_CRYPTO_rsa_sign_blinded (private_key,
141                                         bbuf,
142                                         bbuf_len);
143   start = GNUNET_TIME_absolute_get ();
144   for (i = 0; i < 10; i++)
145   {
146     rsig = GNUNET_CRYPTO_rsa_unblind (sig,
147                                       &bsec[0],
148                                       public_key);
149     GNUNET_CRYPTO_rsa_signature_free (rsig);
150   }
151   printf ("10x %u-unblinding took %s\n",
152           len,
153           GNUNET_STRINGS_relative_time_to_string (
154             GNUNET_TIME_absolute_get_duration (start),
155             GNUNET_YES));
156   GNUNET_snprintf (sbuf,
157                    sizeof(sbuf),
158                    "RSA %u-unblinding",
159                    len);
160   GAUGER ("UTIL",
161           sbuf,
162           64 * 1024 / (1
163                        + GNUNET_TIME_absolute_get_duration
164                          (start).rel_value_us / 1000LL), "ops/ms");
165   rsig = GNUNET_CRYPTO_rsa_unblind (sig,
166                                     &bsec[0],
167                                     public_key);
168   start = GNUNET_TIME_absolute_get ();
169   for (i = 0; i < 10; i++)
170   {
171     GNUNET_assert (GNUNET_OK ==
172                    GNUNET_CRYPTO_rsa_verify (&hc,
173                                              rsig,
174                                              public_key));
175   }
176   printf ("10x %u-verifying took %s\n",
177           len,
178           GNUNET_STRINGS_relative_time_to_string (
179             GNUNET_TIME_absolute_get_duration (start),
180             GNUNET_YES));
181   GNUNET_snprintf (sbuf,
182                    sizeof(sbuf),
183                    "RSA %u-verification",
184                    len);
185   GAUGER ("UTIL",
186           sbuf,
187           64 * 1024 / (1
188                        + GNUNET_TIME_absolute_get_duration
189                          (start).rel_value_us / 1000LL), "ops/ms");
190   GNUNET_CRYPTO_rsa_signature_free (sig);
191   GNUNET_CRYPTO_rsa_public_key_free (public_key);
192   GNUNET_CRYPTO_rsa_private_key_free (private_key);
193   GNUNET_free (bbuf);
194 }
195
196
197 int
198 main (int argc, char *argv[])
199 {
200   eval (1024);
201   eval (2048);
202   /* eval (4096); */
203   return 0;
204 }
205
206
207 /* end of perf_crypto_rsa.c */