• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 21768358452

06 Feb 2026 10:35PM UTC coverage: 90.064% (-0.003%) from 90.067%
21768358452

Pull #5289

github

web-flow
Merge f589db195 into 8ea0ca252
Pull Request #5289: Further misc header reductions, forward declarations, etc

102238 of 113517 relevant lines covered (90.06%)

11357432.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.95
/src/tests/test_dlies.cpp
1
/*
2
* (C) 2014,2015 Jack Lloyd
3
* (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "tests.h"
9

10
#if defined(BOTAN_HAS_DLIES) && defined(BOTAN_HAS_DIFFIE_HELLMAN)
11
   #include "test_pubkey.h"
12
   #include <botan/dh.h>
13
   #include <botan/dl_group.h>
14
   #include <botan/dlies.h>
15
   #include <botan/rng.h>
16
#endif
17

18
namespace Botan_Tests {
19

20
namespace {
21

22
#if defined(BOTAN_HAS_DLIES) && defined(BOTAN_HAS_DIFFIE_HELLMAN)
23

24
class DLIES_KAT_Tests final : public Text_Based_Test {
×
25
   public:
26
      DLIES_KAT_Tests() : Text_Based_Test("pubkey/dlies.vec", "Kdf,Mac,MacKeyLen,Group,X1,X2,Msg,Ciphertext", "IV") {}
2✔
27

28
      Test::Result run_one_test(const std::string& cipher_algo, const VarMap& vars) override {
72✔
29
         const Botan::BigInt x1 = vars.get_req_bn("X1");
72✔
30
         const Botan::BigInt x2 = vars.get_req_bn("X2");
72✔
31

32
         const std::vector<uint8_t> input = vars.get_req_bin("Msg");
72✔
33
         const std::vector<uint8_t> expected = vars.get_req_bin("Ciphertext");
72✔
34

35
         const std::string kdf_algo = vars.get_req_str("Kdf");
72✔
36
         const std::string mac_algo = vars.get_req_str("Mac");
72✔
37
         const size_t mac_key_len = vars.get_req_sz("MacKeyLen");
72✔
38
         const std::string group_name = vars.get_req_str("Group");
72✔
39

40
         const auto iv = Botan::InitializationVector(vars.get_opt_bin("IV"));
122✔
41

42
         Test::Result result("DLIES " + cipher_algo);
72✔
43

44
         auto kdf = Botan::KDF::create(kdf_algo);
72✔
45
         if(!kdf) {
72✔
46
            result.test_note("Skipping due to missing KDF:  " + kdf_algo);
×
47
            return result;
×
48
         }
49

50
         auto mac = Botan::MAC::create(mac_algo);
72✔
51
         if(!mac) {
72✔
52
            result.test_note("Skipping due to missing MAC:  " + mac_algo);
×
53
            return result;
×
54
         }
55

56
         std::unique_ptr<Botan::Cipher_Mode> enc;
72✔
57
         std::unique_ptr<Botan::Cipher_Mode> dec;
72✔
58
         size_t cipher_key_len = 0;
72✔
59

60
         if(cipher_algo != "XOR") {
72✔
61
            enc = Botan::Cipher_Mode::create(cipher_algo, Botan::Cipher_Dir::Encryption);
50✔
62
            dec = Botan::Cipher_Mode::create(cipher_algo, Botan::Cipher_Dir::Decryption);
50✔
63

64
            if(!enc || !dec) {
50✔
65
               result.test_note("Skipping due to missing cipher:  " + mac_algo);
×
66
               return result;
×
67
            }
68

69
            cipher_key_len = enc->key_spec().maximum_keylength();
50✔
70
         }
71

72
         auto group = Botan::DL_Group::from_name(group_name);
72✔
73

74
         const Botan::DH_PrivateKey from(group, x1);
72✔
75
         const Botan::DH_PrivateKey to(group, x2);
72✔
76

77
         Botan::DLIES_Encryptor encryptor(
72✔
78
            from, this->rng(), kdf->new_object(), std::move(enc), cipher_key_len, mac->new_object(), mac_key_len);
144✔
79
         Botan::DLIES_Decryptor decryptor(
72✔
80
            to, this->rng(), std::move(kdf), std::move(dec), cipher_key_len, std::move(mac), mac_key_len);
144✔
81

82
         if(!iv.empty()) {
72✔
83
            encryptor.set_initialization_vector(iv);
50✔
84
            decryptor.set_initialization_vector(iv);
50✔
85
         }
86

87
         encryptor.set_other_key(to.public_value());
144✔
88

89
         result.test_eq("encryption", encryptor.encrypt(input, this->rng()), expected);
144✔
90
         result.test_eq("decryption", decryptor.decrypt(expected), input);
144✔
91

92
         check_invalid_ciphertexts(result, decryptor, input, expected, this->rng());
72✔
93

94
         return result;
72✔
95
      }
482✔
96
};
97

98
BOTAN_REGISTER_TEST("pubkey", "dlies", DLIES_KAT_Tests);
99

100
Test::Result test_xor() {
1✔
101
   Test::Result result("DLIES XOR");
1✔
102

103
   const std::vector<std::string> kdfs = {"KDF2(SHA-512)", "KDF1-18033(SHA-512)"};
1✔
104
   const std::vector<std::string> macs = {"HMAC(SHA-512)", "CMAC(AES-128)"};
1✔
105

106
   const size_t mac_key_len = 16;
1✔
107

108
   std::unique_ptr<Botan::KDF> kdf;
1✔
109
   std::unique_ptr<Botan::MAC> mac;
1✔
110

111
   auto rng = Test::new_rng("dlies_xor");
1✔
112

113
   auto group = Botan::DL_Group::from_name("modp/ietf/2048");
1✔
114

115
   const Botan::DH_PrivateKey alice(*rng, group);
1✔
116
   const Botan::DH_PrivateKey bob(*rng, group);
1✔
117

118
   for(const auto& kfunc : kdfs) {
3✔
119
      kdf = Botan::KDF::create(kfunc);
4✔
120

121
      if(!kdf) {
2✔
122
         result.test_note("Skipping due to missing KDF: " + kfunc);
×
123
         continue;
×
124
      }
125

126
      for(const auto& mfunc : macs) {
6✔
127
         mac = Botan::MAC::create(mfunc);
8✔
128

129
         if(!mac) {
4✔
130
            result.test_note("Skipping due to missing MAC: " + mfunc);
×
131
            continue;
×
132
         }
133

134
         Botan::DLIES_Encryptor encryptor(alice, *rng, kdf->new_object(), mac->new_object(), mac_key_len);
8✔
135

136
         // negative test: other pub key not set
137
         Botan::secure_vector<uint8_t> plaintext = rng->random_vec(32);
4✔
138

139
         result.test_throws("encrypt not possible without setting other public key",
8✔
140
                            [&encryptor, &plaintext, &rng]() { encryptor.encrypt(plaintext, *rng); });
8✔
141

142
         encryptor.set_other_key(bob.public_value());
8✔
143
         std::vector<uint8_t> ciphertext = encryptor.encrypt(plaintext, *rng);
4✔
144

145
         Botan::DLIES_Decryptor decryptor(bob, *rng, kdf->new_object(), mac->new_object(), mac_key_len);
8✔
146

147
         // negative test: ciphertext too short
148
         result.test_throws("ciphertext too short", [&decryptor]() { decryptor.decrypt(std::vector<uint8_t>(2)); });
12✔
149

150
         result.test_eq("decryption", decryptor.decrypt(ciphertext), plaintext);
8✔
151

152
         check_invalid_ciphertexts(result, decryptor, unlock(plaintext), ciphertext, *rng);
4✔
153
      }
12✔
154
   }
155

156
   return result;
1✔
157
}
5✔
158

159
class DLIES_Unit_Tests final : public Test {
1✔
160
   public:
161
      std::vector<Test::Result> run() override {
1✔
162
         std::vector<Test::Result> results;
1✔
163

164
         std::vector<std::function<Test::Result()>> fns = {test_xor};
2✔
165

166
         for(size_t i = 0; i != fns.size(); ++i) {
2✔
167
            try {
1✔
168
               results.push_back(fns[i]());
2✔
169
            } catch(std::exception& e) {
×
170
               results.push_back(Test::Result::Failure("DLIES unit tests " + std::to_string(i), e.what()));
×
171
            }
×
172
         }
173

174
         return results;
1✔
175
      }
2✔
176
};
177

178
BOTAN_REGISTER_TEST("pubkey", "dlies_unit", DLIES_Unit_Tests);
179

180
#endif
181

182
}  // namespace
183

184
}  // namespace Botan_Tests
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc