Merge pull request #1351

e6264a2 wallet2: fix decrypting a bit too much in authenticated mode (moneromooo-monero)
This commit is contained in:
Riccardo Spagni 2016-11-24 12:52:54 +02:00
commit 2121f3b2f0
No known key found for this signature in database
GPG Key ID: 55432DF31CCD4FCD
1 changed files with 5 additions and 4 deletions

View File

@ -4915,14 +4915,15 @@ std::string wallet2::encrypt_with_view_secret_key(const std::string &plaintext,
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
std::string wallet2::decrypt(const std::string &ciphertext, const crypto::secret_key &skey, bool authenticated) const std::string wallet2::decrypt(const std::string &ciphertext, const crypto::secret_key &skey, bool authenticated) const
{ {
THROW_WALLET_EXCEPTION_IF(ciphertext.size() < sizeof(chacha8_iv), const size_t prefix_size = sizeof(chacha8_iv) + (authenticated ? sizeof(crypto::signature) : 0);
error::wallet_internal_error, "key_image generated ephemeral public key not matched with output_key"); THROW_WALLET_EXCEPTION_IF(ciphertext.size() < prefix_size,
error::wallet_internal_error, "Unexpected ciphertext size");
crypto::chacha8_key key; crypto::chacha8_key key;
crypto::generate_chacha8_key(&skey, sizeof(skey), key); crypto::generate_chacha8_key(&skey, sizeof(skey), key);
const crypto::chacha8_iv &iv = *(const crypto::chacha8_iv*)&ciphertext[0]; const crypto::chacha8_iv &iv = *(const crypto::chacha8_iv*)&ciphertext[0];
std::string plaintext; std::string plaintext;
plaintext.resize(ciphertext.size() - sizeof(iv) - (authenticated ? sizeof(crypto::signature) : 0)); plaintext.resize(ciphertext.size() - prefix_size);
if (authenticated) if (authenticated)
{ {
crypto::hash hash; crypto::hash hash;
@ -4933,7 +4934,7 @@ std::string wallet2::decrypt(const std::string &ciphertext, const crypto::secret
THROW_WALLET_EXCEPTION_IF(!crypto::check_signature(hash, pkey, signature), THROW_WALLET_EXCEPTION_IF(!crypto::check_signature(hash, pkey, signature),
error::wallet_internal_error, "Failed to authenticate criphertext"); error::wallet_internal_error, "Failed to authenticate criphertext");
} }
crypto::chacha8(ciphertext.data() + sizeof(iv), ciphertext.size() - sizeof(iv), key, iv, &plaintext[0]); crypto::chacha8(ciphertext.data() + sizeof(iv), ciphertext.size() - prefix_size, key, iv, &plaintext[0]);
return std::move(plaintext); return std::move(plaintext);
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------