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>
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
1613static 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 */
2017static 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
2320static 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,
9997int 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
0 commit comments