mirror of
https://github.com/matrix-construct/construct
synced 2024-10-15 03:58:53 +02:00
ircd::json: Add a preliminary suite of sign()/verify()/hash() for tuple.
This commit is contained in:
parent
f20ff13a44
commit
9bc7587557
1 changed files with 92 additions and 0 deletions
|
@ -64,6 +64,7 @@ struct tuple
|
||||||
using super_type = tuple<T...>;
|
using super_type = tuple<T...>;
|
||||||
|
|
||||||
operator json::value() const;
|
operator json::value() const;
|
||||||
|
operator crh::sha256::buf() const;
|
||||||
|
|
||||||
static constexpr size_t size();
|
static constexpr size_t size();
|
||||||
|
|
||||||
|
@ -995,6 +996,97 @@ operator<<(std::ostream &s, const tuple<T...> &t)
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class... T>
|
||||||
|
tuple<T...>::operator
|
||||||
|
crh::sha256::buf()
|
||||||
|
const
|
||||||
|
{
|
||||||
|
//TODO: XXX
|
||||||
|
const auto preimage
|
||||||
|
{
|
||||||
|
json::strung(*this)
|
||||||
|
};
|
||||||
|
|
||||||
|
return crh::sha256::buf
|
||||||
|
{
|
||||||
|
[&preimage](auto &buf)
|
||||||
|
{
|
||||||
|
sha256{buf, const_raw_buffer{preimage}};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class tuple>
|
||||||
|
enable_if_tuple<tuple, ed25519::sig>
|
||||||
|
sign(const tuple &t,
|
||||||
|
const ed25519::sk &sk)
|
||||||
|
{
|
||||||
|
//TODO: XXX
|
||||||
|
const auto preimage
|
||||||
|
{
|
||||||
|
json::strung(t)
|
||||||
|
};
|
||||||
|
|
||||||
|
return ed25519::sig
|
||||||
|
{
|
||||||
|
[&sk, &preimage](auto &buf)
|
||||||
|
{
|
||||||
|
sk.sign(buf, const_raw_buffer{preimage});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class tuple>
|
||||||
|
enable_if_tuple<tuple, bool>
|
||||||
|
verify(const tuple &t,
|
||||||
|
const ed25519::pk &pk,
|
||||||
|
const ed25519::sig &sig,
|
||||||
|
std::nothrow_t)
|
||||||
|
noexcept try
|
||||||
|
{
|
||||||
|
//TODO: XXX
|
||||||
|
const auto preimage
|
||||||
|
{
|
||||||
|
json::strung(t)
|
||||||
|
};
|
||||||
|
|
||||||
|
return pk.verify(const_raw_buffer{preimage}, sig);
|
||||||
|
}
|
||||||
|
catch(const std::exception &e)
|
||||||
|
{
|
||||||
|
log::error("Verification of json::tuple unexpected failure: %s", e.what());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class tuple>
|
||||||
|
enable_if_tuple<tuple, void>
|
||||||
|
verify(const tuple &t,
|
||||||
|
const ed25519::pk &pk,
|
||||||
|
const ed25519::sig &sig)
|
||||||
|
{
|
||||||
|
if(!verify(t, pk, sig, std::nothrow))
|
||||||
|
throw ed25519::bad_sig{"Verification failed"};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class tuple>
|
||||||
|
enable_if_tuple<tuple, void>
|
||||||
|
verify(const tuple &t,
|
||||||
|
const ed25519::pk &pk)
|
||||||
|
{
|
||||||
|
const ed25519::sig sig
|
||||||
|
{
|
||||||
|
[&t](auto &buf)
|
||||||
|
{
|
||||||
|
b64decode(buf, at<"signatures"_>(t));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
auto copy(t);
|
||||||
|
at<"signatures"_>(copy) = string_view{};
|
||||||
|
if(!verify(copy, pk, sig, std::nothrow))
|
||||||
|
throw ed25519::bad_sig{"Verification failed"};
|
||||||
|
}
|
||||||
|
|
||||||
template<class... T>
|
template<class... T>
|
||||||
tuple<T...>::operator
|
tuple<T...>::operator
|
||||||
json::value()
|
json::value()
|
||||||
|
|
Loading…
Reference in a new issue