core-lightning/common/ecdh_hsmd.c
Rusty Russell 3b4a06f52b common: generalize ecdh function.
common/onion is going to need to use this for the case where it finds a blinding
seed inside the TLV.  But how it does ecdh is daemon-specific.

We already had this problem for devtools/gossipwith, which supplied a
special hsm_do_ecdh().  This just makes it more general.

So we create a generic ecdh() interface, with a specific implementation
which subdaemons and lightningd can use.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-04-04 16:08:49 +10:30

30 lines
827 B
C

#include <common/ecdh_hsmd.h>
#include <hsmd/gen_hsm_wire.h>
#include <wire/wire_sync.h>
static int stashed_hsm_fd = -1;
static void (*stashed_failed)(enum status_failreason, const char *fmt, ...);
void ecdh(const struct pubkey *point, struct secret *ss)
{
const u8 *msg = towire_hsm_ecdh_req(NULL, point);
if (!wire_sync_write(stashed_hsm_fd, take(msg)))
stashed_failed(STATUS_FAIL_HSM_IO, "Write ECDH to hsmd failed");
msg = wire_sync_read(tmpctx, stashed_hsm_fd);
if (!msg)
stashed_failed(STATUS_FAIL_HSM_IO, "No hsmd ECDH response");
if (!fromwire_hsm_ecdh_resp(msg, ss))
stashed_failed(STATUS_FAIL_HSM_IO, "Invalid hsmd ECDH response");
}
void ecdh_hsmd_setup(int hsm_fd,
void (*failed)(enum status_failreason,
const char *fmt, ...))
{
stashed_hsm_fd = hsm_fd;
stashed_failed = failed;
}