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 <stdarg.h>
26 
27 #ifdef _GCRYPT_IN_LIBGCRYPT
28 # include "../src/gcrypt-int.h"
29 # include "../compat/libcompat.h"
30 #else
31 # include <gcrypt.h>
32 #endif
33 
34 #include "stopwatch.h"
35 
36 
37 #define PGM "benchmark"
38 #include "t-common.h"
39 
40 /* Do encryption tests with large buffers (100 KiB).  */
41 static int large_buffers;
42 
43 /* Do encryption tests with huge buffers (256 MiB). */
44 static int huge_buffers;
45 
46 /* Number of cipher repetitions.  */
47 static int cipher_repetitions;
48 
49 /* Number of hash repetitions.  */
50 static int hash_repetitions;
51 
52 /* Number of hash repetitions.  */
53 static int mac_repetitions;
54 
55 /* Alignment of the buffers.  */
56 static int buffer_alignment;
57 
58 /* Whether to include the keysetup in the cipher timings.  */
59 static int cipher_with_keysetup;
60 
61 /* Whether fips mode was active at startup.  */
62 static int in_fips_mode;
63 
64 /* Whether we are running as part of the regression test suite.  */
65 static int in_regression_test;
66 
67 /* Whether --progress is in use.  */
68 static int with_progress;
69 
70 /* Runtime flag to switch to a different progress output.  */
71 static int single_char_progress;
72 
73 
74 static const char sample_private_dsa_key_1024[] =
75 "(private-key\n"
76 "  (dsa\n"
77 "   (p #00A126202D592214C5A8F6016E2C3F4256052ACB1CB17D88E64B1293FAF08F5E4685"
78        "03E6F68366B326A56284370EB2103E92D8346A163E44A08FDC422AC8E9E44268557A"
79        "853539A6AF39353A59CE5E78FD98B57D0F3E3A7EBC8A256AC9A775BA59689F3004BF"
80        "C3035730C4C0C51626C5D7F5852637EC589BB29DAB46C161572E4B#)\n"
81 "   (q #00DEB5A296421887179ECA1762884DE2AF8185AFC5#)\n"
82 "   (g #3958B34AE7747194ECBD312F8FEE8CBE3918E94DF9FD11E2912E56318F33BDC38622"
83        "B18DDFF393074BCA8BAACF50DF27AEE529F3E8AEECE55C398DAB3A5E04C2EA142312"
84        "FACA2FE7F0A88884F8DAC3979EE67598F9A383B2A2325F035C796F352A5C3CDF2CB3"
85        "85AD24EC52A6E55247E1BB37D260F79E617D2A4446415B6AD79A#)\n"
86 "   (y #519E9FE9AB0545A6724E74603B7B04E48DC1437E0284A11EA605A7BA8AB1CF354FD4"
87        "ECC93880AC293391C69B558AD84E7AAFA88F11D028CF3A378F241D6B056A90C588F6"
88        "66F68D27262B4DA84657D15057D371BCEC1F6504032507D5B881E45FC93A1B973155"
89        "D91C57219D090C3ACD75E7C2B9F1176A208AC03D6C12AC28A271#)\n"
90 "   (x #4186F8A58C5DF46C5BCFC7006BEEBF05E93C0CA7#)\n"
91 "))\n";
92 
93 static const char sample_public_dsa_key_1024[] =
94 "(public-key\n"
95 "  (dsa\n"
96 "   (p #00A126202D592214C5A8F6016E2C3F4256052ACB1CB17D88E64B1293FAF08F5E4685"
97        "03E6F68366B326A56284370EB2103E92D8346A163E44A08FDC422AC8E9E44268557A"
98        "853539A6AF39353A59CE5E78FD98B57D0F3E3A7EBC8A256AC9A775BA59689F3004BF"
99        "C3035730C4C0C51626C5D7F5852637EC589BB29DAB46C161572E4B#)\n"
100 "   (q #00DEB5A296421887179ECA1762884DE2AF8185AFC5#)\n"
101 "   (g #3958B34AE7747194ECBD312F8FEE8CBE3918E94DF9FD11E2912E56318F33BDC38622"
102        "B18DDFF393074BCA8BAACF50DF27AEE529F3E8AEECE55C398DAB3A5E04C2EA142312"
103        "FACA2FE7F0A88884F8DAC3979EE67598F9A383B2A2325F035C796F352A5C3CDF2CB3"
104        "85AD24EC52A6E55247E1BB37D260F79E617D2A4446415B6AD79A#)\n"
105 "   (y #519E9FE9AB0545A6724E74603B7B04E48DC1437E0284A11EA605A7BA8AB1CF354FD4"
106        "ECC93880AC293391C69B558AD84E7AAFA88F11D028CF3A378F241D6B056A90C588F6"
107        "66F68D27262B4DA84657D15057D371BCEC1F6504032507D5B881E45FC93A1B973155"
108        "D91C57219D090C3ACD75E7C2B9F1176A208AC03D6C12AC28A271#)\n"
109 "))\n";
110 
111 
112 static const char sample_private_dsa_key_2048[] =
113 "(private-key\n"
114 "  (dsa\n"
115 "   (p #00B54636673962B64F7DC23C71ACEF6E7331796F607560B194DFCC0CA370E858A365"
116        "A413152FB6EB8C664BD171AC316FE5B381CD084D07377571599880A068EF1382D85C"
117        "308B4E9DEAC12D66DE5C4A826EBEB5ED94A62E7301E18927E890589A2F230272A150"
118        "C118BC3DC2965AE0D05BE4F65C6137B2BA7EDABB192C3070D202C10AA3F534574970"
119        "71454DB8A73DDB6511A5BA98EF1450FD90DE5BAAFC9FD3AC22EBEA612DD075BB7405"
120        "D56866D125E33982C046808F7CEBA8E5C0B9F19A6FE451461660A1CBA9EF68891179"
121        "0256A573D3B8F35A5C7A0C6C31F2DB90E25A26845252AD9E485EF2D339E7B5890CD4"
122        "2F9C9F315ED409171EC35CA04CC06B275577B3#)\n"
123 "   (q #00DA67989167FDAC4AE3DF9247A716859A30C0CF9C5A6DBA01EABA3481#)\n"
124 "   (g #48E35DA584A089D05142AA63603FDB00D131B07A0781E2D5A8F9614D2B33D3E40A78"
125        "98A9E10CDBB612CF093F95A3E10D09566726F2C12823836B2D9CD974BB695665F3B3"
126        "5D219A9724B87F380BD5207EDA0AE38C79E8F18122C3F76E4CEB0ABED3250914987F"
127        "B30D4B9E19C04C28A5D4F45560AF586F6A1B41751EAD90AE7F044F4E2A4A50C1F508"
128        "4FC202463F478F678B9A19392F0D2961C5391C546EF365368BB46410C9C1CEE96E9F"
129        "0C953570C2ED06328B11C90E86E57CAA7FA5ABAA278E22A4C8C08E16EE59F484EC44"
130        "2CF55535BAA2C6BEA8833A555372BEFE1E665D3C7DAEF58061D5136331EF4EB61BC3"
131        "6EE4425A553AF8885FEA15A88135BE133520#)\n"
132 "   (y #66E0D1A69D663466F8FEF2B7C0878DAC93C36A2FB2C05E0306A53B926021D4B92A1C"
133        "2FA6860061E88E78CBBBA49B0E12700F07DBF86F72CEB2927EDAC0C7E3969C3A47BB"
134        "4E0AE93D8BB3313E93CC7A72DFEEE442EFBC81B3B2AEC9D8DCBE21220FB760201D79"
135        "328C41C773866587A44B6954767D022A88072900E964089D9B17133603056C985C4F"
136        "8A0B648F297F8D2C3CB43E4371DC6002B5B12CCC085BDB2CFC5074A0587566187EE3"
137        "E11A2A459BD94726248BB8D6CC62938E11E284C2C183576FBB51749EB238C4360923"
138        "79C08CE1C8CD77EB57404CE9B4744395ACF721487450BADE3220576F2F816248B0A7"
139        "14A264330AECCB24DE2A1107847B23490897#)\n"
140 "   (x #477BD14676E22563C5ABA68025CEBA2A48D485F5B2D4AD4C0EBBD6D0#)\n"
141 "))\n";
142 
143 
144 static const char sample_public_dsa_key_2048[] =
145 "(public-key\n"
146 "  (dsa\n"
147 "   (p #00B54636673962B64F7DC23C71ACEF6E7331796F607560B194DFCC0CA370E858A365"
148        "A413152FB6EB8C664BD171AC316FE5B381CD084D07377571599880A068EF1382D85C"
149        "308B4E9DEAC12D66DE5C4A826EBEB5ED94A62E7301E18927E890589A2F230272A150"
150        "C118BC3DC2965AE0D05BE4F65C6137B2BA7EDABB192C3070D202C10AA3F534574970"
151        "71454DB8A73DDB6511A5BA98EF1450FD90DE5BAAFC9FD3AC22EBEA612DD075BB7405"
152        "D56866D125E33982C046808F7CEBA8E5C0B9F19A6FE451461660A1CBA9EF68891179"
153        "0256A573D3B8F35A5C7A0C6C31F2DB90E25A26845252AD9E485EF2D339E7B5890CD4"
154        "2F9C9F315ED409171EC35CA04CC06B275577B3#)\n"
155 "   (q #00DA67989167FDAC4AE3DF9247A716859A30C0CF9C5A6DBA01EABA3481#)\n"
156 "   (g #48E35DA584A089D05142AA63603FDB00D131B07A0781E2D5A8F9614D2B33D3E40A78"
157        "98A9E10CDBB612CF093F95A3E10D09566726F2C12823836B2D9CD974BB695665F3B3"
158        "5D219A9724B87F380BD5207EDA0AE38C79E8F18122C3F76E4CEB0ABED3250914987F"
159        "B30D4B9E19C04C28A5D4F45560AF586F6A1B41751EAD90AE7F044F4E2A4A50C1F508"
160        "4FC202463F478F678B9A19392F0D2961C5391C546EF365368BB46410C9C1CEE96E9F"
161        "0C953570C2ED06328B11C90E86E57CAA7FA5ABAA278E22A4C8C08E16EE59F484EC44"
162        "2CF55535BAA2C6BEA8833A555372BEFE1E665D3C7DAEF58061D5136331EF4EB61BC3"
163        "6EE4425A553AF8885FEA15A88135BE133520#)\n"
164 "   (y #66E0D1A69D663466F8FEF2B7C0878DAC93C36A2FB2C05E0306A53B926021D4B92A1C"
165        "2FA6860061E88E78CBBBA49B0E12700F07DBF86F72CEB2927EDAC0C7E3969C3A47BB"
166        "4E0AE93D8BB3313E93CC7A72DFEEE442EFBC81B3B2AEC9D8DCBE21220FB760201D79"
167        "328C41C773866587A44B6954767D022A88072900E964089D9B17133603056C985C4F"
168        "8A0B648F297F8D2C3CB43E4371DC6002B5B12CCC085BDB2CFC5074A0587566187EE3"
169        "E11A2A459BD94726248BB8D6CC62938E11E284C2C183576FBB51749EB238C4360923"
170        "79C08CE1C8CD77EB57404CE9B4744395ACF721487450BADE3220576F2F816248B0A7"
171        "14A264330AECCB24DE2A1107847B23490897#)\n"
172 "))\n";
173 
174 
175 static const char sample_private_dsa_key_3072[] =
176 "(private-key\n"
177 "  (dsa\n"
178 "   (p #00BA73E148AEA5E8B64878AF5BE712B8302B9671C5F3EEB7722A9D0D9868D048C938"
179        "877C91C335C7819292E69C7D34264F1578E32EC2DA8408DF75D0EB76E0D3030B84B5"
180        "62D8EF93AB53BAB6B8A5DE464F5CA87AEA43BDCF0FB0B7815AA3114CFC84FD916A83"
181        "B3D5FD78390189332232E9D037D215313FD002FF46C048B66703F87FAE092AAA0988"
182        "AC745336EBE672A01DEDBD52395783579B67CF3AE1D6F1602CCCB12154FA0E00AE46"
183        "0D9B289CF709194625BCB919B11038DEFC50ADBBA20C3F320078E4E9529B4F6848E2"
184        "AB5E6278DB961FE226F2EEBD201E071C48C5BEF98B4D9BEE42C1C7102D893EBF8902"
185        "D7A91266340AFD6CE1D09E52282FFF5B97EAFA3886A3FCF84FF76D1E06538D0D8E60"
186        "B3332145785E07D29A5965382DE3470D1D888447FA9C00A2373378FC3FA7B9F7D17E"
187        "95A6A5AE1397BE46D976EF2C96E89913AC4A09351CA661BF6F67E30407DA846946C7"
188        "62D9BAA6B77825097D3E7B886456BB32E3E74516BF3FD93D71B257AA8F723E01CE33"
189        "8015353D3778B02B892AF7#)\n"
190 "   (q #00BFF3F3CC18FA018A5B8155A8695E1E4939660D5E4759322C39D50F3B93E5F68B#)\n"
191 "   (g #6CCFD8219F5FCE8EF2BEF3262929787140847E38674B1EF8DB20255E212CB6330EC4"
192        "DFE8A26AB7ECC5760DEB9BBF59A2B2821D510F1868172222867558B8D204E889C474"
193        "7CA30FBF9D8CF41AE5D5BD845174641101593849FF333E6C93A6550931B2B9D56B98"
194        "9CAB01729D9D736FA6D24A74D2DDE1E9E648D141473E443DD6BBF0B3CAB64F9FE4FC"
195        "134B2EB57437789F75C744DF1FA67FA8A64603E5441BC7ECE29E00BDF262BDC81E8C"
196        "7330A18A412DE38E7546D342B89A0AF675A89E6BEF00540EB107A2FE74EA402B0D89"
197        "F5C02918DEEEAF8B8737AC866B09B50810AB8D8668834A1B9E1E53866E2B0A926FAB"
198        "120A0CDE5B3715FFFE6ACD1AB73588DCC1EC4CE9392FE57F8D1D35811200CB07A0E6"
199        "374E2C4B0AEB7E3D077B8545C0E438DCC0F1AE81E186930E99EBC5B91B77E92803E0"
200        "21602887851A4FFDB3A7896AC655A0901218C121C5CBB0931E7D5EAC243F37711B5F"
201        "D5A62B1B38A83F03D8F6703D8B98DF367FC8A76990335F62173A5391836F0F2413EC"
202        "4997AF9EB55C6660B01A#)\n"
203 "   (y #2320B22434C5DB832B4EC267CC52E78DD5CCFA911E8F0804E7E7F32B186B2D4167AE"
204        "4AA6869822E76400492D6A193B0535322C72B0B7AA4A87E33044FDC84BE24C64A053"
205        "A37655EE9EABDCDC1FDF63F3F1C677CEB41595DF7DEFE9178D85A3D621B4E4775492"
206        "8C0A58D2458D06F9562E4DE2FE6129A64063A99E88E54485B97484A28188C4D33F15"
207        "DDC903B6CEA0135E3E3D27B4EA39319696305CE93D7BA7BE00367DBE3AAF43491E71"
208        "CBF254744A5567F5D70090D6139E0C990239627B3A1C5B20B6F9F6374B8D8D8A8997"
209        "437265BE1E3B4810D4B09254400DE287A0DFFBAEF339E48D422B1D41A37E642BC026"
210        "73314701C8FA9792845C129351A87A945A03E6C895860E51D6FB8B7340A94D1A8A7B"
211        "FA85AC83B4B14E73AB86CB96C236C8BFB0978B61B2367A7FE4F7891070F56C78D5DD"
212        "F5576BFE5BE4F333A4E2664E79528B3294907AADD63F4F2E7AA8147B928D8CD69765"
213        "3DB98C4297CB678046ED55C0DBE60BF7142C594603E4D705DC3D17270F9F086EC561"
214        "2703D518D8D49FF0EBE6#)\n"
215 "   (x #00A9FFFC88E67D6F7B810E291C050BAFEA7FC4A75E8D2F16CFED3416FD77607232#)\n"
216 "))\n";
217 
218 static const char sample_public_dsa_key_3072[] =
219 "(public-key\n"
220 "  (dsa\n"
221 "   (p #00BA73E148AEA5E8B64878AF5BE712B8302B9671C5F3EEB7722A9D0D9868D048C938"
222        "877C91C335C7819292E69C7D34264F1578E32EC2DA8408DF75D0EB76E0D3030B84B5"
223        "62D8EF93AB53BAB6B8A5DE464F5CA87AEA43BDCF0FB0B7815AA3114CFC84FD916A83"
224        "B3D5FD78390189332232E9D037D215313FD002FF46C048B66703F87FAE092AAA0988"
225        "AC745336EBE672A01DEDBD52395783579B67CF3AE1D6F1602CCCB12154FA0E00AE46"
226        "0D9B289CF709194625BCB919B11038DEFC50ADBBA20C3F320078E4E9529B4F6848E2"
227        "AB5E6278DB961FE226F2EEBD201E071C48C5BEF98B4D9BEE42C1C7102D893EBF8902"
228        "D7A91266340AFD6CE1D09E52282FFF5B97EAFA3886A3FCF84FF76D1E06538D0D8E60"
229        "B3332145785E07D29A5965382DE3470D1D888447FA9C00A2373378FC3FA7B9F7D17E"
230        "95A6A5AE1397BE46D976EF2C96E89913AC4A09351CA661BF6F67E30407DA846946C7"
231        "62D9BAA6B77825097D3E7B886456BB32E3E74516BF3FD93D71B257AA8F723E01CE33"
232        "8015353D3778B02B892AF7#)\n"
233 "   (q #00BFF3F3CC18FA018A5B8155A8695E1E4939660D5E4759322C39D50F3B93E5F68B#)\n"
234 "   (g #6CCFD8219F5FCE8EF2BEF3262929787140847E38674B1EF8DB20255E212CB6330EC4"
235        "DFE8A26AB7ECC5760DEB9BBF59A2B2821D510F1868172222867558B8D204E889C474"
236        "7CA30FBF9D8CF41AE5D5BD845174641101593849FF333E6C93A6550931B2B9D56B98"
237        "9CAB01729D9D736FA6D24A74D2DDE1E9E648D141473E443DD6BBF0B3CAB64F9FE4FC"
238        "134B2EB57437789F75C744DF1FA67FA8A64603E5441BC7ECE29E00BDF262BDC81E8C"
239        "7330A18A412DE38E7546D342B89A0AF675A89E6BEF00540EB107A2FE74EA402B0D89"
240        "F5C02918DEEEAF8B8737AC866B09B50810AB8D8668834A1B9E1E53866E2B0A926FAB"
241        "120A0CDE5B3715FFFE6ACD1AB73588DCC1EC4CE9392FE57F8D1D35811200CB07A0E6"
242        "374E2C4B0AEB7E3D077B8545C0E438DCC0F1AE81E186930E99EBC5B91B77E92803E0"
243        "21602887851A4FFDB3A7896AC655A0901218C121C5CBB0931E7D5EAC243F37711B5F"
244        "D5A62B1B38A83F03D8F6703D8B98DF367FC8A76990335F62173A5391836F0F2413EC"
245        "4997AF9EB55C6660B01A#)\n"
246 "   (y #2320B22434C5DB832B4EC267CC52E78DD5CCFA911E8F0804E7E7F32B186B2D4167AE"
247        "4AA6869822E76400492D6A193B0535322C72B0B7AA4A87E33044FDC84BE24C64A053"
248        "A37655EE9EABDCDC1FDF63F3F1C677CEB41595DF7DEFE9178D85A3D621B4E4775492"
249        "8C0A58D2458D06F9562E4DE2FE6129A64063A99E88E54485B97484A28188C4D33F15"
250        "DDC903B6CEA0135E3E3D27B4EA39319696305CE93D7BA7BE00367DBE3AAF43491E71"
251        "CBF254744A5567F5D70090D6139E0C990239627B3A1C5B20B6F9F6374B8D8D8A8997"
252        "437265BE1E3B4810D4B09254400DE287A0DFFBAEF339E48D422B1D41A37E642BC026"
253        "73314701C8FA9792845C129351A87A945A03E6C895860E51D6FB8B7340A94D1A8A7B"
254        "FA85AC83B4B14E73AB86CB96C236C8BFB0978B61B2367A7FE4F7891070F56C78D5DD"
255        "F5576BFE5BE4F333A4E2664E79528B3294907AADD63F4F2E7AA8147B928D8CD69765"
256        "3DB98C4297CB678046ED55C0DBE60BF7142C594603E4D705DC3D17270F9F086EC561"
257        "2703D518D8D49FF0EBE6#)\n"
258 "))\n";
259 
260 
261 static const char sample_public_elg_key_1024[] =
262 "(public-key"
263 "  (elg"
264 "   (p #00F7CC7C08AF096B620C545C9353B1140D698FF8BE2D97A3515C17C7F8DABCDB8FB6"
265        "64A46416C90C530C18DF5ABB6C1DDE3AE2FA9DDC9CE40DF644CDE2E759F6DE43F31A"
266        "EEEBC136A460B3E4B0A8F99326A335145B19F4C81B13804894B7D2A30F78A8A7D7F4"
267        "52B83836FDB0DE90BE327FB5E5318757BEF5FE0FC3A5461CBEA0D3#)"
268 "   (g #06#)"
269 "   (y #36B38FB63E3340A0DD8A0468E9FAA512A32DA010BF7110201D0A3DF1B8FEA0E16F3C"
270        "80374584E554804B96EAA8C270FE531F75D0DBD81BA65640EDB1F76D46C27D2925B7"
271        "3EC3B295CDAEEF242904A84D74FB2879425F82D4C5B59BB49A992F85D574168DED85"
272        "D227600BBEF7AF0B8F0DEB785528370E4C4B3E4D65C536122A5A#)"
273 "   ))";
274 static const char sample_private_elg_key_1024[] =
275 "(private-key"
276 "  (elg"
277 "   (p #00F7CC7C08AF096B620C545C9353B1140D698FF8BE2D97A3515C17C7F8DABCDB8FB6"
278        "64A46416C90C530C18DF5ABB6C1DDE3AE2FA9DDC9CE40DF644CDE2E759F6DE43F31A"
279        "EEEBC136A460B3E4B0A8F99326A335145B19F4C81B13804894B7D2A30F78A8A7D7F4"
280        "52B83836FDB0DE90BE327FB5E5318757BEF5FE0FC3A5461CBEA0D3#)"
281 "   (g #06#)"
282 "   (y #36B38FB63E3340A0DD8A0468E9FAA512A32DA010BF7110201D0A3DF1B8FEA0E16F3C"
283        "80374584E554804B96EAA8C270FE531F75D0DBD81BA65640EDB1F76D46C27D2925B7"
284        "3EC3B295CDAEEF242904A84D74FB2879425F82D4C5B59BB49A992F85D574168DED85"
285        "D227600BBEF7AF0B8F0DEB785528370E4C4B3E4D65C536122A5A#)"
286 "   (x #03656C6186FCD27D4A4B1F5010DC0D2AE7833B501E423FCD51DE5EB6D80DACFE#)"
287 "   ))";
288 
289 
290 static const char sample_public_elg_key_2048[] =
291 "(public-key"
292 "  (elg"
293 "   (p #00BE5A2BB4E562D7B644E3D01321CB818DBA27295C339FC2C47EAE9823225EE1E7B6"
294        "38C5DE300E931080E09CC89A18C9D180C16559FEF0D89D6A09534BB86489CCCEE30D"
295        "C18E007A8726BB99F2B2D90D2694597757B120CD2435C0098AD1B74C20004C25BA97"
296        "73EAA4FBEC594EE17F8B25867EEB0F9F857C751116ADED68ADA2A1E9F9F4F40D18F0"
297        "EC1221CA6A746FC5F4CDA2B8B5D0AB83834564ACF6FDBB1AB01D4BFBD1E2C0108FF5"
298        "5FB3190C6D6DA4D95EA683EFA44935CFBC0BF5C6118ACC3768AEA9A98D06024841B8"
299        "D07C234289D22A5E3948F199C397AA991C59A55BEA0C01E91902E039116946FEA135"
300        "768011AF6B622C5AF366EF0196FC4EAEAA8127#)"
301 "   (g #07#)"
302 "   (y #5AFF87BC23D8B97AA62897A5C1CDFFA86C59F39EDBD6012B6F333CE23D872009B8C8"
303        "D1E220E18CFCADFE0AA16346BA2EA132472FFEC746D11C6E758896052313BB501210"
304        "2389C683A25A3464E9B35A192BAE0A3BB99C973126F7560D968C4A754901DC967354"
305        "D61A90ACD56D90DCC4337AFB71FAE3FD18C60EB0D6DD173877DF5DB5199C4931FE4E"
306        "5046F814422580E1162798406FC6554781142DBB7922D4B5B37A111F23761636090F"
307        "6212681E133365191CF15753AE737F17943ED4B7506DE0A85C3B6D63227F9D65ADF8"
308        "2C3DF0676C8F43B5B1C07D9AD4E6D0C812401D7DA7B9484DBA8CD3B73B19A95EB237"
309        "D493E092AEA2371AA904009C8960B0969D12#)"
310 "   ))";
311 static const char sample_private_elg_key_2048[] =
312 "(private-key"
313 "  (elg"
314 "   (p #00BE5A2BB4E562D7B644E3D01321CB818DBA27295C339FC2C47EAE9823225EE1E7B6"
315        "38C5DE300E931080E09CC89A18C9D180C16559FEF0D89D6A09534BB86489CCCEE30D"
316        "C18E007A8726BB99F2B2D90D2694597757B120CD2435C0098AD1B74C20004C25BA97"
317        "73EAA4FBEC594EE17F8B25867EEB0F9F857C751116ADED68ADA2A1E9F9F4F40D18F0"
318        "EC1221CA6A746FC5F4CDA2B8B5D0AB83834564ACF6FDBB1AB01D4BFBD1E2C0108FF5"
319        "5FB3190C6D6DA4D95EA683EFA44935CFBC0BF5C6118ACC3768AEA9A98D06024841B8"
320        "D07C234289D22A5E3948F199C397AA991C59A55BEA0C01E91902E039116946FEA135"
321        "768011AF6B622C5AF366EF0196FC4EAEAA8127#)"
322 "   (g #07#)"
323 "   (y #5AFF87BC23D8B97AA62897A5C1CDFFA86C59F39EDBD6012B6F333CE23D872009B8C8"
324        "D1E220E18CFCADFE0AA16346BA2EA132472FFEC746D11C6E758896052313BB501210"
325        "2389C683A25A3464E9B35A192BAE0A3BB99C973126F7560D968C4A754901DC967354"
326        "D61A90ACD56D90DCC4337AFB71FAE3FD18C60EB0D6DD173877DF5DB5199C4931FE4E"
327        "5046F814422580E1162798406FC6554781142DBB7922D4B5B37A111F23761636090F"
328        "6212681E133365191CF15753AE737F17943ED4B7506DE0A85C3B6D63227F9D65ADF8"
329        "2C3DF0676C8F43B5B1C07D9AD4E6D0C812401D7DA7B9484DBA8CD3B73B19A95EB237"
330        "D493E092AEA2371AA904009C8960B0969D12#)"
331 "   (x #0628C3903972C55BDC1BC4223075616D3F3BA57D55532DDB40CB14CF72070E0D28BF"
332        "D0402B9088D25ED8FC#)"
333 "  ))";
334 
335 static const char sample_public_elg_key_3072[] =
336 "(public-key"
337 "  (elg"
338 "   (p #008EAA3497AFE3706E1A57FFA52E68C64C500731B58EBAFEB51C4A20AB15BA57FA72"
339        "BA1510A4703D5AA6F05DB67E4A776F92AD08800577DC686D00B793167A5D79C997E0"
340        "5B9A9E5974B4B68B4D71ED8EC37F2F45235D901997D72915643F058E712AA18275A2"
341        "C6F9F7C2B9B7CD1E814D215F12A840800B546AEF2A2E6C077CDD1A322738FFD36DB2"
342        "FA5420B5848EED870BC1A6CF55040AE8D2A5945F11AE2BCBE107B41A59EFDBD3B05C"
343        "F4C876C02C9AEAE22CD4C86806A415302936E4C1E5AA59DBBCCD2F83C20941A29888"
344        "A70ADB94D3B8A6489C46BF2C5219CD9FD2341EA21D4E68A4ECC468FD09D215FE96D4"
345        "7AEA12FD22B2456D2CC13672FC7E9772A365C68668157C51E46966B6A1831C429BA0"
346        "D513519713C49C13C5FC7C14BE0A117627B204C4478D0A93C6B57929E448C9B65BF2"
347        "390E04BC5940320C0262FC1A221E7C796493432239A6F12BC62C5CF32E8ADBC1730C"
348        "84C6E6E6BD95AF62835941F3F344AF46BFE5A8F629D5FA699FE37EF8B8C6A2484E42"
349        "D226206FDF7D1FB93A5457#)"
350 "   (g #0B#)"
351 "   (y #18E734FF645AE169079AEAFC78772371089AD3088627ECF77034AFBDF33ADF594AAF"
352        "3288F6979E0DB59CE3D2F0FEE031DFF187F1E4549D3C79668794CB19C14481ECDE2D"
353        "D50861AB674F87A011D50D35F28E424D0D2353850899C2CDD0CC8FDBFC5A0CA395F0"
354        "E605D46CBDD140DBEF426EBD638C9ADD83C195C45CE84ED2D2B21B87800C783A4F79"
355        "12226FEFBDA01C66B254534A51765AF09687275AA80C5DFBA143A6262E47C547D7E2"
356        "289413F8C5C56AED3FA7E5DF5526958E2294FE318AF590C0E720029C202563E6E686"
357        "9EC810F39A859262FB6047C1D418CAA9047A00BDB127B44B69CF6BC8E6B3709B4C23"
358        "79783C5F8457EFE23EDA6FF00D1DDCC29268FC4A6C18577BE2B7004089CBB824027A"
359        "A53C86B51DB054CC83B4F50C8923E2E9431F0A77D741237226CC68591083A2E40171"
360        "5C7B74100BB74003E2264F8B44A0B0BC5404C44218ABE65C04AA573877506CE4F48C"
361        "9E3F8AD1CD8DD9F285DD015C2FC5DEBCFA5779AD87F0BBC62E9EC6246021AB450DB9"
362        "4DDDEFAFD2C7C66E235D#)"
363 "   ))";
364 static const char sample_private_elg_key_3072[] =
365 "(private-key"
366 "  (elg"
367 "   (p #008EAA3497AFE3706E1A57FFA52E68C64C500731B58EBAFEB51C4A20AB15BA57FA72"
368        "BA1510A4703D5AA6F05DB67E4A776F92AD08800577DC686D00B793167A5D79C997E0"
369        "5B9A9E5974B4B68B4D71ED8EC37F2F45235D901997D72915643F058E712AA18275A2"
370        "C6F9F7C2B9B7CD1E814D215F12A840800B546AEF2A2E6C077CDD1A322738FFD36DB2"
371        "FA5420B5848EED870BC1A6CF55040AE8D2A5945F11AE2BCBE107B41A59EFDBD3B05C"
372        "F4C876C02C9AEAE22CD4C86806A415302936E4C1E5AA59DBBCCD2F83C20941A29888"
373        "A70ADB94D3B8A6489C46BF2C5219CD9FD2341EA21D4E68A4ECC468FD09D215FE96D4"
374        "7AEA12FD22B2456D2CC13672FC7E9772A365C68668157C51E46966B6A1831C429BA0"
375        "D513519713C49C13C5FC7C14BE0A117627B204C4478D0A93C6B57929E448C9B65BF2"
376        "390E04BC5940320C0262FC1A221E7C796493432239A6F12BC62C5CF32E8ADBC1730C"
377        "84C6E6E6BD95AF62835941F3F344AF46BFE5A8F629D5FA699FE37EF8B8C6A2484E42"
378        "D226206FDF7D1FB93A5457#)"
379 "   (g #0B#)"
380 "   (y #18E734FF645AE169079AEAFC78772371089AD3088627ECF77034AFBDF33ADF594AAF"
381        "3288F6979E0DB59CE3D2F0FEE031DFF187F1E4549D3C79668794CB19C14481ECDE2D"
382        "D50861AB674F87A011D50D35F28E424D0D2353850899C2CDD0CC8FDBFC5A0CA395F0"
383        "E605D46CBDD140DBEF426EBD638C9ADD83C195C45CE84ED2D2B21B87800C783A4F79"
384        "12226FEFBDA01C66B254534A51765AF09687275AA80C5DFBA143A6262E47C547D7E2"
385        "289413F8C5C56AED3FA7E5DF5526958E2294FE318AF590C0E720029C202563E6E686"
386        "9EC810F39A859262FB6047C1D418CAA9047A00BDB127B44B69CF6BC8E6B3709B4C23"
387        "79783C5F8457EFE23EDA6FF00D1DDCC29268FC4A6C18577BE2B7004089CBB824027A"
388        "A53C86B51DB054CC83B4F50C8923E2E9431F0A77D741237226CC68591083A2E40171"
389        "5C7B74100BB74003E2264F8B44A0B0BC5404C44218ABE65C04AA573877506CE4F48C"
390        "9E3F8AD1CD8DD9F285DD015C2FC5DEBCFA5779AD87F0BBC62E9EC6246021AB450DB9"
391        "4DDDEFAFD2C7C66E235D#)"
392 "   (x #03A73F0389E470AAC831B039F8AA0C4EBD3A47DD083E32EEA08E4911236CD597C272"
393        "9823D47A51C8535DA52FE6DAB3E8D1C20D#)"
394 "  ))";
395 
396 
397 #define BUG() do {fprintf ( stderr, "Ooops at %s:%d\n", __FILE__ , __LINE__ );\
398 		  exit(2);} while(0)
399 
400 
401 static void
show_sexp(const char * prefix,gcry_sexp_t a)402 show_sexp (const char *prefix, gcry_sexp_t a)
403 {
404   char *buf;
405   size_t size;
406 
407   fputs (prefix, stderr);
408   size = gcry_sexp_sprint (a, GCRYSEXP_FMT_ADVANCED, NULL, 0);
409   buf = malloc (size);
410   if (!buf)
411     die ("out of core\n");
412 
413   gcry_sexp_sprint (a, GCRYSEXP_FMT_ADVANCED, buf, size);
414   fprintf (stderr, "%.*s", (int)size, buf);
415 }
416 
417 
418 static void
progress_cb(void * cb_data,const char * what,int printchar,int current,int total)419 progress_cb (void *cb_data, const char *what, int printchar,
420              int current, int total)
421 {
422   (void)cb_data;
423 
424   if (single_char_progress)
425     {
426       fputc (printchar, stdout);
427       fflush (stderr);
428     }
429   else
430     {
431       fprintf (stderr, PGM ": progress (%s %c %d %d)\n",
432                what, printchar, current, total);
433       fflush (stderr);
434     }
435 }
436 
437 
438 static void
random_bench(int very_strong)439 random_bench (int very_strong)
440 {
441   char buf[128];
442   int i;
443 
444   printf ("%-10s", "random");
445 
446   if (!very_strong)
447     {
448       start_timer ();
449       for (i=0; i < 100; i++)
450         gcry_randomize (buf, sizeof buf, GCRY_STRONG_RANDOM);
451       stop_timer ();
452       printf (" %s", elapsed_time (1));
453     }
454 
455   start_timer ();
456   for (i=0; i < 100; i++)
457     gcry_randomize (buf, 8,
458                     very_strong? GCRY_VERY_STRONG_RANDOM:GCRY_STRONG_RANDOM);
459   stop_timer ();
460   printf (" %s", elapsed_time (1));
461 
462   putchar ('\n');
463   if (verbose)
464     xgcry_control ((GCRYCTL_DUMP_RANDOM_STATS));
465 }
466 
467 
468 
469 static void
md_bench(const char * algoname)470 md_bench ( const char *algoname )
471 {
472   int algo;
473   gcry_md_hd_t hd;
474   int i, j, repcount;
475   char buf_base[1000+15];
476   size_t bufsize = 1000;
477   char *buf;
478   char *largebuf_base;
479   char *largebuf;
480   char digest[512/8];
481   gcry_error_t err = GPG_ERR_NO_ERROR;
482 
483   if (!algoname)
484     {
485       for (i=1; i < 400; i++)
486         if (in_fips_mode && i == GCRY_MD_MD5)
487           ; /* Don't use MD5 in fips mode.  */
488         else if ( !gcry_md_test_algo (i) )
489           md_bench (gcry_md_algo_name (i));
490       return;
491     }
492 
493   buf = buf_base + ((16 - ((size_t)buf_base & 0x0f)) % buffer_alignment);
494 
495   algo = gcry_md_map_name (algoname);
496   if (!algo)
497     {
498       fprintf (stderr, PGM ": invalid hash algorithm `%s'\n", algoname);
499       exit (1);
500     }
501 
502   err = gcry_md_open (&hd, algo, 0);
503   if (err)
504     {
505       fprintf (stderr, PGM ": error opening hash algorithm `%s'\n", algoname);
506       exit (1);
507     }
508 
509   for (i=0; i < bufsize; i++)
510     buf[i] = i;
511 
512   printf ("%-12s", gcry_md_algo_name (algo));
513 
514   start_timer ();
515   for (repcount=0; repcount < hash_repetitions; repcount++)
516     for (i=0; i < 1000; i++)
517       gcry_md_write (hd, buf, bufsize);
518   gcry_md_final (hd);
519   stop_timer ();
520   printf (" %s", elapsed_time (1));
521   fflush (stdout);
522 
523   gcry_md_reset (hd);
524   start_timer ();
525   for (repcount=0; repcount < hash_repetitions; repcount++)
526     for (i=0; i < 10000; i++)
527       gcry_md_write (hd, buf, bufsize/10);
528   gcry_md_final (hd);
529   stop_timer ();
530   printf (" %s", elapsed_time (1));
531   fflush (stdout);
532 
533   gcry_md_reset (hd);
534   start_timer ();
535   for (repcount=0; repcount < hash_repetitions; repcount++)
536     for (i=0; i < 1000000; i++)
537       gcry_md_write (hd, buf, 1);
538   gcry_md_final (hd);
539   stop_timer ();
540   printf (" %s", elapsed_time (1));
541   fflush (stdout);
542 
543   start_timer ();
544   for (repcount=0; repcount < hash_repetitions; repcount++)
545     for (i=0; i < 1000; i++)
546       for (j=0; j < bufsize; j++)
547         gcry_md_putc (hd, buf[j]);
548   gcry_md_final (hd);
549   stop_timer ();
550   printf (" %s", elapsed_time (1));
551   fflush (stdout);
552 
553   gcry_md_close (hd);
554 
555   /* Now 100 hash operations on 10000 bytes using the fast function.
556      We initialize the buffer so that all memory pages are committed
557      and we have repeatable values.  */
558   if (gcry_md_get_algo_dlen (algo) > sizeof digest)
559     die ("digest buffer too short\n");
560 
561   if (gcry_md_get_algo_dlen (algo))
562     {
563       largebuf_base = malloc (10000+15);
564       if (!largebuf_base)
565         die ("out of core\n");
566       largebuf = (largebuf_base
567                   + ((16 - ((size_t)largebuf_base & 0x0f)) % buffer_alignment));
568 
569       for (i=0; i < 10000; i++)
570         largebuf[i] = i;
571       start_timer ();
572       for (repcount=0; repcount < hash_repetitions; repcount++)
573         for (i=0; i < 100; i++)
574           gcry_md_hash_buffer (algo, digest, largebuf, 10000);
575       stop_timer ();
576       printf (" %s", elapsed_time (1));
577       free (largebuf_base);
578     }
579 
580   putchar ('\n');
581   fflush (stdout);
582 }
583 
584 
585 
586 static void
mac_bench(const char * algoname)587 mac_bench ( const char *algoname )
588 {
589   int algo;
590   gcry_mac_hd_t hd;
591   int step, pos, j, i, repcount;
592   char buf_base[1000+15];
593   size_t bufsize = 1000;
594   char *buf;
595   char mac[3][512];
596   char key[512];
597   unsigned int maclen, keylen;
598   size_t macoutlen;
599   gcry_error_t err = GPG_ERR_NO_ERROR;
600 
601   if (!algoname)
602     {
603       for (i=1; i < 600; i++)
604         if (in_fips_mode && i == GCRY_MAC_HMAC_MD5)
605           ; /* Don't use MD5 in fips mode.  */
606         else if ( !gcry_mac_test_algo (i) )
607           mac_bench (gcry_mac_algo_name (i));
608       return;
609     }
610 
611   buf = buf_base + ((16 - ((size_t)buf_base & 0x0f)) % buffer_alignment);
612 
613   algo = gcry_mac_map_name (algoname);
614   if (!algo)
615     {
616       fprintf (stderr, PGM ": invalid MAC algorithm `%s'\n", algoname);
617       exit (1);
618     }
619 
620   maclen = gcry_mac_get_algo_maclen (algo);
621   if (maclen > sizeof(mac))
622     maclen = sizeof(mac);
623 
624   keylen = gcry_mac_get_algo_keylen (algo);
625   if (keylen == 0)
626     keylen = 32;
627   if (keylen > sizeof(key))
628     keylen = sizeof(key);
629   for (i=0; i < keylen; i++)
630     key[i] = (keylen - i) ^ 0x54;
631 
632   err = gcry_mac_open (&hd, algo, 0, NULL);
633   if (err)
634     {
635       fprintf (stderr, PGM ": error opening mac algorithm `%s': %s\n", algoname,
636                gpg_strerror (err));
637       exit (1);
638     }
639 
640   err = gcry_mac_setkey (hd, key, keylen);
641   if (err)
642     {
643       fprintf (stderr, PGM ": error setting key for mac algorithm `%s': %s\n",
644                algoname, gpg_strerror (err));
645       exit (1);
646     }
647 
648   for (i=0; i < bufsize; i++)
649     buf[i] = i;
650 
651   if (algo >= GCRY_MAC_POLY1305_AES && algo <= GCRY_MAC_POLY1305_SEED)
652     {
653       static const char iv[16] = { 1, 2, 3, 4, };
654       err = gcry_mac_setiv(hd, iv, sizeof(iv));
655       if (err)
656         {
657           fprintf (stderr, PGM ": error setting nonce for mac algorithm `%s': %s\n",
658                    algoname, gpg_strerror (err));
659           exit (1);
660         }
661     }
662 
663   printf ("%-20s", gcry_mac_algo_name (algo));
664 
665   start_timer ();
666   for (repcount=0; repcount < mac_repetitions; repcount++)
667     for (i=0; i < 1000; i++)
668       gcry_mac_write (hd, buf, bufsize);
669   macoutlen = maclen;
670   gcry_mac_read (hd, mac[0], &macoutlen);
671   stop_timer ();
672   printf (" %s", elapsed_time (1));
673   fflush (stdout);
674 
675   gcry_mac_reset (hd);
676   start_timer ();
677   for (repcount=0; repcount < mac_repetitions; repcount++)
678     for (i=0; i < 1000; i++)
679       for (step=bufsize/10, pos=0, j=0; j < 10; j++, pos+=step)
680         gcry_mac_write (hd, &buf[pos], step);
681   macoutlen = maclen;
682   gcry_mac_read (hd, mac[1], &macoutlen);
683   stop_timer ();
684   printf (" %s", elapsed_time (1));
685   fflush (stdout);
686 
687   gcry_mac_reset (hd);
688   start_timer ();
689   for (repcount=0; repcount < mac_repetitions; repcount++)
690     for (i=0; i < 1000; i++)
691       for (step=bufsize/100, pos=0, j=0; j < 100; j++, pos+=step)
692         gcry_mac_write (hd, &buf[pos], step);
693   macoutlen = maclen;
694   gcry_mac_read (hd, mac[2], &macoutlen);
695   stop_timer ();
696   printf (" %s", elapsed_time (1));
697   fflush (stdout);
698 
699   gcry_mac_close (hd);
700 
701   for (i=1; i < 3; i++)
702     {
703       if (memcmp(mac[i-1], mac[i], maclen))
704         {
705           fprintf (stderr, PGM ": mac mismatch with algorithm `%s'\n",
706                    algoname);
707           exit(1);
708         }
709     }
710 
711   putchar ('\n');
712   fflush (stdout);
713 }
714 
715 
ccm_aead_init(gcry_cipher_hd_t hd,size_t buflen,int authlen)716 static void ccm_aead_init(gcry_cipher_hd_t hd, size_t buflen, int authlen)
717 {
718   const int _L = 4;
719   const int noncelen = 15 - _L;
720   char nonce[noncelen];
721   u64 params[3];
722   gcry_error_t err = GPG_ERR_NO_ERROR;
723 
724   memset (nonce, 0x33, noncelen);
725 
726   err = gcry_cipher_setiv (hd, nonce, noncelen);
727   if (err)
728     {
729       fprintf (stderr, "gcry_cipher_setiv failed: %s\n",
730                gpg_strerror (err));
731       gcry_cipher_close (hd);
732       exit (1);
733     }
734 
735   params[0] = buflen; /* encryptedlen */
736   params[1] = 0; /* aadlen */
737   params[2] = authlen; /* authtaglen */
738   err = gcry_cipher_ctl (hd, GCRYCTL_SET_CCM_LENGTHS, params, sizeof(params));
739   if (err)
740     {
741       fprintf (stderr, "gcry_cipher_setiv failed: %s\n",
742                gpg_strerror (err));
743       gcry_cipher_close (hd);
744       exit (1);
745     }
746 }
747 
748 
749 static gcry_error_t
cipher_encrypt(gcry_cipher_hd_t h,char * out,size_t outsize,const char * in,size_t inlen,size_t max_inlen)750 cipher_encrypt (gcry_cipher_hd_t h, char *out, size_t outsize,
751 		const char *in, size_t inlen, size_t max_inlen)
752 {
753   gcry_error_t ret;
754 
755   while (inlen)
756     {
757       size_t currlen = inlen;
758 
759       if (currlen > max_inlen)
760 	currlen = max_inlen;
761 
762       ret = gcry_cipher_encrypt(h, out, outsize, in, currlen);
763       if (ret)
764 	return ret;
765 
766       out += currlen;
767       in += currlen;
768       outsize -= currlen;
769       inlen -= currlen;
770     }
771 
772   return 0;
773 }
774 
775 
776 static gcry_error_t
cipher_decrypt(gcry_cipher_hd_t h,char * out,size_t outsize,const char * in,size_t inlen,size_t max_inlen)777 cipher_decrypt (gcry_cipher_hd_t h, char *out, size_t outsize,
778 		const char *in, size_t inlen, size_t max_inlen)
779 {
780   gcry_error_t ret;
781 
782   while (inlen)
783     {
784       size_t currlen = inlen;
785 
786       if (currlen > max_inlen)
787 	currlen = max_inlen;
788 
789       ret = gcry_cipher_decrypt(h, out, outsize, in, currlen);
790       if (ret)
791 	return ret;
792 
793       out += currlen;
794       in += currlen;
795       outsize -= currlen;
796       inlen -= currlen;
797     }
798 
799   return 0;
800 }
801 
802 
803 static void
cipher_bench(const char * algoname)804 cipher_bench ( const char *algoname )
805 {
806   static int header_printed;
807   int algo;
808   gcry_cipher_hd_t hd;
809   int i;
810   int keylen, blklen;
811   char key[128];
812   char *outbuf, *buf;
813   char *raw_outbuf, *raw_buf;
814   size_t allocated_buflen, buflen;
815   int repetitions;
816   static const struct {
817     int mode;
818     const char *name;
819     int blocked;
820     unsigned int max_inlen;
821     void (* const aead_init)(gcry_cipher_hd_t hd, size_t buflen, int authlen);
822     int req_blocksize;
823     int authlen;
824     int noncelen;
825     int doublekey;
826   } modes[] = {
827     { GCRY_CIPHER_MODE_ECB, "   ECB/Stream", 1, 0xffffffffU },
828     { GCRY_CIPHER_MODE_CBC, " CBC/Poly1305", 1, 0xffffffffU },
829     { GCRY_CIPHER_MODE_CFB, "      CFB", 0, 0xffffffffU },
830     { GCRY_CIPHER_MODE_OFB, "      OFB", 0, 0xffffffffU },
831     { GCRY_CIPHER_MODE_CTR, "      CTR", 0, 0xffffffffU },
832     { GCRY_CIPHER_MODE_XTS, "      XTS", 0, 16 << 20,
833       NULL, GCRY_XTS_BLOCK_LEN, 0, 0, 1 },
834     { GCRY_CIPHER_MODE_CCM, "      CCM", 0, 0xffffffffU,
835       ccm_aead_init, GCRY_CCM_BLOCK_LEN, 8, },
836     { GCRY_CIPHER_MODE_GCM, "      GCM", 0, 0xffffffffU,
837       NULL, GCRY_GCM_BLOCK_LEN, GCRY_GCM_BLOCK_LEN },
838     { GCRY_CIPHER_MODE_OCB, "      OCB", 1, 0xffffffffU,
839       NULL, 16, 16, 15 },
840     { GCRY_CIPHER_MODE_EAX, "      EAX", 0, 0xffffffffU,
841       NULL, 0, 8, 8 },
842     { GCRY_CIPHER_MODE_STREAM, "", 0, 0xffffffffU },
843     { GCRY_CIPHER_MODE_POLY1305, "", 0, 0xffffffffU,
844       NULL, 1, 16, 12 },
845     {0}
846   };
847   int modeidx;
848   gcry_error_t err = GPG_ERR_NO_ERROR;
849 
850   if (!algoname)
851     {
852       for (i=1; i < 400; i++)
853         if ( !gcry_cipher_test_algo (i) )
854           cipher_bench (gcry_cipher_algo_name (i));
855       return;
856     }
857 
858   if (huge_buffers)
859     {
860       allocated_buflen = 256 * 1024 * 1024;
861       repetitions = 4;
862     }
863   else if (large_buffers)
864     {
865       allocated_buflen = 1024 * 100;
866       repetitions = 10;
867     }
868   else
869     {
870       allocated_buflen = 1024;
871       repetitions = 1000;
872     }
873   repetitions *= cipher_repetitions;
874 
875   raw_buf = gcry_xcalloc (allocated_buflen+15, 1);
876   buf = (raw_buf
877          + ((16 - ((size_t)raw_buf & 0x0f)) % buffer_alignment));
878   outbuf = raw_outbuf = gcry_xmalloc (allocated_buflen+15);
879   outbuf = (raw_outbuf
880             + ((16 - ((size_t)raw_outbuf & 0x0f)) % buffer_alignment));
881 
882   if (!header_printed)
883     {
884       if (cipher_repetitions != 1)
885         printf ("Running each test %d times.\n", cipher_repetitions);
886       printf ("%-12s", "");
887       for (modeidx=0; modes[modeidx].mode; modeidx++)
888         if (*modes[modeidx].name)
889           printf (" %-15s", modes[modeidx].name );
890       putchar ('\n');
891       printf ("%-12s", "");
892       for (modeidx=0; modes[modeidx].mode; modeidx++)
893         if (*modes[modeidx].name)
894           printf (" ---------------" );
895       putchar ('\n');
896       header_printed = 1;
897     }
898 
899   algo = gcry_cipher_map_name (algoname);
900   if (!algo)
901     {
902       fprintf (stderr, PGM ": invalid cipher algorithm `%s'\n", algoname);
903       exit (1);
904     }
905 
906   keylen = gcry_cipher_get_algo_keylen (algo);
907   if (!keylen)
908     {
909       fprintf (stderr, PGM ": failed to get key length for algorithm `%s'\n",
910 	       algoname);
911       exit (1);
912     }
913   if ( keylen * 2 > sizeof key )
914     {
915         fprintf (stderr, PGM ": algo %d, keylength problem (%d)\n",
916                  algo, keylen );
917         exit (1);
918     }
919   for (i=0; i < keylen * 2; i++)
920     key[i] = i + (clock () & 0xff);
921 
922   blklen = gcry_cipher_get_algo_blklen (algo);
923   if (!blklen)
924     {
925       fprintf (stderr, PGM ": failed to get block length for algorithm `%s'\n",
926 	       algoname);
927       exit (1);
928     }
929 
930   printf ("%-12s", gcry_cipher_algo_name (algo));
931   fflush (stdout);
932 
933   for (modeidx=0; modes[modeidx].mode; modeidx++)
934     {
935       size_t modekeylen = keylen * (!!modes[modeidx].doublekey + 1);
936       int is_stream = modes[modeidx].mode == GCRY_CIPHER_MODE_STREAM
937                       || modes[modeidx].mode == GCRY_CIPHER_MODE_POLY1305;
938 
939       if ((blklen > 1 && is_stream) || (blklen == 1 && !is_stream))
940         continue;
941 
942       if (modes[modeidx].mode == GCRY_CIPHER_MODE_POLY1305
943           && algo != GCRY_CIPHER_CHACHA20)
944         continue;
945 
946       if (modes[modeidx].req_blocksize > 0
947           && blklen != modes[modeidx].req_blocksize)
948         {
949           printf (" %7s %7s", "-", "-" );
950           continue;
951         }
952 
953       for (i=0; i < sizeof buf; i++)
954         buf[i] = i;
955 
956       err = gcry_cipher_open (&hd, algo, modes[modeidx].mode, 0);
957       if (err)
958         {
959           fprintf (stderr, PGM ": error opening cipher `%s'\n", algoname);
960           exit (1);
961         }
962 
963       if (!cipher_with_keysetup)
964         {
965           err = gcry_cipher_setkey (hd, key, modekeylen);
966           if (err)
967             {
968               fprintf (stderr, "gcry_cipher_setkey failed: %s\n",
969                        gpg_strerror (err));
970               gcry_cipher_close (hd);
971               exit (1);
972             }
973         }
974 
975       buflen = allocated_buflen;
976       if (modes[modeidx].blocked)
977         buflen = (buflen / blklen) * blklen;
978 
979       start_timer ();
980       for (i=err=0; !err && i < repetitions; i++)
981         {
982           if (cipher_with_keysetup)
983             {
984               err = gcry_cipher_setkey (hd, key, modekeylen);
985               if (err)
986                 {
987                   fprintf (stderr, "gcry_cipher_setkey failed: %s\n",
988                            gpg_strerror (err));
989                   gcry_cipher_close (hd);
990                   exit (1);
991                 }
992             }
993 
994           if (modes[modeidx].noncelen)
995             {
996               char nonce[100];
997               size_t noncelen;
998 
999               noncelen = modes[modeidx].noncelen;
1000               if (noncelen > sizeof nonce)
1001                 noncelen = sizeof nonce;
1002               memset (nonce, 42, noncelen);
1003               err = gcry_cipher_setiv (hd, nonce, noncelen);
1004               if (err)
1005                 {
1006                   fprintf (stderr, "gcry_cipher_setiv failed: %s\n",
1007                            gpg_strerror (err));
1008                   gcry_cipher_close (hd);
1009                   exit (1);
1010                 }
1011             }
1012 
1013           if (modes[modeidx].aead_init)
1014             {
1015               (*modes[modeidx].aead_init) (hd, buflen, modes[modeidx].authlen);
1016               gcry_cipher_final (hd);
1017               err = cipher_encrypt (hd, outbuf, buflen, buf, buflen,
1018 				    modes[modeidx].max_inlen);
1019               if (err)
1020                 break;
1021               err = gcry_cipher_gettag (hd, outbuf, modes[modeidx].authlen);
1022             }
1023           else
1024             {
1025               err = cipher_encrypt (hd, outbuf, buflen, buf, buflen,
1026 				    modes[modeidx].max_inlen);
1027             }
1028         }
1029       stop_timer ();
1030 
1031       printf (" %s", elapsed_time (1));
1032       fflush (stdout);
1033       gcry_cipher_close (hd);
1034       if (err)
1035         {
1036           fprintf (stderr, "gcry_cipher_encrypt failed: %s\n",
1037                    gpg_strerror (err) );
1038           exit (1);
1039         }
1040 
1041       err = gcry_cipher_open (&hd, algo, modes[modeidx].mode, 0);
1042       if (err)
1043         {
1044           fprintf (stderr, PGM ": error opening cipher `%s'/n", algoname);
1045           exit (1);
1046         }
1047 
1048       if (!cipher_with_keysetup)
1049         {
1050           err = gcry_cipher_setkey (hd, key, modekeylen);
1051           if (err)
1052             {
1053               fprintf (stderr, "gcry_cipher_setkey failed: %s\n",
1054                        gpg_strerror (err));
1055               gcry_cipher_close (hd);
1056               exit (1);
1057             }
1058         }
1059 
1060       start_timer ();
1061       for (i=err=0; !err && i < repetitions; i++)
1062         {
1063           if (cipher_with_keysetup)
1064             {
1065               err = gcry_cipher_setkey (hd, key, modekeylen);
1066               if (err)
1067                 {
1068                   fprintf (stderr, "gcry_cipher_setkey failed: %s\n",
1069                            gpg_strerror (err));
1070                   gcry_cipher_close (hd);
1071                   exit (1);
1072                 }
1073             }
1074 
1075           if (modes[modeidx].noncelen)
1076             {
1077               char nonce[100];
1078               size_t noncelen;
1079 
1080               noncelen = modes[modeidx].noncelen;
1081               if (noncelen > sizeof nonce)
1082                 noncelen = sizeof nonce;
1083               memset (nonce, 42, noncelen);
1084               err = gcry_cipher_setiv (hd, nonce, noncelen);
1085               if (err)
1086                 {
1087                   fprintf (stderr, "gcry_cipher_setiv failed: %s\n",
1088                            gpg_strerror (err));
1089                   gcry_cipher_close (hd);
1090                   exit (1);
1091                 }
1092             }
1093 
1094           if (modes[modeidx].aead_init)
1095             {
1096               (*modes[modeidx].aead_init) (hd, buflen, modes[modeidx].authlen);
1097               gcry_cipher_final (hd);
1098               err = cipher_decrypt (hd, outbuf, buflen, buf, buflen,
1099 				    modes[modeidx].max_inlen);
1100               if (err)
1101                 break;
1102               err = gcry_cipher_checktag (hd, outbuf, modes[modeidx].authlen);
1103               if (gpg_err_code (err) == GPG_ERR_CHECKSUM)
1104                 err = 0;
1105             }
1106           else
1107             {
1108               gcry_cipher_final (hd);
1109               err = cipher_decrypt (hd, outbuf, buflen, buf, buflen,
1110 				    modes[modeidx].max_inlen);
1111             }
1112         }
1113       stop_timer ();
1114       printf (" %s", elapsed_time (1));
1115       fflush (stdout);
1116       gcry_cipher_close (hd);
1117       if (err)
1118         {
1119           fprintf (stderr, "gcry_cipher_decrypt failed: %s\n",
1120                    gpg_strerror (err) );
1121           exit (1);
1122         }
1123     }
1124 
1125   putchar ('\n');
1126   gcry_free (raw_buf);
1127   gcry_free (raw_outbuf);
1128 }
1129 
1130 
1131 
1132 static void
rsa_bench(int iterations,int print_header,int no_blinding)1133 rsa_bench (int iterations, int print_header, int no_blinding)
1134 {
1135   gpg_error_t err;
1136   int p_sizes[] = { 1024, 2048, 3072, 4096 };
1137   int testno;
1138 
1139   if (print_header)
1140     printf ("Algorithm         generate %4d*priv %4d*public\n"
1141             "------------------------------------------------\n",
1142             iterations, iterations );
1143   for (testno=0; testno < DIM (p_sizes); testno++)
1144     {
1145       gcry_sexp_t key_spec, key_pair, pub_key, sec_key;
1146       gcry_mpi_t x;
1147       gcry_sexp_t data;
1148       gcry_sexp_t sig = NULL;
1149       int count;
1150       unsigned nbits = p_sizes[testno];
1151 
1152       printf ("RSA %3d bit    ", nbits);
1153       fflush (stdout);
1154 
1155       if (in_fips_mode && !(nbits == 2048 || nbits == 3072))
1156         {
1157           puts ("[skipped in fips mode]");
1158           continue;
1159         }
1160 
1161       err = gcry_sexp_build (&key_spec, NULL,
1162                              gcry_fips_mode_active ()
1163                              ? "(genkey (RSA (nbits %d)))"
1164                              : "(genkey (RSA (nbits %d)(transient-key)))",
1165                              nbits);
1166       if (err)
1167         die ("creating S-expression failed: %s\n", gcry_strerror (err));
1168 
1169       start_timer ();
1170       err = gcry_pk_genkey (&key_pair, key_spec);
1171       if (err)
1172         die ("creating %d bit RSA key failed: %s\n",
1173              nbits, gcry_strerror (err));
1174 
1175       pub_key = gcry_sexp_find_token (key_pair, "public-key", 0);
1176       if (! pub_key)
1177         die ("public part missing in key\n");
1178       sec_key = gcry_sexp_find_token (key_pair, "private-key", 0);
1179       if (! sec_key)
1180         die ("private part missing in key\n");
1181       gcry_sexp_release (key_pair);
1182       gcry_sexp_release (key_spec);
1183 
1184       stop_timer ();
1185       printf ("   %s", elapsed_time (1));
1186       fflush (stdout);
1187 
1188       x = gcry_mpi_new (nbits);
1189       gcry_mpi_randomize (x, nbits-8, GCRY_WEAK_RANDOM);
1190       err = gcry_sexp_build (&data, NULL,
1191                              "(data (flags raw) (value %m))", x);
1192       gcry_mpi_release (x);
1193       if (err)
1194         die ("converting data failed: %s\n", gcry_strerror (err));
1195 
1196       start_timer ();
1197       for (count=0; count < iterations; count++)
1198         {
1199           gcry_sexp_release (sig);
1200           err = gcry_pk_sign (&sig, data, sec_key);
1201           if (err)
1202             die ("signing failed (%d): %s\n", count, gpg_strerror (err));
1203         }
1204       stop_timer ();
1205       printf ("   %s", elapsed_time (1));
1206       fflush (stdout);
1207 
1208       start_timer ();
1209       for (count=0; count < iterations; count++)
1210         {
1211           err = gcry_pk_verify (sig, data, pub_key);
1212           if (err)
1213             {
1214               putchar ('\n');
1215               show_sexp ("seckey:\n", sec_key);
1216               show_sexp ("data:\n", data);
1217               show_sexp ("sig:\n", sig);
1218               die ("verify failed (%d): %s\n", count, gpg_strerror (err));
1219             }
1220         }
1221       stop_timer ();
1222       printf ("     %s", elapsed_time (1));
1223 
1224       if (no_blinding)
1225         {
1226           fflush (stdout);
1227           x = gcry_mpi_new (nbits);
1228           gcry_mpi_randomize (x, nbits-8, GCRY_WEAK_RANDOM);
1229           err = gcry_sexp_build (&data, NULL,
1230                                  "(data (flags no-blinding) (value %m))", x);
1231           gcry_mpi_release (x);
1232           if (err)
1233             die ("converting data failed: %s\n", gcry_strerror (err));
1234 
1235           start_timer ();
1236           for (count=0; count < iterations; count++)
1237             {
1238               gcry_sexp_release (sig);
1239               err = gcry_pk_sign (&sig, data, sec_key);
1240               if (err)
1241                 die ("signing failed (%d): %s\n", count, gpg_strerror (err));
1242             }
1243           stop_timer ();
1244           printf ("   %s", elapsed_time (1));
1245           fflush (stdout);
1246         }
1247 
1248       putchar ('\n');
1249       fflush (stdout);
1250 
1251       gcry_sexp_release (sig);
1252       gcry_sexp_release (data);
1253       gcry_sexp_release (sec_key);
1254       gcry_sexp_release (pub_key);
1255     }
1256 }
1257 
1258 
1259 static void
elg_bench(int iterations,int print_header)1260 elg_bench (int iterations, int print_header)
1261 {
1262   gpg_error_t err;
1263   gcry_sexp_t pub_key[3], sec_key[3];
1264   int p_sizes[3] = { 1024, 2048, 3072 };
1265   gcry_sexp_t data = NULL;
1266   gcry_sexp_t enc = NULL;
1267   gcry_sexp_t plain = NULL;
1268   int i, j;
1269 
1270   err = gcry_sexp_sscan (pub_key+0, NULL, sample_public_elg_key_1024,
1271                          strlen (sample_public_elg_key_1024));
1272   if (!err)
1273     err = gcry_sexp_sscan (sec_key+0, NULL, sample_private_elg_key_1024,
1274                            strlen (sample_private_elg_key_1024));
1275   if (!err)
1276     err = gcry_sexp_sscan (pub_key+1, NULL, sample_public_elg_key_2048,
1277                            strlen (sample_public_elg_key_2048));
1278   if (!err)
1279     err = gcry_sexp_sscan (sec_key+1, NULL, sample_private_elg_key_2048,
1280                            strlen (sample_private_elg_key_2048));
1281   if (!err)
1282     err = gcry_sexp_sscan (pub_key+2, NULL, sample_public_elg_key_3072,
1283                            strlen (sample_public_elg_key_3072));
1284   if (!err)
1285     err = gcry_sexp_sscan (sec_key+2, NULL, sample_private_elg_key_3072,
1286                            strlen (sample_private_elg_key_3072));
1287   if (err)
1288     {
1289       fprintf (stderr, PGM ": converting sample keys failed: %s\n",
1290                gcry_strerror (err));
1291       exit (1);
1292     }
1293 
1294   if (print_header)
1295     printf ("Algorithm         generate %4d*priv %4d*public\n"
1296             "------------------------------------------------\n",
1297             iterations, iterations );
1298   for (i=0; i < DIM (p_sizes); i++)
1299     {
1300       char timerbuf1[100];
1301 
1302       {
1303         gcry_mpi_t x = gcry_mpi_new (p_sizes[i]);
1304         gcry_mpi_randomize (x, p_sizes[i] - 16, GCRY_WEAK_RANDOM);
1305         err = gcry_sexp_build (&data, NULL, "(data (flags raw) (value %m))", x);
1306         gcry_mpi_release (x);
1307       }
1308       if (err)
1309         {
1310           fprintf (stderr, PGM ": converting data failed: %s\n",
1311                    gcry_strerror (err));
1312           exit (1);
1313         }
1314 
1315       printf ("ELG %d bit             -", p_sizes[i]);
1316       fflush (stdout);
1317 
1318       start_timer ();
1319       for (j=0; j < iterations; j++)
1320         {
1321           gcry_sexp_release (enc);
1322           err = gcry_pk_encrypt (&enc, data, pub_key[i]);
1323           if (err)
1324             {
1325               putchar ('\n');
1326               fprintf (stderr, PGM ": encrypt failed: %s\n",
1327                        gpg_strerror (err));
1328               exit (1);
1329             }
1330         }
1331       stop_timer ();
1332       snprintf (timerbuf1, sizeof timerbuf1, "   %s", elapsed_time (1));
1333       fflush (stdout);
1334 
1335       start_timer ();
1336       for (j=0; j < iterations; j++)
1337         {
1338           gcry_sexp_release (plain);
1339           err = gcry_pk_decrypt (&plain, enc, sec_key[i]);
1340           if (err)
1341             {
1342               putchar ('\n');
1343               fprintf (stderr, PGM ": decrypt failed: %s\n",
1344                        gpg_strerror (err));
1345               exit (1);
1346             }
1347         }
1348       stop_timer ();
1349 
1350       printf ("   %s  %s\n", elapsed_time (1), timerbuf1);
1351       fflush (stdout);
1352 
1353       gcry_sexp_release (plain);
1354       plain = NULL;
1355       gcry_sexp_release (enc);
1356       enc = NULL;
1357       gcry_sexp_release (data);
1358       data = NULL;
1359     }
1360 
1361   for (i=0; i < DIM (p_sizes); i++)
1362     {
1363       gcry_sexp_release (sec_key[i]);
1364       gcry_sexp_release (pub_key[i]);
1365     }
1366 }
1367 
1368 
1369 static void
dsa_bench(int iterations,int print_header)1370 dsa_bench (int iterations, int print_header)
1371 {
1372   gpg_error_t err;
1373   gcry_sexp_t pub_key[3], sec_key[3];
1374   int p_sizes[3] = { 1024, 2048, 3072 };
1375   int q_sizes[3] = { 160, 224, 256 };
1376   gcry_sexp_t data;
1377   gcry_sexp_t sig = NULL;
1378   int i, j;
1379 
1380   err = gcry_sexp_sscan (pub_key+0, NULL, sample_public_dsa_key_1024,
1381                          strlen (sample_public_dsa_key_1024));
1382   if (!err)
1383     err = gcry_sexp_sscan (sec_key+0, NULL, sample_private_dsa_key_1024,
1384                            strlen (sample_private_dsa_key_1024));
1385   if (!err)
1386     err = gcry_sexp_sscan (pub_key+1, NULL, sample_public_dsa_key_2048,
1387                            strlen (sample_public_dsa_key_2048));
1388   if (!err)
1389     err = gcry_sexp_sscan (sec_key+1, NULL, sample_private_dsa_key_2048,
1390                            strlen (sample_private_dsa_key_2048));
1391   if (!err)
1392     err = gcry_sexp_sscan (pub_key+2, NULL, sample_public_dsa_key_3072,
1393                            strlen (sample_public_dsa_key_3072));
1394   if (!err)
1395     err = gcry_sexp_sscan (sec_key+2, NULL, sample_private_dsa_key_3072,
1396                            strlen (sample_private_dsa_key_3072));
1397   if (err)
1398     {
1399       fprintf (stderr, PGM ": converting sample keys failed: %s\n",
1400                gcry_strerror (err));
1401       exit (1);
1402     }
1403 
1404   if (print_header)
1405     printf ("Algorithm         generate %4d*priv %4d*public\n"
1406             "------------------------------------------------\n",
1407             iterations, iterations );
1408   for (i=0; i < DIM (q_sizes); i++)
1409     {
1410       gcry_mpi_t x;
1411 
1412       x = gcry_mpi_new (q_sizes[i]);
1413       gcry_mpi_randomize (x, q_sizes[i], GCRY_WEAK_RANDOM);
1414       err = gcry_sexp_build (&data, NULL, "(data (flags raw) (value %m))", x);
1415       gcry_mpi_release (x);
1416       if (err)
1417         {
1418           fprintf (stderr, PGM ": converting data failed: %s\n",
1419                    gcry_strerror (err));
1420           exit (1);
1421         }
1422 
1423       printf ("DSA %d/%d             -", p_sizes[i], q_sizes[i]);
1424       fflush (stdout);
1425 
1426       start_timer ();
1427       for (j=0; j < iterations; j++)
1428         {
1429           gcry_sexp_release (sig);
1430           err = gcry_pk_sign (&sig, data, sec_key[i]);
1431           if (err)
1432             {
1433               putchar ('\n');
1434               fprintf (stderr, PGM ": signing failed: %s\n",
1435                        gpg_strerror (err));
1436               exit (1);
1437             }
1438         }
1439       stop_timer ();
1440       printf ("   %s", elapsed_time (1));
1441       fflush (stdout);
1442 
1443       start_timer ();
1444       for (j=0; j < iterations; j++)
1445         {
1446           err = gcry_pk_verify (sig, data, pub_key[i]);
1447           if (err)
1448             {
1449               putchar ('\n');
1450               fprintf (stderr, PGM ": verify failed: %s\n",
1451                        gpg_strerror (err));
1452               exit (1);
1453             }
1454         }
1455       stop_timer ();
1456       printf ("     %s\n", elapsed_time (1));
1457       fflush (stdout);
1458 
1459       gcry_sexp_release (sig);
1460       gcry_sexp_release (data);
1461       sig = NULL;
1462     }
1463 
1464 
1465   for (i=0; i < DIM (q_sizes); i++)
1466     {
1467       gcry_sexp_release (sec_key[i]);
1468       gcry_sexp_release (pub_key[i]);
1469     }
1470 }
1471 
1472 
1473 static void
ecc_bench(int iterations,int print_header)1474 ecc_bench (int iterations, int print_header)
1475 {
1476 #if USE_ECC
1477   gpg_error_t err;
1478   const char *p_sizes[] = { "192", "224", "256", "384", "521", "Ed25519",
1479               "gost256", "gost512" };
1480   int testno;
1481 
1482   if (print_header)
1483     printf ("Algorithm         generate %4d*priv %4d*public\n"
1484             "------------------------------------------------\n",
1485             iterations, iterations );
1486   for (testno=0; testno < DIM (p_sizes); testno++)
1487     {
1488       gcry_sexp_t key_spec, key_pair, pub_key, sec_key;
1489       gcry_mpi_t x;
1490       gcry_sexp_t data;
1491       gcry_sexp_t sig = NULL;
1492       int count;
1493       int p_size;
1494       int is_ed25519;
1495       int is_gost;
1496 
1497       is_ed25519 = !strcmp (p_sizes[testno], "Ed25519");
1498       is_gost = !strncmp (p_sizes[testno], "gost", 4);
1499 
1500       /* Only P-{224,256,384,521} are allowed in fips mode */
1501       if (gcry_fips_mode_active()
1502           && (is_ed25519 || is_gost || !strcmp (p_sizes[testno], "192")))
1503          continue;
1504 
1505       if (is_ed25519)
1506         {
1507           p_size = 256;
1508           printf ("EdDSA Ed25519 ");
1509           fflush (stdout);
1510         }
1511       else if (is_gost)
1512         {
1513           p_size = atoi (p_sizes[testno] + 4);
1514           printf ("GOST  %3d bit ", p_size);
1515           fflush (stdout);
1516         }
1517       else
1518         {
1519           p_size = atoi (p_sizes[testno]);
1520           printf ("ECDSA %3d bit ", p_size);
1521         }
1522       fflush (stdout);
1523 
1524       if (is_ed25519)
1525         err = gcry_sexp_build (&key_spec, NULL,
1526                                "(genkey (ecdsa (curve \"Ed25519\")"
1527                                "(flags eddsa)))");
1528       else if (is_gost)
1529         err = gcry_sexp_build (&key_spec, NULL,
1530                                "(genkey (ecdsa (curve %s)))",
1531                                p_size == 256 ? "GOST2001-test" : "GOST2012-512-test");
1532       else
1533         err = gcry_sexp_build (&key_spec, NULL,
1534                                "(genkey (ECDSA (nbits %d)))", p_size);
1535       if (err)
1536         die ("creating S-expression failed: %s\n", gcry_strerror (err));
1537 
1538       start_timer ();
1539       err = gcry_pk_genkey (&key_pair, key_spec);
1540       if (err)
1541         die ("creating %d bit ECC key failed: %s\n",
1542              p_size, gcry_strerror (err));
1543       if (verbose > 2)
1544         show_sexp ("ECC key:\n", key_pair);
1545 
1546       pub_key = gcry_sexp_find_token (key_pair, "public-key", 0);
1547       if (! pub_key)
1548         die ("public part missing in key\n");
1549       sec_key = gcry_sexp_find_token (key_pair, "private-key", 0);
1550       if (! sec_key)
1551         die ("private part missing in key\n");
1552       gcry_sexp_release (key_pair);
1553       gcry_sexp_release (key_spec);
1554 
1555       stop_timer ();
1556       printf ("     %s", elapsed_time (1));
1557       fflush (stdout);
1558 
1559       x = gcry_mpi_new (p_size);
1560       gcry_mpi_randomize (x, p_size, GCRY_WEAK_RANDOM);
1561       if (is_ed25519)
1562         err = gcry_sexp_build (&data, NULL,
1563                                "(data (flags eddsa)(hash-algo sha512)"
1564                                " (value %m))", x);
1565       else if (is_gost)
1566         err = gcry_sexp_build (&data, NULL, "(data (flags gost) (value %m))", x);
1567       else
1568         err = gcry_sexp_build (&data, NULL, "(data (flags raw) (value %m))", x);
1569       gcry_mpi_release (x);
1570 
1571       if (err)
1572         die ("converting data failed: %s\n", gcry_strerror (err));
1573 
1574       start_timer ();
1575       for (count=0; count < iterations; count++)
1576         {
1577           gcry_sexp_release (sig);
1578           err = gcry_pk_sign (&sig, data, sec_key);
1579           if (err)
1580             {
1581               if (verbose)
1582                 {
1583                   putc ('\n', stderr);
1584                   show_sexp ("signing key:\n", sec_key);
1585                   show_sexp ("signed data:\n", data);
1586                 }
1587               die ("signing failed: %s\n", gpg_strerror (err));
1588             }
1589         }
1590       stop_timer ();
1591       printf ("   %s", elapsed_time (1));
1592       fflush (stdout);
1593 
1594       start_timer ();
1595       for (count=0; count < iterations; count++)
1596         {
1597           err = gcry_pk_verify (sig, data, pub_key);
1598           if (err)
1599             {
1600               putchar ('\n');
1601               show_sexp ("seckey:\n", sec_key);
1602               show_sexp ("data:\n", data);
1603               show_sexp ("sig:\n", sig);
1604               die ("verify failed: %s\n", gpg_strerror (err));
1605             }
1606         }
1607       stop_timer ();
1608       printf ("     %s\n", elapsed_time (1));
1609       fflush (stdout);
1610 
1611       gcry_sexp_release (sig);
1612       gcry_sexp_release (data);
1613       gcry_sexp_release (sec_key);
1614       gcry_sexp_release (pub_key);
1615     }
1616 #endif /*USE_ECC*/
1617 }
1618 
1619 
1620 
1621 static void
do_powm(const char * n_str,const char * e_str,const char * m_str)1622 do_powm ( const char *n_str, const char *e_str, const char *m_str)
1623 {
1624   gcry_mpi_t e, n, msg, cip;
1625   gcry_error_t err;
1626   int i;
1627 
1628   err = gcry_mpi_scan (&n, GCRYMPI_FMT_HEX, n_str, 0, 0);
1629   if (err) BUG ();
1630   err = gcry_mpi_scan (&e, GCRYMPI_FMT_HEX, e_str, 0, 0);
1631   if (err) BUG ();
1632   err = gcry_mpi_scan (&msg, GCRYMPI_FMT_HEX, m_str, 0, 0);
1633   if (err) BUG ();
1634 
1635   cip = gcry_mpi_new (0);
1636 
1637   start_timer ();
1638   for (i=0; i < 1000; i++)
1639     gcry_mpi_powm (cip, msg, e, n);
1640   stop_timer ();
1641   printf (" %s", elapsed_time (1)); fflush (stdout);
1642 /*    { */
1643 /*      char *buf; */
1644 
1645 /*      if (gcry_mpi_aprint (GCRYMPI_FMT_HEX, (void**)&buf, NULL, cip)) */
1646 /*        BUG (); */
1647 /*      printf ("result: %s\n", buf); */
1648 /*      gcry_free (buf); */
1649 /*    } */
1650   gcry_mpi_release (cip);
1651   gcry_mpi_release (msg);
1652   gcry_mpi_release (n);
1653   gcry_mpi_release (e);
1654 }
1655 
1656 
1657 static void
mpi_bench(void)1658 mpi_bench (void)
1659 {
1660   printf ("%-10s", "powm"); fflush (stdout);
1661 
1662   do_powm (
1663 "20A94417D4D5EF2B2DA99165C7DC87DADB3979B72961AF90D09D59BA24CB9A10166FDCCC9C659F2B9626EC23F3FA425F564A072BA941B03FA81767CC289E4",
1664            "29",
1665 "B870187A323F1ECD5B8A0B4249507335A1C4CE8394F38FD76B08C78A42C58F6EA136ACF90DFE8603697B1694A3D81114D6117AC1811979C51C4DD013D52F8"
1666            );
1667   do_powm (
1668            "20A94417D4D5EF2B2DA99165C7DC87DADB3979B72961AF90D09D59BA24CB9A10166FDCCC9C659F2B9626EC23F3FA425F564A072BA941B03FA81767CC289E41071F0246879A442658FBD18C1771571E7073EEEB2160BA0CBFB3404D627069A6CFBD53867AD2D9D40231648000787B5C84176B4336144644AE71A403CA40716",
1669            "29",
1670            "B870187A323F1ECD5B8A0B4249507335A1C4CE8394F38FD76B08C78A42C58F6EA136ACF90DFE8603697B1694A3D81114D6117AC1811979C51C4DD013D52F8FC4EE4BB446B83E48ABED7DB81CBF5E81DE4759E8D68AC985846D999F96B0D8A80E5C69D272C766AB8A23B40D50A4FA889FBC2BD2624222D8EB297F4BAEF8593847"
1671            );
1672   do_powm (
1673            "20A94417D4D5EF2B2DA99165C7DC87DADB3979B72961AF90D09D59BA24CB9A10166FDCCC9C659F2B9626EC23F3FA425F564A072BA941B03FA81767CC289E41071F0246879A442658FBD18C1771571E7073EEEB2160BA0CBFB3404D627069A6CFBD53867AD2D9D40231648000787B5C84176B4336144644AE71A403CA4071620A94417D4D5EF2B2DA99165C7DC87DADB3979B72961AF90D09D59BA24CB9A10166FDCCC9C659F2B9626EC23F3FA425F564A072BA941B03FA81767CC289E41071F0246879A442658FBD18C1771571E7073EEEB2160BA0CBFB3404D627069A6CFBD53867AD2D9D40231648000787B5C84176B4336144644AE71A403CA40716",
1674            "29",
1675            "B870187A323F1ECD5B8A0B4249507335A1C4CE8394F38FD76B08C78A42C58F6EA136ACF90DFE8603697B1694A3D81114D6117AC1811979C51C4DD013D52F8FC4EE4BB446B83E48ABED7DB81CBF5E81DE4759E8D68AC985846D999F96B0D8A80E5C69D272C766AB8A23B40D50A4FA889FBC2BD2624222D8EB297F4BAEF8593847B870187A323F1ECD5B8A0B4249507335A1C4CE8394F38FD76B08C78A42C58F6EA136ACF90DFE8603697B1694A3D81114D6117AC1811979C51C4DD013D52F8FC4EE4BB446B83E48ABED7DB81CBF5E81DE4759E8D68AC985846D999F96B0D8A80E5C69D272C766AB8A23B40D50A4FA889FBC2BD2624222D8EB297F4BAEF8593847"
1676            );
1677 
1678   putchar ('\n');
1679 
1680 
1681 }
1682 
1683 
1684 static void
prime_bench(void)1685 prime_bench (void)
1686 {
1687   gpg_error_t err;
1688   int i;
1689   gcry_mpi_t prime;
1690   int old_prog = single_char_progress;
1691 
1692   single_char_progress = 1;
1693   if (!with_progress)
1694     printf ("%-10s", "prime");
1695   fflush (stdout);
1696   start_timer ();
1697   for (i=0; i < 10; i++)
1698     {
1699       if (with_progress)
1700         fputs ("primegen ", stdout);
1701       err = gcry_prime_generate (&prime,
1702                                  1024, 0,
1703                                  NULL,
1704                                  NULL, NULL,
1705                                  GCRY_WEAK_RANDOM,
1706                                  GCRY_PRIME_FLAG_SECRET);
1707       if (with_progress)
1708         {
1709           fputc ('\n', stdout);
1710           fflush (stdout);
1711         }
1712       if (err)
1713         {
1714           fprintf (stderr, PGM ": error creating prime: %s\n",
1715                    gpg_strerror (err));
1716           exit (1);
1717         }
1718       gcry_mpi_release (prime);
1719     }
1720   stop_timer ();
1721   if (with_progress)
1722     printf ("%-10s", "prime");
1723   printf (" %s\n", elapsed_time (1)); fflush (stdout);
1724 
1725   single_char_progress = old_prog;
1726 }
1727 
1728 
1729 int
main(int argc,char ** argv)1730 main( int argc, char **argv )
1731 {
1732   int last_argc = -1;
1733   int no_blinding = 0;
1734   int use_random_daemon = 0;
1735   int use_secmem = 0;
1736   int pk_count = 100;
1737 
1738   buffer_alignment = 1;
1739 
1740   if (argc)
1741     { argc--; argv++; }
1742 
1743   /* We skip this test if we are running under the test suite (no args
1744      and srcdir defined) and GCRYPT_NO_BENCHMARKS is set.  */
1745   if (!argc && getenv ("srcdir") && getenv ("GCRYPT_NO_BENCHMARKS"))
1746     exit (77);
1747 
1748   if (getenv ("GCRYPT_IN_REGRESSION_TEST"))
1749     {
1750       in_regression_test = 1;
1751       pk_count = 10;
1752     }
1753 
1754   while (argc && last_argc != argc )
1755     {
1756       last_argc = argc;
1757       if (!strcmp (*argv, "--"))
1758         {
1759           argc--; argv++;
1760           break;
1761         }
1762       else if (!strcmp (*argv, "--help"))
1763         {
1764           fputs ("usage: benchmark "
1765                  "[md|mac|cipher|random|mpi|rsa|dsa|ecc|prime [algonames]]\n",
1766                  stdout);
1767           exit (0);
1768         }
1769       else if (!strcmp (*argv, "--verbose"))
1770         {
1771           verbose++;
1772           argc--; argv++;
1773         }
1774       else if (!strcmp (*argv, "--debug"))
1775         {
1776           verbose += 2;
1777           debug++;
1778           argc--; argv++;
1779         }
1780       else if (!strcmp (*argv, "--use-random-daemon"))
1781         {
1782           use_random_daemon = 1;
1783           argc--; argv++;
1784         }
1785       else if (!strcmp (*argv, "--use-secmem"))
1786         {
1787           use_secmem = 1;
1788           argc--; argv++;
1789         }
1790       else if (!strcmp (*argv, "--prefer-standard-rng"))
1791         {
1792           /* This is anyway the default, but we may want to use it for
1793              debugging. */
1794           xgcry_control ((GCRYCTL_SET_PREFERRED_RNG_TYPE, GCRY_RNG_TYPE_STANDARD));
1795           argc--; argv++;
1796         }
1797       else if (!strcmp (*argv, "--prefer-fips-rng"))
1798         {
1799           xgcry_control ((GCRYCTL_SET_PREFERRED_RNG_TYPE, GCRY_RNG_TYPE_FIPS));
1800           argc--; argv++;
1801         }
1802       else if (!strcmp (*argv, "--prefer-system-rng"))
1803         {
1804           xgcry_control ((GCRYCTL_SET_PREFERRED_RNG_TYPE, GCRY_RNG_TYPE_SYSTEM));
1805           argc--; argv++;
1806         }
1807       else if (!strcmp (*argv, "--no-blinding"))
1808         {
1809           no_blinding = 1;
1810           argc--; argv++;
1811         }
1812       else if (!strcmp (*argv, "--large-buffers"))
1813         {
1814           large_buffers = 1;
1815           argc--; argv++;
1816         }
1817       else if (!strcmp (*argv, "--huge-buffers"))
1818         {
1819           huge_buffers = 1;
1820           argc--; argv++;
1821         }
1822       else if (!strcmp (*argv, "--cipher-repetitions"))
1823         {
1824           argc--; argv++;
1825           if (argc)
1826             {
1827               cipher_repetitions = atoi(*argv);
1828               argc--; argv++;
1829             }
1830         }
1831       else if (!strcmp (*argv, "--cipher-with-keysetup"))
1832         {
1833           cipher_with_keysetup = 1;
1834           argc--; argv++;
1835         }
1836       else if (!strcmp (*argv, "--hash-repetitions"))
1837         {
1838           argc--; argv++;
1839           if (argc)
1840             {
1841               hash_repetitions = atoi(*argv);
1842               argc--; argv++;
1843             }
1844         }
1845       else if (!strcmp (*argv, "--mac-repetitions"))
1846         {
1847           argc--; argv++;
1848           if (argc)
1849             {
1850               mac_repetitions = atoi(*argv);
1851               argc--; argv++;
1852             }
1853         }
1854       else if (!strcmp (*argv, "--pk-count"))
1855         {
1856           argc--; argv++;
1857           if (argc)
1858             {
1859               pk_count = atoi(*argv);
1860               argc--; argv++;
1861             }
1862         }
1863       else if (!strcmp (*argv, "--alignment"))
1864         {
1865           argc--; argv++;
1866           if (argc)
1867             {
1868               buffer_alignment = atoi(*argv);
1869               argc--; argv++;
1870             }
1871         }
1872       else if (!strcmp (*argv, "--disable-hwf"))
1873         {
1874           argc--; argv++;
1875           if (argc)
1876             {
1877               if (gcry_control (GCRYCTL_DISABLE_HWF, *argv, NULL))
1878                 fprintf (stderr, PGM ": unknown hardware feature `%s'"
1879                          " - option ignored\n", *argv);
1880               argc--; argv++;
1881             }
1882         }
1883       else if (!strcmp (*argv, "--fips"))
1884         {
1885           argc--; argv++;
1886           /* This command needs to be called before gcry_check_version.  */
1887           xgcry_control ((GCRYCTL_FORCE_FIPS_MODE, 0));
1888         }
1889       else if (!strcmp (*argv, "--progress"))
1890         {
1891           argc--; argv++;
1892           with_progress = 1;
1893         }
1894     }
1895 
1896   if (buffer_alignment < 1 || buffer_alignment > 16)
1897     die ("value for --alignment must be in the range 1 to 16\n");
1898 
1899   xgcry_control ((GCRYCTL_SET_VERBOSITY, (int)verbose));
1900 
1901   if (!gcry_check_version (GCRYPT_VERSION))
1902     {
1903       fprintf (stderr, PGM ": version mismatch; pgm=%s, library=%s\n",
1904                GCRYPT_VERSION, gcry_check_version (NULL));
1905       exit (1);
1906     }
1907 
1908   if (debug)
1909     xgcry_control ((GCRYCTL_SET_DEBUG_FLAGS, 1u , 0));
1910 
1911   if (gcry_fips_mode_active ())
1912     in_fips_mode = 1;
1913   else if (!use_secmem)
1914     xgcry_control ((GCRYCTL_DISABLE_SECMEM, 0));
1915 
1916   if (use_random_daemon)
1917     xgcry_control ((GCRYCTL_USE_RANDOM_DAEMON, 1));
1918 
1919   if (with_progress)
1920     gcry_set_progress_handler (progress_cb, NULL);
1921 
1922   xgcry_control ((GCRYCTL_INITIALIZATION_FINISHED, 0));
1923 
1924   if (cipher_repetitions < 1)
1925     cipher_repetitions = 1;
1926   if (hash_repetitions < 1)
1927     hash_repetitions = 1;
1928   if (mac_repetitions < 1)
1929     mac_repetitions = 1;
1930 
1931   if (in_regression_test)
1932     fputs ("Note: " PGM " running in quick regression test mode.\n", stdout);
1933 
1934   if ( !argc )
1935     {
1936       xgcry_control ((GCRYCTL_ENABLE_QUICK_RANDOM, 0));
1937       md_bench (NULL);
1938       putchar ('\n');
1939       mac_bench (NULL);
1940       putchar ('\n');
1941       cipher_bench (NULL);
1942       putchar ('\n');
1943       rsa_bench (pk_count, 1, no_blinding);
1944       elg_bench (pk_count, 0);
1945       dsa_bench (pk_count, 0);
1946       ecc_bench (pk_count, 0);
1947       putchar ('\n');
1948       mpi_bench ();
1949       putchar ('\n');
1950       random_bench (0);
1951     }
1952   else if ( !strcmp (*argv, "random") || !strcmp (*argv, "strongrandom"))
1953     {
1954       if (argc == 1)
1955         random_bench ((**argv == 's'));
1956       else if (argc == 2)
1957         {
1958           xgcry_control ((GCRYCTL_SET_RANDOM_SEED_FILE, argv[1]));
1959           random_bench ((**argv == 's'));
1960           xgcry_control ((GCRYCTL_UPDATE_RANDOM_SEED_FILE));
1961         }
1962       else
1963         fputs ("usage: benchmark [strong]random [seedfile]\n", stdout);
1964     }
1965   else if ( !strcmp (*argv, "md"))
1966     {
1967       if (argc == 1)
1968         md_bench (NULL);
1969       else
1970         for (argc--, argv++; argc; argc--, argv++)
1971           md_bench ( *argv );
1972     }
1973   else if ( !strcmp (*argv, "mac"))
1974     {
1975       if (argc == 1)
1976         mac_bench (NULL);
1977       else
1978         for (argc--, argv++; argc; argc--, argv++)
1979           mac_bench ( *argv );
1980     }
1981   else if ( !strcmp (*argv, "cipher"))
1982     {
1983       if (argc == 1)
1984         cipher_bench (NULL);
1985       else
1986         for (argc--, argv++; argc; argc--, argv++)
1987           cipher_bench ( *argv );
1988     }
1989   else if ( !strcmp (*argv, "mpi"))
1990     {
1991         mpi_bench ();
1992     }
1993   else if ( !strcmp (*argv, "pubkey"))
1994     {
1995         xgcry_control ((GCRYCTL_ENABLE_QUICK_RANDOM, 0));
1996         rsa_bench (pk_count, 1, no_blinding);
1997         elg_bench (pk_count, 0);
1998         dsa_bench (pk_count, 0);
1999         ecc_bench (pk_count, 0);
2000     }
2001   else if ( !strcmp (*argv, "rsa"))
2002     {
2003         xgcry_control ((GCRYCTL_ENABLE_QUICK_RANDOM, 0));
2004         rsa_bench (pk_count, 1, no_blinding);
2005     }
2006   else if ( !strcmp (*argv, "elg"))
2007     {
2008         xgcry_control ((GCRYCTL_ENABLE_QUICK_RANDOM, 0));
2009         elg_bench (pk_count, 1);
2010     }
2011   else if ( !strcmp (*argv, "dsa"))
2012     {
2013         xgcry_control ((GCRYCTL_ENABLE_QUICK_RANDOM, 0));
2014         dsa_bench (pk_count, 1);
2015     }
2016   else if ( !strcmp (*argv, "ecc"))
2017     {
2018         xgcry_control ((GCRYCTL_ENABLE_QUICK_RANDOM, 0));
2019         ecc_bench (pk_count, 1);
2020     }
2021   else if ( !strcmp (*argv, "prime"))
2022     {
2023         xgcry_control ((GCRYCTL_ENABLE_QUICK_RANDOM, 0));
2024         prime_bench ();
2025     }
2026   else
2027     {
2028       fprintf (stderr, PGM ": bad arguments\n");
2029       return 1;
2030     }
2031 
2032 
2033   if (in_fips_mode && !gcry_fips_mode_active ())
2034     fprintf (stderr, PGM ": FIPS mode is not anymore active\n");
2035 
2036   return 0;
2037 }
2038