MCPcopy Create free account
hub / github.com/RustCrypto/utils / Padding

Interface Padding

block-padding/src/lib.rs:15–81  ·  view source on GitHub ↗

Trait for message padding algorithms.

Source from the content-addressed store, hash-verified

13
14/// Trait for message padding algorithms.
15pub trait Padding: 'static {
16 /// Pads `block` filled with data up to `pos` (i.e the message length
17 /// stored in `block` is equal to `pos`).
18 ///
19 /// # Panics
20 /// If `pos` is bigger than `block.len()`. Most padding algorithms also
21 /// panic if they are equal.
22 fn raw_pad(block: &mut [u8], pos: usize);
23
24 /// Unpad data in `block`.
25 ///
26 /// # Errors
27 /// If the block contains malformed padding.
28 fn raw_unpad(block: &[u8]) -> Result<&[u8], Error>;
29
30 /// Pads `block` filled with data up to `pos` (i.e the message length
31 /// stored in `block` is equal to `pos`).
32 ///
33 /// # Panics
34 /// If `pos` is bigger than `BlockSize`. Most padding algorithms also
35 /// panic if they are equal.
36 #[inline]
37 fn pad<BlockSize: ArraySize>(block: &mut Array<u8, BlockSize>, pos: usize) {
38 Self::raw_pad(block.as_mut_slice(), pos);
39 }
40
41 /// Unpad data in `block`.
42 ///
43 /// # Errors
44 /// If the block contains malformed padding.
45 #[inline]
46 fn unpad<BlockSize: ArraySize>(block: &Array<u8, BlockSize>) -> Result<&[u8], Error> {
47 Self::raw_unpad(block.as_slice())
48 }
49
50 /// Pad message and return padded tail block.
51 ///
52 /// [`PaddedData::Error`] is returned only by [`NoPadding`] if `data` length is not multiple
53 /// of the block size. [`NoPadding`] and [`ZeroPadding`] return [`PaddedData::NoPad`]
54 /// if `data` length is multiple of block size. All other padding implementations
55 /// should always return [`PaddedData::Pad`].
56 #[inline]
57 #[must_use]
58 fn pad_detached<BlockSize: ArraySize>(data: &[u8]) -> PaddedData<'_, BlockSize> {
59 let (blocks, tail) = Array::slice_as_chunks(data);
60 let mut tail_block = Array::default();
61 let pos = tail.len();
62 tail_block[..pos].copy_from_slice(tail);
63 Self::pad(&mut tail_block, pos);
64 PaddedData::Pad { blocks, tail_block }
65 }
66
67 /// Unpad data in `blocks` and return unpadded byte slice.
68 ///
69 /// # Errors
70 /// If `blocks` contain malformed padding.
71 #[inline]
72 fn unpad_blocks<BlockSize: ArraySize>(blocks: &[Array<u8, BlockSize>]) -> Result<&[u8], Error> {

Callers

nothing calls this directly

Implementers 1

lib.rsblock-padding/src/lib.rs

Calls

no outgoing calls

Tested by

no test coverage detected