3 * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
5 * This file is part of CyaSSL.
7 * CyaSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * CyaSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22 /* CTaoCrypt benchmark */
31 #include <cyassl/ctaocrypt/des3.h>
32 #include <cyassl/ctaocrypt/arc4.h>
33 #include <cyassl/ctaocrypt/hc128.h>
34 #include <cyassl/ctaocrypt/rabbit.h>
35 #include <cyassl/ctaocrypt/aes.h>
36 #include <cyassl/ctaocrypt/md5.h>
37 #include <cyassl/ctaocrypt/sha.h>
38 #include <cyassl/ctaocrypt/sha256.h>
39 #include <cyassl/ctaocrypt/sha512.h>
40 #include <cyassl/ctaocrypt/rsa.h>
41 #include <cyassl/ctaocrypt/asn.h>
42 #include <cyassl/ctaocrypt/ripemd.h>
43 #include <cyassl/ctaocrypt/ecc.h>
45 #include <cyassl/ctaocrypt/dh.h>
48 /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
49 #pragma warning(disable: 4996)
66 void bench_rsaKeyGen();
69 void bench_eccKeyGen();
70 void bench_eccKeyAgree();
73 double current_time();
77 int main(int argc, char** argv)
119 #ifdef CYASSL_KEY_GEN
131 const int megs = 5; /* how many megs to test (en/de)cryption */
132 const int times = 100; /* public key iterations */
136 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
137 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
138 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
143 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
144 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
145 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
150 byte plain [1024*1024];
151 byte cipher[1024*1024];
155 void bench_aes(int show)
158 double start, total, persec;
161 AesSetKey(&enc, key, 16, iv, AES_ENCRYPTION);
162 start = current_time();
164 for(i = 0; i < megs; i++)
165 AesCbcEncrypt(&enc, plain, cipher, sizeof(plain));
167 total = current_time() - start;
169 persec = 1 / total * megs;
172 printf("AES %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
186 double start, total, persec;
189 AesGcmSetKey(&enc, key, 16, iv);
190 AesGcmSetExpIV(&enc, iv+4);
191 start = current_time();
193 for(i = 0; i < megs; i++)
194 AesGcmEncrypt(&enc, cipher, plain, sizeof(plain),
195 tag, 16, additional, 13);
197 total = current_time() - start;
199 persec = 1 / total * megs;
200 printf("AES-GCM %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
210 double start, total, persec;
213 Des3_SetKey(&enc, key, iv, DES_ENCRYPTION);
214 start = current_time();
216 for(i = 0; i < megs; i++)
217 Des3_CbcEncrypt(&enc, plain, cipher, sizeof(plain));
219 total = current_time() - start;
221 persec = 1 / total * megs;
223 printf("3DES %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
232 double start, total, persec;
235 Arc4SetKey(&enc, key, 16);
236 start = current_time();
238 for(i = 0; i < megs; i++)
239 Arc4Process(&enc, cipher, plain, sizeof(plain));
241 total = current_time() - start;
242 persec = 1 / total * megs;
244 printf("ARC4 %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
253 double start, total, persec;
256 Hc128_SetKey(&enc, key, iv);
257 start = current_time();
259 for(i = 0; i < megs; i++)
260 Hc128_Process(&enc, cipher, plain, sizeof(plain));
262 total = current_time() - start;
263 persec = 1 / total * megs;
265 printf("HC128 %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
268 #endif /* HAVE_HC128 */
275 double start, total, persec;
278 RabbitSetKey(&enc, key, iv);
279 start = current_time();
281 for(i = 0; i < megs; i++)
282 RabbitProcess(&enc, cipher, plain, sizeof(plain));
284 total = current_time() - start;
285 persec = 1 / total * megs;
287 printf("RABBIT %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
290 #endif /* NO_RABBIT */
296 byte digest[MD5_DIGEST_SIZE];
297 double start, total, persec;
301 start = current_time();
303 for(i = 0; i < megs; i++)
304 Md5Update(&hash, plain, sizeof(plain));
306 Md5Final(&hash, digest);
308 total = current_time() - start;
309 persec = 1 / total * megs;
311 printf("MD5 %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
319 byte digest[SHA_DIGEST_SIZE];
320 double start, total, persec;
324 start = current_time();
326 for(i = 0; i < megs; i++)
327 ShaUpdate(&hash, plain, sizeof(plain));
329 ShaFinal(&hash, digest);
331 total = current_time() - start;
332 persec = 1 / total * megs;
334 printf("SHA %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
343 byte digest[SHA256_DIGEST_SIZE];
344 double start, total, persec;
348 start = current_time();
350 for(i = 0; i < megs; i++)
351 Sha256Update(&hash, plain, sizeof(plain));
353 Sha256Final(&hash, digest);
355 total = current_time() - start;
356 persec = 1 / total * megs;
358 printf("SHA-256 %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
367 byte digest[SHA512_DIGEST_SIZE];
368 double start, total, persec;
372 start = current_time();
374 for(i = 0; i < megs; i++)
375 Sha512Update(&hash, plain, sizeof(plain));
377 Sha512Final(&hash, digest);
379 total = current_time() - start;
380 persec = 1 / total * megs;
382 printf("SHA-512 %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
391 byte digest[RIPEMD_DIGEST_SIZE];
392 double start, total, persec;
396 start = current_time();
398 for(i = 0; i < megs; i++)
399 RipeMdUpdate(&hash, plain, sizeof(plain));
401 RipeMdFinal(&hash, digest);
403 total = current_time() - start;
404 persec = 1 / total * megs;
406 printf("RIPEMD %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
421 byte message[] = "Everyone gets Friday off.";
422 byte cipher[512]; /* for up to 4096 bit */
424 const int len = (int)strlen((char*)message);
425 double start, total, each, milliEach;
428 FILE* file = fopen("./certs/rsa2048.der", "rb");
431 printf("can't find ./certs/rsa2048.der, "
432 "Please run from CyaSSL home dir\n");
437 bytes = fread(tmp, 1, sizeof(tmp), file);
439 bytes = RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
441 start = current_time();
443 for (i = 0; i < times; i++)
444 bytes = RsaPublicEncrypt(message,len,cipher,sizeof(cipher), &key, &rng);
446 total = current_time() - start;
447 each = total / times; /* per second */
448 milliEach = each * 1000; /* milliseconds */
450 printf("RSA 2048 encryption took %6.2f milliseconds, avg over %d"
451 " iterations\n", milliEach, times);
453 start = current_time();
455 for (i = 0; i < times; i++)
456 RsaPrivateDecryptInline(cipher, (word32)bytes, &output, &key);
458 total = current_time() - start;
459 each = total / times; /* per second */
460 milliEach = each * 1000; /* milliseconds */
462 printf("RSA 2048 decryption took %6.2f milliseconds, avg over %d"
463 " iterations\n", milliEach, times);
476 word32 idx = 0, pubSz, privSz, pubSz2, privSz2, agreeSz;
478 byte pub[256]; /* for 2048 bit */
479 byte priv[256]; /* for 2048 bit */
480 byte pub2[256]; /* for 2048 bit */
481 byte priv2[256]; /* for 2048 bit */
482 byte agree[256]; /* for 2048 bit */
484 double start, total, each, milliEach;
486 FILE* file = fopen("./certs/dh2048.der", "rb");
489 printf("can't find ./certs/dh2048.der, "
490 "Please run from CyaSSL home dir\n");
494 bytes = fread(tmp, 1, sizeof(tmp), file);
496 bytes = DhKeyDecode(tmp, &idx, &key, (word32)bytes);
498 start = current_time();
500 for (i = 0; i < times; i++)
501 DhGenerateKeyPair(&key, &rng, priv, &privSz, pub, &pubSz);
503 total = current_time() - start;
504 each = total / times; /* per second */
505 milliEach = each * 1000; /* milliseconds */
507 printf("DH 2048 key generation %6.2f milliseconds, avg over %d"
508 " iterations\n", milliEach, times);
510 DhGenerateKeyPair(&key, &rng, priv2, &privSz2, pub2, &pubSz2);
511 start = current_time();
513 for (i = 0; i < times; i++)
514 DhAgree(&key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
516 total = current_time() - start;
517 each = total / times; /* per second */
518 milliEach = each * 1000; /* milliseconds */
520 printf("DH 2048 key agreement %6.2f milliseconds, avg over %d"
521 " iterations\n", milliEach, times);
528 #ifdef CYASSL_KEY_GEN
529 void bench_rsaKeyGen()
532 double start, total, each, milliEach;
534 const int genTimes = 5;
537 start = current_time();
539 for(i = 0; i < genTimes; i++) {
540 InitRsaKey(&genKey, 0);
541 MakeRsaKey(&genKey, 1024, 65537, &rng);
545 total = current_time() - start;
546 each = total / genTimes; /* per second */
547 milliEach = each * 1000; /* millisconds */
549 printf("RSA 1024 key generation %6.2f milliseconds, avg over %d"
550 " iterations\n", milliEach, genTimes);
553 start = current_time();
555 for(i = 0; i < genTimes; i++) {
556 InitRsaKey(&genKey, 0);
557 MakeRsaKey(&genKey, 2048, 65537, &rng);
561 total = current_time() - start;
562 each = total / genTimes; /* per second */
563 milliEach = each * 1000; /* millisconds */
564 printf("RSA 2048 key generation %6.2f milliseconds, avg over %d"
565 " iterations\n", milliEach, genTimes);
567 #endif /* CYASSL_KEY_GEN */
570 void bench_eccKeyGen()
573 double start, total, each, milliEach;
575 const int genTimes = 5;
578 start = current_time();
580 for(i = 0; i < genTimes; i++) {
581 int ret = ecc_make_key(&rng, 32, &genKey);
585 total = current_time() - start;
586 each = total / genTimes; /* per second */
587 milliEach = each * 1000; /* millisconds */
589 printf("ECC 256 key generation %6.2f milliseconds, avg over %d"
590 " iterations\n", milliEach, genTimes);
594 void bench_eccKeyAgree()
596 ecc_key genKey, genKey2;
597 double start, total, each, milliEach;
599 const int agreeTimes = 5;
605 ecc_make_key(&rng, 32, &genKey);
606 ecc_make_key(&rng, 32, &genKey2);
609 start = current_time();
611 for(i = 0; i < agreeTimes; i++) {
613 ecc_shared_secret(&genKey, &genKey2, shared, &x);
616 total = current_time() - start;
617 each = total / agreeTimes; /* per second */
618 milliEach = each * 1000; /* millisconds */
619 printf("EC-DHE key agreement %6.2f milliseconds, avg over %d"
620 " iterations\n", milliEach, agreeTimes);
622 /* make dummy digest */
623 for (i = 0; i < sizeof(digest); i++)
627 start = current_time();
629 for(i = 0; i < agreeTimes; i++) {
631 ecc_sign_hash(digest, sizeof(digest), sig, &x, &rng, &genKey);
634 total = current_time() - start;
635 each = total / agreeTimes; /* per second */
636 milliEach = each * 1000; /* millisconds */
637 printf("EC-DSA sign time %6.2f milliseconds, avg over %d"
638 " iterations\n", milliEach, agreeTimes);
643 #endif /* HAVE_ECC */
648 #define WIN32_LEAN_AND_MEAN
651 double current_time()
654 static LARGE_INTEGER freq;
659 QueryPerformanceFrequency(&freq);
663 QueryPerformanceCounter(&count);
665 return (double)count.QuadPart / freq.QuadPart;
670 #include <sys/time.h>
672 double current_time()
675 gettimeofday(&tv, 0);
677 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;