From 2df795f4b69c1ba6913a96ee49e2c0142ce7750b Mon Sep 17 00:00:00 2001 From: Thomas James Yurek Date: Thu, 12 Dec 2024 10:03:18 -0800 Subject: [PATCH] shuffle around u16/usize length handling stuff --- ipa-core/src/report/hybrid.rs | 26 ++++++++++++++++++-------- ipa-core/src/report/hybrid_info.rs | 10 ++++------ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/ipa-core/src/report/hybrid.rs b/ipa-core/src/report/hybrid.rs index c8cccd184..576c36a60 100644 --- a/ipa-core/src/report/hybrid.rs +++ b/ipa-core/src/report/hybrid.rs @@ -187,6 +187,8 @@ where /// # Errors /// If there is a problem encrypting the report. + /// # Panics + /// If info length + report length does not fit in `u16`. pub fn delimited_encrypt_to( &self, key_id: KeyIdentifier, @@ -195,12 +197,14 @@ where rng: &mut R, out: &mut B, ) -> Result<(), InvalidHybridReportError> { - out.put_u16_le(self.encrypted_len() + info.byte_len()); + out.put_u16_le(self.encrypted_len() + u16::try_from(info.byte_len()).unwrap()); self.encrypt_to(key_id, key_registry, info, rng, out) } /// # Errors /// If there is a problem encrypting the report. + /// # Panics + /// If info length + report length does not fit in `u16`. pub fn encrypt( &self, key_id: KeyIdentifier, @@ -208,9 +212,9 @@ where info: &HybridImpressionInfo, rng: &mut R, ) -> Result, InvalidHybridReportError> { - let mut out = Vec::with_capacity(usize::from(self.encrypted_len() + info.byte_len())); + let mut out = Vec::with_capacity(usize::from(self.encrypted_len() + u16::try_from(info.byte_len()).unwrap())); self.encrypt_to(key_id, key_registry, info, rng, &mut out)?; - debug_assert_eq!(out.len(), usize::from(self.encrypted_len() + info.byte_len())); + debug_assert_eq!(out.len(), usize::from(self.encrypted_len() + u16::try_from(info.byte_len()).unwrap())); Ok(out) } @@ -335,6 +339,8 @@ where /// # Errors /// If there is a problem encrypting the report. + /// # Panics + /// If info length + report length does not fit in `u16`. pub fn delimited_encrypt_to( &self, key_id: KeyIdentifier, @@ -343,12 +349,14 @@ where rng: &mut R, out: &mut B, ) -> Result<(), InvalidHybridReportError> { - out.put_u16_le(self.encrypted_len() + info.byte_len()); + out.put_u16_le(self.encrypted_len() + u16::try_from(info.byte_len()).unwrap()); self.encrypt_to(key_id, key_registry, info, rng, out) } /// # Errors /// If there is a problem encrypting the report. + /// # Panics + /// If info length + report length does not fit in `u16`. pub fn encrypt( &self, key_id: KeyIdentifier, @@ -356,9 +364,9 @@ where info: &HybridConversionInfo, rng: &mut R, ) -> Result, InvalidHybridReportError> { - let mut out = Vec::with_capacity(usize::from(self.ciphertext_len() + info.byte_len())); + let mut out = Vec::with_capacity(usize::from(self.ciphertext_len() + u16::try_from(info.byte_len()).unwrap())); self.encrypt_to(key_id, key_registry, info, rng, &mut out)?; - debug_assert_eq!(out.len(), usize::from(self.encrypted_len() + info.byte_len())); + debug_assert_eq!(out.len(), usize::from(self.encrypted_len() + u16::try_from(info.byte_len()).unwrap())); Ok(out) } @@ -448,6 +456,8 @@ where /// # Errors /// If there is a problem encrypting the report. + /// # Panics + /// If info length + report length does not fit in `u16`. pub fn delimited_encrypt_to( &self, key_id: KeyIdentifier, @@ -458,12 +468,12 @@ where ) -> Result<(), InvalidHybridReportError> { match self { HybridReport::Impression(impression_report) => { - out.put_u16_le(self.encrypted_len() + info.impression.byte_len()); + out.put_u16_le(self.encrypted_len() + u16::try_from(info.impression.byte_len()).unwrap()); out.put_u8(HybridEventType::Impression as u8); impression_report.encrypt_to(key_id, key_registry, &info.impression, rng, out) }, HybridReport::Conversion(conversion_report) => { - out.put_u16_le(self.encrypted_len() + info.conversion.byte_len()); + out.put_u16_le(self.encrypted_len() + u16::try_from(info.conversion.byte_len()).unwrap()); out.put_u8(HybridEventType::Conversion as u8); conversion_report.encrypt_to(key_id, key_registry, &info.conversion, rng, out) }, diff --git a/ipa-core/src/report/hybrid_info.rs b/ipa-core/src/report/hybrid_info.rs index 51ffb71f5..3dfff5b06 100644 --- a/ipa-core/src/report/hybrid_info.rs +++ b/ipa-core/src/report/hybrid_info.rs @@ -20,10 +20,10 @@ impl HybridImpressionInfo { #[must_use] /// # Panics /// If report length does not fit in `u16`. - pub fn byte_len(&self) -> u16 { + pub fn byte_len(&self) -> usize { let out_len = std::mem::size_of_val(&self.key_id); debug_assert_eq!(out_len, self.to_bytes().len(), "Serialization length estimation is incorrect and leads to extra allocation or wasted memory"); - out_len.try_into().unwrap() + out_len } // Converts this instance into an owned byte slice. DO NOT USE AS INPUT TO HPKE @@ -101,9 +101,7 @@ impl HybridConversionInfo { } #[must_use] - /// # Panics - /// If report length does not fit in `u16`. - pub fn byte_len(&self) -> u16 { + pub fn byte_len(&self) -> usize { let out_len = std::mem::size_of_val(&self.key_id) + 1 // delimiter + self.conversion_site_domain.len() @@ -111,7 +109,7 @@ impl HybridConversionInfo { + std::mem::size_of_val(&self.epsilon) + std::mem::size_of_val(&self.sensitivity); debug_assert_eq!(out_len, self.to_bytes().len(), "Serialization length estimation is incorrect and leads to extra allocation or wasted memory"); - out_len.try_into().unwrap() + out_len } // Converts this instance into an owned byte slice. DO NOT USE AS INPUT TO HPKE