Coverage Report

Created: 2020-06-30 13:58

/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
#include <botan/internal/pk_ops_impl.h>
10
#include <botan/ber_dec.h>
11
#include <botan/der_enc.h>
12
#include <botan/rng.h>
13
14
namespace Botan {
15
16
void curve25519_basepoint(uint8_t mypublic[32], const uint8_t secret[32])
17
432
   {
18
432
   const uint8_t basepoint[32] = { 9 };
19
432
   curve25519_donna(mypublic, secret, basepoint);
20
432
   }
21
22
namespace {
23
24
void size_check(size_t size, const char* thing)
25
57
   {
26
57
   if(size != 32)
27
43
      throw Decoding_Error("Invalid size " + std::to_string(size) + " for Curve25519 " + thing);
28
57
   }
29
30
secure_vector<uint8_t> curve25519(const secure_vector<uint8_t>& secret,
31
                               const uint8_t pubval[32])
32
9
   {
33
9
   secure_vector<uint8_t> out(32);
34
9
   curve25519_donna(out.data(), secret.data(), pubval);
35
9
   return out;
36
9
   }
37
38
}
39
40
AlgorithmIdentifier Curve25519_PublicKey::algorithm_identifier() const
41
0
   {
42
0
   return AlgorithmIdentifier(get_oid(), AlgorithmIdentifier::USE_EMPTY_PARAM);
43
0
   }
44
45
bool Curve25519_PublicKey::check_key(RandomNumberGenerator&, bool) const
46
0
   {
47
0
   return true; // no tests possible?
48
0
   }
49
50
Curve25519_PublicKey::Curve25519_PublicKey(const AlgorithmIdentifier&,
51
                                           const std::vector<uint8_t>& key_bits)
52
5
   {
53
5
   m_public = key_bits;
54
5
55
5
   size_check(m_public.size(), "public key");
56
5
   }
Unexecuted instantiation: Botan::Curve25519_PublicKey::Curve25519_PublicKey(Botan::AlgorithmIdentifier const&, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&)
Botan::Curve25519_PublicKey::Curve25519_PublicKey(Botan::AlgorithmIdentifier const&, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&)
Line
Count
Source
52
5
   {
53
5
   m_public = key_bits;
54
5
55
5
   size_check(m_public.size(), "public key");
56
5
   }
57
58
std::vector<uint8_t> Curve25519_PublicKey::public_key_bits() const
59
0
   {
60
0
   return m_public;
61
0
   }
62
63
Curve25519_PrivateKey::Curve25519_PrivateKey(const secure_vector<uint8_t>& secret_key)
64
0
   {
65
0
   if(secret_key.size() != 32)
66
0
     throw Decoding_Error("Invalid size for Curve25519 private key");
67
0
68
0
   m_public.resize(32);
69
0
   m_private = secret_key;
70
0
   curve25519_basepoint(m_public.data(), m_private.data());
71
0
   }
Unexecuted instantiation: Botan::Curve25519_PrivateKey::Curve25519_PrivateKey(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > const&)
Unexecuted instantiation: Botan::Curve25519_PrivateKey::Curve25519_PrivateKey(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > const&)
72
73
Curve25519_PrivateKey::Curve25519_PrivateKey(RandomNumberGenerator& rng)
74
431
   {
75
431
   m_private = rng.random_vec(32);
76
431
   m_public.resize(32);
77
431
   curve25519_basepoint(m_public.data(), m_private.data());
78
431
   }
Unexecuted instantiation: Botan::Curve25519_PrivateKey::Curve25519_PrivateKey(Botan::RandomNumberGenerator&)
Botan::Curve25519_PrivateKey::Curve25519_PrivateKey(Botan::RandomNumberGenerator&)
Line
Count
Source
74
431
   {
75
431
   m_private = rng.random_vec(32);
76
431
   m_public.resize(32);
77
431
   curve25519_basepoint(m_public.data(), m_private.data());
78
431
   }
79
80
Curve25519_PrivateKey::Curve25519_PrivateKey(const AlgorithmIdentifier&,
81
                                             const secure_vector<uint8_t>& key_bits)
82
40
   {
83
40
   BER_Decoder(key_bits).decode(m_private, OCTET_STRING).discard_remaining();
84
40
85
40
   size_check(m_private.size(), "private key");
86
40
   m_public.resize(32);
87
40
   curve25519_basepoint(m_public.data(), m_private.data());
88
40
   }
Unexecuted instantiation: Botan::Curve25519_PrivateKey::Curve25519_PrivateKey(Botan::AlgorithmIdentifier const&, std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > const&)
Botan::Curve25519_PrivateKey::Curve25519_PrivateKey(Botan::AlgorithmIdentifier const&, std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > const&)
Line
Count
Source
82
40
   {
83
40
   BER_Decoder(key_bits).decode(m_private, OCTET_STRING).discard_remaining();
84
40
85
40
   size_check(m_private.size(), "private key");
86
40
   m_public.resize(32);
87
40
   curve25519_basepoint(m_public.data(), m_private.data());
88
40
   }
89
90
secure_vector<uint8_t> Curve25519_PrivateKey::private_key_bits() const
91
0
   {
92
0
   return DER_Encoder().encode(m_private, OCTET_STRING).get_contents();
93
0
   }
94
95
bool Curve25519_PrivateKey::check_key(RandomNumberGenerator&, bool) const
96
0
   {
97
0
   std::vector<uint8_t> public_point(32);
98
0
   curve25519_basepoint(public_point.data(), m_private.data());
99
0
   return public_point == m_public;
100
0
   }
101
102
secure_vector<uint8_t> Curve25519_PrivateKey::agree(const uint8_t w[], size_t w_len) const
103
13
   {
104
13
   size_check(w_len, "public value");
105
13
   return curve25519(m_private, w);
106
13
   }
107
108
namespace {
109
110
/**
111
* Curve25519 operation
112
*/
113
class Curve25519_KA_Operation final : public PK_Ops::Key_Agreement_with_KDF
114
   {
115
   public:
116
117
      Curve25519_KA_Operation(const Curve25519_PrivateKey& key, const std::string& kdf) :
118
         PK_Ops::Key_Agreement_with_KDF(kdf),
119
13
         m_key(key) {}
120
121
0
      size_t agreed_value_size() const override { return 32; }
122
123
      secure_vector<uint8_t> raw_agree(const uint8_t w[], size_t w_len) override
124
13
         {
125
13
         return m_key.agree(w, w_len);
126
13
         }
127
   private:
128
      const Curve25519_PrivateKey& m_key;
129
   };
130
131
}
132
133
std::unique_ptr<PK_Ops::Key_Agreement>
134
Curve25519_PrivateKey::create_key_agreement_op(RandomNumberGenerator& /*rng*/,
135
                                               const std::string& params,
136
                                               const std::string& provider) const
137
13
   {
138
13
   if(provider == "base" || provider.empty())
139
13
      return std::unique_ptr<PK_Ops::Key_Agreement>(new Curve25519_KA_Operation(*this, params));
140
0
   throw Provider_Not_Found(algo_name(), provider);
141
0
   }
142
143
}