diff --git a/aws-lc-rs/src/io.rs b/aws-lc-rs/src/io.rs index 4f5a4dd91ae..a2bebb26ed8 100644 --- a/aws-lc-rs/src/io.rs +++ b/aws-lc-rs/src/io.rs @@ -8,12 +8,6 @@ #[doc(hidden)] pub mod der; -#[cfg(feature = "alloc")] -mod writer; - -#[cfg(feature = "alloc")] -pub(crate) mod der_writer; - pub(crate) mod positive; pub use self::positive::Positive; diff --git a/aws-lc-rs/src/io/der_writer.rs b/aws-lc-rs/src/io/der_writer.rs deleted file mode 100644 index 8427e58b814..00000000000 --- a/aws-lc-rs/src/io/der_writer.rs +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright 2018 Brian Smith. -// SPDX-License-Identifier: ISC -// Modifications copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 OR ISC - -#[cfg(test)] -use super::{ - der::Tag, - writer::{write_copy, Accumulator, LengthMeasurement, Writer}, - Positive, -}; - -#[cfg(test)] -pub(crate) fn write_positive_integer(output: &mut dyn Accumulator, value: &Positive) { - let first_byte = value.first_byte(); - let value = value.big_endian_without_leading_zero_as_input(); - write_tlv(output, Tag::Integer, |output| { - if (first_byte & 0x80) != 0 { - output.write_byte(0); // Disambiguate negative number. - } - write_copy(output, value); - }); -} - -#[cfg(test)] -pub(crate) fn write_all(tag: Tag, write_value: &dyn Fn(&mut dyn Accumulator)) -> Box<[u8]> { - let length = { - let mut length = LengthMeasurement::zero(); - write_tlv(&mut length, tag, write_value); - length - }; - - let mut output = Writer::with_capacity(&length); - write_tlv(&mut output, tag, write_value); - - output.into() -} - -#[cfg(test)] -#[allow(clippy::cast_possible_truncation)] -fn write_tlv(output: &mut dyn Accumulator, tag: Tag, write_value: F) -where - F: Fn(&mut dyn Accumulator), -{ - let length: usize = { - let mut length = LengthMeasurement::zero(); - write_value(&mut length); - length.into() - }; - - output.write_byte(tag as u8); - if length < 0x80 { - output.write_byte(length as u8); - } else if length < 0x1_00 { - output.write_byte(0x81); - output.write_byte(length as u8); - } else if length < 0x1_00_00 { - output.write_byte(0x82); - output.write_byte((length / 0x1_00) as u8); - output.write_byte(length as u8); - } else { - unreachable!(); - }; - - write_value(output); -} - -#[cfg(test)] -mod tests { - use crate::io::der::Tag; - use crate::io::der_writer::{write_all, write_positive_integer}; - use crate::io::writer::{Accumulator, LengthMeasurement}; - use crate::io::Positive; - use crate::rand::{generate, SystemRandom}; - - const TEST_DATA_SIZE: usize = 100; - #[test] - fn test_write_positive_integer() { - let mut data: [u8; TEST_DATA_SIZE] = generate(&SystemRandom::new()).unwrap().expose(); - data[0] |= 0x80; //negative - let positive = Positive::new_non_empty_without_leading_zeros(untrusted::Input::from(&data)); - let mut length_measurement = LengthMeasurement::zero(); - write_positive_integer(&mut length_measurement, &positive); - let measurement: usize = length_measurement.into(); - assert_eq!(measurement, TEST_DATA_SIZE + 3); - } - - #[test] - #[allow(clippy::cast_possible_truncation)] - fn test_write_all() { - let mut data: [u8; TEST_DATA_SIZE] = generate(&SystemRandom::new()).unwrap().expose(); - data[0] |= 0x80; //negative - let positive = Positive::new_non_empty_without_leading_zeros(untrusted::Input::from(&data)); - let tag = Tag::Integer; - - let func: &dyn Fn(&mut dyn Accumulator) = &|output| { - write_positive_integer(output, &positive); - }; - - let result = write_all(tag, &func); - assert_eq!(TEST_DATA_SIZE + 5, result.len()); - assert_eq!(0x02, result[0]); - assert_eq!((TEST_DATA_SIZE + 3) as u8, result[1]); - assert_eq!(0x02, result[2]); - assert_eq!((TEST_DATA_SIZE + 1) as u8, result[3]); - assert_eq!(0x00, result[4]); - - println!("Result: {} {:?}", result.len(), result.as_ref()); - } -} diff --git a/aws-lc-rs/src/io/writer.rs b/aws-lc-rs/src/io/writer.rs deleted file mode 100644 index 45f310b36f5..00000000000 --- a/aws-lc-rs/src/io/writer.rs +++ /dev/null @@ -1,144 +0,0 @@ -// Copyright 2018 Brian Smith. -// SPDX-License-Identifier: ISC -// Modifications copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 OR ISC - -//! Serialization and deserialization. - -/// Trait for structs that accumulate bytes for subsequent processing. -pub(crate) trait Accumulator { - /// Write a single byte - fn write_byte(&mut self, value: u8); - /// Write a sequence of bytes - fn write_bytes(&mut self, value: &[u8]); -} - -pub(super) struct LengthMeasurement { - len: usize, -} - -impl From for usize { - fn from(len_measurement: LengthMeasurement) -> Self { - len_measurement.len - } -} - -impl LengthMeasurement { - #[cfg(test)] - pub fn zero() -> Self { - Self { len: 0 } - } -} - -impl Accumulator for LengthMeasurement { - fn write_byte(&mut self, _value: u8) { - self.len += 1; - } - fn write_bytes(&mut self, value: &[u8]) { - self.len += value.len(); - } -} - -pub(super) struct Writer { - bytes: Vec, - requested_capacity: usize, -} - -impl Writer { - #[cfg(test)] - pub(super) fn with_capacity(capacity: &LengthMeasurement) -> Self { - Self { - bytes: Vec::with_capacity(capacity.len), - requested_capacity: capacity.len, - } - } -} - -impl From for Box<[u8]> { - fn from(writer: Writer) -> Self { - assert_eq!(writer.requested_capacity, writer.bytes.len()); - writer.bytes.into_boxed_slice() - } -} - -impl Accumulator for Writer { - fn write_byte(&mut self, value: u8) { - self.bytes.push(value); - } - fn write_bytes(&mut self, value: &[u8]) { - self.bytes.extend(value); - } -} - -/// Write bytes from accumulator to `to_copy`. -#[cfg(test)] -pub(crate) fn write_copy(accumulator: &mut dyn Accumulator, to_copy: untrusted::Input) { - accumulator.write_bytes(to_copy.as_slice_less_safe()); -} - -#[cfg(test)] -mod tests { - use crate::io::writer::{write_copy, Accumulator, LengthMeasurement, Writer}; - use crate::rand::{generate, SecureRandom, SystemRandom}; - const TEST_DATA_SIZE: usize = 100; - - #[test] - fn test_writer() { - let mut writer = Writer::with_capacity(&LengthMeasurement { - len: TEST_DATA_SIZE, - }); - - let data = test_accumulator(&mut writer); - - assert_eq!(writer.bytes.as_slice(), data.as_slice()); - } - - fn test_accumulator(accumulator: &mut dyn Accumulator) -> [u8; TEST_DATA_SIZE] { - fn next_u32() -> u32 { - let rng = SystemRandom::default(); - let mut bytes = [0u8; 4]; - rng.fill(&mut bytes).unwrap(); - u32::from_be_bytes(bytes) - } - - let data: [u8; TEST_DATA_SIZE] = generate(&SystemRandom::new()).unwrap().expose(); - - accumulator.write_byte(data[0]); - - let mut index = 1; - while index < TEST_DATA_SIZE { - let next_chunk_size = 1 + (next_u32() % 10) as usize; - let mut next_index = index + next_chunk_size; - if next_index > TEST_DATA_SIZE { - next_index = TEST_DATA_SIZE; - } - accumulator.write_bytes(&data[index..next_index]); - index = next_index; - } - - data - } - - #[test] - fn test_length_measurement() { - let mut length_measurement = LengthMeasurement::zero(); - - let data = test_accumulator(&mut length_measurement); - let acc_len: usize = length_measurement.into(); - - assert_eq!(acc_len, data.len()); - } - - #[test] - fn test_write_copy() { - let mut length_measurement = LengthMeasurement::zero(); - let data: [u8; TEST_DATA_SIZE] = generate(&SystemRandom::new()).unwrap().expose(); - - let input = untrusted::Input::from(&data); - write_copy(&mut length_measurement, input); - - let acc_len: usize = length_measurement.into(); - - assert_eq!(acc_len, data.len()); - } -}