proxygen
EncryptedRecordBench.cpp
Go to the documentation of this file.
1 // Copyright 2004-present Facebook. All Rights Reserved.
2 #include <vector>
3 
4 #include <folly/Benchmark.h>
5 #include <folly/Random.h>
6 #include <folly/init/Init.h>
7 
8 #include <fizz/crypto/Utils.h>
13 
14 using namespace fizz;
15 
16 std::unique_ptr<folly::IOBuf> makeRandom(size_t n) {
17  static const char alphanum[] =
18  "0123456789"
19  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
20  "abcdefghijklmnopqrstuvwxyz";
21 
22  std::string rv;
23  rv.reserve(n);
24  for (size_t i = 0; i < n; ++i) {
25  rv.push_back(alphanum[folly::Random::rand32() % (sizeof(alphanum) - 1)]);
26  }
27  return folly::IOBuf::copyBuffer(rv, 5, 17);
28 }
29 
30 std::unique_ptr<folly::IOBuf> toIOBuf(std::string hexData) {
31  std::string out;
32  CHECK(folly::unhexlify(hexData, out));
33  return folly::IOBuf::copyBuffer(out);
34 }
35 
37  TrafficKey trafficKey;
38  trafficKey.key = toIOBuf("000102030405060708090A0B0C0D0E0F");
39  trafficKey.iv = toIOBuf("000102030405060708090A0B");
40  return trafficKey;
41 }
42 
43 void encryptGCM(uint32_t n, size_t size) {
44  std::unique_ptr<Aead> aead;
45  std::vector<fizz::TLSMessage> msgs;
48  aead = std::make_unique<OpenSSLEVPCipher<AESGCM128>>();
49  aead->setKey(getKey());
50  write.setAead(folly::ByteRange(), std::move(aead));
51  for (size_t i = 0; i < n; ++i) {
53  msgs.push_back(std::move(msg));
54  }
55  }
56 
57  TLSContent content;
58  for (auto& msg : msgs) {
59  content = write.write(std::move(msg));
60  }
61  folly::doNotOptimizeAway(content);
62 }
63 
69 
70 #if FOLLY_OPENSSL_IS_110 && !defined(OPENSSL_NO_OCB)
71 void encryptOCB(uint32_t n, size_t size) {
72  std::unique_ptr<Aead> aead;
73  std::vector<fizz::TLSMessage> msgs;
76  aead = std::make_unique<OpenSSLEVPCipher<AESOCB128>>();
77  aead->setKey(getKey());
78  write.setAead(folly::ByteRange(), std::move(aead));
79  for (size_t i = 0; i < n; ++i) {
81  msgs.push_back(std::move(msg));
82  }
83  }
84 
85  TLSContent content;
86  for (auto& msg : msgs) {
87  content = write.write(std::move(msg));
88  }
89  folly::doNotOptimizeAway(content);
90 }
91 
92 BENCHMARK_PARAM(encryptOCB, 10);
93 BENCHMARK_PARAM(encryptOCB, 100);
94 BENCHMARK_PARAM(encryptOCB, 1000);
95 BENCHMARK_PARAM(encryptOCB, 4000);
96 BENCHMARK_PARAM(encryptOCB, 8000);
97 #endif
98 
99 int main(int argc, char** argv) {
100  folly::init(&argc, &argv);
103  return 0;
104 }
bool unhexlify(const InputString &input, OutputString &output)
Definition: String-inl.h:616
void write(const T &in, folly::io::Appender &appender)
Definition: Types-inl.h:112
std::unique_ptr< folly::IOBuf > toIOBuf(std::string hexData)
#define BENCHMARK_SUSPEND
Definition: Benchmark.h:576
int main(int argc, char **argv)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
static void init()
Definition: Utils.cpp:42
std::unique_ptr< folly::IOBuf > key
Definition: Aead.h:17
std::unique_ptr< folly::IOBuf > iv
Definition: Aead.h:18
void runBenchmarks()
Definition: Benchmark.cpp:456
virtual void setKey(TrafficKey key)=0
void encryptGCM(uint32_t n, size_t size)
void init(int *argc, char ***argv, bool removeFlags)
Definition: Init.cpp:34
char ** argv
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
Definition: Actions.h:16
std::unique_ptr< folly::IOBuf > makeRandom(size_t n)
TrafficKey getKey()
const char * string
Definition: Conv.cpp:212
static uint32_t rand32()
Definition: Random.h:213
BENCHMARK_PARAM(encryptGCM, 10)
static std::unique_ptr< IOBuf > copyBuffer(const void *buf, std::size_t size, std::size_t headroom=0, std::size_t minTailroom=0)
Definition: IOBuf.h:1587
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258