-fix (C) notices
[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
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
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_rsa_Signature *sig;
41   struct GNUNET_CRYPTO_rsa_Signature *rsig;
42   struct GNUNET_CRYPTO_rsa_PublicKey *public_key;
43   struct GNUNET_CRYPTO_rsa_PrivateKey *private_key;
44   struct GNUNET_CRYPTO_rsa_BlindingKey *bkey;
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   start = GNUNET_TIME_absolute_get ();
72   for (i=0;i<10;i++)
73   {
74     bkey = GNUNET_CRYPTO_rsa_blinding_key_create (len);
75     GNUNET_CRYPTO_rsa_blinding_key_free (bkey);
76   }
77   printf ("10x %u-blinding key generation took %s\n",
78           len,
79           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
80                                                   GNUNET_YES));
81   GNUNET_snprintf (sbuf,
82                    sizeof (sbuf),
83                    "RSA %u-blinding key generation",
84                    len);
85   GAUGER ("UTIL", sbuf,
86           64 * 1024 / (1 +
87                        GNUNET_TIME_absolute_get_duration
88                        (start).rel_value_us / 1000LL), "keys/ms");
89   bkey = GNUNET_CRYPTO_rsa_blinding_key_create (len);
90   start = GNUNET_TIME_absolute_get ();
91   GNUNET_CRYPTO_hash ("test", 4, &hc);
92   for (i=0;i<10;i++)
93   {
94     bbuf_len = GNUNET_CRYPTO_rsa_blind (&hc,
95                                         bkey,
96                                         public_key,
97                                         &bbuf);
98     GNUNET_free (bbuf);
99   }
100   printf ("10x %u-blinding took %s\n",
101           len,
102           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
103                                                   GNUNET_YES));
104   GNUNET_snprintf (sbuf,
105                    sizeof (sbuf),
106                    "RSA %u-blinding",
107                    len);
108   GAUGER ("UTIL",
109           sbuf,
110           64 * 1024 / (1 +
111                        GNUNET_TIME_absolute_get_duration
112                        (start).rel_value_us / 1000LL), "ops/ms");
113   bbuf_len = GNUNET_CRYPTO_rsa_blind (&hc,
114                                       bkey,
115                                       public_key,
116                                       &bbuf);
117   start = GNUNET_TIME_absolute_get ();
118   for (i=0;i<10;i++)
119   {
120     sig = GNUNET_CRYPTO_rsa_sign (private_key,
121                                   bbuf,
122                                   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 (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                                       bkey,
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                                     bkey,
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_CRYPTO_rsa_blinding_key_free (bkey);
190   GNUNET_free (bbuf);
191 }
192
193
194 int
195 main (int argc, char *argv[])
196 {
197   eval (1024);
198   eval (2048);
199   /* eval (4096); */
200   return 0;
201 }
202
203
204 /* end of perf_crypto_rsa.c */