Skip to content

Commit f4982cc

Browse files
committed
internal: use NUM_ELEMS, reduce internal includes, minor bip85 cleanup
1 parent 7ca0325 commit f4982cc

File tree

5 files changed

+15
-17
lines changed

5 files changed

+15
-17
lines changed

src/bip39.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "wordlist.h"
44
#include "hmac.h"
55
#include "ccan/ccan/crypto/sha256/sha256.h"
6-
#include "ccan/ccan/crypto/sha512/sha512.h"
76
#include <include/wally_bip39.h>
87
#include <include/wally_crypto.h>
98

@@ -53,7 +52,7 @@ int bip39_get_wordlist(const char *lang, struct words **output)
5352
*output = (struct words *)&en_words; /* Fallback to English if not found */
5453

5554
if (lang)
56-
for (i = 0; i < sizeof(lookup) / sizeof(lookup[0]); ++i)
55+
for (i = 0; i < NUM_ELEMS(lookup); ++i)
5756
if (!strcmp(lang, lookup[i].name)) {
5857
*output = (struct words *)lookup[i].words;
5958
break;

src/bip85.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#include "internal.h"
2-
#include "hmac.h"
3-
#include "ccan/ccan/crypto/sha512/sha512.h"
42
#include <include/wally_bip32.h>
53
#include <include/wally_bip39.h>
64
#include <include/wally_bip85.h>
@@ -10,15 +8,14 @@
108
#define BIP85_PURPOSE (BIP32_INITIAL_HARDENED_CHILD | 83696968)
119
#define BIP85_APPLICATION_39 (BIP32_INITIAL_HARDENED_CHILD | 39)
1210
#define BIP85_APPLICATION_RSA (BIP32_INITIAL_HARDENED_CHILD | 828365)
13-
#define BIP85_BIP39_ENTROPY_PATH_LEN 5
14-
#define BIP85_RSA_ENTROPY_PATH_LEN 4
11+
1512
#define BIP85_ENTROPY_HMAC_KEY_LEN 18
1613
static const uint8_t BIP85_ENTROPY_HMAC_KEY[BIP85_ENTROPY_HMAC_KEY_LEN]
1714
= { 'b', 'i', 'p', '-', 'e', 'n', 't', 'r', 'o', 'p', 'y', '-', 'f', 'r', 'o', 'm', '-', 'k' };
1815

1916
/* Bip85 specifies a language code from 0' to 8' - so order here is important */
2017
static const char *bip85_langs[] = { "en", "jp", "kr", "es", "zhs", "zht", "fr", "it", "cz" };
21-
#define BIP85_NUM_LANGS (sizeof(bip85_langs) / sizeof(bip85_langs[0]))
18+
2219

2320
static size_t get_entropy_len(uint32_t num_words)
2421
{
@@ -47,9 +44,10 @@ int bip85_get_bip39_entropy(const struct ext_key *hdkey,
4744
unsigned char* bytes_out, size_t len,
4845
size_t* written)
4946
{
50-
const size_t entropy_len = get_entropy_len(num_words);
51-
uint32_t path[BIP85_BIP39_ENTROPY_PATH_LEN], lang_idx = 0; /* 0=English */
5247
struct ext_key derived;
48+
uint32_t path[5]; /* PURPOSE_BIP85 / APP_39 / land_idx / num_words / index */
49+
uint32_t lang_idx = 0; /* 0=English */
50+
const size_t entropy_len = get_entropy_len(num_words);
5351
int ret;
5452

5553
if (written)
@@ -62,13 +60,13 @@ int bip85_get_bip39_entropy(const struct ext_key *hdkey,
6260
if (lang) {
6361
/* Lookup the callers language */
6462
size_t i;
65-
for (i = 0; i < BIP85_NUM_LANGS; ++i) {
63+
for (i = 0; i < NUM_ELEMS(bip85_langs); ++i) {
6664
if (!strcmp(lang, bip85_langs[i])) {
6765
lang_idx = i;
6866
break;
6967
}
7068
}
71-
if (i == BIP85_NUM_LANGS)
69+
if (i == NUM_ELEMS(bip85_langs))
7270
return WALLY_EINVAL; /* Language not found */
7371
}
7472

@@ -78,8 +76,8 @@ int bip85_get_bip39_entropy(const struct ext_key *hdkey,
7876
path[2] = lang_idx | BIP32_INITIAL_HARDENED_CHILD;
7977
path[3] = num_words | BIP32_INITIAL_HARDENED_CHILD;
8078
path[4] = index | BIP32_INITIAL_HARDENED_CHILD;
81-
ret = bip32_key_from_parent_path(hdkey, path, BIP85_BIP39_ENTROPY_PATH_LEN,
82-
BIP32_FLAG_KEY_PRIVATE|BIP32_FLAG_SKIP_HASH,
79+
ret = bip32_key_from_parent_path(hdkey, path, NUM_ELEMS(path),
80+
BIP32_FLAG_KEY_PRIVATE | BIP32_FLAG_SKIP_HASH,
8381
&derived);
8482

8583
if (ret == WALLY_OK) {
@@ -99,8 +97,8 @@ int bip85_get_bip39_entropy(const struct ext_key *hdkey,
9997
int bip85_get_rsa_entropy(const struct ext_key *hdkey, uint32_t key_bits, uint32_t index,
10098
unsigned char *bytes_out, size_t len, size_t *written)
10199
{
102-
uint32_t path[BIP85_RSA_ENTROPY_PATH_LEN];
103100
struct ext_key derived;
101+
uint32_t path[4]; /* PURPOSE_BIP85 / APP_RSA / key_bits / index */
104102
int ret;
105103

106104
if (written)
@@ -115,7 +113,7 @@ int bip85_get_rsa_entropy(const struct ext_key *hdkey, uint32_t key_bits, uint32
115113
path[1] = BIP85_APPLICATION_RSA;
116114
path[2] = key_bits | BIP32_INITIAL_HARDENED_CHILD;
117115
path[3] = index | BIP32_INITIAL_HARDENED_CHILD;
118-
ret = bip32_key_from_parent_path(hdkey, path, BIP85_RSA_ENTROPY_PATH_LEN,
116+
ret = bip32_key_from_parent_path(hdkey, path, NUM_ELEMS(path),
119117
BIP32_FLAG_KEY_PRIVATE | BIP32_FLAG_SKIP_HASH,
120118
&derived);
121119

src/descriptor.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <stdbool.h>
1515
#include <stdlib.h>
1616

17-
#define NUM_ELEMS(a) (sizeof(a) / sizeof(a[0]))
1817
#define MS_FLAGS_ALL (WALLY_MINISCRIPT_TAPSCRIPT | \
1918
WALLY_MINISCRIPT_ONLY | \
2019
WALLY_MINISCRIPT_REQUIRE_CHECKSUM | \

src/internal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ static void sha256_midstate(struct sha256_ctx *ctx, struct sha256 *res)
215215
/* HW: Already big endian */
216216
memcpy(res->u.u32, ctx->SHA_CTX_STATE, sizeof(ctx->SHA_CTX_STATE));
217217
#else
218-
for (i = 0; i < sizeof(ctx->SHA_CTX_STATE) / sizeof(ctx->SHA_CTX_STATE[0]); i++)
218+
for (i = 0; i < NUM_ELEMS(ctx->SHA_CTX_STATE); i++)
219219
res->u.u32[i] = cpu_to_be32(ctx->SHA_CTX_STATE[i]);
220220
#endif
221221

src/internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ const struct wally_operations *wally_ops(void);
8181
#endif
8282
#define strdup(ptr) __use_wally_strdup_internally__
8383

84+
#define NUM_ELEMS(a) (sizeof(a) / sizeof(a[0]))
85+
8486
/* Validity checking for input parameters */
8587
#define BYTES_VALID(p, len) ((p != NULL) == (len != 0))
8688
#define BYTES_INVALID(p, len) (!BYTES_VALID(p, len))

0 commit comments

Comments
 (0)