ring/aead/overlapping/
partial_block.rs

1// Copyright 2024 Brian Smith.
2//
3// Permission to use, copy, modify, and/or distribute this software for any
4// purpose with or without fee is hereby granted, provided that the above
5// copyright notice and this permission notice appear in all copies.
6//
7// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15use super::Overlapping;
16use crate::error::InputTooLongError;
17
18pub struct PartialBlock<'i, T, const BLOCK_LEN: usize> {
19    // invariant: `self.in_out.len() < BLOCK_LEN`.
20    in_out: Overlapping<'i, T>,
21}
22
23impl<'i, T, const BLOCK_LEN: usize> PartialBlock<'i, T, BLOCK_LEN> {
24    pub fn new(in_out: Overlapping<'i, T>) -> Result<Self, InputTooLongError> {
25        let len = in_out.len();
26        if len >= BLOCK_LEN {
27            return Err(InputTooLongError::new(len));
28        }
29        Ok(Self { in_out })
30    }
31
32    pub fn overwrite_at_start(self, padded: [T; BLOCK_LEN])
33    where
34        T: Copy,
35    {
36        let len = self.len();
37        let output = self.in_out.into_unwritten_output();
38        assert!(output.len() <= padded.len());
39        output.copy_from_slice(&padded[..len]);
40    }
41}
42
43impl<T, const BLOCK_LEN: usize> PartialBlock<'_, T, BLOCK_LEN> {
44    #[inline(always)]
45    pub fn input(&self) -> &[T] {
46        let r = self.in_out.input();
47        // Help the optimizer optimize the caller using the invariant.
48        // TODO: Does this actually help?
49        if r.len() >= BLOCK_LEN {
50            unreachable!()
51        }
52        r
53    }
54
55    #[inline(always)]
56    pub fn len(&self) -> usize {
57        self.input().len()
58    }
59}