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

randombit / botan / 21753596263

06 Feb 2026 02:13PM UTC coverage: 90.063% (-0.01%) from 90.073%
21753596263

Pull #5289

github

web-flow
Merge 587099284 into 8ea0ca252
Pull Request #5289: Further misc header reductions, forward declarations, etc

102237 of 113517 relevant lines covered (90.06%)

11402137.11 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
#if defined(BOTAN_HAS_X25519)
21

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

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

33
         return true;
14✔
34
      }
35

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

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

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

49
BOTAN_REGISTER_TEST("pubkey", "x25519_agreement", X25519_Agreement_Tests);
50

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

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

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

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

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

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

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

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

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

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

94
            Botan::DataSource_Memory a_pub_ds(a_pub_pem);
10✔
95
            Botan::DataSource_Memory b_pub_ds(b_pub_pem);
10✔
96

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

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

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

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

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

118
            results.push_back(result);
10✔
119
         }
80✔
120

121
         return results;
1✔
122
      }
×
123
};
124

125
BOTAN_REGISTER_TEST("pubkey", "x25519_rt", X25519_Roundtrip_Test);
126

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

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

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

140
BOTAN_REGISTER_TEST("pubkey", "x25519_keygen", X25519_Keygen_Tests);
141

142
#endif
143

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