benchmark.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Benchmark demonstration program
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #ifndef _CRT_SECURE_NO_DEPRECATE
  26. #define _CRT_SECURE_NO_DEPRECATE 1
  27. #endif
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include "polarssl/config.h"
  32. #include "polarssl/md4.h"
  33. #include "polarssl/md5.h"
  34. #include "polarssl/sha1.h"
  35. #include "polarssl/sha2.h"
  36. #include "polarssl/sha4.h"
  37. #include "polarssl/arc4.h"
  38. #include "polarssl/des.h"
  39. #include "polarssl/aes.h"
  40. #include "polarssl/camellia.h"
  41. #include "polarssl/rsa.h"
  42. #include "polarssl/timing.h"
  43. #define BUFSIZE 1024
  44. static int myrand( void *rng_state )
  45. {
  46. if( rng_state != NULL )
  47. rng_state = NULL;
  48. return( rand() );
  49. }
  50. unsigned char buf[BUFSIZE];
  51. #if !defined(POLARSSL_TIMING_C)
  52. int main( void )
  53. {
  54. printf("POLARSSL_TIMING_C not defined.\n");
  55. return( 0 );
  56. }
  57. #else
  58. int main( void )
  59. {
  60. int keysize;
  61. unsigned long i, j, tsc;
  62. unsigned char tmp[64];
  63. #if defined(POLARSSL_ARC4_C)
  64. arc4_context arc4;
  65. #endif
  66. #if defined(POLARSSL_DES_C)
  67. des3_context des3;
  68. des_context des;
  69. #endif
  70. #if defined(POLARSSL_AES_C)
  71. aes_context aes;
  72. #endif
  73. #if defined(POLARSSL_CAMELLIA_C)
  74. camellia_context camellia;
  75. #endif
  76. #if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) && \
  77. defined(POLARSSL_GENPRIME)
  78. rsa_context rsa;
  79. #endif
  80. memset( buf, 0xAA, sizeof( buf ) );
  81. printf( "\n" );
  82. #if defined(POLARSSL_MD4_C)
  83. printf( " MD4 : " );
  84. fflush( stdout );
  85. set_alarm( 1 );
  86. for( i = 1; ! alarmed; i++ )
  87. md4( buf, BUFSIZE, tmp );
  88. tsc = hardclock();
  89. for( j = 0; j < 1024; j++ )
  90. md4( buf, BUFSIZE, tmp );
  91. printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
  92. ( hardclock() - tsc ) / ( j * BUFSIZE ) );
  93. #endif
  94. #if defined(POLARSSL_MD5_C)
  95. printf( " MD5 : " );
  96. fflush( stdout );
  97. set_alarm( 1 );
  98. for( i = 1; ! alarmed; i++ )
  99. md5( buf, BUFSIZE, tmp );
  100. tsc = hardclock();
  101. for( j = 0; j < 1024; j++ )
  102. md5( buf, BUFSIZE, tmp );
  103. printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
  104. ( hardclock() - tsc ) / ( j * BUFSIZE ) );
  105. #endif
  106. #if defined(POLARSSL_SHA1_C)
  107. printf( " SHA-1 : " );
  108. fflush( stdout );
  109. set_alarm( 1 );
  110. for( i = 1; ! alarmed; i++ )
  111. sha1( buf, BUFSIZE, tmp );
  112. tsc = hardclock();
  113. for( j = 0; j < 1024; j++ )
  114. sha1( buf, BUFSIZE, tmp );
  115. printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
  116. ( hardclock() - tsc ) / ( j * BUFSIZE ) );
  117. #endif
  118. #if defined(POLARSSL_SHA2_C)
  119. printf( " SHA-256 : " );
  120. fflush( stdout );
  121. set_alarm( 1 );
  122. for( i = 1; ! alarmed; i++ )
  123. sha2( buf, BUFSIZE, tmp, 0 );
  124. tsc = hardclock();
  125. for( j = 0; j < 1024; j++ )
  126. sha2( buf, BUFSIZE, tmp, 0 );
  127. printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
  128. ( hardclock() - tsc ) / ( j * BUFSIZE ) );
  129. #endif
  130. #if defined(POLARSSL_SHA4_C)
  131. printf( " SHA-512 : " );
  132. fflush( stdout );
  133. set_alarm( 1 );
  134. for( i = 1; ! alarmed; i++ )
  135. sha4( buf, BUFSIZE, tmp, 0 );
  136. tsc = hardclock();
  137. for( j = 0; j < 1024; j++ )
  138. sha4( buf, BUFSIZE, tmp, 0 );
  139. printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
  140. ( hardclock() - tsc ) / ( j * BUFSIZE ) );
  141. #endif
  142. #if defined(POLARSSL_ARC4_C)
  143. printf( " ARC4 : " );
  144. fflush( stdout );
  145. arc4_setup( &arc4, tmp, 32 );
  146. set_alarm( 1 );
  147. for( i = 1; ! alarmed; i++ )
  148. arc4_crypt( &arc4, BUFSIZE, buf, buf );
  149. tsc = hardclock();
  150. for( j = 0; j < 1024; j++ )
  151. arc4_crypt( &arc4, BUFSIZE, buf, buf );
  152. printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
  153. ( hardclock() - tsc ) / ( j * BUFSIZE ) );
  154. #endif
  155. #if defined(POLARSSL_DES_C)
  156. printf( " 3DES : " );
  157. fflush( stdout );
  158. des3_set3key_enc( &des3, tmp );
  159. set_alarm( 1 );
  160. for( i = 1; ! alarmed; i++ )
  161. des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
  162. tsc = hardclock();
  163. for( j = 0; j < 1024; j++ )
  164. des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
  165. printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
  166. ( hardclock() - tsc ) / ( j * BUFSIZE ) );
  167. printf( " DES : " );
  168. fflush( stdout );
  169. des_setkey_enc( &des, tmp );
  170. set_alarm( 1 );
  171. for( i = 1; ! alarmed; i++ )
  172. des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
  173. tsc = hardclock();
  174. for( j = 0; j < 1024; j++ )
  175. des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
  176. printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
  177. ( hardclock() - tsc ) / ( j * BUFSIZE ) );
  178. #endif
  179. #if defined(POLARSSL_AES_C)
  180. for( keysize = 128; keysize <= 256; keysize += 64 )
  181. {
  182. printf( " AES-%d : ", keysize );
  183. fflush( stdout );
  184. memset( buf, 0, sizeof( buf ) );
  185. memset( tmp, 0, sizeof( tmp ) );
  186. aes_setkey_enc( &aes, tmp, keysize );
  187. set_alarm( 1 );
  188. for( i = 1; ! alarmed; i++ )
  189. aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
  190. tsc = hardclock();
  191. for( j = 0; j < 4096; j++ )
  192. aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
  193. printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
  194. ( hardclock() - tsc ) / ( j * BUFSIZE ) );
  195. }
  196. #endif
  197. #if defined(POLARSSL_CAMELLIA_C)
  198. for( keysize = 128; keysize <= 256; keysize += 64 )
  199. {
  200. printf( " CAMELLIA-%d : ", keysize );
  201. fflush( stdout );
  202. memset( buf, 0, sizeof( buf ) );
  203. memset( tmp, 0, sizeof( tmp ) );
  204. camellia_setkey_enc( &camellia, tmp, keysize );
  205. set_alarm( 1 );
  206. for( i = 1; ! alarmed; i++ )
  207. camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf );
  208. tsc = hardclock();
  209. for( j = 0; j < 4096; j++ )
  210. camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf );
  211. printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
  212. ( hardclock() - tsc ) / ( j * BUFSIZE ) );
  213. }
  214. #endif
  215. #if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) && \
  216. defined(POLARSSL_GENPRIME)
  217. rsa_init( &rsa, RSA_PKCS_V15, 0 );
  218. rsa_gen_key( &rsa, myrand, NULL, 1024, 65537 );
  219. printf( " RSA-1024 : " );
  220. fflush( stdout );
  221. set_alarm( 3 );
  222. for( i = 1; ! alarmed; i++ )
  223. {
  224. buf[0] = 0;
  225. rsa_public( &rsa, buf, buf );
  226. }
  227. printf( "%9lu public/s\n", i / 3 );
  228. printf( " RSA-1024 : " );
  229. fflush( stdout );
  230. set_alarm( 3 );
  231. for( i = 1; ! alarmed; i++ )
  232. {
  233. buf[0] = 0;
  234. rsa_private( &rsa, buf, buf );
  235. }
  236. printf( "%9lu private/s\n", i / 3 );
  237. rsa_free( &rsa );
  238. rsa_init( &rsa, RSA_PKCS_V15, 0 );
  239. rsa_gen_key( &rsa, myrand, NULL, 2048, 65537 );
  240. printf( " RSA-2048 : " );
  241. fflush( stdout );
  242. set_alarm( 3 );
  243. for( i = 1; ! alarmed; i++ )
  244. {
  245. buf[0] = 0;
  246. rsa_public( &rsa, buf, buf );
  247. }
  248. printf( "%9lu public/s\n", i / 3 );
  249. printf( " RSA-2048 : " );
  250. fflush( stdout );
  251. set_alarm( 3 );
  252. for( i = 1; ! alarmed; i++ )
  253. {
  254. buf[0] = 0;
  255. rsa_private( &rsa, buf, buf );
  256. }
  257. printf( "%9lu private/s\n", i / 3 );
  258. rsa_free( &rsa );
  259. rsa_init( &rsa, RSA_PKCS_V15, 0 );
  260. rsa_gen_key( &rsa, myrand, NULL, 4096, 65537 );
  261. printf( " RSA-4096 : " );
  262. fflush( stdout );
  263. set_alarm( 3 );
  264. for( i = 1; ! alarmed; i++ )
  265. {
  266. buf[0] = 0;
  267. rsa_public( &rsa, buf, buf );
  268. }
  269. printf( "%9lu public/s\n", i / 3 );
  270. printf( " RSA-4096 : " );
  271. fflush( stdout );
  272. set_alarm( 3 );
  273. for( i = 1; ! alarmed; i++ )
  274. {
  275. buf[0] = 0;
  276. rsa_private( &rsa, buf, buf );
  277. }
  278. printf( "%9lu private/s\n", i / 3 );
  279. rsa_free( &rsa );
  280. #endif
  281. printf( "\n" );
  282. #ifdef WIN32
  283. printf( " Press Enter to exit this program.\n" );
  284. fflush( stdout ); getchar();
  285. #endif
  286. return( 0 );
  287. }
  288. #endif /* POLARSSL_TIMING_C */