benchmark-openssl.cc revision 132b87ad
1a98d478eSShuo Chen#include <openssl/aes.h> 2a98d478eSShuo Chen#include <openssl/conf.h> 3a98d478eSShuo Chen#include <openssl/err.h> 4a98d478eSShuo Chen#include <openssl/ssl.h> 5a98d478eSShuo Chen 6a98d478eSShuo Chen#include <stdio.h> 7a98d478eSShuo Chen 89acb42f4SShuo Chen#include "timer.h" 99acb42f4SShuo Chen 10a98d478eSShuo Chenint main(int argc, char* argv[]) 11a98d478eSShuo Chen{ 12a98d478eSShuo Chen SSL_load_error_strings(); 13a98d478eSShuo Chen // ERR_load_BIO_strings(); 14a98d478eSShuo Chen SSL_library_init(); 15a98d478eSShuo Chen OPENSSL_config(NULL); 16a98d478eSShuo Chen 17b140ba61SShuo Chen SSL_CTX* ctx = SSL_CTX_new(TLSv1_2_server_method()); 18a98d478eSShuo Chen 19a98d478eSShuo Chen EC_KEY* ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); 20a98d478eSShuo Chen SSL_CTX_set_options(ctx, SSL_OP_SINGLE_ECDH_USE); 21132b87adSShuo Chen // if (argc > 3) 22132b87adSShuo Chen SSL_CTX_set_tmp_ecdh(ctx, ecdh); 23a98d478eSShuo Chen EC_KEY_free(ecdh); 24a98d478eSShuo Chen 25b140ba61SShuo Chen const char* CertFile = "server.pem"; // argv[1]; 26b140ba61SShuo Chen const char* KeyFile = "server.pem"; // argv[2]; 27a98d478eSShuo Chen SSL_CTX_use_certificate_file(ctx, CertFile, SSL_FILETYPE_PEM); 28a98d478eSShuo Chen SSL_CTX_use_PrivateKey_file(ctx, KeyFile, SSL_FILETYPE_PEM); 29a98d478eSShuo Chen if (!SSL_CTX_check_private_key(ctx)) 30a98d478eSShuo Chen abort(); 31a98d478eSShuo Chen 32b140ba61SShuo Chen SSL_CTX* ctx_client = SSL_CTX_new(TLSv1_2_client_method()); 33a98d478eSShuo Chen 34a98d478eSShuo Chen double start = now(); 35a98d478eSShuo Chen const int N = 1000; 36a98d478eSShuo Chen SSL *ssl, *ssl_client; 379acb42f4SShuo Chen Timer tc, ts; 38a98d478eSShuo Chen for (int i = 0; i < N; ++i) 39a98d478eSShuo Chen { 40132b87adSShuo Chen BIO *client, *server; 41132b87adSShuo Chen BIO_new_bio_pair(&client, 0, &server, 0); 42132b87adSShuo Chen 43a98d478eSShuo Chen ssl = SSL_new (ctx); 44a98d478eSShuo Chen ssl_client = SSL_new (ctx_client); 45132b87adSShuo Chen SSL_set_bio(ssl, server, server); 46132b87adSShuo Chen SSL_set_bio(ssl_client, client, client); 47a98d478eSShuo Chen 489acb42f4SShuo Chen tc.start(); 49a98d478eSShuo Chen int ret = SSL_connect(ssl_client); 509acb42f4SShuo Chen tc.stop(); 51a98d478eSShuo Chen //printf("%d %d\n", ret, BIO_retry_type(&server)); 529acb42f4SShuo Chen ts.start(); 53a98d478eSShuo Chen int ret2 = SSL_accept(ssl); 549acb42f4SShuo Chen ts.stop(); 55a98d478eSShuo Chen //printf("%d %d\n", ret2, BIO_retry_type(&client)); 56a98d478eSShuo Chen 57a98d478eSShuo Chen while (true) 58a98d478eSShuo Chen { 599acb42f4SShuo Chen tc.start(); 609acb42f4SShuo Chen ret = SSL_do_handshake(ssl_client); 619acb42f4SShuo Chen tc.stop(); 62a98d478eSShuo Chen //printf("client handshake %d %d\n", ret, BIO_retry_type(&server)); 639acb42f4SShuo Chen ts.start(); 649acb42f4SShuo Chen ret2 = SSL_do_handshake(ssl); 659acb42f4SShuo Chen ts.stop(); 66a98d478eSShuo Chen //printf("server handshake %d %d\n", ret2, BIO_retry_type(&client)); 67a98d478eSShuo Chen //if (ret == -1 && BIO_retry_type(&server) == 0) 68a98d478eSShuo Chen // break; 69a98d478eSShuo Chen //if (ret2 == -1 && BIO_retry_type(&client) == 0) 70a98d478eSShuo Chen // break; 71a98d478eSShuo Chen if (ret == 1 && ret2 == 1) 72a98d478eSShuo Chen break; 73a98d478eSShuo Chen } 74a98d478eSShuo Chen 75a98d478eSShuo Chen //printf ("SSL connection using %s %s\n", SSL_get_version(ssl), SSL_get_cipher (ssl)); 76a98d478eSShuo Chen if (i == 0) 77a98d478eSShuo Chen printf ("SSL connection using %s %s\n", SSL_get_version(ssl_client), SSL_get_cipher (ssl_client)); 78a98d478eSShuo Chen //SSL_clear(ssl); 79a98d478eSShuo Chen //SSL_clear(ssl_client); 80a98d478eSShuo Chen if (i != N-1) 81a98d478eSShuo Chen { 82a98d478eSShuo Chen SSL_free (ssl); 83a98d478eSShuo Chen SSL_free (ssl_client); 84a98d478eSShuo Chen } 85a98d478eSShuo Chen } 86a98d478eSShuo Chen double elapsed = now() - start; 87a98d478eSShuo Chen printf("%.2fs %.1f handshakes/s\n", elapsed, N / elapsed); 889acb42f4SShuo Chen printf("client %.3f %.1f\n", tc.seconds(), N / tc.seconds()); 899acb42f4SShuo Chen printf("server %.3f %.1f\n", ts.seconds(), N / ts.seconds()); 909acb42f4SShuo Chen printf("server/client %.2f\n", ts.seconds() / tc.seconds()); 919acb42f4SShuo Chen 92a98d478eSShuo Chen 93a98d478eSShuo Chen double start2 = now(); 94a98d478eSShuo Chen const int M = 300; 95a98d478eSShuo Chen char buf[1024] = { 0 }; 96a98d478eSShuo Chen for (int i = 0; i < M*1024; ++i) 97a98d478eSShuo Chen { 98132b87adSShuo Chen int nw = SSL_write(ssl_client, buf, sizeof buf); 99132b87adSShuo Chen if (nw != sizeof buf) 100132b87adSShuo Chen { 101132b87adSShuo Chen printf("nw = %d\n", nw); 102132b87adSShuo Chen } 103132b87adSShuo Chen int nr = SSL_read(ssl, buf, sizeof buf); 104132b87adSShuo Chen if (nr != sizeof buf) 105a98d478eSShuo Chen { 106132b87adSShuo Chen printf("nr = %d\n", nr); 107a98d478eSShuo Chen } 108a98d478eSShuo Chen } 109a98d478eSShuo Chen elapsed = now() - start2; 110a98d478eSShuo Chen printf("%.2f %.1f MiB/s\n", elapsed, M / elapsed); 111a98d478eSShuo Chen SSL_free (ssl); 112a98d478eSShuo Chen SSL_free (ssl_client); 113a98d478eSShuo Chen 114a98d478eSShuo Chen SSL_CTX_free (ctx); 115a98d478eSShuo Chen SSL_CTX_free (ctx_client); 116a98d478eSShuo Chen} 117