New serialized object, public key, and private key interfaces

This introduces functions get and set, and a family of specialized
structs called STExchange. These interfaces allow efficient and
seamless interchange between serialized object fields and user
defined types, especially variable length objects.

A new base class template TypedField is mixed into existing SField
declarations to encode information on the field, allowing template
metaprograms to both customize interchange based on the type and
detect misuse at compile-time.

New types AnyPublicKey and AnySecretKey are introduced. These are
intended to replace the corresponding functionality in the deprecated
class RippleAddress. Specializations of STExchange for these types
are provided to allow interchange. New free functions verify and sign
allow signature verification and signature generation for serialized
objects.

* Add Buffer and Slice primitives
* Add TypedField and modify some SField
* Add STExchange and specializations for STBlob and STInteger
* Improve STBlob and STInteger to support STExchange
* Expose raw data in RippleAddress and Serializer
This commit is contained in:
Vinnie Falco
2015-02-19 18:00:05 -08:00
committed by Tom Ritchford
parent 79ce4ed226
commit a2acffdfa3
20 changed files with 1277 additions and 65 deletions

View File

@@ -0,0 +1,93 @@
//------------------------------------------------------------------------------
/*
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;
}
} // ripple

View File

@@ -0,0 +1,143 @@
//------------------------------------------------------------------------------
/*
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() + 1, &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() + 1;
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

View File

@@ -41,7 +41,6 @@ typedef std::lock_guard <std::mutex> StaticScopedLockType;
// Give this translation unit only, permission to construct SFields
struct SField::make
{
#ifndef _MSC_VER
template <class ...Args>
static SField one(SField const* p, Args&& ...args)
{
@@ -49,45 +48,14 @@ struct SField::make
knownCodeToField[result.fieldCode] = p;
return result;
}
#else // remove this when VS gets variadic templates
template <class A0>
static SField one(SField const* p, A0&& arg0)
{
SField result(std::forward<A0>(arg0));
knownCodeToField[result.fieldCode] = p;
return result;
}
template <class A0, class A1, class A2>
static SField one(SField const* p, A0&& arg0, A1&& arg1, A2&& arg2)
template <class T, class ...Args>
static TypedField<T> one(SField const* p, Args&& ...args)
{
SField result(std::forward<A0>(arg0), std::forward<A1>(arg1),
std::forward<A2>(arg2));
TypedField<T> result(std::forward<Args>(args)...);
knownCodeToField[result.fieldCode] = p;
return result;
}
template <class A0, class A1, class A2, class A3>
static SField one(SField const* p, A0&& arg0, A1&& arg1, A2&& arg2,
A3&& arg3)
{
SField result(std::forward<A0>(arg0), std::forward<A1>(arg1),
std::forward<A2>(arg2), std::forward<A3>(arg3));
knownCodeToField[result.fieldCode] = p;
return result;
}
template <class A0, class A1, class A2, class A3, class A4>
static SField one(SField const* p, A0&& arg0, A1&& arg1, A2&& arg2,
A3&& arg3, A4&& arg4)
{
SField result(std::forward<A0>(arg0), std::forward<A1>(arg1),
std::forward<A2>(arg2), std::forward<A3>(arg3),
std::forward<A4>(arg4));
knownCodeToField[result.fieldCode] = p;
return result;
}
#endif
};
using make = SField::make;
@@ -116,7 +84,7 @@ SField const sfTransactionType = make::one(&sfTransactionType, STI_UINT16, 2, "T
// 32-bit integers (common)
SField const sfFlags = make::one(&sfFlags, STI_UINT32, 2, "Flags");
SField const sfSourceTag = make::one(&sfSourceTag, STI_UINT32, 3, "SourceTag");
SField const sfSequence = make::one(&sfSequence, STI_UINT32, 4, "Sequence");
TypedField<STInteger<std::uint32_t>> const sfSequence = make::one<STInteger<std::uint32_t>>(&sfSequence, STI_UINT32, 4, "Sequence");
SField const sfPreviousTxnLgrSeq = make::one(&sfPreviousTxnLgrSeq, STI_UINT32, 5, "PreviousTxnLgrSeq", SField::sMD_DeleteFinal);
SField const sfLedgerSequence = make::one(&sfLedgerSequence, STI_UINT32, 6, "LedgerSequence");
SField const sfCloseTime = make::one(&sfCloseTime, STI_UINT32, 7, "CloseTime");
@@ -203,20 +171,20 @@ SField const sfRippleEscrow = make::one(&sfRippleEscrow, STI_AMOUNT, 17, "
SField const sfDeliveredAmount = make::one(&sfDeliveredAmount, STI_AMOUNT, 18, "DeliveredAmount");
// variable length
SField const sfPublicKey = make::one(&sfPublicKey, STI_VL, 1, "PublicKey");
SField const sfMessageKey = make::one(&sfMessageKey, STI_VL, 2, "MessageKey");
SField const sfSigningPubKey = make::one(&sfSigningPubKey, STI_VL, 3, "SigningPubKey");
SField const sfTxnSignature = make::one(&sfTxnSignature, STI_VL, 4, "TxnSignature", SField::sMD_Default, false);
SField const sfGenerator = make::one(&sfGenerator, STI_VL, 5, "Generator");
SField const sfSignature = make::one(&sfSignature, STI_VL, 6, "Signature", SField::sMD_Default, false);
SField const sfDomain = make::one(&sfDomain, STI_VL, 7, "Domain");
SField const sfFundCode = make::one(&sfFundCode, STI_VL, 8, "FundCode");
SField const sfRemoveCode = make::one(&sfRemoveCode, STI_VL, 9, "RemoveCode");
SField const sfExpireCode = make::one(&sfExpireCode, STI_VL, 10, "ExpireCode");
SField const sfCreateCode = make::one(&sfCreateCode, STI_VL, 11, "CreateCode");
SField const sfMemoType = make::one(&sfMemoType, STI_VL, 12, "MemoType");
SField const sfMemoData = make::one(&sfMemoData, STI_VL, 13, "MemoData");
SField const sfMemoFormat = make::one(&sfMemoFormat, STI_VL, 14, "MemoFormat");
TypedField<STBlob> const sfPublicKey = make::one<STBlob>(&sfPublicKey, STI_VL, 1, "PublicKey");
TypedField<STBlob> const sfSigningPubKey = make::one<STBlob>(&sfSigningPubKey, STI_VL, 3, "SigningPubKey");
TypedField<STBlob> const sfSignature = make::one<STBlob>(&sfSignature, STI_VL, 6, "Signature", SField::sMD_Default, false);
SField const sfMessageKey = make::one(&sfMessageKey, STI_VL, 2, "MessageKey");
SField const sfTxnSignature = make::one(&sfTxnSignature, STI_VL, 4, "TxnSignature", SField::sMD_Default, false);
SField const sfGenerator = make::one(&sfGenerator, STI_VL, 5, "Generator");
SField const sfDomain = make::one(&sfDomain, STI_VL, 7, "Domain");
SField const sfFundCode = make::one(&sfFundCode, STI_VL, 8, "FundCode");
SField const sfRemoveCode = make::one(&sfRemoveCode, STI_VL, 9, "RemoveCode");
SField const sfExpireCode = make::one(&sfExpireCode, STI_VL, 10, "ExpireCode");
SField const sfCreateCode = make::one(&sfCreateCode, STI_VL, 11, "CreateCode");
SField const sfMemoType = make::one(&sfMemoType, STI_VL, 12, "MemoType");
SField const sfMemoData = make::one(&sfMemoData, STI_VL, 13, "MemoData");
SField const sfMemoFormat = make::one(&sfMemoFormat, STI_VL, 14, "MemoFormat");
// account
SField const sfAccount = make::one(&sfAccount, STI_ACCOUNT, 1, "Account");

View File

@@ -762,6 +762,24 @@ const STVector256& STObject::getFieldV256 (SField::ref field) const
return getFieldByConstRef <STVector256> (field, empty);
}
void
STObject::set (std::unique_ptr<STBase> v)
{
auto const i =
getFieldIndex(v->getFName());
if (i != -1)
{
mData.replace(i, v.release());
}
else
{
if (! isFree())
throw std::runtime_error(
"missing field in templated STObject");
mData.push_back(v.release());
}
}
void STObject::setFieldU8 (SField::ref field, unsigned char v)
{
setFieldUsingSetValue <STUInt8> (field, v);

View File

@@ -0,0 +1,51 @@
//------------------------------------------------------------------------------
/*
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/Sign.h>
namespace ripple {
void
sign (STObject& st, HashPrefix const& prefix,
AnySecretKey const& sk)
{
Serializer ss;
ss.add32(prefix);
st.add(ss, false);
set(st, sfSignature,
sk.sign(ss.data(), ss.size()));
}
bool
verify (STObject const& st,
HashPrefix const& prefix,
AnyPublicKeySlice const& pk)
{
auto const sig = get(st, sfSignature);
if (! sig)
return false;
Serializer ss;
ss.add32(prefix);
st.add(ss, false);
return pk.verify(
ss.data(), ss.size(),
sig->data(), sig->size());
}
} // ripple