LCOV - code coverage report
Current view: top level - tests - benchmark.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 571 891 64.1 %
Date: 2016-09-12 12:56:58 Functions: 12 16 75.0 %

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

Generated by: LCOV version 1.11