0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-09-28 11:48:54 +02:00

ircd:Ⓜ️ Libify hashing/signing functions out of vm::commit().

This commit is contained in:
Jason Volk 2018-03-07 11:54:56 -08:00
parent 8d053c9aba
commit f1a45cc9df
4 changed files with 146 additions and 60 deletions

View file

@ -78,6 +78,14 @@ struct ircd::m::event
using closure = std::function<void (const event &)>; using closure = std::function<void (const event &)>;
using closure_bool = std::function<bool (const event &)>; using closure_bool = std::function<bool (const event &)>;
static ed25519::sig sign(const m::event &);
static ed25519::sig sign(json::iov &event, const json::iov &content);
static string_view signatures(const mutable_buffer &, json::iov &event, const json::iov &content);
static sha256::buf hash(const m::event &);
static sha256::buf hash(json::iov &event, const string_view &content);
static string_view hashes(const mutable_buffer &, json::iov &event, const string_view &content);
using super_type::tuple; using super_type::tuple;
using super_type::operator=; using super_type::operator=;

View file

@ -29,7 +29,7 @@ namespace ircd::m::vm
extern ctx::shared_view<const event> accept; extern ctx::shared_view<const event> accept;
extern const opts default_opts; extern const opts default_opts;
event::id::buf commit(json::iov &event); event::id::buf commit(const m::event &);
event::id::buf commit(json::iov &event, const json::iov &content); event::id::buf commit(json::iov &event, const json::iov &content);
} }

View file

@ -413,6 +413,119 @@ ircd::m::event::event(const id &id,
new (this) m::event(obj); new (this) m::event(obj);
} }
ircd::string_view
ircd::m::event::hashes(const mutable_buffer &out,
json::iov &event,
const string_view &content)
{
const sha256::buf hash_
{
hash(event, content)
};
static const size_t hashb64sz
{
size_t(sizeof(hash_) * 1.34) + 1
};
thread_local char hashb64buf[hashb64sz];
const json::members hashes
{
{ "sha256", b64encode_unpadded(hashb64buf, hash_) }
};
return json::stringify(mutable_buffer{out}, hashes);
}
ircd::sha256::buf
ircd::m::event::hash(json::iov &event,
const string_view &content)
{
const json::iov::push _content
{
event, { "content", content }
};
return hash(event);
}
ircd::sha256::buf
ircd::m::event::hash(const m::event &event)
{
thread_local char buf[64_KiB];
const string_view preimage
{
stringify(buf, event)
};
const sha256::buf hash
{
sha256{preimage}
};
return hash;
}
ircd::string_view
ircd::m::event::signatures(const mutable_buffer &out,
json::iov &event,
const json::iov &content)
{
const ed25519::sig sig
{
sign(event, content)
};
static const size_t sigb64sz
{
size_t(sizeof(sig) * 1.34) + 1
};
thread_local char sigb64buf[sigb64sz];
const json::members sigb64
{
{ self::public_key_id, b64encode_unpadded(sigb64buf, sig) }
};
const json::members sigs
{
{ my_host(), sigb64 }
};
return json::stringify(mutable_buffer{out}, sigs);
}
ircd::ed25519::sig
ircd::m::event::sign(json::iov &event,
const json::iov &content)
{
//TODO: essential keys
const json::iov::push _content
{
event, { "content", "{}" }
};
return sign(event);
}
ircd::ed25519::sig
ircd::m::event::sign(const m::event &event)
{
thread_local char buf[64_KiB];
const string_view preimage
{
stringify(buf, event)
};
const ed25519::sig sig
{
self::secret_key.sign(preimage)
};
assert(self::public_key.verify(preimage, sig));
return sig;
}
// //
// event::prev // event::prev
// //

View file

@ -42,85 +42,55 @@ ircd::m::event::id::buf
ircd::m::vm::commit(json::iov &event, ircd::m::vm::commit(json::iov &event,
const json::iov &contents) const json::iov &contents)
{ {
const auto &room_id
{
event.at("room_id")
};
// derp
const json::strung content
{
contents
};
const json::iov::set set[] const json::iov::set set[]
{ {
{ event, { "origin_server_ts", ircd::time<milliseconds>() }}, { event, { "origin_server_ts", ircd::time<milliseconds>() }},
{ event, { "origin", my_host() }}, { event, { "origin", my_host() }},
}; };
thread_local char preimage_buf[64_KiB]; const json::strung content
const_buffer preimage
{ {
stringify(mutable_buffer{preimage_buf}, event) contents
}; };
sha256::buf hash // event_id
sha256::buf event_id_hash;
{ {
sha256{preimage} thread_local char preimage_buf[64_KiB];
}; event_id_hash = sha256{stringify(mutable_buffer{preimage_buf}, event)};
}
event::id::buf eid_buf; event::id::buf eid_buf;
const json::iov::set _event_id const auto event_id
{ {
event, { "event_id", m::event_id(event, eid_buf, hash) } m::event_id(event, eid_buf, event_id_hash)
}; };
char hashes_buf[128]; const json::iov::set _event_id
string_view hashes;
{ {
const json::iov::push _content event, { "event_id", event_id }
{ };
event, { "content", string_view{content} }
};
// derp // hashes
preimage = stringify(mutable_buffer{preimage_buf}, event);
hash = sha256{preimage};
// derp char hashes_buf[128];
thread_local char hashb64[hash.size() * 2]; const string_view hashes
hashes = stringify(mutable_buffer{hashes_buf}, json::members {
{ m::event::hashes(hashes_buf, event, content)
{ "sha256", b64encode_unpadded(hashb64, hash) } };
});
}
const json::iov::push _hashes const json::iov::push _hashes
{ {
event, { "hashes", hashes } event, { "hashes", hashes }
}; };
// derp // sigs
ed25519::sig sig;
{
const json::iov::push _content
{
event, { "content", "{}" }
};
preimage = stringify(mutable_buffer{preimage_buf}, event); char sigs_buf[384];
sig = self::secret_key.sign(preimage); const string_view sigs
assert(self::public_key.verify(preimage, sig));
}
char sigb64[size_t(size(sig) * 1.34) + 1];
const json::members sigs
{ {
{ my_host(), json::members m::event::signatures(sigs_buf, event, contents)
{
{ self::public_key_id, b64encode_unpadded(sigb64, sig) }
}}
}; };
const json::iov::push _final[] const json::iov::push _final[]
@ -162,13 +132,8 @@ ircd::m::vm::commit_hook
/// out /// out
/// ///
ircd::m::event::id::buf ircd::m::event::id::buf
ircd::m::vm::commit(json::iov &iov) ircd::m::vm::commit(const event &event)
{ {
const m::event event
{
iov
};
check_size(event); check_size(event);
log.debug("injecting event(mark: %ld) %s", log.debug("injecting event(mark: %ld) %s",