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

[Decrypt script] In case decryption fails for a record, ignore and continue to decrypt #1270

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
113 changes: 59 additions & 54 deletions ipa-core/src/cli/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
U128Conversions,
},
hpke::{KeyRegistry, PrivateKeyOnly},
report::{EncryptedOprfReport, EventType, OprfReport, DEFAULT_KEY_ID},
report::{EncryptedOprfReport, EventType, InvalidReportError, OprfReport, DEFAULT_KEY_ID},
secret_sharing::IntoShares,
test_fixture::{ipa::TestRawDataRecord, Reconstruct},
};
Expand Down Expand Up @@ -146,16 +146,15 @@ impl DecryptedReports {
}

impl Iterator for DecryptedReports {
type Item = OprfReport<BA8, BA3, BA20>;
type Item = Result<OprfReport<BA8, BA3, BA20>, InvalidReportError>;

fn next(&mut self) -> Option<Self::Item> {
let mut line = String::new();
if self.reader.read_line(&mut line).unwrap() > 0 {
let encrypted_report_bytes = hex::decode(line.trim()).unwrap();
let enc_report =
EncryptedOprfReport::from_bytes(encrypted_report_bytes.as_slice()).unwrap();
let dec_report: OprfReport<BA8, BA3, BA20> =
enc_report.decrypt(&self.key_registry).unwrap();
let dec_report = enc_report.decrypt(&self.key_registry);
Some(dec_report)
} else {
None
Expand All @@ -182,57 +181,63 @@ pub async fn decrypt_and_reconstruct(args: DecryptArgs) -> Result<(), BoxError>
.open(args.output_file)?,
);

for (dec_report1, (dec_report2, dec_report3)) in
decrypted_reports1.zip(decrypted_reports2.zip(decrypted_reports3))
for (idx, (dec_report1, (dec_report2, dec_report3))) in decrypted_reports1
.zip(decrypted_reports2.zip(decrypted_reports3))
.enumerate()
{
let timestamp = [
dec_report1.timestamp,
dec_report2.timestamp,
dec_report3.timestamp,
]
.reconstruct()
.as_u128();

let match_key = [
dec_report1.match_key,
dec_report2.match_key,
dec_report3.match_key,
]
.reconstruct()
.as_u128();

// these aren't reconstucted, so we explictly make sure
// they are consistent across all three files, then set
// it to the first one (without loss of generality)
assert_eq!(dec_report1.event_type, dec_report2.event_type);
assert_eq!(dec_report2.event_type, dec_report3.event_type);
let is_trigger_report = dec_report1.event_type == EventType::Trigger;

let breakdown_key = [
dec_report1.breakdown_key,
dec_report2.breakdown_key,
dec_report3.breakdown_key,
]
.reconstruct()
.as_u128();

let trigger_value = [
dec_report1.trigger_value,
dec_report2.trigger_value,
dec_report3.trigger_value,
]
.reconstruct()
.as_u128();

writeln!(
writer,
"{},{},{},{},{}",
timestamp,
match_key,
u8::from(is_trigger_report),
breakdown_key,
trigger_value,
)?;
match (dec_report1, dec_report2, dec_report3) {
Copy link
Member

Choose a reason for hiding this comment

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

it may make sense to check each for errors explicitly up top, so that we make sure we log all 3. we'll want to make sure that all 3 are not decryptable. the suggestion is just a sketch, don't merge it directly.

Suggested change
match (dec_report1, dec_report2, dec_report3) {
if dec_report1.is_err() { # log here }
if dec_report2.is_err() { # log here }
if dec_report3.is_err() { # log here }
match (dec_report1, dec_report2, dec_report3) {
(Ok(dec_report1), Ok(dec_report2), Ok(dec_report3)) => { ... }
_ => { ... }
}

(Ok(dec_report1), Ok(dec_report2), Ok(dec_report3)) => {
let timestamp = [
dec_report1.timestamp,
dec_report2.timestamp,
dec_report3.timestamp,
]
.reconstruct()
.as_u128();

let match_key = [
dec_report1.match_key,
dec_report2.match_key,
dec_report3.match_key,
]
.reconstruct()
.as_u128();

// these aren't reconstucted, so we explictly make sure
// they are consistent across all three files, then set
// it to the first one (without loss of generality)
assert_eq!(dec_report1.event_type, dec_report2.event_type);
assert_eq!(dec_report2.event_type, dec_report3.event_type);
let is_trigger_report = dec_report1.event_type == EventType::Trigger;

let breakdown_key = [
dec_report1.breakdown_key,
dec_report2.breakdown_key,
dec_report3.breakdown_key,
]
.reconstruct()
.as_u128();

let trigger_value = [
dec_report1.trigger_value,
dec_report2.trigger_value,
dec_report3.trigger_value,
]
.reconstruct()
.as_u128();

writeln!(
writer,
"{},{},{},{},{}",
timestamp,
match_key,
u8::from(is_trigger_report),
breakdown_key,
trigger_value,
)?;
}
_ => println!("Decryption failed for record no {idx}"),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
_ => println!("Decryption failed for record no {idx}"),
_ => eprintln!("Decryption failed for record no {idx}"),

Copy link
Contributor Author

@richajaindce richajaindce Sep 13, 2024

Choose a reason for hiding this comment

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

FYI I ended up not adding a flag for this as this is useful information while decrypting rows @akoshelev

}
}

Ok(())
Expand Down