Browse Source

Use a more specific exception class for note decryption failure

Closes #1545.
v1.0.9-lin
Jack Grigg 8 years ago
parent
commit
51fde9ea08
No known key found for this signature in database GPG Key ID: 6A6914DAFBEA00DA
  1. 18
      src/gtest/test_noteencryption.cpp
  2. 12
      src/wallet/wallet.cpp
  3. 2
      src/zcash/NoteEncryption.cpp
  4. 5
      src/zcash/NoteEncryption.hpp

18
src/gtest/test_noteencryption.cpp

@ -46,21 +46,25 @@ TEST(noteencryption, api)
ASSERT_TRUE(plaintext == message); ASSERT_TRUE(plaintext == message);
// Test wrong nonce // Test wrong nonce
ASSERT_THROW(decrypter.decrypt(ciphertext, b.get_epk(), uint256(), (i == 0) ? 1 : (i - 1)), std::runtime_error); ASSERT_THROW(decrypter.decrypt(ciphertext, b.get_epk(), uint256(), (i == 0) ? 1 : (i - 1)),
libzcash::note_decryption_failed);
// Test wrong ephemeral key // Test wrong ephemeral key
{ {
ZCNoteEncryption c = ZCNoteEncryption(uint256()); ZCNoteEncryption c = ZCNoteEncryption(uint256());
ASSERT_THROW(decrypter.decrypt(ciphertext, c.get_epk(), uint256(), i), std::runtime_error); ASSERT_THROW(decrypter.decrypt(ciphertext, c.get_epk(), uint256(), i),
libzcash::note_decryption_failed);
} }
// Test wrong seed // Test wrong seed
ASSERT_THROW(decrypter.decrypt(ciphertext, b.get_epk(), uint256S("11035d60bc1983e37950ce4803418a8fb33ea68d5b937ca382ecbae7564d6a77"), i), std::runtime_error); ASSERT_THROW(decrypter.decrypt(ciphertext, b.get_epk(), uint256S("11035d60bc1983e37950ce4803418a8fb33ea68d5b937ca382ecbae7564d6a77"), i),
libzcash::note_decryption_failed);
// Test corrupted ciphertext // Test corrupted ciphertext
ciphertext[10] ^= 0xff; ciphertext[10] ^= 0xff;
ASSERT_THROW(decrypter.decrypt(ciphertext, b.get_epk(), uint256(), i), std::runtime_error); ASSERT_THROW(decrypter.decrypt(ciphertext, b.get_epk(), uint256(), i),
libzcash::note_decryption_failed);
ciphertext[10] ^= 0xff; ciphertext[10] ^= 0xff;
} }
@ -69,7 +73,8 @@ TEST(noteencryption, api)
uint256 sk_enc_2 = ZCNoteEncryption::generate_privkey(uint252()); uint256 sk_enc_2 = ZCNoteEncryption::generate_privkey(uint252());
ZCNoteDecryption decrypter(sk_enc_2); ZCNoteDecryption decrypter(sk_enc_2);
ASSERT_THROW(decrypter.decrypt(ciphertext, b.get_epk(), uint256(), i), std::runtime_error); ASSERT_THROW(decrypter.decrypt(ciphertext, b.get_epk(), uint256(), i),
libzcash::note_decryption_failed);
} }
{ {
@ -81,7 +86,8 @@ TEST(noteencryption, api)
// Test wrong public key (test of KDF) // Test wrong public key (test of KDF)
decrypter.change_pk_enc(uint256()); decrypter.change_pk_enc(uint256());
ASSERT_THROW(decrypter.decrypt(ciphertext, b.get_epk(), uint256(), i), std::runtime_error); ASSERT_THROW(decrypter.decrypt(ciphertext, b.get_epk(), uint256(), i),
libzcash::note_decryption_failed);
} }
} }

12
src/wallet/wallet.cpp

@ -1275,12 +1275,7 @@ mapNoteData_t CWallet::FindMyNotes(const CTransaction& tx) const
noteData.insert(std::make_pair(jsoutpt, nd)); noteData.insert(std::make_pair(jsoutpt, nd));
} }
break; break;
} catch (const std::runtime_error &err) { } catch (const note_decryption_failed &err) {
if (memcmp("Could not decrypt message", err.what(), 25) != 0) {
// Unexpected failure
LogPrintf("FindMyNotes(): Unexpected runtime error while testing decrypt:\n");
LogPrintf("%s\n", err.what());
} // else
// Couldn't decrypt with this decryptor // Couldn't decrypt with this decryptor
} catch (const std::exception &exc) { } catch (const std::exception &exc) {
// Unexpected failure // Unexpected failure
@ -3562,9 +3557,12 @@ void CWallet::GetFilteredNotes(std::vector<CNotePlaintextEntry> & outEntries, st
outEntries.push_back(CNotePlaintextEntry{jsop, plaintext}); outEntries.push_back(CNotePlaintextEntry{jsop, plaintext});
} catch (const std::exception &) { } catch (const note_decryption_failed &err) {
// Couldn't decrypt with this spending key // Couldn't decrypt with this spending key
throw std::runtime_error(strprintf("Could not decrypt note for payment address %s", CZCPaymentAddress(pa).ToString())); throw std::runtime_error(strprintf("Could not decrypt note for payment address %s", CZCPaymentAddress(pa).ToString()));
} catch (const std::exception &exc) {
// Unexpected failure
throw std::runtime_error(strprintf("Error while decrypting note for payment address %s: %s", CZCPaymentAddress(pa).ToString(), exc.what()));
} }
} }
} }

2
src/zcash/NoteEncryption.cpp

@ -129,7 +129,7 @@ typename NoteDecryption<MLEN>::Plaintext NoteDecryption<MLEN>::decrypt
NULL, NULL,
0, 0,
cipher_nonce, K) != 0) { cipher_nonce, K) != 0) {
throw std::runtime_error("Could not decrypt message"); throw note_decryption_failed();
} }
return plaintext; return plaintext;

5
src/zcash/NoteEncryption.hpp

@ -82,6 +82,11 @@ public:
uint256 random_uint256(); uint256 random_uint256();
uint252 random_uint252(); uint252 random_uint252();
class note_decryption_failed : public std::runtime_error {
public:
note_decryption_failed() : std::runtime_error("Could not decrypt message") { }
};
} }
typedef libzcash::NoteEncryption<ZC_NOTEPLAINTEXT_SIZE> ZCNoteEncryption; typedef libzcash::NoteEncryption<ZC_NOTEPLAINTEXT_SIZE> ZCNoteEncryption;

Loading…
Cancel
Save