mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Use SecretKey, PublicKey
This commit is contained in:
committed by
Nik Bougalis
parent
6fccd07479
commit
163e8eb8fc
@@ -19,7 +19,7 @@
|
||||
|
||||
#include <BeastConfig.h>
|
||||
#include <ripple/protocol/AccountID.h>
|
||||
#include <ripple/protocol/AnyPublicKey.h>
|
||||
#include <ripple/protocol/PublicKey.h>
|
||||
#include <ripple/protocol/digest.h>
|
||||
#include <ripple/protocol/tokens.h>
|
||||
#include <cstring>
|
||||
@@ -139,9 +139,8 @@ parseHexOrBase58 (std::string const& s)
|
||||
less secure than Bitcoin. So where there was no good reason
|
||||
to change something, it was not changed."
|
||||
*/
|
||||
|
||||
AccountID
|
||||
calcAccountID (AnyPublicKey const& pk)
|
||||
calcAccountID (PublicKey const& pk)
|
||||
{
|
||||
ripesha_hasher rsh;
|
||||
rsh(pk.data(), pk.size());
|
||||
|
||||
@@ -1,104 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <ripple/protocol/AnyPublicKey.h>
|
||||
#include <ripple/protocol/Serializer.h>
|
||||
#include <ripple/protocol/STExchange.h>
|
||||
#include <ed25519-donna/ed25519.h>
|
||||
#include <cassert>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
/** Verify a secp256k1 signature. */
|
||||
bool
|
||||
verify_secp256k1 (void const* pk,
|
||||
void const* msg, std::size_t msg_size,
|
||||
void const* sig, std::size_t sig_size)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
verify_ed25519 (void const* pk,
|
||||
void const* msg, std::size_t msg_size,
|
||||
void const* sig, std::size_t sig_size)
|
||||
{
|
||||
if (sig_size != 64)
|
||||
return false;
|
||||
ed25519_public_key epk;
|
||||
ed25519_signature es;
|
||||
std::memcpy(epk, pk, 32);
|
||||
std::memcpy(es, sig, sig_size);
|
||||
return ed25519_sign_open(
|
||||
reinterpret_cast<unsigned char const*>(msg),
|
||||
msg_size, epk, es) == 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
KeyType
|
||||
AnyPublicKeySlice::type() const noexcept
|
||||
{
|
||||
auto const pk = data();
|
||||
auto const pk_size = size();
|
||||
|
||||
if (pk_size < 1)
|
||||
return KeyType::unknown;
|
||||
auto const len = pk_size - 1;
|
||||
if (len == 32 &&
|
||||
pk[0] == 0xED)
|
||||
return KeyType::ed25519;
|
||||
if (len == 33 &&
|
||||
(pk[0] == 0x02 || pk[0] == 0x03))
|
||||
return KeyType::secp256k1;
|
||||
return KeyType::unknown;
|
||||
}
|
||||
|
||||
bool
|
||||
AnyPublicKeySlice::verify (
|
||||
void const* msg, std::size_t msg_size,
|
||||
void const* sig, std::size_t sig_size) const
|
||||
{
|
||||
switch(type())
|
||||
{
|
||||
case KeyType::ed25519:
|
||||
return verify_ed25519(data() + 1,
|
||||
msg, msg_size, sig, sig_size);
|
||||
case KeyType::secp256k1:
|
||||
return verify_secp256k1(data() + 1,
|
||||
msg, msg_size, sig, sig_size);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// throw?
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string
|
||||
toString (AnyPublicKey const& pk)
|
||||
{
|
||||
Blob buffer;
|
||||
buffer.reserve (1 + pk.size ());
|
||||
buffer.push_back (TOKEN_NODE_PUBLIC);
|
||||
auto const data = pk.data ();
|
||||
buffer.insert (buffer.end (), data, data + pk.size ());
|
||||
return Base58::encodeWithCheck (buffer);
|
||||
}
|
||||
|
||||
} // ripple
|
||||
@@ -1,143 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <ripple/protocol/AnySecretKey.h>
|
||||
#include <ripple/protocol/RippleAddress.h>
|
||||
#include <ripple/protocol/Serializer.h>
|
||||
#include <ripple/crypto/RandomNumbers.h>
|
||||
#include <ed25519-donna/ed25519.h>
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
AnySecretKey::~AnySecretKey()
|
||||
{
|
||||
// secure erase
|
||||
std::fill(p_.data(), p_.data() + p_.size(), 0);
|
||||
}
|
||||
|
||||
AnySecretKey::AnySecretKey (AnySecretKey&& other)
|
||||
: p_ (std::move(other.p_))
|
||||
, type_ (other.type_)
|
||||
{
|
||||
other.type_ = KeyType::unknown;
|
||||
}
|
||||
|
||||
AnySecretKey&
|
||||
AnySecretKey::operator= (AnySecretKey&& other)
|
||||
{
|
||||
p_ = std::move(other.p_);
|
||||
type_ = other.type_;
|
||||
other.type_ = KeyType::unknown;
|
||||
return *this;
|
||||
}
|
||||
|
||||
AnySecretKey::AnySecretKey (KeyType type,
|
||||
void const* data, std::size_t size)
|
||||
: p_ (data, size)
|
||||
, type_ (type)
|
||||
{
|
||||
if (type_ == KeyType::unknown)
|
||||
throw std::runtime_error(
|
||||
"AnySecretKey: unknown type");
|
||||
if (type_ == KeyType::ed25519 &&
|
||||
size != 32)
|
||||
throw std::runtime_error(
|
||||
"AnySecretKey: wrong ed25519 size");
|
||||
if (type_ == KeyType::secp256k1 &&
|
||||
size != 32)
|
||||
throw std::runtime_error(
|
||||
"AnySecretKey: wrong secp256k1 size");
|
||||
}
|
||||
|
||||
AnyPublicKey
|
||||
AnySecretKey::publicKey() const
|
||||
{
|
||||
switch (type())
|
||||
{
|
||||
case KeyType::ed25519:
|
||||
{
|
||||
unsigned char buf[33];
|
||||
buf[0] = 0xED;
|
||||
ed25519_publickey(p_.data(), &buf[1]);
|
||||
return AnyPublicKey(buf, sizeof(buf));
|
||||
}
|
||||
default:
|
||||
throw std::runtime_error(
|
||||
"AnySecretKey: unknown type");
|
||||
};
|
||||
}
|
||||
|
||||
Buffer
|
||||
AnySecretKey::sign (
|
||||
void const* msg, std::size_t msg_len) const
|
||||
{
|
||||
switch(type_)
|
||||
{
|
||||
case KeyType::ed25519:
|
||||
{
|
||||
auto const sk = p_.data();
|
||||
ed25519_public_key pk;
|
||||
ed25519_publickey(sk, pk);
|
||||
Buffer b(64);
|
||||
ed25519_sign(reinterpret_cast<
|
||||
unsigned char const*>(msg), msg_len,
|
||||
sk, pk, b.data());
|
||||
return b;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
throw std::runtime_error(
|
||||
"AnySecretKey: unknown type");
|
||||
}
|
||||
|
||||
AnySecretKey
|
||||
AnySecretKey::make_ed25519()
|
||||
{
|
||||
std::uint8_t buf[32];
|
||||
random_fill(&buf[0], sizeof(buf));
|
||||
AnySecretKey ask(KeyType::ed25519,
|
||||
buf, sizeof(buf));
|
||||
// secure erase
|
||||
std::fill(buf, buf + sizeof(buf), 0);
|
||||
return ask;
|
||||
}
|
||||
|
||||
std::pair<AnySecretKey, AnyPublicKey>
|
||||
AnySecretKey::make_secp256k1_pair()
|
||||
{
|
||||
// VFALCO What a pile
|
||||
RippleAddress s;
|
||||
s.setSeedRandom();
|
||||
RippleAddress const g =
|
||||
RippleAddress::createGeneratorPublic(s);
|
||||
RippleAddress sk;
|
||||
sk.setAccountPrivate (g, s, 0);
|
||||
RippleAddress pk;
|
||||
pk.setAccountPublic (g, 0);
|
||||
return std::pair<AnySecretKey, AnyPublicKey>(
|
||||
std::piecewise_construct, std::make_tuple(
|
||||
KeyType::secp256k1, sk.data(), sk.size()),
|
||||
std::make_tuple(pk.data(), pk.size()));
|
||||
}
|
||||
|
||||
} // ripple
|
||||
@@ -17,6 +17,7 @@
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <BeastConfig.h>
|
||||
#include <ripple/protocol/STAccount.h>
|
||||
#include <ripple/protocol/STAmount.h>
|
||||
#include <ripple/protocol/STArray.h>
|
||||
|
||||
@@ -17,25 +17,27 @@
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <BeastConfig.h>
|
||||
#include <ripple/protocol/Sign.h>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
void
|
||||
sign (STObject& st, HashPrefix const& prefix,
|
||||
AnySecretKey const& sk)
|
||||
KeyType type, SecretKey const& sk)
|
||||
{
|
||||
Serializer ss;
|
||||
ss.add32(prefix);
|
||||
st.addWithoutSigningFields(ss);
|
||||
set(st, sfSignature,
|
||||
sk.sign(ss.data(), ss.size()));
|
||||
sign(type, sk, ss.slice()));
|
||||
}
|
||||
|
||||
bool
|
||||
verify (STObject const& st,
|
||||
HashPrefix const& prefix,
|
||||
AnyPublicKeySlice const& pk)
|
||||
PublicKey const& pk,
|
||||
bool mustBeFullyCanonical)
|
||||
{
|
||||
auto const sig = get(st, sfSignature);
|
||||
if (! sig)
|
||||
@@ -44,8 +46,9 @@ verify (STObject const& st,
|
||||
ss.add32(prefix);
|
||||
st.addWithoutSigningFields(ss);
|
||||
return pk.verify(
|
||||
ss.data(), ss.size(),
|
||||
sig->data(), sig->size());
|
||||
Slice(ss.data(), ss.size()),
|
||||
Slice(sig->data(), sig->size()),
|
||||
true);
|
||||
}
|
||||
|
||||
} // ripple
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <BeastConfig.h>
|
||||
#include <ripple/protocol/tokens.h>
|
||||
#include <ripple/protocol/digest.h>
|
||||
#include <cassert>
|
||||
|
||||
Reference in New Issue
Block a user