Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

curve25519.cpp
Go to the documentation of this file.
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
13 namespace Botan {
14
15 void curve25519_basepoint(uint8_t mypublic[32], const uint8_t secret[32])
16 {
17 const uint8_t basepoint[32] = { 9 };
18 curve25519_donna(mypublic, secret, basepoint);
19 }
20
21 namespace {
22
23 void size_check(size_t size, const char* thing)
24 {
25 if(size != 32)
26 throw Decoding_Error("Invalid size " + std::to_string(size) + " for Curve25519 " + thing);
27 }
28
29 secure_vector<uint8_t> curve25519(const secure_vector<uint8_t>& secret,
30 const uint8_t pubval[32])
31 {
32 secure_vector<uint8_t> out(32);
33 curve25519_donna(out.data(), secret.data(), pubval);
34 return out;
35 }
36
37 }
38
39 AlgorithmIdentifier Curve25519_PublicKey::algorithm_identifier() const
40 {
41 return AlgorithmIdentifier(get_oid(), AlgorithmIdentifier::USE_NULL_PARAM);
42 }
43
44 bool Curve25519_PublicKey::check_key(RandomNumberGenerator&, bool) const
45 {
46 return true; // no tests possible?
47 }
48
49 Curve25519_PublicKey::Curve25519_PublicKey(const AlgorithmIdentifier&,
50 const std::vector<uint8_t>& key_bits)
51 {
52 BER_Decoder(key_bits)
53 .start_cons(SEQUENCE)
54 .decode(m_public, OCTET_STRING)
55 .end_cons();
56
57 size_check(m_public.size(), "public key");
58 }
59
60 std::vector<uint8_t> Curve25519_PublicKey::public_key_bits() const
61 {
62 return DER_Encoder()
63 .start_cons(SEQUENCE)
64 .encode(m_public, OCTET_STRING)
65 .end_cons()
66 .get_contents_unlocked();
67 }
68
69 Curve25519_PrivateKey::Curve25519_PrivateKey(RandomNumberGenerator& rng)
70 {
71 m_private = rng.random_vec(32);
72 m_public.resize(32);
73 curve25519_basepoint(m_public.data(), m_private.data());
74 }
75
76 Curve25519_PrivateKey::Curve25519_PrivateKey(const AlgorithmIdentifier&,
77 const secure_vector<uint8_t>& key_bits)
78 {
79 BER_Decoder(key_bits)
80 .start_cons(SEQUENCE)
81 .decode(m_public, OCTET_STRING)
82 .decode(m_private, OCTET_STRING)
83 .end_cons();
84
85 size_check(m_public.size(), "public key");
86 size_check(m_private.size(), "private key");
87 }
88
89 secure_vector<uint8_t> Curve25519_PrivateKey::private_key_bits() const
90 {
91 return DER_Encoder()
92 .start_cons(SEQUENCE)
93 .encode(m_public, OCTET_STRING)
94 .encode(m_private, OCTET_STRING)
95 .end_cons()
96 .get_contents();
97 }
98
99 bool Curve25519_PrivateKey::check_key(RandomNumberGenerator&, bool) const
100 {
101 std::vector<uint8_t> public_point(32);
102 curve25519_basepoint(public_point.data(), m_private.data());
103 return public_point == m_public;
104 }
105
106 secure_vector<uint8_t> Curve25519_PrivateKey::agree(const uint8_t w[], size_t w_len) const
107 {
108 size_check(w_len, "public value");
109 return curve25519(m_private, w);
110 }
111
112 namespace {
113
114 /**
115 * Curve25519 operation
116 */
117 class Curve25519_KA_Operation : public PK_Ops::Key_Agreement_with_KDF
118 {
119 public:
120
121 Curve25519_KA_Operation(const Curve25519_PrivateKey& key, const std::string& kdf) :
122 PK_Ops::Key_Agreement_with_KDF(kdf),
123 m_key(key) {}
124
125 secure_vector<uint8_t> raw_agree(const uint8_t w[], size_t w_len) override
126 {
127 return m_key.agree(w, w_len);
128 }
129 private:
130 const Curve25519_PrivateKey& m_key;
131 };
132
133 }
134
135 std::unique_ptr<PK_Ops::Key_Agreement>
136 Curve25519_PrivateKey::create_key_agreement_op(RandomNumberGenerator& /*rng*/,
137 const std::string& params,
138 const std::string& provider) const
139 {
140 if(provider == "base" || provider.empty())
141 return std::unique_ptr<PK_Ops::Key_Agreement>(new Curve25519_KA_Operation(*this, params));
142 throw Provider_Not_Found(algo_name(), provider);
143 }
144
145 }
Botan::Curve25519_PrivateKey::check_key
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition: curve25519.cpp:99
Botan::RandomNumberGenerator::random_vec
secure_vector< uint8_t > random_vec(size_t bytes)
Definition: rng.h:133
Botan::DER_Encoder::get_contents_unlocked
std::vector< uint8_t > get_contents_unlocked()
Definition: der_enc.h:27
Botan::Curve25519_PublicKey::algo_name
std::string algo_name() const override
Definition: curve25519.h:18
Botan::RandomNumberGenerator
Definition: rng.h:25
Botan::DER_Encoder::get_contents
secure_vector< uint8_t > get_contents()
Definition: der_enc.cpp:124
Botan::Curve25519_PublicKey::algorithm_identifier
AlgorithmIdentifier algorithm_identifier() const override
Definition: curve25519.cpp:39
Botan::BER_Decoder::decode
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:376
Botan::ASN1::to_string
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:47
Botan::Curve25519_PublicKey::Curve25519_PublicKey
Curve25519_PublicKey()=default
Botan::DER_Encoder::end_cons
DER_Encoder & end_cons()
Definition: der_enc.cpp:147
Botan::Public_Key::get_oid
virtual OID get_oid() const
Definition: pk_keys.cpp:30
Botan::PK_Ops::Key_Agreement_with_KDF
Definition: pk_ops_impl.h:170
Botan::Curve25519_PublicKey::check_key
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition: curve25519.cpp:44
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:216
Botan::OCTET_STRING
Definition: asn1_obj.h:35
Botan::Curve25519_PublicKey::public_key_bits
std::vector< uint8_t > public_key_bits() const override
Definition: curve25519.cpp:60
Botan::DER_Encoder
Definition: der_enc.h:22
Botan::Curve25519_PrivateKey::private_key_bits
secure_vector< uint8_t > private_key_bits() const override
Definition: curve25519.cpp:89
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
Botan::BER_Decoder::end_cons
BER_Decoder & end_cons()
Definition: ber_dec.cpp:272
Botan::Curve25519_PrivateKey::create_key_agreement_op
std::unique_ptr< PK_Ops::Key_Agreement > create_key_agreement_op(RandomNumberGenerator &rng, const std::string &params, const std::string &provider) const override
Definition: curve25519.cpp:136
Botan::SEQUENCE
Definition: asn1_obj.h:39
Botan::BER_Decoder::start_cons
BER_Decoder start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: ber_dec.cpp:258
Botan::Curve25519_PrivateKey::Curve25519_PrivateKey
Curve25519_PrivateKey(const AlgorithmIdentifier &alg_id, const secure_vector< uint8_t > &key_bits)
Definition: curve25519.cpp:76
Botan
Definition: alg_id.cpp:13
Botan::Provider_Not_Found
Definition: exceptn.h:161
Botan::Curve25519_PublicKey::m_public
std::vector< uint8_t > m_public
Definition: curve25519.h:55
Botan::curve25519_basepoint
void curve25519_basepoint(uint8_t mypublic[32], const uint8_t secret[32])
Definition: curve25519.cpp:15
Botan::Curve25519_PrivateKey::agree
secure_vector< uint8_t > agree(const uint8_t w[], size_t w_len) const
Definition: curve25519.cpp:106
Botan::Curve25519_PrivateKey
Definition: curve25519.h:58
Botan::AlgorithmIdentifier
Definition: alg_id.h:20
Botan::DER_Encoder::start_cons
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:137
Botan::AlgorithmIdentifier::USE_NULL_PARAM
Definition: alg_id.h:23
Botan::BER_Decoder
Definition: ber_dec.h:19
Botan::curve25519_donna
void BOTAN_DLL curve25519_donna(uint8_t mypublic[32], const uint8_t secret[32], const uint8_t basepoint[32])
m_key
const Curve25519_PrivateKey & m_key
Definition: curve25519.cpp:130