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

randombit / botan / 28994301380

09 Jul 2026 12:46AM UTC coverage: 89.396% (+1.7%) from 87.739%
28994301380

push

github

web-flow
Merge pull request #5712 from randombit/jack/cdp-aia-extn

Improve CDP, AIA and IDP extension decoding and handling

113303 of 126743 relevant lines covered (89.4%)

10806632.55 hits per line

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

86.0
/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"));
72✔
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", cipher_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
         const auto ct_len_bound_enc = static_cast<Botan::PK_Encryptor&>(encryptor).ciphertext_length(input.size());
72✔
90
         const auto ct_len_bound_dec = static_cast<Botan::PK_Decryptor&>(decryptor).ciphertext_length(input.size());
72✔
91
         result.test_sz_eq("ciphertext length bounds match", ct_len_bound_enc, ct_len_bound_dec);
72✔
92

93
         result.test_bin_eq("encryption", encryptor.encrypt(input, this->rng()), expected);
72✔
94
         result.test_bin_eq("decryption", decryptor.decrypt(expected), input);
72✔
95
         result.test_sz_lte("ciphertext length within bound",
72✔
96
                            expected.size(),
97
                            static_cast<Botan::PK_Decryptor&>(decryptor).ciphertext_length(input.size()));
72✔
98

99
         check_invalid_ciphertexts(result, decryptor, input, expected, this->rng());
72✔
100

101
         return result;
72✔
102
      }
482✔
103
};
104

105
BOTAN_REGISTER_TEST("pubkey", "dlies", DLIES_KAT_Tests);
106

107
Test::Result test_xor() {
1✔
108
   Test::Result result("DLIES XOR");
1✔
109

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

113
   const size_t mac_key_len = 16;
1✔
114

115
   std::unique_ptr<Botan::KDF> kdf;
1✔
116
   std::unique_ptr<Botan::MAC> mac;
1✔
117

118
   auto rng = Test::new_rng("dlies_xor");
1✔
119

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

122
   const Botan::DH_PrivateKey alice(*rng, group);
1✔
123
   const Botan::DH_PrivateKey bob(*rng, group);
1✔
124

125
   for(const auto& kfunc : kdfs) {
3✔
126
      kdf = Botan::KDF::create(kfunc);
4✔
127

128
      if(!kdf) {
2✔
129
         result.test_note("Skipping due to missing KDF", kfunc);
×
130
         continue;
×
131
      }
132

133
      for(const auto& mfunc : macs) {
6✔
134
         mac = Botan::MAC::create(mfunc);
8✔
135

136
         if(!mac) {
4✔
137
            result.test_note("Skipping due to missing MAC", mfunc);
×
138
            continue;
×
139
         }
140

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

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

146
         result.test_throws("encrypt not possible without setting other public key",
4✔
147
                            [&encryptor, &plaintext, &rng]() { encryptor.encrypt(plaintext, *rng); });
8✔
148

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

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

154
         // negative test: ciphertext too short
155
         result.test_throws("ciphertext too short", [&decryptor]() { decryptor.decrypt(std::vector<uint8_t>(2)); });
8✔
156

157
         result.test_bin_eq("decryption", decryptor.decrypt(ciphertext), plaintext);
4✔
158
         result.test_sz_lte("ciphertext length within bound",
4✔
159
                            ciphertext.size(),
160
                            static_cast<Botan::PK_Decryptor&>(decryptor).ciphertext_length(plaintext.size()));
4✔
161

162
         check_invalid_ciphertexts(result, decryptor, unlock(plaintext), ciphertext, *rng);
4✔
163
      }
12✔
164
   }
165

166
   return result;
1✔
167
}
5✔
168

169
class DLIES_Unit_Tests final : public Test {
1✔
170
   public:
171
      std::vector<Test::Result> run() override {
1✔
172
         std::vector<Test::Result> results;
1✔
173

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

176
         for(size_t i = 0; i != fns.size(); ++i) {
2✔
177
            try {
1✔
178
               results.push_back(fns[i]());
2✔
179
            } catch(std::exception& e) {
×
180
               results.push_back(Test::Result::Failure("DLIES unit tests " + std::to_string(i), e.what()));
×
181
            }
×
182
         }
183

184
         return results;
1✔
185
      }
2✔
186
};
187

188
BOTAN_REGISTER_TEST("pubkey", "dlies_unit", DLIES_Unit_Tests);
189

190
#endif
191

192
}  // namespace
193

194
}  // 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