2015-09-30 03:24:54 +02:00
|
|
|
#include "privkey.h"
|
2016-01-21 21:08:08 +01:00
|
|
|
#include "pubkey.h"
|
2016-07-01 03:49:28 +02:00
|
|
|
#include "utils.h"
|
2016-01-21 21:08:08 +01:00
|
|
|
#include <assert.h>
|
2016-03-30 08:24:16 +02:00
|
|
|
#include <ccan/mem/mem.h>
|
2016-01-21 21:08:08 +01:00
|
|
|
#include <ccan/str/hex/hex.h>
|
2016-07-01 03:49:28 +02:00
|
|
|
#include <ccan/structeq/structeq.h>
|
2015-06-02 04:23:59 +02:00
|
|
|
|
2016-12-02 08:42:58 +01:00
|
|
|
bool pubkey_from_der(const u8 *der, size_t len, struct pubkey *key)
|
2015-06-02 04:23:59 +02:00
|
|
|
{
|
2016-07-01 03:49:28 +02:00
|
|
|
if (len != PUBKEY_DER_LEN)
|
2016-01-21 21:11:47 +01:00
|
|
|
return false;
|
2015-09-30 03:24:54 +02:00
|
|
|
|
2016-12-02 08:42:58 +01:00
|
|
|
if (!secp256k1_ec_pubkey_parse(secp256k1_ctx, &key->pubkey,
|
2016-07-01 03:49:28 +02:00
|
|
|
memcheck(der, len), len))
|
2016-01-21 21:11:47 +01:00
|
|
|
return false;
|
2015-09-30 03:24:54 +02:00
|
|
|
|
|
|
|
return true;
|
2015-06-02 04:23:59 +02:00
|
|
|
}
|
|
|
|
|
2016-12-02 08:42:58 +01:00
|
|
|
void pubkey_to_der(u8 der[PUBKEY_DER_LEN], const struct pubkey *key)
|
2016-07-01 03:49:28 +02:00
|
|
|
{
|
|
|
|
size_t outlen = PUBKEY_DER_LEN;
|
2016-12-02 08:42:58 +01:00
|
|
|
if (!secp256k1_ec_pubkey_serialize(secp256k1_ctx, der, &outlen,
|
2016-07-01 03:49:28 +02:00
|
|
|
&key->pubkey,
|
|
|
|
SECP256K1_EC_COMPRESSED))
|
|
|
|
abort();
|
|
|
|
assert(outlen == PUBKEY_DER_LEN);
|
|
|
|
}
|
|
|
|
|
2015-09-30 03:24:54 +02:00
|
|
|
/* Pubkey from privkey */
|
2016-12-02 08:42:58 +01:00
|
|
|
bool pubkey_from_privkey(const struct privkey *privkey,
|
2016-07-01 03:57:57 +02:00
|
|
|
struct pubkey *key)
|
2015-06-05 04:54:58 +02:00
|
|
|
{
|
2016-12-02 08:42:58 +01:00
|
|
|
if (!secp256k1_ec_pubkey_create(secp256k1_ctx,
|
|
|
|
&key->pubkey, privkey->secret))
|
2016-01-21 21:11:47 +01:00
|
|
|
return false;
|
2015-09-30 03:24:54 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-11-11 00:02:04 +01:00
|
|
|
|
2016-12-02 08:42:58 +01:00
|
|
|
bool pubkey_from_hexstr(const char *derstr, size_t slen, struct pubkey *key)
|
2015-09-30 03:24:54 +02:00
|
|
|
{
|
2016-01-21 21:11:47 +01:00
|
|
|
size_t dlen;
|
2016-07-01 03:49:28 +02:00
|
|
|
unsigned char der[PUBKEY_DER_LEN];
|
2015-09-30 03:24:54 +02:00
|
|
|
|
|
|
|
dlen = hex_data_size(slen);
|
2016-03-30 08:24:16 +02:00
|
|
|
if (dlen != sizeof(der))
|
2015-06-05 04:54:58 +02:00
|
|
|
return false;
|
2015-09-30 03:24:54 +02:00
|
|
|
|
|
|
|
if (!hex_decode(derstr, slen, der, dlen))
|
2015-06-05 04:54:58 +02:00
|
|
|
return false;
|
2015-09-30 03:24:54 +02:00
|
|
|
|
2016-12-02 08:42:58 +01:00
|
|
|
return pubkey_from_der(der, dlen, key);
|
2015-09-30 03:24:54 +02:00
|
|
|
}
|
|
|
|
|
2016-12-02 08:42:58 +01:00
|
|
|
char *pubkey_to_hexstr(const tal_t *ctx, const struct pubkey *key)
|
2016-07-01 03:49:28 +02:00
|
|
|
{
|
|
|
|
unsigned char der[PUBKEY_DER_LEN];
|
|
|
|
|
2016-12-02 08:42:58 +01:00
|
|
|
pubkey_to_der(der, key);
|
2016-07-01 03:49:28 +02:00
|
|
|
return tal_hexstr(ctx, der, sizeof(der));
|
|
|
|
}
|
2016-11-11 00:02:04 +01:00
|
|
|
|
2015-09-30 03:24:54 +02:00
|
|
|
bool pubkey_eq(const struct pubkey *a, const struct pubkey *b)
|
|
|
|
{
|
2016-07-01 03:49:28 +02:00
|
|
|
return structeq(&a->pubkey, &b->pubkey);
|
2015-06-05 04:54:58 +02:00
|
|
|
}
|
2016-12-12 13:12:58 +01:00
|
|
|
|
|
|
|
int pubkey_cmp(const struct pubkey *a, const struct pubkey *b)
|
|
|
|
{
|
|
|
|
u8 keya[33], keyb[33];
|
|
|
|
pubkey_to_der(keya, a);
|
|
|
|
pubkey_to_der(keyb, b);
|
|
|
|
return memcmp(keya, keyb, sizeof(keya));
|
|
|
|
}
|