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 #if defined(CYASSL_MDK_ARM)
56 extern FILE * CyaSSL_fopen(const char *fname, const char *mode) ;
57 #define fopen CyaSSL_fopen
60 #if defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)
61 /* include test cert and key buffers for use with NO_FILESYSTEM */
62 #if defined(CYASSL_MDK_ARM)
63 #include "cert_data.h" /* use certs_test.c for initial data,
64 so other commands can share the data. */
66 #include <cyassl/certs_test.h>
72 #include <cyassl/ctaocrypt/blake2.h>
73 void bench_blake2(void);
77 /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
78 #pragma warning(disable: 4996)
82 void bench_arc4(void);
83 void bench_hc128(void);
84 void bench_rabbit(void);
86 void bench_aesgcm(void);
87 void bench_aesccm(void);
88 void bench_aesctr(void);
89 void bench_camellia(void);
93 void bench_sha256(void);
94 void bench_sha512(void);
95 void bench_ripemd(void);
98 void bench_rsaKeyGen(void);
101 void bench_eccKeyGen(void);
102 void bench_eccKeyAgree(void);
105 double current_time(int);
110 static int OpenNitroxDevice(int dma_mode,int dev_id)
112 Csp1CoreAssignment core_assign;
115 if (CspInitialize(CAVIUM_DIRECT,CAVIUM_DEV_ID))
117 if (Csp1GetDevType(&device))
119 if (device != NPX_DEVICE) {
120 if (ioctl(gpkpdev_hdlr[CAVIUM_DEV_ID], IOCTL_CSP1_GET_CORE_ASSIGNMENT,
121 (Uint32 *)&core_assign)!= 0)
124 CspShutdown(CAVIUM_DEV_ID);
126 return CspInitialize(dma_mode, dev_id);
132 /* so embedded projects can pull in tests on their own */
133 #if !defined(NO_MAIN_DRIVER)
135 int main(int argc, char** argv)
141 int benchmark_test(void *args)
146 int ret = OpenNitroxDevice(CAVIUM_DIRECT, CAVIUM_DEV_ID);
148 printf("Cavium OpenNitroxDevice failed\n");
151 #endif /* HAVE_CAVIUM */
160 #ifdef CYASSL_AES_COUNTER
214 #if defined(CYASSL_KEY_GEN) && !defined(NO_RSA)
227 #ifdef BENCH_EMBEDDED
228 enum BenchmarkBounds {
229 numBlocks = 25, /* how many kB to test (en/de)cryption */
231 genTimes = 5, /* public key iterations */
234 static const char blockType[] = "kB"; /* used in printf output */
236 enum BenchmarkBounds {
237 numBlocks = 5, /* how many megs to test (en/de)cryption */
242 static const char blockType[] = "megs"; /* used in printf output */
245 static const byte key[] =
247 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
248 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
249 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
252 static const byte iv[] =
254 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
255 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
256 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
261 /* use kB instead of mB for embedded benchmarking */
262 #ifdef BENCH_EMBEDDED
263 static byte plain [1024];
264 static byte cipher[1024];
266 static byte plain [1024*1024];
267 static byte cipher[1024*1024];
272 void bench_aes(int show)
275 double start, total, persec;
280 if (AesInitCavium(&enc, CAVIUM_DEV_ID) != 0) {
281 printf("aes init cavium failed\n");
286 ret = AesSetKey(&enc, key, 16, iv, AES_ENCRYPTION);
288 printf("AesSetKey failed, ret = %d\n", ret);
291 start = current_time(1);
293 for(i = 0; i < numBlocks; i++)
294 AesCbcEncrypt(&enc, plain, cipher, sizeof(plain));
296 total = current_time(0) - start;
298 persec = 1 / total * numBlocks;
299 #ifdef BENCH_EMBEDDED
300 /* since using kB, convert to MB/s */
301 persec = persec / 1024;
305 printf("AES %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
306 blockType, total, persec);
314 #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
315 static byte additional[13];
321 void bench_aesgcm(void)
324 double start, total, persec;
327 AesGcmSetKey(&enc, key, 16);
328 start = current_time(1);
330 for(i = 0; i < numBlocks; i++)
331 AesGcmEncrypt(&enc, cipher, plain, sizeof(plain), iv, 12,
332 tag, 16, additional, 13);
334 total = current_time(0) - start;
336 persec = 1 / total * numBlocks;
337 #ifdef BENCH_EMBEDDED
338 /* since using kB, convert to MB/s */
339 persec = persec / 1024;
342 printf("AES-GCM %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
343 blockType, total, persec);
347 #ifdef CYASSL_AES_COUNTER
348 void bench_aesctr(void)
351 double start, total, persec;
354 AesSetKeyDirect(&enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
355 start = current_time(1);
357 for(i = 0; i < numBlocks; i++)
358 AesCtrEncrypt(&enc, plain, cipher, sizeof(plain));
360 total = current_time(0) - start;
362 persec = 1 / total * numBlocks;
363 #ifdef BENCH_EMBEDDED
364 /* since using kB, convert to MB/s */
365 persec = persec / 1024;
368 printf("AES-CTR %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
369 blockType, total, persec);
376 void bench_aesccm(void)
379 double start, total, persec;
382 AesCcmSetKey(&enc, key, 16);
383 start = current_time(1);
385 for(i = 0; i < numBlocks; i++)
386 AesCcmEncrypt(&enc, cipher, plain, sizeof(plain), iv, 12,
387 tag, 16, additional, 13);
389 total = current_time(0) - start;
391 persec = 1 / total * numBlocks;
392 #ifdef BENCH_EMBEDDED
393 /* since using kB, convert to MB/s */
394 persec = persec / 1024;
397 printf("AES-CCM %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
398 blockType, total, persec);
404 void bench_camellia(void)
407 double start, total, persec;
410 ret = CamelliaSetKey(&cam, key, 16, iv);
412 printf("CamelliaSetKey failed, ret = %d\n", ret);
415 start = current_time(1);
417 for(i = 0; i < numBlocks; i++)
418 CamelliaCbcEncrypt(&cam, plain, cipher, sizeof(plain));
420 total = current_time(0) - start;
422 persec = 1 / total * numBlocks;
423 #ifdef BENCH_EMBEDDED
424 /* since using kB, convert to MB/s */
425 persec = persec / 1024;
428 printf("Camellia %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
429 blockType, total, persec);
438 double start, total, persec;
442 if (Des3_InitCavium(&enc, CAVIUM_DEV_ID) != 0)
443 printf("des3 init cavium failed\n");
445 ret = Des3_SetKey(&enc, key, iv, DES_ENCRYPTION);
447 printf("Des3_SetKey failed, ret = %d\n", ret);
450 start = current_time(1);
452 for(i = 0; i < numBlocks; i++)
453 Des3_CbcEncrypt(&enc, plain, cipher, sizeof(plain));
455 total = current_time(0) - start;
457 persec = 1 / total * numBlocks;
458 #ifdef BENCH_EMBEDDED
459 /* since using kB, convert to MB/s */
460 persec = persec / 1024;
463 printf("3DES %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
464 blockType, total, persec);
466 Des3_FreeCavium(&enc);
473 void bench_arc4(void)
476 double start, total, persec;
480 if (Arc4InitCavium(&enc, CAVIUM_DEV_ID) != 0)
481 printf("arc4 init cavium failed\n");
484 Arc4SetKey(&enc, key, 16);
485 start = current_time(1);
487 for(i = 0; i < numBlocks; i++)
488 Arc4Process(&enc, cipher, plain, sizeof(plain));
490 total = current_time(0) - start;
491 persec = 1 / total * numBlocks;
492 #ifdef BENCH_EMBEDDED
493 /* since using kB, convert to MB/s */
494 persec = persec / 1024;
497 printf("ARC4 %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
498 blockType, total, persec);
500 Arc4FreeCavium(&enc);
507 void bench_hc128(void)
510 double start, total, persec;
513 Hc128_SetKey(&enc, key, iv);
514 start = current_time(1);
516 for(i = 0; i < numBlocks; i++)
517 Hc128_Process(&enc, cipher, plain, sizeof(plain));
519 total = current_time(0) - start;
520 persec = 1 / total * numBlocks;
521 #ifdef BENCH_EMBEDDED
522 /* since using kB, convert to MB/s */
523 persec = persec / 1024;
526 printf("HC128 %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
527 blockType, total, persec);
529 #endif /* HAVE_HC128 */
533 void bench_rabbit(void)
536 double start, total, persec;
539 RabbitSetKey(&enc, key, iv);
540 start = current_time(1);
542 for(i = 0; i < numBlocks; i++)
543 RabbitProcess(&enc, cipher, plain, sizeof(plain));
545 total = current_time(0) - start;
546 persec = 1 / total * numBlocks;
547 #ifdef BENCH_EMBEDDED
548 /* since using kB, convert to MB/s */
549 persec = persec / 1024;
552 printf("RABBIT %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
553 blockType, total, persec);
555 #endif /* NO_RABBIT */
562 byte digest[MD5_DIGEST_SIZE];
563 double start, total, persec;
567 start = current_time(1);
569 for(i = 0; i < numBlocks; i++)
570 Md5Update(&hash, plain, sizeof(plain));
572 Md5Final(&hash, digest);
574 total = current_time(0) - start;
575 persec = 1 / total * numBlocks;
576 #ifdef BENCH_EMBEDDED
577 /* since using kB, convert to MB/s */
578 persec = persec / 1024;
581 printf("MD5 %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
582 blockType, total, persec);
591 byte digest[SHA_DIGEST_SIZE];
592 double start, total, persec;
595 ret = InitSha(&hash);
597 printf("InitSha failed, ret = %d\n", ret);
600 start = current_time(1);
602 for(i = 0; i < numBlocks; i++)
603 ShaUpdate(&hash, plain, sizeof(plain));
605 ShaFinal(&hash, digest);
607 total = current_time(0) - start;
608 persec = 1 / total * numBlocks;
609 #ifdef BENCH_EMBEDDED
610 /* since using kB, convert to MB/s */
611 persec = persec / 1024;
614 printf("SHA %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
615 blockType, total, persec);
621 void bench_sha256(void)
624 byte digest[SHA256_DIGEST_SIZE];
625 double start, total, persec;
628 ret = InitSha256(&hash);
630 printf("InitSha256 failed, ret = %d\n", ret);
633 start = current_time(1);
635 for(i = 0; i < numBlocks; i++) {
636 ret = Sha256Update(&hash, plain, sizeof(plain));
638 printf("Sha256Update failed, ret = %d\n", ret);
643 ret = Sha256Final(&hash, digest);
645 printf("Sha256Final failed, ret = %d\n", ret);
649 total = current_time(0) - start;
650 persec = 1 / total * numBlocks;
651 #ifdef BENCH_EMBEDDED
652 /* since using kB, convert to MB/s */
653 persec = persec / 1024;
656 printf("SHA-256 %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
657 blockType, total, persec);
662 void bench_sha512(void)
665 byte digest[SHA512_DIGEST_SIZE];
666 double start, total, persec;
669 ret = InitSha512(&hash);
671 printf("InitSha512 failed, ret = %d\n", ret);
674 start = current_time(1);
676 for(i = 0; i < numBlocks; i++) {
677 ret = Sha512Update(&hash, plain, sizeof(plain));
679 printf("Sha512Update failed, ret = %d\n", ret);
684 ret = Sha512Final(&hash, digest);
686 printf("Sha512Final failed, ret = %d\n", ret);
690 total = current_time(0) - start;
691 persec = 1 / total * numBlocks;
692 #ifdef BENCH_EMBEDDED
693 /* since using kB, convert to MB/s */
694 persec = persec / 1024;
697 printf("SHA-512 %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
698 blockType, total, persec);
703 void bench_ripemd(void)
706 byte digest[RIPEMD_DIGEST_SIZE];
707 double start, total, persec;
711 start = current_time(1);
713 for(i = 0; i < numBlocks; i++)
714 RipeMdUpdate(&hash, plain, sizeof(plain));
716 RipeMdFinal(&hash, digest);
718 total = current_time(0) - start;
719 persec = 1 / total * numBlocks;
720 #ifdef BENCH_EMBEDDED
721 /* since using kB, convert to MB/s */
722 persec = persec / 1024;
725 printf("RIPEMD %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
726 blockType, total, persec);
732 void bench_blake2(void)
736 double start, total, persec;
739 ret = InitBlake2b(&b2b, 64);
741 printf("InitBlake2b failed, ret = %d\n", ret);
744 start = current_time(1);
746 for(i = 0; i < numBlocks; i++) {
747 ret = Blake2bUpdate(&b2b, plain, sizeof(plain));
749 printf("Blake2bUpdate failed, ret = %d\n", ret);
754 ret = Blake2bFinal(&b2b, digest, 64);
756 printf("Blake2bFinal failed, ret = %d\n", ret);
760 total = current_time(0) - start;
761 persec = 1 / total * numBlocks;
762 #ifdef BENCH_EMBEDDED
763 /* since using kB, convert to MB/s */
764 persec = persec / 1024;
767 printf("BLAKE2b %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks,
768 blockType, total, persec);
773 #if !defined(NO_RSA) || !defined(NO_DH) \
774 || defined(CYASSL_KEYGEN) || defined(HAVE_ECC)
781 #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
782 defined(CYASSL_MDK_SHELL)
783 static char *certRSAname = "certs/rsa2048.der" ;
784 static void set_Bench_RSA_File(char * cert) { certRSAname = cert ; }
785 /* set by shell command */
786 #elif defined(CYASSL_MDK_SHELL)
789 static const char *certRSAname = "certs/rsa2048.der" ;
800 byte message[] = "Everyone gets Friday off.";
801 byte enc[512]; /* for up to 4096 bit */
802 const int len = (int)strlen((char*)message);
803 double start, total, each, milliEach;
806 int rsaKeySz = 2048; /* used in printf */
808 #ifdef USE_CERT_BUFFERS_1024
809 XMEMCPY(tmp, rsa_key_der_1024, sizeof_rsa_key_der_1024);
810 bytes = sizeof_rsa_key_der_1024;
812 #elif defined(USE_CERT_BUFFERS_2048)
813 XMEMCPY(tmp, rsa_key_der_2048, sizeof_rsa_key_der_2048);
814 bytes = sizeof_rsa_key_der_2048;
816 FILE* file = fopen(certRSAname, "rb");
819 printf("can't find %s, Please run from CyaSSL home dir\n", certRSAname);
823 bytes = fread(tmp, 1, sizeof(tmp), file);
825 #endif /* USE_CERT_BUFFERS */
829 if (RsaInitCavium(&rsaKey, CAVIUM_DEV_ID) != 0)
830 printf("RSA init cavium failed\n");
834 printf("InitRNG failed\n");
837 ret = InitRsaKey(&rsaKey, 0);
839 printf("InitRsaKey failed\n");
842 ret = RsaPrivateKeyDecode(tmp, &idx, &rsaKey, (word32)bytes);
844 start = current_time(1);
846 for (i = 0; i < ntimes; i++)
847 ret = RsaPublicEncrypt(message,len,enc,sizeof(enc), &rsaKey, &rng);
849 total = current_time(0) - start;
850 each = total / ntimes; /* per second */
851 milliEach = each * 1000; /* milliseconds */
853 printf("RSA %d encryption took %6.3f milliseconds, avg over %d"
854 " iterations\n", rsaKeySz, milliEach, ntimes);
857 printf("Rsa Public Encrypt failed\n");
861 start = current_time(1);
863 for (i = 0; i < ntimes; i++) {
864 byte out[512]; /* for up to 4096 bit */
865 RsaPrivateDecrypt(enc, (word32)ret, out, sizeof(out), &rsaKey);
868 total = current_time(0) - start;
869 each = total / ntimes; /* per second */
870 milliEach = each * 1000; /* milliseconds */
872 printf("RSA %d decryption took %6.3f milliseconds, avg over %d"
873 " iterations\n", rsaKeySz, milliEach, ntimes);
877 RsaFreeCavium(&rsaKey);
886 #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
887 defined(CYASSL_MDK_SHELL)
888 static char *certDHname = "certs/dh2048.der" ;
889 void set_Bench_DH_File(char * cert) { certDHname = cert ; }
890 /* set by shell command */
891 #elif defined(CYASSL_MDK_SHELL)
894 static const char *certDHname = "certs/dh2048.der" ;
902 word32 idx = 0, pubSz, privSz = 0, pubSz2, privSz2, agreeSz;
904 byte pub[256]; /* for 2048 bit */
905 byte priv[256]; /* for 2048 bit */
906 byte pub2[256]; /* for 2048 bit */
907 byte priv2[256]; /* for 2048 bit */
908 byte agree[256]; /* for 2048 bit */
910 double start, total, each, milliEach;
912 int dhKeySz = 2048; /* used in printf */
915 #ifdef USE_CERT_BUFFERS_1024
916 XMEMCPY(tmp, dh_key_der_1024, sizeof_dh_key_der_1024);
917 bytes = sizeof_dh_key_der_1024;
919 #elif defined(USE_CERT_BUFFERS_2048)
920 XMEMCPY(tmp, dh_key_der_2048, sizeof_dh_key_der_2048);
921 bytes = sizeof_dh_key_der_2048;
923 FILE* file = fopen(certDHname, "rb");
926 printf("can't find %s, Please run from CyaSSL home dir\n", certDHname);
932 printf("InitRNG failed\n");
935 bytes = fread(tmp, 1, sizeof(tmp), file);
936 #endif /* USE_CERT_BUFFERS */
940 bytes = DhKeyDecode(tmp, &idx, &dhKey, (word32)bytes);
942 printf("dhekydecode failed, can't benchmark\n");
943 #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
949 start = current_time(1);
951 for (i = 0; i < ntimes; i++)
952 DhGenerateKeyPair(&dhKey, &rng, priv, &privSz, pub, &pubSz);
954 total = current_time(0) - start;
955 each = total / ntimes; /* per second */
956 milliEach = each * 1000; /* milliseconds */
958 printf("DH %d key generation %6.3f milliseconds, avg over %d"
959 " iterations\n", dhKeySz, milliEach, ntimes);
961 DhGenerateKeyPair(&dhKey, &rng, priv2, &privSz2, pub2, &pubSz2);
962 start = current_time(1);
964 for (i = 0; i < ntimes; i++)
965 DhAgree(&dhKey, agree, &agreeSz, priv, privSz, pub2, pubSz2);
967 total = current_time(0) - start;
968 each = total / ntimes; /* per second */
969 milliEach = each * 1000; /* milliseconds */
971 printf("DH %d key agreement %6.3f milliseconds, avg over %d"
972 " iterations\n", dhKeySz, milliEach, ntimes);
974 #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
981 #if defined(CYASSL_KEY_GEN) && !defined(NO_RSA)
982 void bench_rsaKeyGen(void)
985 double start, total, each, milliEach;
989 start = current_time(1);
991 for(i = 0; i < genTimes; i++) {
992 InitRsaKey(&genKey, 0);
993 MakeRsaKey(&genKey, 1024, 65537, &rng);
997 total = current_time(0) - start;
998 each = total / genTimes; /* per second */
999 milliEach = each * 1000; /* millisconds */
1001 printf("RSA 1024 key generation %6.3f milliseconds, avg over %d"
1002 " iterations\n", milliEach, genTimes);
1005 start = current_time(1);
1007 for(i = 0; i < genTimes; i++) {
1008 InitRsaKey(&genKey, 0);
1009 MakeRsaKey(&genKey, 2048, 65537, &rng);
1010 FreeRsaKey(&genKey);
1013 total = current_time(0) - start;
1014 each = total / genTimes; /* per second */
1015 milliEach = each * 1000; /* millisconds */
1016 printf("RSA 2048 key generation %6.3f milliseconds, avg over %d"
1017 " iterations\n", milliEach, genTimes);
1019 #endif /* CYASSL_KEY_GEN */
1022 void bench_eccKeyGen(void)
1025 double start, total, each, milliEach;
1028 ret = InitRng(&rng);
1030 printf("InitRNG failed\n");
1034 start = current_time(1);
1036 for(i = 0; i < genTimes; i++) {
1037 ecc_make_key(&rng, 32, &genKey);
1041 total = current_time(0) - start;
1042 each = total / genTimes; /* per second */
1043 milliEach = each * 1000; /* millisconds */
1045 printf("ECC 256 key generation %6.3f milliseconds, avg over %d"
1046 " iterations\n", milliEach, genTimes);
1050 void bench_eccKeyAgree(void)
1052 ecc_key genKey, genKey2;
1053 double start, total, each, milliEach;
1063 ret = InitRng(&rng);
1065 printf("InitRNG failed\n");
1069 ret = ecc_make_key(&rng, 32, &genKey);
1071 printf("ecc_make_key failed\n");
1074 ret = ecc_make_key(&rng, 32, &genKey2);
1076 printf("ecc_make_key failed\n");
1081 start = current_time(1);
1083 for(i = 0; i < agreeTimes; i++) {
1085 ret = ecc_shared_secret(&genKey, &genKey2, shared, &x);
1087 printf("ecc_shared_secret failed\n");
1092 total = current_time(0) - start;
1093 each = total / agreeTimes; /* per second */
1094 milliEach = each * 1000; /* millisconds */
1095 printf("EC-DHE key agreement %6.3f milliseconds, avg over %d"
1096 " iterations\n", milliEach, agreeTimes);
1098 /* make dummy digest */
1099 for (i = 0; i < (int)sizeof(digest); i++)
1100 digest[i] = (byte)i;
1103 start = current_time(1);
1105 for(i = 0; i < agreeTimes; i++) {
1107 ret = ecc_sign_hash(digest, sizeof(digest), sig, &x, &rng, &genKey);
1109 printf("ecc_sign_hash failed\n");
1114 total = current_time(0) - start;
1115 each = total / agreeTimes; /* per second */
1116 milliEach = each * 1000; /* millisconds */
1117 printf("EC-DSA sign time %6.3f milliseconds, avg over %d"
1118 " iterations\n", milliEach, agreeTimes);
1120 start = current_time(1);
1122 for(i = 0; i < agreeTimes; i++) {
1124 ret = ecc_verify_hash(sig, x, digest, sizeof(digest), &verify, &genKey);
1126 printf("ecc_verify_hash failed\n");
1131 total = current_time(0) - start;
1132 each = total / agreeTimes; /* per second */
1133 milliEach = each * 1000; /* millisconds */
1134 printf("EC-DSA verify time %6.3f milliseconds, avg over %d"
1135 " iterations\n", milliEach, agreeTimes);
1140 #endif /* HAVE_ECC */
1145 #define WIN32_LEAN_AND_MEAN
1146 #include <windows.h>
1148 double current_time(int reset)
1150 static int init = 0;
1151 static LARGE_INTEGER freq;
1153 LARGE_INTEGER count;
1158 QueryPerformanceFrequency(&freq);
1162 QueryPerformanceCounter(&count);
1164 return (double)count.QuadPart / freq.QuadPart;
1167 #elif defined MICROCHIP_PIC32
1168 #if defined(CYASSL_MICROCHIP_PIC32MZ)
1169 #define CLOCK 8000000.0
1171 #include <peripheral/timer.h>
1172 #define CLOCK 4000000.0
1175 double current_time(int reset)
1183 /* get timer in ns */
1184 ns = ReadCoreTimer();
1186 /* return seconds as a double */
1187 return ( ns / CLOCK * 2.0);
1190 #elif defined CYASSL_MDK_ARM
1192 extern double current_time(int reset) ;
1194 #elif defined FREERTOS
1196 double current_time(int reset)
1200 portTickType tickCount;
1202 /* tick count == ms, if configTICK_RATE_HZ is set to 1000 */
1203 tickCount = xTaskGetTickCount();
1204 return (double)tickCount / 1000;
1209 #include <sys/time.h>
1211 double current_time(int reset)
1217 gettimeofday(&tv, 0);
1219 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;