1/* benchmark.c - for libgcrypt
2 * Copyright (C) 2002, 2004, 2005, 2006, 2008 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23#include <stdio.h>
24#include <stdlib.h>
25#include <time.h>
26#include <stdarg.h>
27#ifdef _WIN32
28#include <windows.h>
29#else
30#include <sys/times.h>
31#endif
32
33#ifdef _GCRYPT_IN_LIBGCRYPT
34# include "../src/gcrypt.h"
35# include "../compat/libcompat.h"
36#else
37# include <gcrypt.h>
38#endif
39
40
41#define PGM "benchmark"
42
43static int verbose;
44
45/* Do encryption tests with large buffers.  */
46static int large_buffers;
47
48/* Number of cipher repetitions.  */
49static int cipher_repetitions;
50
51/* Number of hash repetitions.  */
52static int hash_repetitions;
53
54/* Alignment of the buffers.  */
55static int buffer_alignment;
56
57/* Whether to include the keysetup in the cipher timings.  */
58static int cipher_with_keysetup;
59
60/* Whether fips mode was active at startup.  */
61static int in_fips_mode;
62
63
64static const char sample_private_dsa_key_1024[] =
65"(private-key\n"
66"  (dsa\n"
67"   (p #00A126202D592214C5A8F6016E2C3F4256052ACB1CB17D88E64B1293FAF08F5E4685"
68       "03E6F68366B326A56284370EB2103E92D8346A163E44A08FDC422AC8E9E44268557A"
69       "853539A6AF39353A59CE5E78FD98B57D0F3E3A7EBC8A256AC9A775BA59689F3004BF"
70       "C3035730C4C0C51626C5D7F5852637EC589BB29DAB46C161572E4B#)\n"
71"   (q #00DEB5A296421887179ECA1762884DE2AF8185AFC5#)\n"
72"   (g #3958B34AE7747194ECBD312F8FEE8CBE3918E94DF9FD11E2912E56318F33BDC38622"
73       "B18DDFF393074BCA8BAACF50DF27AEE529F3E8AEECE55C398DAB3A5E04C2EA142312"
74       "FACA2FE7F0A88884F8DAC3979EE67598F9A383B2A2325F035C796F352A5C3CDF2CB3"
75       "85AD24EC52A6E55247E1BB37D260F79E617D2A4446415B6AD79A#)\n"
76"   (y #519E9FE9AB0545A6724E74603B7B04E48DC1437E0284A11EA605A7BA8AB1CF354FD4"
77       "ECC93880AC293391C69B558AD84E7AAFA88F11D028CF3A378F241D6B056A90C588F6"
78       "66F68D27262B4DA84657D15057D371BCEC1F6504032507D5B881E45FC93A1B973155"
79       "D91C57219D090C3ACD75E7C2B9F1176A208AC03D6C12AC28A271#)\n"
80"   (x #4186F8A58C5DF46C5BCFC7006BEEBF05E93C0CA7#)\n"
81"))\n";
82
83static const char sample_public_dsa_key_1024[] =
84"(public-key\n"
85"  (dsa\n"
86"   (p #00A126202D592214C5A8F6016E2C3F4256052ACB1CB17D88E64B1293FAF08F5E4685"
87       "03E6F68366B326A56284370EB2103E92D8346A163E44A08FDC422AC8E9E44268557A"
88       "853539A6AF39353A59CE5E78FD98B57D0F3E3A7EBC8A256AC9A775BA59689F3004BF"
89       "C3035730C4C0C51626C5D7F5852637EC589BB29DAB46C161572E4B#)\n"
90"   (q #00DEB5A296421887179ECA1762884DE2AF8185AFC5#)\n"
91"   (g #3958B34AE7747194ECBD312F8FEE8CBE3918E94DF9FD11E2912E56318F33BDC38622"
92       "B18DDFF393074BCA8BAACF50DF27AEE529F3E8AEECE55C398DAB3A5E04C2EA142312"
93       "FACA2FE7F0A88884F8DAC3979EE67598F9A383B2A2325F035C796F352A5C3CDF2CB3"
94       "85AD24EC52A6E55247E1BB37D260F79E617D2A4446415B6AD79A#)\n"
95"   (y #519E9FE9AB0545A6724E74603B7B04E48DC1437E0284A11EA605A7BA8AB1CF354FD4"
96       "ECC93880AC293391C69B558AD84E7AAFA88F11D028CF3A378F241D6B056A90C588F6"
97       "66F68D27262B4DA84657D15057D371BCEC1F6504032507D5B881E45FC93A1B973155"
98       "D91C57219D090C3ACD75E7C2B9F1176A208AC03D6C12AC28A271#)\n"
99"))\n";
100
101
102static const char sample_private_dsa_key_2048[] =
103"(private-key\n"
104"  (dsa\n"
105"   (p #00B54636673962B64F7DC23C71ACEF6E7331796F607560B194DFCC0CA370E858A365"
106       "A413152FB6EB8C664BD171AC316FE5B381CD084D07377571599880A068EF1382D85C"
107       "308B4E9DEAC12D66DE5C4A826EBEB5ED94A62E7301E18927E890589A2F230272A150"
108       "C118BC3DC2965AE0D05BE4F65C6137B2BA7EDABB192C3070D202C10AA3F534574970"
109       "71454DB8A73DDB6511A5BA98EF1450FD90DE5BAAFC9FD3AC22EBEA612DD075BB7405"
110       "D56866D125E33982C046808F7CEBA8E5C0B9F19A6FE451461660A1CBA9EF68891179"
111       "0256A573D3B8F35A5C7A0C6C31F2DB90E25A26845252AD9E485EF2D339E7B5890CD4"
112       "2F9C9F315ED409171EC35CA04CC06B275577B3#)\n"
113"   (q #00DA67989167FDAC4AE3DF9247A716859A30C0CF9C5A6DBA01EABA3481#)\n"
114"   (g #48E35DA584A089D05142AA63603FDB00D131B07A0781E2D5A8F9614D2B33D3E40A78"
115       "98A9E10CDBB612CF093F95A3E10D09566726F2C12823836B2D9CD974BB695665F3B3"
116       "5D219A9724B87F380BD5207EDA0AE38C79E8F18122C3F76E4CEB0ABED3250914987F"
117       "B30D4B9E19C04C28A5D4F45560AF586F6A1B41751EAD90AE7F044F4E2A4A50C1F508"
118       "4FC202463F478F678B9A19392F0D2961C5391C546EF365368BB46410C9C1CEE96E9F"
119       "0C953570C2ED06328B11C90E86E57CAA7FA5ABAA278E22A4C8C08E16EE59F484EC44"
120       "2CF55535BAA2C6BEA8833A555372BEFE1E665D3C7DAEF58061D5136331EF4EB61BC3"
121       "6EE4425A553AF8885FEA15A88135BE133520#)\n"
122"   (y #66E0D1A69D663466F8FEF2B7C0878DAC93C36A2FB2C05E0306A53B926021D4B92A1C"
123       "2FA6860061E88E78CBBBA49B0E12700F07DBF86F72CEB2927EDAC0C7E3969C3A47BB"
124       "4E0AE93D8BB3313E93CC7A72DFEEE442EFBC81B3B2AEC9D8DCBE21220FB760201D79"
125       "328C41C773866587A44B6954767D022A88072900E964089D9B17133603056C985C4F"
126       "8A0B648F297F8D2C3CB43E4371DC6002B5B12CCC085BDB2CFC5074A0587566187EE3"
127       "E11A2A459BD94726248BB8D6CC62938E11E284C2C183576FBB51749EB238C4360923"
128       "79C08CE1C8CD77EB57404CE9B4744395ACF721487450BADE3220576F2F816248B0A7"
129       "14A264330AECCB24DE2A1107847B23490897#)\n"
130"   (x #477BD14676E22563C5ABA68025CEBA2A48D485F5B2D4AD4C0EBBD6D0#)\n"
131"))\n";
132
133
134static const char sample_public_dsa_key_2048[] =
135"(public-key\n"
136"  (dsa\n"
137"   (p #00B54636673962B64F7DC23C71ACEF6E7331796F607560B194DFCC0CA370E858A365"
138       "A413152FB6EB8C664BD171AC316FE5B381CD084D07377571599880A068EF1382D85C"
139       "308B4E9DEAC12D66DE5C4A826EBEB5ED94A62E7301E18927E890589A2F230272A150"
140       "C118BC3DC2965AE0D05BE4F65C6137B2BA7EDABB192C3070D202C10AA3F534574970"
141       "71454DB8A73DDB6511A5BA98EF1450FD90DE5BAAFC9FD3AC22EBEA612DD075BB7405"
142       "D56866D125E33982C046808F7CEBA8E5C0B9F19A6FE451461660A1CBA9EF68891179"
143       "0256A573D3B8F35A5C7A0C6C31F2DB90E25A26845252AD9E485EF2D339E7B5890CD4"
144       "2F9C9F315ED409171EC35CA04CC06B275577B3#)\n"
145"   (q #00DA67989167FDAC4AE3DF9247A716859A30C0CF9C5A6DBA01EABA3481#)\n"
146"   (g #48E35DA584A089D05142AA63603FDB00D131B07A0781E2D5A8F9614D2B33D3E40A78"
147       "98A9E10CDBB612CF093F95A3E10D09566726F2C12823836B2D9CD974BB695665F3B3"
148       "5D219A9724B87F380BD5207EDA0AE38C79E8F18122C3F76E4CEB0ABED3250914987F"
149       "B30D4B9E19C04C28A5D4F45560AF586F6A1B41751EAD90AE7F044F4E2A4A50C1F508"
150       "4FC202463F478F678B9A19392F0D2961C5391C546EF365368BB46410C9C1CEE96E9F"
151       "0C953570C2ED06328B11C90E86E57CAA7FA5ABAA278E22A4C8C08E16EE59F484EC44"
152       "2CF55535BAA2C6BEA8833A555372BEFE1E665D3C7DAEF58061D5136331EF4EB61BC3"
153       "6EE4425A553AF8885FEA15A88135BE133520#)\n"
154"   (y #66E0D1A69D663466F8FEF2B7C0878DAC93C36A2FB2C05E0306A53B926021D4B92A1C"
155       "2FA6860061E88E78CBBBA49B0E12700F07DBF86F72CEB2927EDAC0C7E3969C3A47BB"
156       "4E0AE93D8BB3313E93CC7A72DFEEE442EFBC81B3B2AEC9D8DCBE21220FB760201D79"
157       "328C41C773866587A44B6954767D022A88072900E964089D9B17133603056C985C4F"
158       "8A0B648F297F8D2C3CB43E4371DC6002B5B12CCC085BDB2CFC5074A0587566187EE3"
159       "E11A2A459BD94726248BB8D6CC62938E11E284C2C183576FBB51749EB238C4360923"
160       "79C08CE1C8CD77EB57404CE9B4744395ACF721487450BADE3220576F2F816248B0A7"
161       "14A264330AECCB24DE2A1107847B23490897#)\n"
162"))\n";
163
164
165static const char sample_private_dsa_key_3072[] =
166"(private-key\n"
167"  (dsa\n"
168"   (p #00BA73E148AEA5E8B64878AF5BE712B8302B9671C5F3EEB7722A9D0D9868D048C938"
169       "877C91C335C7819292E69C7D34264F1578E32EC2DA8408DF75D0EB76E0D3030B84B5"
170       "62D8EF93AB53BAB6B8A5DE464F5CA87AEA43BDCF0FB0B7815AA3114CFC84FD916A83"
171       "B3D5FD78390189332232E9D037D215313FD002FF46C048B66703F87FAE092AAA0988"
172       "AC745336EBE672A01DEDBD52395783579B67CF3AE1D6F1602CCCB12154FA0E00AE46"
173       "0D9B289CF709194625BCB919B11038DEFC50ADBBA20C3F320078E4E9529B4F6848E2"
174       "AB5E6278DB961FE226F2EEBD201E071C48C5BEF98B4D9BEE42C1C7102D893EBF8902"
175       "D7A91266340AFD6CE1D09E52282FFF5B97EAFA3886A3FCF84FF76D1E06538D0D8E60"
176       "B3332145785E07D29A5965382DE3470D1D888447FA9C00A2373378FC3FA7B9F7D17E"
177       "95A6A5AE1397BE46D976EF2C96E89913AC4A09351CA661BF6F67E30407DA846946C7"
178       "62D9BAA6B77825097D3E7B886456BB32E3E74516BF3FD93D71B257AA8F723E01CE33"
179       "8015353D3778B02B892AF7#)\n"
180"   (q #00BFF3F3CC18FA018A5B8155A8695E1E4939660D5E4759322C39D50F3B93E5F68B#)\n"
181"   (g #6CCFD8219F5FCE8EF2BEF3262929787140847E38674B1EF8DB20255E212CB6330EC4"
182       "DFE8A26AB7ECC5760DEB9BBF59A2B2821D510F1868172222867558B8D204E889C474"
183       "7CA30FBF9D8CF41AE5D5BD845174641101593849FF333E6C93A6550931B2B9D56B98"
184       "9CAB01729D9D736FA6D24A74D2DDE1E9E648D141473E443DD6BBF0B3CAB64F9FE4FC"
185       "134B2EB57437789F75C744DF1FA67FA8A64603E5441BC7ECE29E00BDF262BDC81E8C"
186       "7330A18A412DE38E7546D342B89A0AF675A89E6BEF00540EB107A2FE74EA402B0D89"
187       "F5C02918DEEEAF8B8737AC866B09B50810AB8D8668834A1B9E1E53866E2B0A926FAB"
188       "120A0CDE5B3715FFFE6ACD1AB73588DCC1EC4CE9392FE57F8D1D35811200CB07A0E6"
189       "374E2C4B0AEB7E3D077B8545C0E438DCC0F1AE81E186930E99EBC5B91B77E92803E0"
190       "21602887851A4FFDB3A7896AC655A0901218C121C5CBB0931E7D5EAC243F37711B5F"
191       "D5A62B1B38A83F03D8F6703D8B98DF367FC8A76990335F62173A5391836F0F2413EC"
192       "4997AF9EB55C6660B01A#)\n"
193"   (y #2320B22434C5DB832B4EC267CC52E78DD5CCFA911E8F0804E7E7F32B186B2D4167AE"
194       "4AA6869822E76400492D6A193B0535322C72B0B7AA4A87E33044FDC84BE24C64A053"
195       "A37655EE9EABDCDC1FDF63F3F1C677CEB41595DF7DEFE9178D85A3D621B4E4775492"
196       "8C0A58D2458D06F9562E4DE2FE6129A64063A99E88E54485B97484A28188C4D33F15"
197       "DDC903B6CEA0135E3E3D27B4EA39319696305CE93D7BA7BE00367DBE3AAF43491E71"
198       "CBF254744A5567F5D70090D6139E0C990239627B3A1C5B20B6F9F6374B8D8D8A8997"
199       "437265BE1E3B4810D4B09254400DE287A0DFFBAEF339E48D422B1D41A37E642BC026"
200       "73314701C8FA9792845C129351A87A945A03E6C895860E51D6FB8B7340A94D1A8A7B"
201       "FA85AC83B4B14E73AB86CB96C236C8BFB0978B61B2367A7FE4F7891070F56C78D5DD"
202       "F5576BFE5BE4F333A4E2664E79528B3294907AADD63F4F2E7AA8147B928D8CD69765"
203       "3DB98C4297CB678046ED55C0DBE60BF7142C594603E4D705DC3D17270F9F086EC561"
204       "2703D518D8D49FF0EBE6#)\n"
205"   (x #00A9FFFC88E67D6F7B810E291C050BAFEA7FC4A75E8D2F16CFED3416FD77607232#)\n"
206"))\n";
207
208static const char sample_public_dsa_key_3072[] =
209"(public-key\n"
210"  (dsa\n"
211"   (p #00BA73E148AEA5E8B64878AF5BE712B8302B9671C5F3EEB7722A9D0D9868D048C938"
212       "877C91C335C7819292E69C7D34264F1578E32EC2DA8408DF75D0EB76E0D3030B84B5"
213       "62D8EF93AB53BAB6B8A5DE464F5CA87AEA43BDCF0FB0B7815AA3114CFC84FD916A83"
214       "B3D5FD78390189332232E9D037D215313FD002FF46C048B66703F87FAE092AAA0988"
215       "AC745336EBE672A01DEDBD52395783579B67CF3AE1D6F1602CCCB12154FA0E00AE46"
216       "0D9B289CF709194625BCB919B11038DEFC50ADBBA20C3F320078E4E9529B4F6848E2"
217       "AB5E6278DB961FE226F2EEBD201E071C48C5BEF98B4D9BEE42C1C7102D893EBF8902"
218       "D7A91266340AFD6CE1D09E52282FFF5B97EAFA3886A3FCF84FF76D1E06538D0D8E60"
219       "B3332145785E07D29A5965382DE3470D1D888447FA9C00A2373378FC3FA7B9F7D17E"
220       "95A6A5AE1397BE46D976EF2C96E89913AC4A09351CA661BF6F67E30407DA846946C7"
221       "62D9BAA6B77825097D3E7B886456BB32E3E74516BF3FD93D71B257AA8F723E01CE33"
222       "8015353D3778B02B892AF7#)\n"
223"   (q #00BFF3F3CC18FA018A5B8155A8695E1E4939660D5E4759322C39D50F3B93E5F68B#)\n"
224"   (g #6CCFD8219F5FCE8EF2BEF3262929787140847E38674B1EF8DB20255E212CB6330EC4"
225       "DFE8A26AB7ECC5760DEB9BBF59A2B2821D510F1868172222867558B8D204E889C474"
226       "7CA30FBF9D8CF41AE5D5BD845174641101593849FF333E6C93A6550931B2B9D56B98"
227       "9CAB01729D9D736FA6D24A74D2DDE1E9E648D141473E443DD6BBF0B3CAB64F9FE4FC"
228       "134B2EB57437789F75C744DF1FA67FA8A64603E5441BC7ECE29E00BDF262BDC81E8C"
229       "7330A18A412DE38E7546D342B89A0AF675A89E6BEF00540EB107A2FE74EA402B0D89"
230       "F5C02918DEEEAF8B8737AC866B09B50810AB8D8668834A1B9E1E53866E2B0A926FAB"
231       "120A0CDE5B3715FFFE6ACD1AB73588DCC1EC4CE9392FE57F8D1D35811200CB07A0E6"
232       "374E2C4B0AEB7E3D077B8545C0E438DCC0F1AE81E186930E99EBC5B91B77E92803E0"
233       "21602887851A4FFDB3A7896AC655A0901218C121C5CBB0931E7D5EAC243F37711B5F"
234       "D5A62B1B38A83F03D8F6703D8B98DF367FC8A76990335F62173A5391836F0F2413EC"
235       "4997AF9EB55C6660B01A#)\n"
236"   (y #2320B22434C5DB832B4EC267CC52E78DD5CCFA911E8F0804E7E7F32B186B2D4167AE"
237       "4AA6869822E76400492D6A193B0535322C72B0B7AA4A87E33044FDC84BE24C64A053"
238       "A37655EE9EABDCDC1FDF63F3F1C677CEB41595DF7DEFE9178D85A3D621B4E4775492"
239       "8C0A58D2458D06F9562E4DE2FE6129A64063A99E88E54485B97484A28188C4D33F15"
240       "DDC903B6CEA0135E3E3D27B4EA39319696305CE93D7BA7BE00367DBE3AAF43491E71"
241       "CBF254744A5567F5D70090D6139E0C990239627B3A1C5B20B6F9F6374B8D8D8A8997"
242       "437265BE1E3B4810D4B09254400DE287A0DFFBAEF339E48D422B1D41A37E642BC026"
243       "73314701C8FA9792845C129351A87A945A03E6C895860E51D6FB8B7340A94D1A8A7B"
244       "FA85AC83B4B14E73AB86CB96C236C8BFB0978B61B2367A7FE4F7891070F56C78D5DD"
245       "F5576BFE5BE4F333A4E2664E79528B3294907AADD63F4F2E7AA8147B928D8CD69765"
246       "3DB98C4297CB678046ED55C0DBE60BF7142C594603E4D705DC3D17270F9F086EC561"
247       "2703D518D8D49FF0EBE6#)\n"
248"))\n";
249
250
251#define DIM(v)		     (sizeof(v)/sizeof((v)[0]))
252#define DIMof(type,member)   DIM(((type *)0)->member)
253#define BUG() do {fprintf ( stderr, "Ooops at %s:%d\n", __FILE__ , __LINE__ );\
254		  exit(2);} while(0)
255
256
257/* Helper for the start and stop timer. */
258#ifdef _WIN32
259struct {
260  FILETIME creation_time, exit_time, kernel_time, user_time;
261} started_at, stopped_at;
262#else
263static clock_t started_at, stopped_at;
264#endif
265
266static void
267die (const char *format, ...)
268{
269  va_list arg_ptr ;
270
271  va_start( arg_ptr, format ) ;
272  putchar ('\n');
273  fputs ( PGM ": ", stderr);
274  vfprintf (stderr, format, arg_ptr );
275  va_end(arg_ptr);
276  exit (1);
277}
278
279static void
280show_sexp (const char *prefix, gcry_sexp_t a)
281{
282  char *buf;
283  size_t size;
284
285  fputs (prefix, stderr);
286  size = gcry_sexp_sprint (a, GCRYSEXP_FMT_ADVANCED, NULL, 0);
287  buf = malloc (size);
288  if (!buf)
289    die ("out of core\n");
290
291  gcry_sexp_sprint (a, GCRYSEXP_FMT_ADVANCED, buf, size);
292  fprintf (stderr, "%.*s", (int)size, buf);
293}
294
295
296static void
297start_timer (void)
298{
299#ifdef _WIN32
300#ifdef __MINGW32CE__
301  GetThreadTimes (GetCurrentThread (),
302                   &started_at.creation_time, &started_at.exit_time,
303                   &started_at.kernel_time, &started_at.user_time);
304#else
305  GetProcessTimes (GetCurrentProcess (),
306                   &started_at.creation_time, &started_at.exit_time,
307                   &started_at.kernel_time, &started_at.user_time);
308#endif
309  stopped_at = started_at;
310#else
311  struct tms tmp;
312
313  times (&tmp);
314  started_at = stopped_at = tmp.tms_utime;
315#endif
316}
317
318static void
319stop_timer (void)
320{
321#ifdef _WIN32
322#ifdef __MINGW32CE__
323  GetThreadTimes (GetCurrentThread (),
324                   &stopped_at.creation_time, &stopped_at.exit_time,
325                   &stopped_at.kernel_time, &stopped_at.user_time);
326#else
327  GetProcessTimes (GetCurrentProcess (),
328                   &stopped_at.creation_time, &stopped_at.exit_time,
329                   &stopped_at.kernel_time, &stopped_at.user_time);
330#endif
331#else
332  struct tms tmp;
333
334  times (&tmp);
335  stopped_at = tmp.tms_utime;
336#endif
337}
338
339static const char *
340elapsed_time (void)
341{
342  static char buf[50];
343#if _WIN32
344  unsigned long long t1, t2, t;
345
346  t1 = (((unsigned long long)started_at.kernel_time.dwHighDateTime << 32)
347        + started_at.kernel_time.dwLowDateTime);
348  t1 += (((unsigned long long)started_at.user_time.dwHighDateTime << 32)
349        + started_at.user_time.dwLowDateTime);
350  t2 = (((unsigned long long)stopped_at.kernel_time.dwHighDateTime << 32)
351        + stopped_at.kernel_time.dwLowDateTime);
352  t2 += (((unsigned long long)stopped_at.user_time.dwHighDateTime << 32)
353        + stopped_at.user_time.dwLowDateTime);
354  t = (t2 - t1)/10000;
355  snprintf (buf, sizeof buf, "%5.0fms", (double)t );
356#else
357  snprintf (buf, sizeof buf, "%5.0fms",
358            (((double) (stopped_at - started_at))/CLOCKS_PER_SEC)*10000000);
359#endif
360  return buf;
361}
362
363
364static void
365progress_cb (void *cb_data, const char *what, int printchar,
366             int current, int total)
367{
368  (void)cb_data;
369
370  fprintf (stderr, PGM ": progress (%s %c %d %d)\n",
371           what, printchar, current, total);
372  fflush (stderr);
373}
374
375
376static void
377random_bench (int very_strong)
378{
379  char buf[128];
380  int i;
381
382  printf ("%-10s", "random");
383
384  if (!very_strong)
385    {
386      start_timer ();
387      for (i=0; i < 100; i++)
388        gcry_randomize (buf, sizeof buf, GCRY_STRONG_RANDOM);
389      stop_timer ();
390      printf (" %s", elapsed_time ());
391    }
392
393  start_timer ();
394  for (i=0; i < 100; i++)
395    gcry_randomize (buf, 8,
396                    very_strong? GCRY_VERY_STRONG_RANDOM:GCRY_STRONG_RANDOM);
397  stop_timer ();
398  printf (" %s", elapsed_time ());
399
400  putchar ('\n');
401  if (verbose)
402    gcry_control (GCRYCTL_DUMP_RANDOM_STATS);
403}
404
405
406
407static void
408md_bench ( const char *algoname )
409{
410  int algo;
411  gcry_md_hd_t hd;
412  int i, j, repcount;
413  char buf_base[1000+15];
414  size_t bufsize = 1000;
415  char *buf;
416  char *largebuf_base;
417  char *largebuf;
418  char digest[512/8];
419  gcry_error_t err = GPG_ERR_NO_ERROR;
420
421  if (!algoname)
422    {
423      for (i=1; i < 400; i++)
424        if (in_fips_mode && i == GCRY_MD_MD5)
425          ; /* Don't use MD5 in fips mode.  */
426        else if ( !gcry_md_test_algo (i) )
427          md_bench (gcry_md_algo_name (i));
428      return;
429    }
430
431  buf = buf_base + ((16 - ((size_t)buf_base & 0x0f)) % buffer_alignment);
432
433  algo = gcry_md_map_name (algoname);
434  if (!algo)
435    {
436      fprintf (stderr, PGM ": invalid hash algorithm `%s'\n", algoname);
437      exit (1);
438    }
439
440  err = gcry_md_open (&hd, algo, 0);
441  if (err)
442    {
443      fprintf (stderr, PGM ": error opening hash algorithm `%s'\n", algoname);
444      exit (1);
445    }
446
447  for (i=0; i < bufsize; i++)
448    buf[i] = i;
449
450  printf ("%-12s", gcry_md_algo_name (algo));
451
452  start_timer ();
453  for (repcount=0; repcount < hash_repetitions; repcount++)
454    for (i=0; i < 1000; i++)
455      gcry_md_write (hd, buf, bufsize);
456  gcry_md_final (hd);
457  stop_timer ();
458  printf (" %s", elapsed_time ());
459  fflush (stdout);
460
461  gcry_md_reset (hd);
462  start_timer ();
463  for (repcount=0; repcount < hash_repetitions; repcount++)
464    for (i=0; i < 10000; i++)
465      gcry_md_write (hd, buf, bufsize/10);
466  gcry_md_final (hd);
467  stop_timer ();
468  printf (" %s", elapsed_time ());
469  fflush (stdout);
470
471  gcry_md_reset (hd);
472  start_timer ();
473  for (repcount=0; repcount < hash_repetitions; repcount++)
474    for (i=0; i < 1000000; i++)
475      gcry_md_write (hd, buf, 1);
476  gcry_md_final (hd);
477  stop_timer ();
478  printf (" %s", elapsed_time ());
479  fflush (stdout);
480
481  start_timer ();
482  for (repcount=0; repcount < hash_repetitions; repcount++)
483    for (i=0; i < 1000; i++)
484      for (j=0; j < bufsize; j++)
485        gcry_md_putc (hd, buf[j]);
486  gcry_md_final (hd);
487  stop_timer ();
488  printf (" %s", elapsed_time ());
489  fflush (stdout);
490
491  gcry_md_close (hd);
492
493  /* Now 100 hash operations on 10000 bytes using the fast function.
494     We initialize the buffer so that all memory pages are committed
495     and we have repeatable values.  */
496  if (gcry_md_get_algo_dlen (algo) > sizeof digest)
497    die ("digest buffer too short\n");
498
499  largebuf_base = malloc (10000+15);
500  if (!largebuf_base)
501    die ("out of core\n");
502  largebuf = (largebuf_base
503              + ((16 - ((size_t)largebuf_base & 0x0f)) % buffer_alignment));
504
505  for (i=0; i < 10000; i++)
506    largebuf[i] = i;
507  start_timer ();
508  for (repcount=0; repcount < hash_repetitions; repcount++)
509    for (i=0; i < 100; i++)
510      gcry_md_hash_buffer (algo, digest, largebuf, 10000);
511  stop_timer ();
512  printf (" %s", elapsed_time ());
513  free (largebuf_base);
514
515  putchar ('\n');
516  fflush (stdout);
517}
518
519static void
520cipher_bench ( const char *algoname )
521{
522  static int header_printed;
523  int algo;
524  gcry_cipher_hd_t hd;
525  int i;
526  int keylen, blklen;
527  char key[128];
528  char *outbuf, *buf;
529  char *raw_outbuf, *raw_buf;
530  size_t allocated_buflen, buflen;
531  int repetitions;
532  static struct { int mode; const char *name; int blocked; } modes[] = {
533    { GCRY_CIPHER_MODE_ECB, "   ECB/Stream", 1 },
534    { GCRY_CIPHER_MODE_CBC, "      CBC", 1 },
535    { GCRY_CIPHER_MODE_CFB, "      CFB", 0 },
536    { GCRY_CIPHER_MODE_OFB, "      OFB", 0 },
537    { GCRY_CIPHER_MODE_CTR, "      CTR", 0 },
538    { GCRY_CIPHER_MODE_STREAM, "", 0 },
539    {0}
540  };
541  int modeidx;
542  gcry_error_t err = GPG_ERR_NO_ERROR;
543
544
545  if (!algoname)
546    {
547      for (i=1; i < 400; i++)
548        if ( !gcry_cipher_test_algo (i) )
549          cipher_bench (gcry_cipher_algo_name (i));
550      return;
551    }
552
553  if (large_buffers)
554    {
555      allocated_buflen = 1024 * 100;
556      repetitions = 10;
557    }
558  else
559    {
560      allocated_buflen = 1024;
561      repetitions = 1000;
562    }
563  repetitions *= cipher_repetitions;
564
565  raw_buf = gcry_xmalloc (allocated_buflen+15);
566  buf = (raw_buf
567         + ((16 - ((size_t)raw_buf & 0x0f)) % buffer_alignment));
568  outbuf = raw_outbuf = gcry_xmalloc (allocated_buflen+15);
569  outbuf = (raw_outbuf
570            + ((16 - ((size_t)raw_outbuf & 0x0f)) % buffer_alignment));
571
572  if (!header_printed)
573    {
574      if (cipher_repetitions != 1)
575        printf ("Running each test %d times.\n", cipher_repetitions);
576      printf ("%-12s", "");
577      for (modeidx=0; modes[modeidx].mode; modeidx++)
578        if (*modes[modeidx].name)
579          printf (" %-15s", modes[modeidx].name );
580      putchar ('\n');
581      printf ("%-12s", "");
582      for (modeidx=0; modes[modeidx].mode; modeidx++)
583        if (*modes[modeidx].name)
584          printf (" ---------------" );
585      putchar ('\n');
586      header_printed = 1;
587    }
588
589  algo = gcry_cipher_map_name (algoname);
590  if (!algo)
591    {
592      fprintf (stderr, PGM ": invalid cipher algorithm `%s'\n", algoname);
593      exit (1);
594    }
595
596  keylen = gcry_cipher_get_algo_keylen (algo);
597  if (!keylen)
598    {
599      fprintf (stderr, PGM ": failed to get key length for algorithm `%s'\n",
600	       algoname);
601      exit (1);
602    }
603  if ( keylen > sizeof key )
604    {
605        fprintf (stderr, PGM ": algo %d, keylength problem (%d)\n",
606                 algo, keylen );
607        exit (1);
608    }
609  for (i=0; i < keylen; i++)
610    key[i] = i + (clock () & 0xff);
611
612  blklen = gcry_cipher_get_algo_blklen (algo);
613  if (!blklen)
614    {
615      fprintf (stderr, PGM ": failed to get block length for algorithm `%s'\n",
616	       algoname);
617      exit (1);
618    }
619
620  printf ("%-12s", gcry_cipher_algo_name (algo));
621  fflush (stdout);
622
623  for (modeidx=0; modes[modeidx].mode; modeidx++)
624    {
625      if ((blklen > 1 && modes[modeidx].mode == GCRY_CIPHER_MODE_STREAM)
626          | (blklen == 1 && modes[modeidx].mode != GCRY_CIPHER_MODE_STREAM))
627        continue;
628
629      for (i=0; i < sizeof buf; i++)
630        buf[i] = i;
631
632      err = gcry_cipher_open (&hd, algo, modes[modeidx].mode, 0);
633      if (err)
634        {
635          fprintf (stderr, PGM ": error opening cipher `%s'\n", algoname);
636          exit (1);
637        }
638
639      if (!cipher_with_keysetup)
640        {
641          err = gcry_cipher_setkey (hd, key, keylen);
642          if (err)
643            {
644              fprintf (stderr, "gcry_cipher_setkey failed: %s\n",
645                       gpg_strerror (err));
646              gcry_cipher_close (hd);
647              exit (1);
648            }
649        }
650
651      buflen = allocated_buflen;
652      if (modes[modeidx].blocked)
653        buflen = (buflen / blklen) * blklen;
654
655      start_timer ();
656      for (i=err=0; !err && i < repetitions; i++)
657        {
658          if (cipher_with_keysetup)
659            {
660              err = gcry_cipher_setkey (hd, key, keylen);
661              if (err)
662                {
663                  fprintf (stderr, "gcry_cipher_setkey failed: %s\n",
664                           gpg_strerror (err));
665                  gcry_cipher_close (hd);
666                  exit (1);
667                }
668            }
669          err = gcry_cipher_encrypt ( hd, outbuf, buflen, buf, buflen);
670        }
671      stop_timer ();
672
673      printf (" %s", elapsed_time ());
674      fflush (stdout);
675      gcry_cipher_close (hd);
676      if (err)
677        {
678          fprintf (stderr, "gcry_cipher_encrypt failed: %s\n",
679                   gpg_strerror (err) );
680          exit (1);
681        }
682
683      err = gcry_cipher_open (&hd, algo, modes[modeidx].mode, 0);
684      if (err)
685        {
686          fprintf (stderr, PGM ": error opening cipher `%s'/n", algoname);
687          exit (1);
688        }
689
690      if (!cipher_with_keysetup)
691        {
692          err = gcry_cipher_setkey (hd, key, keylen);
693          if (err)
694            {
695              fprintf (stderr, "gcry_cipher_setkey failed: %s\n",
696                       gpg_strerror (err));
697              gcry_cipher_close (hd);
698              exit (1);
699            }
700        }
701
702      start_timer ();
703      for (i=err=0; !err && i < repetitions; i++)
704        {
705          if (cipher_with_keysetup)
706            {
707              err = gcry_cipher_setkey (hd, key, keylen);
708              if (err)
709                {
710                  fprintf (stderr, "gcry_cipher_setkey failed: %s\n",
711                           gpg_strerror (err));
712                  gcry_cipher_close (hd);
713                  exit (1);
714                }
715            }
716          err = gcry_cipher_decrypt ( hd, outbuf, buflen,  buf, buflen);
717        }
718      stop_timer ();
719      printf (" %s", elapsed_time ());
720      fflush (stdout);
721      gcry_cipher_close (hd);
722      if (err)
723        {
724          fprintf (stderr, "gcry_cipher_decrypt failed: %s\n",
725                   gpg_strerror (err) );
726          exit (1);
727        }
728    }
729
730  putchar ('\n');
731  gcry_free (raw_buf);
732  gcry_free (raw_outbuf);
733}
734
735
736
737static void
738rsa_bench (int iterations, int print_header, int no_blinding)
739{
740  gpg_error_t err;
741  int p_sizes[] = { 1024, 2048, 3072, 4096 };
742  int testno;
743
744  if (print_header)
745    printf ("Algorithm         generate %4d*sign %4d*verify\n"
746            "------------------------------------------------\n",
747            iterations, iterations );
748  for (testno=0; testno < DIM (p_sizes); testno++)
749    {
750      gcry_sexp_t key_spec, key_pair, pub_key, sec_key;
751      gcry_mpi_t x;
752      gcry_sexp_t data;
753      gcry_sexp_t sig = NULL;
754      int count;
755
756      printf ("RSA %3d bit    ", p_sizes[testno]);
757      fflush (stdout);
758
759      err = gcry_sexp_build (&key_spec, NULL,
760                             gcry_fips_mode_active ()
761                             ? "(genkey (RSA (nbits %d)))"
762                             : "(genkey (RSA (nbits %d)(transient-key)))",
763                             p_sizes[testno]);
764      if (err)
765        die ("creating S-expression failed: %s\n", gcry_strerror (err));
766
767      start_timer ();
768      err = gcry_pk_genkey (&key_pair, key_spec);
769      if (err)
770        die ("creating %d bit RSA key failed: %s\n",
771             p_sizes[testno], gcry_strerror (err));
772
773      pub_key = gcry_sexp_find_token (key_pair, "public-key", 0);
774      if (! pub_key)
775        die ("public part missing in key\n");
776      sec_key = gcry_sexp_find_token (key_pair, "private-key", 0);
777      if (! sec_key)
778        die ("private part missing in key\n");
779      gcry_sexp_release (key_pair);
780      gcry_sexp_release (key_spec);
781
782      stop_timer ();
783      printf ("   %s", elapsed_time ());
784      fflush (stdout);
785
786      x = gcry_mpi_new (p_sizes[testno]);
787      gcry_mpi_randomize (x, p_sizes[testno]-8, GCRY_WEAK_RANDOM);
788      err = gcry_sexp_build (&data, NULL,
789                             "(data (flags raw) (value %m))", x);
790      gcry_mpi_release (x);
791      if (err)
792        die ("converting data failed: %s\n", gcry_strerror (err));
793
794      start_timer ();
795      for (count=0; count < iterations; count++)
796        {
797          gcry_sexp_release (sig);
798          err = gcry_pk_sign (&sig, data, sec_key);
799          if (err)
800            die ("signing failed (%d): %s\n", count, gpg_strerror (err));
801        }
802      stop_timer ();
803      printf ("   %s", elapsed_time ());
804      fflush (stdout);
805
806      start_timer ();
807      for (count=0; count < iterations; count++)
808        {
809          err = gcry_pk_verify (sig, data, pub_key);
810          if (err)
811            {
812              putchar ('\n');
813              show_sexp ("seckey:\n", sec_key);
814              show_sexp ("data:\n", data);
815              show_sexp ("sig:\n", sig);
816              die ("verify failed (%d): %s\n", count, gpg_strerror (err));
817            }
818        }
819      stop_timer ();
820      printf ("     %s", elapsed_time ());
821
822      if (no_blinding)
823        {
824          fflush (stdout);
825          x = gcry_mpi_new (p_sizes[testno]);
826          gcry_mpi_randomize (x, p_sizes[testno]-8, GCRY_WEAK_RANDOM);
827          err = gcry_sexp_build (&data, NULL,
828                                 "(data (flags no-blinding) (value %m))", x);
829          gcry_mpi_release (x);
830          if (err)
831            die ("converting data failed: %s\n", gcry_strerror (err));
832
833          start_timer ();
834          for (count=0; count < iterations; count++)
835            {
836              gcry_sexp_release (sig);
837              err = gcry_pk_sign (&sig, data, sec_key);
838              if (err)
839                die ("signing failed (%d): %s\n", count, gpg_strerror (err));
840            }
841          stop_timer ();
842          printf ("   %s", elapsed_time ());
843          fflush (stdout);
844        }
845
846      putchar ('\n');
847      fflush (stdout);
848
849      gcry_sexp_release (sig);
850      gcry_sexp_release (data);
851      gcry_sexp_release (sec_key);
852      gcry_sexp_release (pub_key);
853    }
854}
855
856
857
858static void
859dsa_bench (int iterations, int print_header)
860{
861  gpg_error_t err;
862  gcry_sexp_t pub_key[3], sec_key[3];
863  int p_sizes[3] = { 1024, 2048, 3072 };
864  int q_sizes[3] = { 160, 224, 256 };
865  gcry_sexp_t data;
866  gcry_sexp_t sig;
867  int i, j;
868
869  err = gcry_sexp_sscan (pub_key+0, NULL, sample_public_dsa_key_1024,
870                         strlen (sample_public_dsa_key_1024));
871  if (!err)
872    err = gcry_sexp_sscan (sec_key+0, NULL, sample_private_dsa_key_1024,
873                           strlen (sample_private_dsa_key_1024));
874  if (!err)
875    err = gcry_sexp_sscan (pub_key+1, NULL, sample_public_dsa_key_2048,
876                           strlen (sample_public_dsa_key_2048));
877  if (!err)
878    err = gcry_sexp_sscan (sec_key+1, NULL, sample_private_dsa_key_2048,
879                           strlen (sample_private_dsa_key_2048));
880  if (!err)
881    err = gcry_sexp_sscan (pub_key+2, NULL, sample_public_dsa_key_3072,
882                           strlen (sample_public_dsa_key_3072));
883  if (!err)
884    err = gcry_sexp_sscan (sec_key+2, NULL, sample_private_dsa_key_3072,
885                           strlen (sample_private_dsa_key_3072));
886  if (err)
887    {
888      fprintf (stderr, PGM ": converting sample keys failed: %s\n",
889               gcry_strerror (err));
890      exit (1);
891    }
892
893  if (print_header)
894    printf ("Algorithm         generate %4d*sign %4d*verify\n"
895            "------------------------------------------------\n",
896            iterations, iterations );
897  for (i=0; i < DIM (q_sizes); i++)
898    {
899      gcry_mpi_t x;
900
901      x = gcry_mpi_new (q_sizes[i]);
902      gcry_mpi_randomize (x, q_sizes[i], GCRY_WEAK_RANDOM);
903      err = gcry_sexp_build (&data, NULL, "(data (flags raw) (value %m))", x);
904      gcry_mpi_release (x);
905      if (err)
906        {
907          fprintf (stderr, PGM ": converting data failed: %s\n",
908                   gcry_strerror (err));
909          exit (1);
910        }
911
912      printf ("DSA %d/%d             -", p_sizes[i], q_sizes[i]);
913      fflush (stdout);
914
915      start_timer ();
916      for (j=0; j < iterations; j++)
917        {
918          err = gcry_pk_sign (&sig, data, sec_key[i]);
919          if (err)
920            {
921              putchar ('\n');
922              fprintf (stderr, PGM ": signing failed: %s\n",
923                       gpg_strerror (err));
924              exit (1);
925            }
926        }
927      stop_timer ();
928      printf ("   %s", elapsed_time ());
929      fflush (stdout);
930
931      start_timer ();
932      for (j=0; j < iterations; j++)
933        {
934          err = gcry_pk_verify (sig, data, pub_key[i]);
935          if (err)
936            {
937              putchar ('\n');
938              fprintf (stderr, PGM ": verify failed: %s\n",
939                       gpg_strerror (err));
940              exit (1);
941            }
942        }
943      stop_timer ();
944      printf ("     %s\n", elapsed_time ());
945      fflush (stdout);
946
947      gcry_sexp_release (sig);
948      gcry_sexp_release (data);
949    }
950
951
952  for (i=0; i < DIM (q_sizes); i++)
953    {
954      gcry_sexp_release (sec_key[i]);
955      gcry_sexp_release (pub_key[i]);
956    }
957}
958
959
960static void
961ecc_bench (int iterations, int print_header)
962{
963#if USE_ECC
964  gpg_error_t err;
965  int p_sizes[] = { 192, 224, 256, 384, 521 };
966  int testno;
967
968  if (print_header)
969    printf ("Algorithm         generate %4d*sign %4d*verify\n"
970            "------------------------------------------------\n",
971            iterations, iterations );
972  for (testno=0; testno < DIM (p_sizes); testno++)
973    {
974      gcry_sexp_t key_spec, key_pair, pub_key, sec_key;
975      gcry_mpi_t x;
976      gcry_sexp_t data;
977      gcry_sexp_t sig = NULL;
978      int count;
979
980      printf ("ECDSA %3d bit ", p_sizes[testno]);
981      fflush (stdout);
982
983      err = gcry_sexp_build (&key_spec, NULL,
984                             "(genkey (ECDSA (nbits %d)))", p_sizes[testno]);
985      if (err)
986        die ("creating S-expression failed: %s\n", gcry_strerror (err));
987
988      start_timer ();
989      err = gcry_pk_genkey (&key_pair, key_spec);
990      if (err)
991        die ("creating %d bit ECC key failed: %s\n",
992             p_sizes[testno], gcry_strerror (err));
993
994      pub_key = gcry_sexp_find_token (key_pair, "public-key", 0);
995      if (! pub_key)
996        die ("public part missing in key\n");
997      sec_key = gcry_sexp_find_token (key_pair, "private-key", 0);
998      if (! sec_key)
999        die ("private part missing in key\n");
1000      gcry_sexp_release (key_pair);
1001      gcry_sexp_release (key_spec);
1002
1003      stop_timer ();
1004      printf ("     %s", elapsed_time ());
1005      fflush (stdout);
1006
1007      x = gcry_mpi_new (p_sizes[testno]);
1008      gcry_mpi_randomize (x, p_sizes[testno], GCRY_WEAK_RANDOM);
1009      err = gcry_sexp_build (&data, NULL, "(data (flags raw) (value %m))", x);
1010      gcry_mpi_release (x);
1011      if (err)
1012        die ("converting data failed: %s\n", gcry_strerror (err));
1013
1014      start_timer ();
1015      for (count=0; count < iterations; count++)
1016        {
1017          gcry_sexp_release (sig);
1018          err = gcry_pk_sign (&sig, data, sec_key);
1019          if (err)
1020            die ("signing failed: %s\n", gpg_strerror (err));
1021        }
1022      stop_timer ();
1023      printf ("   %s", elapsed_time ());
1024      fflush (stdout);
1025
1026      start_timer ();
1027      for (count=0; count < iterations; count++)
1028        {
1029          err = gcry_pk_verify (sig, data, pub_key);
1030          if (err)
1031            {
1032              putchar ('\n');
1033              show_sexp ("seckey:\n", sec_key);
1034              show_sexp ("data:\n", data);
1035              show_sexp ("sig:\n", sig);
1036              die ("verify failed: %s\n", gpg_strerror (err));
1037            }
1038        }
1039      stop_timer ();
1040      printf ("     %s\n", elapsed_time ());
1041      fflush (stdout);
1042
1043      gcry_sexp_release (sig);
1044      gcry_sexp_release (data);
1045      gcry_sexp_release (sec_key);
1046      gcry_sexp_release (pub_key);
1047    }
1048#endif /*USE_ECC*/
1049}
1050
1051
1052
1053static void
1054do_powm ( const char *n_str, const char *e_str, const char *m_str)
1055{
1056  gcry_mpi_t e, n, msg, cip;
1057  gcry_error_t err;
1058  int i;
1059
1060  err = gcry_mpi_scan (&n, GCRYMPI_FMT_HEX, n_str, 0, 0);
1061  if (err) BUG ();
1062  err = gcry_mpi_scan (&e, GCRYMPI_FMT_HEX, e_str, 0, 0);
1063  if (err) BUG ();
1064  err = gcry_mpi_scan (&msg, GCRYMPI_FMT_HEX, m_str, 0, 0);
1065  if (err) BUG ();
1066
1067  cip = gcry_mpi_new (0);
1068
1069  start_timer ();
1070  for (i=0; i < 1000; i++)
1071    gcry_mpi_powm (cip, msg, e, n);
1072  stop_timer ();
1073  printf (" %s", elapsed_time ()); fflush (stdout);
1074/*    { */
1075/*      char *buf; */
1076
1077/*      if (gcry_mpi_aprint (GCRYMPI_FMT_HEX, (void**)&buf, NULL, cip)) */
1078/*        BUG (); */
1079/*      printf ("result: %s\n", buf); */
1080/*      gcry_free (buf); */
1081/*    } */
1082  gcry_mpi_release (cip);
1083  gcry_mpi_release (msg);
1084  gcry_mpi_release (n);
1085  gcry_mpi_release (e);
1086}
1087
1088
1089static void
1090mpi_bench (void)
1091{
1092  printf ("%-10s", "powm"); fflush (stdout);
1093
1094  do_powm (
1095"20A94417D4D5EF2B2DA99165C7DC87DADB3979B72961AF90D09D59BA24CB9A10166FDCCC9C659F2B9626EC23F3FA425F564A072BA941B03FA81767CC289E4",
1096           "29",
1097"B870187A323F1ECD5B8A0B4249507335A1C4CE8394F38FD76B08C78A42C58F6EA136ACF90DFE8603697B1694A3D81114D6117AC1811979C51C4DD013D52F8"
1098           );
1099  do_powm (
1100           "20A94417D4D5EF2B2DA99165C7DC87DADB3979B72961AF90D09D59BA24CB9A10166FDCCC9C659F2B9626EC23F3FA425F564A072BA941B03FA81767CC289E41071F0246879A442658FBD18C1771571E7073EEEB2160BA0CBFB3404D627069A6CFBD53867AD2D9D40231648000787B5C84176B4336144644AE71A403CA40716",
1101           "29",
1102           "B870187A323F1ECD5B8A0B4249507335A1C4CE8394F38FD76B08C78A42C58F6EA136ACF90DFE8603697B1694A3D81114D6117AC1811979C51C4DD013D52F8FC4EE4BB446B83E48ABED7DB81CBF5E81DE4759E8D68AC985846D999F96B0D8A80E5C69D272C766AB8A23B40D50A4FA889FBC2BD2624222D8EB297F4BAEF8593847"
1103           );
1104  do_powm (
1105           "20A94417D4D5EF2B2DA99165C7DC87DADB3979B72961AF90D09D59BA24CB9A10166FDCCC9C659F2B9626EC23F3FA425F564A072BA941B03FA81767CC289E41071F0246879A442658FBD18C1771571E7073EEEB2160BA0CBFB3404D627069A6CFBD53867AD2D9D40231648000787B5C84176B4336144644AE71A403CA4071620A94417D4D5EF2B2DA99165C7DC87DADB3979B72961AF90D09D59BA24CB9A10166FDCCC9C659F2B9626EC23F3FA425F564A072BA941B03FA81767CC289E41071F0246879A442658FBD18C1771571E7073EEEB2160BA0CBFB3404D627069A6CFBD53867AD2D9D40231648000787B5C84176B4336144644AE71A403CA40716",
1106           "29",
1107           "B870187A323F1ECD5B8A0B4249507335A1C4CE8394F38FD76B08C78A42C58F6EA136ACF90DFE8603697B1694A3D81114D6117AC1811979C51C4DD013D52F8FC4EE4BB446B83E48ABED7DB81CBF5E81DE4759E8D68AC985846D999F96B0D8A80E5C69D272C766AB8A23B40D50A4FA889FBC2BD2624222D8EB297F4BAEF8593847B870187A323F1ECD5B8A0B4249507335A1C4CE8394F38FD76B08C78A42C58F6EA136ACF90DFE8603697B1694A3D81114D6117AC1811979C51C4DD013D52F8FC4EE4BB446B83E48ABED7DB81CBF5E81DE4759E8D68AC985846D999F96B0D8A80E5C69D272C766AB8A23B40D50A4FA889FBC2BD2624222D8EB297F4BAEF8593847"
1108           );
1109
1110  putchar ('\n');
1111
1112
1113}
1114
1115
1116int
1117main( int argc, char **argv )
1118{
1119  int last_argc = -1;
1120  int no_blinding = 0;
1121  int use_random_daemon = 0;
1122  int with_progress = 0;
1123
1124  buffer_alignment = 1;
1125
1126  if (argc)
1127    { argc--; argv++; }
1128
1129  while (argc && last_argc != argc )
1130    {
1131      last_argc = argc;
1132      if (!strcmp (*argv, "--"))
1133        {
1134          argc--; argv++;
1135          break;
1136        }
1137      else if (!strcmp (*argv, "--help"))
1138        {
1139          fputs ("usage: benchmark "
1140                 "[md|cipher|random|mpi|rsa|dsa|ecc [algonames]]\n",
1141                 stdout);
1142          exit (0);
1143        }
1144      else if (!strcmp (*argv, "--verbose"))
1145        {
1146          verbose++;
1147          argc--; argv++;
1148        }
1149      else if (!strcmp (*argv, "--use-random-daemon"))
1150        {
1151          use_random_daemon = 1;
1152          argc--; argv++;
1153        }
1154      else if (!strcmp (*argv, "--no-blinding"))
1155        {
1156          no_blinding = 1;
1157          argc--; argv++;
1158        }
1159      else if (!strcmp (*argv, "--large-buffers"))
1160        {
1161          large_buffers = 1;
1162          argc--; argv++;
1163        }
1164      else if (!strcmp (*argv, "--cipher-repetitions"))
1165        {
1166          argc--; argv++;
1167          if (argc)
1168            {
1169              cipher_repetitions = atoi(*argv);
1170              argc--; argv++;
1171            }
1172        }
1173      else if (!strcmp (*argv, "--cipher-with-keysetup"))
1174        {
1175          cipher_with_keysetup = 1;
1176          argc--; argv++;
1177        }
1178      else if (!strcmp (*argv, "--hash-repetitions"))
1179        {
1180          argc--; argv++;
1181          if (argc)
1182            {
1183              hash_repetitions = atoi(*argv);
1184              argc--; argv++;
1185            }
1186        }
1187      else if (!strcmp (*argv, "--alignment"))
1188        {
1189          argc--; argv++;
1190          if (argc)
1191            {
1192              buffer_alignment = atoi(*argv);
1193              argc--; argv++;
1194            }
1195        }
1196      else if (!strcmp (*argv, "--disable-hwf"))
1197        {
1198          argc--; argv++;
1199          if (argc)
1200            {
1201              if (gcry_control (GCRYCTL_DISABLE_HWF, *argv, NULL))
1202                fprintf (stderr, PGM ": unknown hardware feature `%s'"
1203                         " - option ignored\n", *argv);
1204              argc--; argv++;
1205            }
1206        }
1207      else if (!strcmp (*argv, "--fips"))
1208        {
1209          argc--; argv++;
1210          /* This command needs to be called before gcry_check_version.  */
1211          gcry_control (GCRYCTL_FORCE_FIPS_MODE, 0);
1212        }
1213      else if (!strcmp (*argv, "--progress"))
1214        {
1215          argc--; argv++;
1216          with_progress = 1;
1217        }
1218    }
1219
1220  if (buffer_alignment < 1 || buffer_alignment > 16)
1221    die ("value for --alignment must be in the range 1 to 16\n");
1222
1223  gcry_control (GCRYCTL_SET_VERBOSITY, (int)verbose);
1224
1225  if (!gcry_check_version (GCRYPT_VERSION))
1226    {
1227      fprintf (stderr, PGM ": version mismatch; pgm=%s, library=%s\n",
1228               GCRYPT_VERSION, gcry_check_version (NULL));
1229      exit (1);
1230    }
1231
1232  if (gcry_fips_mode_active ())
1233    in_fips_mode = 1;
1234  else
1235    gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
1236
1237  if (use_random_daemon)
1238    gcry_control (GCRYCTL_USE_RANDOM_DAEMON, 1);
1239
1240  if (with_progress)
1241    gcry_set_progress_handler (progress_cb, NULL);
1242
1243  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
1244
1245  if (cipher_repetitions < 1)
1246    cipher_repetitions = 1;
1247  if (hash_repetitions < 1)
1248    hash_repetitions = 1;
1249
1250  if ( !argc )
1251    {
1252      gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
1253      md_bench (NULL);
1254      putchar ('\n');
1255      cipher_bench (NULL);
1256      putchar ('\n');
1257      rsa_bench (100, 1, no_blinding);
1258      dsa_bench (100, 0);
1259      ecc_bench (100, 0);
1260      putchar ('\n');
1261      mpi_bench ();
1262      putchar ('\n');
1263      random_bench (0);
1264    }
1265  else if ( !strcmp (*argv, "random") || !strcmp (*argv, "strongrandom"))
1266    {
1267      if (argc == 1)
1268        random_bench ((**argv == 's'));
1269      else if (argc == 2)
1270        {
1271          gcry_control (GCRYCTL_SET_RANDOM_SEED_FILE, argv[1]);
1272          random_bench ((**argv == 's'));
1273          gcry_control (GCRYCTL_UPDATE_RANDOM_SEED_FILE);
1274        }
1275      else
1276        fputs ("usage: benchmark [strong]random [seedfile]\n", stdout);
1277    }
1278  else if ( !strcmp (*argv, "md"))
1279    {
1280      if (argc == 1)
1281        md_bench (NULL);
1282      else
1283        for (argc--, argv++; argc; argc--, argv++)
1284          md_bench ( *argv );
1285    }
1286  else if ( !strcmp (*argv, "cipher"))
1287    {
1288      if (argc == 1)
1289        cipher_bench (NULL);
1290      else
1291        for (argc--, argv++; argc; argc--, argv++)
1292          cipher_bench ( *argv );
1293    }
1294  else if ( !strcmp (*argv, "mpi"))
1295    {
1296        mpi_bench ();
1297    }
1298  else if ( !strcmp (*argv, "rsa"))
1299    {
1300        gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
1301        rsa_bench (100, 1, no_blinding);
1302    }
1303  else if ( !strcmp (*argv, "dsa"))
1304    {
1305        gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
1306        dsa_bench (100, 1);
1307    }
1308  else if ( !strcmp (*argv, "ecc"))
1309    {
1310        gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
1311        ecc_bench (100, 1);
1312    }
1313  else
1314    {
1315      fprintf (stderr, PGM ": bad arguments\n");
1316      return 1;
1317    }
1318
1319
1320  if (in_fips_mode && !gcry_fips_mode_active ())
1321    fprintf (stderr, PGM ": FIPS mode is not anymore active\n");
1322
1323  return 0;
1324}
1325