Coverage Report

Created: 2024-02-25 06:16

/src/botan/src/lib/pubkey/curve25519/curve25519.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* Curve25519
3
* (C) 2014 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/curve25519.h>
9
10
#include <botan/ber_dec.h>
11
#include <botan/der_enc.h>
12
#include <botan/rng.h>
13
#include <botan/internal/fmt.h>
14
#include <botan/internal/pk_ops_impl.h>
15
16
namespace Botan {
17
18
159
void curve25519_basepoint(uint8_t mypublic[32], const uint8_t secret[32]) {
19
159
   const uint8_t basepoint[32] = {9};
20
159
   curve25519_donna(mypublic, secret, basepoint);
21
159
}
22
23
namespace {
24
25
0
void size_check(size_t size, const char* thing) {
26
0
   if(size != 32) {
27
0
      throw Decoding_Error(fmt("Invalid size {} for Curve2551 {}", size, thing));
28
0
   }
29
0
}
30
31
0
secure_vector<uint8_t> curve25519(const secure_vector<uint8_t>& secret, const uint8_t pubval[32]) {
32
0
   secure_vector<uint8_t> out(32);
33
0
   curve25519_donna(out.data(), secret.data(), pubval);
34
0
   return out;
35
0
}
36
37
}  // namespace
38
39
0
AlgorithmIdentifier Curve25519_PublicKey::algorithm_identifier() const {
40
0
   return AlgorithmIdentifier(object_identifier(), AlgorithmIdentifier::USE_EMPTY_PARAM);
41
0
}
42
43
0
bool Curve25519_PublicKey::check_key(RandomNumberGenerator& /*rng*/, bool /*strong*/) const {
44
0
   return true;  // no tests possible?
45
0
}
46
47
0
Curve25519_PublicKey::Curve25519_PublicKey(const AlgorithmIdentifier& /*unused*/, std::span<const uint8_t> key_bits) {
48
0
   m_public.assign(key_bits.begin(), key_bits.end());
49
50
0
   size_check(m_public.size(), "public key");
51
0
}
Unexecuted instantiation: Botan::Curve25519_PublicKey::Curve25519_PublicKey(Botan::AlgorithmIdentifier const&, std::__1::span<unsigned char const, 18446744073709551615ul>)
Unexecuted instantiation: Botan::Curve25519_PublicKey::Curve25519_PublicKey(Botan::AlgorithmIdentifier const&, std::__1::span<unsigned char const, 18446744073709551615ul>)
52
53
0
std::vector<uint8_t> Curve25519_PublicKey::public_key_bits() const {
54
0
   return m_public;
55
0
}
56
57
0
std::unique_ptr<Private_Key> Curve25519_PublicKey::generate_another(RandomNumberGenerator& rng) const {
58
0
   return std::make_unique<Curve25519_PrivateKey>(rng);
59
0
};
60
61
159
Curve25519_PrivateKey::Curve25519_PrivateKey(const secure_vector<uint8_t>& secret_key) {
62
159
   if(secret_key.size() != 32) {
63
0
      throw Decoding_Error("Invalid size for Curve25519 private key");
64
0
   }
65
66
159
   m_public.resize(32);
67
159
   m_private = secret_key;
68
159
   curve25519_basepoint(m_public.data(), m_private.data());
69
159
}
Unexecuted instantiation: Botan::Curve25519_PrivateKey::Curve25519_PrivateKey(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > const&)
Botan::Curve25519_PrivateKey::Curve25519_PrivateKey(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > const&)
Line
Count
Source
61
159
Curve25519_PrivateKey::Curve25519_PrivateKey(const secure_vector<uint8_t>& secret_key) {
62
159
   if(secret_key.size() != 32) {
63
0
      throw Decoding_Error("Invalid size for Curve25519 private key");
64
0
   }
65
66
159
   m_public.resize(32);
67
159
   m_private = secret_key;
68
159
   curve25519_basepoint(m_public.data(), m_private.data());
69
159
}
70
71
0
Curve25519_PrivateKey::Curve25519_PrivateKey(RandomNumberGenerator& rng) {
72
0
   m_private = rng.random_vec(32);
73
0
   m_public.resize(32);
74
0
   curve25519_basepoint(m_public.data(), m_private.data());
75
0
}
Unexecuted instantiation: Botan::Curve25519_PrivateKey::Curve25519_PrivateKey(Botan::RandomNumberGenerator&)
Unexecuted instantiation: Botan::Curve25519_PrivateKey::Curve25519_PrivateKey(Botan::RandomNumberGenerator&)
76
77
0
Curve25519_PrivateKey::Curve25519_PrivateKey(const AlgorithmIdentifier& /*unused*/, std::span<const uint8_t> key_bits) {
78
0
   BER_Decoder(key_bits).decode(m_private, ASN1_Type::OctetString).discard_remaining();
79
80
0
   size_check(m_private.size(), "private key");
81
0
   m_public.resize(32);
82
0
   curve25519_basepoint(m_public.data(), m_private.data());
83
0
}
Unexecuted instantiation: Botan::Curve25519_PrivateKey::Curve25519_PrivateKey(Botan::AlgorithmIdentifier const&, std::__1::span<unsigned char const, 18446744073709551615ul>)
Unexecuted instantiation: Botan::Curve25519_PrivateKey::Curve25519_PrivateKey(Botan::AlgorithmIdentifier const&, std::__1::span<unsigned char const, 18446744073709551615ul>)
84
85
0
std::unique_ptr<Public_Key> Curve25519_PrivateKey::public_key() const {
86
0
   return std::make_unique<Curve25519_PublicKey>(public_value());
87
0
}
88
89
0
secure_vector<uint8_t> Curve25519_PrivateKey::private_key_bits() const {
90
0
   return DER_Encoder().encode(m_private, ASN1_Type::OctetString).get_contents();
91
0
}
92
93
0
bool Curve25519_PrivateKey::check_key(RandomNumberGenerator& /*rng*/, bool /*strong*/) const {
94
0
   std::vector<uint8_t> public_point(32);
95
0
   curve25519_basepoint(public_point.data(), m_private.data());
96
0
   return public_point == m_public;
97
0
}
98
99
0
secure_vector<uint8_t> Curve25519_PrivateKey::agree(const uint8_t w[], size_t w_len) const {
100
0
   size_check(w_len, "public value");
101
0
   return curve25519(m_private, w);
102
0
}
103
104
namespace {
105
106
/**
107
* Curve25519 operation
108
*/
109
class Curve25519_KA_Operation final : public PK_Ops::Key_Agreement_with_KDF {
110
   public:
111
      Curve25519_KA_Operation(const Curve25519_PrivateKey& key, std::string_view kdf) :
112
0
            PK_Ops::Key_Agreement_with_KDF(kdf), m_key(key) {}
113
114
0
      size_t agreed_value_size() const override { return 32; }
115
116
0
      secure_vector<uint8_t> raw_agree(const uint8_t w[], size_t w_len) override { return m_key.agree(w, w_len); }
117
118
   private:
119
      const Curve25519_PrivateKey& m_key;
120
};
121
122
}  // namespace
123
124
std::unique_ptr<PK_Ops::Key_Agreement> Curve25519_PrivateKey::create_key_agreement_op(RandomNumberGenerator& /*rng*/,
125
                                                                                      std::string_view params,
126
0
                                                                                      std::string_view provider) const {
127
0
   if(provider == "base" || provider.empty()) {
128
0
      return std::make_unique<Curve25519_KA_Operation>(*this, params);
129
0
   }
130
0
   throw Provider_Not_Found(algo_name(), provider);
131
0
}
132
133
}  // namespace Botan