benchmark.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. #ifdef PRINTF_STDLIB
  31. #include <stdio.h>
  32. #endif
  33. #ifdef PRINTF_CUSTOM
  34. #include "tinystdio.h"
  35. #endif
  36. #include "polarssl/config.h"
  37. #include "polarssl/md4.h"
  38. #include "polarssl/md5.h"
  39. #include "polarssl/sha1.h"
  40. #include "polarssl/sha2.h"
  41. #include "polarssl/sha4.h"
  42. #include "polarssl/arc4.h"
  43. #include "polarssl/des.h"
  44. #include "polarssl/aes.h"
  45. #include "polarssl/camellia.h"
  46. #include "polarssl/rsa.h"
  47. #include "polarssl/timing.h"
  48. #define BUFSIZE 1024
  49. static int myrand( void *rng_state )
  50. {
  51. if( rng_state != NULL )
  52. rng_state = NULL;
  53. return( rand() );
  54. }
  55. unsigned char buf[BUFSIZE];
  56. #if !defined(POLARSSL_TIMING_C)
  57. int main( void )
  58. {
  59. printf("POLARSSL_TIMING_C not defined.\n");
  60. return( 0 );
  61. }
  62. #else
  63. int main( void )
  64. {
  65. int keysize;
  66. unsigned long i, j, tsc;
  67. unsigned char tmp[64];
  68. #if defined(POLARSSL_ARC4_C)
  69. arc4_context arc4;
  70. #endif
  71. #if defined(POLARSSL_DES_C)
  72. des3_context des3;
  73. des_context des;
  74. #endif
  75. #if defined(POLARSSL_AES_C)
  76. aes_context aes;
  77. #endif
  78. #if defined(POLARSSL_CAMELLIA_C)
  79. camellia_context camellia;
  80. #endif
  81. #if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) && \
  82. defined(POLARSSL_GENPRIME)
  83. rsa_context rsa;
  84. #endif
  85. memset( buf, 0xAA, sizeof( buf ) );
  86. printf( "\n" );
  87. #if defined(POLARSSL_MD4_C)
  88. printf( " MD4 : " );
  89. fflush( stdout );
  90. set_alarm( 1 );
  91. for( i = 1; ! alarmed; i++ )
  92. md4( buf, BUFSIZE, tmp );
  93. tsc = hardclock();
  94. for( j = 0; j < 1024; j++ )
  95. md4( buf, BUFSIZE, tmp );
  96. printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
  97. ( hardclock() - tsc ) / ( j * BUFSIZE ) );
  98. #endif
  99. #if defined(POLARSSL_MD5_C)
  100. printf( " MD5 : " );
  101. fflush( stdout );
  102. set_alarm( 1 );
  103. for( i = 1; ! alarmed; i++ )
  104. md5( buf, BUFSIZE, tmp );
  105. tsc = hardclock();
  106. for( j = 0; j < 1024; j++ )
  107. md5( buf, BUFSIZE, tmp );
  108. printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
  109. ( hardclock() - tsc ) / ( j * BUFSIZE ) );
  110. #endif
  111. #if defined(POLARSSL_SHA1_C)
  112. printf( " SHA-1 : " );
  113. fflush( stdout );
  114. set_alarm( 1 );
  115. for( i = 1; ! alarmed; i++ )
  116. sha1( buf, BUFSIZE, tmp );
  117. tsc = hardclock();
  118. for( j = 0; j < 1024; j++ )
  119. sha1( buf, BUFSIZE, tmp );
  120. printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
  121. ( hardclock() - tsc ) / ( j * BUFSIZE ) );
  122. #endif
  123. #if defined(POLARSSL_SHA2_C)
  124. printf( " SHA-256 : " );
  125. fflush( stdout );
  126. set_alarm( 1 );
  127. for( i = 1; ! alarmed; i++ )
  128. sha2( buf, BUFSIZE, tmp, 0 );
  129. tsc = hardclock();
  130. for( j = 0; j < 1024; j++ )
  131. sha2( buf, BUFSIZE, tmp, 0 );
  132. printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
  133. ( hardclock() - tsc ) / ( j * BUFSIZE ) );
  134. #endif
  135. #if defined(POLARSSL_SHA4_C)
  136. printf( " SHA-512 : " );
  137. fflush( stdout );
  138. set_alarm( 1 );
  139. for( i = 1; ! alarmed; i++ )
  140. sha4( buf, BUFSIZE, tmp, 0 );
  141. tsc = hardclock();
  142. for( j = 0; j < 1024; j++ )
  143. sha4( buf, BUFSIZE, tmp, 0 );
  144. printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
  145. ( hardclock() - tsc ) / ( j * BUFSIZE ) );
  146. #endif
  147. #if defined(POLARSSL_ARC4_C)
  148. printf( " ARC4 : " );
  149. fflush( stdout );
  150. arc4_setup( &arc4, tmp, 32 );
  151. set_alarm( 1 );
  152. for( i = 1; ! alarmed; i++ )
  153. arc4_crypt( &arc4, BUFSIZE, buf, buf );
  154. tsc = hardclock();
  155. for( j = 0; j < 1024; j++ )
  156. arc4_crypt( &arc4, BUFSIZE, buf, buf );
  157. printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
  158. ( hardclock() - tsc ) / ( j * BUFSIZE ) );
  159. #endif
  160. #if defined(POLARSSL_DES_C)
  161. printf( " 3DES : " );
  162. fflush( stdout );
  163. des3_set3key_enc( &des3, tmp );
  164. set_alarm( 1 );
  165. for( i = 1; ! alarmed; i++ )
  166. des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
  167. tsc = hardclock();
  168. for( j = 0; j < 1024; j++ )
  169. des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
  170. printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
  171. ( hardclock() - tsc ) / ( j * BUFSIZE ) );
  172. printf( " DES : " );
  173. fflush( stdout );
  174. des_setkey_enc( &des, tmp );
  175. set_alarm( 1 );
  176. for( i = 1; ! alarmed; i++ )
  177. des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
  178. tsc = hardclock();
  179. for( j = 0; j < 1024; j++ )
  180. des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
  181. printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
  182. ( hardclock() - tsc ) / ( j * BUFSIZE ) );
  183. #endif
  184. #if defined(POLARSSL_AES_C)
  185. for( keysize = 128; keysize <= 256; keysize += 64 )
  186. {
  187. printf( " AES-%d : ", keysize );
  188. fflush( stdout );
  189. memset( buf, 0, sizeof( buf ) );
  190. memset( tmp, 0, sizeof( tmp ) );
  191. aes_setkey_enc( &aes, tmp, keysize );
  192. set_alarm( 1 );
  193. for( i = 1; ! alarmed; i++ )
  194. aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
  195. tsc = hardclock();
  196. for( j = 0; j < 4096; j++ )
  197. aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
  198. printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
  199. ( hardclock() - tsc ) / ( j * BUFSIZE ) );
  200. }
  201. #endif
  202. #if defined(POLARSSL_CAMELLIA_C)
  203. for( keysize = 128; keysize <= 256; keysize += 64 )
  204. {
  205. printf( " CAMELLIA-%d : ", keysize );
  206. fflush( stdout );
  207. memset( buf, 0, sizeof( buf ) );
  208. memset( tmp, 0, sizeof( tmp ) );
  209. camellia_setkey_enc( &camellia, tmp, keysize );
  210. set_alarm( 1 );
  211. for( i = 1; ! alarmed; i++ )
  212. camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf );
  213. tsc = hardclock();
  214. for( j = 0; j < 4096; j++ )
  215. camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf );
  216. printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
  217. ( hardclock() - tsc ) / ( j * BUFSIZE ) );
  218. }
  219. #endif
  220. #if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) && \
  221. defined(POLARSSL_GENPRIME)
  222. rsa_init( &rsa, RSA_PKCS_V15, 0 );
  223. rsa_gen_key( &rsa, myrand, NULL, 1024, 65537 );
  224. printf( " RSA-1024 : " );
  225. fflush( stdout );
  226. set_alarm( 3 );
  227. for( i = 1; ! alarmed; i++ )
  228. {
  229. buf[0] = 0;
  230. rsa_public( &rsa, buf, buf );
  231. }
  232. printf( "%9lu public/s\n", i / 3 );
  233. printf( " RSA-1024 : " );
  234. fflush( stdout );
  235. set_alarm( 3 );
  236. for( i = 1; ! alarmed; i++ )
  237. {
  238. buf[0] = 0;
  239. rsa_private( &rsa, buf, buf );
  240. }
  241. printf( "%9lu private/s\n", i / 3 );
  242. rsa_free( &rsa );
  243. rsa_init( &rsa, RSA_PKCS_V15, 0 );
  244. rsa_gen_key( &rsa, myrand, NULL, 2048, 65537 );
  245. printf( " RSA-2048 : " );
  246. fflush( stdout );
  247. set_alarm( 3 );
  248. for( i = 1; ! alarmed; i++ )
  249. {
  250. buf[0] = 0;
  251. rsa_public( &rsa, buf, buf );
  252. }
  253. printf( "%9lu public/s\n", i / 3 );
  254. printf( " RSA-2048 : " );
  255. fflush( stdout );
  256. set_alarm( 3 );
  257. for( i = 1; ! alarmed; i++ )
  258. {
  259. buf[0] = 0;
  260. rsa_private( &rsa, buf, buf );
  261. }
  262. printf( "%9lu private/s\n", i / 3 );
  263. rsa_free( &rsa );
  264. rsa_init( &rsa, RSA_PKCS_V15, 0 );
  265. rsa_gen_key( &rsa, myrand, NULL, 4096, 65537 );
  266. printf( " RSA-4096 : " );
  267. fflush( stdout );
  268. set_alarm( 3 );
  269. for( i = 1; ! alarmed; i++ )
  270. {
  271. buf[0] = 0;
  272. rsa_public( &rsa, buf, buf );
  273. }
  274. printf( "%9lu public/s\n", i / 3 );
  275. printf( " RSA-4096 : " );
  276. fflush( stdout );
  277. set_alarm( 3 );
  278. for( i = 1; ! alarmed; i++ )
  279. {
  280. buf[0] = 0;
  281. rsa_private( &rsa, buf, buf );
  282. }
  283. printf( "%9lu private/s\n", i / 3 );
  284. rsa_free( &rsa );
  285. #endif
  286. printf( "\n" );
  287. #ifdef WIN32
  288. printf( " Press Enter to exit this program.\n" );
  289. fflush( stdout ); getchar();
  290. #endif
  291. return( 0 );
  292. }
  293. #endif /* POLARSSL_TIMING_C */