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

randombit / botan / 23111636944

15 Mar 2026 01:45PM UTC coverage: 89.732% (+0.002%) from 89.73%
23111636944

Pull #5446

github

web-flow
Merge 75b20e42b into e9952d62f
Pull Request #5446: Address (or silence) various new warnings from clang-tidy 22

104217 of 116142 relevant lines covered (89.73%)

11426748.42 hits per line

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

93.1
/src/tests/test_x25519.cpp
1
/*
2
* (C) 2014,2015 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "tests.h"
8

9
#if defined(BOTAN_HAS_X25519)
10
   #include "test_pubkey.h"
11
   #include <botan/data_src.h>
12
   #include <botan/pkcs8.h>
13
   #include <botan/pubkey.h>
14
   #include <botan/x25519.h>
15
   #include <botan/x509_key.h>
16
#endif
17

18
namespace Botan_Tests {
19

20
namespace {
21

22
#if defined(BOTAN_HAS_X25519)
23

24
class X25519_Agreement_Tests final : public PK_Key_Agreement_Test {
25
   public:
26
      X25519_Agreement_Tests() : PK_Key_Agreement_Test("X25519", "pubkey/x25519.vec", "Secret,CounterKey,K") {}
2✔
27

28
      bool agreement_should_fail(const std::string& /*unused*/, const VarMap& vars) const override {
90✔
29
         for(const auto byte : vars.get_req_bin("K")) {
599✔
30
            if(byte != 0) {
585✔
31
               return false;
76✔
32
            }
33
         }
76✔
34

35
         return true;
14✔
36
      }
37

38
      std::string default_kdf(const VarMap& /*unused*/) const override { return "Raw"; }
90✔
39

40
      std::unique_ptr<Botan::Private_Key> load_our_key(const std::string& /*header*/, const VarMap& vars) override {
90✔
41
         const std::vector<uint8_t> secret_vec = vars.get_req_bin("Secret");
90✔
42
         Botan::secure_vector<uint8_t> secret(secret_vec.begin(), secret_vec.end());
90✔
43
         return std::make_unique<Botan::X25519_PrivateKey>(secret);
180✔
44
      }
180✔
45

46
      std::vector<uint8_t> load_their_key(const std::string& /*header*/, const VarMap& vars) override {
90✔
47
         return vars.get_req_bin("CounterKey");
90✔
48
      }
49
};
50

51
BOTAN_REGISTER_TEST("pubkey", "x25519_agreement", X25519_Agreement_Tests);
52

53
class X25519_Roundtrip_Test final : public Test {
1✔
54
   public:
55
      std::vector<Test::Result> run() override {
1✔
56
         std::vector<Test::Result> results;
1✔
57

58
         for(size_t i = 0; i < 10; ++i) {
11✔
59
            Test::Result result("X25519 roundtrip");
10✔
60

61
            const Botan::X25519_PrivateKey a_priv_gen(this->rng());
10✔
62
            const Botan::X25519_PrivateKey b_priv_gen(this->rng());
10✔
63

64
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_AEAD_GCM) && \
65
      defined(BOTAN_HAS_SHA2_32)
66
            // Then serialize to encrypted storage
67

68
            const std::string a_pass = "alice pass";
10✔
69
            const std::string b_pass = "bob pass";
10✔
70
            const auto pbe_time = std::chrono::milliseconds(1);
10✔
71
            const std::string a_priv_pem = Botan::PKCS8::PEM_encode(a_priv_gen, this->rng(), a_pass, pbe_time);
10✔
72
            const std::string b_priv_pem = Botan::PKCS8::PEM_encode(b_priv_gen, this->rng(), b_pass, pbe_time);
10✔
73

74
            // Reload back into memory
75
            Botan::DataSource_Memory a_priv_ds(a_priv_pem);
10✔
76
            Botan::DataSource_Memory b_priv_ds(b_priv_pem);
10✔
77

78
            auto a_priv = Botan::PKCS8::load_key(a_priv_ds, [a_pass]() { return std::string(a_pass); });
60✔
79
            auto b_priv = Botan::PKCS8::load_key(b_priv_ds, b_pass);
10✔
80
   #else
81
            const std::string a_priv_pem = Botan::PKCS8::PEM_encode(a_priv_gen);
82
            const std::string b_priv_pem = Botan::PKCS8::PEM_encode(b_priv_gen);
83

84
            // Reload back into memory
85
            Botan::DataSource_Memory a_priv_ds(a_priv_pem);
86
            Botan::DataSource_Memory b_priv_ds(b_priv_pem);
87

88
            auto a_priv = Botan::PKCS8::load_key(a_priv_ds);
89
            auto b_priv = Botan::PKCS8::load_key(b_priv_ds);
90
   #endif
91

92
            // Export public keys as PEM
93
            const std::string a_pub_pem = Botan::X509::PEM_encode(*a_priv);
10✔
94
            const std::string b_pub_pem = Botan::X509::PEM_encode(*b_priv);
10✔
95

96
            Botan::DataSource_Memory a_pub_ds(a_pub_pem);
10✔
97
            Botan::DataSource_Memory b_pub_ds(b_pub_pem);
10✔
98

99
            auto a_pub = Botan::X509::load_key(a_pub_ds);
10✔
100
            auto b_pub = Botan::X509::load_key(b_pub_ds);
10✔
101

102
            const Botan::X25519_PublicKey* a_pub_key = dynamic_cast<Botan::X25519_PublicKey*>(a_pub.get());
10✔
103
            const Botan::X25519_PublicKey* b_pub_key = dynamic_cast<Botan::X25519_PublicKey*>(b_pub.get());
10✔
104

105
            if(a_pub_key != nullptr && b_pub_key != nullptr) {
10✔
106
               const Botan::PK_Key_Agreement a_ka(*a_priv, this->rng(), "Raw");
10✔
107
               const Botan::PK_Key_Agreement b_ka(*b_priv, this->rng(), "Raw");
10✔
108

109
               const Botan::SymmetricKey a_key = a_ka.derive_key(32, b_pub_key->public_value());
10✔
110
               const Botan::SymmetricKey b_key = b_ka.derive_key(32, a_pub_key->public_value());
10✔
111

112
               if(!result.test_bin_eq("key agreement", a_key.bits_of(), b_key.bits_of())) {
30✔
113
                  result.test_note(a_priv_pem);
×
114
                  result.test_note(b_priv_pem);
×
115
               }
116
            } else {
20✔
117
               result.test_failure("Cast back to X25519 failed");
×
118
            }
119

120
            results.push_back(result);
10✔
121
         }
80✔
122

123
         return results;
1✔
124
      }
×
125
};
126

127
BOTAN_REGISTER_TEST("pubkey", "x25519_rt", X25519_Roundtrip_Test);
128

129
class X25519_Keygen_Tests final : public PK_Key_Generation_Test {
1✔
130
   public:
131
      std::vector<std::string> keygen_params() const override { return {""}; }
1✔
132

133
      std::string algo_name() const override { return "X25519"; }
1✔
134

135
      std::unique_ptr<Botan::Public_Key> public_key_from_raw(std::string_view /* keygen_params */,
1✔
136
                                                             std::string_view /* provider */,
137
                                                             std::span<const uint8_t> raw_pk) const override {
138
         return std::make_unique<Botan::X25519_PublicKey>(raw_pk);
1✔
139
      }
140
};
141

142
BOTAN_REGISTER_TEST("pubkey", "x25519_keygen", X25519_Keygen_Tests);
143

144
#endif
145

146
}  // namespace
147

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