4ad348d5bc13fd138d6d2d6f90fa1825865c697b
[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
19 /**
20  * @author Christian Grothoff
21  * @file util/perf_crypto_rsa.c
22  * @brief measure performance of RSA signing
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include <gauger.h>
27
28
29 /**
30  * Evaluate RSA performance.
31  *
32  * @param len keylength to evaluate with
33  */
34 static void
35 eval (unsigned int len)
36 {
37   struct GNUNET_TIME_Absolute start;
38   struct GNUNET_CRYPTO_RsaSignature *sig;
39   struct GNUNET_CRYPTO_RsaSignature *rsig;
40   struct GNUNET_CRYPTO_RsaPublicKey *public_key;
41   struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
42   struct GNUNET_CRYPTO_RsaBlindingKeySecret bsec[10];
43   unsigned int i;
44   char sbuf[128];
45   char *bbuf;
46   size_t bbuf_len;
47   struct GNUNET_HashCode hc;
48
49   start = GNUNET_TIME_absolute_get ();
50   for (i=0;i<10;i++)
51   {
52     private_key = GNUNET_CRYPTO_rsa_private_key_create (len);
53     GNUNET_CRYPTO_rsa_private_key_free (private_key);
54   }
55   printf ("10x %u-key generation took %s\n",
56           len,
57           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
58                                                   GNUNET_YES));
59   GNUNET_snprintf (sbuf,
60                    sizeof (sbuf),
61                    "RSA %u-key generation",
62                    len);
63   GAUGER ("UTIL", sbuf,
64           64 * 1024 / (1 +
65                        GNUNET_TIME_absolute_get_duration
66                        (start).rel_value_us / 1000LL), "keys/ms");
67   private_key = GNUNET_CRYPTO_rsa_private_key_create (len);
68   public_key = GNUNET_CRYPTO_rsa_private_key_get_public (private_key);
69   for (i=0;i<10;i++)
70     GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
71                                 &bsec[i], sizeof (bsec[0]));
72   /*
73   start = GNUNET_TIME_absolute_get ();
74   for (i=0;i<10;i++)
75     rsa_blinding_key_derive(public_key, &bsec[i]);
76   printf ("10x %u-blinding key generation took %s\n",
77           len,
78           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
79                                                   GNUNET_YES));
80   GNUNET_snprintf (sbuf,
81                    sizeof (sbuf),
82                    "RSA %u-blinding key generation",
83                    len);
84   GAUGER ("UTIL", sbuf,
85           64 * 1024 / (1 +
86                        GNUNET_TIME_absolute_get_duration
87                        (start).rel_value_us / 1000LL), "keys/ms");
88   */
89   start = GNUNET_TIME_absolute_get ();
90   GNUNET_CRYPTO_hash ("test", 4, &hc);
91   for (i=0;i<10;i++)
92   {
93     GNUNET_CRYPTO_rsa_blind (&hc,
94                              &bsec[i],
95                              public_key,
96                              &bbuf, &bbuf_len);
97     GNUNET_free (bbuf);
98   }
99   printf ("10x %u-blinding took %s\n",
100           len,
101           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
102                                                   GNUNET_YES));
103   GNUNET_snprintf (sbuf,
104                    sizeof (sbuf),
105                    "RSA %u-blinding",
106                    len);
107   GAUGER ("UTIL",
108           sbuf,
109           64 * 1024 / (1 +
110                        GNUNET_TIME_absolute_get_duration
111                        (start).rel_value_us / 1000LL), "ops/ms");
112   GNUNET_CRYPTO_rsa_blind (&hc,
113                            &bsec[0],
114                            public_key,
115                            &bbuf, &bbuf_len);
116   start = GNUNET_TIME_absolute_get ();
117   for (i=0;i<10;i++)
118   {
119     sig = GNUNET_CRYPTO_rsa_sign_blinded (private_key,
120                                           bbuf, bbuf_len);
121     GNUNET_CRYPTO_rsa_signature_free (sig);
122   }
123   printf ("10x %u-signing took %s\n",
124           len,
125           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
126                                                   GNUNET_YES));
127   GNUNET_snprintf (sbuf,
128                    sizeof (sbuf),
129                    "RSA %u-signing",
130                    len);
131   GAUGER ("UTIL",
132           sbuf,
133           64 * 1024 / (1 +
134                        GNUNET_TIME_absolute_get_duration
135                        (start).rel_value_us / 1000LL), "ops/ms");
136   sig = GNUNET_CRYPTO_rsa_sign_blinded (private_key,
137                                         bbuf,
138                                         bbuf_len);
139   start = GNUNET_TIME_absolute_get ();
140   for (i=0;i<10;i++)
141   {
142     rsig = GNUNET_CRYPTO_rsa_unblind (sig,
143                                       &bsec[0],
144                                       public_key);
145     GNUNET_CRYPTO_rsa_signature_free (rsig);
146   }
147   printf ("10x %u-unblinding took %s\n",
148           len,
149           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
150                                                   GNUNET_YES));
151   GNUNET_snprintf (sbuf,
152                    sizeof (sbuf),
153                    "RSA %u-unblinding",
154                    len);
155   GAUGER ("UTIL",
156           sbuf,
157           64 * 1024 / (1 +
158                        GNUNET_TIME_absolute_get_duration
159                        (start).rel_value_us / 1000LL), "ops/ms");
160   rsig = GNUNET_CRYPTO_rsa_unblind (sig,
161                                     &bsec[0],
162                                     public_key);
163   start = GNUNET_TIME_absolute_get ();
164   for (i=0;i<10;i++)
165   {
166     GNUNET_assert (GNUNET_OK ==
167                    GNUNET_CRYPTO_rsa_verify (&hc,
168                                              rsig,
169                                              public_key));
170   }
171   printf ("10x %u-verifying took %s\n",
172           len,
173           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
174                                                   GNUNET_YES));
175   GNUNET_snprintf (sbuf,
176                    sizeof (sbuf),
177                    "RSA %u-verification",
178                    len);
179   GAUGER ("UTIL",
180           sbuf,
181           64 * 1024 / (1 +
182                        GNUNET_TIME_absolute_get_duration
183                        (start).rel_value_us / 1000LL), "ops/ms");
184   GNUNET_CRYPTO_rsa_signature_free (sig);
185   GNUNET_CRYPTO_rsa_public_key_free (public_key);
186   GNUNET_CRYPTO_rsa_private_key_free (private_key);
187   GNUNET_free (bbuf);
188 }
189
190
191 int
192 main (int argc, char *argv[])
193 {
194   eval (1024); 
195   /* eval (2048); */
196   /* eval (4096); */
197   return 0;
198 }
199
200
201 /* end of perf_crypto_rsa.c */