2011-07-27 00:27:22 +04:00
|
|
|
/* benchmark.c
|
|
|
|
*
|
2012-02-13 23:54:10 +04:00
|
|
|
* Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
|
2011-07-27 00:27:22 +04:00
|
|
|
*
|
|
|
|
* This file is part of CyaSSL.
|
|
|
|
*
|
|
|
|
* CyaSSL is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* CyaSSL is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
*/
|
2011-08-05 02:42:55 +04:00
|
|
|
|
2011-02-05 22:14:47 +03:00
|
|
|
/* CTaoCrypt benchmark */
|
|
|
|
|
2011-08-25 03:37:16 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2011-08-05 02:42:55 +04:00
|
|
|
|
2011-02-05 22:14:47 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2011-08-25 23:41:19 +04:00
|
|
|
#include <cyassl/ctaocrypt/des3.h>
|
|
|
|
#include <cyassl/ctaocrypt/arc4.h>
|
|
|
|
#include <cyassl/ctaocrypt/hc128.h>
|
|
|
|
#include <cyassl/ctaocrypt/rabbit.h>
|
|
|
|
#include <cyassl/ctaocrypt/aes.h>
|
|
|
|
#include <cyassl/ctaocrypt/md5.h>
|
|
|
|
#include <cyassl/ctaocrypt/sha.h>
|
|
|
|
#include <cyassl/ctaocrypt/sha256.h>
|
|
|
|
#include <cyassl/ctaocrypt/sha512.h>
|
|
|
|
#include <cyassl/ctaocrypt/rsa.h>
|
|
|
|
#include <cyassl/ctaocrypt/asn.h>
|
|
|
|
#include <cyassl/ctaocrypt/ripemd.h>
|
2011-12-23 02:59:57 +04:00
|
|
|
#include <cyassl/ctaocrypt/ecc.h>
|
2011-08-25 23:41:19 +04:00
|
|
|
|
|
|
|
#include <cyassl/ctaocrypt/dh.h>
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
/* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
|
|
|
|
#pragma warning(disable: 4996)
|
|
|
|
#endif
|
|
|
|
|
2012-09-20 10:38:41 +04:00
|
|
|
void bench_des(void);
|
|
|
|
void bench_arc4(void);
|
|
|
|
void bench_hc128(void);
|
|
|
|
void bench_rabbit(void);
|
2011-02-05 22:14:47 +03:00
|
|
|
void bench_aes(int);
|
2012-09-20 10:38:41 +04:00
|
|
|
void bench_aesgcm(void);
|
2013-01-11 04:38:52 +04:00
|
|
|
void bench_aesccm(void);
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-09-20 10:38:41 +04:00
|
|
|
void bench_md5(void);
|
|
|
|
void bench_sha(void);
|
|
|
|
void bench_sha256(void);
|
|
|
|
void bench_sha512(void);
|
|
|
|
void bench_ripemd(void);
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-09-20 10:38:41 +04:00
|
|
|
void bench_rsa(void);
|
|
|
|
void bench_rsaKeyGen(void);
|
|
|
|
void bench_dh(void);
|
2011-12-23 02:59:57 +04:00
|
|
|
#ifdef HAVE_ECC
|
2012-09-20 10:38:41 +04:00
|
|
|
void bench_eccKeyGen(void);
|
|
|
|
void bench_eccKeyAgree(void);
|
2011-12-23 02:59:57 +04:00
|
|
|
#endif
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-09-20 10:38:41 +04:00
|
|
|
double current_time(void);
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2012-09-20 10:38:41 +04:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
2011-02-05 22:14:47 +03:00
|
|
|
#ifndef NO_AES
|
|
|
|
bench_aes(0);
|
|
|
|
bench_aes(1);
|
2012-07-01 03:55:29 +04:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_AESGCM
|
|
|
|
bench_aesgcm();
|
2011-02-05 22:14:47 +03:00
|
|
|
#endif
|
2013-01-11 04:38:52 +04:00
|
|
|
#ifdef HAVE_AESCCM
|
|
|
|
bench_aesccm();
|
|
|
|
#endif
|
2012-12-01 03:45:43 +04:00
|
|
|
#ifndef NO_RC4
|
2011-02-05 22:14:47 +03:00
|
|
|
bench_arc4();
|
2012-12-01 03:45:43 +04:00
|
|
|
#endif
|
2011-09-29 00:19:05 +04:00
|
|
|
#ifdef HAVE_HC128
|
2011-02-05 22:14:47 +03:00
|
|
|
bench_hc128();
|
|
|
|
#endif
|
|
|
|
#ifndef NO_RABBIT
|
|
|
|
bench_rabbit();
|
|
|
|
#endif
|
|
|
|
#ifndef NO_DES3
|
|
|
|
bench_des();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
2012-11-27 04:28:22 +04:00
|
|
|
#ifndef NO_MD5
|
2011-02-05 22:14:47 +03:00
|
|
|
bench_md5();
|
2012-11-27 04:28:22 +04:00
|
|
|
#endif
|
2011-02-05 22:14:47 +03:00
|
|
|
bench_sha();
|
|
|
|
#ifndef NO_SHA256
|
|
|
|
bench_sha256();
|
|
|
|
#endif
|
|
|
|
#ifdef CYASSL_SHA512
|
|
|
|
bench_sha512();
|
|
|
|
#endif
|
|
|
|
#ifdef CYASSL_RIPEMD
|
|
|
|
bench_ripemd();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
printf("\n");
|
2012-10-30 02:39:42 +04:00
|
|
|
|
|
|
|
#ifndef NO_RSA
|
2011-02-05 22:14:47 +03:00
|
|
|
bench_rsa();
|
2012-10-30 02:39:42 +04:00
|
|
|
#endif
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
#ifndef NO_DH
|
|
|
|
bench_dh();
|
|
|
|
#endif
|
|
|
|
|
2012-10-30 02:39:42 +04:00
|
|
|
#if defined(CYASSL_KEY_GEN) && !defined(NO_RSA)
|
2011-02-05 22:14:47 +03:00
|
|
|
bench_rsaKeyGen();
|
|
|
|
#endif
|
|
|
|
|
2011-12-23 02:59:57 +04:00
|
|
|
#ifdef HAVE_ECC
|
|
|
|
bench_eccKeyGen();
|
|
|
|
bench_eccKeyAgree();
|
|
|
|
#endif
|
|
|
|
|
2011-02-05 22:14:47 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int megs = 5; /* how many megs to test (en/de)cryption */
|
|
|
|
const int times = 100; /* public key iterations */
|
|
|
|
|
|
|
|
const byte key[] =
|
|
|
|
{
|
|
|
|
0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
|
|
|
|
0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
|
|
|
|
0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
|
|
|
|
};
|
|
|
|
|
|
|
|
const byte iv[] =
|
|
|
|
{
|
|
|
|
0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
|
|
|
|
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
|
|
|
|
0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
byte plain [1024*1024];
|
|
|
|
byte cipher[1024*1024];
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef NO_AES
|
|
|
|
void bench_aes(int show)
|
|
|
|
{
|
|
|
|
Aes enc;
|
|
|
|
double start, total, persec;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
AesSetKey(&enc, key, 16, iv, AES_ENCRYPTION);
|
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for(i = 0; i < megs; i++)
|
|
|
|
AesCbcEncrypt(&enc, plain, cipher, sizeof(plain));
|
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
|
|
|
|
persec = 1 / total * megs;
|
|
|
|
|
|
|
|
if (show)
|
|
|
|
printf("AES %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
|
|
|
|
persec);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2012-07-01 03:55:29 +04:00
|
|
|
byte additional[13];
|
|
|
|
byte tag[16];
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_AESGCM
|
2012-09-20 10:38:41 +04:00
|
|
|
void bench_aesgcm(void)
|
2012-07-01 03:55:29 +04:00
|
|
|
{
|
|
|
|
Aes enc;
|
|
|
|
double start, total, persec;
|
|
|
|
int i;
|
|
|
|
|
2012-07-31 20:32:29 +04:00
|
|
|
AesGcmSetKey(&enc, key, 16, iv);
|
|
|
|
AesGcmSetExpIV(&enc, iv+4);
|
2012-07-01 03:55:29 +04:00
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for(i = 0; i < megs; i++)
|
|
|
|
AesGcmEncrypt(&enc, cipher, plain, sizeof(plain),
|
|
|
|
tag, 16, additional, 13);
|
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
|
|
|
|
persec = 1 / total * megs;
|
|
|
|
printf("AES-GCM %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
|
|
|
|
persec);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2013-01-11 04:38:52 +04:00
|
|
|
#ifdef HAVE_AESCCM
|
|
|
|
void bench_aesccm(void)
|
|
|
|
{
|
|
|
|
Aes enc;
|
|
|
|
double start, total, persec;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
AesCcmSetKey(&enc, key, 16, iv, 12);
|
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for(i = 0; i < megs; i++)
|
|
|
|
AesCcmEncrypt(&enc, cipher, plain, sizeof(plain),
|
|
|
|
tag, 16, additional, 13);
|
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
|
|
|
|
persec = 1 / total * megs;
|
|
|
|
printf("AES-CCM %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
|
|
|
|
persec);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-02-05 22:14:47 +03:00
|
|
|
#ifndef NO_DES3
|
2012-09-20 10:38:41 +04:00
|
|
|
void bench_des(void)
|
2011-02-05 22:14:47 +03:00
|
|
|
{
|
|
|
|
Des3 enc;
|
|
|
|
double start, total, persec;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
Des3_SetKey(&enc, key, iv, DES_ENCRYPTION);
|
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for(i = 0; i < megs; i++)
|
|
|
|
Des3_CbcEncrypt(&enc, plain, cipher, sizeof(plain));
|
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
|
|
|
|
persec = 1 / total * megs;
|
|
|
|
|
|
|
|
printf("3DES %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
|
|
|
|
persec);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2012-12-01 03:45:43 +04:00
|
|
|
#ifndef NO_RC4
|
2012-09-20 10:38:41 +04:00
|
|
|
void bench_arc4(void)
|
2011-02-05 22:14:47 +03:00
|
|
|
{
|
|
|
|
Arc4 enc;
|
|
|
|
double start, total, persec;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
Arc4SetKey(&enc, key, 16);
|
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for(i = 0; i < megs; i++)
|
|
|
|
Arc4Process(&enc, cipher, plain, sizeof(plain));
|
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
persec = 1 / total * megs;
|
|
|
|
|
|
|
|
printf("ARC4 %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
|
|
|
|
persec);
|
|
|
|
}
|
2012-12-01 03:45:43 +04:00
|
|
|
#endif
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
|
2011-09-29 00:19:05 +04:00
|
|
|
#ifdef HAVE_HC128
|
2012-09-20 10:38:41 +04:00
|
|
|
void bench_hc128(void)
|
2011-02-05 22:14:47 +03:00
|
|
|
{
|
|
|
|
HC128 enc;
|
|
|
|
double start, total, persec;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
Hc128_SetKey(&enc, key, iv);
|
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for(i = 0; i < megs; i++)
|
|
|
|
Hc128_Process(&enc, cipher, plain, sizeof(plain));
|
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
persec = 1 / total * megs;
|
|
|
|
|
|
|
|
printf("HC128 %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
|
|
|
|
persec);
|
|
|
|
}
|
2011-08-25 02:54:58 +04:00
|
|
|
#endif /* HAVE_HC128 */
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef NO_RABBIT
|
2012-09-20 10:38:41 +04:00
|
|
|
void bench_rabbit(void)
|
2011-02-05 22:14:47 +03:00
|
|
|
{
|
|
|
|
Rabbit enc;
|
|
|
|
double start, total, persec;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
RabbitSetKey(&enc, key, iv);
|
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for(i = 0; i < megs; i++)
|
|
|
|
RabbitProcess(&enc, cipher, plain, sizeof(plain));
|
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
persec = 1 / total * megs;
|
|
|
|
|
|
|
|
printf("RABBIT %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
|
|
|
|
persec);
|
|
|
|
}
|
|
|
|
#endif /* NO_RABBIT */
|
|
|
|
|
|
|
|
|
2012-11-27 04:28:22 +04:00
|
|
|
#ifndef NO_MD5
|
2012-09-20 10:38:41 +04:00
|
|
|
void bench_md5(void)
|
2011-02-05 22:14:47 +03:00
|
|
|
{
|
|
|
|
Md5 hash;
|
|
|
|
byte digest[MD5_DIGEST_SIZE];
|
|
|
|
double start, total, persec;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
InitMd5(&hash);
|
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for(i = 0; i < megs; i++)
|
|
|
|
Md5Update(&hash, plain, sizeof(plain));
|
|
|
|
|
|
|
|
Md5Final(&hash, digest);
|
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
persec = 1 / total * megs;
|
|
|
|
|
|
|
|
printf("MD5 %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
|
|
|
|
persec);
|
|
|
|
}
|
2012-11-27 04:28:22 +04:00
|
|
|
#endif /* NO_MD5 */
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
|
2012-09-20 10:38:41 +04:00
|
|
|
void bench_sha(void)
|
2011-02-05 22:14:47 +03:00
|
|
|
{
|
|
|
|
Sha hash;
|
|
|
|
byte digest[SHA_DIGEST_SIZE];
|
|
|
|
double start, total, persec;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
InitSha(&hash);
|
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for(i = 0; i < megs; i++)
|
|
|
|
ShaUpdate(&hash, plain, sizeof(plain));
|
|
|
|
|
|
|
|
ShaFinal(&hash, digest);
|
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
persec = 1 / total * megs;
|
|
|
|
|
|
|
|
printf("SHA %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
|
|
|
|
persec);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef NO_SHA256
|
2012-09-20 10:38:41 +04:00
|
|
|
void bench_sha256(void)
|
2011-02-05 22:14:47 +03:00
|
|
|
{
|
|
|
|
Sha256 hash;
|
|
|
|
byte digest[SHA256_DIGEST_SIZE];
|
|
|
|
double start, total, persec;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
InitSha256(&hash);
|
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for(i = 0; i < megs; i++)
|
|
|
|
Sha256Update(&hash, plain, sizeof(plain));
|
|
|
|
|
|
|
|
Sha256Final(&hash, digest);
|
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
persec = 1 / total * megs;
|
|
|
|
|
|
|
|
printf("SHA-256 %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
|
|
|
|
persec);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CYASSL_SHA512
|
2012-09-20 10:38:41 +04:00
|
|
|
void bench_sha512(void)
|
2011-02-05 22:14:47 +03:00
|
|
|
{
|
|
|
|
Sha512 hash;
|
|
|
|
byte digest[SHA512_DIGEST_SIZE];
|
|
|
|
double start, total, persec;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
InitSha512(&hash);
|
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for(i = 0; i < megs; i++)
|
|
|
|
Sha512Update(&hash, plain, sizeof(plain));
|
|
|
|
|
|
|
|
Sha512Final(&hash, digest);
|
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
persec = 1 / total * megs;
|
|
|
|
|
|
|
|
printf("SHA-512 %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
|
|
|
|
persec);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CYASSL_RIPEMD
|
2012-09-20 10:38:41 +04:00
|
|
|
void bench_ripemd(void)
|
2011-02-05 22:14:47 +03:00
|
|
|
{
|
|
|
|
RipeMd hash;
|
|
|
|
byte digest[RIPEMD_DIGEST_SIZE];
|
|
|
|
double start, total, persec;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
InitRipeMd(&hash);
|
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for(i = 0; i < megs; i++)
|
|
|
|
RipeMdUpdate(&hash, plain, sizeof(plain));
|
|
|
|
|
|
|
|
RipeMdFinal(&hash, digest);
|
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
persec = 1 / total * megs;
|
|
|
|
|
|
|
|
printf("RIPEMD %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
|
|
|
|
persec);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2012-10-30 02:39:42 +04:00
|
|
|
#if !defined(NO_RSA) || !defined(NO_DH) \
|
|
|
|
|| defined(CYASSL_KEYGEN) || defined(HAVE_ECC)
|
2011-02-05 22:14:47 +03:00
|
|
|
RNG rng;
|
2012-10-30 02:39:42 +04:00
|
|
|
#endif
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-10-30 02:39:42 +04:00
|
|
|
#ifndef NO_RSA
|
2012-09-20 10:38:41 +04:00
|
|
|
void bench_rsa(void)
|
2011-02-05 22:14:47 +03:00
|
|
|
{
|
|
|
|
int i;
|
2012-10-31 04:35:12 +04:00
|
|
|
byte tmp[3072];
|
2011-02-05 22:14:47 +03:00
|
|
|
size_t bytes;
|
|
|
|
word32 idx = 0;
|
|
|
|
|
|
|
|
byte message[] = "Everyone gets Friday off.";
|
|
|
|
byte* output;
|
2013-01-08 02:08:56 +04:00
|
|
|
byte enc[512]; /* for up to 4096 bit */
|
2011-02-05 22:14:47 +03:00
|
|
|
const int len = (int)strlen((char*)message);
|
|
|
|
double start, total, each, milliEach;
|
|
|
|
|
2012-09-21 02:39:15 +04:00
|
|
|
RsaKey rsaKey;
|
2011-10-24 22:41:25 +04:00
|
|
|
FILE* file = fopen("./certs/rsa2048.der", "rb");
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
if (!file) {
|
2011-10-24 22:41:25 +04:00
|
|
|
printf("can't find ./certs/rsa2048.der, "
|
2011-08-25 02:54:58 +04:00
|
|
|
"Please run from CyaSSL home dir\n");
|
2011-02-05 22:14:47 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
InitRng(&rng);
|
|
|
|
bytes = fread(tmp, 1, sizeof(tmp), file);
|
2012-09-21 02:39:15 +04:00
|
|
|
InitRsaKey(&rsaKey, 0);
|
|
|
|
bytes = RsaPrivateKeyDecode(tmp, &idx, &rsaKey, (word32)bytes);
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for (i = 0; i < times; i++)
|
2012-09-21 02:39:15 +04:00
|
|
|
bytes = RsaPublicEncrypt(message,len,enc,sizeof(enc), &rsaKey, &rng);
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
each = total / times; /* per second */
|
|
|
|
milliEach = each * 1000; /* milliseconds */
|
|
|
|
|
2011-10-24 22:41:25 +04:00
|
|
|
printf("RSA 2048 encryption took %6.2f milliseconds, avg over %d"
|
2011-02-05 22:14:47 +03:00
|
|
|
" iterations\n", milliEach, times);
|
|
|
|
|
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for (i = 0; i < times; i++)
|
2012-09-21 02:39:15 +04:00
|
|
|
RsaPrivateDecryptInline(enc, (word32)bytes, &output, &rsaKey);
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
each = total / times; /* per second */
|
|
|
|
milliEach = each * 1000; /* milliseconds */
|
|
|
|
|
2011-10-24 22:41:25 +04:00
|
|
|
printf("RSA 2048 decryption took %6.2f milliseconds, avg over %d"
|
2011-02-05 22:14:47 +03:00
|
|
|
" iterations\n", milliEach, times);
|
|
|
|
|
|
|
|
fclose(file);
|
2012-09-21 02:39:15 +04:00
|
|
|
FreeRsaKey(&rsaKey);
|
2011-02-05 22:14:47 +03:00
|
|
|
}
|
2012-10-30 02:39:42 +04:00
|
|
|
#endif
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef NO_DH
|
2012-09-20 10:38:41 +04:00
|
|
|
void bench_dh(void)
|
2011-02-05 22:14:47 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
byte tmp[1024];
|
|
|
|
size_t bytes;
|
|
|
|
word32 idx = 0, pubSz, privSz, pubSz2, privSz2, agreeSz;
|
|
|
|
|
2011-10-24 22:41:25 +04:00
|
|
|
byte pub[256]; /* for 2048 bit */
|
|
|
|
byte priv[256]; /* for 2048 bit */
|
|
|
|
byte pub2[256]; /* for 2048 bit */
|
|
|
|
byte priv2[256]; /* for 2048 bit */
|
|
|
|
byte agree[256]; /* for 2048 bit */
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
double start, total, each, milliEach;
|
2012-09-21 02:39:15 +04:00
|
|
|
DhKey dhKey;
|
2011-10-24 22:41:25 +04:00
|
|
|
FILE* file = fopen("./certs/dh2048.der", "rb");
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
if (!file) {
|
2011-10-24 22:41:25 +04:00
|
|
|
printf("can't find ./certs/dh2048.der, "
|
2011-08-25 02:54:58 +04:00
|
|
|
"Please run from CyaSSL home dir\n");
|
2011-02-05 22:14:47 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-24 22:41:25 +04:00
|
|
|
bytes = fread(tmp, 1, sizeof(tmp), file);
|
2012-09-21 02:39:15 +04:00
|
|
|
InitDhKey(&dhKey);
|
|
|
|
bytes = DhKeyDecode(tmp, &idx, &dhKey, (word32)bytes);
|
2012-12-13 06:03:32 +04:00
|
|
|
if (bytes != 0) {
|
|
|
|
printf("dhekydecode failed, can't benchmark\n");
|
|
|
|
return;
|
|
|
|
}
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for (i = 0; i < times; i++)
|
2012-09-21 02:39:15 +04:00
|
|
|
DhGenerateKeyPair(&dhKey, &rng, priv, &privSz, pub, &pubSz);
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
each = total / times; /* per second */
|
|
|
|
milliEach = each * 1000; /* milliseconds */
|
|
|
|
|
2011-10-24 22:41:25 +04:00
|
|
|
printf("DH 2048 key generation %6.2f milliseconds, avg over %d"
|
2011-02-05 22:14:47 +03:00
|
|
|
" iterations\n", milliEach, times);
|
|
|
|
|
2012-09-21 02:39:15 +04:00
|
|
|
DhGenerateKeyPair(&dhKey, &rng, priv2, &privSz2, pub2, &pubSz2);
|
2011-02-05 22:14:47 +03:00
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for (i = 0; i < times; i++)
|
2012-09-21 02:39:15 +04:00
|
|
|
DhAgree(&dhKey, agree, &agreeSz, priv, privSz, pub2, pubSz2);
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
each = total / times; /* per second */
|
|
|
|
milliEach = each * 1000; /* milliseconds */
|
|
|
|
|
2011-10-24 22:41:25 +04:00
|
|
|
printf("DH 2048 key agreement %6.2f milliseconds, avg over %d"
|
2011-02-05 22:14:47 +03:00
|
|
|
" iterations\n", milliEach, times);
|
|
|
|
|
|
|
|
fclose(file);
|
2012-09-21 02:39:15 +04:00
|
|
|
FreeDhKey(&dhKey);
|
2011-02-05 22:14:47 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-30 02:39:42 +04:00
|
|
|
#if defined(CYASSL_KEY_GEN) && !defined(NO_RSA)
|
2012-09-20 10:38:41 +04:00
|
|
|
void bench_rsaKeyGen(void)
|
2011-02-05 22:14:47 +03:00
|
|
|
{
|
|
|
|
RsaKey genKey;
|
|
|
|
double start, total, each, milliEach;
|
|
|
|
int i;
|
|
|
|
const int genTimes = 5;
|
|
|
|
|
|
|
|
/* 1024 bit */
|
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for(i = 0; i < genTimes; i++) {
|
|
|
|
InitRsaKey(&genKey, 0);
|
|
|
|
MakeRsaKey(&genKey, 1024, 65537, &rng);
|
|
|
|
FreeRsaKey(&genKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
each = total / genTimes; /* per second */
|
|
|
|
milliEach = each * 1000; /* millisconds */
|
|
|
|
printf("\n");
|
|
|
|
printf("RSA 1024 key generation %6.2f milliseconds, avg over %d"
|
|
|
|
" iterations\n", milliEach, genTimes);
|
|
|
|
|
|
|
|
/* 2048 bit */
|
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for(i = 0; i < genTimes; i++) {
|
|
|
|
InitRsaKey(&genKey, 0);
|
|
|
|
MakeRsaKey(&genKey, 2048, 65537, &rng);
|
|
|
|
FreeRsaKey(&genKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
each = total / genTimes; /* per second */
|
|
|
|
milliEach = each * 1000; /* millisconds */
|
|
|
|
printf("RSA 2048 key generation %6.2f milliseconds, avg over %d"
|
|
|
|
" iterations\n", milliEach, genTimes);
|
|
|
|
}
|
|
|
|
#endif /* CYASSL_KEY_GEN */
|
|
|
|
|
2011-12-23 02:59:57 +04:00
|
|
|
#ifdef HAVE_ECC
|
2012-09-20 10:38:41 +04:00
|
|
|
void bench_eccKeyGen(void)
|
2011-12-23 02:59:57 +04:00
|
|
|
{
|
|
|
|
ecc_key genKey;
|
|
|
|
double start, total, each, milliEach;
|
|
|
|
int i;
|
|
|
|
const int genTimes = 5;
|
|
|
|
|
|
|
|
/* 256 bit */
|
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for(i = 0; i < genTimes; i++) {
|
2012-10-10 01:04:27 +04:00
|
|
|
ecc_make_key(&rng, 32, &genKey);
|
2011-12-23 02:59:57 +04:00
|
|
|
ecc_free(&genKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
each = total / genTimes; /* per second */
|
|
|
|
milliEach = each * 1000; /* millisconds */
|
|
|
|
printf("\n");
|
|
|
|
printf("ECC 256 key generation %6.2f milliseconds, avg over %d"
|
|
|
|
" iterations\n", milliEach, genTimes);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-20 10:38:41 +04:00
|
|
|
void bench_eccKeyAgree(void)
|
2011-12-23 02:59:57 +04:00
|
|
|
{
|
|
|
|
ecc_key genKey, genKey2;
|
|
|
|
double start, total, each, milliEach;
|
|
|
|
int i;
|
|
|
|
const int agreeTimes = 5;
|
|
|
|
byte shared[1024];
|
|
|
|
byte sig[1024];
|
|
|
|
byte digest[32];
|
|
|
|
word32 x;
|
|
|
|
|
|
|
|
ecc_make_key(&rng, 32, &genKey);
|
|
|
|
ecc_make_key(&rng, 32, &genKey2);
|
|
|
|
|
|
|
|
/* 256 bit */
|
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for(i = 0; i < agreeTimes; i++) {
|
|
|
|
x = sizeof(shared);
|
|
|
|
ecc_shared_secret(&genKey, &genKey2, shared, &x);
|
|
|
|
}
|
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
each = total / agreeTimes; /* per second */
|
|
|
|
milliEach = each * 1000; /* millisconds */
|
|
|
|
printf("EC-DHE key agreement %6.2f milliseconds, avg over %d"
|
|
|
|
" iterations\n", milliEach, agreeTimes);
|
|
|
|
|
|
|
|
/* make dummy digest */
|
2012-10-10 01:04:27 +04:00
|
|
|
for (i = 0; i < (int)sizeof(digest); i++)
|
2011-12-23 02:59:57 +04:00
|
|
|
digest[i] = i;
|
|
|
|
|
|
|
|
|
|
|
|
start = current_time();
|
|
|
|
|
|
|
|
for(i = 0; i < agreeTimes; i++) {
|
|
|
|
x = sizeof(sig);
|
|
|
|
ecc_sign_hash(digest, sizeof(digest), sig, &x, &rng, &genKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
total = current_time() - start;
|
|
|
|
each = total / agreeTimes; /* per second */
|
|
|
|
milliEach = each * 1000; /* millisconds */
|
|
|
|
printf("EC-DSA sign time %6.2f milliseconds, avg over %d"
|
|
|
|
" iterations\n", milliEach, agreeTimes);
|
|
|
|
|
|
|
|
ecc_free(&genKey2);
|
|
|
|
ecc_free(&genKey);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_ECC */
|
|
|
|
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
double current_time()
|
|
|
|
{
|
|
|
|
static int init = 0;
|
|
|
|
static LARGE_INTEGER freq;
|
|
|
|
|
|
|
|
LARGE_INTEGER count;
|
|
|
|
|
|
|
|
if (!init) {
|
|
|
|
QueryPerformanceFrequency(&freq);
|
|
|
|
init = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryPerformanceCounter(&count);
|
|
|
|
|
|
|
|
return (double)count.QuadPart / freq.QuadPart;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2012-09-20 10:38:41 +04:00
|
|
|
double current_time(void)
|
2011-02-05 22:14:47 +03:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, 0);
|
|
|
|
|
|
|
|
return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|