3 * Copyright (C) 2006-2014 wolfSSL Inc.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 /* CTaoCrypt benchmark */
28 #include <cyassl/ctaocrypt/settings.h>
33 #include <cyassl/ctaocrypt/des3.h>
34 #include <cyassl/ctaocrypt/arc4.h>
35 #include <cyassl/ctaocrypt/hc128.h>
36 #include <cyassl/ctaocrypt/rabbit.h>
37 #include <cyassl/ctaocrypt/aes.h>
38 #include <cyassl/ctaocrypt/camellia.h>
39 #include <cyassl/ctaocrypt/md5.h>
40 #include <cyassl/ctaocrypt/sha.h>
41 #include <cyassl/ctaocrypt/sha256.h>
42 #include <cyassl/ctaocrypt/sha512.h>
43 #include <cyassl/ctaocrypt/rsa.h>
44 #include <cyassl/ctaocrypt/asn.h>
45 #include <cyassl/ctaocrypt/ripemd.h>
46 #include <cyassl/ctaocrypt/ecc.h>
48 #include <cyassl/ctaocrypt/dh.h>
50 #include "cavium_sysdep.h"
51 #include "cavium_common.h"
52 #include "cavium_ioctl.h"
55 #include "ntru_crypto.h"
58 #if defined(CYASSL_MDK_ARM)
59 extern FILE * CyaSSL_fopen(const char *fname, const char *mode) ;
60 #define fopen CyaSSL_fopen
63 #if defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)
64 /* include test cert and key buffers for use with NO_FILESYSTEM */
65 #if defined(CYASSL_MDK_ARM)
66 #include "cert_data.h" /* use certs_test.c for initial data,
67 so other commands can share the data. */
69 #include <cyassl/certs_test.h>
75 #include <cyassl/ctaocrypt/blake2.h>
76 void bench_blake2(void);
80 /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
81 #pragma warning(disable: 4996)
85 void bench_arc4(void);
86 void bench_hc128(void);
87 void bench_rabbit(void);
89 void bench_aesgcm(void);
90 void bench_aesccm(void);
91 void bench_aesctr(void);
92 void bench_camellia(void);
96 void bench_sha256(void);
97 void bench_sha512(void);
98 void bench_ripemd(void);
100 void bench_rsa(void);
101 void bench_rsaKeyGen(void);
104 void bench_eccKeyGen(void);
105 void bench_eccKeyAgree(void);
108 void bench_ntruKeyGen(void);
111 double current_time(int);
116 static int OpenNitroxDevice(int dma_mode,int dev_id)
118 Csp1CoreAssignment core_assign;
121 if (CspInitialize(CAVIUM_DIRECT,CAVIUM_DEV_ID))
123 if (Csp1GetDevType(&device))
125 if (device != NPX_DEVICE) {
126 if (ioctl(gpkpdev_hdlr[CAVIUM_DEV_ID], IOCTL_CSP1_GET_CORE_ASSIGNMENT,
127 (Uint32 *)&core_assign)!= 0)
130 CspShutdown(CAVIUM_DEV_ID);
132 return CspInitialize(dma_mode, dev_id);
137 #if defined(DEBUG_CYASSL) && !defined(HAVE_VALGRIND)
138 CYASSL_API int CyaSSL_Debugging_ON();
141 /* so embedded projects can pull in tests on their own */
142 #if !defined(NO_MAIN_DRIVER)
144 int main(int argc, char** argv)
150 int benchmark_test(void *args)
154 #if defined(DEBUG_CYASSL) && !defined(HAVE_VALGRIND)
155 CyaSSL_Debugging_ON();
159 int ret = OpenNitroxDevice(CAVIUM_DIRECT, CAVIUM_DEV_ID);
161 printf("Cavium OpenNitroxDevice failed\n");
164 #endif /* HAVE_CAVIUM */
173 #ifdef CYASSL_AES_COUNTER
227 #if defined(CYASSL_KEY_GEN) && !defined(NO_RSA)
244 #ifdef BENCH_EMBEDDED
245 enum BenchmarkBounds {
246 numBlocks = 25, /* how many kB to test (en/de)cryption */
248 genTimes = 5, /* public key iterations */
251 static const char blockType[] = "kB"; /* used in printf output */
253 enum BenchmarkBounds {
254 numBlocks = 5, /* how many megs to test (en/de)cryption */
259 static const char blockType[] = "megs"; /* used in printf output */
262 static const byte key[] =
264 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
265 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
266 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
269 static const byte iv[] =
271 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
272 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
273 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
278 /* use kB instead of mB for embedded benchmarking */
279 #ifdef BENCH_EMBEDDED
280 static byte plain [1024];
281 static byte cipher[1024];
283 static byte plain [1024*1024];
284 static byte cipher[1024*1024];
289 void bench_aes(int show)
292 double start, total, persec;
297 if (AesInitCavium(&enc, CAVIUM_DEV_ID) != 0) {
298 printf("aes init cavium failed\n");
303 ret = AesSetKey(&enc, key, 16, iv, AES_ENCRYPTION);
305 printf("AesSetKey failed, ret = %d\n", ret);
308 start = current_time(1);
310 for(i = 0; i < numBlocks; i++)
311 AesCbcEncrypt(&enc, plain, cipher, sizeof(plain));
313 total = current_time(0) - start;
315 persec = 1 / total * numBlocks;
316 #ifdef BENCH_EMBEDDED
317 /* since using kB, convert to MB/s */
318 persec = persec / 1024;
322 printf("AES %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
323 blockType, total, persec);
331 #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
332 static byte additional[13];
338 void bench_aesgcm(void)
341 double start, total, persec;
344 AesGcmSetKey(&enc, key, 16);
345 start = current_time(1);
347 for(i = 0; i < numBlocks; i++)
348 AesGcmEncrypt(&enc, cipher, plain, sizeof(plain), iv, 12,
349 tag, 16, additional, 13);
351 total = current_time(0) - start;
353 persec = 1 / total * numBlocks;
354 #ifdef BENCH_EMBEDDED
355 /* since using kB, convert to MB/s */
356 persec = persec / 1024;
359 printf("AES-GCM %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
360 blockType, total, persec);
364 #ifdef CYASSL_AES_COUNTER
365 void bench_aesctr(void)
368 double start, total, persec;
371 AesSetKeyDirect(&enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
372 start = current_time(1);
374 for(i = 0; i < numBlocks; i++)
375 AesCtrEncrypt(&enc, plain, cipher, sizeof(plain));
377 total = current_time(0) - start;
379 persec = 1 / total * numBlocks;
380 #ifdef BENCH_EMBEDDED
381 /* since using kB, convert to MB/s */
382 persec = persec / 1024;
385 printf("AES-CTR %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
386 blockType, total, persec);
393 void bench_aesccm(void)
396 double start, total, persec;
399 AesCcmSetKey(&enc, key, 16);
400 start = current_time(1);
402 for(i = 0; i < numBlocks; i++)
403 AesCcmEncrypt(&enc, cipher, plain, sizeof(plain), iv, 12,
404 tag, 16, additional, 13);
406 total = current_time(0) - start;
408 persec = 1 / total * numBlocks;
409 #ifdef BENCH_EMBEDDED
410 /* since using kB, convert to MB/s */
411 persec = persec / 1024;
414 printf("AES-CCM %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
415 blockType, total, persec);
421 void bench_camellia(void)
424 double start, total, persec;
427 ret = CamelliaSetKey(&cam, key, 16, iv);
429 printf("CamelliaSetKey failed, ret = %d\n", ret);
432 start = current_time(1);
434 for(i = 0; i < numBlocks; i++)
435 CamelliaCbcEncrypt(&cam, plain, cipher, sizeof(plain));
437 total = current_time(0) - start;
439 persec = 1 / total * numBlocks;
440 #ifdef BENCH_EMBEDDED
441 /* since using kB, convert to MB/s */
442 persec = persec / 1024;
445 printf("Camellia %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
446 blockType, total, persec);
455 double start, total, persec;
459 if (Des3_InitCavium(&enc, CAVIUM_DEV_ID) != 0)
460 printf("des3 init cavium failed\n");
462 ret = Des3_SetKey(&enc, key, iv, DES_ENCRYPTION);
464 printf("Des3_SetKey failed, ret = %d\n", ret);
467 start = current_time(1);
469 for(i = 0; i < numBlocks; i++)
470 Des3_CbcEncrypt(&enc, plain, cipher, sizeof(plain));
472 total = current_time(0) - start;
474 persec = 1 / total * numBlocks;
475 #ifdef BENCH_EMBEDDED
476 /* since using kB, convert to MB/s */
477 persec = persec / 1024;
480 printf("3DES %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
481 blockType, total, persec);
483 Des3_FreeCavium(&enc);
490 void bench_arc4(void)
493 double start, total, persec;
497 if (Arc4InitCavium(&enc, CAVIUM_DEV_ID) != 0)
498 printf("arc4 init cavium failed\n");
501 Arc4SetKey(&enc, key, 16);
502 start = current_time(1);
504 for(i = 0; i < numBlocks; i++)
505 Arc4Process(&enc, cipher, plain, sizeof(plain));
507 total = current_time(0) - start;
508 persec = 1 / total * numBlocks;
509 #ifdef BENCH_EMBEDDED
510 /* since using kB, convert to MB/s */
511 persec = persec / 1024;
514 printf("ARC4 %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
515 blockType, total, persec);
517 Arc4FreeCavium(&enc);
524 void bench_hc128(void)
527 double start, total, persec;
530 Hc128_SetKey(&enc, key, iv);
531 start = current_time(1);
533 for(i = 0; i < numBlocks; i++)
534 Hc128_Process(&enc, cipher, plain, sizeof(plain));
536 total = current_time(0) - start;
537 persec = 1 / total * numBlocks;
538 #ifdef BENCH_EMBEDDED
539 /* since using kB, convert to MB/s */
540 persec = persec / 1024;
543 printf("HC128 %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
544 blockType, total, persec);
546 #endif /* HAVE_HC128 */
550 void bench_rabbit(void)
553 double start, total, persec;
556 RabbitSetKey(&enc, key, iv);
557 start = current_time(1);
559 for(i = 0; i < numBlocks; i++)
560 RabbitProcess(&enc, cipher, plain, sizeof(plain));
562 total = current_time(0) - start;
563 persec = 1 / total * numBlocks;
564 #ifdef BENCH_EMBEDDED
565 /* since using kB, convert to MB/s */
566 persec = persec / 1024;
569 printf("RABBIT %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
570 blockType, total, persec);
572 #endif /* NO_RABBIT */
579 byte digest[MD5_DIGEST_SIZE];
580 double start, total, persec;
584 start = current_time(1);
586 for(i = 0; i < numBlocks; i++)
587 Md5Update(&hash, plain, sizeof(plain));
589 Md5Final(&hash, digest);
591 total = current_time(0) - start;
592 persec = 1 / total * numBlocks;
593 #ifdef BENCH_EMBEDDED
594 /* since using kB, convert to MB/s */
595 persec = persec / 1024;
598 printf("MD5 %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
599 blockType, total, persec);
608 byte digest[SHA_DIGEST_SIZE];
609 double start, total, persec;
612 ret = InitSha(&hash);
614 printf("InitSha failed, ret = %d\n", ret);
617 start = current_time(1);
619 for(i = 0; i < numBlocks; i++)
620 ShaUpdate(&hash, plain, sizeof(plain));
622 ShaFinal(&hash, digest);
624 total = current_time(0) - start;
625 persec = 1 / total * numBlocks;
626 #ifdef BENCH_EMBEDDED
627 /* since using kB, convert to MB/s */
628 persec = persec / 1024;
631 printf("SHA %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
632 blockType, total, persec);
638 void bench_sha256(void)
641 byte digest[SHA256_DIGEST_SIZE];
642 double start, total, persec;
645 ret = InitSha256(&hash);
647 printf("InitSha256 failed, ret = %d\n", ret);
650 start = current_time(1);
652 for(i = 0; i < numBlocks; i++) {
653 ret = Sha256Update(&hash, plain, sizeof(plain));
655 printf("Sha256Update failed, ret = %d\n", ret);
660 ret = Sha256Final(&hash, digest);
662 printf("Sha256Final failed, ret = %d\n", ret);
666 total = current_time(0) - start;
667 persec = 1 / total * numBlocks;
668 #ifdef BENCH_EMBEDDED
669 /* since using kB, convert to MB/s */
670 persec = persec / 1024;
673 printf("SHA-256 %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
674 blockType, total, persec);
679 void bench_sha512(void)
682 byte digest[SHA512_DIGEST_SIZE];
683 double start, total, persec;
686 ret = InitSha512(&hash);
688 printf("InitSha512 failed, ret = %d\n", ret);
691 start = current_time(1);
693 for(i = 0; i < numBlocks; i++) {
694 ret = Sha512Update(&hash, plain, sizeof(plain));
696 printf("Sha512Update failed, ret = %d\n", ret);
701 ret = Sha512Final(&hash, digest);
703 printf("Sha512Final failed, ret = %d\n", ret);
707 total = current_time(0) - start;
708 persec = 1 / total * numBlocks;
709 #ifdef BENCH_EMBEDDED
710 /* since using kB, convert to MB/s */
711 persec = persec / 1024;
714 printf("SHA-512 %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
715 blockType, total, persec);
720 void bench_ripemd(void)
723 byte digest[RIPEMD_DIGEST_SIZE];
724 double start, total, persec;
728 start = current_time(1);
730 for(i = 0; i < numBlocks; i++)
731 RipeMdUpdate(&hash, plain, sizeof(plain));
733 RipeMdFinal(&hash, digest);
735 total = current_time(0) - start;
736 persec = 1 / total * numBlocks;
737 #ifdef BENCH_EMBEDDED
738 /* since using kB, convert to MB/s */
739 persec = persec / 1024;
742 printf("RIPEMD %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
743 blockType, total, persec);
749 void bench_blake2(void)
753 double start, total, persec;
756 ret = InitBlake2b(&b2b, 64);
758 printf("InitBlake2b failed, ret = %d\n", ret);
761 start = current_time(1);
763 for(i = 0; i < numBlocks; i++) {
764 ret = Blake2bUpdate(&b2b, plain, sizeof(plain));
766 printf("Blake2bUpdate failed, ret = %d\n", ret);
771 ret = Blake2bFinal(&b2b, digest, 64);
773 printf("Blake2bFinal failed, ret = %d\n", ret);
777 total = current_time(0) - start;
778 persec = 1 / total * numBlocks;
779 #ifdef BENCH_EMBEDDED
780 /* since using kB, convert to MB/s */
781 persec = persec / 1024;
784 printf("BLAKE2b %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
785 blockType, total, persec);
790 #if !defined(NO_RSA) || !defined(NO_DH) \
791 || defined(CYASSL_KEYGEN) || defined(HAVE_ECC)
798 #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
799 #if defined(CYASSL_MDK_SHELL)
800 static char *certRSAname = "certs/rsa2048.der";
801 /* set by shell command */
802 static void set_Bench_RSA_File(char * cert) { certRSAname = cert ; }
804 static const char *certRSAname = "certs/rsa2048.der";
816 byte message[] = "Everyone gets Friday off.";
817 byte enc[512]; /* for up to 4096 bit */
818 const int len = (int)strlen((char*)message);
819 double start, total, each, milliEach;
822 int rsaKeySz = 2048; /* used in printf */
824 #ifdef USE_CERT_BUFFERS_1024
825 XMEMCPY(tmp, rsa_key_der_1024, sizeof_rsa_key_der_1024);
826 bytes = sizeof_rsa_key_der_1024;
828 #elif defined(USE_CERT_BUFFERS_2048)
829 XMEMCPY(tmp, rsa_key_der_2048, sizeof_rsa_key_der_2048);
830 bytes = sizeof_rsa_key_der_2048;
832 FILE* file = fopen(certRSAname, "rb");
835 printf("can't find %s, Please run from CyaSSL home dir\n", certRSAname);
839 bytes = fread(tmp, 1, sizeof(tmp), file);
841 #endif /* USE_CERT_BUFFERS */
845 if (RsaInitCavium(&rsaKey, CAVIUM_DEV_ID) != 0)
846 printf("RSA init cavium failed\n");
850 printf("InitRNG failed\n");
853 ret = InitRsaKey(&rsaKey, 0);
855 printf("InitRsaKey failed\n");
858 ret = RsaPrivateKeyDecode(tmp, &idx, &rsaKey, (word32)bytes);
860 start = current_time(1);
862 for (i = 0; i < ntimes; i++)
863 ret = RsaPublicEncrypt(message,len,enc,sizeof(enc), &rsaKey, &rng);
865 total = current_time(0) - start;
866 each = total / ntimes; /* per second */
867 milliEach = each * 1000; /* milliseconds */
869 printf("RSA %d encryption took %6.3f milliseconds, avg over %d"
870 " iterations\n", rsaKeySz, milliEach, ntimes);
873 printf("Rsa Public Encrypt failed\n");
877 start = current_time(1);
879 for (i = 0; i < ntimes; i++) {
880 byte out[512]; /* for up to 4096 bit */
881 RsaPrivateDecrypt(enc, (word32)ret, out, sizeof(out), &rsaKey);
884 total = current_time(0) - start;
885 each = total / ntimes; /* per second */
886 milliEach = each * 1000; /* milliseconds */
888 printf("RSA %d decryption took %6.3f milliseconds, avg over %d"
889 " iterations\n", rsaKeySz, milliEach, ntimes);
893 RsaFreeCavium(&rsaKey);
902 #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
903 #if defined(CYASSL_MDK_SHELL)
904 static char *certDHname = "certs/dh2048.der";
905 /* set by shell command */
906 void set_Bench_DH_File(char * cert) { certDHname = cert ; }
908 static const char *certDHname = "certs/dh2048.der";
914 #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
920 word32 idx = 0, pubSz, privSz = 0, pubSz2, privSz2, agreeSz;
922 byte pub[256]; /* for 2048 bit */
923 byte priv[256]; /* for 2048 bit */
924 byte pub2[256]; /* for 2048 bit */
925 byte priv2[256]; /* for 2048 bit */
926 byte agree[256]; /* for 2048 bit */
928 double start, total, each, milliEach;
930 int dhKeySz = 2048; /* used in printf */
933 #ifdef USE_CERT_BUFFERS_1024
934 XMEMCPY(tmp, dh_key_der_1024, sizeof_dh_key_der_1024);
935 bytes = sizeof_dh_key_der_1024;
937 #elif defined(USE_CERT_BUFFERS_2048)
938 XMEMCPY(tmp, dh_key_der_2048, sizeof_dh_key_der_2048);
939 bytes = sizeof_dh_key_der_2048;
941 FILE* file = fopen(certDHname, "rb");
944 printf("can't find %s, Please run from CyaSSL home dir\n", certDHname);
950 printf("InitRNG failed\n");
953 bytes = fread(tmp, 1, sizeof(tmp), file);
954 #endif /* USE_CERT_BUFFERS */
958 bytes = DhKeyDecode(tmp, &idx, &dhKey, (word32)bytes);
960 printf("dhekydecode failed, can't benchmark\n");
961 #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
967 start = current_time(1);
969 for (i = 0; i < ntimes; i++)
970 DhGenerateKeyPair(&dhKey, &rng, priv, &privSz, pub, &pubSz);
972 total = current_time(0) - start;
973 each = total / ntimes; /* per second */
974 milliEach = each * 1000; /* milliseconds */
976 printf("DH %d key generation %6.3f milliseconds, avg over %d"
977 " iterations\n", dhKeySz, milliEach, ntimes);
979 DhGenerateKeyPair(&dhKey, &rng, priv2, &privSz2, pub2, &pubSz2);
980 start = current_time(1);
982 for (i = 0; i < ntimes; i++)
983 DhAgree(&dhKey, agree, &agreeSz, priv, privSz, pub2, pubSz2);
985 total = current_time(0) - start;
986 each = total / ntimes; /* per second */
987 milliEach = each * 1000; /* milliseconds */
989 printf("DH %d key agreement %6.3f milliseconds, avg over %d"
990 " iterations\n", dhKeySz, milliEach, ntimes);
992 #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
999 #if defined(CYASSL_KEY_GEN) && !defined(NO_RSA)
1000 void bench_rsaKeyGen(void)
1003 double start, total, each, milliEach;
1007 start = current_time(1);
1009 for(i = 0; i < genTimes; i++) {
1010 InitRsaKey(&genKey, 0);
1011 MakeRsaKey(&genKey, 1024, 65537, &rng);
1012 FreeRsaKey(&genKey);
1015 total = current_time(0) - start;
1016 each = total / genTimes; /* per second */
1017 milliEach = each * 1000; /* millisconds */
1019 printf("RSA 1024 key generation %6.3f milliseconds, avg over %d"
1020 " iterations\n", milliEach, genTimes);
1023 start = current_time(1);
1025 for(i = 0; i < genTimes; i++) {
1026 InitRsaKey(&genKey, 0);
1027 MakeRsaKey(&genKey, 2048, 65537, &rng);
1028 FreeRsaKey(&genKey);
1031 total = current_time(0) - start;
1032 each = total / genTimes; /* per second */
1033 milliEach = each * 1000; /* millisconds */
1034 printf("RSA 2048 key generation %6.3f milliseconds, avg over %d"
1035 " iterations\n", milliEach, genTimes);
1037 #endif /* CYASSL_KEY_GEN */
1039 byte GetEntropy(ENTROPY_CMD cmd, byte* out);
1041 byte GetEntropy(ENTROPY_CMD cmd, byte* out)
1044 return (InitRng(&rng) == 0) ? 1 : 0;
1049 if (cmd == GET_BYTE_OF_ENTROPY)
1050 return (RNG_GenerateBlock(&rng, out, 1) == 0) ? 1 : 0;
1052 if (cmd == GET_NUM_BYTES_PER_BYTE_OF_ENTROPY) {
1059 void bench_ntruKeyGen(void)
1061 double start, total, each, milliEach;
1064 byte public_key[557]; /* 2048 key equivalent to rsa */
1065 word16 public_key_len = sizeof(public_key);
1066 byte private_key[607];
1067 word16 private_key_len = sizeof(private_key);
1070 static uint8_t const pers_str[] = {
1071 'C', 'y', 'a', 'S', 'S', 'L', ' ', 't', 'e', 's', 't'
1074 word32 rc = ntru_crypto_drbg_instantiate(112, pers_str, sizeof(pers_str),
1077 printf("NTRU drbg instantiate failed\n");
1081 start = current_time(1);
1083 for(i = 0; i < genTimes; i++) {
1084 ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2, &public_key_len,
1085 public_key, &private_key_len, private_key);
1088 total = current_time(0) - start;
1090 rc = ntru_crypto_drbg_uninstantiate(drbg);
1092 if (rc != NTRU_OK) {
1093 printf("NTRU drbg uninstantiate failed\n");
1097 each = total / genTimes;
1098 milliEach = each * 1000;
1101 printf("NTRU 112 key generation %6.3f milliseconds, avg over %d"
1102 " iterations\n", milliEach, genTimes);
1108 void bench_eccKeyGen(void)
1111 double start, total, each, milliEach;
1114 ret = InitRng(&rng);
1116 printf("InitRNG failed\n");
1120 start = current_time(1);
1122 for(i = 0; i < genTimes; i++) {
1123 ecc_make_key(&rng, 32, &genKey);
1127 total = current_time(0) - start;
1128 each = total / genTimes; /* per second */
1129 milliEach = each * 1000; /* millisconds */
1131 printf("ECC 256 key generation %6.3f milliseconds, avg over %d"
1132 " iterations\n", milliEach, genTimes);
1136 void bench_eccKeyAgree(void)
1138 ecc_key genKey, genKey2;
1139 double start, total, each, milliEach;
1149 ret = InitRng(&rng);
1151 printf("InitRNG failed\n");
1155 ret = ecc_make_key(&rng, 32, &genKey);
1157 printf("ecc_make_key failed\n");
1160 ret = ecc_make_key(&rng, 32, &genKey2);
1162 printf("ecc_make_key failed\n");
1167 start = current_time(1);
1169 for(i = 0; i < agreeTimes; i++) {
1171 ret = ecc_shared_secret(&genKey, &genKey2, shared, &x);
1173 printf("ecc_shared_secret failed\n");
1178 total = current_time(0) - start;
1179 each = total / agreeTimes; /* per second */
1180 milliEach = each * 1000; /* millisconds */
1181 printf("EC-DHE key agreement %6.3f milliseconds, avg over %d"
1182 " iterations\n", milliEach, agreeTimes);
1184 /* make dummy digest */
1185 for (i = 0; i < (int)sizeof(digest); i++)
1186 digest[i] = (byte)i;
1189 start = current_time(1);
1191 for(i = 0; i < agreeTimes; i++) {
1193 ret = ecc_sign_hash(digest, sizeof(digest), sig, &x, &rng, &genKey);
1195 printf("ecc_sign_hash failed\n");
1200 total = current_time(0) - start;
1201 each = total / agreeTimes; /* per second */
1202 milliEach = each * 1000; /* millisconds */
1203 printf("EC-DSA sign time %6.3f milliseconds, avg over %d"
1204 " iterations\n", milliEach, agreeTimes);
1206 start = current_time(1);
1208 for(i = 0; i < agreeTimes; i++) {
1210 ret = ecc_verify_hash(sig, x, digest, sizeof(digest), &verify, &genKey);
1212 printf("ecc_verify_hash failed\n");
1217 total = current_time(0) - start;
1218 each = total / agreeTimes; /* per second */
1219 milliEach = each * 1000; /* millisconds */
1220 printf("EC-DSA verify time %6.3f milliseconds, avg over %d"
1221 " iterations\n", milliEach, agreeTimes);
1226 #endif /* HAVE_ECC */
1230 #define WIN32_LEAN_AND_MEAN
1231 #include <windows.h>
1233 double current_time(int reset)
1235 static int init = 0;
1236 static LARGE_INTEGER freq;
1238 LARGE_INTEGER count;
1243 QueryPerformanceFrequency(&freq);
1247 QueryPerformanceCounter(&count);
1249 return (double)count.QuadPart / freq.QuadPart;
1252 #elif defined MICROCHIP_PIC32
1253 #if defined(CYASSL_MICROCHIP_PIC32MZ)
1254 #define CLOCK 80000000.0
1256 #include <peripheral/timer.h>
1257 #define CLOCK 40000000.0
1260 double current_time(int reset)
1268 /* get timer in ns */
1269 ns = ReadCoreTimer();
1271 /* return seconds as a double */
1272 return ( ns / CLOCK * 2.0);
1275 #elif defined(CYASSL_IAR_ARM) || defined (CYASSL_MDK_ARM)
1276 #warning "Write your current_time()"
1277 double current_time(int reset) { return 0.0 ; }
1279 #elif defined FREERTOS
1281 double current_time(int reset)
1285 portTickType tickCount;
1287 /* tick count == ms, if configTICK_RATE_HZ is set to 1000 */
1288 tickCount = xTaskGetTickCount();
1289 return (double)tickCount / 1000;
1294 #include <sys/time.h>
1296 double current_time(int reset)
1302 gettimeofday(&tv, 0);
1304 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;