/src/libtorrent/src/kademlia/ed25519.cpp
Line | Count | Source |
1 | | /* |
2 | | |
3 | | Copyright (c) 2016, Alden Torres |
4 | | Copyright (c) 2017, 2019-2020, Arvid Norberg |
5 | | All rights reserved. |
6 | | |
7 | | Redistribution and use in source and binary forms, with or without |
8 | | modification, are permitted provided that the following conditions |
9 | | are met: |
10 | | |
11 | | * Redistributions of source code must retain the above copyright |
12 | | notice, this list of conditions and the following disclaimer. |
13 | | * Redistributions in binary form must reproduce the above copyright |
14 | | notice, this list of conditions and the following disclaimer in |
15 | | the documentation and/or other materials provided with the distribution. |
16 | | * Neither the name of the author nor the names of its |
17 | | contributors may be used to endorse or promote products derived |
18 | | from this software without specific prior written permission. |
19 | | |
20 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
21 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
22 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
23 | | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
24 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
25 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
26 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
27 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
28 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
29 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
30 | | POSSIBILITY OF SUCH DAMAGE. |
31 | | |
32 | | */ |
33 | | |
34 | | #include <libtorrent/kademlia/ed25519.hpp> |
35 | | #include <libtorrent/random.hpp> |
36 | | #include <libtorrent/aux_/ed25519.hpp> |
37 | | |
38 | | namespace libtorrent { namespace dht { |
39 | | |
40 | | std::array<char, 32> ed25519_create_seed() |
41 | 0 | { |
42 | 0 | std::array<char, 32> seed; |
43 | 0 | aux::crypto_random_bytes(seed); |
44 | 0 | return seed; |
45 | 0 | } |
46 | | |
47 | | std::tuple<public_key, secret_key> ed25519_create_keypair( |
48 | | std::array<char, 32> const& seed) |
49 | 0 | { |
50 | 0 | public_key pk; |
51 | 0 | secret_key sk; |
52 | |
|
53 | 0 | auto const pk_ptr = reinterpret_cast<unsigned char*>(pk.bytes.data()); |
54 | 0 | auto const sk_ptr = reinterpret_cast<unsigned char*>(sk.bytes.data()); |
55 | 0 | auto const seed_ptr = reinterpret_cast<unsigned char const*>(seed.data()); |
56 | |
|
57 | 0 | lt::aux::ed25519_create_keypair(pk_ptr, sk_ptr, seed_ptr); |
58 | |
|
59 | 0 | return std::make_tuple(pk, sk); |
60 | 0 | } |
61 | | |
62 | | signature ed25519_sign(span<char const> msg |
63 | | , public_key const& pk, secret_key const& sk) |
64 | 0 | { |
65 | 0 | signature sig; |
66 | |
|
67 | 0 | auto const sig_ptr = reinterpret_cast<unsigned char*>(sig.bytes.data()); |
68 | 0 | auto const msg_ptr = reinterpret_cast<unsigned char const*>(msg.data()); |
69 | 0 | auto const pk_ptr = reinterpret_cast<unsigned char const*>(pk.bytes.data()); |
70 | 0 | auto const sk_ptr = reinterpret_cast<unsigned char const*>(sk.bytes.data()); |
71 | |
|
72 | 0 | lt::aux::ed25519_sign(sig_ptr, msg_ptr, msg.size(), pk_ptr, sk_ptr); |
73 | |
|
74 | 0 | return sig; |
75 | 0 | } |
76 | | |
77 | | bool ed25519_verify(signature const& sig |
78 | | , span<char const> msg, public_key const& pk) |
79 | 0 | { |
80 | 0 | auto const sig_ptr = reinterpret_cast<unsigned char const*>(sig.bytes.data()); |
81 | 0 | auto const msg_ptr = reinterpret_cast<unsigned char const*>(msg.data()); |
82 | 0 | auto const pk_ptr = reinterpret_cast<unsigned char const*>(pk.bytes.data()); |
83 | |
|
84 | 0 | return lt::aux::ed25519_verify(sig_ptr, msg_ptr, msg.size(), pk_ptr) == 1; |
85 | 0 | } |
86 | | |
87 | | public_key ed25519_add_scalar(public_key const& pk |
88 | | , std::array<char, 32> const& scalar) |
89 | 0 | { |
90 | 0 | public_key ret(pk.bytes.data()); |
91 | |
|
92 | 0 | auto const ret_ptr = reinterpret_cast<unsigned char*>(ret.bytes.data()); |
93 | 0 | auto const scalar_ptr = reinterpret_cast<unsigned char const*>(scalar.data()); |
94 | |
|
95 | 0 | lt::aux::ed25519_add_scalar(ret_ptr, nullptr, scalar_ptr); |
96 | |
|
97 | 0 | return ret; |
98 | 0 | } |
99 | | |
100 | | secret_key ed25519_add_scalar(secret_key const& sk |
101 | | , std::array<char, 32> const& scalar) |
102 | 0 | { |
103 | 0 | secret_key ret(sk.bytes.data()); |
104 | |
|
105 | 0 | auto const ret_ptr = reinterpret_cast<unsigned char*>(ret.bytes.data()); |
106 | 0 | auto const scalar_ptr = reinterpret_cast<unsigned char const*>(scalar.data()); |
107 | |
|
108 | 0 | lt::aux::ed25519_add_scalar(nullptr, ret_ptr, scalar_ptr); |
109 | |
|
110 | 0 | return ret; |
111 | 0 | } |
112 | | |
113 | | std::array<char, 32> ed25519_key_exchange( |
114 | | public_key const& pk, secret_key const& sk) |
115 | 0 | { |
116 | 0 | std::array<char, 32> secret; |
117 | |
|
118 | 0 | auto const secret_ptr = reinterpret_cast<unsigned char*>(secret.data()); |
119 | 0 | auto const pk_ptr = reinterpret_cast<unsigned char const*>(pk.bytes.data()); |
120 | 0 | auto const sk_ptr = reinterpret_cast<unsigned char const*>(sk.bytes.data()); |
121 | |
|
122 | 0 | lt::aux::ed25519_key_exchange(secret_ptr, pk_ptr, sk_ptr); |
123 | |
|
124 | 0 | return secret; |
125 | 0 | } |
126 | | |
127 | | }} |