Skip to content

Commit 0cd1a2e

Browse files
committed
Merge bitcoin/bitcoin#23595: util: Add ParseHex<std::byte>() helper
facd1fb refactor: Use Span of std::byte in CExtKey::SetSeed (MarcoFalke) fae1006 util: Add ParseHex<std::byte>() helper (MarcoFalke) fabdf81 test: Add test for embedded null in hex string (MarcoFalke) Pull request description: This adds the hex->`std::byte` helper after the `std::byte`->hex helper was added in commit 9394964 ACKs for top commit: pk-b2: ACK bitcoin/bitcoin@facd1fb laanwj: Code review ACK facd1fb Tree-SHA512: e2329fbdea2e580bd1618caab31f5d0e59c245a028e1236662858e621929818870b76ab6834f7ac6a46d7874dfec63f498380ad99da6efe4218f720a60e859be
2 parents a7e3afb + facd1fb commit 0cd1a2e

File tree

8 files changed

+28
-13
lines changed

8 files changed

+28
-13
lines changed

src/key.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,11 @@ bool CExtKey::Derive(CExtKey &out, unsigned int _nChild) const {
340340
return key.Derive(out.key, out.chaincode, _nChild, chaincode);
341341
}
342342

343-
void CExtKey::SetSeed(Span<const uint8_t> seed)
343+
void CExtKey::SetSeed(Span<const std::byte> seed)
344344
{
345345
static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
346346
std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
347-
CHMAC_SHA512{hashkey, sizeof(hashkey)}.Write(seed.data(), seed.size()).Finalize(vout.data());
347+
CHMAC_SHA512{hashkey, sizeof(hashkey)}.Write(UCharCast(seed.data()), seed.size()).Finalize(vout.data());
348348
key.Set(vout.data(), vout.data() + 32, true);
349349
memcpy(chaincode.begin(), vout.data() + 32, 32);
350350
nDepth = 0;

src/key.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class CKey
8585

8686
//! Simple read-only vector-like interface.
8787
unsigned int size() const { return (fValid ? keydata.size() : 0); }
88-
const unsigned char* data() const { return keydata.data(); }
88+
const std::byte* data() const { return reinterpret_cast<const std::byte*>(keydata.data()); }
8989
const unsigned char* begin() const { return keydata.data(); }
9090
const unsigned char* end() const { return keydata.data() + size(); }
9191

@@ -178,7 +178,7 @@ struct CExtKey {
178178
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
179179
bool Derive(CExtKey& out, unsigned int nChild) const;
180180
CExtPubKey Neuter() const;
181-
void SetSeed(Span<const uint8_t> seed);
181+
void SetSeed(Span<const std::byte> seed);
182182
};
183183

184184
/** Initialize the elliptic curve support. May not be called twice without calling ECC_Stop first. */

src/test/bip32_tests.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ const std::vector<std::string> TEST5 = {
120120
"xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHL"
121121
};
122122

123-
void RunTest(const TestVector &test) {
124-
std::vector<unsigned char> seed = ParseHex(test.strHexMaster);
123+
void RunTest(const TestVector& test)
124+
{
125+
std::vector<std::byte> seed{ParseHex<std::byte>(test.strHexMaster)};
125126
CExtKey key;
126127
CExtPubKey pubkey;
127128
key.SetSeed(seed);

src/test/fuzz/hex.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ FUZZ_TARGET_INIT(hex, initialize_hex)
2525
{
2626
const std::string random_hex_string(buffer.begin(), buffer.end());
2727
const std::vector<unsigned char> data = ParseHex(random_hex_string);
28+
const std::vector<std::byte> bytes{ParseHex<std::byte>(random_hex_string)};
29+
assert(AsBytes(Span{data}) == Span{bytes});
2830
const std::string hex_data = HexStr(data);
2931
if (IsHex(random_hex_string)) {
3032
assert(ToLower(random_hex_string) == hex_data);

src/test/key_io_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ BOOST_AUTO_TEST_CASE(key_io_valid_parse)
3535
continue;
3636
}
3737
std::string exp_base58string = test[0].get_str();
38-
std::vector<unsigned char> exp_payload = ParseHex(test[1].get_str());
38+
const std::vector<std::byte> exp_payload{ParseHex<std::byte>(test[1].get_str())};
3939
const UniValue &metadata = test[2].get_obj();
4040
bool isPrivkey = find_value(metadata, "isPrivkey").get_bool();
4141
SelectParams(find_value(metadata, "chain").get_str());

src/test/util_tests.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ static const unsigned char ParseHex_expected[65] = {
153153
0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
154154
0x5f
155155
};
156-
BOOST_AUTO_TEST_CASE(util_ParseHex)
156+
BOOST_AUTO_TEST_CASE(parse_hex)
157157
{
158158
std::vector<unsigned char> result;
159159
std::vector<unsigned char> expected(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected));
@@ -169,6 +169,14 @@ BOOST_AUTO_TEST_CASE(util_ParseHex)
169169
result = ParseHex(" 89 34 56 78");
170170
BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
171171

172+
// Embedded null is treated as end
173+
const std::string with_embedded_null{" 11 "s
174+
" \0 "
175+
" 22 "s};
176+
BOOST_CHECK_EQUAL(with_embedded_null.size(), 11);
177+
result = ParseHex(with_embedded_null);
178+
BOOST_CHECK(result.size() == 1 && result[0] == 0x11);
179+
172180
// Stop parsing at invalid value
173181
result = ParseHex("1234 invalid 1234");
174182
BOOST_CHECK(result.size() == 2 && result[0] == 0x12 && result[1] == 0x34);

src/util/strencodings.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ bool IsHexNumber(std::string_view str)
7777
return str.size() > 0;
7878
}
7979

80-
std::vector<unsigned char> ParseHex(std::string_view str)
80+
template <typename Byte>
81+
std::vector<Byte> ParseHex(std::string_view str)
8182
{
82-
// convert hex dump to vector
83-
std::vector<unsigned char> vch;
83+
std::vector<Byte> vch;
8484
auto it = str.begin();
8585
while (it != str.end() && it + 1 != str.end()) {
8686
if (IsSpace(*it)) {
@@ -90,10 +90,12 @@ std::vector<unsigned char> ParseHex(std::string_view str)
9090
auto c1 = HexDigit(*(it++));
9191
auto c2 = HexDigit(*(it++));
9292
if (c1 < 0 || c2 < 0) break;
93-
vch.push_back(uint8_t(c1 << 4) | c2);
93+
vch.push_back(Byte(c1 << 4) | Byte(c2));
9494
}
9595
return vch;
9696
}
97+
template std::vector<std::byte> ParseHex(std::string_view);
98+
template std::vector<uint8_t> ParseHex(std::string_view);
9799

98100
void SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut)
99101
{

src/util/strencodings.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ enum class ByteUnit : uint64_t {
5555
* @return A new string without unsafe chars
5656
*/
5757
std::string SanitizeString(std::string_view str, int rule = SAFE_CHARS_DEFAULT);
58-
std::vector<unsigned char> ParseHex(std::string_view str);
58+
/** Parse the hex string into bytes (uint8_t or std::byte). Ignores whitespace. */
59+
template <typename Byte = uint8_t>
60+
std::vector<Byte> ParseHex(std::string_view str);
5961
signed char HexDigit(char c);
6062
/* Returns true if each character in str is a hex character, and has an even
6163
* number of hex digits.*/

0 commit comments

Comments
 (0)