Browse Source

protocol: remove support for uncompressed pubkeys.

There's no good reason to support them, and this way every key is 33 bytes.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
b6943b9198
  1. 3
      bitcoin/address.c
  2. 41
      bitcoin/pubkey.c
  3. 5
      bitcoin/pubkey.h
  4. 8
      bitcoin/script.c
  5. 2
      daemon/packets.c
  6. 2
      daemon/peer.c
  7. 2
      daemon/secrets.c
  8. 2
      lightning.pb-c.h
  9. 2
      lightning.proto
  10. 2
      protobuf_convert.c

3
bitcoin/address.c

@ -1,11 +1,12 @@
#include "address.h" #include "address.h"
#include "pubkey.h" #include "pubkey.h"
#include <ccan/mem/mem.h>
#include <ccan/crypto/sha256/sha256.h> #include <ccan/crypto/sha256/sha256.h>
void bitcoin_address(const struct pubkey *key, struct bitcoin_address *addr) void bitcoin_address(const struct pubkey *key, struct bitcoin_address *addr)
{ {
struct sha256 h; struct sha256 h;
sha256(&h, key->der, pubkey_derlen(key)); sha256(&h, memcheck(key->der, sizeof(key->der)), sizeof(key->der));
ripemd160(&addr->addr, h.u.u8, sizeof(h)); ripemd160(&addr->addr, h.u.u8, sizeof(h));
} }

41
bitcoin/pubkey.c

@ -1,41 +1,19 @@
#include "privkey.h" #include "privkey.h"
#include "pubkey.h" #include "pubkey.h"
#include <assert.h> #include <assert.h>
#include <ccan/mem/mem.h>
#include <ccan/str/hex/hex.h> #include <ccan/str/hex/hex.h>
/* Must agree on key validity with bitcoin! Stolen from bitcoin/src/pubkey.h's
* GetLen:
* // Copyright (c) 2009-2010 Satoshi Nakamoto
* // Copyright (c) 2009-2014 The Bitcoin Core developers
* // Distributed under the MIT software license, see the accompanying
* // file COPYING or http://www.opensource.org/licenses/mit-license.php.
*/
static unsigned int GetLen(unsigned char chHeader)
{
if (chHeader == 2 || chHeader == 3)
return 33;
if (chHeader == 4 || chHeader == 6 || chHeader == 7)
return 65;
return 0;
}
size_t pubkey_derlen(const struct pubkey *key)
{
size_t len = GetLen(key->der[0]);
assert(len);
return len;
}
bool pubkey_from_der(secp256k1_context *secpctx, bool pubkey_from_der(secp256k1_context *secpctx,
const u8 *der, size_t len, const u8 *der, size_t len,
struct pubkey *key) struct pubkey *key)
{ {
if (len > sizeof(key->der)) if (len != sizeof(key->der))
return false; return false;
memcpy(key->der, der, len); memcpy(key->der, memcheck(der, sizeof(key->der)), sizeof(key->der));
if (!secp256k1_ec_pubkey_parse(secpctx, &key->pubkey, key->der, len)) if (!secp256k1_ec_pubkey_parse(secpctx, &key->pubkey, key->der,
sizeof(key->der)))
return false; return false;
return true; return true;
@ -55,7 +33,7 @@ bool pubkey_from_privkey(secp256k1_context *secpctx,
if (!secp256k1_ec_pubkey_serialize(secpctx, key->der, &outlen, if (!secp256k1_ec_pubkey_serialize(secpctx, key->der, &outlen,
&key->pubkey, compressed_flags)) &key->pubkey, compressed_flags))
return false; return false;
assert(outlen == pubkey_derlen(key)); assert(outlen == sizeof(key->der));
return true; return true;
} }
@ -63,10 +41,10 @@ bool pubkey_from_hexstr(secp256k1_context *secpctx,
const char *derstr, size_t slen, struct pubkey *key) const char *derstr, size_t slen, struct pubkey *key)
{ {
size_t dlen; size_t dlen;
unsigned char der[65]; unsigned char der[sizeof(key->der)];
dlen = hex_data_size(slen); dlen = hex_data_size(slen);
if (dlen > sizeof(der)) if (dlen != sizeof(der))
return false; return false;
if (!hex_decode(derstr, slen, der, dlen)) if (!hex_decode(derstr, slen, der, dlen))
@ -77,6 +55,5 @@ bool pubkey_from_hexstr(secp256k1_context *secpctx,
bool pubkey_eq(const struct pubkey *a, const struct pubkey *b) bool pubkey_eq(const struct pubkey *a, const struct pubkey *b)
{ {
return pubkey_derlen(a) == pubkey_derlen(b) return memcmp(a->der, b->der, sizeof(a->der)) == 0;
&& memcmp(a->der, b->der, pubkey_derlen(a)) == 0;
} }

5
bitcoin/pubkey.h

@ -9,7 +9,7 @@ struct privkey;
struct pubkey { struct pubkey {
/* DER-encoded key (as hashed by bitcoin, for addresses) */ /* DER-encoded key (as hashed by bitcoin, for addresses) */
u8 der[65]; u8 der[33];
/* Unpacked pubkey (as used by libsecp256k1 internally) */ /* Unpacked pubkey (as used by libsecp256k1 internally) */
secp256k1_pubkey pubkey; secp256k1_pubkey pubkey;
}; };
@ -28,9 +28,6 @@ bool pubkey_from_privkey(secp256k1_context *secpctx,
bool pubkey_from_der(secp256k1_context *secpctx, bool pubkey_from_der(secp256k1_context *secpctx,
const u8 *der, size_t len, struct pubkey *key); const u8 *der, size_t len, struct pubkey *key);
/* How many bytes of key->der are valid. */
size_t pubkey_derlen(const struct pubkey *key);
/* Are these keys equal? */ /* Are these keys equal? */
bool pubkey_eq(const struct pubkey *a, const struct pubkey *b); bool pubkey_eq(const struct pubkey *a, const struct pubkey *b);
#endif /* LIGHTNING_PUBKEY_H */ #endif /* LIGHTNING_PUBKEY_H */

8
bitcoin/script.c

@ -102,7 +102,7 @@ static void add_number(u8 **script, u32 num)
static void add_push_key(u8 **scriptp, const struct pubkey *key) static void add_push_key(u8 **scriptp, const struct pubkey *key)
{ {
add_push_bytes(scriptp, key->der, pubkey_derlen(key)); add_push_bytes(scriptp, key->der, sizeof(key->der));
} }
static void add_push_sig(u8 **scriptp, const struct bitcoin_signature *sig) static void add_push_sig(u8 **scriptp, const struct bitcoin_signature *sig)
@ -129,11 +129,7 @@ static void add_push_sig(u8 **scriptp, const struct bitcoin_signature *sig)
/* Is a < b? (If equal we don't care) */ /* Is a < b? (If equal we don't care) */
static bool key_less(const struct pubkey *a, const struct pubkey *b) static bool key_less(const struct pubkey *a, const struct pubkey *b)
{ {
/* Shorter one wins. */ return memcmp(a->der, b->der, sizeof(a->der)) < 0;
if (pubkey_derlen(a) != pubkey_derlen(b))
return pubkey_derlen(a) < pubkey_derlen(b);
return memcmp(a->der, b->der, pubkey_derlen(a)) < 0;
} }
/* tal_count() gives the length of the script. */ /* tal_count() gives the length of the script. */

2
daemon/packets.c

@ -35,7 +35,7 @@ static void dump_tx(const char *str, const struct bitcoin_tx *tx)
static void dump_key(const char *str, const struct pubkey *key) static void dump_key(const char *str, const struct pubkey *key)
{ {
printf("%s:%s\n", str, hex_of(NULL, key->der, pubkey_derlen(key))); printf("%s:%s\n", str, hex_of(NULL, key->der, sizeof(key->der)));
} }
/* Wrap (and own!) member inside Pkt */ /* Wrap (and own!) member inside Pkt */

2
daemon/peer.c

@ -1363,7 +1363,7 @@ static void json_getpeers(struct command *cmd,
/* This is only valid after crypto setup. */ /* This is only valid after crypto setup. */
if (p->state != STATE_INIT) if (p->state != STATE_INIT)
json_add_hex(response, "peerid", json_add_hex(response, "peerid",
p->id.der, pubkey_derlen(&p->id)); p->id.der, sizeof(p->id.der));
if (p->cstate) { if (p->cstate) {
json_object_start(response, "channel"); json_object_start(response, "channel");

2
daemon/secrets.c

@ -176,5 +176,5 @@ void secrets_init(struct lightningd_state *dstate)
fatal("Invalid privkey"); fatal("Invalid privkey");
log_info(dstate->base_log, "ID: "); log_info(dstate->base_log, "ID: ");
log_add_hex(dstate->base_log, dstate->id.der, pubkey_derlen(&dstate->id)); log_add_hex(dstate->base_log, dstate->id.der, sizeof(dstate->id.der));
} }

2
lightning.pb-c.h

@ -114,7 +114,7 @@ struct _BitcoinPubkey
{ {
ProtobufCMessage base; ProtobufCMessage base;
/* /*
* Either 65 or 33 bytes. * Must be 33 bytes.
*/ */
ProtobufCBinaryData key; ProtobufCBinaryData key;
}; };

2
lightning.proto

@ -35,7 +35,7 @@ message locktime {
// Pubkey for commitment transaction input. // Pubkey for commitment transaction input.
message bitcoin_pubkey { message bitcoin_pubkey {
// Either 65 or 33 bytes. // Must be 33 bytes.
required bytes key = 1; required bytes key = 1;
} }

2
protobuf_convert.c

@ -71,7 +71,7 @@ BitcoinPubkey *pubkey_to_proto(const tal_t *ctx, const struct pubkey *key)
struct pubkey check; struct pubkey check;
bitcoin_pubkey__init(p); bitcoin_pubkey__init(p);
p->key.len = pubkey_derlen(key); p->key.len = sizeof(key->der);
p->key.data = tal_dup_arr(p, u8, key->der, p->key.len, 0); p->key.data = tal_dup_arr(p, u8, key->der, p->key.len, 0);
{ {

Loading…
Cancel
Save