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

randombit / botan / 5111374265

29 May 2023 11:19AM UTC coverage: 92.227% (+0.5%) from 91.723%
5111374265

push

github

randombit
Next release will be 3.1.0. Update release notes

75588 of 81959 relevant lines covered (92.23%)

11886470.91 hits per line

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

83.52
/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") {}
3✔
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
         Botan::DL_Group domain(group_name);
72✔
72

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

76
         Botan::DLIES_Encryptor encryptor(
72✔
77
            from, Test::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, Test::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, Test::rng()), expected);
144✔
89
         result.test_eq("decryption", decryptor.decrypt(expected), input);
144✔
90

91
         check_invalid_ciphertexts(result, decryptor, input, expected);
72✔
92

93
         return result;
72✔
94
      }
591✔
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)"};
3✔
103
   std::vector<std::string> macs = {"HMAC(SHA-512)", "CMAC(AES-128)"};
3✔
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
   Botan::DH_PrivateKey alice(Test::rng(), Botan::DL_Group("modp/ietf/2048"));
1✔
111
   Botan::DH_PrivateKey bob(Test::rng(), Botan::DL_Group("modp/ietf/2048"));
1✔
112

113
   for(const auto& kfunc : kdfs) {
3✔
114
      kdf = Botan::KDF::create(kfunc);
4✔
115

116
      if(!kdf) {
2✔
117
         result.test_note("Skipping due to missing KDF: " + kfunc);
×
118
         continue;
×
119
      }
120

121
      for(const auto& mfunc : macs) {
6✔
122
         mac = Botan::MAC::create(mfunc);
8✔
123

124
         if(!mac) {
4✔
125
            result.test_note("Skipping due to missing MAC: " + mfunc);
×
126
            continue;
×
127
         }
128

129
         Botan::DLIES_Encryptor encryptor(alice, Test::rng(), kdf->new_object(), mac->new_object(), mac_key_len);
8✔
130

131
         // negative test: other pub key not set
132
         Botan::secure_vector<uint8_t> plaintext = Test::rng().random_vec(32);
4✔
133

134
         result.test_throws("encrypt not possible without setting other public key",
8✔
135
                            [&encryptor, &plaintext]() { encryptor.encrypt(plaintext, Test::rng()); });
4✔
136

137
         encryptor.set_other_key(bob.public_value());
8✔
138
         std::vector<uint8_t> ciphertext = encryptor.encrypt(plaintext, Test::rng());
4✔
139

140
         Botan::DLIES_Decryptor decryptor(bob, Test::rng(), kdf->new_object(), mac->new_object(), mac_key_len);
8✔
141

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

145
         result.test_eq("decryption", decryptor.decrypt(ciphertext), plaintext);
8✔
146

147
         check_invalid_ciphertexts(result, decryptor, unlock(plaintext), ciphertext);
4✔
148
      }
12✔
149
   }
150

151
   return result;
1✔
152
}
3✔
153

154
class DLIES_Unit_Tests final : public Test {
×
155
   public:
156
      std::vector<Test::Result> run() override {
1✔
157
         std::vector<Test::Result> results;
1✔
158

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

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

169
         return results;
1✔
170
      }
1✔
171
};
172

173
BOTAN_REGISTER_TEST("pubkey", "dlies_unit", DLIES_Unit_Tests);
174

175
#endif
176

177
}  // namespace
178

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

© 2025 Coveralls, Inc