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

randombit / botan / 11844561993

14 Nov 2024 07:58PM UTC coverage: 91.178% (+0.1%) from 91.072%
11844561993

Pull #4435

github

web-flow
Merge 81dcb29da into e430f157a
Pull Request #4435: Test duration values ​​are now presented in seconds with six digits of precision. Tests without time measurements have been edited.

91856 of 100744 relevant lines covered (91.18%)

9311006.71 hits per line

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

84.04
/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
         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, 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
   result.start_timer();
1✔
102

103
   std::vector<std::string> kdfs = {"KDF2(SHA-512)", "KDF1-18033(SHA-512)"};
1✔
104
   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
   Botan::DH_PrivateKey alice(*rng, Botan::DL_Group("modp/ietf/2048"));
1✔
114
   Botan::DH_PrivateKey bob(*rng, Botan::DL_Group("modp/ietf/2048"));
1✔
115

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

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

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

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

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

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

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

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

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

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

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

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

154
   result.end_timer();
1✔
155
   return result;
1✔
156
}
4✔
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

© 2025 Coveralls, Inc