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

randombit / botan / 12929155010

23 Jan 2025 12:27PM UTC coverage: 91.202% (-0.01%) from 91.213%
12929155010

push

github

web-flow
Merge pull request #4580 from randombit/jack/dl-group-from-name

Add DL_Group::from_name

93548 of 102572 relevant lines covered (91.2%)

11702890.34 hits per line

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

83.87
/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
#endif
16

17
namespace Botan_Tests {
18

19
namespace {
20

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

93
         return result;
72✔
94
      }
626✔
95
};
96

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

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

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

105
   const size_t mac_key_len = 16;
1✔
106

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

155
   return result;
1✔
156
}
5✔
157

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

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

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

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

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

179
#endif
180

181
}  // namespace
182

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