footprint-openssl.cc revision 116e48de
1116e48deSShuo Chen#include <openssl/aes.h>
2116e48deSShuo Chen#include <openssl/conf.h>
3116e48deSShuo Chen#include <openssl/err.h>
4116e48deSShuo Chen#include <openssl/ssl.h>
5116e48deSShuo Chen
6116e48deSShuo Chen#include <malloc.h>
7116e48deSShuo Chen#include <mcheck.h>
8116e48deSShuo Chen#include <stdio.h>
9116e48deSShuo Chen
10116e48deSShuo Chen#include "timer.h"
11116e48deSShuo Chen
12116e48deSShuo Chenvoid (*__MALLOC_HOOK_VOLATILE old_free_hook) (void *__ptr, const void *);
13116e48deSShuo Chenvoid *(*__MALLOC_HOOK_VOLATILE old_malloc_hook)(size_t __size, const void *);
14116e48deSShuo Chen
15116e48deSShuo Chenvoid my_free_hook (void*, const void *);
16116e48deSShuo Chen
17116e48deSShuo Chenvoid* my_malloc_hook(size_t size, const void* caller)
18116e48deSShuo Chen{
19116e48deSShuo Chen  void *result;
20116e48deSShuo Chen  /* Restore all old hooks */
21116e48deSShuo Chen  __malloc_hook = old_malloc_hook;
22116e48deSShuo Chen  __free_hook = old_free_hook;
23116e48deSShuo Chen  /* Call recursively */
24116e48deSShuo Chen  result = malloc (size);
25116e48deSShuo Chen  /* Save underlying hooks */
26116e48deSShuo Chen  old_malloc_hook = __malloc_hook;
27116e48deSShuo Chen  old_free_hook = __free_hook;
28116e48deSShuo Chen  /* printf might call malloc, so protect it too. */
29116e48deSShuo Chen  printf ("%p malloc (%u) returns %p\n", caller, (unsigned int) size, result);
30116e48deSShuo Chen  /* Restore our own hooks */
31116e48deSShuo Chen  __malloc_hook = my_malloc_hook;
32116e48deSShuo Chen  __free_hook = my_free_hook;
33116e48deSShuo Chen  return result;
34116e48deSShuo Chen}
35116e48deSShuo Chen
36116e48deSShuo Chenvoid my_free_hook (void *ptr, const void *caller)
37116e48deSShuo Chen{
38116e48deSShuo Chen  if (!ptr) return;
39116e48deSShuo Chen  /* Restore all old hooks */
40116e48deSShuo Chen  __malloc_hook = old_malloc_hook;
41116e48deSShuo Chen  __free_hook = old_free_hook;
42116e48deSShuo Chen  /* Call recursively */
43116e48deSShuo Chen  free (ptr);
44116e48deSShuo Chen  /* Save underlying hooks */
45116e48deSShuo Chen  old_malloc_hook = __malloc_hook;
46116e48deSShuo Chen  old_free_hook = __free_hook;
47116e48deSShuo Chen  /* printf might call free, so protect it too. */
48116e48deSShuo Chen  printf ("freed %p\n", ptr);
49116e48deSShuo Chen  /* Restore our own hooks */
50116e48deSShuo Chen  __malloc_hook = my_malloc_hook;
51116e48deSShuo Chen  __free_hook = my_free_hook;
52116e48deSShuo Chen}
53116e48deSShuo Chen
54116e48deSShuo Chenvoid init_hook()
55116e48deSShuo Chen{
56116e48deSShuo Chen  old_malloc_hook = __malloc_hook;
57116e48deSShuo Chen  old_free_hook = __free_hook;
58116e48deSShuo Chen  __malloc_hook = my_malloc_hook;
59116e48deSShuo Chen  __free_hook = my_free_hook;
60116e48deSShuo Chen}
61116e48deSShuo Chen
62116e48deSShuo Chenint main(int argc, char* argv[])
63116e48deSShuo Chen{
64116e48deSShuo Chen  SSL_load_error_strings();
65116e48deSShuo Chen  ERR_load_BIO_strings();
66116e48deSShuo Chen  SSL_library_init();
67116e48deSShuo Chen  OPENSSL_config(NULL);
68116e48deSShuo Chen
69116e48deSShuo Chen  SSL_CTX* ctx = SSL_CTX_new(TLSv1_2_server_method());
70116e48deSShuo Chen
71116e48deSShuo Chen  EC_KEY* ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
72116e48deSShuo Chen  SSL_CTX_set_options(ctx, SSL_OP_SINGLE_ECDH_USE);
73116e48deSShuo Chen  SSL_CTX_set_tmp_ecdh(ctx, ecdh);
74116e48deSShuo Chen  EC_KEY_free(ecdh);
75116e48deSShuo Chen
76116e48deSShuo Chen  const char* CertFile = "server.pem";  // argv[1];
77116e48deSShuo Chen  const char* KeyFile = "server.pem";  // argv[2];
78116e48deSShuo Chen  SSL_CTX_use_certificate_file(ctx, CertFile, SSL_FILETYPE_PEM);
79116e48deSShuo Chen  SSL_CTX_use_PrivateKey_file(ctx, KeyFile, SSL_FILETYPE_PEM);
80116e48deSShuo Chen  if (!SSL_CTX_check_private_key(ctx))
81116e48deSShuo Chen    abort();
82116e48deSShuo Chen
83116e48deSShuo Chen  SSL_CTX* ctx_client = SSL_CTX_new(TLSv1_2_client_method());
84116e48deSShuo Chen
85116e48deSShuo Chen  init_hook();
86116e48deSShuo Chen
87116e48deSShuo Chen  const int N = 3;
88116e48deSShuo Chen  SSL *ssl, *ssl_client;
89116e48deSShuo Chen  for (int i = 0; i < N; ++i)
90116e48deSShuo Chen  {
91116e48deSShuo Chen    printf("=============================================== BIO_new_bio_pair %d\n", i);
92116e48deSShuo Chen    BIO *client, *server;
93116e48deSShuo Chen    BIO_new_bio_pair(&client, 0, &server, 0);
94116e48deSShuo Chen
95116e48deSShuo Chen    printf("=============================================== SSL_new server %d\n", i);
96116e48deSShuo Chen    ssl = SSL_new (ctx);
97116e48deSShuo Chen    printf("=============================================== SSL_new client %d\n", i);
98116e48deSShuo Chen    ssl_client = SSL_new (ctx_client);
99116e48deSShuo Chen    SSL_set_bio(ssl, server, server);
100116e48deSShuo Chen    SSL_set_bio(ssl_client, client, client);
101116e48deSShuo Chen
102116e48deSShuo Chen    printf("=============================================== SSL_connect client %d\n", i);
103116e48deSShuo Chen    int ret = SSL_connect(ssl_client);
104116e48deSShuo Chen    printf("=============================================== SSL_accept server %d\n", i);
105116e48deSShuo Chen    int ret2 = SSL_accept(ssl);
106116e48deSShuo Chen
107116e48deSShuo Chen    while (true)
108116e48deSShuo Chen    {
109116e48deSShuo Chen      printf("=============================================== SSL_handshake client %d\n", i);
110116e48deSShuo Chen      ret = SSL_do_handshake(ssl_client);
111116e48deSShuo Chen      printf("=============================================== SSL_handshake server %d\n", i);
112116e48deSShuo Chen      ret2 = SSL_do_handshake(ssl);
113116e48deSShuo Chen      if (ret == 1 && ret2 == 1)
114116e48deSShuo Chen        break;
115116e48deSShuo Chen    }
116116e48deSShuo Chen
117116e48deSShuo Chen    if (i == 0)
118116e48deSShuo Chen      printf ("SSL connection using %s %s\n", SSL_get_version(ssl_client), SSL_get_cipher (ssl_client));
119116e48deSShuo Chen    if (i != N-1)
120116e48deSShuo Chen    {
121116e48deSShuo Chen      printf("=============================================== SSL_free server %d\n", i);
122116e48deSShuo Chen      SSL_free (ssl);
123116e48deSShuo Chen      printf("=============================================== SSL_free client %d\n", i);
124116e48deSShuo Chen      SSL_free (ssl_client);
125116e48deSShuo Chen    }
126116e48deSShuo Chen  }
127116e48deSShuo Chen
128116e48deSShuo Chen  printf("=============================================== data \n");
129116e48deSShuo Chen
130116e48deSShuo Chen  double start2 = now();
131116e48deSShuo Chen  const int M = 300;
132116e48deSShuo Chen  char buf[1024] = { 0 };
133116e48deSShuo Chen  for (int i = 0; i < M*1024; ++i)
134116e48deSShuo Chen  {
135116e48deSShuo Chen    int nw = SSL_write(ssl_client, buf, sizeof buf);
136116e48deSShuo Chen    if (nw != sizeof buf)
137116e48deSShuo Chen    {
138116e48deSShuo Chen      printf("nw = %d\n", nw);
139116e48deSShuo Chen    }
140116e48deSShuo Chen    int nr = SSL_read(ssl, buf, sizeof buf);
141116e48deSShuo Chen    if (nr != sizeof buf)
142116e48deSShuo Chen    {
143116e48deSShuo Chen      printf("nr = %d\n", nr);
144116e48deSShuo Chen    }
145116e48deSShuo Chen  }
146116e48deSShuo Chen  double elapsed = now() - start2;
147116e48deSShuo Chen  printf("%.2f %.1f MiB/s\n", elapsed, M / elapsed);
148116e48deSShuo Chen  printf("=============================================== SSL_CTX_free\n");
149116e48deSShuo Chen  SSL_free (ssl);
150116e48deSShuo Chen  SSL_free (ssl_client);
151116e48deSShuo Chen  printf("=============================================== end\n");
152116e48deSShuo Chen
153116e48deSShuo Chen  SSL_CTX_free (ctx);
154116e48deSShuo Chen  SSL_CTX_free (ctx_client);
155116e48deSShuo Chen  // OPENSSL_cleanup();  // only in 1.1.0
156116e48deSShuo Chen}
157