1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//! 0x80 Padding Module (ISO/IEC 9797-1 Padding Method 2)
//!
//! This module provides functionality for applying and removing 0x80 padding
//! to and from byte arrays, aligning with ISO/IEC 9797-1 padding method 2.
//! This padding scheme is commonly used in cryptographic protocols and involves
//! appending a byte with the value 0x80 (binary 1000 0000) followed by as many
//! zero bytes (0x00) as needed to fill the last block to a specified block size.
//!
//! This method ensures that the padded message is always of a length that is a
//! multiple of the block size, which is crucial for block cipher operations in
//! cryptographic algorithms. The distinct 0x80 byte guarantees that the padding
//! is unambiguous, ensuring reliable padding removal.
//!
//! # Features
//!
//! - `pad_80`: Applies 0x80 padding to a byte array, extending its size to a multiple
//! of the specified block size.
//!
//! - `unpad_80`: Removes 0x80 padding from a byte array, reverting it to its original
//! unpadded state.
//!
//! # Usage
//!
//! This padding scheme is particularly useful in cryptographic applications where
//! the input data size must align with the block size of a block cipher algorithm,
//! such as AES. It can be employed in various scenarios, including encryption and
//! message authentication code (MAC) generation.
//!
//! # Examples
//!
//! Basic usage examples demonstrating padding and unpadding a byte array:
//!
//! ```
//! use soft_aes::padding::{pad_80, unpad_80};
//!
//! let mut data = vec![0x01, 0x02, 0x03];
//! let block_size = 8;
//! pad_80(&mut data, block_size).expect("Padding failed");
//!
//! // Data is now padded according to ISO/IEC 9797-1 Padding Method 2
//! assert_eq!(data, vec![0x01, 0x02, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00]);
//!
//! unpad_80(&mut data).expect("Unpadding failed");
//!
//! // Data is back to its original form
//! assert_eq!(data, vec![0x01, 0x02, 0x03]);
//! ```
//!
//! # References
//!
//! - ISO/IEC 9797-1: Information technology – Security techniques – Message Authentication Codes (MACs) –
//! Part 1: Mechanisms using a block cipher
//!
//! # Disclaimer
//!
//! - This implementation focuses on clarity and correctness as per ISO/IEC 9797-1 Padding Method 2.
//! For high-performance requirements, additional optimizations may be necessary.
use Error;
/// Apply 0x80 padding to a given byte array, in-place.
///
/// This function pads the input byte array so that its length is a multiple of
/// the specified block size. The padding starts with a single 0x80 byte followed
/// by 0x00 bytes.
///
/// # Arguments
///
/// * `data` : A mutable reference to the byte array (`Vec<u8>`) to be padded.
/// * `block_size` : The block size (`usize`) for padding.
///
/// # Returns
///
/// * `Ok(())` if the padding is successfully applied.
/// * `Err(Box<dyn Error>)` if the block size is invalid.
/// Remove 0x80 padding from a given byte array, in-place.
///
/// This function removes the 0x80 padding from the provided byte array.
/// It checks for the presence of 0x80 followed by 0x00 bytes and removes them.
///
/// # Arguments
///
/// * `data` : A mutable reference to the byte array (`Vec<u8>`) from which
/// padding is to be removed.
///
/// # Returns
///
/// * `Ok(())` if the unpadding is successfully performed.
/// * `Err(Box<dyn Error>)` if there's an issue with the padding.