Skip to content

Commit 9a022d6

Browse files
committed
[tests] Added AES GCM unit test for CCryptoControl.
1 parent 8a77525 commit 9a022d6

File tree

4 files changed

+118
-4
lines changed

4 files changed

+118
-4
lines changed

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,10 +1355,11 @@ if (ENABLE_UNITTESTS AND ENABLE_CXX11)
13551355
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
13561356

13571357
# Version ranges are only supported with CMake 3.19 or later.
1358+
# Need GTest v1.10 or higher to support GTEST_SKIP.
13581359
if (${CMAKE_VERSION} VERSION_LESS "3.19.0")
1359-
find_package(GTest 1.8)
1360+
find_package(GTest 1.10)
13601361
else()
1361-
find_package(GTest 1.8...1.12)
1362+
find_package(GTest 1.10...1.12)
13621363
endif()
13631364
if (NOT GTEST_FOUND)
13641365
message(STATUS "GTEST not found! Fetching from git.")

scripts/googletest-download.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ExternalProject_Add(
1111
BINARY_DIR "@GOOGLETEST_DOWNLOAD_ROOT@/googletest-build"
1212
GIT_REPOSITORY
1313
https://github.com/google/googletest.git
14-
GIT_TAG release-1.8.1
14+
GIT_TAG release-1.10.0
1515
CONFIGURE_COMMAND ""
1616
BUILD_COMMAND ""
1717
INSTALL_COMMAND ""

test/filelist.maf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ test_buffer_rcv.cpp
66
test_bonding.cpp
77
test_common.cpp
88
test_connection_timeout.cpp
9-
test_many_connections.cpp
9+
test_crypto.cpp
1010
test_cryspr.cpp
1111
test_enforced_encryption.cpp
1212
test_epoll.cpp
@@ -16,6 +16,7 @@ test_ipv6.cpp
1616
test_listen_callback.cpp
1717
test_losslist_rcv.cpp
1818
test_losslist_snd.cpp
19+
test_many_connections.cpp
1920
test_muxer.cpp
2021
test_seqno.cpp
2122
test_socket_options.cpp

test/test_crypto.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include <array>
2+
#include <numeric>
3+
4+
#include "gtest/gtest.h"
5+
6+
#ifdef SRT_ENABLE_ENCRYPTION
7+
#include "crypto.h"
8+
#include "hcrypt.h" // Imports the CRYSPR_HAS_AESGCM definition.
9+
#include "socketconfig.h"
10+
11+
12+
namespace srt
13+
{
14+
15+
class Crypto
16+
: public ::testing::Test
17+
{
18+
protected:
19+
Crypto()
20+
: m_crypt(0)
21+
{
22+
// initialization code here
23+
}
24+
25+
virtual ~Crypto()
26+
{
27+
// cleanup any pending stuff, but no exceptions allowed
28+
}
29+
30+
protected:
31+
// SetUp() is run immediately before a test starts.
32+
void SetUp() override
33+
{
34+
CSrtConfig cfg;
35+
36+
memset(&cfg.CryptoSecret, 0, sizeof(cfg.CryptoSecret));
37+
cfg.CryptoSecret.typ = HAICRYPT_SECTYP_PASSPHRASE;
38+
cfg.CryptoSecret.len = (m_pwd.size() <= (int)sizeof(cfg.CryptoSecret.str) ? m_pwd.size() : (int)sizeof(cfg.CryptoSecret.str));
39+
memcpy((cfg.CryptoSecret.str), m_pwd.c_str(), m_pwd.size());
40+
41+
m_crypt.setCryptoSecret(cfg.CryptoSecret);
42+
43+
// 2 = 128, 3 = 192, 4 = 256
44+
cfg.iSndCryptoKeyLen = SrtHSRequest::SRT_PBKEYLEN_BITS::wrap(4);
45+
m_crypt.setCryptoKeylen(cfg.iSndCryptoKeyLen);
46+
47+
cfg.iCryptoMode = CSrtConfig::CIPHER_MODE_AES_GCM;
48+
EXPECT_EQ(m_crypt.init(HSD_INITIATOR, cfg, true), HaiCrypt_IsAESGCM_Supported() != 0);
49+
50+
const unsigned char* kmmsg = m_crypt.getKmMsg_data(0);
51+
const size_t km_len = m_crypt.getKmMsg_size(0);
52+
uint32_t kmout[72];
53+
size_t kmout_len = 72;
54+
55+
std::array<uint32_t, 72> km_nworder;
56+
NtoHLA(km_nworder.data(), reinterpret_cast<const uint32_t*>(kmmsg), km_len);
57+
m_crypt.processSrtMsg_KMREQ(km_nworder.data(), km_len, 5, kmout, kmout_len);
58+
}
59+
60+
void TearDown() override
61+
{
62+
}
63+
64+
protected:
65+
66+
srt::CCryptoControl m_crypt;
67+
const std::string m_pwd = "abcdefghijk";
68+
};
69+
70+
71+
// Check that destroying the buffer also frees memory units.
72+
TEST_F(Crypto, GCM)
73+
{
74+
if (HaiCrypt_IsAESGCM_Supported() == 0)
75+
GTEST_SKIP() << "The crypto service provider does not support AES GCM.";
76+
77+
const size_t mtu_size = 1500;
78+
const size_t pld_size = 1316;
79+
const size_t tag_len = 16;
80+
81+
CPacket pkt;
82+
pkt.allocate(mtu_size);
83+
84+
const int seqno = 1;
85+
const int msgno = 1;
86+
const int inorder = 1;
87+
const int kflg = m_crypt.getSndCryptoFlags();
88+
89+
pkt.m_iSeqNo = seqno;
90+
pkt.m_iMsgNo = msgno | inorder | PacketBoundaryBits(PB_SOLO) | MSGNO_ENCKEYSPEC::wrap(kflg);;
91+
pkt.m_iTimeStamp = 356;
92+
93+
std::iota(pkt.data(), pkt.data() + pld_size, '0');
94+
pkt.setLength(pld_size);
95+
96+
EXPECT_EQ(m_crypt.encrypt(pkt), ENCS_CLEAR);
97+
EXPECT_EQ(pkt.getLength(), pld_size + tag_len);
98+
99+
auto pkt_enc = std::unique_ptr<CPacket>(pkt.clone());
100+
101+
EXPECT_EQ(m_crypt.decrypt(pkt), ENCS_CLEAR);
102+
EXPECT_EQ(pkt.getLength(), pld_size);
103+
104+
// Modify the payload and expect auth to fail.
105+
pkt_enc->data()[10] = '5';
106+
EXPECT_EQ(m_crypt.decrypt(*pkt_enc.get()), ENCS_FAILED);
107+
108+
}
109+
110+
} // namespace srt
111+
112+
#endif //SRT_ENABLE_ENCRYPTION

0 commit comments

Comments
 (0)