|
| 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