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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

87.3
/src/tests/test_c25519.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_CURVE_25519)
10
   #include "test_pubkey.h"
11
   #include <botan/curve25519.h>
12
   #include <botan/data_src.h>
13
   #include <botan/pkcs8.h>
14
   #include <botan/x509_key.h>
15
#endif
16

17
namespace Botan_Tests {
18

19
#if defined(BOTAN_HAS_CURVE_25519)
20

21
class Curve25519_Sclarmult_Tests final : public Text_Based_Test {
×
22
   public:
23
      Curve25519_Sclarmult_Tests() : Text_Based_Test("pubkey/c25519_scalar.vec", "Secret,Basepoint,Out") {}
3✔
24

25
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
18✔
26
         const std::vector<uint8_t> secret = vars.get_req_bin("Secret");
18✔
27
         const std::vector<uint8_t> basepoint = vars.get_req_bin("Basepoint");
18✔
28
         const std::vector<uint8_t> expected = vars.get_req_bin("Out");
18✔
29

30
         std::vector<uint8_t> got(32);
18✔
31
         Botan::curve25519_donna(got.data(), secret.data(), basepoint.data());
18✔
32

33
         Test::Result result("Curve25519 scalarmult");
18✔
34
         result.test_eq("basemult", got, expected);
18✔
35
         return result;
18✔
36
      }
72✔
37
};
38

39
BOTAN_REGISTER_TEST("pubkey", "curve25519_scalar", Curve25519_Sclarmult_Tests);
40

41
class Curve25519_Agreement_Tests final : public PK_Key_Agreement_Test {
×
42
   public:
43
      Curve25519_Agreement_Tests() : PK_Key_Agreement_Test("X25519", "pubkey/x25519.vec", "Secret,CounterKey,K") {}
4✔
44

45
      std::string default_kdf(const VarMap& /*unused*/) const override { return "Raw"; }
72✔
46

47
      std::unique_ptr<Botan::Private_Key> load_our_key(const std::string& /*header*/, const VarMap& vars) override {
72✔
48
         const std::vector<uint8_t> secret_vec = vars.get_req_bin("Secret");
72✔
49
         Botan::secure_vector<uint8_t> secret(secret_vec.begin(), secret_vec.end());
72✔
50
         return std::make_unique<Botan::Curve25519_PrivateKey>(secret);
144✔
51
      }
144✔
52

53
      std::vector<uint8_t> load_their_key(const std::string& /*header*/, const VarMap& vars) override {
72✔
54
         return vars.get_req_bin("CounterKey");
144✔
55
      }
56
};
57

58
BOTAN_REGISTER_TEST("pubkey", "curve25519_agreement", Curve25519_Agreement_Tests);
59

60
class Curve25519_Roundtrip_Test final : public Test {
×
61
   public:
62
      std::vector<Test::Result> run() override {
1✔
63
         std::vector<Test::Result> results;
1✔
64

65
         for(size_t i = 0; i < 10; ++i) {
11✔
66
            Test::Result result("Curve25519 roundtrip");
10✔
67

68
            Botan::Curve25519_PrivateKey a_priv_gen(Test::rng());
10✔
69
            Botan::Curve25519_PrivateKey b_priv_gen(Test::rng());
10✔
70

71
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_AEAD_GCM) && \
72
      defined(BOTAN_HAS_SHA2_32)
73
            // Then serialize to encrypted storage
74

75
            const std::string a_pass = "alice pass";
10✔
76
            const std::string b_pass = "bob pass";
10✔
77
            const auto pbe_time = std::chrono::milliseconds(1);
10✔
78
            const std::string a_priv_pem = Botan::PKCS8::PEM_encode(a_priv_gen, Test::rng(), a_pass, pbe_time);
10✔
79
            const std::string b_priv_pem = Botan::PKCS8::PEM_encode(b_priv_gen, Test::rng(), b_pass, pbe_time);
10✔
80

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

85
            auto a_priv = Botan::PKCS8::load_key(a_priv_ds, [a_pass]() { return a_pass; });
60✔
86
            auto b_priv = Botan::PKCS8::load_key(b_priv_ds, b_pass);
10✔
87
   #else
88
            const std::string a_priv_pem = Botan::PKCS8::PEM_encode(a_priv_gen);
89
            const std::string b_priv_pem = Botan::PKCS8::PEM_encode(b_priv_gen);
90

91
            // Reload back into memory
92
            Botan::DataSource_Memory a_priv_ds(a_priv_pem);
93
            Botan::DataSource_Memory b_priv_ds(b_priv_pem);
94

95
            auto a_priv = Botan::PKCS8::load_key(a_priv_ds);
96
            auto b_priv = Botan::PKCS8::load_key(b_priv_ds);
97
   #endif
98

99
            // Export public keys as PEM
100
            const std::string a_pub_pem = Botan::X509::PEM_encode(*a_priv);
10✔
101
            const std::string b_pub_pem = Botan::X509::PEM_encode(*b_priv);
10✔
102

103
            Botan::DataSource_Memory a_pub_ds(a_pub_pem);
10✔
104
            Botan::DataSource_Memory b_pub_ds(b_pub_pem);
10✔
105

106
            auto a_pub = Botan::X509::load_key(a_pub_ds);
10✔
107
            auto b_pub = Botan::X509::load_key(b_pub_ds);
10✔
108

109
            Botan::Curve25519_PublicKey* a_pub_key = dynamic_cast<Botan::Curve25519_PublicKey*>(a_pub.get());
10✔
110
            Botan::Curve25519_PublicKey* b_pub_key = dynamic_cast<Botan::Curve25519_PublicKey*>(b_pub.get());
10✔
111

112
            if(a_pub_key && b_pub_key) {
10✔
113
               Botan::PK_Key_Agreement a_ka(*a_priv, Test::rng(), "Raw");
10✔
114
               Botan::PK_Key_Agreement b_ka(*b_priv, Test::rng(), "Raw");
10✔
115

116
               Botan::SymmetricKey a_key = a_ka.derive_key(32, b_pub_key->public_value());
10✔
117
               Botan::SymmetricKey b_key = b_ka.derive_key(32, a_pub_key->public_value());
10✔
118

119
               if(!result.test_eq("key agreement", a_key.bits_of(), b_key.bits_of())) {
40✔
120
                  result.test_note(a_priv_pem);
×
121
                  result.test_note(b_priv_pem);
×
122
               }
123
            } else {
20✔
124
               result.test_failure("Cast back to Curve25519 failed");
×
125
            }
126

127
            results.push_back(result);
10✔
128
         }
120✔
129

130
         return results;
1✔
131
      }
×
132
};
133

134
BOTAN_REGISTER_TEST("pubkey", "curve25519_rt", Curve25519_Roundtrip_Test);
135

136
class Curve25519_Keygen_Tests final : public PK_Key_Generation_Test {
×
137
   public:
138
      std::vector<std::string> keygen_params() const override { return {""}; }
2✔
139

140
      std::string algo_name() const override { return "Curve25519"; }
3✔
141
};
142

143
BOTAN_REGISTER_TEST("pubkey", "curve25519_keygen", Curve25519_Keygen_Tests);
144

145
#endif
146

147
}
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