Skip to content

Commit 451c5c1

Browse files
committed
libsodium benchmark refactoring
1 parent 83ef224 commit 451c5c1

File tree

1 file changed

+100
-20
lines changed

1 file changed

+100
-20
lines changed

src/libsodium_test.cpp

Lines changed: 100 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
#include <catch2/catch_test_macros.hpp>
44
#include <catch2/benchmark/catch_benchmark.hpp>
55
#include <catch2/catch_session.hpp>
6+
#include "wifibroadcast.hpp"
67

7-
static const size_t MESSAGE_LEN = 4096; // packet
8+
static const size_t SESSION_MESSAGE_LEN = sizeof(wsession_data_t); // session packet
9+
static const size_t DATA_MESSAGE_LEN = 4096; // data packet
810
static const size_t AD_LEN = 32; // Additional data length
911

1012
static const size_t KEY_LEN = crypto_aead_chacha20poly1305_KEYBYTES;
@@ -16,45 +18,123 @@ TEST_CASE("Chacha20-Poly1305 encryption benchmark", "[benchmark]")
1618
{
1719
uint8_t key[KEY_LEN];
1820
uint8_t nonce[NPUB_LEN];
19-
uint8_t message[MESSAGE_LEN];
21+
uint8_t message[DATA_MESSAGE_LEN];
2022
uint8_t ad[AD_LEN];
2123

22-
uint8_t ciphertext[MESSAGE_LEN + TAG_LEN];
23-
uint8_t decrypted[MESSAGE_LEN];
24+
uint8_t ciphertext[DATA_MESSAGE_LEN + TAG_LEN];
25+
uint8_t decrypted[DATA_MESSAGE_LEN];
2426

25-
randombytes_buf(key, KEY_LEN);
26-
randombytes_buf(nonce, NPUB_LEN);
27-
randombytes_buf(message, MESSAGE_LEN);
28-
randombytes_buf(ad, AD_LEN);
27+
BENCHMARK_ADVANCED("encrypt data packet")(Catch::Benchmark::Chronometer meter)
28+
{
29+
randombytes_buf(key, KEY_LEN);
30+
randombytes_buf(message, DATA_MESSAGE_LEN);
31+
randombytes_buf(ad, AD_LEN);
32+
randombytes_buf(nonce, NPUB_LEN);
33+
34+
meter.measure([&ciphertext, &message, &ad, &nonce, &key]()
35+
{
36+
int ret = crypto_aead_chacha20poly1305_encrypt(
37+
ciphertext, NULL,
38+
message, DATA_MESSAGE_LEN,
39+
ad, AD_LEN,
40+
NULL, // nsec
41+
nonce, key
42+
);
43+
REQUIRE(ret == 0);
44+
return ret;
45+
});
46+
};
2947

30-
BENCHMARK("encrypt packet")
48+
BENCHMARK_ADVANCED("decrypt data packet")(Catch::Benchmark::Chronometer meter)
3149
{
50+
randombytes_buf(key, KEY_LEN);
51+
randombytes_buf(message, DATA_MESSAGE_LEN);
52+
randombytes_buf(ad, AD_LEN);
53+
randombytes_buf(nonce, NPUB_LEN);
54+
3255
int ret = crypto_aead_chacha20poly1305_encrypt(
3356
ciphertext, NULL,
34-
message, MESSAGE_LEN,
57+
message, DATA_MESSAGE_LEN,
3558
ad, AD_LEN,
3659
NULL, // nsec
3760
nonce, key
3861
);
3962
REQUIRE(ret == 0);
63+
64+
meter.measure([&message, &decrypted, &ciphertext, &ad, &nonce, &key]()
65+
{
66+
int ret = crypto_aead_chacha20poly1305_decrypt(
67+
decrypted, NULL,
68+
NULL,
69+
ciphertext, DATA_MESSAGE_LEN + TAG_LEN,
70+
ad, AD_LEN,
71+
nonce, key
72+
);
73+
REQUIRE(ret == 0);
74+
75+
ret |= memcmp(message, decrypted, DATA_MESSAGE_LEN);
76+
REQUIRE(ret == 0);
77+
return ret;
78+
});
79+
80+
return ret;
4081
};
82+
}
83+
84+
TEST_CASE("libsodium crypto_box benchmarks", "[benchmark]")
85+
{
86+
uint8_t pk_sender[crypto_box_PUBLICKEYBYTES];
87+
uint8_t sk_sender[crypto_box_SECRETKEYBYTES];
88+
uint8_t pk_recipient[crypto_box_PUBLICKEYBYTES];
89+
uint8_t sk_recipient[crypto_box_SECRETKEYBYTES];
90+
91+
crypto_box_keypair(pk_sender, sk_sender);
92+
crypto_box_keypair(pk_recipient, sk_recipient);
93+
94+
uint8_t message[SESSION_MESSAGE_LEN];
95+
uint8_t ciphertext[SESSION_MESSAGE_LEN + crypto_box_MACBYTES];
96+
uint8_t decrypted[SESSION_MESSAGE_LEN];
97+
uint8_t nonce[crypto_box_NONCEBYTES];
4198

42-
BENCHMARK("decrypt packet")
99+
BENCHMARK_ADVANCED("encrypt session packet")(Catch::Benchmark::Chronometer meter)
43100
{
44-
int ret = crypto_aead_chacha20poly1305_decrypt(
45-
decrypted, NULL,
46-
NULL,
47-
ciphertext, MESSAGE_LEN + TAG_LEN,
48-
ad, AD_LEN,
49-
nonce, key
50-
);
51-
REQUIRE(ret == 0);
101+
randombytes_buf(message, SESSION_MESSAGE_LEN);
102+
randombytes_buf(nonce, crypto_box_NONCEBYTES);
103+
104+
meter.measure([&ciphertext, &message, &nonce, &pk_recipient, &sk_sender]()
105+
{
106+
int ret = crypto_box_easy(ciphertext, message, SESSION_MESSAGE_LEN,
107+
nonce, pk_recipient, sk_sender);
108+
REQUIRE(ret == 0);
109+
return ret;
110+
});
111+
};
52112

53-
ret = memcmp(message, decrypted, MESSAGE_LEN);
113+
BENCHMARK_ADVANCED("decrypt session packet")(Catch::Benchmark::Chronometer meter)
114+
{
115+
randombytes_buf(message, SESSION_MESSAGE_LEN);
116+
randombytes_buf(nonce, crypto_box_NONCEBYTES);
117+
118+
int ret = crypto_box_easy(ciphertext, message, SESSION_MESSAGE_LEN,
119+
nonce, pk_recipient, sk_sender);
54120
REQUIRE(ret == 0);
121+
122+
meter.measure([&message, &decrypted, &ciphertext, &nonce, &sk_recipient, &pk_sender]()
123+
{
124+
int ret = crypto_box_open_easy(decrypted, ciphertext,
125+
SESSION_MESSAGE_LEN + crypto_box_MACBYTES, nonce,
126+
pk_sender, sk_recipient);
127+
REQUIRE(ret == 0);
128+
129+
ret |= memcmp(message, decrypted, SESSION_MESSAGE_LEN);
130+
REQUIRE(ret == 0);
131+
return ret;
132+
});
133+
return ret;
55134
};
56135
}
57136

137+
58138
int main(int argc, char* argv[])
59139
{
60140
Catch::Session session;

0 commit comments

Comments
 (0)