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