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

randombit / botan / 12969367069

25 Jan 2025 11:33PM UTC coverage: 91.224% (-0.003%) from 91.227%
12969367069

Pull #4593

github

web-flow
Merge 212e6285e into 79027d241
Pull Request #4593: New interface for PKCS8 key encryption

93636 of 102644 relevant lines covered (91.22%)

11516421.43 hits per line

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

91.49
/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/x25519.h>
14
   #include <botan/x509_key.h>
15
#endif
16

17
namespace Botan_Tests {
18

19
#if defined(BOTAN_HAS_X25519)
20

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

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

32
         return true;
14✔
33
      }
34

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

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

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

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

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

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

58
            Botan::X25519_PrivateKey a_priv(this->rng());
10✔
59
            Botan::X25519_PrivateKey b_priv(this->rng());
10✔
60

61
            // Export public keys as PEM
62
            const std::string a_pub_pem = Botan::X509::PEM_encode(a_priv);
10✔
63
            const std::string b_pub_pem = Botan::X509::PEM_encode(b_priv);
10✔
64

65
            Botan::DataSource_Memory a_pub_ds(a_pub_pem);
10✔
66
            Botan::DataSource_Memory b_pub_ds(b_pub_pem);
10✔
67

68
            auto a_pub = Botan::X509::load_key(a_pub_ds);
10✔
69
            auto b_pub = Botan::X509::load_key(b_pub_ds);
10✔
70

71
            Botan::X25519_PublicKey* a_pub_key = dynamic_cast<Botan::X25519_PublicKey*>(a_pub.get());
10✔
72
            Botan::X25519_PublicKey* b_pub_key = dynamic_cast<Botan::X25519_PublicKey*>(b_pub.get());
10✔
73

74
            if(a_pub_key && b_pub_key) {
10✔
75
               Botan::PK_Key_Agreement a_ka(a_priv, this->rng(), "Raw");
10✔
76
               Botan::PK_Key_Agreement b_ka(b_priv, this->rng(), "Raw");
10✔
77

78
               Botan::SymmetricKey a_key = a_ka.derive_key(32, b_pub_key->public_value());
10✔
79
               Botan::SymmetricKey b_key = b_ka.derive_key(32, a_pub_key->public_value());
10✔
80

81
               result.test_eq("key agreement", a_key.bits_of(), b_key.bits_of());
40✔
82
            } else {
20✔
83
               result.test_failure("Cast back to X25519 failed");
×
84
            }
85

86
            results.push_back(result);
10✔
87
         }
40✔
88

89
         return results;
1✔
90
      }
×
91
};
92

93
BOTAN_REGISTER_TEST("pubkey", "x25519_rt", X25519_Roundtrip_Test);
94

95
class X25519_Keygen_Tests final : public PK_Key_Generation_Test {
×
96
   public:
97
      std::vector<std::string> keygen_params() const override { return {""}; }
1✔
98

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

101
      std::unique_ptr<Botan::Public_Key> public_key_from_raw(std::string_view /* keygen_params */,
1✔
102
                                                             std::string_view /* provider */,
103
                                                             std::span<const uint8_t> raw_pk) const override {
104
         return std::make_unique<Botan::X25519_PublicKey>(raw_pk);
1✔
105
      }
106
};
107

108
BOTAN_REGISTER_TEST("pubkey", "x25519_keygen", X25519_Keygen_Tests);
109

110
#endif
111

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