benchmark-libressl.cc revision 2c01beb4
12c01beb4SShuo Chen#include <muduo/net/Buffer.h> 22c01beb4SShuo Chen 32c01beb4SShuo Chen#include <stdio.h> 42c01beb4SShuo Chen 52c01beb4SShuo Chen#include <tls.h> 62c01beb4SShuo Chen 72c01beb4SShuo Chen#include "timer.h" 82c01beb4SShuo Chen 92c01beb4SShuo Chenmuduo::net::Buffer clientOut, serverOut; 102c01beb4SShuo Chen 112c01beb4SShuo Chenssize_t net_read(struct tls *ctx, void *buf, size_t len, void *arg) 122c01beb4SShuo Chen{ 132c01beb4SShuo Chen muduo::net::Buffer* in = ((arg == &clientOut) ? &serverOut : &clientOut); 142c01beb4SShuo Chen if (in->readableBytes() > 0) 152c01beb4SShuo Chen { 162c01beb4SShuo Chen size_t n = std::min(in->readableBytes(), len); 172c01beb4SShuo Chen memcpy(buf, in->peek(), n); 182c01beb4SShuo Chen in->retrieve(n); 192c01beb4SShuo Chen return n; 202c01beb4SShuo Chen } 212c01beb4SShuo Chen else 222c01beb4SShuo Chen { 232c01beb4SShuo Chen return TLS_WANT_POLLIN; 242c01beb4SShuo Chen } 252c01beb4SShuo Chen} 262c01beb4SShuo Chen 272c01beb4SShuo Chenssize_t net_write(struct tls *ctx, const void *buf, size_t len, void *arg) 282c01beb4SShuo Chen{ 292c01beb4SShuo Chen muduo::net::Buffer* out = static_cast<muduo::net::Buffer*>(arg); 302c01beb4SShuo Chen out->append(buf, len); 312c01beb4SShuo Chen return len; 322c01beb4SShuo Chen} 332c01beb4SShuo Chen 342c01beb4SShuo Chenstruct tls* client() 352c01beb4SShuo Chen{ 362c01beb4SShuo Chen struct tls_config* cfg = tls_config_new(); 372c01beb4SShuo Chen assert(cfg != NULL); 382c01beb4SShuo Chen 392c01beb4SShuo Chen tls_config_set_ca_file(cfg, "ca.pem"); 402c01beb4SShuo Chen // tls_config_insecure_noverifycert(cfg); 412c01beb4SShuo Chen // tls_config_insecure_noverifyname(cfg); 422c01beb4SShuo Chen 432c01beb4SShuo Chen struct tls* ctx = tls_client(); 442c01beb4SShuo Chen assert(ctx != NULL); 452c01beb4SShuo Chen 462c01beb4SShuo Chen int ret = tls_configure(ctx, cfg); 472c01beb4SShuo Chen assert(ret == 0); 482c01beb4SShuo Chen 492c01beb4SShuo Chen tls_connect_cbs(ctx, net_read, net_write, &clientOut, "Test Server Cert"); 502c01beb4SShuo Chen 512c01beb4SShuo Chen return ctx; 522c01beb4SShuo Chen} 532c01beb4SShuo Chen 542c01beb4SShuo Chenstruct tls* server() 552c01beb4SShuo Chen{ 562c01beb4SShuo Chen struct tls_config* cfg = tls_config_new(); 572c01beb4SShuo Chen assert(cfg != NULL); 582c01beb4SShuo Chen 592c01beb4SShuo Chen int ret = tls_config_set_cert_file(cfg, "server.pem"); 602c01beb4SShuo Chen assert(ret == 0); 612c01beb4SShuo Chen 622c01beb4SShuo Chen ret = tls_config_set_key_file(cfg, "server.pem"); 632c01beb4SShuo Chen assert(ret == 0); 642c01beb4SShuo Chen 652c01beb4SShuo Chen // tls_config_verify_client_optional(cfg); 662c01beb4SShuo Chen struct tls* ctx = tls_server(); 672c01beb4SShuo Chen assert(ctx != NULL); 682c01beb4SShuo Chen 692c01beb4SShuo Chen ret = tls_configure(ctx, cfg); 702c01beb4SShuo Chen assert(ret == 0); 712c01beb4SShuo Chen 722c01beb4SShuo Chen struct tls* conn_ctx = NULL; 732c01beb4SShuo Chen tls_accept_cbs(ctx, &conn_ctx, net_read, net_write, &serverOut); 742c01beb4SShuo Chen return conn_ctx; 752c01beb4SShuo Chen} 762c01beb4SShuo Chen 772c01beb4SShuo ChenTimer tclient, tserver; 782c01beb4SShuo Chen 792c01beb4SShuo Chenvoid handshake(struct tls* cctx, struct tls* sctx) 802c01beb4SShuo Chen{ 812c01beb4SShuo Chen int client_done = false, server_done = false; 822c01beb4SShuo Chen while (!(client_done && server_done)) 832c01beb4SShuo Chen { 842c01beb4SShuo Chen if (!client_done) 852c01beb4SShuo Chen { 862c01beb4SShuo Chen tclient.start(); 872c01beb4SShuo Chen int ret = tls_handshake(cctx); 882c01beb4SShuo Chen tclient.stop(); 892c01beb4SShuo Chen // printf("c %d\n", ret); 902c01beb4SShuo Chen if (ret == 0) 912c01beb4SShuo Chen client_done = true; 922c01beb4SShuo Chen else if (ret == -1) 932c01beb4SShuo Chen break; 942c01beb4SShuo Chen } 952c01beb4SShuo Chen 962c01beb4SShuo Chen if (!server_done) 972c01beb4SShuo Chen { 982c01beb4SShuo Chen tserver.start(); 992c01beb4SShuo Chen int ret = tls_handshake(sctx); 1002c01beb4SShuo Chen tserver.stop(); 1012c01beb4SShuo Chen // printf("s %d\n", ret); 1022c01beb4SShuo Chen if (ret == 0) 1032c01beb4SShuo Chen server_done = true; 1042c01beb4SShuo Chen else if (ret == -1) 1052c01beb4SShuo Chen break; 1062c01beb4SShuo Chen } 1072c01beb4SShuo Chen } 1082c01beb4SShuo Chen} 1092c01beb4SShuo Chen 1102c01beb4SShuo Chenint main(int argc, char* argv[]) 1112c01beb4SShuo Chen{ 1122c01beb4SShuo Chen int ret = tls_init(); 1132c01beb4SShuo Chen assert(ret == 0); 1142c01beb4SShuo Chen 1152c01beb4SShuo Chen struct tls* cctx = client(); 1162c01beb4SShuo Chen struct tls* sctx = server(); 1172c01beb4SShuo Chen 1182c01beb4SShuo Chen const int N = 500; 1192c01beb4SShuo Chen Timer all, client, server; 1202c01beb4SShuo Chen all.start(); 1212c01beb4SShuo Chen for (int i = 0; i < N; ++i) 1222c01beb4SShuo Chen { 1232c01beb4SShuo Chen handshake(cctx, sctx); 1242c01beb4SShuo Chen 1252c01beb4SShuo Chen if (i == 0) 1262c01beb4SShuo Chen printf("cipher %s\n", tls_conn_cipher(cctx)); 1272c01beb4SShuo Chen } 1282c01beb4SShuo Chen all.stop(); 1292c01beb4SShuo Chen printf("%f secs, %f handshakes/sec\n", all.seconds(), N / all.seconds()); 1302c01beb4SShuo Chen printf("client %f secs, server %f secs\n", tclient.seconds(), tserver.seconds()); 1312c01beb4SShuo Chen} 1322c01beb4SShuo Chen 133