From afbe29cccfa1b138a1fbfd0b521bb6dddbcbeeeb Mon Sep 17 00:00:00 2001 From: Richa Jain Date: Tue, 10 Sep 2024 21:06:52 +0800 Subject: [PATCH] In case decryption fails, do not error out --- ipa-core/src/cli/crypto.rs | 117 +++++++++++++++++++------------------ 1 file changed, 61 insertions(+), 56 deletions(-) diff --git a/ipa-core/src/cli/crypto.rs b/ipa-core/src/cli/crypto.rs index 69acd1e11..59b454422 100644 --- a/ipa-core/src/cli/crypto.rs +++ b/ipa-core/src/cli/crypto.rs @@ -1,3 +1,6 @@ +use crate::report::InvalidReportError; +use clap::Parser; +use rand::thread_rng; use std::{ fmt::Debug, fs::{read_to_string, File, OpenOptions}, @@ -6,9 +9,6 @@ use std::{ path::PathBuf, }; -use clap::Parser; -use rand::thread_rng; - use crate::{ cli::playbook::{BreakdownKey, InputSource, Timestamp, TriggerValue}, config::{hpke_registry, HpkeServerConfig, KeyRegistries, NetworkConfig}, @@ -146,7 +146,7 @@ impl DecryptedReports { } impl Iterator for DecryptedReports { - type Item = OprfReport; + type Item = Result, InvalidReportError>; fn next(&mut self) -> Option { let mut line = String::new(); @@ -154,8 +154,7 @@ impl Iterator for DecryptedReports { 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 = - enc_report.decrypt(&self.key_registry).unwrap(); + let dec_report = enc_report.decrypt(&self.key_registry); Some(dec_report) } else { None @@ -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) { + (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}"), + } } Ok(())