/* Copyright (c) 2016, Arvid Norberg, Alden Torres All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include namespace libtorrent { namespace dht { void ed25519_create_seed(std::array& seed) { aux::random_bytes(seed); } void ed25519_create_keypair(public_key& pk , secret_key& sk, std::array const& seed) { auto const pk_ptr = reinterpret_cast(pk.bytes.data()); auto const sk_ptr = reinterpret_cast(sk.bytes.data()); auto const seed_ptr = reinterpret_cast(seed.data()); libtorrent::ed25519_create_keypair(pk_ptr, sk_ptr, seed_ptr); } void ed25519_sign(signature& sig, span msg , public_key const& pk, secret_key const& sk) { auto const sig_ptr = reinterpret_cast(sig.bytes.data()); auto const msg_ptr = reinterpret_cast(msg.data()); auto const pk_ptr = reinterpret_cast(pk.bytes.data()); auto const sk_ptr = reinterpret_cast(sk.bytes.data()); libtorrent::ed25519_sign(sig_ptr, msg_ptr, msg.size(), pk_ptr, sk_ptr); } bool ed25519_verify(signature const& sig , span msg, public_key const& pk) { auto const sig_ptr = reinterpret_cast(sig.bytes.data()); auto const msg_ptr = reinterpret_cast(msg.data()); auto const pk_ptr = reinterpret_cast(pk.bytes.data()); return libtorrent::ed25519_verify(sig_ptr, msg_ptr, msg.size(), pk_ptr) == 1; } void ed25519_add_scalar(std::shared_ptr pk , std::shared_ptr sk, std::array const& scalar) { auto const pk_ptr = pk ? reinterpret_cast(pk->bytes.data()) : nullptr; auto const sk_ptr = sk ? reinterpret_cast(sk->bytes.data()) : nullptr; auto const scalar_ptr = reinterpret_cast(scalar.data()); libtorrent::ed25519_add_scalar(pk_ptr, sk_ptr, scalar_ptr); } void ed25519_key_exchange(std::array& secret , public_key const& pk, secret_key const& sk) { auto const secret_ptr = reinterpret_cast(secret.data()); auto const pk_ptr = reinterpret_cast(pk.bytes.data()); auto const sk_ptr = reinterpret_cast(sk.bytes.data()); libtorrent::ed25519_key_exchange(secret_ptr, pk_ptr, sk_ptr); } }}