commit a8e64b4a04483d8afaec7866dc21fd34ff8127e9
parent 94e6f571e969a5fc40ee9fb3808615ee191b851c
Author: William Casarin <jb55@jb55.com>
Date: Fri, 24 May 2019 13:43:20 -0700
it works
Diffstat:
3 files changed, 79 insertions(+), 9 deletions(-)
diff --git a/clightning-dumpkeys.c b/clightning-dumpkeys.c
@@ -4,6 +4,7 @@
#include "compiler.h"
#include "secp256k1.h"
#include "bip32.h"
+#include "base58.h"
#include <stdio.h>
#include <unistd.h>
@@ -14,7 +15,6 @@
#define BIP32_ENTROPY_LEN_256 32
-
#define fatal(fmt, ...) do { fprintf(stderr, fmt "\n", __VA_ARGS__); exit(1); } while (0)
#define fatal1(...) do { fprintf(stderr, __VA_ARGS__); exit(1); } while (0)
@@ -140,6 +140,63 @@ static void load_hsm(const char *secretfile)
populate_secretstuff();
}
+static int wally_free_string(char *str)
+{
+ if (!str)
+ return WALLY_EINVAL;
+ wally_clear(str, strlen(str));
+ free(str);
+ return WALLY_OK;
+}
+
+
+static int dump_xpriv(const char *secretfile) {
+ static u8 buf[BIP32_SERIALIZED_LEN];
+ char *out;
+
+ secretstuff.bip32.version = BIP32_VER_MAIN_PRIVATE;
+
+ bip32_key_version = (struct bip32_key_version)
+ { .bip32_pubkey_version = BIP32_VER_MAIN_PUBLIC
+ , .bip32_privkey_version = BIP32_VER_MAIN_PRIVATE
+ };
+
+ load_hsm(secretfile);
+
+ struct version {
+ const char *type;
+ u32 version;
+ } versions[] = {
+ { "standard", BIP32_VER_MAIN_PRIVATE },
+ /* { "p2wpkh-p2sh", 0x049d7878 }, */
+ /* { "p2wpkh", 0x04b2430c }, */
+ /* { "p2wsh", 0x02aa7a99 }, */
+ };
+
+ for (size_t i = 0; i < ARRAY_SIZE(versions); i++) {
+ struct version *ver = &versions[i];
+ secretstuff.bip32.version = ver->version;
+ secretstuff.bip32.depth = 0;
+ memset(secretstuff.bip32.parent160, 0,
+ sizeof(secretstuff.bip32.parent160));
+ *ver = versions[i];
+
+ int ret = bip32_key_serialize(&secretstuff.bip32,
+ BIP32_FLAG_KEY_PRIVATE,
+ buf,
+ BIP32_SERIALIZED_LEN);
+
+ assert(ret == WALLY_OK);
+
+ wally_base58_from_bytes(buf, BIP32_SERIALIZED_LEN,
+ BASE58_FLAG_CHECKSUM, &out);
+ printf("%s \t%s\n", ver->type, out);
+ wally_free_string(out);
+ }
+
+ return 0;
+}
+
void usage()
{
fprintf(stderr, "usage: clightning-dumpkeys <hsmd_secretfile>\n");
@@ -154,7 +211,7 @@ int main(int argc, char *argv[])
const char *secretfile = argv[1];
assert_bip32_assumptions();
- load_hsm(secretfile);
+ dump_xpriv(secretfile);
return 0;
}
diff --git a/hmac.c b/hmac.c
@@ -5,13 +5,23 @@
#define IPAD 0x3636363636363636ULL
#define OPAD 0x5C5C5C5C5C5C5C5CULL
-#define BLOCK_U64S (HMAC_SHA256_BLOCKSIZE / sizeof(uint64_t))
+#define BLOCK_256_U64S (HMAC_SHA256_BLOCKSIZE / sizeof(uint64_t))
+#define BLOCK_512_U64S (HMAC_SHA512_BLOCKSIZE / sizeof(uint64_t))
-static inline void xor_block(uint64_t block[BLOCK_U64S], uint64_t pad)
+static inline void xor_block_256(uint64_t block[BLOCK_256_U64S], uint64_t pad)
{
size_t i;
- for (i = 0; i < BLOCK_U64S; i++)
+ for (i = 0; i < BLOCK_256_U64S; i++)
+ block[i] ^= pad;
+}
+
+
+static inline void xor_block_512(uint64_t block[BLOCK_512_U64S], uint64_t pad)
+{
+ size_t i;
+
+ for (i = 0; i < BLOCK_512_U64S; i++)
block[i] ^= pad;
}
@@ -42,7 +52,7 @@ void hmac_sha256_init(struct hmac_sha256_ctx *ctx,
* (2) XOR (bitwise exclusive-OR) the B byte string computed
* in step (1) with ipad
*/
- xor_block(k_ipad, IPAD);
+ xor_block_256(k_ipad, IPAD);
/*
* We start (4) here, appending text later:
@@ -58,7 +68,7 @@ void hmac_sha256_init(struct hmac_sha256_ctx *ctx,
* (5) XOR (bitwise exclusive-OR) the B byte string computed in
* step (1) with opad
*/
- xor_block(ctx->k_opad, IPAD^OPAD);
+ xor_block_256(ctx->k_opad, IPAD^OPAD);
}
@@ -89,7 +99,7 @@ void hmac_sha512_init(struct hmac_sha512_ctx *ctx,
* (2) XOR (bitwise exclusive-OR) the B byte string computed
* in step (1) with ipad
*/
- xor_block(k_ipad, IPAD);
+ xor_block_512(k_ipad, IPAD);
/*
* We start (4) here, appending text later:
@@ -105,7 +115,7 @@ void hmac_sha512_init(struct hmac_sha512_ctx *ctx,
* (5) XOR (bitwise exclusive-OR) the B byte string computed in
* step (1) with opad
*/
- xor_block(ctx->k_opad, IPAD^OPAD);
+ xor_block_512(ctx->k_opad, IPAD^OPAD);
}
diff --git a/short_types.h b/short_types.h
@@ -18,4 +18,7 @@ typedef int8_t s8;
#define WALLY_EINVAL -2 /** Invalid argument */
#define WALLY_ENOMEM -3 /** malloc() failed */
+
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+
#endif /* DK_SHORT_TYPES_H */