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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//! PKCS#7 Padding and Unpadding Module
//!
//! This module provides functionality for applying and removing PKCS#7 padding
//! to and from byte arrays. PKCS#7 padding, defined in the PKCS#7 (Public Key
//! Cryptography Standards #7) standard by RSA Laboratories, is commonly used
//! in various cryptographic algorithms to ensure that data blocks are of a
//! uniform size.
//!
//! The PKCS#7 standard defines a padding scheme that appends a set of bytes to
//! the end of a data block. Each byte of the padding is set to the same value,
//! which is equal to the number of padding bytes added. This module implements
//! two primary functions:
//!
//! - `pkcs7_pad`: Applies PKCS#7 padding to a given byte array (`Vec<u8>`),
//! ensuring that its length is a multiple of a specified block size. The
//! function adds padding in place and is designed to be efficient and
//! straightforward to use.
//!
//! - `pkcs7_unpad`: Removes PKCS#7 padding from a given byte array (`Vec<u8>`),
//! verifying the consistency and correctness of the padding before removal.
//! This function also modifies the data in place and ensures that the
//! unpadding operation is secure and reliable.
//!
//! # Usage
//!
//! The module is designed to be easily integrated into cryptographic
//! applications, particularly those involving block ciphers where data blocks
//! need to be a specific size. The padding and unpadding operations are
//! essential in scenarios where data must be encrypted and decrypted in a
//! manner consistent with PKCS#7 standards.
//!
//! # Examples
//!
//! Basic usage examples demonstrating padding and unpadding a byte array:
//!
//! ```
//! use soft_aes::padding::{pkcs7_pad, pkcs7_unpad};
//!
//! let mut data = vec![0x01, 0x02, 0x03];
//! let block_size = 8;
//! pkcs7_pad(&mut data, block_size).expect("Padding failed");
//!
//! // Data is now padded according to PKCS#7
//! assert_eq!(data, vec![0x01, 0x02, 0x03, 0x05, 0x05, 0x05, 0x05, 0x05]);
//!
//! pkcs7_unpad(&mut data).expect("Unpadding failed");
//!
//! // Data is back to its original form
//! assert_eq!(data, vec![0x01, 0x02, 0x03]);
//! ```
//!
//! # Official Standard Reference
//!
//! - The PKCS#7 padding scheme is defined in the PKCS#7 standard, which is part
//! of the Public Key Cryptography Standards series. The standard is detailed
//! in the document "PKCS #7: Cryptographic Message Syntax Version 1.5",
//! paragraph 10.3. "Content-encryption process":
//! [https://www.rfc-editor.org/rfc/rfc2315](https://www.rfc-editor.org/rfc/rfc2315).
//!
//! # Note
//!
//! - The implementation focuses on clarity and correctness of the PKCS#7
//! standard. It's suitable for educational and straightforward practical
//! applications. For high-performance requirements, further optimizations
//! may be necessary.
use Error;
/// Apply PKCS#7 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, according to the PKCS#7 padding scheme. The
/// padding bytes added are all the same value, equal to the number of bytes
/// added. If the input length is already a multiple of the block size, an
/// entire block of padding is added.
///
/// # Arguments
///
/// * `data` : A mutable reference to the byte array (`Vec<u8>`) to be padded.
/// The data is manipulated directly, adding padding in place.
/// * `block_size` : The block size (`usize`) for padding. Must be greater than
/// 0 and less than 256.
///
/// # Returns
///
/// * `Ok(())` if the padding is successfully applied,
/// * `Err(Box<dyn Error>)` if the block size is invalid (0 or >= 256).
/// Remove PKCS#7 padding from a given byte array, in-place.
///
/// This function inspects and removes the PKCS#7 padding from the provided
/// byte array. It checks the value of the last byte of the array (which
/// indicates the padding size), verifies that the padding is consistent, and
/// then removes the padding bytes.
///
/// # Arguments
///
/// * `data` : A mutable reference to the byte array (`Vec<u8>`) from which
/// padding is to be removed. The data is manipulated directly, with
/// padding bytes being removed in place.
///
/// # Returns
///
/// * `Ok(())` if the unpadding is successfully performed,
/// * `Err(Box<dyn Error>)` if there's an issue with the padding (e.g.,
/// inconsistent padding bytes, invalid padding size, or empty input data).