Expand description
Padding and unpadding of messages with random prepended bytes and trailing zeros
This crate provides a padding scheme compatible with block-padding crate
in which random bytes are prepended and zeros are appended.
For a message of length size, a buffer of length
block_size * ((size + 1) / block_size) + 2 * block_size is required for padding. Let pad_len be
(-size - 2) % block_size + 2. Apparently pad_len is the number of bytes to pad the message into
multiple of block_size. The padding scheme appends pad_len + 1 bytes at the front of the
message, where the lower log(block_size) bits of the first byte stores pad_len - 2 and the rest
of the bits in the padding are random. At this point the message needs block_size - 1 more
bytes to form multiple of block_size and we will just pad \0 at the end.
So TxPadding<N> comes with a type parameter N which specify the block size to use which is
essential for unpadding. N must be a power of 2.
use tx_padding::{TxPadding, Padding};
use tx_padding::consts::{U8};
let msg = b"test";
let n = msg.len();
let mut buffer = [0xff; 16];
buffer[..n].copy_from_slice(msg);
let padded_msg = TxPadding::<U8>::pad(&mut buffer, n, 8).unwrap();
assert_eq!(&padded_msg[5..], b"test\x00\x00\x00\x00\x00\x00\x00");
assert_eq!((padded_msg[0] & 0x7) + 2, 4);
assert_eq!(TxPadding::<U8>::unpad(&padded_msg).unwrap(), msg);use tx_padding::{TxPadding, Padding};
use tx_padding::consts::{U8};
let mut buffer = [0xff; 8];
assert!(TxPadding::<U8>::pad(&mut buffer, 5, 8).is_err());pad_block will always return PadError since it is not intended to be called. pad will
return PadError if block_size > 511, block_size mismatch type parameter N or buffer
is not sufficiently large, which is stricter than the requirement of the Padding trait.
Modules§
Structs§
- PadError
- Error for indicating failed padding operation
- Unpad
Error - Error for indicating failed unpadding operation
Enums§
Traits§
- Padding
- Trait for padding messages divided into blocks