• 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

90.0
/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
            result.start_timer();
10✔
58

59
            Botan::X25519_PrivateKey a_priv_gen(this->rng());
10✔
60
            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
            Botan::X25519_PublicKey* a_pub_key = dynamic_cast<Botan::X25519_PublicKey*>(a_pub.get());
10✔
101
            Botan::X25519_PublicKey* b_pub_key = dynamic_cast<Botan::X25519_PublicKey*>(b_pub.get());
10✔
102

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

107
               Botan::SymmetricKey a_key = a_ka.derive_key(32, b_pub_key->public_value());
10✔
108
               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
            result.end_timer();
10✔
119
            results.push_back(result);
10✔
120
         }
80✔
121

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

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

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

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

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

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

143
#endif
144

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