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

monero: only mask user features on new polyseed, not on decode #503

Merged
merged 4 commits into from
Feb 20, 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
12 changes: 11 additions & 1 deletion coins/monero/src/tests/seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use curve25519_dalek::scalar::Scalar;
use crate::{
hash,
wallet::seed::{
Seed, SeedType,
Seed, SeedType, SeedError,
classic::{self, trim_by_lang},
polyseed,
},
Expand Down Expand Up @@ -405,3 +405,13 @@ fn test_polyseed() {
}
}
}

#[test]
fn test_invalid_polyseed() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd clarify how it's invalid.

// This seed includes unsupported features bits and should error on decode
let seed = "include domain claim resemble urban hire lunch bird \
crucial fire best wife ring warm ignore model"
.into();
let res = Seed::from_string(Zeroizing::new(seed));
assert_eq!(res, Err(SeedError::UnsupportedFeatures));
}
36 changes: 24 additions & 12 deletions coins/monero/src/wallet/seed/polyseed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,31 +217,43 @@ impl Polyseed {
poly
}

/// Create a new `Polyseed` with specific internals.
///
/// `birthday` is defined in seconds since the Unix epoch.
pub fn from(
fn from_internal(
language: Language,
features: u8,
birthday: u64,
masked_features: u8,
encoded_birthday: u16,
entropy: Zeroizing<[u8; 32]>,
) -> Result<Polyseed, SeedError> {
let features = user_features(features);
if !polyseed_features_supported(features) {
if !polyseed_features_supported(masked_features) {
Err(SeedError::UnsupportedFeatures)?;
}

let birthday = birthday_encode(birthday);

if !valid_entropy(&entropy) {
Err(SeedError::InvalidEntropy)?;
}

let mut res = Polyseed { language, birthday, features, entropy, checksum: 0 };
let mut res = Polyseed {
language,
birthday: encoded_birthday,
features: masked_features,
entropy,
checksum: 0,
};
res.checksum = poly_eval(&res.to_poly());
Ok(res)
}

/// Create a new `Polyseed` with specific internals.
///
/// `birthday` is defined in seconds since the Unix epoch.
pub fn from(
language: Language,
features: u8,
birthday: u64,
entropy: Zeroizing<[u8; 32]>,
) -> Result<Polyseed, SeedError> {
Self::from_internal(language, user_features(features), birthday_encode(birthday), entropy)
}

/// Create a new `Polyseed`.
///
/// This uses the system's time for the birthday, if available.
Expand Down Expand Up @@ -375,7 +387,7 @@ impl Polyseed {
let features =
u8::try_from(extra >> DATE_BITS).expect("couldn't convert extra >> DATE_BITS to u8");

let res = Polyseed::from(lang, features, birthday_decode(birthday), entropy);
let res = Self::from_internal(lang, features, birthday, entropy);
if let Ok(res) = res.as_ref() {
debug_assert_eq!(res.checksum, checksum);
}
Expand Down
Loading