Skip to main content

securedrop_protocol_minimal/primitives/
pad.rs

1use alloc::vec::Vec;
2
3/// Fixed-length padded message length.
4///
5/// Note: I made this up. We should pick something based on actual reasons.
6pub const PADDED_MESSAGE_LEN: usize = 100000;
7
8/// Pad a message to a fixed length.
9///
10/// # Panics
11///
12/// Panics if `message` is longer than `PADDED_MESSAGE_LEN`. The verification
13/// precondition (`requires`) rules this out, so the panic is provably
14/// unreachable and the length subtraction is safe.
15#[cfg_attr(hax, hax_lib::requires(message.len() <= PADDED_MESSAGE_LEN))]
16pub fn pad_message(message: &[u8]) -> Vec<u8> {
17    if message.len() > PADDED_MESSAGE_LEN {
18        panic!("Message too long for padding");
19    }
20
21    let mut padded = Vec::with_capacity(PADDED_MESSAGE_LEN);
22    padded.extend_from_slice(message);
23
24    // Append zeros to reach the fixed length.
25    let padding = alloc::vec![0u8; PADDED_MESSAGE_LEN - message.len()];
26    padded.extend_from_slice(&padding);
27
28    padded
29}