Skip to content

Commit

Permalink
shuffle around u16/usize length handling stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
tyurek committed Dec 12, 2024
1 parent e69a1ee commit 2df795f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
26 changes: 18 additions & 8 deletions ipa-core/src/report/hybrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R: CryptoRng + RngCore, B: BufMut>(
&self,
key_id: KeyIdentifier,
Expand All @@ -195,22 +197,24 @@ 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)

Check warning on line 201 in ipa-core/src/report/hybrid.rs

View check run for this annotation

Codecov / codecov/patch

ipa-core/src/report/hybrid.rs#L200-L201

Added lines #L200 - L201 were not covered by tests
}

/// # Errors
/// If there is a problem encrypting the report.
/// # Panics
/// If info length + report length does not fit in `u16`.
pub fn encrypt<R: CryptoRng + RngCore>(
&self,
key_id: KeyIdentifier,
key_registry: &impl PublicKeyRegistry,
info: &HybridImpressionInfo,
rng: &mut R,
) -> Result<Vec<u8>, 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)
}

Expand Down Expand Up @@ -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<R: CryptoRng + RngCore, B: BufMut>(
&self,
key_id: KeyIdentifier,
Expand All @@ -343,22 +349,24 @@ 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)

Check warning on line 353 in ipa-core/src/report/hybrid.rs

View check run for this annotation

Codecov / codecov/patch

ipa-core/src/report/hybrid.rs#L352-L353

Added lines #L352 - L353 were not covered by tests
}

/// # Errors
/// If there is a problem encrypting the report.
/// # Panics
/// If info length + report length does not fit in `u16`.
pub fn encrypt<R: CryptoRng + RngCore>(
&self,
key_id: KeyIdentifier,
key_registry: &impl PublicKeyRegistry,
info: &HybridConversionInfo,
rng: &mut R,
) -> Result<Vec<u8>, 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)
}

Expand Down Expand Up @@ -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<R: CryptoRng + RngCore, B: BufMut>(
&self,
key_id: KeyIdentifier,
Expand All @@ -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)
},
Expand Down
10 changes: 4 additions & 6 deletions ipa-core/src/report/hybrid_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -101,17 +101,15 @@ 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()
+ std::mem::size_of_val(&self.timestamp)
+ 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
Expand Down

0 comments on commit 2df795f

Please sign in to comment.