Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Fix panic when decrypting note with incorrect view-key #241

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Update `bls12_381-bls` dep to 0.4

### Fixed

- Fix panic when attempting to decrypt the note with an incorrect view-key [#240]

## [0.30.0] - 2024-07-03

### Added
Expand Down Expand Up @@ -370,6 +374,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Canonical implementation shielded by feature.

<!-- ISSUES -->
[#240]: https://github.com/dusk-network/phoenix/issues/240
[#222]: https://github.com/dusk-network/phoenix/issues/222
[#214]: https://github.com/dusk-network/phoenix/issues/214
[#208]: https://github.com/dusk-network/phoenix/issues/208
Expand Down
14 changes: 6 additions & 8 deletions core/src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ impl Note {
fn decrypt_value(
&self,
vk: &ViewKey,
) -> Result<(u64, JubJubScalar), BytesError> {
) -> Result<(u64, JubJubScalar), Error> {
let R = self.stealth_address.R();
let shared_secret = dhke(vk.a(), R);

Expand All @@ -249,7 +249,7 @@ impl Note {
match JubJubScalar::from_slice(&dec_plaintext[u64::SIZE..])?.into()
{
Some(scalar) => scalar,
None => return Err(BytesError::InvalidData),
None => return Err(Error::InvalidData),
};

Ok((value, value_blinder))
Expand Down Expand Up @@ -337,10 +337,9 @@ impl Note {
u64::from_slice(&self.value_enc[..u64::SIZE]).unwrap();
Ok(value)
}
(NoteType::Obfuscated, Some(vk)) => self
.decrypt_value(vk)
.map(|(value, _)| value)
.map_err(|_| Error::InvalidEncryption),
(NoteType::Obfuscated, Some(vk)) => {
self.decrypt_value(vk).map(|(value, _)| value)
}
_ => Err(Error::MissingViewKey),
}
}
Expand All @@ -356,8 +355,7 @@ impl Note {
(NoteType::Transparent, _) => Ok(TRANSPARENT_BLINDER),
(NoteType::Obfuscated, Some(vk)) => self
.decrypt_value(vk)
.map(|(_, value_blinder)| value_blinder)
.map_err(|_| Error::InvalidEncryption),
.map(|(_, value_blinder)| value_blinder),
_ => Err(Error::MissingViewKey),
}
}
Expand Down
26 changes: 26 additions & 0 deletions core/tests/note_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,29 @@ fn obfuscated_deterministic_note() -> Result<(), Error> {

Ok(())
}

#[test]
fn note_not_owned() {
let mut rng = StdRng::seed_from_u64(0xc0b);

let owner_pk = PublicKey::from(&SecretKey::random(&mut rng));
let value_blinder = JubJubScalar::random(&mut rng);
let sender_blinder = [
JubJubScalar::random(&mut rng),
JubJubScalar::random(&mut rng),
];

let note = Note::obfuscated(
&mut rng,
&owner_pk,
&owner_pk,
42,
value_blinder,
sender_blinder,
);

let not_owner_sk = SecretKey::random(&mut rng);
let not_owner_vk = ViewKey::from(&not_owner_sk);

assert!(note.value(Some(&not_owner_vk)).is_err());
}