0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-27 07:54:05 +01:00

ircd::json: Add a preliminary suite of sign()/verify()/hash() for tuple.

This commit is contained in:
Jason Volk 2017-10-15 21:22:52 -07:00
parent f20ff13a44
commit 9bc7587557

View file

@ -64,6 +64,7 @@ struct tuple
using super_type = tuple<T...>;
operator json::value() const;
operator crh::sha256::buf() const;
static constexpr size_t size();
@ -995,6 +996,97 @@ operator<<(std::ostream &s, const tuple<T...> &t)
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>
tuple<T...>::operator
json::value()