benchmark-openssl.cc revision 3af4c543
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());
183af4c543SShuo Chen  SSL_CTX_set_options(ctx, SSL_OP_NO_COMPRESSION);
19a98d478eSShuo Chen
20a98d478eSShuo Chen  EC_KEY* ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
21a98d478eSShuo Chen  SSL_CTX_set_options(ctx, SSL_OP_SINGLE_ECDH_USE);
22132b87adSShuo Chen  // if (argc > 3)
23132b87adSShuo Chen  SSL_CTX_set_tmp_ecdh(ctx, ecdh);
24a98d478eSShuo Chen  EC_KEY_free(ecdh);
25a98d478eSShuo Chen
26b140ba61SShuo Chen  const char* CertFile = "server.pem";  // argv[1];
27b140ba61SShuo Chen  const char* KeyFile = "server.pem";  // argv[2];
28a98d478eSShuo Chen  SSL_CTX_use_certificate_file(ctx, CertFile, SSL_FILETYPE_PEM);
29a98d478eSShuo Chen  SSL_CTX_use_PrivateKey_file(ctx, KeyFile, SSL_FILETYPE_PEM);
30a98d478eSShuo Chen  if (!SSL_CTX_check_private_key(ctx))
31a98d478eSShuo Chen    abort();
32a98d478eSShuo Chen
33b140ba61SShuo Chen  SSL_CTX* ctx_client = SSL_CTX_new(TLSv1_2_client_method());
34a98d478eSShuo Chen
35a98d478eSShuo Chen  double start = now();
36a98d478eSShuo Chen  const int N = 1000;
37a98d478eSShuo Chen  SSL *ssl, *ssl_client;
389acb42f4SShuo Chen  Timer tc, ts;
39a98d478eSShuo Chen  for (int i = 0; i < N; ++i)
40a98d478eSShuo Chen  {
41132b87adSShuo Chen    BIO *client, *server;
42132b87adSShuo Chen    BIO_new_bio_pair(&client, 0, &server, 0);
43132b87adSShuo Chen
44a98d478eSShuo Chen    ssl = SSL_new (ctx);
45a98d478eSShuo Chen    ssl_client = SSL_new (ctx_client);
46132b87adSShuo Chen    SSL_set_bio(ssl, server, server);
47132b87adSShuo Chen    SSL_set_bio(ssl_client, client, client);
48a98d478eSShuo Chen
499acb42f4SShuo Chen    tc.start();
50a98d478eSShuo Chen    int ret = SSL_connect(ssl_client);
519acb42f4SShuo Chen    tc.stop();
52a98d478eSShuo Chen    //printf("%d %d\n", ret, BIO_retry_type(&server));
539acb42f4SShuo Chen    ts.start();
54a98d478eSShuo Chen    int ret2 = SSL_accept(ssl);
559acb42f4SShuo Chen    ts.stop();
56a98d478eSShuo Chen    //printf("%d %d\n", ret2, BIO_retry_type(&client));
57a98d478eSShuo Chen
58a98d478eSShuo Chen    while (true)
59a98d478eSShuo Chen    {
609acb42f4SShuo Chen      tc.start();
619acb42f4SShuo Chen      ret = SSL_do_handshake(ssl_client);
629acb42f4SShuo Chen      tc.stop();
63a98d478eSShuo Chen      //printf("client handshake %d %d\n", ret, BIO_retry_type(&server));
649acb42f4SShuo Chen      ts.start();
659acb42f4SShuo Chen      ret2 = SSL_do_handshake(ssl);
669acb42f4SShuo Chen      ts.stop();
67a98d478eSShuo Chen      //printf("server handshake %d %d\n", ret2, BIO_retry_type(&client));
68a98d478eSShuo Chen      //if (ret == -1 && BIO_retry_type(&server) == 0)
69a98d478eSShuo Chen      //  break;
70a98d478eSShuo Chen      //if (ret2 == -1 && BIO_retry_type(&client) == 0)
71a98d478eSShuo Chen      //  break;
72a98d478eSShuo Chen      if (ret == 1 && ret2 == 1)
73a98d478eSShuo Chen        break;
74a98d478eSShuo Chen    }
75a98d478eSShuo Chen
76a98d478eSShuo Chen    //printf ("SSL connection using %s %s\n", SSL_get_version(ssl), SSL_get_cipher (ssl));
77a98d478eSShuo Chen    if (i == 0)
78a98d478eSShuo Chen      printf ("SSL connection using %s %s\n", SSL_get_version(ssl_client), SSL_get_cipher (ssl_client));
79a98d478eSShuo Chen    //SSL_clear(ssl);
80a98d478eSShuo Chen    //SSL_clear(ssl_client);
81a98d478eSShuo Chen    if (i != N-1)
82a98d478eSShuo Chen    {
83a98d478eSShuo Chen      SSL_free (ssl);
84a98d478eSShuo Chen      SSL_free (ssl_client);
85a98d478eSShuo Chen    }
86a98d478eSShuo Chen  }
87a98d478eSShuo Chen  double elapsed = now() - start;
88a98d478eSShuo Chen  printf("%.2fs %.1f handshakes/s\n", elapsed, N / elapsed);
899acb42f4SShuo Chen  printf("client %.3f %.1f\n", tc.seconds(), N / tc.seconds());
909acb42f4SShuo Chen  printf("server %.3f %.1f\n", ts.seconds(), N / ts.seconds());
919acb42f4SShuo Chen  printf("server/client %.2f\n", ts.seconds() / tc.seconds());
929acb42f4SShuo Chen
93a98d478eSShuo Chen
94a98d478eSShuo Chen  double start2 = now();
95a98d478eSShuo Chen  const int M = 300;
96a98d478eSShuo Chen  char buf[1024] = { 0 };
97a98d478eSShuo Chen  for (int i = 0; i < M*1024; ++i)
98a98d478eSShuo Chen  {
99132b87adSShuo Chen    int nw = SSL_write(ssl_client, buf, sizeof buf);
100132b87adSShuo Chen    if (nw != sizeof buf)
101132b87adSShuo Chen    {
102132b87adSShuo Chen      printf("nw = %d\n", nw);
103132b87adSShuo Chen    }
104132b87adSShuo Chen    int nr = SSL_read(ssl, buf, sizeof buf);
105132b87adSShuo Chen    if (nr != sizeof buf)
106a98d478eSShuo Chen    {
107132b87adSShuo Chen      printf("nr = %d\n", nr);
108a98d478eSShuo Chen    }
109a98d478eSShuo Chen  }
110a98d478eSShuo Chen  elapsed = now() - start2;
111a98d478eSShuo Chen  printf("%.2f %.1f MiB/s\n", elapsed, M / elapsed);
112a98d478eSShuo Chen  SSL_free (ssl);
113a98d478eSShuo Chen  SSL_free (ssl_client);
114a98d478eSShuo Chen
115a98d478eSShuo Chen  SSL_CTX_free (ctx);
116a98d478eSShuo Chen  SSL_CTX_free (ctx_client);
117a98d478eSShuo Chen}
118