Skip to content

Commit 1464dd7

Browse files
committed
Check decrypted backup
1 parent 5b9ec5c commit 1464dd7

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/hwi_tapsigner.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,11 @@ std::string HWITapsignerImpl::DecryptBackup(const Bytes &encrypted_data,
503503
}
504504
Bytes decrypted = AES128CTRDecrypt(encrypted_data, backup_key_bytes);
505505

506+
if (decrypted.size() < std::size(xprv)) {
507+
throw TapProtoException(TapProtoException::INVALID_BACKUP_KEY,
508+
"Invalid backup key");
509+
}
510+
506511
if (std::equal(std::begin(xprv), std::end(xprv), std::begin(decrypted)) ||
507512
std::equal(std::begin(tprv), std::end(tprv), std::begin(decrypted))) {
508513
return {std::begin(decrypted), std::end(decrypted)};

tests/hwi_tapsigner_test.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ TEST_CASE("decrypt backup") {
129129
CHECK(decrypted == expected);
130130
}
131131

132+
TEST_CASE("decrypt backup invalid data") {
133+
std::unique_ptr<tap_protocol::Transport> tp =
134+
std::make_unique<CardEmulator>();
135+
136+
std::unique_ptr<tap_protocol::Tapsigner> tapsigner =
137+
std::make_unique<tap_protocol::Tapsigner>(std::move(tp));
138+
139+
auto hwi = tap_protocol::MakeHWITapsigner(tapsigner.get(), default_cvc);
140+
hwi->SetChain(tap_protocol::HWITapsigner::Chain::TESTNET);
141+
142+
std::string backup_key = "41414141414141414141414141414141";
143+
auto encrypted = tap_protocol::Hex2Bytes("");
144+
CHECK_THROWS_AS({ hwi->DecryptBackup(encrypted, backup_key); },
145+
tap_protocol::TapProtoException);
146+
}
147+
132148
TEST_CASE("get xpub at path") {
133149
std::unique_ptr<tap_protocol::Transport> tp =
134150
std::make_unique<CardEmulator>();

0 commit comments

Comments
 (0)